Print this page
3515 sd gives RMW warnings for reads
Reviewed by: Albert Lee <trisk@nexenta.com>
Reviewed by: Kevin Crowe <kevin.crowe@nexenta.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/scsi/targets/sd.c
+++ new/usr/src/uts/common/io/scsi/targets/sd.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25 /*
26 - * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 26 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
28 27 * Copyright (c) 2012 by Delphix. All rights reserved.
28 + * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 29 */
30 30 /*
31 31 * Copyright 2011 cyril.galibern@opensvc.com
32 32 */
33 33
34 34 /*
35 35 * SCSI disk target driver.
36 36 */
37 37 #include <sys/scsi/scsi.h>
38 38 #include <sys/dkbad.h>
39 39 #include <sys/dklabel.h>
40 40 #include <sys/dkio.h>
41 41 #include <sys/fdio.h>
42 42 #include <sys/cdio.h>
43 43 #include <sys/mhd.h>
44 44 #include <sys/vtoc.h>
45 45 #include <sys/dktp/fdisk.h>
46 46 #include <sys/kstat.h>
47 47 #include <sys/vtrace.h>
48 48 #include <sys/note.h>
49 49 #include <sys/thread.h>
50 50 #include <sys/proc.h>
51 51 #include <sys/efi_partition.h>
52 52 #include <sys/var.h>
53 53 #include <sys/aio_req.h>
54 54
55 55 #ifdef __lock_lint
56 56 #define _LP64
57 57 #define __amd64
58 58 #endif
59 59
60 60 #if (defined(__fibre))
61 61 /* Note: is there a leadville version of the following? */
62 62 #include <sys/fc4/fcal_linkapp.h>
63 63 #endif
64 64 #include <sys/taskq.h>
65 65 #include <sys/uuid.h>
66 66 #include <sys/byteorder.h>
67 67 #include <sys/sdt.h>
68 68
69 69 #include "sd_xbuf.h"
70 70
71 71 #include <sys/scsi/targets/sddef.h>
72 72 #include <sys/cmlb.h>
73 73 #include <sys/sysevent/eventdefs.h>
74 74 #include <sys/sysevent/dev.h>
75 75
76 76 #include <sys/fm/protocol.h>
77 77
78 78 /*
79 79 * Loadable module info.
80 80 */
81 81 #if (defined(__fibre))
82 82 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver"
83 83 #else /* !__fibre */
84 84 #define SD_MODULE_NAME "SCSI Disk Driver"
85 85 #endif /* !__fibre */
86 86
87 87 /*
88 88 * Define the interconnect type, to allow the driver to distinguish
89 89 * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
90 90 *
91 91 * This is really for backward compatibility. In the future, the driver
92 92 * should actually check the "interconnect-type" property as reported by
93 93 * the HBA; however at present this property is not defined by all HBAs,
94 94 * so we will use this #define (1) to permit the driver to run in
95 95 * backward-compatibility mode; and (2) to print a notification message
96 96 * if an FC HBA does not support the "interconnect-type" property. The
97 97 * behavior of the driver will be to assume parallel SCSI behaviors unless
98 98 * the "interconnect-type" property is defined by the HBA **AND** has a
99 99 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
100 100 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
101 101 * Channel behaviors (as per the old ssd). (Note that the
102 102 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
103 103 * will result in the driver assuming parallel SCSI behaviors.)
104 104 *
105 105 * (see common/sys/scsi/impl/services.h)
106 106 *
107 107 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
108 108 * since some FC HBAs may already support that, and there is some code in
109 109 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the
110 110 * default would confuse that code, and besides things should work fine
111 111 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
112 112 * "interconnect_type" property.
113 113 *
114 114 */
115 115 #if (defined(__fibre))
116 116 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE
117 117 #else
118 118 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL
119 119 #endif
120 120
121 121 /*
122 122 * The name of the driver, established from the module name in _init.
123 123 */
124 124 static char *sd_label = NULL;
125 125
126 126 /*
127 127 * Driver name is unfortunately prefixed on some driver.conf properties.
128 128 */
129 129 #if (defined(__fibre))
130 130 #define sd_max_xfer_size ssd_max_xfer_size
131 131 #define sd_config_list ssd_config_list
132 132 static char *sd_max_xfer_size = "ssd_max_xfer_size";
133 133 static char *sd_config_list = "ssd-config-list";
134 134 #else
135 135 static char *sd_max_xfer_size = "sd_max_xfer_size";
136 136 static char *sd_config_list = "sd-config-list";
137 137 #endif
138 138
139 139 /*
140 140 * Driver global variables
141 141 */
142 142
143 143 #if (defined(__fibre))
144 144 /*
145 145 * These #defines are to avoid namespace collisions that occur because this
146 146 * code is currently used to compile two separate driver modules: sd and ssd.
147 147 * All global variables need to be treated this way (even if declared static)
148 148 * in order to allow the debugger to resolve the names properly.
149 149 * It is anticipated that in the near future the ssd module will be obsoleted,
150 150 * at which time this namespace issue should go away.
151 151 */
152 152 #define sd_state ssd_state
153 153 #define sd_io_time ssd_io_time
154 154 #define sd_failfast_enable ssd_failfast_enable
155 155 #define sd_ua_retry_count ssd_ua_retry_count
156 156 #define sd_report_pfa ssd_report_pfa
157 157 #define sd_max_throttle ssd_max_throttle
158 158 #define sd_min_throttle ssd_min_throttle
159 159 #define sd_rot_delay ssd_rot_delay
160 160
161 161 #define sd_retry_on_reservation_conflict \
162 162 ssd_retry_on_reservation_conflict
163 163 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay
164 164 #define sd_resv_conflict_name ssd_resv_conflict_name
165 165
166 166 #define sd_component_mask ssd_component_mask
167 167 #define sd_level_mask ssd_level_mask
168 168 #define sd_debug_un ssd_debug_un
169 169 #define sd_error_level ssd_error_level
170 170
171 171 #define sd_xbuf_active_limit ssd_xbuf_active_limit
172 172 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit
173 173
174 174 #define sd_tr ssd_tr
175 175 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout
176 176 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout
177 177 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable
178 178 #define sd_check_media_time ssd_check_media_time
179 179 #define sd_wait_cmds_complete ssd_wait_cmds_complete
180 180 #define sd_label_mutex ssd_label_mutex
181 181 #define sd_detach_mutex ssd_detach_mutex
182 182 #define sd_log_buf ssd_log_buf
183 183 #define sd_log_mutex ssd_log_mutex
184 184
185 185 #define sd_disk_table ssd_disk_table
186 186 #define sd_disk_table_size ssd_disk_table_size
187 187 #define sd_sense_mutex ssd_sense_mutex
188 188 #define sd_cdbtab ssd_cdbtab
189 189
190 190 #define sd_cb_ops ssd_cb_ops
191 191 #define sd_ops ssd_ops
192 192 #define sd_additional_codes ssd_additional_codes
193 193 #define sd_tgops ssd_tgops
194 194
195 195 #define sd_minor_data ssd_minor_data
196 196 #define sd_minor_data_efi ssd_minor_data_efi
197 197
198 198 #define sd_tq ssd_tq
199 199 #define sd_wmr_tq ssd_wmr_tq
200 200 #define sd_taskq_name ssd_taskq_name
201 201 #define sd_wmr_taskq_name ssd_wmr_taskq_name
202 202 #define sd_taskq_minalloc ssd_taskq_minalloc
203 203 #define sd_taskq_maxalloc ssd_taskq_maxalloc
204 204
205 205 #define sd_dump_format_string ssd_dump_format_string
206 206
207 207 #define sd_iostart_chain ssd_iostart_chain
208 208 #define sd_iodone_chain ssd_iodone_chain
209 209
210 210 #define sd_pm_idletime ssd_pm_idletime
211 211
212 212 #define sd_force_pm_supported ssd_force_pm_supported
213 213
214 214 #define sd_dtype_optical_bind ssd_dtype_optical_bind
215 215
216 216 #define sd_ssc_init ssd_ssc_init
217 217 #define sd_ssc_send ssd_ssc_send
218 218 #define sd_ssc_fini ssd_ssc_fini
219 219 #define sd_ssc_assessment ssd_ssc_assessment
220 220 #define sd_ssc_post ssd_ssc_post
221 221 #define sd_ssc_print ssd_ssc_print
222 222 #define sd_ssc_ereport_post ssd_ssc_ereport_post
223 223 #define sd_ssc_set_info ssd_ssc_set_info
224 224 #define sd_ssc_extract_info ssd_ssc_extract_info
225 225
226 226 #endif
227 227
228 228 #ifdef SDDEBUG
229 229 int sd_force_pm_supported = 0;
230 230 #endif /* SDDEBUG */
231 231
232 232 void *sd_state = NULL;
233 233 int sd_io_time = SD_IO_TIME;
234 234 int sd_failfast_enable = 1;
235 235 int sd_ua_retry_count = SD_UA_RETRY_COUNT;
236 236 int sd_report_pfa = 1;
237 237 int sd_max_throttle = SD_MAX_THROTTLE;
238 238 int sd_min_throttle = SD_MIN_THROTTLE;
239 239 int sd_rot_delay = 4; /* Default 4ms Rotation delay */
240 240 int sd_qfull_throttle_enable = TRUE;
241 241
242 242 int sd_retry_on_reservation_conflict = 1;
243 243 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
244 244 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
245 245
246 246 static int sd_dtype_optical_bind = -1;
247 247
248 248 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
249 249 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict";
250 250
251 251 /*
252 252 * Global data for debug logging. To enable debug printing, sd_component_mask
253 253 * and sd_level_mask should be set to the desired bit patterns as outlined in
254 254 * sddef.h.
255 255 */
256 256 uint_t sd_component_mask = 0x0;
257 257 uint_t sd_level_mask = 0x0;
258 258 struct sd_lun *sd_debug_un = NULL;
259 259 uint_t sd_error_level = SCSI_ERR_RETRYABLE;
260 260
261 261 /* Note: these may go away in the future... */
262 262 static uint32_t sd_xbuf_active_limit = 512;
263 263 static uint32_t sd_xbuf_reserve_limit = 16;
264 264
265 265 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
266 266
267 267 /*
268 268 * Timer value used to reset the throttle after it has been reduced
269 269 * (typically in response to TRAN_BUSY or STATUS_QFULL)
270 270 */
271 271 static int sd_reset_throttle_timeout = SD_RESET_THROTTLE_TIMEOUT;
272 272 static int sd_qfull_throttle_timeout = SD_QFULL_THROTTLE_TIMEOUT;
273 273
274 274 /*
275 275 * Interval value associated with the media change scsi watch.
276 276 */
277 277 static int sd_check_media_time = 3000000;
278 278
279 279 /*
280 280 * Wait value used for in progress operations during a DDI_SUSPEND
281 281 */
282 282 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE;
283 283
284 284 /*
285 285 * sd_label_mutex protects a static buffer used in the disk label
286 286 * component of the driver
287 287 */
288 288 static kmutex_t sd_label_mutex;
289 289
290 290 /*
291 291 * sd_detach_mutex protects un_layer_count, un_detach_count, and
292 292 * un_opens_in_progress in the sd_lun structure.
293 293 */
294 294 static kmutex_t sd_detach_mutex;
295 295
296 296 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
297 297 sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
298 298
299 299 /*
300 300 * Global buffer and mutex for debug logging
301 301 */
302 302 static char sd_log_buf[1024];
303 303 static kmutex_t sd_log_mutex;
304 304
305 305 /*
306 306 * Structs and globals for recording attached lun information.
307 307 * This maintains a chain. Each node in the chain represents a SCSI controller.
308 308 * The structure records the number of luns attached to each target connected
309 309 * with the controller.
310 310 * For parallel scsi device only.
311 311 */
312 312 struct sd_scsi_hba_tgt_lun {
313 313 struct sd_scsi_hba_tgt_lun *next;
314 314 dev_info_t *pdip;
315 315 int nlun[NTARGETS_WIDE];
316 316 };
317 317
318 318 /*
319 319 * Flag to indicate the lun is attached or detached
320 320 */
321 321 #define SD_SCSI_LUN_ATTACH 0
322 322 #define SD_SCSI_LUN_DETACH 1
323 323
324 324 static kmutex_t sd_scsi_target_lun_mutex;
325 325 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL;
326 326
327 327 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
328 328 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
329 329
330 330 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
331 331 sd_scsi_target_lun_head))
332 332
333 333 /*
334 334 * "Smart" Probe Caching structs, globals, #defines, etc.
335 335 * For parallel scsi and non-self-identify device only.
336 336 */
337 337
338 338 /*
339 339 * The following resources and routines are implemented to support
340 340 * "smart" probing, which caches the scsi_probe() results in an array,
341 341 * in order to help avoid long probe times.
342 342 */
343 343 struct sd_scsi_probe_cache {
344 344 struct sd_scsi_probe_cache *next;
345 345 dev_info_t *pdip;
346 346 int cache[NTARGETS_WIDE];
347 347 };
348 348
349 349 static kmutex_t sd_scsi_probe_cache_mutex;
350 350 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
351 351
352 352 /*
353 353 * Really we only need protection on the head of the linked list, but
354 354 * better safe than sorry.
355 355 */
356 356 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
357 357 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
358 358
359 359 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
360 360 sd_scsi_probe_cache_head))
361 361
362 362 /*
363 363 * Power attribute table
364 364 */
365 365 static sd_power_attr_ss sd_pwr_ss = {
366 366 { "NAME=spindle-motor", "0=off", "1=on", NULL },
367 367 {0, 100},
368 368 {30, 0},
369 369 {20000, 0}
370 370 };
371 371
372 372 static sd_power_attr_pc sd_pwr_pc = {
373 373 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
374 374 "3=active", NULL },
375 375 {0, 0, 0, 100},
376 376 {90, 90, 20, 0},
377 377 {15000, 15000, 1000, 0}
378 378 };
379 379
380 380 /*
381 381 * Power level to power condition
382 382 */
383 383 static int sd_pl2pc[] = {
384 384 SD_TARGET_START_VALID,
385 385 SD_TARGET_STANDBY,
386 386 SD_TARGET_IDLE,
387 387 SD_TARGET_ACTIVE
388 388 };
389 389
390 390 /*
391 391 * Vendor specific data name property declarations
392 392 */
393 393
394 394 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
395 395
396 396 static sd_tunables seagate_properties = {
397 397 SEAGATE_THROTTLE_VALUE,
398 398 0,
399 399 0,
400 400 0,
401 401 0,
402 402 0,
403 403 0,
404 404 0,
405 405 0
406 406 };
407 407
408 408
409 409 static sd_tunables fujitsu_properties = {
410 410 FUJITSU_THROTTLE_VALUE,
411 411 0,
412 412 0,
413 413 0,
414 414 0,
415 415 0,
416 416 0,
417 417 0,
418 418 0
419 419 };
420 420
421 421 static sd_tunables ibm_properties = {
422 422 IBM_THROTTLE_VALUE,
423 423 0,
424 424 0,
425 425 0,
426 426 0,
427 427 0,
428 428 0,
429 429 0,
430 430 0
431 431 };
432 432
433 433 static sd_tunables purple_properties = {
434 434 PURPLE_THROTTLE_VALUE,
435 435 0,
436 436 0,
437 437 PURPLE_BUSY_RETRIES,
438 438 PURPLE_RESET_RETRY_COUNT,
439 439 PURPLE_RESERVE_RELEASE_TIME,
440 440 0,
441 441 0,
442 442 0
443 443 };
444 444
445 445 static sd_tunables sve_properties = {
446 446 SVE_THROTTLE_VALUE,
447 447 0,
448 448 0,
449 449 SVE_BUSY_RETRIES,
450 450 SVE_RESET_RETRY_COUNT,
451 451 SVE_RESERVE_RELEASE_TIME,
452 452 SVE_MIN_THROTTLE_VALUE,
453 453 SVE_DISKSORT_DISABLED_FLAG,
454 454 0
455 455 };
456 456
457 457 static sd_tunables maserati_properties = {
458 458 0,
459 459 0,
460 460 0,
461 461 0,
462 462 0,
463 463 0,
464 464 0,
465 465 MASERATI_DISKSORT_DISABLED_FLAG,
466 466 MASERATI_LUN_RESET_ENABLED_FLAG
467 467 };
468 468
469 469 static sd_tunables pirus_properties = {
470 470 PIRUS_THROTTLE_VALUE,
471 471 0,
472 472 PIRUS_NRR_COUNT,
473 473 PIRUS_BUSY_RETRIES,
474 474 PIRUS_RESET_RETRY_COUNT,
475 475 0,
476 476 PIRUS_MIN_THROTTLE_VALUE,
477 477 PIRUS_DISKSORT_DISABLED_FLAG,
478 478 PIRUS_LUN_RESET_ENABLED_FLAG
479 479 };
480 480
481 481 #endif
482 482
483 483 #if (defined(__sparc) && !defined(__fibre)) || \
484 484 (defined(__i386) || defined(__amd64))
485 485
486 486
487 487 static sd_tunables elite_properties = {
488 488 ELITE_THROTTLE_VALUE,
489 489 0,
490 490 0,
491 491 0,
492 492 0,
493 493 0,
494 494 0,
495 495 0,
496 496 0
497 497 };
498 498
499 499 static sd_tunables st31200n_properties = {
500 500 ST31200N_THROTTLE_VALUE,
501 501 0,
502 502 0,
503 503 0,
504 504 0,
505 505 0,
506 506 0,
507 507 0,
508 508 0
509 509 };
510 510
511 511 #endif /* Fibre or not */
512 512
513 513 static sd_tunables lsi_properties_scsi = {
514 514 LSI_THROTTLE_VALUE,
515 515 0,
516 516 LSI_NOTREADY_RETRIES,
517 517 0,
518 518 0,
519 519 0,
520 520 0,
521 521 0,
522 522 0
523 523 };
524 524
525 525 static sd_tunables symbios_properties = {
526 526 SYMBIOS_THROTTLE_VALUE,
527 527 0,
528 528 SYMBIOS_NOTREADY_RETRIES,
529 529 0,
530 530 0,
531 531 0,
532 532 0,
533 533 0,
534 534 0
535 535 };
536 536
537 537 static sd_tunables lsi_properties = {
538 538 0,
539 539 0,
540 540 LSI_NOTREADY_RETRIES,
541 541 0,
542 542 0,
543 543 0,
544 544 0,
545 545 0,
546 546 0
547 547 };
548 548
549 549 static sd_tunables lsi_oem_properties = {
550 550 0,
551 551 0,
552 552 LSI_OEM_NOTREADY_RETRIES,
553 553 0,
554 554 0,
555 555 0,
556 556 0,
557 557 0,
558 558 0,
559 559 1
560 560 };
561 561
562 562
563 563
564 564 #if (defined(SD_PROP_TST))
565 565
566 566 #define SD_TST_CTYPE_VAL CTYPE_CDROM
567 567 #define SD_TST_THROTTLE_VAL 16
568 568 #define SD_TST_NOTREADY_VAL 12
569 569 #define SD_TST_BUSY_VAL 60
570 570 #define SD_TST_RST_RETRY_VAL 36
571 571 #define SD_TST_RSV_REL_TIME 60
572 572
573 573 static sd_tunables tst_properties = {
574 574 SD_TST_THROTTLE_VAL,
575 575 SD_TST_CTYPE_VAL,
576 576 SD_TST_NOTREADY_VAL,
577 577 SD_TST_BUSY_VAL,
578 578 SD_TST_RST_RETRY_VAL,
579 579 SD_TST_RSV_REL_TIME,
580 580 0,
581 581 0,
582 582 0
583 583 };
584 584 #endif
585 585
586 586 /* This is similar to the ANSI toupper implementation */
587 587 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
588 588
589 589 /*
590 590 * Static Driver Configuration Table
591 591 *
592 592 * This is the table of disks which need throttle adjustment (or, perhaps
593 593 * something else as defined by the flags at a future time.) device_id
594 594 * is a string consisting of concatenated vid (vendor), pid (product/model)
595 595 * and revision strings as defined in the scsi_inquiry structure. Offsets of
596 596 * the parts of the string are as defined by the sizes in the scsi_inquiry
597 597 * structure. Device type is searched as far as the device_id string is
598 598 * defined. Flags defines which values are to be set in the driver from the
599 599 * properties list.
600 600 *
601 601 * Entries below which begin and end with a "*" are a special case.
602 602 * These do not have a specific vendor, and the string which follows
603 603 * can appear anywhere in the 16 byte PID portion of the inquiry data.
604 604 *
605 605 * Entries below which begin and end with a " " (blank) are a special
606 606 * case. The comparison function will treat multiple consecutive blanks
607 607 * as equivalent to a single blank. For example, this causes a
608 608 * sd_disk_table entry of " NEC CDROM " to match a device's id string
609 609 * of "NEC CDROM".
610 610 *
611 611 * Note: The MD21 controller type has been obsoleted.
612 612 * ST318202F is a Legacy device
613 613 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
614 614 * made with an FC connection. The entries here are a legacy.
615 615 */
616 616 static sd_disk_config_t sd_disk_table[] = {
617 617 #if defined(__fibre) || defined(__i386) || defined(__amd64)
618 618 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 619 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 620 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 621 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 622 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 623 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 624 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 625 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 626 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 627 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628 628 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629 629 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630 630 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631 631 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
632 632 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
633 633 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 634 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 635 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 636 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637 637 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638 638 { "FUJITSU MAM3182FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
639 639 { "FUJITSU MAM3364FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
640 640 { "FUJITSU MAM3738FC", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
641 641 { "IBM DDYFT1835", SD_CONF_BSET_THROTTLE, &ibm_properties },
642 642 { "IBM DDYFT3695", SD_CONF_BSET_THROTTLE, &ibm_properties },
643 643 { "IBM IC35LF2D2", SD_CONF_BSET_THROTTLE, &ibm_properties },
644 644 { "IBM IC35LF2PR", SD_CONF_BSET_THROTTLE, &ibm_properties },
645 645 { "IBM 1724-100", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 646 { "IBM 1726-2xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 647 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 648 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 649 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 650 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 651 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 652 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 653 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 654 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 655 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 656 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 657 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 658 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 659 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 660 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 661 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 662 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 663 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664 664 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665 665 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666 666 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
667 667 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT |
668 668 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
669 669 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT |
670 670 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
671 671 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
672 672 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties },
673 673 { "SUN T3", SD_CONF_BSET_THROTTLE |
674 674 SD_CONF_BSET_BSY_RETRY_COUNT|
675 675 SD_CONF_BSET_RST_RETRIES|
676 676 SD_CONF_BSET_RSV_REL_TIME,
677 677 &purple_properties },
678 678 { "SUN SESS01", SD_CONF_BSET_THROTTLE |
679 679 SD_CONF_BSET_BSY_RETRY_COUNT|
680 680 SD_CONF_BSET_RST_RETRIES|
681 681 SD_CONF_BSET_RSV_REL_TIME|
682 682 SD_CONF_BSET_MIN_THROTTLE|
683 683 SD_CONF_BSET_DISKSORT_DISABLED,
684 684 &sve_properties },
685 685 { "SUN T4", SD_CONF_BSET_THROTTLE |
686 686 SD_CONF_BSET_BSY_RETRY_COUNT|
687 687 SD_CONF_BSET_RST_RETRIES|
688 688 SD_CONF_BSET_RSV_REL_TIME,
689 689 &purple_properties },
690 690 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
691 691 SD_CONF_BSET_LUN_RESET_ENABLED,
692 692 &maserati_properties },
693 693 { "SUN SE6920", SD_CONF_BSET_THROTTLE |
694 694 SD_CONF_BSET_NRR_COUNT|
695 695 SD_CONF_BSET_BSY_RETRY_COUNT|
696 696 SD_CONF_BSET_RST_RETRIES|
697 697 SD_CONF_BSET_MIN_THROTTLE|
698 698 SD_CONF_BSET_DISKSORT_DISABLED|
699 699 SD_CONF_BSET_LUN_RESET_ENABLED,
700 700 &pirus_properties },
701 701 { "SUN SE6940", SD_CONF_BSET_THROTTLE |
702 702 SD_CONF_BSET_NRR_COUNT|
703 703 SD_CONF_BSET_BSY_RETRY_COUNT|
704 704 SD_CONF_BSET_RST_RETRIES|
705 705 SD_CONF_BSET_MIN_THROTTLE|
706 706 SD_CONF_BSET_DISKSORT_DISABLED|
707 707 SD_CONF_BSET_LUN_RESET_ENABLED,
708 708 &pirus_properties },
709 709 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE |
710 710 SD_CONF_BSET_NRR_COUNT|
711 711 SD_CONF_BSET_BSY_RETRY_COUNT|
712 712 SD_CONF_BSET_RST_RETRIES|
713 713 SD_CONF_BSET_MIN_THROTTLE|
714 714 SD_CONF_BSET_DISKSORT_DISABLED|
715 715 SD_CONF_BSET_LUN_RESET_ENABLED,
716 716 &pirus_properties },
717 717 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE |
718 718 SD_CONF_BSET_NRR_COUNT|
719 719 SD_CONF_BSET_BSY_RETRY_COUNT|
720 720 SD_CONF_BSET_RST_RETRIES|
721 721 SD_CONF_BSET_MIN_THROTTLE|
722 722 SD_CONF_BSET_DISKSORT_DISABLED|
723 723 SD_CONF_BSET_LUN_RESET_ENABLED,
724 724 &pirus_properties },
725 725 { "SUN PSX1000", SD_CONF_BSET_THROTTLE |
726 726 SD_CONF_BSET_NRR_COUNT|
727 727 SD_CONF_BSET_BSY_RETRY_COUNT|
728 728 SD_CONF_BSET_RST_RETRIES|
729 729 SD_CONF_BSET_MIN_THROTTLE|
730 730 SD_CONF_BSET_DISKSORT_DISABLED|
731 731 SD_CONF_BSET_LUN_RESET_ENABLED,
732 732 &pirus_properties },
733 733 { "SUN SE6330", SD_CONF_BSET_THROTTLE |
734 734 SD_CONF_BSET_NRR_COUNT|
735 735 SD_CONF_BSET_BSY_RETRY_COUNT|
736 736 SD_CONF_BSET_RST_RETRIES|
737 737 SD_CONF_BSET_MIN_THROTTLE|
738 738 SD_CONF_BSET_DISKSORT_DISABLED|
739 739 SD_CONF_BSET_LUN_RESET_ENABLED,
740 740 &pirus_properties },
741 741 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
742 742 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
743 743 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
744 744 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745 745 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746 746 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
747 747 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
748 748 #endif /* fibre or NON-sparc platforms */
749 749 #if ((defined(__sparc) && !defined(__fibre)) ||\
750 750 (defined(__i386) || defined(__amd64)))
751 751 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
752 752 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
753 753 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
754 754 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL },
755 755 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
756 756 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
757 757 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
758 758 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
759 759 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
760 760 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
761 761 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
762 762 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL },
763 763 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
764 764 &symbios_properties },
765 765 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
766 766 &lsi_properties_scsi },
767 767 #if defined(__i386) || defined(__amd64)
768 768 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
769 769 | SD_CONF_BSET_READSUB_BCD
770 770 | SD_CONF_BSET_READ_TOC_ADDR_BCD
771 771 | SD_CONF_BSET_NO_READ_HEADER
772 772 | SD_CONF_BSET_READ_CD_XD4), NULL },
773 773
774 774 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
775 775 | SD_CONF_BSET_READSUB_BCD
776 776 | SD_CONF_BSET_READ_TOC_ADDR_BCD
777 777 | SD_CONF_BSET_NO_READ_HEADER
778 778 | SD_CONF_BSET_READ_CD_XD4), NULL },
779 779 #endif /* __i386 || __amd64 */
780 780 #endif /* sparc NON-fibre or NON-sparc platforms */
781 781
782 782 #if (defined(SD_PROP_TST))
783 783 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE
784 784 | SD_CONF_BSET_CTYPE
785 785 | SD_CONF_BSET_NRR_COUNT
786 786 | SD_CONF_BSET_FAB_DEVID
787 787 | SD_CONF_BSET_NOCACHE
788 788 | SD_CONF_BSET_BSY_RETRY_COUNT
789 789 | SD_CONF_BSET_PLAYMSF_BCD
790 790 | SD_CONF_BSET_READSUB_BCD
791 791 | SD_CONF_BSET_READ_TOC_TRK_BCD
792 792 | SD_CONF_BSET_READ_TOC_ADDR_BCD
793 793 | SD_CONF_BSET_NO_READ_HEADER
794 794 | SD_CONF_BSET_READ_CD_XD4
795 795 | SD_CONF_BSET_RST_RETRIES
796 796 | SD_CONF_BSET_RSV_REL_TIME
797 797 | SD_CONF_BSET_TUR_CHECK), &tst_properties},
798 798 #endif
799 799 };
800 800
801 801 static const int sd_disk_table_size =
802 802 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
803 803
804 804 /*
805 805 * Emulation mode disk drive VID/PID table
806 806 */
807 807 static char sd_flash_dev_table[][25] = {
808 808 "ATA MARVELL SD88SA02",
809 809 "MARVELL SD88SA02",
810 810 "TOSHIBA THNSNV05",
811 811 };
812 812
813 813 static const int sd_flash_dev_table_size =
814 814 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
815 815
816 816 #define SD_INTERCONNECT_PARALLEL 0
817 817 #define SD_INTERCONNECT_FABRIC 1
818 818 #define SD_INTERCONNECT_FIBRE 2
819 819 #define SD_INTERCONNECT_SSA 3
820 820 #define SD_INTERCONNECT_SATA 4
821 821 #define SD_INTERCONNECT_SAS 5
822 822
823 823 #define SD_IS_PARALLEL_SCSI(un) \
824 824 ((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
825 825 #define SD_IS_SERIAL(un) \
826 826 (((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
827 827 ((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
828 828
829 829 /*
830 830 * Definitions used by device id registration routines
831 831 */
832 832 #define VPD_HEAD_OFFSET 3 /* size of head for vpd page */
833 833 #define VPD_PAGE_LENGTH 3 /* offset for pge length data */
834 834 #define VPD_MODE_PAGE 1 /* offset into vpd pg for "page code" */
835 835
836 836 static kmutex_t sd_sense_mutex = {0};
837 837
838 838 /*
839 839 * Macros for updates of the driver state
840 840 */
841 841 #define New_state(un, s) \
842 842 (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
843 843 #define Restore_state(un) \
844 844 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
845 845
846 846 static struct sd_cdbinfo sd_cdbtab[] = {
847 847 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, },
848 848 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, },
849 849 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, },
850 850 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
851 851 };
852 852
853 853 /*
854 854 * Specifies the number of seconds that must have elapsed since the last
855 855 * cmd. has completed for a device to be declared idle to the PM framework.
856 856 */
857 857 static int sd_pm_idletime = 1;
858 858
859 859 /*
860 860 * Internal function prototypes
861 861 */
862 862
863 863 #if (defined(__fibre))
864 864 /*
865 865 * These #defines are to avoid namespace collisions that occur because this
866 866 * code is currently used to compile two separate driver modules: sd and ssd.
867 867 * All function names need to be treated this way (even if declared static)
868 868 * in order to allow the debugger to resolve the names properly.
869 869 * It is anticipated that in the near future the ssd module will be obsoleted,
870 870 * at which time this ugliness should go away.
871 871 */
872 872 #define sd_log_trace ssd_log_trace
873 873 #define sd_log_info ssd_log_info
874 874 #define sd_log_err ssd_log_err
875 875 #define sdprobe ssdprobe
876 876 #define sdinfo ssdinfo
877 877 #define sd_prop_op ssd_prop_op
878 878 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init
879 879 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini
880 880 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache
881 881 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache
882 882 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init
883 883 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini
884 884 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count
885 885 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target
886 886 #define sd_spin_up_unit ssd_spin_up_unit
887 887 #define sd_enable_descr_sense ssd_enable_descr_sense
888 888 #define sd_reenable_dsense_task ssd_reenable_dsense_task
889 889 #define sd_set_mmc_caps ssd_set_mmc_caps
890 890 #define sd_read_unit_properties ssd_read_unit_properties
891 891 #define sd_process_sdconf_file ssd_process_sdconf_file
892 892 #define sd_process_sdconf_table ssd_process_sdconf_table
893 893 #define sd_sdconf_id_match ssd_sdconf_id_match
894 894 #define sd_blank_cmp ssd_blank_cmp
895 895 #define sd_chk_vers1_data ssd_chk_vers1_data
896 896 #define sd_set_vers1_properties ssd_set_vers1_properties
897 897 #define sd_check_solid_state ssd_check_solid_state
898 898 #define sd_check_emulation_mode ssd_check_emulation_mode
899 899
900 900 #define sd_get_physical_geometry ssd_get_physical_geometry
901 901 #define sd_get_virtual_geometry ssd_get_virtual_geometry
902 902 #define sd_update_block_info ssd_update_block_info
903 903 #define sd_register_devid ssd_register_devid
904 904 #define sd_get_devid ssd_get_devid
905 905 #define sd_create_devid ssd_create_devid
906 906 #define sd_write_deviceid ssd_write_deviceid
907 907 #define sd_check_vpd_page_support ssd_check_vpd_page_support
908 908 #define sd_setup_pm ssd_setup_pm
909 909 #define sd_create_pm_components ssd_create_pm_components
910 910 #define sd_ddi_suspend ssd_ddi_suspend
911 911 #define sd_ddi_resume ssd_ddi_resume
912 912 #define sd_pm_state_change ssd_pm_state_change
913 913 #define sdpower ssdpower
914 914 #define sdattach ssdattach
915 915 #define sddetach ssddetach
916 916 #define sd_unit_attach ssd_unit_attach
917 917 #define sd_unit_detach ssd_unit_detach
918 918 #define sd_set_unit_attributes ssd_set_unit_attributes
919 919 #define sd_create_errstats ssd_create_errstats
920 920 #define sd_set_errstats ssd_set_errstats
921 921 #define sd_set_pstats ssd_set_pstats
922 922 #define sddump ssddump
923 923 #define sd_scsi_poll ssd_scsi_poll
924 924 #define sd_send_polled_RQS ssd_send_polled_RQS
925 925 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll
926 926 #define sd_init_event_callbacks ssd_init_event_callbacks
927 927 #define sd_event_callback ssd_event_callback
928 928 #define sd_cache_control ssd_cache_control
929 929 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled
930 930 #define sd_get_nv_sup ssd_get_nv_sup
931 931 #define sd_make_device ssd_make_device
932 932 #define sdopen ssdopen
933 933 #define sdclose ssdclose
934 934 #define sd_ready_and_valid ssd_ready_and_valid
935 935 #define sdmin ssdmin
936 936 #define sdread ssdread
937 937 #define sdwrite ssdwrite
938 938 #define sdaread ssdaread
939 939 #define sdawrite ssdawrite
940 940 #define sdstrategy ssdstrategy
941 941 #define sdioctl ssdioctl
942 942 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart
943 943 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart
944 944 #define sd_checksum_iostart ssd_checksum_iostart
945 945 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart
946 946 #define sd_pm_iostart ssd_pm_iostart
947 947 #define sd_core_iostart ssd_core_iostart
948 948 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone
949 949 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone
950 950 #define sd_checksum_iodone ssd_checksum_iodone
951 951 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone
952 952 #define sd_pm_iodone ssd_pm_iodone
953 953 #define sd_initpkt_for_buf ssd_initpkt_for_buf
954 954 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf
955 955 #define sd_setup_rw_pkt ssd_setup_rw_pkt
956 956 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt
957 957 #define sd_buf_iodone ssd_buf_iodone
958 958 #define sd_uscsi_strategy ssd_uscsi_strategy
959 959 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi
960 960 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi
961 961 #define sd_uscsi_iodone ssd_uscsi_iodone
962 962 #define sd_xbuf_strategy ssd_xbuf_strategy
963 963 #define sd_xbuf_init ssd_xbuf_init
964 964 #define sd_pm_entry ssd_pm_entry
965 965 #define sd_pm_exit ssd_pm_exit
966 966
967 967 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler
968 968 #define sd_pm_timeout_handler ssd_pm_timeout_handler
969 969
970 970 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq
971 971 #define sdintr ssdintr
972 972 #define sd_start_cmds ssd_start_cmds
973 973 #define sd_send_scsi_cmd ssd_send_scsi_cmd
974 974 #define sd_bioclone_alloc ssd_bioclone_alloc
975 975 #define sd_bioclone_free ssd_bioclone_free
976 976 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc
977 977 #define sd_shadow_buf_free ssd_shadow_buf_free
978 978 #define sd_print_transport_rejected_message \
979 979 ssd_print_transport_rejected_message
980 980 #define sd_retry_command ssd_retry_command
981 981 #define sd_set_retry_bp ssd_set_retry_bp
982 982 #define sd_send_request_sense_command ssd_send_request_sense_command
983 983 #define sd_start_retry_command ssd_start_retry_command
984 984 #define sd_start_direct_priority_command \
985 985 ssd_start_direct_priority_command
986 986 #define sd_return_failed_command ssd_return_failed_command
987 987 #define sd_return_failed_command_no_restart \
988 988 ssd_return_failed_command_no_restart
989 989 #define sd_return_command ssd_return_command
990 990 #define sd_sync_with_callback ssd_sync_with_callback
991 991 #define sdrunout ssdrunout
992 992 #define sd_mark_rqs_busy ssd_mark_rqs_busy
993 993 #define sd_mark_rqs_idle ssd_mark_rqs_idle
994 994 #define sd_reduce_throttle ssd_reduce_throttle
995 995 #define sd_restore_throttle ssd_restore_throttle
996 996 #define sd_print_incomplete_msg ssd_print_incomplete_msg
997 997 #define sd_init_cdb_limits ssd_init_cdb_limits
998 998 #define sd_pkt_status_good ssd_pkt_status_good
999 999 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition
1000 1000 #define sd_pkt_status_busy ssd_pkt_status_busy
1001 1001 #define sd_pkt_status_reservation_conflict \
1002 1002 ssd_pkt_status_reservation_conflict
1003 1003 #define sd_pkt_status_qfull ssd_pkt_status_qfull
1004 1004 #define sd_handle_request_sense ssd_handle_request_sense
1005 1005 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense
1006 1006 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg
1007 1007 #define sd_validate_sense_data ssd_validate_sense_data
1008 1008 #define sd_decode_sense ssd_decode_sense
1009 1009 #define sd_print_sense_msg ssd_print_sense_msg
1010 1010 #define sd_sense_key_no_sense ssd_sense_key_no_sense
1011 1011 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error
1012 1012 #define sd_sense_key_not_ready ssd_sense_key_not_ready
1013 1013 #define sd_sense_key_medium_or_hardware_error \
1014 1014 ssd_sense_key_medium_or_hardware_error
1015 1015 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request
1016 1016 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention
1017 1017 #define sd_sense_key_fail_command ssd_sense_key_fail_command
1018 1018 #define sd_sense_key_blank_check ssd_sense_key_blank_check
1019 1019 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command
1020 1020 #define sd_sense_key_default ssd_sense_key_default
1021 1021 #define sd_print_retry_msg ssd_print_retry_msg
1022 1022 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg
1023 1023 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete
1024 1024 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err
1025 1025 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset
1026 1026 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted
1027 1027 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout
1028 1028 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free
1029 1029 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject
1030 1030 #define sd_pkt_reason_default ssd_pkt_reason_default
1031 1031 #define sd_reset_target ssd_reset_target
1032 1032 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback
1033 1033 #define sd_start_stop_unit_task ssd_start_stop_unit_task
1034 1034 #define sd_taskq_create ssd_taskq_create
1035 1035 #define sd_taskq_delete ssd_taskq_delete
1036 1036 #define sd_target_change_task ssd_target_change_task
1037 1037 #define sd_log_dev_status_event ssd_log_dev_status_event
1038 1038 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event
1039 1039 #define sd_log_eject_request_event ssd_log_eject_request_event
1040 1040 #define sd_media_change_task ssd_media_change_task
1041 1041 #define sd_handle_mchange ssd_handle_mchange
1042 1042 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK
1043 1043 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY
1044 1044 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16
1045 1045 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION
1046 1046 #define sd_send_scsi_feature_GET_CONFIGURATION \
1047 1047 sd_send_scsi_feature_GET_CONFIGURATION
1048 1048 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT
1049 1049 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY
1050 1050 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY
1051 1051 #define sd_send_scsi_PERSISTENT_RESERVE_IN \
1052 1052 ssd_send_scsi_PERSISTENT_RESERVE_IN
1053 1053 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \
1054 1054 ssd_send_scsi_PERSISTENT_RESERVE_OUT
1055 1055 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE
1056 1056 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \
1057 1057 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1058 1058 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE
1059 1059 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT
1060 1060 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR
1061 1061 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE
1062 1062 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION \
1063 1063 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1064 1064 #define sd_gesn_media_data_valid ssd_gesn_media_data_valid
1065 1065 #define sd_alloc_rqs ssd_alloc_rqs
1066 1066 #define sd_free_rqs ssd_free_rqs
1067 1067 #define sd_dump_memory ssd_dump_memory
1068 1068 #define sd_get_media_info_com ssd_get_media_info_com
1069 1069 #define sd_get_media_info ssd_get_media_info
1070 1070 #define sd_get_media_info_ext ssd_get_media_info_ext
1071 1071 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info
1072 1072 #define sd_nvpair_str_decode ssd_nvpair_str_decode
1073 1073 #define sd_strtok_r ssd_strtok_r
1074 1074 #define sd_set_properties ssd_set_properties
1075 1075 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf
1076 1076 #define sd_setup_next_xfer ssd_setup_next_xfer
1077 1077 #define sd_dkio_get_temp ssd_dkio_get_temp
1078 1078 #define sd_check_mhd ssd_check_mhd
1079 1079 #define sd_mhd_watch_cb ssd_mhd_watch_cb
1080 1080 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete
1081 1081 #define sd_sname ssd_sname
1082 1082 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover
1083 1083 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread
1084 1084 #define sd_take_ownership ssd_take_ownership
1085 1085 #define sd_reserve_release ssd_reserve_release
1086 1086 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req
1087 1087 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb
1088 1088 #define sd_persistent_reservation_in_read_keys \
1089 1089 ssd_persistent_reservation_in_read_keys
1090 1090 #define sd_persistent_reservation_in_read_resv \
1091 1091 ssd_persistent_reservation_in_read_resv
1092 1092 #define sd_mhdioc_takeown ssd_mhdioc_takeown
1093 1093 #define sd_mhdioc_failfast ssd_mhdioc_failfast
1094 1094 #define sd_mhdioc_release ssd_mhdioc_release
1095 1095 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid
1096 1096 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys
1097 1097 #define sd_mhdioc_inresv ssd_mhdioc_inresv
1098 1098 #define sr_change_blkmode ssr_change_blkmode
1099 1099 #define sr_change_speed ssr_change_speed
1100 1100 #define sr_atapi_change_speed ssr_atapi_change_speed
1101 1101 #define sr_pause_resume ssr_pause_resume
1102 1102 #define sr_play_msf ssr_play_msf
1103 1103 #define sr_play_trkind ssr_play_trkind
1104 1104 #define sr_read_all_subcodes ssr_read_all_subcodes
1105 1105 #define sr_read_subchannel ssr_read_subchannel
1106 1106 #define sr_read_tocentry ssr_read_tocentry
1107 1107 #define sr_read_tochdr ssr_read_tochdr
1108 1108 #define sr_read_cdda ssr_read_cdda
1109 1109 #define sr_read_cdxa ssr_read_cdxa
1110 1110 #define sr_read_mode1 ssr_read_mode1
1111 1111 #define sr_read_mode2 ssr_read_mode2
1112 1112 #define sr_read_cd_mode2 ssr_read_cd_mode2
1113 1113 #define sr_sector_mode ssr_sector_mode
1114 1114 #define sr_eject ssr_eject
1115 1115 #define sr_ejected ssr_ejected
1116 1116 #define sr_check_wp ssr_check_wp
1117 1117 #define sd_watch_request_submit ssd_watch_request_submit
1118 1118 #define sd_check_media ssd_check_media
1119 1119 #define sd_media_watch_cb ssd_media_watch_cb
1120 1120 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast
1121 1121 #define sr_volume_ctrl ssr_volume_ctrl
1122 1122 #define sr_read_sony_session_offset ssr_read_sony_session_offset
1123 1123 #define sd_log_page_supported ssd_log_page_supported
1124 1124 #define sd_check_for_writable_cd ssd_check_for_writable_cd
1125 1125 #define sd_wm_cache_constructor ssd_wm_cache_constructor
1126 1126 #define sd_wm_cache_destructor ssd_wm_cache_destructor
1127 1127 #define sd_range_lock ssd_range_lock
1128 1128 #define sd_get_range ssd_get_range
1129 1129 #define sd_free_inlist_wmap ssd_free_inlist_wmap
1130 1130 #define sd_range_unlock ssd_range_unlock
1131 1131 #define sd_read_modify_write_task ssd_read_modify_write_task
1132 1132 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw
1133 1133
1134 1134 #define sd_iostart_chain ssd_iostart_chain
1135 1135 #define sd_iodone_chain ssd_iodone_chain
1136 1136 #define sd_initpkt_map ssd_initpkt_map
1137 1137 #define sd_destroypkt_map ssd_destroypkt_map
1138 1138 #define sd_chain_type_map ssd_chain_type_map
1139 1139 #define sd_chain_index_map ssd_chain_index_map
1140 1140
1141 1141 #define sd_failfast_flushctl ssd_failfast_flushctl
1142 1142 #define sd_failfast_flushq ssd_failfast_flushq
1143 1143 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback
1144 1144
1145 1145 #define sd_is_lsi ssd_is_lsi
1146 1146 #define sd_tg_rdwr ssd_tg_rdwr
1147 1147 #define sd_tg_getinfo ssd_tg_getinfo
1148 1148 #define sd_rmw_msg_print_handler ssd_rmw_msg_print_handler
1149 1149
1150 1150 #endif /* #if (defined(__fibre)) */
1151 1151
1152 1152
1153 1153 int _init(void);
1154 1154 int _fini(void);
1155 1155 int _info(struct modinfo *modinfop);
1156 1156
1157 1157 /*PRINTFLIKE3*/
1158 1158 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1159 1159 /*PRINTFLIKE3*/
1160 1160 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1161 1161 /*PRINTFLIKE3*/
1162 1162 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1163 1163
1164 1164 static int sdprobe(dev_info_t *devi);
1165 1165 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1166 1166 void **result);
1167 1167 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1168 1168 int mod_flags, char *name, caddr_t valuep, int *lengthp);
1169 1169
1170 1170 /*
1171 1171 * Smart probe for parallel scsi
1172 1172 */
1173 1173 static void sd_scsi_probe_cache_init(void);
1174 1174 static void sd_scsi_probe_cache_fini(void);
1175 1175 static void sd_scsi_clear_probe_cache(void);
1176 1176 static int sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1177 1177
1178 1178 /*
1179 1179 * Attached luns on target for parallel scsi
1180 1180 */
1181 1181 static void sd_scsi_target_lun_init(void);
1182 1182 static void sd_scsi_target_lun_fini(void);
1183 1183 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1184 1184 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1185 1185
1186 1186 static int sd_spin_up_unit(sd_ssc_t *ssc);
1187 1187
1188 1188 /*
1189 1189 * Using sd_ssc_init to establish sd_ssc_t struct
1190 1190 * Using sd_ssc_send to send uscsi internal command
1191 1191 * Using sd_ssc_fini to free sd_ssc_t struct
1192 1192 */
1193 1193 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1194 1194 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1195 1195 int flag, enum uio_seg dataspace, int path_flag);
1196 1196 static void sd_ssc_fini(sd_ssc_t *ssc);
1197 1197
1198 1198 /*
1199 1199 * Using sd_ssc_assessment to set correct type-of-assessment
1200 1200 * Using sd_ssc_post to post ereport & system log
1201 1201 * sd_ssc_post will call sd_ssc_print to print system log
1202 1202 * sd_ssc_post will call sd_ssd_ereport_post to post ereport
1203 1203 */
1204 1204 static void sd_ssc_assessment(sd_ssc_t *ssc,
1205 1205 enum sd_type_assessment tp_assess);
1206 1206
1207 1207 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1208 1208 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1209 1209 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1210 1210 enum sd_driver_assessment drv_assess);
1211 1211
1212 1212 /*
1213 1213 * Using sd_ssc_set_info to mark an un-decodable-data error.
1214 1214 * Using sd_ssc_extract_info to transfer information from internal
1215 1215 * data structures to sd_ssc_t.
1216 1216 */
1217 1217 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1218 1218 const char *fmt, ...);
1219 1219 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1220 1220 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1221 1221
1222 1222 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1223 1223 enum uio_seg dataspace, int path_flag);
1224 1224
1225 1225 #ifdef _LP64
1226 1226 static void sd_enable_descr_sense(sd_ssc_t *ssc);
1227 1227 static void sd_reenable_dsense_task(void *arg);
1228 1228 #endif /* _LP64 */
1229 1229
1230 1230 static void sd_set_mmc_caps(sd_ssc_t *ssc);
1231 1231
1232 1232 static void sd_read_unit_properties(struct sd_lun *un);
1233 1233 static int sd_process_sdconf_file(struct sd_lun *un);
1234 1234 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1235 1235 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1236 1236 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1237 1237 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1238 1238 int *data_list, sd_tunables *values);
1239 1239 static void sd_process_sdconf_table(struct sd_lun *un);
1240 1240 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1241 1241 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1242 1242 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1243 1243 int list_len, char *dataname_ptr);
1244 1244 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1245 1245 sd_tunables *prop_list);
1246 1246
1247 1247 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1248 1248 int reservation_flag);
1249 1249 static int sd_get_devid(sd_ssc_t *ssc);
1250 1250 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1251 1251 static int sd_write_deviceid(sd_ssc_t *ssc);
1252 1252 static int sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1253 1253 static int sd_check_vpd_page_support(sd_ssc_t *ssc);
1254 1254
1255 1255 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1256 1256 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1257 1257
1258 1258 static int sd_ddi_suspend(dev_info_t *devi);
1259 1259 static int sd_ddi_resume(dev_info_t *devi);
1260 1260 static int sd_pm_state_change(struct sd_lun *un, int level, int flag);
1261 1261 static int sdpower(dev_info_t *devi, int component, int level);
1262 1262
1263 1263 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1264 1264 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1265 1265 static int sd_unit_attach(dev_info_t *devi);
1266 1266 static int sd_unit_detach(dev_info_t *devi);
1267 1267
1268 1268 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1269 1269 static void sd_create_errstats(struct sd_lun *un, int instance);
1270 1270 static void sd_set_errstats(struct sd_lun *un);
1271 1271 static void sd_set_pstats(struct sd_lun *un);
1272 1272
1273 1273 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1274 1274 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1275 1275 static int sd_send_polled_RQS(struct sd_lun *un);
1276 1276 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1277 1277
1278 1278 #if (defined(__fibre))
1279 1279 /*
1280 1280 * Event callbacks (photon)
1281 1281 */
1282 1282 static void sd_init_event_callbacks(struct sd_lun *un);
1283 1283 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1284 1284 #endif
1285 1285
1286 1286 /*
1287 1287 * Defines for sd_cache_control
1288 1288 */
1289 1289
1290 1290 #define SD_CACHE_ENABLE 1
1291 1291 #define SD_CACHE_DISABLE 0
1292 1292 #define SD_CACHE_NOCHANGE -1
1293 1293
1294 1294 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1295 1295 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1296 1296 static void sd_get_nv_sup(sd_ssc_t *ssc);
1297 1297 static dev_t sd_make_device(dev_info_t *devi);
1298 1298 static void sd_check_solid_state(sd_ssc_t *ssc);
1299 1299 static void sd_check_emulation_mode(sd_ssc_t *ssc);
1300 1300 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1301 1301 uint64_t capacity);
1302 1302
1303 1303 /*
1304 1304 * Driver entry point functions.
1305 1305 */
1306 1306 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1307 1307 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1308 1308 static int sd_ready_and_valid(sd_ssc_t *ssc, int part);
1309 1309
1310 1310 static void sdmin(struct buf *bp);
1311 1311 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1312 1312 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1313 1313 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1314 1314 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1315 1315
1316 1316 static int sdstrategy(struct buf *bp);
1317 1317 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1318 1318
1319 1319 /*
1320 1320 * Function prototypes for layering functions in the iostart chain.
1321 1321 */
1322 1322 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1323 1323 struct buf *bp);
1324 1324 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1325 1325 struct buf *bp);
1326 1326 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1327 1327 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1328 1328 struct buf *bp);
1329 1329 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1330 1330 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1331 1331
1332 1332 /*
1333 1333 * Function prototypes for layering functions in the iodone chain.
1334 1334 */
1335 1335 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1336 1336 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1337 1337 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1338 1338 struct buf *bp);
1339 1339 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1340 1340 struct buf *bp);
1341 1341 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1342 1342 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1343 1343 struct buf *bp);
1344 1344 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1345 1345
1346 1346 /*
1347 1347 * Prototypes for functions to support buf(9S) based IO.
1348 1348 */
1349 1349 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1350 1350 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1351 1351 static void sd_destroypkt_for_buf(struct buf *);
1352 1352 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1353 1353 struct buf *bp, int flags,
1354 1354 int (*callback)(caddr_t), caddr_t callback_arg,
1355 1355 diskaddr_t lba, uint32_t blockcount);
1356 1356 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1357 1357 struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1358 1358
1359 1359 /*
1360 1360 * Prototypes for functions to support USCSI IO.
1361 1361 */
1362 1362 static int sd_uscsi_strategy(struct buf *bp);
1363 1363 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1364 1364 static void sd_destroypkt_for_uscsi(struct buf *);
1365 1365
1366 1366 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1367 1367 uchar_t chain_type, void *pktinfop);
1368 1368
1369 1369 static int sd_pm_entry(struct sd_lun *un);
1370 1370 static void sd_pm_exit(struct sd_lun *un);
1371 1371
1372 1372 static void sd_pm_idletimeout_handler(void *arg);
1373 1373
1374 1374 /*
1375 1375 * sd_core internal functions (used at the sd_core_io layer).
1376 1376 */
1377 1377 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1378 1378 static void sdintr(struct scsi_pkt *pktp);
1379 1379 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1380 1380
1381 1381 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1382 1382 enum uio_seg dataspace, int path_flag);
1383 1383
1384 1384 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1385 1385 daddr_t blkno, int (*func)(struct buf *));
1386 1386 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1387 1387 uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1388 1388 static void sd_bioclone_free(struct buf *bp);
1389 1389 static void sd_shadow_buf_free(struct buf *bp);
1390 1390
1391 1391 static void sd_print_transport_rejected_message(struct sd_lun *un,
1392 1392 struct sd_xbuf *xp, int code);
1393 1393 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1394 1394 void *arg, int code);
1395 1395 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1396 1396 void *arg, int code);
1397 1397 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1398 1398 void *arg, int code);
1399 1399
1400 1400 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1401 1401 int retry_check_flag,
1402 1402 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1403 1403 int c),
1404 1404 void *user_arg, int failure_code, clock_t retry_delay,
1405 1405 void (*statp)(kstat_io_t *));
1406 1406
1407 1407 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1408 1408 clock_t retry_delay, void (*statp)(kstat_io_t *));
1409 1409
1410 1410 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1411 1411 struct scsi_pkt *pktp);
1412 1412 static void sd_start_retry_command(void *arg);
1413 1413 static void sd_start_direct_priority_command(void *arg);
1414 1414 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1415 1415 int errcode);
1416 1416 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1417 1417 struct buf *bp, int errcode);
1418 1418 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1419 1419 static void sd_sync_with_callback(struct sd_lun *un);
1420 1420 static int sdrunout(caddr_t arg);
1421 1421
1422 1422 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1423 1423 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1424 1424
1425 1425 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1426 1426 static void sd_restore_throttle(void *arg);
1427 1427
1428 1428 static void sd_init_cdb_limits(struct sd_lun *un);
1429 1429
1430 1430 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1431 1431 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1432 1432
1433 1433 /*
1434 1434 * Error handling functions
1435 1435 */
1436 1436 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1437 1437 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1438 1438 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1439 1439 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1440 1440 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1441 1441 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1442 1442 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1443 1443 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1444 1444
1445 1445 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1446 1446 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1447 1447 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1448 1448 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1449 1449 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1450 1450 struct sd_xbuf *xp, size_t actual_len);
1451 1451 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1452 1452 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1453 1453
1454 1454 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1455 1455 void *arg, int code);
1456 1456
1457 1457 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1458 1458 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1459 1459 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1460 1460 uint8_t *sense_datap,
1461 1461 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1462 1462 static void sd_sense_key_not_ready(struct sd_lun *un,
1463 1463 uint8_t *sense_datap,
1464 1464 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465 1465 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1466 1466 uint8_t *sense_datap,
1467 1467 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1468 1468 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1469 1469 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1470 1470 static void sd_sense_key_unit_attention(struct sd_lun *un,
1471 1471 uint8_t *sense_datap,
1472 1472 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1473 1473 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1474 1474 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1475 1475 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1476 1476 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1477 1477 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1478 1478 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1479 1479 static void sd_sense_key_default(struct sd_lun *un,
1480 1480 uint8_t *sense_datap,
1481 1481 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1482 1482
1483 1483 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1484 1484 void *arg, int flag);
1485 1485
1486 1486 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1487 1487 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1488 1488 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1489 1489 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1490 1490 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1491 1491 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1492 1492 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1493 1493 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1494 1494 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1495 1495 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1496 1496 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1497 1497 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1498 1498 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1499 1499 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1500 1500 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1501 1501 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1502 1502
1503 1503 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1504 1504
1505 1505 static void sd_start_stop_unit_callback(void *arg);
1506 1506 static void sd_start_stop_unit_task(void *arg);
1507 1507
1508 1508 static void sd_taskq_create(void);
1509 1509 static void sd_taskq_delete(void);
1510 1510 static void sd_target_change_task(void *arg);
1511 1511 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1512 1512 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1513 1513 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1514 1514 static void sd_media_change_task(void *arg);
1515 1515
1516 1516 static int sd_handle_mchange(struct sd_lun *un);
1517 1517 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1518 1518 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1519 1519 uint32_t *lbap, int path_flag);
1520 1520 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1521 1521 uint32_t *lbap, uint32_t *psp, int path_flag);
1522 1522 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1523 1523 int flag, int path_flag);
1524 1524 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1525 1525 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1526 1526 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1527 1527 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1528 1528 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1529 1529 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1530 1530 uchar_t usr_cmd, uchar_t *usr_bufp);
1531 1531 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1532 1532 struct dk_callback *dkc);
1533 1533 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1534 1534 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1535 1535 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1536 1536 uchar_t *bufaddr, uint_t buflen, int path_flag);
1537 1537 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1538 1538 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1539 1539 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1540 1540 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1541 1541 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1542 1542 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1543 1543 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1544 1544 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1545 1545 size_t buflen, daddr_t start_block, int path_flag);
1546 1546 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1547 1547 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1548 1548 path_flag)
1549 1549 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1550 1550 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1551 1551 path_flag)
1552 1552
1553 1553 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1554 1554 uint16_t buflen, uchar_t page_code, uchar_t page_control,
1555 1555 uint16_t param_ptr, int path_flag);
1556 1556 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1557 1557 uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1558 1558 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1559 1559
1560 1560 static int sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1561 1561 static void sd_free_rqs(struct sd_lun *un);
1562 1562
1563 1563 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1564 1564 uchar_t *data, int len, int fmt);
1565 1565 static void sd_panic_for_res_conflict(struct sd_lun *un);
1566 1566
1567 1567 /*
1568 1568 * Disk Ioctl Function Prototypes
1569 1569 */
1570 1570 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1571 1571 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1572 1572 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1573 1573 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1574 1574
1575 1575 /*
1576 1576 * Multi-host Ioctl Prototypes
1577 1577 */
1578 1578 static int sd_check_mhd(dev_t dev, int interval);
1579 1579 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1580 1580 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1581 1581 static char *sd_sname(uchar_t status);
1582 1582 static void sd_mhd_resvd_recover(void *arg);
1583 1583 static void sd_resv_reclaim_thread();
1584 1584 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1585 1585 static int sd_reserve_release(dev_t dev, int cmd);
1586 1586 static void sd_rmv_resv_reclaim_req(dev_t dev);
1587 1587 static void sd_mhd_reset_notify_cb(caddr_t arg);
1588 1588 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1589 1589 mhioc_inkeys_t *usrp, int flag);
1590 1590 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1591 1591 mhioc_inresvs_t *usrp, int flag);
1592 1592 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1593 1593 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1594 1594 static int sd_mhdioc_release(dev_t dev);
1595 1595 static int sd_mhdioc_register_devid(dev_t dev);
1596 1596 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1597 1597 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1598 1598
1599 1599 /*
1600 1600 * SCSI removable prototypes
1601 1601 */
1602 1602 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1603 1603 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1604 1604 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1605 1605 static int sr_pause_resume(dev_t dev, int mode);
1606 1606 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1607 1607 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1608 1608 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1609 1609 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1610 1610 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1611 1611 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1612 1612 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1613 1613 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1614 1614 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1615 1615 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1616 1616 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1617 1617 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1618 1618 static int sr_eject(dev_t dev);
1619 1619 static void sr_ejected(register struct sd_lun *un);
1620 1620 static int sr_check_wp(dev_t dev);
1621 1621 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1622 1622 static int sd_check_media(dev_t dev, enum dkio_state state);
1623 1623 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1624 1624 static void sd_delayed_cv_broadcast(void *arg);
1625 1625 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1626 1626 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1627 1627
1628 1628 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1629 1629
1630 1630 /*
1631 1631 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1632 1632 */
1633 1633 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1634 1634 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1635 1635 static void sd_wm_cache_destructor(void *wm, void *un);
1636 1636 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1637 1637 daddr_t endb, ushort_t typ);
1638 1638 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1639 1639 daddr_t endb);
1640 1640 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1641 1641 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1642 1642 static void sd_read_modify_write_task(void * arg);
1643 1643 static int
1644 1644 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1645 1645 struct buf **bpp);
1646 1646
1647 1647
1648 1648 /*
1649 1649 * Function prototypes for failfast support.
1650 1650 */
1651 1651 static void sd_failfast_flushq(struct sd_lun *un);
1652 1652 static int sd_failfast_flushq_callback(struct buf *bp);
1653 1653
1654 1654 /*
1655 1655 * Function prototypes to check for lsi devices
1656 1656 */
1657 1657 static void sd_is_lsi(struct sd_lun *un);
1658 1658
1659 1659 /*
1660 1660 * Function prototypes for partial DMA support
1661 1661 */
1662 1662 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1663 1663 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1664 1664
1665 1665
1666 1666 /* Function prototypes for cmlb */
1667 1667 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1668 1668 diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1669 1669
1670 1670 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1671 1671
1672 1672 /*
1673 1673 * For printing RMW warning message timely
1674 1674 */
1675 1675 static void sd_rmw_msg_print_handler(void *arg);
1676 1676
1677 1677 /*
1678 1678 * Constants for failfast support:
1679 1679 *
1680 1680 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1681 1681 * failfast processing being performed.
1682 1682 *
1683 1683 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1684 1684 * failfast processing on all bufs with B_FAILFAST set.
1685 1685 */
1686 1686
1687 1687 #define SD_FAILFAST_INACTIVE 0
1688 1688 #define SD_FAILFAST_ACTIVE 1
1689 1689
1690 1690 /*
1691 1691 * Bitmask to control behavior of buf(9S) flushes when a transition to
1692 1692 * the failfast state occurs. Optional bits include:
1693 1693 *
1694 1694 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1695 1695 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1696 1696 * be flushed.
1697 1697 *
1698 1698 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1699 1699 * driver, in addition to the regular wait queue. This includes the xbuf
1700 1700 * queues. When clear, only the driver's wait queue will be flushed.
1701 1701 */
1702 1702 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01
1703 1703 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02
1704 1704
1705 1705 /*
1706 1706 * The default behavior is to only flush bufs that have B_FAILFAST set, but
1707 1707 * to flush all queues within the driver.
1708 1708 */
1709 1709 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1710 1710
1711 1711
1712 1712 /*
1713 1713 * SD Testing Fault Injection
1714 1714 */
1715 1715 #ifdef SD_FAULT_INJECTION
1716 1716 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1717 1717 static void sd_faultinjection(struct scsi_pkt *pktp);
1718 1718 static void sd_injection_log(char *buf, struct sd_lun *un);
1719 1719 #endif
1720 1720
1721 1721 /*
1722 1722 * Device driver ops vector
1723 1723 */
1724 1724 static struct cb_ops sd_cb_ops = {
1725 1725 sdopen, /* open */
1726 1726 sdclose, /* close */
1727 1727 sdstrategy, /* strategy */
1728 1728 nodev, /* print */
1729 1729 sddump, /* dump */
1730 1730 sdread, /* read */
1731 1731 sdwrite, /* write */
1732 1732 sdioctl, /* ioctl */
1733 1733 nodev, /* devmap */
1734 1734 nodev, /* mmap */
1735 1735 nodev, /* segmap */
1736 1736 nochpoll, /* poll */
1737 1737 sd_prop_op, /* cb_prop_op */
1738 1738 0, /* streamtab */
1739 1739 D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1740 1740 CB_REV, /* cb_rev */
1741 1741 sdaread, /* async I/O read entry point */
1742 1742 sdawrite /* async I/O write entry point */
1743 1743 };
1744 1744
1745 1745 struct dev_ops sd_ops = {
1746 1746 DEVO_REV, /* devo_rev, */
1747 1747 0, /* refcnt */
1748 1748 sdinfo, /* info */
1749 1749 nulldev, /* identify */
1750 1750 sdprobe, /* probe */
1751 1751 sdattach, /* attach */
1752 1752 sddetach, /* detach */
1753 1753 nodev, /* reset */
1754 1754 &sd_cb_ops, /* driver operations */
1755 1755 NULL, /* bus operations */
1756 1756 sdpower, /* power */
1757 1757 ddi_quiesce_not_needed, /* quiesce */
1758 1758 };
1759 1759
1760 1760 /*
1761 1761 * This is the loadable module wrapper.
1762 1762 */
1763 1763 #include <sys/modctl.h>
1764 1764
1765 1765 #ifndef XPV_HVM_DRIVER
1766 1766 static struct modldrv modldrv = {
1767 1767 &mod_driverops, /* Type of module. This one is a driver */
1768 1768 SD_MODULE_NAME, /* Module name. */
1769 1769 &sd_ops /* driver ops */
1770 1770 };
1771 1771
1772 1772 static struct modlinkage modlinkage = {
1773 1773 MODREV_1, &modldrv, NULL
1774 1774 };
1775 1775
1776 1776 #else /* XPV_HVM_DRIVER */
1777 1777 static struct modlmisc modlmisc = {
1778 1778 &mod_miscops, /* Type of module. This one is a misc */
1779 1779 "HVM " SD_MODULE_NAME, /* Module name. */
1780 1780 };
1781 1781
1782 1782 static struct modlinkage modlinkage = {
1783 1783 MODREV_1, &modlmisc, NULL
1784 1784 };
1785 1785
1786 1786 #endif /* XPV_HVM_DRIVER */
1787 1787
1788 1788 static cmlb_tg_ops_t sd_tgops = {
1789 1789 TG_DK_OPS_VERSION_1,
1790 1790 sd_tg_rdwr,
1791 1791 sd_tg_getinfo
1792 1792 };
1793 1793
1794 1794 static struct scsi_asq_key_strings sd_additional_codes[] = {
1795 1795 0x81, 0, "Logical Unit is Reserved",
1796 1796 0x85, 0, "Audio Address Not Valid",
1797 1797 0xb6, 0, "Media Load Mechanism Failed",
1798 1798 0xB9, 0, "Audio Play Operation Aborted",
1799 1799 0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1800 1800 0x53, 2, "Medium removal prevented",
1801 1801 0x6f, 0, "Authentication failed during key exchange",
1802 1802 0x6f, 1, "Key not present",
1803 1803 0x6f, 2, "Key not established",
1804 1804 0x6f, 3, "Read without proper authentication",
1805 1805 0x6f, 4, "Mismatched region to this logical unit",
1806 1806 0x6f, 5, "Region reset count error",
1807 1807 0xffff, 0x0, NULL
1808 1808 };
1809 1809
1810 1810
1811 1811 /*
1812 1812 * Struct for passing printing information for sense data messages
1813 1813 */
1814 1814 struct sd_sense_info {
1815 1815 int ssi_severity;
1816 1816 int ssi_pfa_flag;
1817 1817 };
1818 1818
1819 1819 /*
1820 1820 * Table of function pointers for iostart-side routines. Separate "chains"
1821 1821 * of layered function calls are formed by placing the function pointers
1822 1822 * sequentially in the desired order. Functions are called according to an
1823 1823 * incrementing table index ordering. The last function in each chain must
1824 1824 * be sd_core_iostart(). The corresponding iodone-side routines are expected
1825 1825 * in the sd_iodone_chain[] array.
1826 1826 *
1827 1827 * Note: It may seem more natural to organize both the iostart and iodone
1828 1828 * functions together, into an array of structures (or some similar
1829 1829 * organization) with a common index, rather than two separate arrays which
1830 1830 * must be maintained in synchronization. The purpose of this division is
1831 1831 * to achieve improved performance: individual arrays allows for more
1832 1832 * effective cache line utilization on certain platforms.
1833 1833 */
1834 1834
1835 1835 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1836 1836
1837 1837
1838 1838 static sd_chain_t sd_iostart_chain[] = {
1839 1839
1840 1840 /* Chain for buf IO for disk drive targets (PM enabled) */
1841 1841 sd_mapblockaddr_iostart, /* Index: 0 */
1842 1842 sd_pm_iostart, /* Index: 1 */
1843 1843 sd_core_iostart, /* Index: 2 */
1844 1844
1845 1845 /* Chain for buf IO for disk drive targets (PM disabled) */
1846 1846 sd_mapblockaddr_iostart, /* Index: 3 */
1847 1847 sd_core_iostart, /* Index: 4 */
1848 1848
1849 1849 /*
1850 1850 * Chain for buf IO for removable-media or large sector size
1851 1851 * disk drive targets with RMW needed (PM enabled)
1852 1852 */
1853 1853 sd_mapblockaddr_iostart, /* Index: 5 */
1854 1854 sd_mapblocksize_iostart, /* Index: 6 */
1855 1855 sd_pm_iostart, /* Index: 7 */
1856 1856 sd_core_iostart, /* Index: 8 */
1857 1857
1858 1858 /*
1859 1859 * Chain for buf IO for removable-media or large sector size
1860 1860 * disk drive targets with RMW needed (PM disabled)
1861 1861 */
1862 1862 sd_mapblockaddr_iostart, /* Index: 9 */
1863 1863 sd_mapblocksize_iostart, /* Index: 10 */
1864 1864 sd_core_iostart, /* Index: 11 */
1865 1865
1866 1866 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1867 1867 sd_mapblockaddr_iostart, /* Index: 12 */
1868 1868 sd_checksum_iostart, /* Index: 13 */
1869 1869 sd_pm_iostart, /* Index: 14 */
1870 1870 sd_core_iostart, /* Index: 15 */
1871 1871
1872 1872 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1873 1873 sd_mapblockaddr_iostart, /* Index: 16 */
1874 1874 sd_checksum_iostart, /* Index: 17 */
1875 1875 sd_core_iostart, /* Index: 18 */
1876 1876
1877 1877 /* Chain for USCSI commands (all targets) */
1878 1878 sd_pm_iostart, /* Index: 19 */
1879 1879 sd_core_iostart, /* Index: 20 */
1880 1880
1881 1881 /* Chain for checksumming USCSI commands (all targets) */
1882 1882 sd_checksum_uscsi_iostart, /* Index: 21 */
1883 1883 sd_pm_iostart, /* Index: 22 */
1884 1884 sd_core_iostart, /* Index: 23 */
1885 1885
1886 1886 /* Chain for "direct" USCSI commands (all targets) */
1887 1887 sd_core_iostart, /* Index: 24 */
1888 1888
1889 1889 /* Chain for "direct priority" USCSI commands (all targets) */
1890 1890 sd_core_iostart, /* Index: 25 */
1891 1891
1892 1892 /*
1893 1893 * Chain for buf IO for large sector size disk drive targets
1894 1894 * with RMW needed with checksumming (PM enabled)
1895 1895 */
1896 1896 sd_mapblockaddr_iostart, /* Index: 26 */
1897 1897 sd_mapblocksize_iostart, /* Index: 27 */
1898 1898 sd_checksum_iostart, /* Index: 28 */
1899 1899 sd_pm_iostart, /* Index: 29 */
1900 1900 sd_core_iostart, /* Index: 30 */
1901 1901
1902 1902 /*
1903 1903 * Chain for buf IO for large sector size disk drive targets
1904 1904 * with RMW needed with checksumming (PM disabled)
1905 1905 */
1906 1906 sd_mapblockaddr_iostart, /* Index: 31 */
1907 1907 sd_mapblocksize_iostart, /* Index: 32 */
1908 1908 sd_checksum_iostart, /* Index: 33 */
1909 1909 sd_core_iostart, /* Index: 34 */
1910 1910
1911 1911 };
1912 1912
1913 1913 /*
1914 1914 * Macros to locate the first function of each iostart chain in the
1915 1915 * sd_iostart_chain[] array. These are located by the index in the array.
1916 1916 */
1917 1917 #define SD_CHAIN_DISK_IOSTART 0
1918 1918 #define SD_CHAIN_DISK_IOSTART_NO_PM 3
1919 1919 #define SD_CHAIN_MSS_DISK_IOSTART 5
1920 1920 #define SD_CHAIN_RMMEDIA_IOSTART 5
1921 1921 #define SD_CHAIN_MSS_DISK_IOSTART_NO_PM 9
1922 1922 #define SD_CHAIN_RMMEDIA_IOSTART_NO_PM 9
1923 1923 #define SD_CHAIN_CHKSUM_IOSTART 12
1924 1924 #define SD_CHAIN_CHKSUM_IOSTART_NO_PM 16
1925 1925 #define SD_CHAIN_USCSI_CMD_IOSTART 19
1926 1926 #define SD_CHAIN_USCSI_CHKSUM_IOSTART 21
1927 1927 #define SD_CHAIN_DIRECT_CMD_IOSTART 24
1928 1928 #define SD_CHAIN_PRIORITY_CMD_IOSTART 25
1929 1929 #define SD_CHAIN_MSS_CHKSUM_IOSTART 26
1930 1930 #define SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM 31
1931 1931
1932 1932
1933 1933 /*
1934 1934 * Table of function pointers for the iodone-side routines for the driver-
1935 1935 * internal layering mechanism. The calling sequence for iodone routines
1936 1936 * uses a decrementing table index, so the last routine called in a chain
1937 1937 * must be at the lowest array index location for that chain. The last
1938 1938 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1939 1939 * or sd_uscsi_iodone() (for uscsi IOs). Other than this, the ordering
1940 1940 * of the functions in an iodone side chain must correspond to the ordering
1941 1941 * of the iostart routines for that chain. Note that there is no iodone
1942 1942 * side routine that corresponds to sd_core_iostart(), so there is no
1943 1943 * entry in the table for this.
1944 1944 */
1945 1945
1946 1946 static sd_chain_t sd_iodone_chain[] = {
1947 1947
1948 1948 /* Chain for buf IO for disk drive targets (PM enabled) */
1949 1949 sd_buf_iodone, /* Index: 0 */
1950 1950 sd_mapblockaddr_iodone, /* Index: 1 */
1951 1951 sd_pm_iodone, /* Index: 2 */
1952 1952
1953 1953 /* Chain for buf IO for disk drive targets (PM disabled) */
1954 1954 sd_buf_iodone, /* Index: 3 */
1955 1955 sd_mapblockaddr_iodone, /* Index: 4 */
1956 1956
1957 1957 /*
1958 1958 * Chain for buf IO for removable-media or large sector size
1959 1959 * disk drive targets with RMW needed (PM enabled)
1960 1960 */
1961 1961 sd_buf_iodone, /* Index: 5 */
1962 1962 sd_mapblockaddr_iodone, /* Index: 6 */
1963 1963 sd_mapblocksize_iodone, /* Index: 7 */
1964 1964 sd_pm_iodone, /* Index: 8 */
1965 1965
1966 1966 /*
1967 1967 * Chain for buf IO for removable-media or large sector size
1968 1968 * disk drive targets with RMW needed (PM disabled)
1969 1969 */
1970 1970 sd_buf_iodone, /* Index: 9 */
1971 1971 sd_mapblockaddr_iodone, /* Index: 10 */
1972 1972 sd_mapblocksize_iodone, /* Index: 11 */
1973 1973
1974 1974 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
1975 1975 sd_buf_iodone, /* Index: 12 */
1976 1976 sd_mapblockaddr_iodone, /* Index: 13 */
1977 1977 sd_checksum_iodone, /* Index: 14 */
1978 1978 sd_pm_iodone, /* Index: 15 */
1979 1979
1980 1980 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
1981 1981 sd_buf_iodone, /* Index: 16 */
1982 1982 sd_mapblockaddr_iodone, /* Index: 17 */
1983 1983 sd_checksum_iodone, /* Index: 18 */
1984 1984
1985 1985 /* Chain for USCSI commands (non-checksum targets) */
1986 1986 sd_uscsi_iodone, /* Index: 19 */
1987 1987 sd_pm_iodone, /* Index: 20 */
1988 1988
1989 1989 /* Chain for USCSI commands (checksum targets) */
1990 1990 sd_uscsi_iodone, /* Index: 21 */
1991 1991 sd_checksum_uscsi_iodone, /* Index: 22 */
1992 1992 sd_pm_iodone, /* Index: 22 */
1993 1993
1994 1994 /* Chain for "direct" USCSI commands (all targets) */
1995 1995 sd_uscsi_iodone, /* Index: 24 */
1996 1996
1997 1997 /* Chain for "direct priority" USCSI commands (all targets) */
1998 1998 sd_uscsi_iodone, /* Index: 25 */
1999 1999
2000 2000 /*
2001 2001 * Chain for buf IO for large sector size disk drive targets
2002 2002 * with checksumming (PM enabled)
2003 2003 */
2004 2004 sd_buf_iodone, /* Index: 26 */
2005 2005 sd_mapblockaddr_iodone, /* Index: 27 */
2006 2006 sd_mapblocksize_iodone, /* Index: 28 */
2007 2007 sd_checksum_iodone, /* Index: 29 */
2008 2008 sd_pm_iodone, /* Index: 30 */
2009 2009
2010 2010 /*
2011 2011 * Chain for buf IO for large sector size disk drive targets
2012 2012 * with checksumming (PM disabled)
2013 2013 */
2014 2014 sd_buf_iodone, /* Index: 31 */
2015 2015 sd_mapblockaddr_iodone, /* Index: 32 */
2016 2016 sd_mapblocksize_iodone, /* Index: 33 */
2017 2017 sd_checksum_iodone, /* Index: 34 */
2018 2018 };
2019 2019
2020 2020
2021 2021 /*
2022 2022 * Macros to locate the "first" function in the sd_iodone_chain[] array for
2023 2023 * each iodone-side chain. These are located by the array index, but as the
2024 2024 * iodone side functions are called in a decrementing-index order, the
2025 2025 * highest index number in each chain must be specified (as these correspond
2026 2026 * to the first function in the iodone chain that will be called by the core
2027 2027 * at IO completion time).
2028 2028 */
2029 2029
2030 2030 #define SD_CHAIN_DISK_IODONE 2
2031 2031 #define SD_CHAIN_DISK_IODONE_NO_PM 4
2032 2032 #define SD_CHAIN_RMMEDIA_IODONE 8
2033 2033 #define SD_CHAIN_MSS_DISK_IODONE 8
2034 2034 #define SD_CHAIN_RMMEDIA_IODONE_NO_PM 11
2035 2035 #define SD_CHAIN_MSS_DISK_IODONE_NO_PM 11
2036 2036 #define SD_CHAIN_CHKSUM_IODONE 15
2037 2037 #define SD_CHAIN_CHKSUM_IODONE_NO_PM 18
2038 2038 #define SD_CHAIN_USCSI_CMD_IODONE 20
2039 2039 #define SD_CHAIN_USCSI_CHKSUM_IODONE 22
2040 2040 #define SD_CHAIN_DIRECT_CMD_IODONE 24
2041 2041 #define SD_CHAIN_PRIORITY_CMD_IODONE 25
2042 2042 #define SD_CHAIN_MSS_CHKSUM_IODONE 30
2043 2043 #define SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM 34
2044 2044
2045 2045
2046 2046
2047 2047 /*
2048 2048 * Array to map a layering chain index to the appropriate initpkt routine.
2049 2049 * The redundant entries are present so that the index used for accessing
2050 2050 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2051 2051 * with this table as well.
2052 2052 */
2053 2053 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2054 2054
2055 2055 static sd_initpkt_t sd_initpkt_map[] = {
2056 2056
2057 2057 /* Chain for buf IO for disk drive targets (PM enabled) */
2058 2058 sd_initpkt_for_buf, /* Index: 0 */
2059 2059 sd_initpkt_for_buf, /* Index: 1 */
2060 2060 sd_initpkt_for_buf, /* Index: 2 */
2061 2061
2062 2062 /* Chain for buf IO for disk drive targets (PM disabled) */
2063 2063 sd_initpkt_for_buf, /* Index: 3 */
2064 2064 sd_initpkt_for_buf, /* Index: 4 */
2065 2065
2066 2066 /*
2067 2067 * Chain for buf IO for removable-media or large sector size
2068 2068 * disk drive targets (PM enabled)
2069 2069 */
2070 2070 sd_initpkt_for_buf, /* Index: 5 */
2071 2071 sd_initpkt_for_buf, /* Index: 6 */
2072 2072 sd_initpkt_for_buf, /* Index: 7 */
2073 2073 sd_initpkt_for_buf, /* Index: 8 */
2074 2074
2075 2075 /*
2076 2076 * Chain for buf IO for removable-media or large sector size
2077 2077 * disk drive targets (PM disabled)
2078 2078 */
2079 2079 sd_initpkt_for_buf, /* Index: 9 */
2080 2080 sd_initpkt_for_buf, /* Index: 10 */
2081 2081 sd_initpkt_for_buf, /* Index: 11 */
2082 2082
2083 2083 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2084 2084 sd_initpkt_for_buf, /* Index: 12 */
2085 2085 sd_initpkt_for_buf, /* Index: 13 */
2086 2086 sd_initpkt_for_buf, /* Index: 14 */
2087 2087 sd_initpkt_for_buf, /* Index: 15 */
2088 2088
2089 2089 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2090 2090 sd_initpkt_for_buf, /* Index: 16 */
2091 2091 sd_initpkt_for_buf, /* Index: 17 */
2092 2092 sd_initpkt_for_buf, /* Index: 18 */
2093 2093
2094 2094 /* Chain for USCSI commands (non-checksum targets) */
2095 2095 sd_initpkt_for_uscsi, /* Index: 19 */
2096 2096 sd_initpkt_for_uscsi, /* Index: 20 */
2097 2097
2098 2098 /* Chain for USCSI commands (checksum targets) */
2099 2099 sd_initpkt_for_uscsi, /* Index: 21 */
2100 2100 sd_initpkt_for_uscsi, /* Index: 22 */
2101 2101 sd_initpkt_for_uscsi, /* Index: 22 */
2102 2102
2103 2103 /* Chain for "direct" USCSI commands (all targets) */
2104 2104 sd_initpkt_for_uscsi, /* Index: 24 */
2105 2105
2106 2106 /* Chain for "direct priority" USCSI commands (all targets) */
2107 2107 sd_initpkt_for_uscsi, /* Index: 25 */
2108 2108
2109 2109 /*
2110 2110 * Chain for buf IO for large sector size disk drive targets
2111 2111 * with checksumming (PM enabled)
2112 2112 */
2113 2113 sd_initpkt_for_buf, /* Index: 26 */
2114 2114 sd_initpkt_for_buf, /* Index: 27 */
2115 2115 sd_initpkt_for_buf, /* Index: 28 */
2116 2116 sd_initpkt_for_buf, /* Index: 29 */
2117 2117 sd_initpkt_for_buf, /* Index: 30 */
2118 2118
2119 2119 /*
2120 2120 * Chain for buf IO for large sector size disk drive targets
2121 2121 * with checksumming (PM disabled)
2122 2122 */
2123 2123 sd_initpkt_for_buf, /* Index: 31 */
2124 2124 sd_initpkt_for_buf, /* Index: 32 */
2125 2125 sd_initpkt_for_buf, /* Index: 33 */
2126 2126 sd_initpkt_for_buf, /* Index: 34 */
2127 2127 };
2128 2128
2129 2129
2130 2130 /*
2131 2131 * Array to map a layering chain index to the appropriate destroypktpkt routine.
2132 2132 * The redundant entries are present so that the index used for accessing
2133 2133 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2134 2134 * with this table as well.
2135 2135 */
2136 2136 typedef void (*sd_destroypkt_t)(struct buf *);
2137 2137
2138 2138 static sd_destroypkt_t sd_destroypkt_map[] = {
2139 2139
2140 2140 /* Chain for buf IO for disk drive targets (PM enabled) */
2141 2141 sd_destroypkt_for_buf, /* Index: 0 */
2142 2142 sd_destroypkt_for_buf, /* Index: 1 */
2143 2143 sd_destroypkt_for_buf, /* Index: 2 */
2144 2144
2145 2145 /* Chain for buf IO for disk drive targets (PM disabled) */
2146 2146 sd_destroypkt_for_buf, /* Index: 3 */
2147 2147 sd_destroypkt_for_buf, /* Index: 4 */
2148 2148
2149 2149 /*
2150 2150 * Chain for buf IO for removable-media or large sector size
2151 2151 * disk drive targets (PM enabled)
2152 2152 */
2153 2153 sd_destroypkt_for_buf, /* Index: 5 */
2154 2154 sd_destroypkt_for_buf, /* Index: 6 */
2155 2155 sd_destroypkt_for_buf, /* Index: 7 */
2156 2156 sd_destroypkt_for_buf, /* Index: 8 */
2157 2157
2158 2158 /*
2159 2159 * Chain for buf IO for removable-media or large sector size
2160 2160 * disk drive targets (PM disabled)
2161 2161 */
2162 2162 sd_destroypkt_for_buf, /* Index: 9 */
2163 2163 sd_destroypkt_for_buf, /* Index: 10 */
2164 2164 sd_destroypkt_for_buf, /* Index: 11 */
2165 2165
2166 2166 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2167 2167 sd_destroypkt_for_buf, /* Index: 12 */
2168 2168 sd_destroypkt_for_buf, /* Index: 13 */
2169 2169 sd_destroypkt_for_buf, /* Index: 14 */
2170 2170 sd_destroypkt_for_buf, /* Index: 15 */
2171 2171
2172 2172 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2173 2173 sd_destroypkt_for_buf, /* Index: 16 */
2174 2174 sd_destroypkt_for_buf, /* Index: 17 */
2175 2175 sd_destroypkt_for_buf, /* Index: 18 */
2176 2176
2177 2177 /* Chain for USCSI commands (non-checksum targets) */
2178 2178 sd_destroypkt_for_uscsi, /* Index: 19 */
2179 2179 sd_destroypkt_for_uscsi, /* Index: 20 */
2180 2180
2181 2181 /* Chain for USCSI commands (checksum targets) */
2182 2182 sd_destroypkt_for_uscsi, /* Index: 21 */
2183 2183 sd_destroypkt_for_uscsi, /* Index: 22 */
2184 2184 sd_destroypkt_for_uscsi, /* Index: 22 */
2185 2185
2186 2186 /* Chain for "direct" USCSI commands (all targets) */
2187 2187 sd_destroypkt_for_uscsi, /* Index: 24 */
2188 2188
2189 2189 /* Chain for "direct priority" USCSI commands (all targets) */
2190 2190 sd_destroypkt_for_uscsi, /* Index: 25 */
2191 2191
2192 2192 /*
2193 2193 * Chain for buf IO for large sector size disk drive targets
2194 2194 * with checksumming (PM disabled)
2195 2195 */
2196 2196 sd_destroypkt_for_buf, /* Index: 26 */
2197 2197 sd_destroypkt_for_buf, /* Index: 27 */
2198 2198 sd_destroypkt_for_buf, /* Index: 28 */
2199 2199 sd_destroypkt_for_buf, /* Index: 29 */
2200 2200 sd_destroypkt_for_buf, /* Index: 30 */
2201 2201
2202 2202 /*
2203 2203 * Chain for buf IO for large sector size disk drive targets
2204 2204 * with checksumming (PM enabled)
2205 2205 */
2206 2206 sd_destroypkt_for_buf, /* Index: 31 */
2207 2207 sd_destroypkt_for_buf, /* Index: 32 */
2208 2208 sd_destroypkt_for_buf, /* Index: 33 */
2209 2209 sd_destroypkt_for_buf, /* Index: 34 */
2210 2210 };
2211 2211
2212 2212
2213 2213
2214 2214 /*
2215 2215 * Array to map a layering chain index to the appropriate chain "type".
2216 2216 * The chain type indicates a specific property/usage of the chain.
2217 2217 * The redundant entries are present so that the index used for accessing
2218 2218 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2219 2219 * with this table as well.
2220 2220 */
2221 2221
2222 2222 #define SD_CHAIN_NULL 0 /* for the special RQS cmd */
2223 2223 #define SD_CHAIN_BUFIO 1 /* regular buf IO */
2224 2224 #define SD_CHAIN_USCSI 2 /* regular USCSI commands */
2225 2225 #define SD_CHAIN_DIRECT 3 /* uscsi, w/ bypass power mgt */
2226 2226 #define SD_CHAIN_DIRECT_PRIORITY 4 /* uscsi, w/ bypass power mgt */
2227 2227 /* (for error recovery) */
2228 2228
2229 2229 static int sd_chain_type_map[] = {
2230 2230
2231 2231 /* Chain for buf IO for disk drive targets (PM enabled) */
2232 2232 SD_CHAIN_BUFIO, /* Index: 0 */
2233 2233 SD_CHAIN_BUFIO, /* Index: 1 */
2234 2234 SD_CHAIN_BUFIO, /* Index: 2 */
2235 2235
2236 2236 /* Chain for buf IO for disk drive targets (PM disabled) */
2237 2237 SD_CHAIN_BUFIO, /* Index: 3 */
2238 2238 SD_CHAIN_BUFIO, /* Index: 4 */
2239 2239
2240 2240 /*
2241 2241 * Chain for buf IO for removable-media or large sector size
2242 2242 * disk drive targets (PM enabled)
2243 2243 */
2244 2244 SD_CHAIN_BUFIO, /* Index: 5 */
2245 2245 SD_CHAIN_BUFIO, /* Index: 6 */
2246 2246 SD_CHAIN_BUFIO, /* Index: 7 */
2247 2247 SD_CHAIN_BUFIO, /* Index: 8 */
2248 2248
2249 2249 /*
2250 2250 * Chain for buf IO for removable-media or large sector size
2251 2251 * disk drive targets (PM disabled)
2252 2252 */
2253 2253 SD_CHAIN_BUFIO, /* Index: 9 */
2254 2254 SD_CHAIN_BUFIO, /* Index: 10 */
2255 2255 SD_CHAIN_BUFIO, /* Index: 11 */
2256 2256
2257 2257 /* Chain for buf IO for disk drives with checksumming (PM enabled) */
2258 2258 SD_CHAIN_BUFIO, /* Index: 12 */
2259 2259 SD_CHAIN_BUFIO, /* Index: 13 */
2260 2260 SD_CHAIN_BUFIO, /* Index: 14 */
2261 2261 SD_CHAIN_BUFIO, /* Index: 15 */
2262 2262
2263 2263 /* Chain for buf IO for disk drives with checksumming (PM disabled) */
2264 2264 SD_CHAIN_BUFIO, /* Index: 16 */
2265 2265 SD_CHAIN_BUFIO, /* Index: 17 */
2266 2266 SD_CHAIN_BUFIO, /* Index: 18 */
2267 2267
2268 2268 /* Chain for USCSI commands (non-checksum targets) */
2269 2269 SD_CHAIN_USCSI, /* Index: 19 */
2270 2270 SD_CHAIN_USCSI, /* Index: 20 */
2271 2271
2272 2272 /* Chain for USCSI commands (checksum targets) */
2273 2273 SD_CHAIN_USCSI, /* Index: 21 */
2274 2274 SD_CHAIN_USCSI, /* Index: 22 */
2275 2275 SD_CHAIN_USCSI, /* Index: 23 */
2276 2276
2277 2277 /* Chain for "direct" USCSI commands (all targets) */
2278 2278 SD_CHAIN_DIRECT, /* Index: 24 */
2279 2279
2280 2280 /* Chain for "direct priority" USCSI commands (all targets) */
2281 2281 SD_CHAIN_DIRECT_PRIORITY, /* Index: 25 */
2282 2282
2283 2283 /*
2284 2284 * Chain for buf IO for large sector size disk drive targets
2285 2285 * with checksumming (PM enabled)
2286 2286 */
2287 2287 SD_CHAIN_BUFIO, /* Index: 26 */
2288 2288 SD_CHAIN_BUFIO, /* Index: 27 */
2289 2289 SD_CHAIN_BUFIO, /* Index: 28 */
2290 2290 SD_CHAIN_BUFIO, /* Index: 29 */
2291 2291 SD_CHAIN_BUFIO, /* Index: 30 */
2292 2292
2293 2293 /*
2294 2294 * Chain for buf IO for large sector size disk drive targets
2295 2295 * with checksumming (PM disabled)
2296 2296 */
2297 2297 SD_CHAIN_BUFIO, /* Index: 31 */
2298 2298 SD_CHAIN_BUFIO, /* Index: 32 */
2299 2299 SD_CHAIN_BUFIO, /* Index: 33 */
2300 2300 SD_CHAIN_BUFIO, /* Index: 34 */
2301 2301 };
2302 2302
2303 2303
2304 2304 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2305 2305 #define SD_IS_BUFIO(xp) \
2306 2306 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2307 2307
2308 2308 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2309 2309 #define SD_IS_DIRECT_PRIORITY(xp) \
2310 2310 (sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2311 2311
2312 2312
2313 2313
2314 2314 /*
2315 2315 * Struct, array, and macros to map a specific chain to the appropriate
2316 2316 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2317 2317 *
2318 2318 * The sd_chain_index_map[] array is used at attach time to set the various
2319 2319 * un_xxx_chain type members of the sd_lun softstate to the specific layering
2320 2320 * chain to be used with the instance. This allows different instances to use
2321 2321 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2322 2322 * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2323 2323 * values at sd_xbuf init time, this allows (1) layering chains may be changed
2324 2324 * dynamically & without the use of locking; and (2) a layer may update the
2325 2325 * xb_chain_io[start|done] member in a given xbuf with its current index value,
2326 2326 * to allow for deferred processing of an IO within the same chain from a
2327 2327 * different execution context.
2328 2328 */
2329 2329
2330 2330 struct sd_chain_index {
2331 2331 int sci_iostart_index;
2332 2332 int sci_iodone_index;
2333 2333 };
2334 2334
2335 2335 static struct sd_chain_index sd_chain_index_map[] = {
2336 2336 { SD_CHAIN_DISK_IOSTART, SD_CHAIN_DISK_IODONE },
2337 2337 { SD_CHAIN_DISK_IOSTART_NO_PM, SD_CHAIN_DISK_IODONE_NO_PM },
2338 2338 { SD_CHAIN_RMMEDIA_IOSTART, SD_CHAIN_RMMEDIA_IODONE },
2339 2339 { SD_CHAIN_RMMEDIA_IOSTART_NO_PM, SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2340 2340 { SD_CHAIN_CHKSUM_IOSTART, SD_CHAIN_CHKSUM_IODONE },
2341 2341 { SD_CHAIN_CHKSUM_IOSTART_NO_PM, SD_CHAIN_CHKSUM_IODONE_NO_PM },
2342 2342 { SD_CHAIN_USCSI_CMD_IOSTART, SD_CHAIN_USCSI_CMD_IODONE },
2343 2343 { SD_CHAIN_USCSI_CHKSUM_IOSTART, SD_CHAIN_USCSI_CHKSUM_IODONE },
2344 2344 { SD_CHAIN_DIRECT_CMD_IOSTART, SD_CHAIN_DIRECT_CMD_IODONE },
2345 2345 { SD_CHAIN_PRIORITY_CMD_IOSTART, SD_CHAIN_PRIORITY_CMD_IODONE },
2346 2346 { SD_CHAIN_MSS_CHKSUM_IOSTART, SD_CHAIN_MSS_CHKSUM_IODONE },
2347 2347 { SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2348 2348
2349 2349 };
2350 2350
2351 2351
2352 2352 /*
2353 2353 * The following are indexes into the sd_chain_index_map[] array.
2354 2354 */
2355 2355
2356 2356 /* un->un_buf_chain_type must be set to one of these */
2357 2357 #define SD_CHAIN_INFO_DISK 0
2358 2358 #define SD_CHAIN_INFO_DISK_NO_PM 1
2359 2359 #define SD_CHAIN_INFO_RMMEDIA 2
2360 2360 #define SD_CHAIN_INFO_MSS_DISK 2
2361 2361 #define SD_CHAIN_INFO_RMMEDIA_NO_PM 3
2362 2362 #define SD_CHAIN_INFO_MSS_DSK_NO_PM 3
2363 2363 #define SD_CHAIN_INFO_CHKSUM 4
2364 2364 #define SD_CHAIN_INFO_CHKSUM_NO_PM 5
2365 2365 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM 10
2366 2366 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11
2367 2367
2368 2368 /* un->un_uscsi_chain_type must be set to one of these */
2369 2369 #define SD_CHAIN_INFO_USCSI_CMD 6
2370 2370 /* USCSI with PM disabled is the same as DIRECT */
2371 2371 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8
2372 2372 #define SD_CHAIN_INFO_USCSI_CHKSUM 7
2373 2373
2374 2374 /* un->un_direct_chain_type must be set to one of these */
2375 2375 #define SD_CHAIN_INFO_DIRECT_CMD 8
2376 2376
2377 2377 /* un->un_priority_chain_type must be set to one of these */
2378 2378 #define SD_CHAIN_INFO_PRIORITY_CMD 9
2379 2379
2380 2380 /* size for devid inquiries */
2381 2381 #define MAX_INQUIRY_SIZE 0xF0
2382 2382
2383 2383 /*
2384 2384 * Macros used by functions to pass a given buf(9S) struct along to the
2385 2385 * next function in the layering chain for further processing.
2386 2386 *
2387 2387 * In the following macros, passing more than three arguments to the called
2388 2388 * routines causes the optimizer for the SPARC compiler to stop doing tail
2389 2389 * call elimination which results in significant performance degradation.
2390 2390 */
2391 2391 #define SD_BEGIN_IOSTART(index, un, bp) \
2392 2392 ((*(sd_iostart_chain[index]))(index, un, bp))
2393 2393
2394 2394 #define SD_BEGIN_IODONE(index, un, bp) \
2395 2395 ((*(sd_iodone_chain[index]))(index, un, bp))
2396 2396
2397 2397 #define SD_NEXT_IOSTART(index, un, bp) \
2398 2398 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2399 2399
2400 2400 #define SD_NEXT_IODONE(index, un, bp) \
2401 2401 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2402 2402
2403 2403 /*
2404 2404 * Function: _init
2405 2405 *
2406 2406 * Description: This is the driver _init(9E) entry point.
2407 2407 *
2408 2408 * Return Code: Returns the value from mod_install(9F) or
2409 2409 * ddi_soft_state_init(9F) as appropriate.
2410 2410 *
2411 2411 * Context: Called when driver module loaded.
2412 2412 */
2413 2413
2414 2414 int
2415 2415 _init(void)
2416 2416 {
2417 2417 int err;
2418 2418
2419 2419 /* establish driver name from module name */
2420 2420 sd_label = (char *)mod_modname(&modlinkage);
2421 2421
2422 2422 #ifndef XPV_HVM_DRIVER
2423 2423 err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2424 2424 SD_MAXUNIT);
2425 2425 if (err != 0) {
2426 2426 return (err);
2427 2427 }
2428 2428
2429 2429 #else /* XPV_HVM_DRIVER */
2430 2430 /* Remove the leading "hvm_" from the module name */
2431 2431 ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
2432 2432 sd_label += strlen("hvm_");
2433 2433
2434 2434 #endif /* XPV_HVM_DRIVER */
2435 2435
2436 2436 mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2437 2437 mutex_init(&sd_log_mutex, NULL, MUTEX_DRIVER, NULL);
2438 2438 mutex_init(&sd_label_mutex, NULL, MUTEX_DRIVER, NULL);
2439 2439
2440 2440 mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2441 2441 cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2442 2442 cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2443 2443
2444 2444 /*
2445 2445 * it's ok to init here even for fibre device
2446 2446 */
2447 2447 sd_scsi_probe_cache_init();
2448 2448
2449 2449 sd_scsi_target_lun_init();
2450 2450
2451 2451 /*
2452 2452 * Creating taskq before mod_install ensures that all callers (threads)
2453 2453 * that enter the module after a successful mod_install encounter
2454 2454 * a valid taskq.
2455 2455 */
2456 2456 sd_taskq_create();
2457 2457
2458 2458 err = mod_install(&modlinkage);
2459 2459 if (err != 0) {
2460 2460 /* delete taskq if install fails */
2461 2461 sd_taskq_delete();
2462 2462
2463 2463 mutex_destroy(&sd_detach_mutex);
2464 2464 mutex_destroy(&sd_log_mutex);
2465 2465 mutex_destroy(&sd_label_mutex);
2466 2466
2467 2467 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2468 2468 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2469 2469 cv_destroy(&sd_tr.srq_inprocess_cv);
2470 2470
2471 2471 sd_scsi_probe_cache_fini();
2472 2472
2473 2473 sd_scsi_target_lun_fini();
2474 2474
2475 2475 #ifndef XPV_HVM_DRIVER
2476 2476 ddi_soft_state_fini(&sd_state);
2477 2477 #endif /* !XPV_HVM_DRIVER */
2478 2478 return (err);
2479 2479 }
2480 2480
2481 2481 return (err);
2482 2482 }
2483 2483
2484 2484
2485 2485 /*
2486 2486 * Function: _fini
2487 2487 *
2488 2488 * Description: This is the driver _fini(9E) entry point.
2489 2489 *
2490 2490 * Return Code: Returns the value from mod_remove(9F)
2491 2491 *
2492 2492 * Context: Called when driver module is unloaded.
2493 2493 */
2494 2494
2495 2495 int
2496 2496 _fini(void)
2497 2497 {
2498 2498 int err;
2499 2499
2500 2500 if ((err = mod_remove(&modlinkage)) != 0) {
2501 2501 return (err);
2502 2502 }
2503 2503
2504 2504 sd_taskq_delete();
2505 2505
2506 2506 mutex_destroy(&sd_detach_mutex);
2507 2507 mutex_destroy(&sd_log_mutex);
2508 2508 mutex_destroy(&sd_label_mutex);
2509 2509 mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2510 2510
2511 2511 sd_scsi_probe_cache_fini();
2512 2512
2513 2513 sd_scsi_target_lun_fini();
2514 2514
2515 2515 cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2516 2516 cv_destroy(&sd_tr.srq_inprocess_cv);
2517 2517
2518 2518 #ifndef XPV_HVM_DRIVER
2519 2519 ddi_soft_state_fini(&sd_state);
2520 2520 #endif /* !XPV_HVM_DRIVER */
2521 2521
2522 2522 return (err);
2523 2523 }
2524 2524
2525 2525
2526 2526 /*
2527 2527 * Function: _info
2528 2528 *
2529 2529 * Description: This is the driver _info(9E) entry point.
2530 2530 *
2531 2531 * Arguments: modinfop - pointer to the driver modinfo structure
2532 2532 *
2533 2533 * Return Code: Returns the value from mod_info(9F).
2534 2534 *
2535 2535 * Context: Kernel thread context
2536 2536 */
2537 2537
2538 2538 int
2539 2539 _info(struct modinfo *modinfop)
2540 2540 {
2541 2541 return (mod_info(&modlinkage, modinfop));
2542 2542 }
2543 2543
2544 2544
2545 2545 /*
2546 2546 * The following routines implement the driver message logging facility.
2547 2547 * They provide component- and level- based debug output filtering.
2548 2548 * Output may also be restricted to messages for a single instance by
2549 2549 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2550 2550 * to NULL, then messages for all instances are printed.
2551 2551 *
2552 2552 * These routines have been cloned from each other due to the language
2553 2553 * constraints of macros and variable argument list processing.
2554 2554 */
2555 2555
2556 2556
2557 2557 /*
2558 2558 * Function: sd_log_err
2559 2559 *
2560 2560 * Description: This routine is called by the SD_ERROR macro for debug
2561 2561 * logging of error conditions.
2562 2562 *
2563 2563 * Arguments: comp - driver component being logged
2564 2564 * dev - pointer to driver info structure
2565 2565 * fmt - error string and format to be logged
2566 2566 */
2567 2567
2568 2568 static void
2569 2569 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2570 2570 {
2571 2571 va_list ap;
2572 2572 dev_info_t *dev;
2573 2573
2574 2574 ASSERT(un != NULL);
2575 2575 dev = SD_DEVINFO(un);
2576 2576 ASSERT(dev != NULL);
2577 2577
2578 2578 /*
2579 2579 * Filter messages based on the global component and level masks.
2580 2580 * Also print if un matches the value of sd_debug_un, or if
2581 2581 * sd_debug_un is set to NULL.
2582 2582 */
2583 2583 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2584 2584 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2585 2585 mutex_enter(&sd_log_mutex);
2586 2586 va_start(ap, fmt);
2587 2587 (void) vsprintf(sd_log_buf, fmt, ap);
2588 2588 va_end(ap);
2589 2589 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2590 2590 mutex_exit(&sd_log_mutex);
2591 2591 }
2592 2592 #ifdef SD_FAULT_INJECTION
2593 2593 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2594 2594 if (un->sd_injection_mask & comp) {
2595 2595 mutex_enter(&sd_log_mutex);
2596 2596 va_start(ap, fmt);
2597 2597 (void) vsprintf(sd_log_buf, fmt, ap);
2598 2598 va_end(ap);
2599 2599 sd_injection_log(sd_log_buf, un);
2600 2600 mutex_exit(&sd_log_mutex);
2601 2601 }
2602 2602 #endif
2603 2603 }
2604 2604
2605 2605
2606 2606 /*
2607 2607 * Function: sd_log_info
2608 2608 *
2609 2609 * Description: This routine is called by the SD_INFO macro for debug
2610 2610 * logging of general purpose informational conditions.
2611 2611 *
2612 2612 * Arguments: comp - driver component being logged
2613 2613 * dev - pointer to driver info structure
2614 2614 * fmt - info string and format to be logged
2615 2615 */
2616 2616
2617 2617 static void
2618 2618 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2619 2619 {
2620 2620 va_list ap;
2621 2621 dev_info_t *dev;
2622 2622
2623 2623 ASSERT(un != NULL);
2624 2624 dev = SD_DEVINFO(un);
2625 2625 ASSERT(dev != NULL);
2626 2626
2627 2627 /*
2628 2628 * Filter messages based on the global component and level masks.
2629 2629 * Also print if un matches the value of sd_debug_un, or if
2630 2630 * sd_debug_un is set to NULL.
2631 2631 */
2632 2632 if ((sd_component_mask & component) &&
2633 2633 (sd_level_mask & SD_LOGMASK_INFO) &&
2634 2634 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2635 2635 mutex_enter(&sd_log_mutex);
2636 2636 va_start(ap, fmt);
2637 2637 (void) vsprintf(sd_log_buf, fmt, ap);
2638 2638 va_end(ap);
2639 2639 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2640 2640 mutex_exit(&sd_log_mutex);
2641 2641 }
2642 2642 #ifdef SD_FAULT_INJECTION
2643 2643 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2644 2644 if (un->sd_injection_mask & component) {
2645 2645 mutex_enter(&sd_log_mutex);
2646 2646 va_start(ap, fmt);
2647 2647 (void) vsprintf(sd_log_buf, fmt, ap);
2648 2648 va_end(ap);
2649 2649 sd_injection_log(sd_log_buf, un);
2650 2650 mutex_exit(&sd_log_mutex);
2651 2651 }
2652 2652 #endif
2653 2653 }
2654 2654
2655 2655
2656 2656 /*
2657 2657 * Function: sd_log_trace
2658 2658 *
2659 2659 * Description: This routine is called by the SD_TRACE macro for debug
2660 2660 * logging of trace conditions (i.e. function entry/exit).
2661 2661 *
2662 2662 * Arguments: comp - driver component being logged
2663 2663 * dev - pointer to driver info structure
2664 2664 * fmt - trace string and format to be logged
2665 2665 */
2666 2666
2667 2667 static void
2668 2668 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2669 2669 {
2670 2670 va_list ap;
2671 2671 dev_info_t *dev;
2672 2672
2673 2673 ASSERT(un != NULL);
2674 2674 dev = SD_DEVINFO(un);
2675 2675 ASSERT(dev != NULL);
2676 2676
2677 2677 /*
2678 2678 * Filter messages based on the global component and level masks.
2679 2679 * Also print if un matches the value of sd_debug_un, or if
2680 2680 * sd_debug_un is set to NULL.
2681 2681 */
2682 2682 if ((sd_component_mask & component) &&
2683 2683 (sd_level_mask & SD_LOGMASK_TRACE) &&
2684 2684 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2685 2685 mutex_enter(&sd_log_mutex);
2686 2686 va_start(ap, fmt);
2687 2687 (void) vsprintf(sd_log_buf, fmt, ap);
2688 2688 va_end(ap);
2689 2689 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2690 2690 mutex_exit(&sd_log_mutex);
2691 2691 }
2692 2692 #ifdef SD_FAULT_INJECTION
2693 2693 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2694 2694 if (un->sd_injection_mask & component) {
2695 2695 mutex_enter(&sd_log_mutex);
2696 2696 va_start(ap, fmt);
2697 2697 (void) vsprintf(sd_log_buf, fmt, ap);
2698 2698 va_end(ap);
2699 2699 sd_injection_log(sd_log_buf, un);
2700 2700 mutex_exit(&sd_log_mutex);
2701 2701 }
2702 2702 #endif
2703 2703 }
2704 2704
2705 2705
2706 2706 /*
2707 2707 * Function: sdprobe
2708 2708 *
2709 2709 * Description: This is the driver probe(9e) entry point function.
2710 2710 *
2711 2711 * Arguments: devi - opaque device info handle
2712 2712 *
2713 2713 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2714 2714 * DDI_PROBE_FAILURE: If the probe failed.
2715 2715 * DDI_PROBE_PARTIAL: If the instance is not present now,
2716 2716 * but may be present in the future.
2717 2717 */
2718 2718
2719 2719 static int
2720 2720 sdprobe(dev_info_t *devi)
2721 2721 {
2722 2722 struct scsi_device *devp;
2723 2723 int rval;
2724 2724 #ifndef XPV_HVM_DRIVER
2725 2725 int instance = ddi_get_instance(devi);
2726 2726 #endif /* !XPV_HVM_DRIVER */
2727 2727
2728 2728 /*
2729 2729 * if it wasn't for pln, sdprobe could actually be nulldev
2730 2730 * in the "__fibre" case.
2731 2731 */
2732 2732 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2733 2733 return (DDI_PROBE_DONTCARE);
2734 2734 }
2735 2735
2736 2736 devp = ddi_get_driver_private(devi);
2737 2737
2738 2738 if (devp == NULL) {
2739 2739 /* Ooops... nexus driver is mis-configured... */
2740 2740 return (DDI_PROBE_FAILURE);
2741 2741 }
2742 2742
2743 2743 #ifndef XPV_HVM_DRIVER
2744 2744 if (ddi_get_soft_state(sd_state, instance) != NULL) {
2745 2745 return (DDI_PROBE_PARTIAL);
2746 2746 }
2747 2747 #endif /* !XPV_HVM_DRIVER */
2748 2748
2749 2749 /*
2750 2750 * Call the SCSA utility probe routine to see if we actually
2751 2751 * have a target at this SCSI nexus.
2752 2752 */
2753 2753 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2754 2754 case SCSIPROBE_EXISTS:
2755 2755 switch (devp->sd_inq->inq_dtype) {
2756 2756 case DTYPE_DIRECT:
2757 2757 rval = DDI_PROBE_SUCCESS;
2758 2758 break;
2759 2759 case DTYPE_RODIRECT:
2760 2760 /* CDs etc. Can be removable media */
2761 2761 rval = DDI_PROBE_SUCCESS;
2762 2762 break;
2763 2763 case DTYPE_OPTICAL:
2764 2764 /*
2765 2765 * Rewritable optical driver HP115AA
2766 2766 * Can also be removable media
2767 2767 */
2768 2768
2769 2769 /*
2770 2770 * Do not attempt to bind to DTYPE_OPTICAL if
2771 2771 * pre solaris 9 sparc sd behavior is required
2772 2772 *
2773 2773 * If first time through and sd_dtype_optical_bind
2774 2774 * has not been set in /etc/system check properties
2775 2775 */
2776 2776
2777 2777 if (sd_dtype_optical_bind < 0) {
2778 2778 sd_dtype_optical_bind = ddi_prop_get_int
2779 2779 (DDI_DEV_T_ANY, devi, 0,
2780 2780 "optical-device-bind", 1);
2781 2781 }
2782 2782
2783 2783 if (sd_dtype_optical_bind == 0) {
2784 2784 rval = DDI_PROBE_FAILURE;
2785 2785 } else {
2786 2786 rval = DDI_PROBE_SUCCESS;
2787 2787 }
2788 2788 break;
2789 2789
2790 2790 case DTYPE_NOTPRESENT:
2791 2791 default:
2792 2792 rval = DDI_PROBE_FAILURE;
2793 2793 break;
2794 2794 }
2795 2795 break;
2796 2796 default:
2797 2797 rval = DDI_PROBE_PARTIAL;
2798 2798 break;
2799 2799 }
2800 2800
2801 2801 /*
2802 2802 * This routine checks for resource allocation prior to freeing,
2803 2803 * so it will take care of the "smart probing" case where a
2804 2804 * scsi_probe() may or may not have been issued and will *not*
2805 2805 * free previously-freed resources.
2806 2806 */
2807 2807 scsi_unprobe(devp);
2808 2808 return (rval);
2809 2809 }
2810 2810
2811 2811
2812 2812 /*
2813 2813 * Function: sdinfo
2814 2814 *
2815 2815 * Description: This is the driver getinfo(9e) entry point function.
2816 2816 * Given the device number, return the devinfo pointer from
2817 2817 * the scsi_device structure or the instance number
2818 2818 * associated with the dev_t.
2819 2819 *
2820 2820 * Arguments: dip - pointer to device info structure
2821 2821 * infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2822 2822 * DDI_INFO_DEVT2INSTANCE)
2823 2823 * arg - driver dev_t
2824 2824 * resultp - user buffer for request response
2825 2825 *
2826 2826 * Return Code: DDI_SUCCESS
2827 2827 * DDI_FAILURE
2828 2828 */
2829 2829 /* ARGSUSED */
2830 2830 static int
2831 2831 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2832 2832 {
2833 2833 struct sd_lun *un;
2834 2834 dev_t dev;
2835 2835 int instance;
2836 2836 int error;
2837 2837
2838 2838 switch (infocmd) {
2839 2839 case DDI_INFO_DEVT2DEVINFO:
2840 2840 dev = (dev_t)arg;
2841 2841 instance = SDUNIT(dev);
2842 2842 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2843 2843 return (DDI_FAILURE);
2844 2844 }
2845 2845 *result = (void *) SD_DEVINFO(un);
2846 2846 error = DDI_SUCCESS;
2847 2847 break;
2848 2848 case DDI_INFO_DEVT2INSTANCE:
2849 2849 dev = (dev_t)arg;
2850 2850 instance = SDUNIT(dev);
2851 2851 *result = (void *)(uintptr_t)instance;
2852 2852 error = DDI_SUCCESS;
2853 2853 break;
2854 2854 default:
2855 2855 error = DDI_FAILURE;
2856 2856 }
2857 2857 return (error);
2858 2858 }
2859 2859
2860 2860 /*
2861 2861 * Function: sd_prop_op
2862 2862 *
2863 2863 * Description: This is the driver prop_op(9e) entry point function.
2864 2864 * Return the number of blocks for the partition in question
2865 2865 * or forward the request to the property facilities.
2866 2866 *
2867 2867 * Arguments: dev - device number
2868 2868 * dip - pointer to device info structure
2869 2869 * prop_op - property operator
2870 2870 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2871 2871 * name - pointer to property name
2872 2872 * valuep - pointer or address of the user buffer
2873 2873 * lengthp - property length
2874 2874 *
2875 2875 * Return Code: DDI_PROP_SUCCESS
2876 2876 * DDI_PROP_NOT_FOUND
2877 2877 * DDI_PROP_UNDEFINED
2878 2878 * DDI_PROP_NO_MEMORY
2879 2879 * DDI_PROP_BUF_TOO_SMALL
2880 2880 */
2881 2881
2882 2882 static int
2883 2883 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2884 2884 char *name, caddr_t valuep, int *lengthp)
2885 2885 {
2886 2886 struct sd_lun *un;
2887 2887
2888 2888 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2889 2889 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2890 2890 name, valuep, lengthp));
2891 2891
2892 2892 return (cmlb_prop_op(un->un_cmlbhandle,
2893 2893 dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2894 2894 SDPART(dev), (void *)SD_PATH_DIRECT));
2895 2895 }
2896 2896
2897 2897 /*
2898 2898 * The following functions are for smart probing:
2899 2899 * sd_scsi_probe_cache_init()
2900 2900 * sd_scsi_probe_cache_fini()
2901 2901 * sd_scsi_clear_probe_cache()
2902 2902 * sd_scsi_probe_with_cache()
2903 2903 */
2904 2904
2905 2905 /*
2906 2906 * Function: sd_scsi_probe_cache_init
2907 2907 *
2908 2908 * Description: Initializes the probe response cache mutex and head pointer.
2909 2909 *
2910 2910 * Context: Kernel thread context
2911 2911 */
2912 2912
2913 2913 static void
2914 2914 sd_scsi_probe_cache_init(void)
2915 2915 {
2916 2916 mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2917 2917 sd_scsi_probe_cache_head = NULL;
2918 2918 }
2919 2919
2920 2920
2921 2921 /*
2922 2922 * Function: sd_scsi_probe_cache_fini
2923 2923 *
2924 2924 * Description: Frees all resources associated with the probe response cache.
2925 2925 *
2926 2926 * Context: Kernel thread context
2927 2927 */
2928 2928
2929 2929 static void
2930 2930 sd_scsi_probe_cache_fini(void)
2931 2931 {
2932 2932 struct sd_scsi_probe_cache *cp;
2933 2933 struct sd_scsi_probe_cache *ncp;
2934 2934
2935 2935 /* Clean up our smart probing linked list */
2936 2936 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2937 2937 ncp = cp->next;
2938 2938 kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2939 2939 }
2940 2940 sd_scsi_probe_cache_head = NULL;
2941 2941 mutex_destroy(&sd_scsi_probe_cache_mutex);
2942 2942 }
2943 2943
2944 2944
2945 2945 /*
2946 2946 * Function: sd_scsi_clear_probe_cache
2947 2947 *
2948 2948 * Description: This routine clears the probe response cache. This is
2949 2949 * done when open() returns ENXIO so that when deferred
2950 2950 * attach is attempted (possibly after a device has been
2951 2951 * turned on) we will retry the probe. Since we don't know
2952 2952 * which target we failed to open, we just clear the
2953 2953 * entire cache.
2954 2954 *
2955 2955 * Context: Kernel thread context
2956 2956 */
2957 2957
2958 2958 static void
2959 2959 sd_scsi_clear_probe_cache(void)
2960 2960 {
2961 2961 struct sd_scsi_probe_cache *cp;
2962 2962 int i;
2963 2963
2964 2964 mutex_enter(&sd_scsi_probe_cache_mutex);
2965 2965 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2966 2966 /*
2967 2967 * Reset all entries to SCSIPROBE_EXISTS. This will
2968 2968 * force probing to be performed the next time
2969 2969 * sd_scsi_probe_with_cache is called.
2970 2970 */
2971 2971 for (i = 0; i < NTARGETS_WIDE; i++) {
2972 2972 cp->cache[i] = SCSIPROBE_EXISTS;
2973 2973 }
2974 2974 }
2975 2975 mutex_exit(&sd_scsi_probe_cache_mutex);
2976 2976 }
2977 2977
2978 2978
2979 2979 /*
2980 2980 * Function: sd_scsi_probe_with_cache
2981 2981 *
2982 2982 * Description: This routine implements support for a scsi device probe
2983 2983 * with cache. The driver maintains a cache of the target
2984 2984 * responses to scsi probes. If we get no response from a
2985 2985 * target during a probe inquiry, we remember that, and we
2986 2986 * avoid additional calls to scsi_probe on non-zero LUNs
2987 2987 * on the same target until the cache is cleared. By doing
2988 2988 * so we avoid the 1/4 sec selection timeout for nonzero
2989 2989 * LUNs. lun0 of a target is always probed.
2990 2990 *
2991 2991 * Arguments: devp - Pointer to a scsi_device(9S) structure
2992 2992 * waitfunc - indicates what the allocator routines should
2993 2993 * do when resources are not available. This value
2994 2994 * is passed on to scsi_probe() when that routine
2995 2995 * is called.
2996 2996 *
2997 2997 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2998 2998 * otherwise the value returned by scsi_probe(9F).
2999 2999 *
3000 3000 * Context: Kernel thread context
3001 3001 */
3002 3002
3003 3003 static int
3004 3004 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3005 3005 {
3006 3006 struct sd_scsi_probe_cache *cp;
3007 3007 dev_info_t *pdip = ddi_get_parent(devp->sd_dev);
3008 3008 int lun, tgt;
3009 3009
3010 3010 lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3011 3011 SCSI_ADDR_PROP_LUN, 0);
3012 3012 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3013 3013 SCSI_ADDR_PROP_TARGET, -1);
3014 3014
3015 3015 /* Make sure caching enabled and target in range */
3016 3016 if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3017 3017 /* do it the old way (no cache) */
3018 3018 return (scsi_probe(devp, waitfn));
3019 3019 }
3020 3020
3021 3021 mutex_enter(&sd_scsi_probe_cache_mutex);
3022 3022
3023 3023 /* Find the cache for this scsi bus instance */
3024 3024 for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3025 3025 if (cp->pdip == pdip) {
3026 3026 break;
3027 3027 }
3028 3028 }
3029 3029
3030 3030 /* If we can't find a cache for this pdip, create one */
3031 3031 if (cp == NULL) {
3032 3032 int i;
3033 3033
3034 3034 cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3035 3035 KM_SLEEP);
3036 3036 cp->pdip = pdip;
3037 3037 cp->next = sd_scsi_probe_cache_head;
3038 3038 sd_scsi_probe_cache_head = cp;
3039 3039 for (i = 0; i < NTARGETS_WIDE; i++) {
3040 3040 cp->cache[i] = SCSIPROBE_EXISTS;
3041 3041 }
3042 3042 }
3043 3043
3044 3044 mutex_exit(&sd_scsi_probe_cache_mutex);
3045 3045
3046 3046 /* Recompute the cache for this target if LUN zero */
3047 3047 if (lun == 0) {
3048 3048 cp->cache[tgt] = SCSIPROBE_EXISTS;
3049 3049 }
3050 3050
3051 3051 /* Don't probe if cache remembers a NORESP from a previous LUN. */
3052 3052 if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3053 3053 return (SCSIPROBE_NORESP);
3054 3054 }
3055 3055
3056 3056 /* Do the actual probe; save & return the result */
3057 3057 return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3058 3058 }
3059 3059
3060 3060
3061 3061 /*
3062 3062 * Function: sd_scsi_target_lun_init
3063 3063 *
3064 3064 * Description: Initializes the attached lun chain mutex and head pointer.
3065 3065 *
3066 3066 * Context: Kernel thread context
3067 3067 */
3068 3068
3069 3069 static void
3070 3070 sd_scsi_target_lun_init(void)
3071 3071 {
3072 3072 mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3073 3073 sd_scsi_target_lun_head = NULL;
3074 3074 }
3075 3075
3076 3076
3077 3077 /*
3078 3078 * Function: sd_scsi_target_lun_fini
3079 3079 *
3080 3080 * Description: Frees all resources associated with the attached lun
3081 3081 * chain
3082 3082 *
3083 3083 * Context: Kernel thread context
3084 3084 */
3085 3085
3086 3086 static void
3087 3087 sd_scsi_target_lun_fini(void)
3088 3088 {
3089 3089 struct sd_scsi_hba_tgt_lun *cp;
3090 3090 struct sd_scsi_hba_tgt_lun *ncp;
3091 3091
3092 3092 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3093 3093 ncp = cp->next;
3094 3094 kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3095 3095 }
3096 3096 sd_scsi_target_lun_head = NULL;
3097 3097 mutex_destroy(&sd_scsi_target_lun_mutex);
3098 3098 }
3099 3099
3100 3100
3101 3101 /*
3102 3102 * Function: sd_scsi_get_target_lun_count
3103 3103 *
3104 3104 * Description: This routine will check in the attached lun chain to see
3105 3105 * how many luns are attached on the required SCSI controller
3106 3106 * and target. Currently, some capabilities like tagged queue
3107 3107 * are supported per target based by HBA. So all luns in a
3108 3108 * target have the same capabilities. Based on this assumption,
3109 3109 * sd should only set these capabilities once per target. This
3110 3110 * function is called when sd needs to decide how many luns
3111 3111 * already attached on a target.
3112 3112 *
3113 3113 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI
3114 3114 * controller device.
3115 3115 * target - The target ID on the controller's SCSI bus.
3116 3116 *
3117 3117 * Return Code: The number of luns attached on the required target and
3118 3118 * controller.
3119 3119 * -1 if target ID is not in parallel SCSI scope or the given
3120 3120 * dip is not in the chain.
3121 3121 *
3122 3122 * Context: Kernel thread context
3123 3123 */
3124 3124
3125 3125 static int
3126 3126 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3127 3127 {
3128 3128 struct sd_scsi_hba_tgt_lun *cp;
3129 3129
3130 3130 if ((target < 0) || (target >= NTARGETS_WIDE)) {
3131 3131 return (-1);
3132 3132 }
3133 3133
3134 3134 mutex_enter(&sd_scsi_target_lun_mutex);
3135 3135
3136 3136 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3137 3137 if (cp->pdip == dip) {
3138 3138 break;
3139 3139 }
3140 3140 }
3141 3141
3142 3142 mutex_exit(&sd_scsi_target_lun_mutex);
3143 3143
3144 3144 if (cp == NULL) {
3145 3145 return (-1);
3146 3146 }
3147 3147
3148 3148 return (cp->nlun[target]);
3149 3149 }
3150 3150
3151 3151
3152 3152 /*
3153 3153 * Function: sd_scsi_update_lun_on_target
3154 3154 *
3155 3155 * Description: This routine is used to update the attached lun chain when a
3156 3156 * lun is attached or detached on a target.
3157 3157 *
3158 3158 * Arguments: dip - Pointer to the system's dev_info_t for the SCSI
3159 3159 * controller device.
3160 3160 * target - The target ID on the controller's SCSI bus.
3161 3161 * flag - Indicate the lun is attached or detached.
3162 3162 *
3163 3163 * Context: Kernel thread context
3164 3164 */
3165 3165
3166 3166 static void
3167 3167 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3168 3168 {
3169 3169 struct sd_scsi_hba_tgt_lun *cp;
3170 3170
3171 3171 mutex_enter(&sd_scsi_target_lun_mutex);
3172 3172
3173 3173 for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3174 3174 if (cp->pdip == dip) {
3175 3175 break;
3176 3176 }
3177 3177 }
3178 3178
3179 3179 if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3180 3180 cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3181 3181 KM_SLEEP);
3182 3182 cp->pdip = dip;
3183 3183 cp->next = sd_scsi_target_lun_head;
3184 3184 sd_scsi_target_lun_head = cp;
3185 3185 }
3186 3186
3187 3187 mutex_exit(&sd_scsi_target_lun_mutex);
3188 3188
3189 3189 if (cp != NULL) {
3190 3190 if (flag == SD_SCSI_LUN_ATTACH) {
3191 3191 cp->nlun[target] ++;
3192 3192 } else {
3193 3193 cp->nlun[target] --;
3194 3194 }
3195 3195 }
3196 3196 }
3197 3197
3198 3198
3199 3199 /*
3200 3200 * Function: sd_spin_up_unit
3201 3201 *
3202 3202 * Description: Issues the following commands to spin-up the device:
3203 3203 * START STOP UNIT, and INQUIRY.
3204 3204 *
3205 3205 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3206 3206 * structure for this target.
3207 3207 *
3208 3208 * Return Code: 0 - success
3209 3209 * EIO - failure
3210 3210 * EACCES - reservation conflict
3211 3211 *
3212 3212 * Context: Kernel thread context
3213 3213 */
3214 3214
3215 3215 static int
3216 3216 sd_spin_up_unit(sd_ssc_t *ssc)
3217 3217 {
3218 3218 size_t resid = 0;
3219 3219 int has_conflict = FALSE;
3220 3220 uchar_t *bufaddr;
3221 3221 int status;
3222 3222 struct sd_lun *un;
3223 3223
3224 3224 ASSERT(ssc != NULL);
3225 3225 un = ssc->ssc_un;
3226 3226 ASSERT(un != NULL);
3227 3227
3228 3228 /*
3229 3229 * Send a throwaway START UNIT command.
3230 3230 *
3231 3231 * If we fail on this, we don't care presently what precisely
3232 3232 * is wrong. EMC's arrays will also fail this with a check
3233 3233 * condition (0x2/0x4/0x3) if the device is "inactive," but
3234 3234 * we don't want to fail the attach because it may become
3235 3235 * "active" later.
3236 3236 * We don't know if power condition is supported or not at
3237 3237 * this stage, use START STOP bit.
3238 3238 */
3239 3239 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3240 3240 SD_TARGET_START, SD_PATH_DIRECT);
3241 3241
3242 3242 if (status != 0) {
3243 3243 if (status == EACCES)
3244 3244 has_conflict = TRUE;
3245 3245 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3246 3246 }
3247 3247
3248 3248 /*
3249 3249 * Send another INQUIRY command to the target. This is necessary for
3250 3250 * non-removable media direct access devices because their INQUIRY data
3251 3251 * may not be fully qualified until they are spun up (perhaps via the
3252 3252 * START command above). Note: This seems to be needed for some
3253 3253 * legacy devices only.) The INQUIRY command should succeed even if a
3254 3254 * Reservation Conflict is present.
3255 3255 */
3256 3256 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3257 3257
3258 3258 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3259 3259 != 0) {
3260 3260 kmem_free(bufaddr, SUN_INQSIZE);
3261 3261 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3262 3262 return (EIO);
3263 3263 }
3264 3264
3265 3265 /*
3266 3266 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3267 3267 * Note that this routine does not return a failure here even if the
3268 3268 * INQUIRY command did not return any data. This is a legacy behavior.
3269 3269 */
3270 3270 if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3271 3271 bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3272 3272 }
3273 3273
3274 3274 kmem_free(bufaddr, SUN_INQSIZE);
3275 3275
3276 3276 /* If we hit a reservation conflict above, tell the caller. */
3277 3277 if (has_conflict == TRUE) {
3278 3278 return (EACCES);
3279 3279 }
3280 3280
3281 3281 return (0);
3282 3282 }
3283 3283
3284 3284 #ifdef _LP64
3285 3285 /*
3286 3286 * Function: sd_enable_descr_sense
3287 3287 *
3288 3288 * Description: This routine attempts to select descriptor sense format
3289 3289 * using the Control mode page. Devices that support 64 bit
3290 3290 * LBAs (for >2TB luns) should also implement descriptor
3291 3291 * sense data so we will call this function whenever we see
3292 3292 * a lun larger than 2TB. If for some reason the device
3293 3293 * supports 64 bit LBAs but doesn't support descriptor sense
3294 3294 * presumably the mode select will fail. Everything will
3295 3295 * continue to work normally except that we will not get
3296 3296 * complete sense data for commands that fail with an LBA
3297 3297 * larger than 32 bits.
3298 3298 *
3299 3299 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3300 3300 * structure for this target.
3301 3301 *
3302 3302 * Context: Kernel thread context only
3303 3303 */
3304 3304
3305 3305 static void
3306 3306 sd_enable_descr_sense(sd_ssc_t *ssc)
3307 3307 {
3308 3308 uchar_t *header;
3309 3309 struct mode_control_scsi3 *ctrl_bufp;
3310 3310 size_t buflen;
3311 3311 size_t bd_len;
3312 3312 int status;
3313 3313 struct sd_lun *un;
3314 3314
3315 3315 ASSERT(ssc != NULL);
3316 3316 un = ssc->ssc_un;
3317 3317 ASSERT(un != NULL);
3318 3318
3319 3319 /*
3320 3320 * Read MODE SENSE page 0xA, Control Mode Page
3321 3321 */
3322 3322 buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3323 3323 sizeof (struct mode_control_scsi3);
3324 3324 header = kmem_zalloc(buflen, KM_SLEEP);
3325 3325
3326 3326 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3327 3327 MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3328 3328
3329 3329 if (status != 0) {
3330 3330 SD_ERROR(SD_LOG_COMMON, un,
3331 3331 "sd_enable_descr_sense: mode sense ctrl page failed\n");
3332 3332 goto eds_exit;
3333 3333 }
3334 3334
3335 3335 /*
3336 3336 * Determine size of Block Descriptors in order to locate
3337 3337 * the mode page data. ATAPI devices return 0, SCSI devices
3338 3338 * should return MODE_BLK_DESC_LENGTH.
3339 3339 */
3340 3340 bd_len = ((struct mode_header *)header)->bdesc_length;
3341 3341
3342 3342 /* Clear the mode data length field for MODE SELECT */
3343 3343 ((struct mode_header *)header)->length = 0;
3344 3344
3345 3345 ctrl_bufp = (struct mode_control_scsi3 *)
3346 3346 (header + MODE_HEADER_LENGTH + bd_len);
3347 3347
3348 3348 /*
3349 3349 * If the page length is smaller than the expected value,
3350 3350 * the target device doesn't support D_SENSE. Bail out here.
3351 3351 */
3352 3352 if (ctrl_bufp->mode_page.length <
3353 3353 sizeof (struct mode_control_scsi3) - 2) {
3354 3354 SD_ERROR(SD_LOG_COMMON, un,
3355 3355 "sd_enable_descr_sense: enable D_SENSE failed\n");
3356 3356 goto eds_exit;
3357 3357 }
3358 3358
3359 3359 /*
3360 3360 * Clear PS bit for MODE SELECT
3361 3361 */
3362 3362 ctrl_bufp->mode_page.ps = 0;
3363 3363
3364 3364 /*
3365 3365 * Set D_SENSE to enable descriptor sense format.
3366 3366 */
3367 3367 ctrl_bufp->d_sense = 1;
3368 3368
3369 3369 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3370 3370
3371 3371 /*
3372 3372 * Use MODE SELECT to commit the change to the D_SENSE bit
3373 3373 */
3374 3374 status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3375 3375 buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3376 3376
3377 3377 if (status != 0) {
3378 3378 SD_INFO(SD_LOG_COMMON, un,
3379 3379 "sd_enable_descr_sense: mode select ctrl page failed\n");
3380 3380 } else {
3381 3381 kmem_free(header, buflen);
3382 3382 return;
3383 3383 }
3384 3384
3385 3385 eds_exit:
3386 3386 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3387 3387 kmem_free(header, buflen);
3388 3388 }
3389 3389
3390 3390 /*
3391 3391 * Function: sd_reenable_dsense_task
3392 3392 *
3393 3393 * Description: Re-enable descriptor sense after device or bus reset
3394 3394 *
3395 3395 * Context: Executes in a taskq() thread context
3396 3396 */
3397 3397 static void
3398 3398 sd_reenable_dsense_task(void *arg)
3399 3399 {
3400 3400 struct sd_lun *un = arg;
3401 3401 sd_ssc_t *ssc;
3402 3402
3403 3403 ASSERT(un != NULL);
3404 3404
3405 3405 ssc = sd_ssc_init(un);
3406 3406 sd_enable_descr_sense(ssc);
3407 3407 sd_ssc_fini(ssc);
3408 3408 }
3409 3409 #endif /* _LP64 */
3410 3410
3411 3411 /*
3412 3412 * Function: sd_set_mmc_caps
3413 3413 *
3414 3414 * Description: This routine determines if the device is MMC compliant and if
3415 3415 * the device supports CDDA via a mode sense of the CDVD
3416 3416 * capabilities mode page. Also checks if the device is a
3417 3417 * dvdram writable device.
3418 3418 *
3419 3419 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
3420 3420 * structure for this target.
3421 3421 *
3422 3422 * Context: Kernel thread context only
3423 3423 */
3424 3424
3425 3425 static void
3426 3426 sd_set_mmc_caps(sd_ssc_t *ssc)
3427 3427 {
3428 3428 struct mode_header_grp2 *sense_mhp;
3429 3429 uchar_t *sense_page;
3430 3430 caddr_t buf;
3431 3431 int bd_len;
3432 3432 int status;
3433 3433 struct uscsi_cmd com;
3434 3434 int rtn;
3435 3435 uchar_t *out_data_rw, *out_data_hd;
3436 3436 uchar_t *rqbuf_rw, *rqbuf_hd;
3437 3437 uchar_t *out_data_gesn;
3438 3438 int gesn_len;
3439 3439 struct sd_lun *un;
3440 3440
3441 3441 ASSERT(ssc != NULL);
3442 3442 un = ssc->ssc_un;
3443 3443 ASSERT(un != NULL);
3444 3444
3445 3445 /*
3446 3446 * The flags which will be set in this function are - mmc compliant,
3447 3447 * dvdram writable device, cdda support. Initialize them to FALSE
3448 3448 * and if a capability is detected - it will be set to TRUE.
3449 3449 */
3450 3450 un->un_f_mmc_cap = FALSE;
3451 3451 un->un_f_dvdram_writable_device = FALSE;
3452 3452 un->un_f_cfg_cdda = FALSE;
3453 3453
3454 3454 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3455 3455 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3456 3456 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3457 3457
3458 3458 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3459 3459
3460 3460 if (status != 0) {
3461 3461 /* command failed; just return */
3462 3462 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3463 3463 return;
3464 3464 }
3465 3465 /*
3466 3466 * If the mode sense request for the CDROM CAPABILITIES
3467 3467 * page (0x2A) succeeds the device is assumed to be MMC.
3468 3468 */
3469 3469 un->un_f_mmc_cap = TRUE;
3470 3470
3471 3471 /* See if GET STATUS EVENT NOTIFICATION is supported */
3472 3472 if (un->un_f_mmc_gesn_polling) {
3473 3473 gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3474 3474 out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3475 3475
3476 3476 rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3477 3477 out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3478 3478
3479 3479 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3480 3480
3481 3481 if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3482 3482 un->un_f_mmc_gesn_polling = FALSE;
3483 3483 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3484 3484 "sd_set_mmc_caps: gesn not supported "
3485 3485 "%d %x %x %x %x\n", rtn,
3486 3486 out_data_gesn[0], out_data_gesn[1],
3487 3487 out_data_gesn[2], out_data_gesn[3]);
3488 3488 }
3489 3489
3490 3490 kmem_free(out_data_gesn, gesn_len);
3491 3491 }
3492 3492
3493 3493 /* Get to the page data */
3494 3494 sense_mhp = (struct mode_header_grp2 *)buf;
3495 3495 bd_len = (sense_mhp->bdesc_length_hi << 8) |
3496 3496 sense_mhp->bdesc_length_lo;
3497 3497 if (bd_len > MODE_BLK_DESC_LENGTH) {
3498 3498 /*
3499 3499 * We did not get back the expected block descriptor
3500 3500 * length so we cannot determine if the device supports
3501 3501 * CDDA. However, we still indicate the device is MMC
3502 3502 * according to the successful response to the page
3503 3503 * 0x2A mode sense request.
3504 3504 */
3505 3505 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3506 3506 "sd_set_mmc_caps: Mode Sense returned "
3507 3507 "invalid block descriptor length\n");
3508 3508 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3509 3509 return;
3510 3510 }
3511 3511
3512 3512 /* See if read CDDA is supported */
3513 3513 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3514 3514 bd_len);
3515 3515 un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3516 3516
3517 3517 /* See if writing DVD RAM is supported. */
3518 3518 un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3519 3519 if (un->un_f_dvdram_writable_device == TRUE) {
3520 3520 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3521 3521 return;
3522 3522 }
3523 3523
3524 3524 /*
3525 3525 * If the device presents DVD or CD capabilities in the mode
3526 3526 * page, we can return here since a RRD will not have
3527 3527 * these capabilities.
3528 3528 */
3529 3529 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3530 3530 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3531 3531 return;
3532 3532 }
3533 3533 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3534 3534
3535 3535 /*
3536 3536 * If un->un_f_dvdram_writable_device is still FALSE,
3537 3537 * check for a Removable Rigid Disk (RRD). A RRD
3538 3538 * device is identified by the features RANDOM_WRITABLE and
3539 3539 * HARDWARE_DEFECT_MANAGEMENT.
3540 3540 */
3541 3541 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3542 3542 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3543 3543
3544 3544 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3545 3545 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3546 3546 RANDOM_WRITABLE, SD_PATH_STANDARD);
3547 3547
3548 3548 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3549 3549
3550 3550 if (rtn != 0) {
3551 3551 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3552 3552 kmem_free(rqbuf_rw, SENSE_LENGTH);
3553 3553 return;
3554 3554 }
3555 3555
3556 3556 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3557 3557 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3558 3558
3559 3559 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3560 3560 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3561 3561 HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3562 3562
3563 3563 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3564 3564
3565 3565 if (rtn == 0) {
3566 3566 /*
3567 3567 * We have good information, check for random writable
3568 3568 * and hardware defect features.
3569 3569 */
3570 3570 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3571 3571 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3572 3572 un->un_f_dvdram_writable_device = TRUE;
3573 3573 }
3574 3574 }
3575 3575
3576 3576 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3577 3577 kmem_free(rqbuf_rw, SENSE_LENGTH);
3578 3578 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3579 3579 kmem_free(rqbuf_hd, SENSE_LENGTH);
3580 3580 }
3581 3581
3582 3582 /*
3583 3583 * Function: sd_check_for_writable_cd
3584 3584 *
3585 3585 * Description: This routine determines if the media in the device is
3586 3586 * writable or not. It uses the get configuration command (0x46)
3587 3587 * to determine if the media is writable
3588 3588 *
3589 3589 * Arguments: un - driver soft state (unit) structure
3590 3590 * path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3591 3591 * chain and the normal command waitq, or
3592 3592 * SD_PATH_DIRECT_PRIORITY to use the USCSI
3593 3593 * "direct" chain and bypass the normal command
3594 3594 * waitq.
3595 3595 *
3596 3596 * Context: Never called at interrupt context.
3597 3597 */
3598 3598
3599 3599 static void
3600 3600 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3601 3601 {
3602 3602 struct uscsi_cmd com;
3603 3603 uchar_t *out_data;
3604 3604 uchar_t *rqbuf;
3605 3605 int rtn;
3606 3606 uchar_t *out_data_rw, *out_data_hd;
3607 3607 uchar_t *rqbuf_rw, *rqbuf_hd;
3608 3608 struct mode_header_grp2 *sense_mhp;
3609 3609 uchar_t *sense_page;
3610 3610 caddr_t buf;
3611 3611 int bd_len;
3612 3612 int status;
3613 3613 struct sd_lun *un;
3614 3614
3615 3615 ASSERT(ssc != NULL);
3616 3616 un = ssc->ssc_un;
3617 3617 ASSERT(un != NULL);
3618 3618 ASSERT(mutex_owned(SD_MUTEX(un)));
3619 3619
3620 3620 /*
3621 3621 * Initialize the writable media to false, if configuration info.
3622 3622 * tells us otherwise then only we will set it.
3623 3623 */
3624 3624 un->un_f_mmc_writable_media = FALSE;
3625 3625 mutex_exit(SD_MUTEX(un));
3626 3626
3627 3627 out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3628 3628 rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3629 3629
3630 3630 rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3631 3631 out_data, SD_PROFILE_HEADER_LEN, path_flag);
3632 3632
3633 3633 if (rtn != 0)
3634 3634 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3635 3635
3636 3636 mutex_enter(SD_MUTEX(un));
3637 3637 if (rtn == 0) {
3638 3638 /*
3639 3639 * We have good information, check for writable DVD.
3640 3640 */
3641 3641 if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3642 3642 un->un_f_mmc_writable_media = TRUE;
3643 3643 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3644 3644 kmem_free(rqbuf, SENSE_LENGTH);
3645 3645 return;
3646 3646 }
3647 3647 }
3648 3648
3649 3649 kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3650 3650 kmem_free(rqbuf, SENSE_LENGTH);
3651 3651
3652 3652 /*
3653 3653 * Determine if this is a RRD type device.
3654 3654 */
3655 3655 mutex_exit(SD_MUTEX(un));
3656 3656 buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3657 3657 status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3658 3658 BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3659 3659
3660 3660 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3661 3661
3662 3662 mutex_enter(SD_MUTEX(un));
3663 3663 if (status != 0) {
3664 3664 /* command failed; just return */
3665 3665 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3666 3666 return;
3667 3667 }
3668 3668
3669 3669 /* Get to the page data */
3670 3670 sense_mhp = (struct mode_header_grp2 *)buf;
3671 3671 bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3672 3672 if (bd_len > MODE_BLK_DESC_LENGTH) {
3673 3673 /*
3674 3674 * We did not get back the expected block descriptor length so
3675 3675 * we cannot check the mode page.
3676 3676 */
3677 3677 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3678 3678 "sd_check_for_writable_cd: Mode Sense returned "
3679 3679 "invalid block descriptor length\n");
3680 3680 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3681 3681 return;
3682 3682 }
3683 3683
3684 3684 /*
3685 3685 * If the device presents DVD or CD capabilities in the mode
3686 3686 * page, we can return here since a RRD device will not have
3687 3687 * these capabilities.
3688 3688 */
3689 3689 sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3690 3690 if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3691 3691 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3692 3692 return;
3693 3693 }
3694 3694 kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3695 3695
3696 3696 /*
3697 3697 * If un->un_f_mmc_writable_media is still FALSE,
3698 3698 * check for RRD type media. A RRD device is identified
3699 3699 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3700 3700 */
3701 3701 mutex_exit(SD_MUTEX(un));
3702 3702 out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3703 3703 rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3704 3704
3705 3705 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3706 3706 SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3707 3707 RANDOM_WRITABLE, path_flag);
3708 3708
3709 3709 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3710 3710 if (rtn != 0) {
3711 3711 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3712 3712 kmem_free(rqbuf_rw, SENSE_LENGTH);
3713 3713 mutex_enter(SD_MUTEX(un));
3714 3714 return;
3715 3715 }
3716 3716
3717 3717 out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3718 3718 rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3719 3719
3720 3720 rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3721 3721 SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3722 3722 HARDWARE_DEFECT_MANAGEMENT, path_flag);
3723 3723
3724 3724 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3725 3725 mutex_enter(SD_MUTEX(un));
3726 3726 if (rtn == 0) {
3727 3727 /*
3728 3728 * We have good information, check for random writable
3729 3729 * and hardware defect features as current.
3730 3730 */
3731 3731 if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3732 3732 (out_data_rw[10] & 0x1) &&
3733 3733 (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3734 3734 (out_data_hd[10] & 0x1)) {
3735 3735 un->un_f_mmc_writable_media = TRUE;
3736 3736 }
3737 3737 }
3738 3738
3739 3739 kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3740 3740 kmem_free(rqbuf_rw, SENSE_LENGTH);
3741 3741 kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3742 3742 kmem_free(rqbuf_hd, SENSE_LENGTH);
3743 3743 }
3744 3744
3745 3745 /*
3746 3746 * Function: sd_read_unit_properties
3747 3747 *
3748 3748 * Description: The following implements a property lookup mechanism.
3749 3749 * Properties for particular disks (keyed on vendor, model
3750 3750 * and rev numbers) are sought in the sd.conf file via
3751 3751 * sd_process_sdconf_file(), and if not found there, are
3752 3752 * looked for in a list hardcoded in this driver via
3753 3753 * sd_process_sdconf_table() Once located the properties
3754 3754 * are used to update the driver unit structure.
3755 3755 *
3756 3756 * Arguments: un - driver soft state (unit) structure
3757 3757 */
3758 3758
3759 3759 static void
3760 3760 sd_read_unit_properties(struct sd_lun *un)
3761 3761 {
3762 3762 /*
3763 3763 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3764 3764 * the "sd-config-list" property (from the sd.conf file) or if
3765 3765 * there was not a match for the inquiry vid/pid. If this event
3766 3766 * occurs the static driver configuration table is searched for
3767 3767 * a match.
3768 3768 */
3769 3769 ASSERT(un != NULL);
3770 3770 if (sd_process_sdconf_file(un) == SD_FAILURE) {
3771 3771 sd_process_sdconf_table(un);
3772 3772 }
3773 3773
3774 3774 /* check for LSI device */
3775 3775 sd_is_lsi(un);
3776 3776
3777 3777
3778 3778 }
3779 3779
3780 3780
3781 3781 /*
3782 3782 * Function: sd_process_sdconf_file
3783 3783 *
3784 3784 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3785 3785 * driver's config file (ie, sd.conf) and update the driver
3786 3786 * soft state structure accordingly.
3787 3787 *
3788 3788 * Arguments: un - driver soft state (unit) structure
3789 3789 *
3790 3790 * Return Code: SD_SUCCESS - The properties were successfully set according
3791 3791 * to the driver configuration file.
3792 3792 * SD_FAILURE - The driver config list was not obtained or
3793 3793 * there was no vid/pid match. This indicates that
3794 3794 * the static config table should be used.
3795 3795 *
3796 3796 * The config file has a property, "sd-config-list". Currently we support
3797 3797 * two kinds of formats. For both formats, the value of this property
3798 3798 * is a list of duplets:
3799 3799 *
3800 3800 * sd-config-list=
3801 3801 * <duplet>,
3802 3802 * [,<duplet>]*;
3803 3803 *
3804 3804 * For the improved format, where
3805 3805 *
3806 3806 * <duplet>:= "<vid+pid>","<tunable-list>"
3807 3807 *
3808 3808 * and
3809 3809 *
3810 3810 * <tunable-list>:= <tunable> [, <tunable> ]*;
3811 3811 * <tunable> = <name> : <value>
3812 3812 *
3813 3813 * The <vid+pid> is the string that is returned by the target device on a
3814 3814 * SCSI inquiry command, the <tunable-list> contains one or more tunables
3815 3815 * to apply to all target devices with the specified <vid+pid>.
3816 3816 *
3817 3817 * Each <tunable> is a "<name> : <value>" pair.
3818 3818 *
3819 3819 * For the old format, the structure of each duplet is as follows:
3820 3820 *
3821 3821 * <duplet>:= "<vid+pid>","<data-property-name_list>"
3822 3822 *
3823 3823 * The first entry of the duplet is the device ID string (the concatenated
3824 3824 * vid & pid; not to be confused with a device_id). This is defined in
3825 3825 * the same way as in the sd_disk_table.
3826 3826 *
3827 3827 * The second part of the duplet is a string that identifies a
3828 3828 * data-property-name-list. The data-property-name-list is defined as
3829 3829 * follows:
3830 3830 *
3831 3831 * <data-property-name-list>:=<data-property-name> [<data-property-name>]
3832 3832 *
3833 3833 * The syntax of <data-property-name> depends on the <version> field.
3834 3834 *
3835 3835 * If version = SD_CONF_VERSION_1 we have the following syntax:
3836 3836 *
3837 3837 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3838 3838 *
3839 3839 * where the prop0 value will be used to set prop0 if bit0 set in the
3840 3840 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3841 3841 *
3842 3842 */
3843 3843
3844 3844 static int
3845 3845 sd_process_sdconf_file(struct sd_lun *un)
3846 3846 {
3847 3847 char **config_list = NULL;
3848 3848 uint_t nelements;
3849 3849 char *vidptr;
3850 3850 int vidlen;
3851 3851 char *dnlist_ptr;
3852 3852 char *dataname_ptr;
3853 3853 char *dataname_lasts;
3854 3854 int *data_list = NULL;
3855 3855 uint_t data_list_len;
3856 3856 int rval = SD_FAILURE;
3857 3857 int i;
3858 3858
3859 3859 ASSERT(un != NULL);
3860 3860
3861 3861 /* Obtain the configuration list associated with the .conf file */
3862 3862 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3863 3863 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3864 3864 &config_list, &nelements) != DDI_PROP_SUCCESS) {
3865 3865 return (SD_FAILURE);
3866 3866 }
3867 3867
3868 3868 /*
3869 3869 * Compare vids in each duplet to the inquiry vid - if a match is
3870 3870 * made, get the data value and update the soft state structure
3871 3871 * accordingly.
3872 3872 *
3873 3873 * Each duplet should show as a pair of strings, return SD_FAILURE
3874 3874 * otherwise.
3875 3875 */
3876 3876 if (nelements & 1) {
3877 3877 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3878 3878 "sd-config-list should show as pairs of strings.\n");
3879 3879 if (config_list)
3880 3880 ddi_prop_free(config_list);
3881 3881 return (SD_FAILURE);
3882 3882 }
3883 3883
3884 3884 for (i = 0; i < nelements; i += 2) {
3885 3885 /*
3886 3886 * Note: The assumption here is that each vid entry is on
3887 3887 * a unique line from its associated duplet.
3888 3888 */
3889 3889 vidptr = config_list[i];
3890 3890 vidlen = (int)strlen(vidptr);
3891 3891 if ((vidlen == 0) ||
3892 3892 (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3893 3893 continue;
3894 3894 }
3895 3895
3896 3896 /*
3897 3897 * dnlist contains 1 or more blank separated
3898 3898 * data-property-name entries
3899 3899 */
3900 3900 dnlist_ptr = config_list[i + 1];
3901 3901
3902 3902 if (strchr(dnlist_ptr, ':') != NULL) {
3903 3903 /*
3904 3904 * Decode the improved format sd-config-list.
3905 3905 */
3906 3906 sd_nvpair_str_decode(un, dnlist_ptr);
3907 3907 } else {
3908 3908 /*
3909 3909 * The old format sd-config-list, loop through all
3910 3910 * data-property-name entries in the
3911 3911 * data-property-name-list
3912 3912 * setting the properties for each.
3913 3913 */
3914 3914 for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3915 3915 &dataname_lasts); dataname_ptr != NULL;
3916 3916 dataname_ptr = sd_strtok_r(NULL, " \t",
3917 3917 &dataname_lasts)) {
3918 3918 int version;
3919 3919
3920 3920 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3921 3921 "sd_process_sdconf_file: disk:%s, "
3922 3922 "data:%s\n", vidptr, dataname_ptr);
3923 3923
3924 3924 /* Get the data list */
3925 3925 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3926 3926 SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3927 3927 &data_list_len) != DDI_PROP_SUCCESS) {
3928 3928 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3929 3929 "sd_process_sdconf_file: data "
3930 3930 "property (%s) has no value\n",
3931 3931 dataname_ptr);
3932 3932 continue;
3933 3933 }
3934 3934
3935 3935 version = data_list[0];
3936 3936
3937 3937 if (version == SD_CONF_VERSION_1) {
3938 3938 sd_tunables values;
3939 3939
3940 3940 /* Set the properties */
3941 3941 if (sd_chk_vers1_data(un, data_list[1],
3942 3942 &data_list[2], data_list_len,
3943 3943 dataname_ptr) == SD_SUCCESS) {
3944 3944 sd_get_tunables_from_conf(un,
3945 3945 data_list[1], &data_list[2],
3946 3946 &values);
3947 3947 sd_set_vers1_properties(un,
3948 3948 data_list[1], &values);
3949 3949 rval = SD_SUCCESS;
3950 3950 } else {
3951 3951 rval = SD_FAILURE;
3952 3952 }
3953 3953 } else {
3954 3954 scsi_log(SD_DEVINFO(un), sd_label,
3955 3955 CE_WARN, "data property %s version "
3956 3956 "0x%x is invalid.",
3957 3957 dataname_ptr, version);
3958 3958 rval = SD_FAILURE;
3959 3959 }
3960 3960 if (data_list)
3961 3961 ddi_prop_free(data_list);
3962 3962 }
3963 3963 }
3964 3964 }
3965 3965
3966 3966 /* free up the memory allocated by ddi_prop_lookup_string_array(). */
3967 3967 if (config_list) {
3968 3968 ddi_prop_free(config_list);
3969 3969 }
3970 3970
3971 3971 return (rval);
3972 3972 }
3973 3973
3974 3974 /*
3975 3975 * Function: sd_nvpair_str_decode()
3976 3976 *
3977 3977 * Description: Parse the improved format sd-config-list to get
3978 3978 * each entry of tunable, which includes a name-value pair.
3979 3979 * Then call sd_set_properties() to set the property.
3980 3980 *
3981 3981 * Arguments: un - driver soft state (unit) structure
3982 3982 * nvpair_str - the tunable list
3983 3983 */
3984 3984 static void
3985 3985 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3986 3986 {
3987 3987 char *nv, *name, *value, *token;
3988 3988 char *nv_lasts, *v_lasts, *x_lasts;
3989 3989
3990 3990 for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3991 3991 nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3992 3992 token = sd_strtok_r(nv, ":", &v_lasts);
3993 3993 name = sd_strtok_r(token, " \t", &x_lasts);
3994 3994 token = sd_strtok_r(NULL, ":", &v_lasts);
3995 3995 value = sd_strtok_r(token, " \t", &x_lasts);
3996 3996 if (name == NULL || value == NULL) {
3997 3997 SD_INFO(SD_LOG_ATTACH_DETACH, un,
3998 3998 "sd_nvpair_str_decode: "
3999 3999 "name or value is not valid!\n");
4000 4000 } else {
4001 4001 sd_set_properties(un, name, value);
4002 4002 }
4003 4003 }
4004 4004 }
4005 4005
4006 4006 /*
4007 4007 * Function: sd_strtok_r()
4008 4008 *
4009 4009 * Description: This function uses strpbrk and strspn to break
4010 4010 * string into tokens on sequentially subsequent calls. Return
4011 4011 * NULL when no non-separator characters remain. The first
4012 4012 * argument is NULL for subsequent calls.
4013 4013 */
4014 4014 static char *
4015 4015 sd_strtok_r(char *string, const char *sepset, char **lasts)
4016 4016 {
4017 4017 char *q, *r;
4018 4018
4019 4019 /* First or subsequent call */
4020 4020 if (string == NULL)
4021 4021 string = *lasts;
4022 4022
4023 4023 if (string == NULL)
4024 4024 return (NULL);
4025 4025
4026 4026 /* Skip leading separators */
4027 4027 q = string + strspn(string, sepset);
4028 4028
4029 4029 if (*q == '\0')
4030 4030 return (NULL);
4031 4031
4032 4032 if ((r = strpbrk(q, sepset)) == NULL)
4033 4033 *lasts = NULL;
4034 4034 else {
4035 4035 *r = '\0';
4036 4036 *lasts = r + 1;
4037 4037 }
4038 4038 return (q);
4039 4039 }
4040 4040
4041 4041 /*
4042 4042 * Function: sd_set_properties()
4043 4043 *
4044 4044 * Description: Set device properties based on the improved
4045 4045 * format sd-config-list.
4046 4046 *
4047 4047 * Arguments: un - driver soft state (unit) structure
4048 4048 * name - supported tunable name
4049 4049 * value - tunable value
4050 4050 */
4051 4051 static void
4052 4052 sd_set_properties(struct sd_lun *un, char *name, char *value)
4053 4053 {
4054 4054 char *endptr = NULL;
4055 4055 long val = 0;
4056 4056
4057 4057 if (strcasecmp(name, "cache-nonvolatile") == 0) {
4058 4058 if (strcasecmp(value, "true") == 0) {
4059 4059 un->un_f_suppress_cache_flush = TRUE;
4060 4060 } else if (strcasecmp(value, "false") == 0) {
4061 4061 un->un_f_suppress_cache_flush = FALSE;
4062 4062 } else {
4063 4063 goto value_invalid;
4064 4064 }
4065 4065 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4066 4066 "suppress_cache_flush flag set to %d\n",
4067 4067 un->un_f_suppress_cache_flush);
4068 4068 return;
4069 4069 }
4070 4070
4071 4071 if (strcasecmp(name, "controller-type") == 0) {
4072 4072 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4073 4073 un->un_ctype = val;
4074 4074 } else {
4075 4075 goto value_invalid;
4076 4076 }
4077 4077 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4078 4078 "ctype set to %d\n", un->un_ctype);
4079 4079 return;
4080 4080 }
4081 4081
4082 4082 if (strcasecmp(name, "delay-busy") == 0) {
4083 4083 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4084 4084 un->un_busy_timeout = drv_usectohz(val / 1000);
4085 4085 } else {
4086 4086 goto value_invalid;
4087 4087 }
4088 4088 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4089 4089 "busy_timeout set to %d\n", un->un_busy_timeout);
4090 4090 return;
4091 4091 }
4092 4092
4093 4093 if (strcasecmp(name, "disksort") == 0) {
4094 4094 if (strcasecmp(value, "true") == 0) {
4095 4095 un->un_f_disksort_disabled = FALSE;
4096 4096 } else if (strcasecmp(value, "false") == 0) {
4097 4097 un->un_f_disksort_disabled = TRUE;
4098 4098 } else {
4099 4099 goto value_invalid;
4100 4100 }
4101 4101 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4102 4102 "disksort disabled flag set to %d\n",
4103 4103 un->un_f_disksort_disabled);
4104 4104 return;
4105 4105 }
4106 4106
4107 4107 if (strcasecmp(name, "power-condition") == 0) {
4108 4108 if (strcasecmp(value, "true") == 0) {
4109 4109 un->un_f_power_condition_disabled = FALSE;
4110 4110 } else if (strcasecmp(value, "false") == 0) {
4111 4111 un->un_f_power_condition_disabled = TRUE;
4112 4112 } else {
4113 4113 goto value_invalid;
4114 4114 }
4115 4115 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4116 4116 "power condition disabled flag set to %d\n",
4117 4117 un->un_f_power_condition_disabled);
4118 4118 return;
4119 4119 }
4120 4120
4121 4121 if (strcasecmp(name, "timeout-releasereservation") == 0) {
4122 4122 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4123 4123 un->un_reserve_release_time = val;
4124 4124 } else {
4125 4125 goto value_invalid;
4126 4126 }
4127 4127 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4128 4128 "reservation release timeout set to %d\n",
4129 4129 un->un_reserve_release_time);
4130 4130 return;
4131 4131 }
4132 4132
4133 4133 if (strcasecmp(name, "reset-lun") == 0) {
4134 4134 if (strcasecmp(value, "true") == 0) {
4135 4135 un->un_f_lun_reset_enabled = TRUE;
4136 4136 } else if (strcasecmp(value, "false") == 0) {
4137 4137 un->un_f_lun_reset_enabled = FALSE;
4138 4138 } else {
4139 4139 goto value_invalid;
4140 4140 }
4141 4141 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4142 4142 "lun reset enabled flag set to %d\n",
4143 4143 un->un_f_lun_reset_enabled);
4144 4144 return;
4145 4145 }
4146 4146
4147 4147 if (strcasecmp(name, "retries-busy") == 0) {
4148 4148 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4149 4149 un->un_busy_retry_count = val;
4150 4150 } else {
4151 4151 goto value_invalid;
4152 4152 }
4153 4153 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4154 4154 "busy retry count set to %d\n", un->un_busy_retry_count);
4155 4155 return;
4156 4156 }
4157 4157
4158 4158 if (strcasecmp(name, "retries-timeout") == 0) {
4159 4159 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4160 4160 un->un_retry_count = val;
4161 4161 } else {
4162 4162 goto value_invalid;
4163 4163 }
4164 4164 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4165 4165 "timeout retry count set to %d\n", un->un_retry_count);
4166 4166 return;
4167 4167 }
4168 4168
4169 4169 if (strcasecmp(name, "retries-notready") == 0) {
4170 4170 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4171 4171 un->un_notready_retry_count = val;
4172 4172 } else {
4173 4173 goto value_invalid;
4174 4174 }
4175 4175 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4176 4176 "notready retry count set to %d\n",
4177 4177 un->un_notready_retry_count);
4178 4178 return;
4179 4179 }
4180 4180
4181 4181 if (strcasecmp(name, "retries-reset") == 0) {
4182 4182 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4183 4183 un->un_reset_retry_count = val;
4184 4184 } else {
4185 4185 goto value_invalid;
4186 4186 }
4187 4187 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4188 4188 "reset retry count set to %d\n",
4189 4189 un->un_reset_retry_count);
4190 4190 return;
4191 4191 }
4192 4192
4193 4193 if (strcasecmp(name, "throttle-max") == 0) {
4194 4194 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4195 4195 un->un_saved_throttle = un->un_throttle = val;
4196 4196 } else {
4197 4197 goto value_invalid;
4198 4198 }
4199 4199 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4200 4200 "throttle set to %d\n", un->un_throttle);
4201 4201 }
4202 4202
4203 4203 if (strcasecmp(name, "throttle-min") == 0) {
4204 4204 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4205 4205 un->un_min_throttle = val;
4206 4206 } else {
4207 4207 goto value_invalid;
4208 4208 }
4209 4209 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4210 4210 "min throttle set to %d\n", un->un_min_throttle);
4211 4211 }
4212 4212
4213 4213 if (strcasecmp(name, "rmw-type") == 0) {
4214 4214 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4215 4215 un->un_f_rmw_type = val;
4216 4216 } else {
4217 4217 goto value_invalid;
4218 4218 }
4219 4219 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4220 4220 "RMW type set to %d\n", un->un_f_rmw_type);
4221 4221 }
4222 4222
4223 4223 if (strcasecmp(name, "physical-block-size") == 0) {
4224 4224 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4225 4225 ISP2(val) && val >= un->un_tgt_blocksize &&
4226 4226 val >= un->un_sys_blocksize) {
4227 4227 un->un_phy_blocksize = val;
4228 4228 } else {
4229 4229 goto value_invalid;
4230 4230 }
4231 4231 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4232 4232 "physical block size set to %d\n", un->un_phy_blocksize);
4233 4233 }
4234 4234
4235 4235 /*
4236 4236 * Validate the throttle values.
4237 4237 * If any of the numbers are invalid, set everything to defaults.
4238 4238 */
4239 4239 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4240 4240 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4241 4241 (un->un_min_throttle > un->un_throttle)) {
4242 4242 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4243 4243 un->un_min_throttle = sd_min_throttle;
4244 4244 }
4245 4245
4246 4246 if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4247 4247 if (strcasecmp(value, "true") == 0) {
4248 4248 un->un_f_mmc_gesn_polling = TRUE;
4249 4249 } else if (strcasecmp(value, "false") == 0) {
4250 4250 un->un_f_mmc_gesn_polling = FALSE;
4251 4251 } else {
4252 4252 goto value_invalid;
4253 4253 }
4254 4254 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4255 4255 "mmc-gesn-polling set to %d\n",
4256 4256 un->un_f_mmc_gesn_polling);
4257 4257 }
4258 4258
4259 4259 return;
4260 4260
4261 4261 value_invalid:
4262 4262 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4263 4263 "value of prop %s is invalid\n", name);
4264 4264 }
4265 4265
4266 4266 /*
4267 4267 * Function: sd_get_tunables_from_conf()
4268 4268 *
4269 4269 *
4270 4270 * This function reads the data list from the sd.conf file and pulls
4271 4271 * the values that can have numeric values as arguments and places
4272 4272 * the values in the appropriate sd_tunables member.
4273 4273 * Since the order of the data list members varies across platforms
4274 4274 * This function reads them from the data list in a platform specific
4275 4275 * order and places them into the correct sd_tunable member that is
4276 4276 * consistent across all platforms.
4277 4277 */
4278 4278 static void
4279 4279 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4280 4280 sd_tunables *values)
4281 4281 {
4282 4282 int i;
4283 4283 int mask;
4284 4284
4285 4285 bzero(values, sizeof (sd_tunables));
4286 4286
4287 4287 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4288 4288
4289 4289 mask = 1 << i;
4290 4290 if (mask > flags) {
4291 4291 break;
4292 4292 }
4293 4293
4294 4294 switch (mask & flags) {
4295 4295 case 0: /* This mask bit not set in flags */
4296 4296 continue;
4297 4297 case SD_CONF_BSET_THROTTLE:
4298 4298 values->sdt_throttle = data_list[i];
4299 4299 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4300 4300 "sd_get_tunables_from_conf: throttle = %d\n",
4301 4301 values->sdt_throttle);
4302 4302 break;
4303 4303 case SD_CONF_BSET_CTYPE:
4304 4304 values->sdt_ctype = data_list[i];
4305 4305 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4306 4306 "sd_get_tunables_from_conf: ctype = %d\n",
4307 4307 values->sdt_ctype);
4308 4308 break;
4309 4309 case SD_CONF_BSET_NRR_COUNT:
4310 4310 values->sdt_not_rdy_retries = data_list[i];
4311 4311 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4312 4312 "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4313 4313 values->sdt_not_rdy_retries);
4314 4314 break;
4315 4315 case SD_CONF_BSET_BSY_RETRY_COUNT:
4316 4316 values->sdt_busy_retries = data_list[i];
4317 4317 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4318 4318 "sd_get_tunables_from_conf: busy_retries = %d\n",
4319 4319 values->sdt_busy_retries);
4320 4320 break;
4321 4321 case SD_CONF_BSET_RST_RETRIES:
4322 4322 values->sdt_reset_retries = data_list[i];
4323 4323 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4324 4324 "sd_get_tunables_from_conf: reset_retries = %d\n",
4325 4325 values->sdt_reset_retries);
4326 4326 break;
4327 4327 case SD_CONF_BSET_RSV_REL_TIME:
4328 4328 values->sdt_reserv_rel_time = data_list[i];
4329 4329 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4330 4330 "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4331 4331 values->sdt_reserv_rel_time);
4332 4332 break;
4333 4333 case SD_CONF_BSET_MIN_THROTTLE:
4334 4334 values->sdt_min_throttle = data_list[i];
4335 4335 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4336 4336 "sd_get_tunables_from_conf: min_throttle = %d\n",
4337 4337 values->sdt_min_throttle);
4338 4338 break;
4339 4339 case SD_CONF_BSET_DISKSORT_DISABLED:
4340 4340 values->sdt_disk_sort_dis = data_list[i];
4341 4341 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4342 4342 "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4343 4343 values->sdt_disk_sort_dis);
4344 4344 break;
4345 4345 case SD_CONF_BSET_LUN_RESET_ENABLED:
4346 4346 values->sdt_lun_reset_enable = data_list[i];
4347 4347 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4348 4348 "sd_get_tunables_from_conf: lun_reset_enable = %d"
4349 4349 "\n", values->sdt_lun_reset_enable);
4350 4350 break;
4351 4351 case SD_CONF_BSET_CACHE_IS_NV:
4352 4352 values->sdt_suppress_cache_flush = data_list[i];
4353 4353 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4354 4354 "sd_get_tunables_from_conf: \
4355 4355 suppress_cache_flush = %d"
4356 4356 "\n", values->sdt_suppress_cache_flush);
4357 4357 break;
4358 4358 case SD_CONF_BSET_PC_DISABLED:
4359 4359 values->sdt_disk_sort_dis = data_list[i];
4360 4360 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4361 4361 "sd_get_tunables_from_conf: power_condition_dis = "
4362 4362 "%d\n", values->sdt_power_condition_dis);
4363 4363 break;
4364 4364 }
4365 4365 }
4366 4366 }
4367 4367
4368 4368 /*
4369 4369 * Function: sd_process_sdconf_table
4370 4370 *
4371 4371 * Description: Search the static configuration table for a match on the
4372 4372 * inquiry vid/pid and update the driver soft state structure
4373 4373 * according to the table property values for the device.
4374 4374 *
4375 4375 * The form of a configuration table entry is:
4376 4376 * <vid+pid>,<flags>,<property-data>
4377 4377 * "SEAGATE ST42400N",1,0x40000,
4378 4378 * 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4379 4379 *
4380 4380 * Arguments: un - driver soft state (unit) structure
4381 4381 */
4382 4382
4383 4383 static void
4384 4384 sd_process_sdconf_table(struct sd_lun *un)
4385 4385 {
4386 4386 char *id = NULL;
4387 4387 int table_index;
4388 4388 int idlen;
4389 4389
4390 4390 ASSERT(un != NULL);
4391 4391 for (table_index = 0; table_index < sd_disk_table_size;
4392 4392 table_index++) {
4393 4393 id = sd_disk_table[table_index].device_id;
4394 4394 idlen = strlen(id);
4395 4395 if (idlen == 0) {
4396 4396 continue;
4397 4397 }
4398 4398
4399 4399 /*
4400 4400 * The static configuration table currently does not
4401 4401 * implement version 10 properties. Additionally,
4402 4402 * multiple data-property-name entries are not
4403 4403 * implemented in the static configuration table.
4404 4404 */
4405 4405 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4406 4406 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4407 4407 "sd_process_sdconf_table: disk %s\n", id);
4408 4408 sd_set_vers1_properties(un,
4409 4409 sd_disk_table[table_index].flags,
4410 4410 sd_disk_table[table_index].properties);
4411 4411 break;
4412 4412 }
4413 4413 }
4414 4414 }
4415 4415
4416 4416
4417 4417 /*
4418 4418 * Function: sd_sdconf_id_match
4419 4419 *
4420 4420 * Description: This local function implements a case sensitive vid/pid
4421 4421 * comparison as well as the boundary cases of wild card and
4422 4422 * multiple blanks.
4423 4423 *
4424 4424 * Note: An implicit assumption made here is that the scsi
4425 4425 * inquiry structure will always keep the vid, pid and
4426 4426 * revision strings in consecutive sequence, so they can be
4427 4427 * read as a single string. If this assumption is not the
4428 4428 * case, a separate string, to be used for the check, needs
4429 4429 * to be built with these strings concatenated.
4430 4430 *
4431 4431 * Arguments: un - driver soft state (unit) structure
4432 4432 * id - table or config file vid/pid
4433 4433 * idlen - length of the vid/pid (bytes)
4434 4434 *
4435 4435 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4436 4436 * SD_FAILURE - Indicates no match with the inquiry vid/pid
4437 4437 */
4438 4438
4439 4439 static int
4440 4440 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4441 4441 {
4442 4442 struct scsi_inquiry *sd_inq;
4443 4443 int rval = SD_SUCCESS;
4444 4444
4445 4445 ASSERT(un != NULL);
4446 4446 sd_inq = un->un_sd->sd_inq;
4447 4447 ASSERT(id != NULL);
4448 4448
4449 4449 /*
4450 4450 * We use the inq_vid as a pointer to a buffer containing the
4451 4451 * vid and pid and use the entire vid/pid length of the table
4452 4452 * entry for the comparison. This works because the inq_pid
4453 4453 * data member follows inq_vid in the scsi_inquiry structure.
4454 4454 */
4455 4455 if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4456 4456 /*
4457 4457 * The user id string is compared to the inquiry vid/pid
4458 4458 * using a case insensitive comparison and ignoring
4459 4459 * multiple spaces.
4460 4460 */
4461 4461 rval = sd_blank_cmp(un, id, idlen);
4462 4462 if (rval != SD_SUCCESS) {
4463 4463 /*
4464 4464 * User id strings that start and end with a "*"
4465 4465 * are a special case. These do not have a
4466 4466 * specific vendor, and the product string can
4467 4467 * appear anywhere in the 16 byte PID portion of
4468 4468 * the inquiry data. This is a simple strstr()
4469 4469 * type search for the user id in the inquiry data.
4470 4470 */
4471 4471 if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4472 4472 char *pidptr = &id[1];
4473 4473 int i;
4474 4474 int j;
4475 4475 int pidstrlen = idlen - 2;
4476 4476 j = sizeof (SD_INQUIRY(un)->inq_pid) -
4477 4477 pidstrlen;
4478 4478
4479 4479 if (j < 0) {
4480 4480 return (SD_FAILURE);
4481 4481 }
4482 4482 for (i = 0; i < j; i++) {
4483 4483 if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4484 4484 pidptr, pidstrlen) == 0) {
4485 4485 rval = SD_SUCCESS;
4486 4486 break;
4487 4487 }
4488 4488 }
4489 4489 }
4490 4490 }
4491 4491 }
4492 4492 return (rval);
4493 4493 }
4494 4494
4495 4495
4496 4496 /*
4497 4497 * Function: sd_blank_cmp
4498 4498 *
4499 4499 * Description: If the id string starts and ends with a space, treat
4500 4500 * multiple consecutive spaces as equivalent to a single
4501 4501 * space. For example, this causes a sd_disk_table entry
4502 4502 * of " NEC CDROM " to match a device's id string of
4503 4503 * "NEC CDROM".
4504 4504 *
4505 4505 * Note: The success exit condition for this routine is if
4506 4506 * the pointer to the table entry is '\0' and the cnt of
4507 4507 * the inquiry length is zero. This will happen if the inquiry
4508 4508 * string returned by the device is padded with spaces to be
4509 4509 * exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4510 4510 * SCSI spec states that the inquiry string is to be padded with
4511 4511 * spaces.
4512 4512 *
4513 4513 * Arguments: un - driver soft state (unit) structure
4514 4514 * id - table or config file vid/pid
4515 4515 * idlen - length of the vid/pid (bytes)
4516 4516 *
4517 4517 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4518 4518 * SD_FAILURE - Indicates no match with the inquiry vid/pid
4519 4519 */
4520 4520
4521 4521 static int
4522 4522 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4523 4523 {
4524 4524 char *p1;
4525 4525 char *p2;
4526 4526 int cnt;
4527 4527 cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4528 4528 sizeof (SD_INQUIRY(un)->inq_pid);
4529 4529
4530 4530 ASSERT(un != NULL);
4531 4531 p2 = un->un_sd->sd_inq->inq_vid;
4532 4532 ASSERT(id != NULL);
4533 4533 p1 = id;
4534 4534
4535 4535 if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4536 4536 /*
4537 4537 * Note: string p1 is terminated by a NUL but string p2
4538 4538 * isn't. The end of p2 is determined by cnt.
4539 4539 */
4540 4540 for (;;) {
4541 4541 /* skip over any extra blanks in both strings */
4542 4542 while ((*p1 != '\0') && (*p1 == ' ')) {
4543 4543 p1++;
4544 4544 }
4545 4545 while ((cnt != 0) && (*p2 == ' ')) {
4546 4546 p2++;
4547 4547 cnt--;
4548 4548 }
4549 4549
4550 4550 /* compare the two strings */
4551 4551 if ((cnt == 0) ||
4552 4552 (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4553 4553 break;
4554 4554 }
4555 4555 while ((cnt > 0) &&
4556 4556 (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4557 4557 p1++;
4558 4558 p2++;
4559 4559 cnt--;
4560 4560 }
4561 4561 }
4562 4562 }
4563 4563
4564 4564 /* return SD_SUCCESS if both strings match */
4565 4565 return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4566 4566 }
4567 4567
4568 4568
4569 4569 /*
4570 4570 * Function: sd_chk_vers1_data
4571 4571 *
4572 4572 * Description: Verify the version 1 device properties provided by the
4573 4573 * user via the configuration file
4574 4574 *
4575 4575 * Arguments: un - driver soft state (unit) structure
4576 4576 * flags - integer mask indicating properties to be set
4577 4577 * prop_list - integer list of property values
4578 4578 * list_len - number of the elements
4579 4579 *
4580 4580 * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4581 4581 * SD_FAILURE - Indicates the user provided data is invalid
4582 4582 */
4583 4583
4584 4584 static int
4585 4585 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4586 4586 int list_len, char *dataname_ptr)
4587 4587 {
4588 4588 int i;
4589 4589 int mask = 1;
4590 4590 int index = 0;
4591 4591
4592 4592 ASSERT(un != NULL);
4593 4593
4594 4594 /* Check for a NULL property name and list */
4595 4595 if (dataname_ptr == NULL) {
4596 4596 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4597 4597 "sd_chk_vers1_data: NULL data property name.");
4598 4598 return (SD_FAILURE);
4599 4599 }
4600 4600 if (prop_list == NULL) {
4601 4601 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4602 4602 "sd_chk_vers1_data: %s NULL data property list.",
4603 4603 dataname_ptr);
4604 4604 return (SD_FAILURE);
4605 4605 }
4606 4606
4607 4607 /* Display a warning if undefined bits are set in the flags */
4608 4608 if (flags & ~SD_CONF_BIT_MASK) {
4609 4609 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4610 4610 "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4611 4611 "Properties not set.",
4612 4612 (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4613 4613 return (SD_FAILURE);
4614 4614 }
4615 4615
4616 4616 /*
4617 4617 * Verify the length of the list by identifying the highest bit set
4618 4618 * in the flags and validating that the property list has a length
4619 4619 * up to the index of this bit.
4620 4620 */
4621 4621 for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4622 4622 if (flags & mask) {
4623 4623 index++;
4624 4624 }
4625 4625 mask = 1 << i;
4626 4626 }
4627 4627 if (list_len < (index + 2)) {
4628 4628 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4629 4629 "sd_chk_vers1_data: "
4630 4630 "Data property list %s size is incorrect. "
4631 4631 "Properties not set.", dataname_ptr);
4632 4632 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4633 4633 "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4634 4634 return (SD_FAILURE);
4635 4635 }
4636 4636 return (SD_SUCCESS);
4637 4637 }
4638 4638
4639 4639
4640 4640 /*
4641 4641 * Function: sd_set_vers1_properties
4642 4642 *
4643 4643 * Description: Set version 1 device properties based on a property list
4644 4644 * retrieved from the driver configuration file or static
4645 4645 * configuration table. Version 1 properties have the format:
4646 4646 *
4647 4647 * <data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4648 4648 *
4649 4649 * where the prop0 value will be used to set prop0 if bit0
4650 4650 * is set in the flags
4651 4651 *
4652 4652 * Arguments: un - driver soft state (unit) structure
4653 4653 * flags - integer mask indicating properties to be set
4654 4654 * prop_list - integer list of property values
4655 4655 */
4656 4656
4657 4657 static void
4658 4658 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4659 4659 {
4660 4660 ASSERT(un != NULL);
4661 4661
4662 4662 /*
4663 4663 * Set the flag to indicate cache is to be disabled. An attempt
4664 4664 * to disable the cache via sd_cache_control() will be made
4665 4665 * later during attach once the basic initialization is complete.
4666 4666 */
4667 4667 if (flags & SD_CONF_BSET_NOCACHE) {
4668 4668 un->un_f_opt_disable_cache = TRUE;
4669 4669 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4670 4670 "sd_set_vers1_properties: caching disabled flag set\n");
4671 4671 }
4672 4672
4673 4673 /* CD-specific configuration parameters */
4674 4674 if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4675 4675 un->un_f_cfg_playmsf_bcd = TRUE;
4676 4676 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4677 4677 "sd_set_vers1_properties: playmsf_bcd set\n");
4678 4678 }
4679 4679 if (flags & SD_CONF_BSET_READSUB_BCD) {
4680 4680 un->un_f_cfg_readsub_bcd = TRUE;
4681 4681 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4682 4682 "sd_set_vers1_properties: readsub_bcd set\n");
4683 4683 }
4684 4684 if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4685 4685 un->un_f_cfg_read_toc_trk_bcd = TRUE;
4686 4686 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4687 4687 "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4688 4688 }
4689 4689 if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4690 4690 un->un_f_cfg_read_toc_addr_bcd = TRUE;
4691 4691 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4692 4692 "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4693 4693 }
4694 4694 if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4695 4695 un->un_f_cfg_no_read_header = TRUE;
4696 4696 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4697 4697 "sd_set_vers1_properties: no_read_header set\n");
4698 4698 }
4699 4699 if (flags & SD_CONF_BSET_READ_CD_XD4) {
4700 4700 un->un_f_cfg_read_cd_xd4 = TRUE;
4701 4701 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4702 4702 "sd_set_vers1_properties: read_cd_xd4 set\n");
4703 4703 }
4704 4704
4705 4705 /* Support for devices which do not have valid/unique serial numbers */
4706 4706 if (flags & SD_CONF_BSET_FAB_DEVID) {
4707 4707 un->un_f_opt_fab_devid = TRUE;
4708 4708 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4709 4709 "sd_set_vers1_properties: fab_devid bit set\n");
4710 4710 }
4711 4711
4712 4712 /* Support for user throttle configuration */
4713 4713 if (flags & SD_CONF_BSET_THROTTLE) {
4714 4714 ASSERT(prop_list != NULL);
4715 4715 un->un_saved_throttle = un->un_throttle =
4716 4716 prop_list->sdt_throttle;
4717 4717 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4718 4718 "sd_set_vers1_properties: throttle set to %d\n",
4719 4719 prop_list->sdt_throttle);
4720 4720 }
4721 4721
4722 4722 /* Set the per disk retry count according to the conf file or table. */
4723 4723 if (flags & SD_CONF_BSET_NRR_COUNT) {
4724 4724 ASSERT(prop_list != NULL);
4725 4725 if (prop_list->sdt_not_rdy_retries) {
4726 4726 un->un_notready_retry_count =
4727 4727 prop_list->sdt_not_rdy_retries;
4728 4728 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4729 4729 "sd_set_vers1_properties: not ready retry count"
4730 4730 " set to %d\n", un->un_notready_retry_count);
4731 4731 }
4732 4732 }
4733 4733
4734 4734 /* The controller type is reported for generic disk driver ioctls */
4735 4735 if (flags & SD_CONF_BSET_CTYPE) {
4736 4736 ASSERT(prop_list != NULL);
4737 4737 switch (prop_list->sdt_ctype) {
4738 4738 case CTYPE_CDROM:
4739 4739 un->un_ctype = prop_list->sdt_ctype;
4740 4740 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4741 4741 "sd_set_vers1_properties: ctype set to "
4742 4742 "CTYPE_CDROM\n");
4743 4743 break;
4744 4744 case CTYPE_CCS:
4745 4745 un->un_ctype = prop_list->sdt_ctype;
4746 4746 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4747 4747 "sd_set_vers1_properties: ctype set to "
4748 4748 "CTYPE_CCS\n");
4749 4749 break;
4750 4750 case CTYPE_ROD: /* RW optical */
4751 4751 un->un_ctype = prop_list->sdt_ctype;
4752 4752 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4753 4753 "sd_set_vers1_properties: ctype set to "
4754 4754 "CTYPE_ROD\n");
4755 4755 break;
4756 4756 default:
4757 4757 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4758 4758 "sd_set_vers1_properties: Could not set "
4759 4759 "invalid ctype value (%d)",
4760 4760 prop_list->sdt_ctype);
4761 4761 }
4762 4762 }
4763 4763
4764 4764 /* Purple failover timeout */
4765 4765 if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4766 4766 ASSERT(prop_list != NULL);
4767 4767 un->un_busy_retry_count =
4768 4768 prop_list->sdt_busy_retries;
4769 4769 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4770 4770 "sd_set_vers1_properties: "
4771 4771 "busy retry count set to %d\n",
4772 4772 un->un_busy_retry_count);
4773 4773 }
4774 4774
4775 4775 /* Purple reset retry count */
4776 4776 if (flags & SD_CONF_BSET_RST_RETRIES) {
4777 4777 ASSERT(prop_list != NULL);
4778 4778 un->un_reset_retry_count =
4779 4779 prop_list->sdt_reset_retries;
4780 4780 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4781 4781 "sd_set_vers1_properties: "
4782 4782 "reset retry count set to %d\n",
4783 4783 un->un_reset_retry_count);
4784 4784 }
4785 4785
4786 4786 /* Purple reservation release timeout */
4787 4787 if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4788 4788 ASSERT(prop_list != NULL);
4789 4789 un->un_reserve_release_time =
4790 4790 prop_list->sdt_reserv_rel_time;
4791 4791 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4792 4792 "sd_set_vers1_properties: "
4793 4793 "reservation release timeout set to %d\n",
4794 4794 un->un_reserve_release_time);
4795 4795 }
4796 4796
4797 4797 /*
4798 4798 * Driver flag telling the driver to verify that no commands are pending
4799 4799 * for a device before issuing a Test Unit Ready. This is a workaround
4800 4800 * for a firmware bug in some Seagate eliteI drives.
4801 4801 */
4802 4802 if (flags & SD_CONF_BSET_TUR_CHECK) {
4803 4803 un->un_f_cfg_tur_check = TRUE;
4804 4804 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4805 4805 "sd_set_vers1_properties: tur queue check set\n");
4806 4806 }
4807 4807
4808 4808 if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4809 4809 un->un_min_throttle = prop_list->sdt_min_throttle;
4810 4810 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4811 4811 "sd_set_vers1_properties: min throttle set to %d\n",
4812 4812 un->un_min_throttle);
4813 4813 }
4814 4814
4815 4815 if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4816 4816 un->un_f_disksort_disabled =
4817 4817 (prop_list->sdt_disk_sort_dis != 0) ?
4818 4818 TRUE : FALSE;
4819 4819 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4820 4820 "sd_set_vers1_properties: disksort disabled "
4821 4821 "flag set to %d\n",
4822 4822 prop_list->sdt_disk_sort_dis);
4823 4823 }
4824 4824
4825 4825 if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4826 4826 un->un_f_lun_reset_enabled =
4827 4827 (prop_list->sdt_lun_reset_enable != 0) ?
4828 4828 TRUE : FALSE;
4829 4829 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4830 4830 "sd_set_vers1_properties: lun reset enabled "
4831 4831 "flag set to %d\n",
4832 4832 prop_list->sdt_lun_reset_enable);
4833 4833 }
4834 4834
4835 4835 if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4836 4836 un->un_f_suppress_cache_flush =
4837 4837 (prop_list->sdt_suppress_cache_flush != 0) ?
4838 4838 TRUE : FALSE;
4839 4839 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4840 4840 "sd_set_vers1_properties: suppress_cache_flush "
4841 4841 "flag set to %d\n",
4842 4842 prop_list->sdt_suppress_cache_flush);
4843 4843 }
4844 4844
4845 4845 if (flags & SD_CONF_BSET_PC_DISABLED) {
4846 4846 un->un_f_power_condition_disabled =
4847 4847 (prop_list->sdt_power_condition_dis != 0) ?
4848 4848 TRUE : FALSE;
4849 4849 SD_INFO(SD_LOG_ATTACH_DETACH, un,
4850 4850 "sd_set_vers1_properties: power_condition_disabled "
4851 4851 "flag set to %d\n",
4852 4852 prop_list->sdt_power_condition_dis);
4853 4853 }
4854 4854
4855 4855 /*
4856 4856 * Validate the throttle values.
4857 4857 * If any of the numbers are invalid, set everything to defaults.
4858 4858 */
4859 4859 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4860 4860 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4861 4861 (un->un_min_throttle > un->un_throttle)) {
4862 4862 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4863 4863 un->un_min_throttle = sd_min_throttle;
4864 4864 }
4865 4865 }
4866 4866
4867 4867 /*
4868 4868 * Function: sd_is_lsi()
4869 4869 *
4870 4870 * Description: Check for lsi devices, step through the static device
4871 4871 * table to match vid/pid.
4872 4872 *
4873 4873 * Args: un - ptr to sd_lun
4874 4874 *
4875 4875 * Notes: When creating new LSI property, need to add the new LSI property
4876 4876 * to this function.
4877 4877 */
4878 4878 static void
4879 4879 sd_is_lsi(struct sd_lun *un)
4880 4880 {
4881 4881 char *id = NULL;
4882 4882 int table_index;
4883 4883 int idlen;
4884 4884 void *prop;
4885 4885
4886 4886 ASSERT(un != NULL);
4887 4887 for (table_index = 0; table_index < sd_disk_table_size;
4888 4888 table_index++) {
4889 4889 id = sd_disk_table[table_index].device_id;
4890 4890 idlen = strlen(id);
4891 4891 if (idlen == 0) {
4892 4892 continue;
4893 4893 }
4894 4894
4895 4895 if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4896 4896 prop = sd_disk_table[table_index].properties;
4897 4897 if (prop == &lsi_properties ||
4898 4898 prop == &lsi_oem_properties ||
4899 4899 prop == &lsi_properties_scsi ||
4900 4900 prop == &symbios_properties) {
4901 4901 un->un_f_cfg_is_lsi = TRUE;
4902 4902 }
4903 4903 break;
4904 4904 }
4905 4905 }
4906 4906 }
4907 4907
4908 4908 /*
4909 4909 * Function: sd_get_physical_geometry
4910 4910 *
4911 4911 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4912 4912 * MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4913 4913 * target, and use this information to initialize the physical
4914 4914 * geometry cache specified by pgeom_p.
4915 4915 *
4916 4916 * MODE SENSE is an optional command, so failure in this case
4917 4917 * does not necessarily denote an error. We want to use the
4918 4918 * MODE SENSE commands to derive the physical geometry of the
4919 4919 * device, but if either command fails, the logical geometry is
4920 4920 * used as the fallback for disk label geometry in cmlb.
4921 4921 *
4922 4922 * This requires that un->un_blockcount and un->un_tgt_blocksize
4923 4923 * have already been initialized for the current target and
4924 4924 * that the current values be passed as args so that we don't
4925 4925 * end up ever trying to use -1 as a valid value. This could
4926 4926 * happen if either value is reset while we're not holding
4927 4927 * the mutex.
4928 4928 *
4929 4929 * Arguments: un - driver soft state (unit) structure
4930 4930 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4931 4931 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4932 4932 * to use the USCSI "direct" chain and bypass the normal
4933 4933 * command waitq.
4934 4934 *
4935 4935 * Context: Kernel thread only (can sleep).
4936 4936 */
4937 4937
4938 4938 static int
4939 4939 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4940 4940 diskaddr_t capacity, int lbasize, int path_flag)
4941 4941 {
4942 4942 struct mode_format *page3p;
4943 4943 struct mode_geometry *page4p;
4944 4944 struct mode_header *headerp;
4945 4945 int sector_size;
4946 4946 int nsect;
4947 4947 int nhead;
4948 4948 int ncyl;
4949 4949 int intrlv;
4950 4950 int spc;
4951 4951 diskaddr_t modesense_capacity;
4952 4952 int rpm;
4953 4953 int bd_len;
4954 4954 int mode_header_length;
4955 4955 uchar_t *p3bufp;
4956 4956 uchar_t *p4bufp;
4957 4957 int cdbsize;
4958 4958 int ret = EIO;
4959 4959 sd_ssc_t *ssc;
4960 4960 int status;
4961 4961
4962 4962 ASSERT(un != NULL);
4963 4963
4964 4964 if (lbasize == 0) {
4965 4965 if (ISCD(un)) {
4966 4966 lbasize = 2048;
4967 4967 } else {
4968 4968 lbasize = un->un_sys_blocksize;
4969 4969 }
4970 4970 }
4971 4971 pgeom_p->g_secsize = (unsigned short)lbasize;
4972 4972
4973 4973 /*
4974 4974 * If the unit is a cd/dvd drive MODE SENSE page three
4975 4975 * and MODE SENSE page four are reserved (see SBC spec
4976 4976 * and MMC spec). To prevent soft errors just return
4977 4977 * using the default LBA size.
4978 4978 */
4979 4979 if (ISCD(un))
4980 4980 return (ret);
4981 4981
4982 4982 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4983 4983
4984 4984 /*
4985 4985 * Retrieve MODE SENSE page 3 - Format Device Page
4986 4986 */
4987 4987 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4988 4988 ssc = sd_ssc_init(un);
4989 4989 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4990 4990 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4991 4991 if (status != 0) {
4992 4992 SD_ERROR(SD_LOG_COMMON, un,
4993 4993 "sd_get_physical_geometry: mode sense page 3 failed\n");
4994 4994 goto page3_exit;
4995 4995 }
4996 4996
4997 4997 /*
4998 4998 * Determine size of Block Descriptors in order to locate the mode
4999 4999 * page data. ATAPI devices return 0, SCSI devices should return
5000 5000 * MODE_BLK_DESC_LENGTH.
5001 5001 */
5002 5002 headerp = (struct mode_header *)p3bufp;
5003 5003 if (un->un_f_cfg_is_atapi == TRUE) {
5004 5004 struct mode_header_grp2 *mhp =
5005 5005 (struct mode_header_grp2 *)headerp;
5006 5006 mode_header_length = MODE_HEADER_LENGTH_GRP2;
5007 5007 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5008 5008 } else {
5009 5009 mode_header_length = MODE_HEADER_LENGTH;
5010 5010 bd_len = ((struct mode_header *)headerp)->bdesc_length;
5011 5011 }
5012 5012
5013 5013 if (bd_len > MODE_BLK_DESC_LENGTH) {
5014 5014 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5015 5015 "sd_get_physical_geometry: received unexpected bd_len "
5016 5016 "of %d, page3\n", bd_len);
5017 5017 status = EIO;
5018 5018 goto page3_exit;
5019 5019 }
5020 5020
5021 5021 page3p = (struct mode_format *)
5022 5022 ((caddr_t)headerp + mode_header_length + bd_len);
5023 5023
5024 5024 if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5025 5025 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5026 5026 "sd_get_physical_geometry: mode sense pg3 code mismatch "
5027 5027 "%d\n", page3p->mode_page.code);
5028 5028 status = EIO;
5029 5029 goto page3_exit;
5030 5030 }
5031 5031
5032 5032 /*
5033 5033 * Use this physical geometry data only if BOTH MODE SENSE commands
5034 5034 * complete successfully; otherwise, revert to the logical geometry.
5035 5035 * So, we need to save everything in temporary variables.
5036 5036 */
5037 5037 sector_size = BE_16(page3p->data_bytes_sect);
5038 5038
5039 5039 /*
5040 5040 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5041 5041 */
5042 5042 if (sector_size == 0) {
5043 5043 sector_size = un->un_sys_blocksize;
5044 5044 } else {
5045 5045 sector_size &= ~(un->un_sys_blocksize - 1);
5046 5046 }
5047 5047
5048 5048 nsect = BE_16(page3p->sect_track);
5049 5049 intrlv = BE_16(page3p->interleave);
5050 5050
5051 5051 SD_INFO(SD_LOG_COMMON, un,
5052 5052 "sd_get_physical_geometry: Format Parameters (page 3)\n");
5053 5053 SD_INFO(SD_LOG_COMMON, un,
5054 5054 " mode page: %d; nsect: %d; sector size: %d;\n",
5055 5055 page3p->mode_page.code, nsect, sector_size);
5056 5056 SD_INFO(SD_LOG_COMMON, un,
5057 5057 " interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5058 5058 BE_16(page3p->track_skew),
5059 5059 BE_16(page3p->cylinder_skew));
5060 5060
5061 5061 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5062 5062
5063 5063 /*
5064 5064 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5065 5065 */
5066 5066 p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5067 5067 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5068 5068 SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5069 5069 if (status != 0) {
5070 5070 SD_ERROR(SD_LOG_COMMON, un,
5071 5071 "sd_get_physical_geometry: mode sense page 4 failed\n");
5072 5072 goto page4_exit;
5073 5073 }
5074 5074
5075 5075 /*
5076 5076 * Determine size of Block Descriptors in order to locate the mode
5077 5077 * page data. ATAPI devices return 0, SCSI devices should return
5078 5078 * MODE_BLK_DESC_LENGTH.
5079 5079 */
5080 5080 headerp = (struct mode_header *)p4bufp;
5081 5081 if (un->un_f_cfg_is_atapi == TRUE) {
5082 5082 struct mode_header_grp2 *mhp =
5083 5083 (struct mode_header_grp2 *)headerp;
5084 5084 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5085 5085 } else {
5086 5086 bd_len = ((struct mode_header *)headerp)->bdesc_length;
5087 5087 }
5088 5088
5089 5089 if (bd_len > MODE_BLK_DESC_LENGTH) {
5090 5090 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5091 5091 "sd_get_physical_geometry: received unexpected bd_len of "
5092 5092 "%d, page4\n", bd_len);
5093 5093 status = EIO;
5094 5094 goto page4_exit;
5095 5095 }
5096 5096
5097 5097 page4p = (struct mode_geometry *)
5098 5098 ((caddr_t)headerp + mode_header_length + bd_len);
5099 5099
5100 5100 if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5101 5101 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5102 5102 "sd_get_physical_geometry: mode sense pg4 code mismatch "
5103 5103 "%d\n", page4p->mode_page.code);
5104 5104 status = EIO;
5105 5105 goto page4_exit;
5106 5106 }
5107 5107
5108 5108 /*
5109 5109 * Stash the data now, after we know that both commands completed.
5110 5110 */
5111 5111
5112 5112
5113 5113 nhead = (int)page4p->heads; /* uchar, so no conversion needed */
5114 5114 spc = nhead * nsect;
5115 5115 ncyl = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5116 5116 rpm = BE_16(page4p->rpm);
5117 5117
5118 5118 modesense_capacity = spc * ncyl;
5119 5119
5120 5120 SD_INFO(SD_LOG_COMMON, un,
5121 5121 "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5122 5122 SD_INFO(SD_LOG_COMMON, un,
5123 5123 " cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5124 5124 SD_INFO(SD_LOG_COMMON, un,
5125 5125 " computed capacity(h*s*c): %d;\n", modesense_capacity);
5126 5126 SD_INFO(SD_LOG_COMMON, un, " pgeom_p: %p; read cap: %d\n",
5127 5127 (void *)pgeom_p, capacity);
5128 5128
5129 5129 /*
5130 5130 * Compensate if the drive's geometry is not rectangular, i.e.,
5131 5131 * the product of C * H * S returned by MODE SENSE >= that returned
5132 5132 * by read capacity. This is an idiosyncrasy of the original x86
5133 5133 * disk subsystem.
5134 5134 */
5135 5135 if (modesense_capacity >= capacity) {
5136 5136 SD_INFO(SD_LOG_COMMON, un,
5137 5137 "sd_get_physical_geometry: adjusting acyl; "
5138 5138 "old: %d; new: %d\n", pgeom_p->g_acyl,
5139 5139 (modesense_capacity - capacity + spc - 1) / spc);
5140 5140 if (sector_size != 0) {
5141 5141 /* 1243403: NEC D38x7 drives don't support sec size */
5142 5142 pgeom_p->g_secsize = (unsigned short)sector_size;
5143 5143 }
5144 5144 pgeom_p->g_nsect = (unsigned short)nsect;
5145 5145 pgeom_p->g_nhead = (unsigned short)nhead;
5146 5146 pgeom_p->g_capacity = capacity;
5147 5147 pgeom_p->g_acyl =
5148 5148 (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5149 5149 pgeom_p->g_ncyl = ncyl - pgeom_p->g_acyl;
5150 5150 }
5151 5151
5152 5152 pgeom_p->g_rpm = (unsigned short)rpm;
5153 5153 pgeom_p->g_intrlv = (unsigned short)intrlv;
5154 5154 ret = 0;
5155 5155
5156 5156 SD_INFO(SD_LOG_COMMON, un,
5157 5157 "sd_get_physical_geometry: mode sense geometry:\n");
5158 5158 SD_INFO(SD_LOG_COMMON, un,
5159 5159 " nsect: %d; sector size: %d; interlv: %d\n",
5160 5160 nsect, sector_size, intrlv);
5161 5161 SD_INFO(SD_LOG_COMMON, un,
5162 5162 " nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5163 5163 nhead, ncyl, rpm, modesense_capacity);
5164 5164 SD_INFO(SD_LOG_COMMON, un,
5165 5165 "sd_get_physical_geometry: (cached)\n");
5166 5166 SD_INFO(SD_LOG_COMMON, un,
5167 5167 " ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5168 5168 pgeom_p->g_ncyl, pgeom_p->g_acyl,
5169 5169 pgeom_p->g_nhead, pgeom_p->g_nsect);
5170 5170 SD_INFO(SD_LOG_COMMON, un,
5171 5171 " lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5172 5172 pgeom_p->g_secsize, pgeom_p->g_capacity,
5173 5173 pgeom_p->g_intrlv, pgeom_p->g_rpm);
5174 5174 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5175 5175
5176 5176 page4_exit:
5177 5177 kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5178 5178
5179 5179 page3_exit:
5180 5180 kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5181 5181
5182 5182 if (status != 0) {
5183 5183 if (status == EIO) {
5184 5184 /*
5185 5185 * Some disks do not support mode sense(6), we
5186 5186 * should ignore this kind of error(sense key is
5187 5187 * 0x5 - illegal request).
5188 5188 */
5189 5189 uint8_t *sensep;
5190 5190 int senlen;
5191 5191
5192 5192 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5193 5193 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5194 5194 ssc->ssc_uscsi_cmd->uscsi_rqresid);
5195 5195
5196 5196 if (senlen > 0 &&
5197 5197 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5198 5198 sd_ssc_assessment(ssc,
5199 5199 SD_FMT_IGNORE_COMPROMISE);
5200 5200 } else {
5201 5201 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5202 5202 }
5203 5203 } else {
5204 5204 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5205 5205 }
5206 5206 }
5207 5207 sd_ssc_fini(ssc);
5208 5208 return (ret);
5209 5209 }
5210 5210
5211 5211 /*
5212 5212 * Function: sd_get_virtual_geometry
5213 5213 *
5214 5214 * Description: Ask the controller to tell us about the target device.
5215 5215 *
5216 5216 * Arguments: un - pointer to softstate
5217 5217 * capacity - disk capacity in #blocks
5218 5218 * lbasize - disk block size in bytes
5219 5219 *
5220 5220 * Context: Kernel thread only
5221 5221 */
5222 5222
5223 5223 static int
5224 5224 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5225 5225 diskaddr_t capacity, int lbasize)
5226 5226 {
5227 5227 uint_t geombuf;
5228 5228 int spc;
5229 5229
5230 5230 ASSERT(un != NULL);
5231 5231
5232 5232 /* Set sector size, and total number of sectors */
5233 5233 (void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size", lbasize, 1);
5234 5234 (void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5235 5235
5236 5236 /* Let the HBA tell us its geometry */
5237 5237 geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5238 5238
5239 5239 /* A value of -1 indicates an undefined "geometry" property */
5240 5240 if (geombuf == (-1)) {
5241 5241 return (EINVAL);
5242 5242 }
5243 5243
5244 5244 /* Initialize the logical geometry cache. */
5245 5245 lgeom_p->g_nhead = (geombuf >> 16) & 0xffff;
5246 5246 lgeom_p->g_nsect = geombuf & 0xffff;
5247 5247 lgeom_p->g_secsize = un->un_sys_blocksize;
5248 5248
5249 5249 spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5250 5250
5251 5251 /*
5252 5252 * Note: The driver originally converted the capacity value from
5253 5253 * target blocks to system blocks. However, the capacity value passed
5254 5254 * to this routine is already in terms of system blocks (this scaling
5255 5255 * is done when the READ CAPACITY command is issued and processed).
5256 5256 * This 'error' may have gone undetected because the usage of g_ncyl
5257 5257 * (which is based upon g_capacity) is very limited within the driver
5258 5258 */
5259 5259 lgeom_p->g_capacity = capacity;
5260 5260
5261 5261 /*
5262 5262 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5263 5263 * hba may return zero values if the device has been removed.
5264 5264 */
5265 5265 if (spc == 0) {
5266 5266 lgeom_p->g_ncyl = 0;
5267 5267 } else {
5268 5268 lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5269 5269 }
5270 5270 lgeom_p->g_acyl = 0;
5271 5271
5272 5272 SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5273 5273 return (0);
5274 5274
5275 5275 }
5276 5276 /*
5277 5277 * Function: sd_update_block_info
5278 5278 *
5279 5279 * Description: Calculate a byte count to sector count bitshift value
5280 5280 * from sector size.
5281 5281 *
5282 5282 * Arguments: un: unit struct.
5283 5283 * lbasize: new target sector size
5284 5284 * capacity: new target capacity, ie. block count
5285 5285 *
5286 5286 * Context: Kernel thread context
5287 5287 */
5288 5288
5289 5289 static void
5290 5290 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5291 5291 {
5292 5292 if (lbasize != 0) {
5293 5293 un->un_tgt_blocksize = lbasize;
5294 5294 un->un_f_tgt_blocksize_is_valid = TRUE;
5295 5295 if (!un->un_f_has_removable_media) {
5296 5296 un->un_sys_blocksize = lbasize;
5297 5297 }
5298 5298 }
5299 5299
5300 5300 if (capacity != 0) {
5301 5301 un->un_blockcount = capacity;
5302 5302 un->un_f_blockcount_is_valid = TRUE;
5303 5303
5304 5304 /*
5305 5305 * The capacity has changed so update the errstats.
5306 5306 */
5307 5307 if (un->un_errstats != NULL) {
5308 5308 struct sd_errstats *stp;
5309 5309
5310 5310 capacity *= un->un_sys_blocksize;
5311 5311 stp = (struct sd_errstats *)un->un_errstats->ks_data;
5312 5312 if (stp->sd_capacity.value.ui64 < capacity)
5313 5313 stp->sd_capacity.value.ui64 = capacity;
5314 5314 }
5315 5315 }
5316 5316 }
5317 5317
5318 5318
5319 5319 /*
5320 5320 * Function: sd_register_devid
5321 5321 *
5322 5322 * Description: This routine will obtain the device id information from the
5323 5323 * target, obtain the serial number, and register the device
5324 5324 * id with the ddi framework.
5325 5325 *
5326 5326 * Arguments: devi - the system's dev_info_t for the device.
5327 5327 * un - driver soft state (unit) structure
5328 5328 * reservation_flag - indicates if a reservation conflict
5329 5329 * occurred during attach
5330 5330 *
5331 5331 * Context: Kernel Thread
5332 5332 */
5333 5333 static void
5334 5334 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5335 5335 {
5336 5336 int rval = 0;
5337 5337 uchar_t *inq80 = NULL;
5338 5338 size_t inq80_len = MAX_INQUIRY_SIZE;
5339 5339 size_t inq80_resid = 0;
5340 5340 uchar_t *inq83 = NULL;
5341 5341 size_t inq83_len = MAX_INQUIRY_SIZE;
5342 5342 size_t inq83_resid = 0;
5343 5343 int dlen, len;
5344 5344 char *sn;
5345 5345 struct sd_lun *un;
5346 5346
5347 5347 ASSERT(ssc != NULL);
5348 5348 un = ssc->ssc_un;
5349 5349 ASSERT(un != NULL);
5350 5350 ASSERT(mutex_owned(SD_MUTEX(un)));
5351 5351 ASSERT((SD_DEVINFO(un)) == devi);
5352 5352
5353 5353
5354 5354 /*
5355 5355 * We check the availability of the World Wide Name (0x83) and Unit
5356 5356 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5357 5357 * un_vpd_page_mask from them, we decide which way to get the WWN. If
5358 5358 * 0x83 is available, that is the best choice. Our next choice is
5359 5359 * 0x80. If neither are available, we munge the devid from the device
5360 5360 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5361 5361 * to fabricate a devid for non-Sun qualified disks.
5362 5362 */
5363 5363 if (sd_check_vpd_page_support(ssc) == 0) {
5364 5364 /* collect page 80 data if available */
5365 5365 if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5366 5366
5367 5367 mutex_exit(SD_MUTEX(un));
5368 5368 inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5369 5369
5370 5370 rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5371 5371 0x01, 0x80, &inq80_resid);
5372 5372
5373 5373 if (rval != 0) {
5374 5374 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5375 5375 kmem_free(inq80, inq80_len);
5376 5376 inq80 = NULL;
5377 5377 inq80_len = 0;
5378 5378 } else if (ddi_prop_exists(
5379 5379 DDI_DEV_T_NONE, SD_DEVINFO(un),
5380 5380 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5381 5381 INQUIRY_SERIAL_NO) == 0) {
5382 5382 /*
5383 5383 * If we don't already have a serial number
5384 5384 * property, do quick verify of data returned
5385 5385 * and define property.
5386 5386 */
5387 5387 dlen = inq80_len - inq80_resid;
5388 5388 len = (size_t)inq80[3];
5389 5389 if ((dlen >= 4) && ((len + 4) <= dlen)) {
5390 5390 /*
5391 5391 * Ensure sn termination, skip leading
5392 5392 * blanks, and create property
5393 5393 * 'inquiry-serial-no'.
5394 5394 */
5395 5395 sn = (char *)&inq80[4];
5396 5396 sn[len] = 0;
5397 5397 while (*sn && (*sn == ' '))
5398 5398 sn++;
5399 5399 if (*sn) {
5400 5400 (void) ddi_prop_update_string(
5401 5401 DDI_DEV_T_NONE,
5402 5402 SD_DEVINFO(un),
5403 5403 INQUIRY_SERIAL_NO, sn);
5404 5404 }
5405 5405 }
5406 5406 }
5407 5407 mutex_enter(SD_MUTEX(un));
5408 5408 }
5409 5409
5410 5410 /* collect page 83 data if available */
5411 5411 if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5412 5412 mutex_exit(SD_MUTEX(un));
5413 5413 inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5414 5414
5415 5415 rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5416 5416 0x01, 0x83, &inq83_resid);
5417 5417
5418 5418 if (rval != 0) {
5419 5419 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5420 5420 kmem_free(inq83, inq83_len);
5421 5421 inq83 = NULL;
5422 5422 inq83_len = 0;
5423 5423 }
5424 5424 mutex_enter(SD_MUTEX(un));
5425 5425 }
5426 5426 }
5427 5427
5428 5428 /*
5429 5429 * If transport has already registered a devid for this target
5430 5430 * then that takes precedence over the driver's determination
5431 5431 * of the devid.
5432 5432 *
5433 5433 * NOTE: The reason this check is done here instead of at the beginning
5434 5434 * of the function is to allow the code above to create the
5435 5435 * 'inquiry-serial-no' property.
5436 5436 */
5437 5437 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5438 5438 ASSERT(un->un_devid);
5439 5439 un->un_f_devid_transport_defined = TRUE;
5440 5440 goto cleanup; /* use devid registered by the transport */
5441 5441 }
5442 5442
5443 5443 /*
5444 5444 * This is the case of antiquated Sun disk drives that have the
5445 5445 * FAB_DEVID property set in the disk_table. These drives
5446 5446 * manage the devid's by storing them in last 2 available sectors
5447 5447 * on the drive and have them fabricated by the ddi layer by calling
5448 5448 * ddi_devid_init and passing the DEVID_FAB flag.
5449 5449 */
5450 5450 if (un->un_f_opt_fab_devid == TRUE) {
5451 5451 /*
5452 5452 * Depending on EINVAL isn't reliable, since a reserved disk
5453 5453 * may result in invalid geometry, so check to make sure a
5454 5454 * reservation conflict did not occur during attach.
5455 5455 */
5456 5456 if ((sd_get_devid(ssc) == EINVAL) &&
5457 5457 (reservation_flag != SD_TARGET_IS_RESERVED)) {
5458 5458 /*
5459 5459 * The devid is invalid AND there is no reservation
5460 5460 * conflict. Fabricate a new devid.
5461 5461 */
5462 5462 (void) sd_create_devid(ssc);
5463 5463 }
5464 5464
5465 5465 /* Register the devid if it exists */
5466 5466 if (un->un_devid != NULL) {
5467 5467 (void) ddi_devid_register(SD_DEVINFO(un),
5468 5468 un->un_devid);
5469 5469 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5470 5470 "sd_register_devid: Devid Fabricated\n");
5471 5471 }
5472 5472 goto cleanup;
5473 5473 }
5474 5474
5475 5475 /* encode best devid possible based on data available */
5476 5476 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5477 5477 (char *)ddi_driver_name(SD_DEVINFO(un)),
5478 5478 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5479 5479 inq80, inq80_len - inq80_resid, inq83, inq83_len -
5480 5480 inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5481 5481
5482 5482 /* devid successfully encoded, register devid */
5483 5483 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5484 5484
5485 5485 } else {
5486 5486 /*
5487 5487 * Unable to encode a devid based on data available.
5488 5488 * This is not a Sun qualified disk. Older Sun disk
5489 5489 * drives that have the SD_FAB_DEVID property
5490 5490 * set in the disk_table and non Sun qualified
5491 5491 * disks are treated in the same manner. These
5492 5492 * drives manage the devid's by storing them in
5493 5493 * last 2 available sectors on the drive and
5494 5494 * have them fabricated by the ddi layer by
5495 5495 * calling ddi_devid_init and passing the
5496 5496 * DEVID_FAB flag.
5497 5497 * Create a fabricate devid only if there's no
5498 5498 * fabricate devid existed.
5499 5499 */
5500 5500 if (sd_get_devid(ssc) == EINVAL) {
5501 5501 (void) sd_create_devid(ssc);
5502 5502 }
5503 5503 un->un_f_opt_fab_devid = TRUE;
5504 5504
5505 5505 /* Register the devid if it exists */
5506 5506 if (un->un_devid != NULL) {
5507 5507 (void) ddi_devid_register(SD_DEVINFO(un),
5508 5508 un->un_devid);
5509 5509 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5510 5510 "sd_register_devid: devid fabricated using "
5511 5511 "ddi framework\n");
5512 5512 }
5513 5513 }
5514 5514
5515 5515 cleanup:
5516 5516 /* clean up resources */
5517 5517 if (inq80 != NULL) {
5518 5518 kmem_free(inq80, inq80_len);
5519 5519 }
5520 5520 if (inq83 != NULL) {
5521 5521 kmem_free(inq83, inq83_len);
5522 5522 }
5523 5523 }
5524 5524
5525 5525
5526 5526
5527 5527 /*
5528 5528 * Function: sd_get_devid
5529 5529 *
5530 5530 * Description: This routine will return 0 if a valid device id has been
5531 5531 * obtained from the target and stored in the soft state. If a
5532 5532 * valid device id has not been previously read and stored, a
5533 5533 * read attempt will be made.
5534 5534 *
5535 5535 * Arguments: un - driver soft state (unit) structure
5536 5536 *
5537 5537 * Return Code: 0 if we successfully get the device id
5538 5538 *
5539 5539 * Context: Kernel Thread
5540 5540 */
5541 5541
5542 5542 static int
5543 5543 sd_get_devid(sd_ssc_t *ssc)
5544 5544 {
5545 5545 struct dk_devid *dkdevid;
5546 5546 ddi_devid_t tmpid;
5547 5547 uint_t *ip;
5548 5548 size_t sz;
5549 5549 diskaddr_t blk;
5550 5550 int status;
5551 5551 int chksum;
5552 5552 int i;
5553 5553 size_t buffer_size;
5554 5554 struct sd_lun *un;
5555 5555
5556 5556 ASSERT(ssc != NULL);
5557 5557 un = ssc->ssc_un;
5558 5558 ASSERT(un != NULL);
5559 5559 ASSERT(mutex_owned(SD_MUTEX(un)));
5560 5560
5561 5561 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5562 5562 un);
5563 5563
5564 5564 if (un->un_devid != NULL) {
5565 5565 return (0);
5566 5566 }
5567 5567
5568 5568 mutex_exit(SD_MUTEX(un));
5569 5569 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5570 5570 (void *)SD_PATH_DIRECT) != 0) {
5571 5571 mutex_enter(SD_MUTEX(un));
5572 5572 return (EINVAL);
5573 5573 }
5574 5574
5575 5575 /*
5576 5576 * Read and verify device id, stored in the reserved cylinders at the
5577 5577 * end of the disk. Backup label is on the odd sectors of the last
5578 5578 * track of the last cylinder. Device id will be on track of the next
5579 5579 * to last cylinder.
5580 5580 */
5581 5581 mutex_enter(SD_MUTEX(un));
5582 5582 buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5583 5583 mutex_exit(SD_MUTEX(un));
5584 5584 dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5585 5585 status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5586 5586 SD_PATH_DIRECT);
5587 5587
5588 5588 if (status != 0) {
5589 5589 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5590 5590 goto error;
5591 5591 }
5592 5592
5593 5593 /* Validate the revision */
5594 5594 if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5595 5595 (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5596 5596 status = EINVAL;
5597 5597 goto error;
5598 5598 }
5599 5599
5600 5600 /* Calculate the checksum */
5601 5601 chksum = 0;
5602 5602 ip = (uint_t *)dkdevid;
5603 5603 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5604 5604 i++) {
5605 5605 chksum ^= ip[i];
5606 5606 }
5607 5607
5608 5608 /* Compare the checksums */
5609 5609 if (DKD_GETCHKSUM(dkdevid) != chksum) {
5610 5610 status = EINVAL;
5611 5611 goto error;
5612 5612 }
5613 5613
5614 5614 /* Validate the device id */
5615 5615 if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5616 5616 status = EINVAL;
5617 5617 goto error;
5618 5618 }
5619 5619
5620 5620 /*
5621 5621 * Store the device id in the driver soft state
5622 5622 */
5623 5623 sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5624 5624 tmpid = kmem_alloc(sz, KM_SLEEP);
5625 5625
5626 5626 mutex_enter(SD_MUTEX(un));
5627 5627
5628 5628 un->un_devid = tmpid;
5629 5629 bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5630 5630
5631 5631 kmem_free(dkdevid, buffer_size);
5632 5632
5633 5633 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5634 5634
5635 5635 return (status);
5636 5636 error:
5637 5637 mutex_enter(SD_MUTEX(un));
5638 5638 kmem_free(dkdevid, buffer_size);
5639 5639 return (status);
5640 5640 }
5641 5641
5642 5642
5643 5643 /*
5644 5644 * Function: sd_create_devid
5645 5645 *
5646 5646 * Description: This routine will fabricate the device id and write it
5647 5647 * to the disk.
5648 5648 *
5649 5649 * Arguments: un - driver soft state (unit) structure
5650 5650 *
5651 5651 * Return Code: value of the fabricated device id
5652 5652 *
5653 5653 * Context: Kernel Thread
5654 5654 */
5655 5655
5656 5656 static ddi_devid_t
5657 5657 sd_create_devid(sd_ssc_t *ssc)
5658 5658 {
5659 5659 struct sd_lun *un;
5660 5660
5661 5661 ASSERT(ssc != NULL);
5662 5662 un = ssc->ssc_un;
5663 5663 ASSERT(un != NULL);
5664 5664
5665 5665 /* Fabricate the devid */
5666 5666 if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5667 5667 == DDI_FAILURE) {
5668 5668 return (NULL);
5669 5669 }
5670 5670
5671 5671 /* Write the devid to disk */
5672 5672 if (sd_write_deviceid(ssc) != 0) {
5673 5673 ddi_devid_free(un->un_devid);
5674 5674 un->un_devid = NULL;
5675 5675 }
5676 5676
5677 5677 return (un->un_devid);
5678 5678 }
5679 5679
5680 5680
5681 5681 /*
5682 5682 * Function: sd_write_deviceid
5683 5683 *
5684 5684 * Description: This routine will write the device id to the disk
5685 5685 * reserved sector.
5686 5686 *
5687 5687 * Arguments: un - driver soft state (unit) structure
5688 5688 *
5689 5689 * Return Code: EINVAL
5690 5690 * value returned by sd_send_scsi_cmd
5691 5691 *
5692 5692 * Context: Kernel Thread
5693 5693 */
5694 5694
5695 5695 static int
5696 5696 sd_write_deviceid(sd_ssc_t *ssc)
5697 5697 {
5698 5698 struct dk_devid *dkdevid;
5699 5699 uchar_t *buf;
5700 5700 diskaddr_t blk;
5701 5701 uint_t *ip, chksum;
5702 5702 int status;
5703 5703 int i;
5704 5704 struct sd_lun *un;
5705 5705
5706 5706 ASSERT(ssc != NULL);
5707 5707 un = ssc->ssc_un;
5708 5708 ASSERT(un != NULL);
5709 5709 ASSERT(mutex_owned(SD_MUTEX(un)));
5710 5710
5711 5711 mutex_exit(SD_MUTEX(un));
5712 5712 if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5713 5713 (void *)SD_PATH_DIRECT) != 0) {
5714 5714 mutex_enter(SD_MUTEX(un));
5715 5715 return (-1);
5716 5716 }
5717 5717
5718 5718
5719 5719 /* Allocate the buffer */
5720 5720 buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5721 5721 dkdevid = (struct dk_devid *)buf;
5722 5722
5723 5723 /* Fill in the revision */
5724 5724 dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5725 5725 dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5726 5726
5727 5727 /* Copy in the device id */
5728 5728 mutex_enter(SD_MUTEX(un));
5729 5729 bcopy(un->un_devid, &dkdevid->dkd_devid,
5730 5730 ddi_devid_sizeof(un->un_devid));
5731 5731 mutex_exit(SD_MUTEX(un));
5732 5732
5733 5733 /* Calculate the checksum */
5734 5734 chksum = 0;
5735 5735 ip = (uint_t *)dkdevid;
5736 5736 for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5737 5737 i++) {
5738 5738 chksum ^= ip[i];
5739 5739 }
5740 5740
5741 5741 /* Fill-in checksum */
5742 5742 DKD_FORMCHKSUM(chksum, dkdevid);
5743 5743
5744 5744 /* Write the reserved sector */
5745 5745 status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5746 5746 SD_PATH_DIRECT);
5747 5747 if (status != 0)
5748 5748 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5749 5749
5750 5750 kmem_free(buf, un->un_sys_blocksize);
5751 5751
5752 5752 mutex_enter(SD_MUTEX(un));
5753 5753 return (status);
5754 5754 }
5755 5755
5756 5756
5757 5757 /*
5758 5758 * Function: sd_check_vpd_page_support
5759 5759 *
5760 5760 * Description: This routine sends an inquiry command with the EVPD bit set and
5761 5761 * a page code of 0x00 to the device. It is used to determine which
5762 5762 * vital product pages are available to find the devid. We are
5763 5763 * looking for pages 0x83 0x80 or 0xB1. If we return a negative 1,
5764 5764 * the device does not support that command.
5765 5765 *
5766 5766 * Arguments: un - driver soft state (unit) structure
5767 5767 *
5768 5768 * Return Code: 0 - success
5769 5769 * 1 - check condition
5770 5770 *
5771 5771 * Context: This routine can sleep.
5772 5772 */
5773 5773
5774 5774 static int
5775 5775 sd_check_vpd_page_support(sd_ssc_t *ssc)
5776 5776 {
5777 5777 uchar_t *page_list = NULL;
5778 5778 uchar_t page_length = 0xff; /* Use max possible length */
5779 5779 uchar_t evpd = 0x01; /* Set the EVPD bit */
5780 5780 uchar_t page_code = 0x00; /* Supported VPD Pages */
5781 5781 int rval = 0;
5782 5782 int counter;
5783 5783 struct sd_lun *un;
5784 5784
5785 5785 ASSERT(ssc != NULL);
5786 5786 un = ssc->ssc_un;
5787 5787 ASSERT(un != NULL);
5788 5788 ASSERT(mutex_owned(SD_MUTEX(un)));
5789 5789
5790 5790 mutex_exit(SD_MUTEX(un));
5791 5791
5792 5792 /*
5793 5793 * We'll set the page length to the maximum to save figuring it out
5794 5794 * with an additional call.
5795 5795 */
5796 5796 page_list = kmem_zalloc(page_length, KM_SLEEP);
5797 5797
5798 5798 rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5799 5799 page_code, NULL);
5800 5800
5801 5801 if (rval != 0)
5802 5802 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5803 5803
5804 5804 mutex_enter(SD_MUTEX(un));
5805 5805
5806 5806 /*
5807 5807 * Now we must validate that the device accepted the command, as some
5808 5808 * drives do not support it. If the drive does support it, we will
5809 5809 * return 0, and the supported pages will be in un_vpd_page_mask. If
5810 5810 * not, we return -1.
5811 5811 */
5812 5812 if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5813 5813 /* Loop to find one of the 2 pages we need */
5814 5814 counter = 4; /* Supported pages start at byte 4, with 0x00 */
5815 5815
5816 5816 /*
5817 5817 * Pages are returned in ascending order, and 0x83 is what we
5818 5818 * are hoping for.
5819 5819 */
5820 5820 while ((page_list[counter] <= 0xB1) &&
5821 5821 (counter <= (page_list[VPD_PAGE_LENGTH] +
5822 5822 VPD_HEAD_OFFSET))) {
5823 5823 /*
5824 5824 * Add 3 because page_list[3] is the number of
5825 5825 * pages minus 3
5826 5826 */
5827 5827
5828 5828 switch (page_list[counter]) {
5829 5829 case 0x00:
5830 5830 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5831 5831 break;
5832 5832 case 0x80:
5833 5833 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5834 5834 break;
5835 5835 case 0x81:
5836 5836 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5837 5837 break;
5838 5838 case 0x82:
5839 5839 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5840 5840 break;
5841 5841 case 0x83:
5842 5842 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5843 5843 break;
5844 5844 case 0x86:
5845 5845 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5846 5846 break;
5847 5847 case 0xB1:
5848 5848 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5849 5849 break;
5850 5850 }
5851 5851 counter++;
5852 5852 }
5853 5853
5854 5854 } else {
5855 5855 rval = -1;
5856 5856
5857 5857 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5858 5858 "sd_check_vpd_page_support: This drive does not implement "
5859 5859 "VPD pages.\n");
5860 5860 }
5861 5861
5862 5862 kmem_free(page_list, page_length);
5863 5863
5864 5864 return (rval);
5865 5865 }
5866 5866
5867 5867
5868 5868 /*
5869 5869 * Function: sd_setup_pm
5870 5870 *
5871 5871 * Description: Initialize Power Management on the device
5872 5872 *
5873 5873 * Context: Kernel Thread
5874 5874 */
5875 5875
5876 5876 static void
5877 5877 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5878 5878 {
5879 5879 uint_t log_page_size;
5880 5880 uchar_t *log_page_data;
5881 5881 int rval = 0;
5882 5882 struct sd_lun *un;
5883 5883
5884 5884 ASSERT(ssc != NULL);
5885 5885 un = ssc->ssc_un;
5886 5886 ASSERT(un != NULL);
5887 5887
5888 5888 /*
5889 5889 * Since we are called from attach, holding a mutex for
5890 5890 * un is unnecessary. Because some of the routines called
5891 5891 * from here require SD_MUTEX to not be held, assert this
5892 5892 * right up front.
5893 5893 */
5894 5894 ASSERT(!mutex_owned(SD_MUTEX(un)));
5895 5895 /*
5896 5896 * Since the sd device does not have the 'reg' property,
5897 5897 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5898 5898 * The following code is to tell cpr that this device
5899 5899 * DOES need to be suspended and resumed.
5900 5900 */
5901 5901 (void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5902 5902 "pm-hardware-state", "needs-suspend-resume");
5903 5903
5904 5904 /*
5905 5905 * This complies with the new power management framework
5906 5906 * for certain desktop machines. Create the pm_components
5907 5907 * property as a string array property.
5908 5908 * If un_f_pm_supported is TRUE, that means the disk
5909 5909 * attached HBA has set the "pm-capable" property and
5910 5910 * the value of this property is bigger than 0.
5911 5911 */
5912 5912 if (un->un_f_pm_supported) {
5913 5913 /*
5914 5914 * not all devices have a motor, try it first.
5915 5915 * some devices may return ILLEGAL REQUEST, some
5916 5916 * will hang
5917 5917 * The following START_STOP_UNIT is used to check if target
5918 5918 * device has a motor.
5919 5919 */
5920 5920 un->un_f_start_stop_supported = TRUE;
5921 5921
5922 5922 if (un->un_f_power_condition_supported) {
5923 5923 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5924 5924 SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5925 5925 SD_PATH_DIRECT);
5926 5926 if (rval != 0) {
5927 5927 un->un_f_power_condition_supported = FALSE;
5928 5928 }
5929 5929 }
5930 5930 if (!un->un_f_power_condition_supported) {
5931 5931 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5932 5932 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5933 5933 }
5934 5934 if (rval != 0) {
5935 5935 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5936 5936 un->un_f_start_stop_supported = FALSE;
5937 5937 }
5938 5938
5939 5939 /*
5940 5940 * create pm properties anyways otherwise the parent can't
5941 5941 * go to sleep
5942 5942 */
5943 5943 un->un_f_pm_is_enabled = TRUE;
5944 5944 (void) sd_create_pm_components(devi, un);
5945 5945
5946 5946 /*
5947 5947 * If it claims that log sense is supported, check it out.
5948 5948 */
5949 5949 if (un->un_f_log_sense_supported) {
5950 5950 rval = sd_log_page_supported(ssc,
5951 5951 START_STOP_CYCLE_PAGE);
5952 5952 if (rval == 1) {
5953 5953 /* Page found, use it. */
5954 5954 un->un_start_stop_cycle_page =
5955 5955 START_STOP_CYCLE_PAGE;
5956 5956 } else {
5957 5957 /*
5958 5958 * Page not found or log sense is not
5959 5959 * supported.
5960 5960 * Notice we do not check the old style
5961 5961 * START_STOP_CYCLE_VU_PAGE because this
5962 5962 * code path does not apply to old disks.
5963 5963 */
5964 5964 un->un_f_log_sense_supported = FALSE;
5965 5965 un->un_f_pm_log_sense_smart = FALSE;
5966 5966 }
5967 5967 }
5968 5968
5969 5969 return;
5970 5970 }
5971 5971
5972 5972 /*
5973 5973 * For the disk whose attached HBA has not set the "pm-capable"
5974 5974 * property, check if it supports the power management.
5975 5975 */
5976 5976 if (!un->un_f_log_sense_supported) {
5977 5977 un->un_power_level = SD_SPINDLE_ON;
5978 5978 un->un_f_pm_is_enabled = FALSE;
5979 5979 return;
5980 5980 }
5981 5981
5982 5982 rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5983 5983
5984 5984 #ifdef SDDEBUG
5985 5985 if (sd_force_pm_supported) {
5986 5986 /* Force a successful result */
5987 5987 rval = 1;
5988 5988 }
5989 5989 #endif
5990 5990
5991 5991 /*
5992 5992 * If the start-stop cycle counter log page is not supported
5993 5993 * or if the pm-capable property is set to be false (0),
5994 5994 * then we should not create the pm_components property.
5995 5995 */
5996 5996 if (rval == -1) {
5997 5997 /*
5998 5998 * Error.
5999 5999 * Reading log sense failed, most likely this is
6000 6000 * an older drive that does not support log sense.
6001 6001 * If this fails auto-pm is not supported.
6002 6002 */
6003 6003 un->un_power_level = SD_SPINDLE_ON;
6004 6004 un->un_f_pm_is_enabled = FALSE;
6005 6005
6006 6006 } else if (rval == 0) {
6007 6007 /*
6008 6008 * Page not found.
6009 6009 * The start stop cycle counter is implemented as page
6010 6010 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6011 6011 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6012 6012 */
6013 6013 if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6014 6014 /*
6015 6015 * Page found, use this one.
6016 6016 */
6017 6017 un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6018 6018 un->un_f_pm_is_enabled = TRUE;
6019 6019 } else {
6020 6020 /*
6021 6021 * Error or page not found.
6022 6022 * auto-pm is not supported for this device.
6023 6023 */
6024 6024 un->un_power_level = SD_SPINDLE_ON;
6025 6025 un->un_f_pm_is_enabled = FALSE;
6026 6026 }
6027 6027 } else {
6028 6028 /*
6029 6029 * Page found, use it.
6030 6030 */
6031 6031 un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6032 6032 un->un_f_pm_is_enabled = TRUE;
6033 6033 }
6034 6034
6035 6035
6036 6036 if (un->un_f_pm_is_enabled == TRUE) {
6037 6037 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6038 6038 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6039 6039
6040 6040 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6041 6041 log_page_size, un->un_start_stop_cycle_page,
6042 6042 0x01, 0, SD_PATH_DIRECT);
6043 6043
6044 6044 if (rval != 0) {
6045 6045 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6046 6046 }
6047 6047
6048 6048 #ifdef SDDEBUG
6049 6049 if (sd_force_pm_supported) {
6050 6050 /* Force a successful result */
6051 6051 rval = 0;
6052 6052 }
6053 6053 #endif
6054 6054
6055 6055 /*
6056 6056 * If the Log sense for Page( Start/stop cycle counter page)
6057 6057 * succeeds, then power management is supported and we can
6058 6058 * enable auto-pm.
6059 6059 */
6060 6060 if (rval == 0) {
6061 6061 (void) sd_create_pm_components(devi, un);
6062 6062 } else {
6063 6063 un->un_power_level = SD_SPINDLE_ON;
6064 6064 un->un_f_pm_is_enabled = FALSE;
6065 6065 }
6066 6066
6067 6067 kmem_free(log_page_data, log_page_size);
6068 6068 }
6069 6069 }
6070 6070
6071 6071
6072 6072 /*
6073 6073 * Function: sd_create_pm_components
6074 6074 *
6075 6075 * Description: Initialize PM property.
6076 6076 *
6077 6077 * Context: Kernel thread context
6078 6078 */
6079 6079
6080 6080 static void
6081 6081 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6082 6082 {
6083 6083 ASSERT(!mutex_owned(SD_MUTEX(un)));
6084 6084
6085 6085 if (un->un_f_power_condition_supported) {
6086 6086 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6087 6087 "pm-components", sd_pwr_pc.pm_comp, 5)
6088 6088 != DDI_PROP_SUCCESS) {
6089 6089 un->un_power_level = SD_SPINDLE_ACTIVE;
6090 6090 un->un_f_pm_is_enabled = FALSE;
6091 6091 return;
6092 6092 }
6093 6093 } else {
6094 6094 if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6095 6095 "pm-components", sd_pwr_ss.pm_comp, 3)
6096 6096 != DDI_PROP_SUCCESS) {
6097 6097 un->un_power_level = SD_SPINDLE_ON;
6098 6098 un->un_f_pm_is_enabled = FALSE;
6099 6099 return;
6100 6100 }
6101 6101 }
6102 6102 /*
6103 6103 * When components are initially created they are idle,
6104 6104 * power up any non-removables.
6105 6105 * Note: the return value of pm_raise_power can't be used
6106 6106 * for determining if PM should be enabled for this device.
6107 6107 * Even if you check the return values and remove this
6108 6108 * property created above, the PM framework will not honor the
6109 6109 * change after the first call to pm_raise_power. Hence,
6110 6110 * removal of that property does not help if pm_raise_power
6111 6111 * fails. In the case of removable media, the start/stop
6112 6112 * will fail if the media is not present.
6113 6113 */
6114 6114 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6115 6115 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6116 6116 mutex_enter(SD_MUTEX(un));
6117 6117 un->un_power_level = SD_PM_STATE_ACTIVE(un);
6118 6118 mutex_enter(&un->un_pm_mutex);
6119 6119 /* Set to on and not busy. */
6120 6120 un->un_pm_count = 0;
6121 6121 } else {
6122 6122 mutex_enter(SD_MUTEX(un));
6123 6123 un->un_power_level = SD_PM_STATE_STOPPED(un);
6124 6124 mutex_enter(&un->un_pm_mutex);
6125 6125 /* Set to off. */
6126 6126 un->un_pm_count = -1;
6127 6127 }
6128 6128 mutex_exit(&un->un_pm_mutex);
6129 6129 mutex_exit(SD_MUTEX(un));
6130 6130 }
6131 6131
6132 6132
6133 6133 /*
6134 6134 * Function: sd_ddi_suspend
6135 6135 *
6136 6136 * Description: Performs system power-down operations. This includes
6137 6137 * setting the drive state to indicate its suspended so
6138 6138 * that no new commands will be accepted. Also, wait for
6139 6139 * all commands that are in transport or queued to a timer
6140 6140 * for retry to complete. All timeout threads are cancelled.
6141 6141 *
6142 6142 * Return Code: DDI_FAILURE or DDI_SUCCESS
6143 6143 *
6144 6144 * Context: Kernel thread context
6145 6145 */
6146 6146
6147 6147 static int
6148 6148 sd_ddi_suspend(dev_info_t *devi)
6149 6149 {
6150 6150 struct sd_lun *un;
6151 6151 clock_t wait_cmds_complete;
6152 6152
6153 6153 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6154 6154 if (un == NULL) {
6155 6155 return (DDI_FAILURE);
6156 6156 }
6157 6157
6158 6158 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6159 6159
6160 6160 mutex_enter(SD_MUTEX(un));
6161 6161
6162 6162 /* Return success if the device is already suspended. */
6163 6163 if (un->un_state == SD_STATE_SUSPENDED) {
6164 6164 mutex_exit(SD_MUTEX(un));
6165 6165 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6166 6166 "device already suspended, exiting\n");
6167 6167 return (DDI_SUCCESS);
6168 6168 }
6169 6169
6170 6170 /* Return failure if the device is being used by HA */
6171 6171 if (un->un_resvd_status &
6172 6172 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6173 6173 mutex_exit(SD_MUTEX(un));
6174 6174 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6175 6175 "device in use by HA, exiting\n");
6176 6176 return (DDI_FAILURE);
6177 6177 }
6178 6178
6179 6179 /*
6180 6180 * Return failure if the device is in a resource wait
6181 6181 * or power changing state.
6182 6182 */
6183 6183 if ((un->un_state == SD_STATE_RWAIT) ||
6184 6184 (un->un_state == SD_STATE_PM_CHANGING)) {
6185 6185 mutex_exit(SD_MUTEX(un));
6186 6186 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6187 6187 "device in resource wait state, exiting\n");
6188 6188 return (DDI_FAILURE);
6189 6189 }
6190 6190
6191 6191
6192 6192 un->un_save_state = un->un_last_state;
6193 6193 New_state(un, SD_STATE_SUSPENDED);
6194 6194
6195 6195 /*
6196 6196 * Wait for all commands that are in transport or queued to a timer
6197 6197 * for retry to complete.
6198 6198 *
6199 6199 * While waiting, no new commands will be accepted or sent because of
6200 6200 * the new state we set above.
6201 6201 *
6202 6202 * Wait till current operation has completed. If we are in the resource
6203 6203 * wait state (with an intr outstanding) then we need to wait till the
6204 6204 * intr completes and starts the next cmd. We want to wait for
6205 6205 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6206 6206 */
6207 6207 wait_cmds_complete = ddi_get_lbolt() +
6208 6208 (sd_wait_cmds_complete * drv_usectohz(1000000));
6209 6209
6210 6210 while (un->un_ncmds_in_transport != 0) {
6211 6211 /*
6212 6212 * Fail if commands do not finish in the specified time.
6213 6213 */
6214 6214 if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6215 6215 wait_cmds_complete) == -1) {
6216 6216 /*
6217 6217 * Undo the state changes made above. Everything
6218 6218 * must go back to it's original value.
6219 6219 */
6220 6220 Restore_state(un);
6221 6221 un->un_last_state = un->un_save_state;
6222 6222 /* Wake up any threads that might be waiting. */
6223 6223 cv_broadcast(&un->un_suspend_cv);
6224 6224 mutex_exit(SD_MUTEX(un));
6225 6225 SD_ERROR(SD_LOG_IO_PM, un,
6226 6226 "sd_ddi_suspend: failed due to outstanding cmds\n");
6227 6227 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6228 6228 return (DDI_FAILURE);
6229 6229 }
6230 6230 }
6231 6231
6232 6232 /*
6233 6233 * Cancel SCSI watch thread and timeouts, if any are active
6234 6234 */
6235 6235
6236 6236 if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6237 6237 opaque_t temp_token = un->un_swr_token;
6238 6238 mutex_exit(SD_MUTEX(un));
6239 6239 scsi_watch_suspend(temp_token);
6240 6240 mutex_enter(SD_MUTEX(un));
6241 6241 }
6242 6242
6243 6243 if (un->un_reset_throttle_timeid != NULL) {
6244 6244 timeout_id_t temp_id = un->un_reset_throttle_timeid;
6245 6245 un->un_reset_throttle_timeid = NULL;
6246 6246 mutex_exit(SD_MUTEX(un));
6247 6247 (void) untimeout(temp_id);
6248 6248 mutex_enter(SD_MUTEX(un));
6249 6249 }
6250 6250
6251 6251 if (un->un_dcvb_timeid != NULL) {
6252 6252 timeout_id_t temp_id = un->un_dcvb_timeid;
6253 6253 un->un_dcvb_timeid = NULL;
6254 6254 mutex_exit(SD_MUTEX(un));
6255 6255 (void) untimeout(temp_id);
6256 6256 mutex_enter(SD_MUTEX(un));
6257 6257 }
6258 6258
6259 6259 mutex_enter(&un->un_pm_mutex);
6260 6260 if (un->un_pm_timeid != NULL) {
6261 6261 timeout_id_t temp_id = un->un_pm_timeid;
6262 6262 un->un_pm_timeid = NULL;
6263 6263 mutex_exit(&un->un_pm_mutex);
6264 6264 mutex_exit(SD_MUTEX(un));
6265 6265 (void) untimeout(temp_id);
6266 6266 mutex_enter(SD_MUTEX(un));
6267 6267 } else {
6268 6268 mutex_exit(&un->un_pm_mutex);
6269 6269 }
6270 6270
6271 6271 if (un->un_rmw_msg_timeid != NULL) {
6272 6272 timeout_id_t temp_id = un->un_rmw_msg_timeid;
6273 6273 un->un_rmw_msg_timeid = NULL;
6274 6274 mutex_exit(SD_MUTEX(un));
6275 6275 (void) untimeout(temp_id);
6276 6276 mutex_enter(SD_MUTEX(un));
6277 6277 }
6278 6278
6279 6279 if (un->un_retry_timeid != NULL) {
6280 6280 timeout_id_t temp_id = un->un_retry_timeid;
6281 6281 un->un_retry_timeid = NULL;
6282 6282 mutex_exit(SD_MUTEX(un));
6283 6283 (void) untimeout(temp_id);
6284 6284 mutex_enter(SD_MUTEX(un));
6285 6285
6286 6286 if (un->un_retry_bp != NULL) {
6287 6287 un->un_retry_bp->av_forw = un->un_waitq_headp;
6288 6288 un->un_waitq_headp = un->un_retry_bp;
6289 6289 if (un->un_waitq_tailp == NULL) {
6290 6290 un->un_waitq_tailp = un->un_retry_bp;
6291 6291 }
6292 6292 un->un_retry_bp = NULL;
6293 6293 un->un_retry_statp = NULL;
6294 6294 }
6295 6295 }
6296 6296
6297 6297 if (un->un_direct_priority_timeid != NULL) {
6298 6298 timeout_id_t temp_id = un->un_direct_priority_timeid;
6299 6299 un->un_direct_priority_timeid = NULL;
6300 6300 mutex_exit(SD_MUTEX(un));
6301 6301 (void) untimeout(temp_id);
6302 6302 mutex_enter(SD_MUTEX(un));
6303 6303 }
6304 6304
6305 6305 if (un->un_f_is_fibre == TRUE) {
6306 6306 /*
6307 6307 * Remove callbacks for insert and remove events
6308 6308 */
6309 6309 if (un->un_insert_event != NULL) {
6310 6310 mutex_exit(SD_MUTEX(un));
6311 6311 (void) ddi_remove_event_handler(un->un_insert_cb_id);
6312 6312 mutex_enter(SD_MUTEX(un));
6313 6313 un->un_insert_event = NULL;
6314 6314 }
6315 6315
6316 6316 if (un->un_remove_event != NULL) {
6317 6317 mutex_exit(SD_MUTEX(un));
6318 6318 (void) ddi_remove_event_handler(un->un_remove_cb_id);
6319 6319 mutex_enter(SD_MUTEX(un));
6320 6320 un->un_remove_event = NULL;
6321 6321 }
6322 6322 }
6323 6323
6324 6324 mutex_exit(SD_MUTEX(un));
6325 6325
6326 6326 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6327 6327
6328 6328 return (DDI_SUCCESS);
6329 6329 }
6330 6330
6331 6331
6332 6332 /*
6333 6333 * Function: sd_ddi_resume
6334 6334 *
6335 6335 * Description: Performs system power-up operations..
6336 6336 *
6337 6337 * Return Code: DDI_SUCCESS
6338 6338 * DDI_FAILURE
6339 6339 *
6340 6340 * Context: Kernel thread context
6341 6341 */
6342 6342
6343 6343 static int
6344 6344 sd_ddi_resume(dev_info_t *devi)
6345 6345 {
6346 6346 struct sd_lun *un;
6347 6347
6348 6348 un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6349 6349 if (un == NULL) {
6350 6350 return (DDI_FAILURE);
6351 6351 }
6352 6352
6353 6353 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6354 6354
6355 6355 mutex_enter(SD_MUTEX(un));
6356 6356 Restore_state(un);
6357 6357
6358 6358 /*
6359 6359 * Restore the state which was saved to give the
6360 6360 * the right state in un_last_state
6361 6361 */
6362 6362 un->un_last_state = un->un_save_state;
6363 6363 /*
6364 6364 * Note: throttle comes back at full.
6365 6365 * Also note: this MUST be done before calling pm_raise_power
6366 6366 * otherwise the system can get hung in biowait. The scenario where
6367 6367 * this'll happen is under cpr suspend. Writing of the system
6368 6368 * state goes through sddump, which writes 0 to un_throttle. If
6369 6369 * writing the system state then fails, example if the partition is
6370 6370 * too small, then cpr attempts a resume. If throttle isn't restored
6371 6371 * from the saved value until after calling pm_raise_power then
6372 6372 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6373 6373 * in biowait.
6374 6374 */
6375 6375 un->un_throttle = un->un_saved_throttle;
6376 6376
6377 6377 /*
6378 6378 * The chance of failure is very rare as the only command done in power
6379 6379 * entry point is START command when you transition from 0->1 or
6380 6380 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6381 6381 * which suspend was done. Ignore the return value as the resume should
6382 6382 * not be failed. In the case of removable media the media need not be
6383 6383 * inserted and hence there is a chance that raise power will fail with
6384 6384 * media not present.
6385 6385 */
6386 6386 if (un->un_f_attach_spinup) {
6387 6387 mutex_exit(SD_MUTEX(un));
6388 6388 (void) pm_raise_power(SD_DEVINFO(un), 0,
6389 6389 SD_PM_STATE_ACTIVE(un));
6390 6390 mutex_enter(SD_MUTEX(un));
6391 6391 }
6392 6392
6393 6393 /*
6394 6394 * Don't broadcast to the suspend cv and therefore possibly
6395 6395 * start I/O until after power has been restored.
6396 6396 */
6397 6397 cv_broadcast(&un->un_suspend_cv);
6398 6398 cv_broadcast(&un->un_state_cv);
6399 6399
6400 6400 /* restart thread */
6401 6401 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6402 6402 scsi_watch_resume(un->un_swr_token);
6403 6403 }
6404 6404
6405 6405 #if (defined(__fibre))
6406 6406 if (un->un_f_is_fibre == TRUE) {
6407 6407 /*
6408 6408 * Add callbacks for insert and remove events
6409 6409 */
6410 6410 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6411 6411 sd_init_event_callbacks(un);
6412 6412 }
6413 6413 }
6414 6414 #endif
6415 6415
6416 6416 /*
6417 6417 * Transport any pending commands to the target.
6418 6418 *
6419 6419 * If this is a low-activity device commands in queue will have to wait
6420 6420 * until new commands come in, which may take awhile. Also, we
6421 6421 * specifically don't check un_ncmds_in_transport because we know that
6422 6422 * there really are no commands in progress after the unit was
6423 6423 * suspended and we could have reached the throttle level, been
6424 6424 * suspended, and have no new commands coming in for awhile. Highly
6425 6425 * unlikely, but so is the low-activity disk scenario.
6426 6426 */
6427 6427 ddi_xbuf_dispatch(un->un_xbuf_attr);
6428 6428
6429 6429 sd_start_cmds(un, NULL);
6430 6430 mutex_exit(SD_MUTEX(un));
6431 6431
6432 6432 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6433 6433
6434 6434 return (DDI_SUCCESS);
6435 6435 }
6436 6436
6437 6437
6438 6438 /*
6439 6439 * Function: sd_pm_state_change
6440 6440 *
6441 6441 * Description: Change the driver power state.
6442 6442 * Someone else is required to actually change the driver
6443 6443 * power level.
6444 6444 *
6445 6445 * Arguments: un - driver soft state (unit) structure
6446 6446 * level - the power level that is changed to
6447 6447 * flag - to decide how to change the power state
6448 6448 *
6449 6449 * Return Code: DDI_SUCCESS
6450 6450 *
6451 6451 * Context: Kernel thread context
6452 6452 */
6453 6453 static int
6454 6454 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6455 6455 {
6456 6456 ASSERT(un != NULL);
6457 6457 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6458 6458
6459 6459 ASSERT(!mutex_owned(SD_MUTEX(un)));
6460 6460 mutex_enter(SD_MUTEX(un));
6461 6461
6462 6462 if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6463 6463 un->un_power_level = level;
6464 6464 ASSERT(!mutex_owned(&un->un_pm_mutex));
6465 6465 mutex_enter(&un->un_pm_mutex);
6466 6466 if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6467 6467 un->un_pm_count++;
6468 6468 ASSERT(un->un_pm_count == 0);
6469 6469 }
6470 6470 mutex_exit(&un->un_pm_mutex);
6471 6471 } else {
6472 6472 /*
6473 6473 * Exit if power management is not enabled for this device,
6474 6474 * or if the device is being used by HA.
6475 6475 */
6476 6476 if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6477 6477 (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6478 6478 mutex_exit(SD_MUTEX(un));
6479 6479 SD_TRACE(SD_LOG_POWER, un,
6480 6480 "sd_pm_state_change: exiting\n");
6481 6481 return (DDI_FAILURE);
6482 6482 }
6483 6483
6484 6484 SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6485 6485 "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6486 6486
6487 6487 /*
6488 6488 * See if the device is not busy, ie.:
6489 6489 * - we have no commands in the driver for this device
6490 6490 * - not waiting for resources
6491 6491 */
6492 6492 if ((un->un_ncmds_in_driver == 0) &&
6493 6493 (un->un_state != SD_STATE_RWAIT)) {
6494 6494 /*
6495 6495 * The device is not busy, so it is OK to go to low
6496 6496 * power state. Indicate low power, but rely on someone
6497 6497 * else to actually change it.
6498 6498 */
6499 6499 mutex_enter(&un->un_pm_mutex);
6500 6500 un->un_pm_count = -1;
6501 6501 mutex_exit(&un->un_pm_mutex);
6502 6502 un->un_power_level = level;
6503 6503 }
6504 6504 }
6505 6505
6506 6506 mutex_exit(SD_MUTEX(un));
6507 6507
6508 6508 SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6509 6509
6510 6510 return (DDI_SUCCESS);
6511 6511 }
6512 6512
6513 6513
6514 6514 /*
6515 6515 * Function: sd_pm_idletimeout_handler
6516 6516 *
6517 6517 * Description: A timer routine that's active only while a device is busy.
6518 6518 * The purpose is to extend slightly the pm framework's busy
6519 6519 * view of the device to prevent busy/idle thrashing for
6520 6520 * back-to-back commands. Do this by comparing the current time
6521 6521 * to the time at which the last command completed and when the
6522 6522 * difference is greater than sd_pm_idletime, call
6523 6523 * pm_idle_component. In addition to indicating idle to the pm
6524 6524 * framework, update the chain type to again use the internal pm
6525 6525 * layers of the driver.
6526 6526 *
6527 6527 * Arguments: arg - driver soft state (unit) structure
6528 6528 *
6529 6529 * Context: Executes in a timeout(9F) thread context
6530 6530 */
6531 6531
6532 6532 static void
6533 6533 sd_pm_idletimeout_handler(void *arg)
6534 6534 {
6535 6535 struct sd_lun *un = arg;
6536 6536
6537 6537 time_t now;
6538 6538
6539 6539 mutex_enter(&sd_detach_mutex);
6540 6540 if (un->un_detach_count != 0) {
6541 6541 /* Abort if the instance is detaching */
6542 6542 mutex_exit(&sd_detach_mutex);
6543 6543 return;
6544 6544 }
6545 6545 mutex_exit(&sd_detach_mutex);
6546 6546
6547 6547 now = ddi_get_time();
6548 6548 /*
6549 6549 * Grab both mutexes, in the proper order, since we're accessing
6550 6550 * both PM and softstate variables.
6551 6551 */
6552 6552 mutex_enter(SD_MUTEX(un));
6553 6553 mutex_enter(&un->un_pm_mutex);
6554 6554 if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
6555 6555 (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6556 6556 /*
6557 6557 * Update the chain types.
6558 6558 * This takes affect on the next new command received.
6559 6559 */
6560 6560 if (un->un_f_non_devbsize_supported) {
6561 6561 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6562 6562 } else {
6563 6563 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6564 6564 }
6565 6565 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6566 6566
6567 6567 SD_TRACE(SD_LOG_IO_PM, un,
6568 6568 "sd_pm_idletimeout_handler: idling device\n");
6569 6569 (void) pm_idle_component(SD_DEVINFO(un), 0);
6570 6570 un->un_pm_idle_timeid = NULL;
6571 6571 } else {
6572 6572 un->un_pm_idle_timeid =
6573 6573 timeout(sd_pm_idletimeout_handler, un,
6574 6574 (drv_usectohz((clock_t)300000))); /* 300 ms. */
6575 6575 }
6576 6576 mutex_exit(&un->un_pm_mutex);
6577 6577 mutex_exit(SD_MUTEX(un));
6578 6578 }
6579 6579
6580 6580
6581 6581 /*
6582 6582 * Function: sd_pm_timeout_handler
6583 6583 *
6584 6584 * Description: Callback to tell framework we are idle.
6585 6585 *
6586 6586 * Context: timeout(9f) thread context.
6587 6587 */
6588 6588
6589 6589 static void
6590 6590 sd_pm_timeout_handler(void *arg)
6591 6591 {
6592 6592 struct sd_lun *un = arg;
6593 6593
6594 6594 (void) pm_idle_component(SD_DEVINFO(un), 0);
6595 6595 mutex_enter(&un->un_pm_mutex);
6596 6596 un->un_pm_timeid = NULL;
6597 6597 mutex_exit(&un->un_pm_mutex);
6598 6598 }
6599 6599
6600 6600
6601 6601 /*
6602 6602 * Function: sdpower
6603 6603 *
6604 6604 * Description: PM entry point.
6605 6605 *
6606 6606 * Return Code: DDI_SUCCESS
6607 6607 * DDI_FAILURE
6608 6608 *
6609 6609 * Context: Kernel thread context
6610 6610 */
6611 6611
6612 6612 static int
6613 6613 sdpower(dev_info_t *devi, int component, int level)
6614 6614 {
6615 6615 struct sd_lun *un;
6616 6616 int instance;
6617 6617 int rval = DDI_SUCCESS;
6618 6618 uint_t i, log_page_size, maxcycles, ncycles;
6619 6619 uchar_t *log_page_data;
6620 6620 int log_sense_page;
6621 6621 int medium_present;
6622 6622 time_t intvlp;
6623 6623 struct pm_trans_data sd_pm_tran_data;
6624 6624 uchar_t save_state;
6625 6625 int sval;
6626 6626 uchar_t state_before_pm;
6627 6627 int got_semaphore_here;
6628 6628 sd_ssc_t *ssc;
6629 6629 int last_power_level;
6630 6630
6631 6631 instance = ddi_get_instance(devi);
6632 6632
6633 6633 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6634 6634 !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6635 6635 return (DDI_FAILURE);
6636 6636 }
6637 6637
6638 6638 ssc = sd_ssc_init(un);
6639 6639
6640 6640 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6641 6641
6642 6642 /*
6643 6643 * Must synchronize power down with close.
6644 6644 * Attempt to decrement/acquire the open/close semaphore,
6645 6645 * but do NOT wait on it. If it's not greater than zero,
6646 6646 * ie. it can't be decremented without waiting, then
6647 6647 * someone else, either open or close, already has it
6648 6648 * and the try returns 0. Use that knowledge here to determine
6649 6649 * if it's OK to change the device power level.
6650 6650 * Also, only increment it on exit if it was decremented, ie. gotten,
6651 6651 * here.
6652 6652 */
6653 6653 got_semaphore_here = sema_tryp(&un->un_semoclose);
6654 6654
6655 6655 mutex_enter(SD_MUTEX(un));
6656 6656
6657 6657 SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6658 6658 un->un_ncmds_in_driver);
6659 6659
6660 6660 /*
6661 6661 * If un_ncmds_in_driver is non-zero it indicates commands are
6662 6662 * already being processed in the driver, or if the semaphore was
6663 6663 * not gotten here it indicates an open or close is being processed.
6664 6664 * At the same time somebody is requesting to go to a lower power
6665 6665 * that can't perform I/O, which can't happen, therefore we need to
6666 6666 * return failure.
6667 6667 */
6668 6668 if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6669 6669 ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6670 6670 mutex_exit(SD_MUTEX(un));
6671 6671
6672 6672 if (got_semaphore_here != 0) {
6673 6673 sema_v(&un->un_semoclose);
6674 6674 }
6675 6675 SD_TRACE(SD_LOG_IO_PM, un,
6676 6676 "sdpower: exit, device has queued cmds.\n");
6677 6677
6678 6678 goto sdpower_failed;
6679 6679 }
6680 6680
6681 6681 /*
6682 6682 * if it is OFFLINE that means the disk is completely dead
6683 6683 * in our case we have to put the disk in on or off by sending commands
6684 6684 * Of course that will fail anyway so return back here.
6685 6685 *
6686 6686 * Power changes to a device that's OFFLINE or SUSPENDED
6687 6687 * are not allowed.
6688 6688 */
6689 6689 if ((un->un_state == SD_STATE_OFFLINE) ||
6690 6690 (un->un_state == SD_STATE_SUSPENDED)) {
6691 6691 mutex_exit(SD_MUTEX(un));
6692 6692
6693 6693 if (got_semaphore_here != 0) {
6694 6694 sema_v(&un->un_semoclose);
6695 6695 }
6696 6696 SD_TRACE(SD_LOG_IO_PM, un,
6697 6697 "sdpower: exit, device is off-line.\n");
6698 6698
6699 6699 goto sdpower_failed;
6700 6700 }
6701 6701
6702 6702 /*
6703 6703 * Change the device's state to indicate it's power level
6704 6704 * is being changed. Do this to prevent a power off in the
6705 6705 * middle of commands, which is especially bad on devices
6706 6706 * that are really powered off instead of just spun down.
6707 6707 */
6708 6708 state_before_pm = un->un_state;
6709 6709 un->un_state = SD_STATE_PM_CHANGING;
6710 6710
6711 6711 mutex_exit(SD_MUTEX(un));
6712 6712
6713 6713 /*
6714 6714 * If log sense command is not supported, bypass the
6715 6715 * following checking, otherwise, check the log sense
6716 6716 * information for this device.
6717 6717 */
6718 6718 if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6719 6719 un->un_f_log_sense_supported) {
6720 6720 /*
6721 6721 * Get the log sense information to understand whether the
6722 6722 * the powercycle counts have gone beyond the threshhold.
6723 6723 */
6724 6724 log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6725 6725 log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6726 6726
6727 6727 mutex_enter(SD_MUTEX(un));
6728 6728 log_sense_page = un->un_start_stop_cycle_page;
6729 6729 mutex_exit(SD_MUTEX(un));
6730 6730
6731 6731 rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6732 6732 log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6733 6733
6734 6734 if (rval != 0) {
6735 6735 if (rval == EIO)
6736 6736 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6737 6737 else
6738 6738 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6739 6739 }
6740 6740
6741 6741 #ifdef SDDEBUG
6742 6742 if (sd_force_pm_supported) {
6743 6743 /* Force a successful result */
6744 6744 rval = 0;
6745 6745 }
6746 6746 #endif
6747 6747 if (rval != 0) {
6748 6748 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6749 6749 "Log Sense Failed\n");
6750 6750
6751 6751 kmem_free(log_page_data, log_page_size);
6752 6752 /* Cannot support power management on those drives */
6753 6753
6754 6754 if (got_semaphore_here != 0) {
6755 6755 sema_v(&un->un_semoclose);
6756 6756 }
6757 6757 /*
6758 6758 * On exit put the state back to it's original value
6759 6759 * and broadcast to anyone waiting for the power
6760 6760 * change completion.
6761 6761 */
6762 6762 mutex_enter(SD_MUTEX(un));
6763 6763 un->un_state = state_before_pm;
6764 6764 cv_broadcast(&un->un_suspend_cv);
6765 6765 mutex_exit(SD_MUTEX(un));
6766 6766 SD_TRACE(SD_LOG_IO_PM, un,
6767 6767 "sdpower: exit, Log Sense Failed.\n");
6768 6768
6769 6769 goto sdpower_failed;
6770 6770 }
6771 6771
6772 6772 /*
6773 6773 * From the page data - Convert the essential information to
6774 6774 * pm_trans_data
6775 6775 */
6776 6776 maxcycles =
6777 6777 (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6778 6778 (log_page_data[0x1E] << 8) | log_page_data[0x1F];
6779 6779
6780 6780 ncycles =
6781 6781 (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6782 6782 (log_page_data[0x26] << 8) | log_page_data[0x27];
6783 6783
6784 6784 if (un->un_f_pm_log_sense_smart) {
6785 6785 sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6786 6786 sd_pm_tran_data.un.smart_count.consumed = ncycles;
6787 6787 sd_pm_tran_data.un.smart_count.flag = 0;
6788 6788 sd_pm_tran_data.format = DC_SMART_FORMAT;
6789 6789 } else {
6790 6790 sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6791 6791 sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6792 6792 for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6793 6793 sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6794 6794 log_page_data[8+i];
6795 6795 }
6796 6796 sd_pm_tran_data.un.scsi_cycles.flag = 0;
6797 6797 sd_pm_tran_data.format = DC_SCSI_FORMAT;
6798 6798 }
6799 6799
6800 6800 kmem_free(log_page_data, log_page_size);
6801 6801
6802 6802 /*
6803 6803 * Call pm_trans_check routine to get the Ok from
6804 6804 * the global policy
6805 6805 */
6806 6806 rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6807 6807 #ifdef SDDEBUG
6808 6808 if (sd_force_pm_supported) {
6809 6809 /* Force a successful result */
6810 6810 rval = 1;
6811 6811 }
6812 6812 #endif
6813 6813 switch (rval) {
6814 6814 case 0:
6815 6815 /*
6816 6816 * Not Ok to Power cycle or error in parameters passed
6817 6817 * Would have given the advised time to consider power
6818 6818 * cycle. Based on the new intvlp parameter we are
6819 6819 * supposed to pretend we are busy so that pm framework
6820 6820 * will never call our power entry point. Because of
6821 6821 * that install a timeout handler and wait for the
6822 6822 * recommended time to elapse so that power management
6823 6823 * can be effective again.
6824 6824 *
6825 6825 * To effect this behavior, call pm_busy_component to
6826 6826 * indicate to the framework this device is busy.
6827 6827 * By not adjusting un_pm_count the rest of PM in
6828 6828 * the driver will function normally, and independent
6829 6829 * of this but because the framework is told the device
6830 6830 * is busy it won't attempt powering down until it gets
6831 6831 * a matching idle. The timeout handler sends this.
6832 6832 * Note: sd_pm_entry can't be called here to do this
6833 6833 * because sdpower may have been called as a result
6834 6834 * of a call to pm_raise_power from within sd_pm_entry.
6835 6835 *
6836 6836 * If a timeout handler is already active then
6837 6837 * don't install another.
6838 6838 */
6839 6839 mutex_enter(&un->un_pm_mutex);
6840 6840 if (un->un_pm_timeid == NULL) {
6841 6841 un->un_pm_timeid =
6842 6842 timeout(sd_pm_timeout_handler,
6843 6843 un, intvlp * drv_usectohz(1000000));
6844 6844 mutex_exit(&un->un_pm_mutex);
6845 6845 (void) pm_busy_component(SD_DEVINFO(un), 0);
6846 6846 } else {
6847 6847 mutex_exit(&un->un_pm_mutex);
6848 6848 }
6849 6849 if (got_semaphore_here != 0) {
6850 6850 sema_v(&un->un_semoclose);
6851 6851 }
6852 6852 /*
6853 6853 * On exit put the state back to it's original value
6854 6854 * and broadcast to anyone waiting for the power
6855 6855 * change completion.
6856 6856 */
6857 6857 mutex_enter(SD_MUTEX(un));
6858 6858 un->un_state = state_before_pm;
6859 6859 cv_broadcast(&un->un_suspend_cv);
6860 6860 mutex_exit(SD_MUTEX(un));
6861 6861
6862 6862 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6863 6863 "trans check Failed, not ok to power cycle.\n");
6864 6864
6865 6865 goto sdpower_failed;
6866 6866 case -1:
6867 6867 if (got_semaphore_here != 0) {
6868 6868 sema_v(&un->un_semoclose);
6869 6869 }
6870 6870 /*
6871 6871 * On exit put the state back to it's original value
6872 6872 * and broadcast to anyone waiting for the power
6873 6873 * change completion.
6874 6874 */
6875 6875 mutex_enter(SD_MUTEX(un));
6876 6876 un->un_state = state_before_pm;
6877 6877 cv_broadcast(&un->un_suspend_cv);
6878 6878 mutex_exit(SD_MUTEX(un));
6879 6879 SD_TRACE(SD_LOG_IO_PM, un,
6880 6880 "sdpower: exit, trans check command Failed.\n");
6881 6881
6882 6882 goto sdpower_failed;
6883 6883 }
6884 6884 }
6885 6885
6886 6886 if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6887 6887 /*
6888 6888 * Save the last state... if the STOP FAILS we need it
6889 6889 * for restoring
6890 6890 */
6891 6891 mutex_enter(SD_MUTEX(un));
6892 6892 save_state = un->un_last_state;
6893 6893 last_power_level = un->un_power_level;
6894 6894 /*
6895 6895 * There must not be any cmds. getting processed
6896 6896 * in the driver when we get here. Power to the
6897 6897 * device is potentially going off.
6898 6898 */
6899 6899 ASSERT(un->un_ncmds_in_driver == 0);
6900 6900 mutex_exit(SD_MUTEX(un));
6901 6901
6902 6902 /*
6903 6903 * For now PM suspend the device completely before spindle is
6904 6904 * turned off
6905 6905 */
6906 6906 if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6907 6907 == DDI_FAILURE) {
6908 6908 if (got_semaphore_here != 0) {
6909 6909 sema_v(&un->un_semoclose);
6910 6910 }
6911 6911 /*
6912 6912 * On exit put the state back to it's original value
6913 6913 * and broadcast to anyone waiting for the power
6914 6914 * change completion.
6915 6915 */
6916 6916 mutex_enter(SD_MUTEX(un));
6917 6917 un->un_state = state_before_pm;
6918 6918 un->un_power_level = last_power_level;
6919 6919 cv_broadcast(&un->un_suspend_cv);
6920 6920 mutex_exit(SD_MUTEX(un));
6921 6921 SD_TRACE(SD_LOG_IO_PM, un,
6922 6922 "sdpower: exit, PM suspend Failed.\n");
6923 6923
6924 6924 goto sdpower_failed;
6925 6925 }
6926 6926 }
6927 6927
6928 6928 /*
6929 6929 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6930 6930 * close, or strategy. Dump no long uses this routine, it uses it's
6931 6931 * own code so it can be done in polled mode.
6932 6932 */
6933 6933
6934 6934 medium_present = TRUE;
6935 6935
6936 6936 /*
6937 6937 * When powering up, issue a TUR in case the device is at unit
6938 6938 * attention. Don't do retries. Bypass the PM layer, otherwise
6939 6939 * a deadlock on un_pm_busy_cv will occur.
6940 6940 */
6941 6941 if (SD_PM_IS_IO_CAPABLE(un, level)) {
6942 6942 sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6943 6943 SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6944 6944 if (sval != 0)
6945 6945 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6946 6946 }
6947 6947
6948 6948 if (un->un_f_power_condition_supported) {
6949 6949 char *pm_condition_name[] = {"STOPPED", "STANDBY",
6950 6950 "IDLE", "ACTIVE"};
6951 6951 SD_TRACE(SD_LOG_IO_PM, un,
6952 6952 "sdpower: sending \'%s\' power condition",
6953 6953 pm_condition_name[level]);
6954 6954 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6955 6955 sd_pl2pc[level], SD_PATH_DIRECT);
6956 6956 } else {
6957 6957 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6958 6958 ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6959 6959 sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6960 6960 ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6961 6961 SD_TARGET_STOP), SD_PATH_DIRECT);
6962 6962 }
6963 6963 if (sval != 0) {
6964 6964 if (sval == EIO)
6965 6965 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6966 6966 else
6967 6967 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6968 6968 }
6969 6969
6970 6970 /* Command failed, check for media present. */
6971 6971 if ((sval == ENXIO) && un->un_f_has_removable_media) {
6972 6972 medium_present = FALSE;
6973 6973 }
6974 6974
6975 6975 /*
6976 6976 * The conditions of interest here are:
6977 6977 * if a spindle off with media present fails,
6978 6978 * then restore the state and return an error.
6979 6979 * else if a spindle on fails,
6980 6980 * then return an error (there's no state to restore).
6981 6981 * In all other cases we setup for the new state
6982 6982 * and return success.
6983 6983 */
6984 6984 if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6985 6985 if ((medium_present == TRUE) && (sval != 0)) {
6986 6986 /* The stop command from above failed */
6987 6987 rval = DDI_FAILURE;
6988 6988 /*
6989 6989 * The stop command failed, and we have media
6990 6990 * present. Put the level back by calling the
6991 6991 * sd_pm_resume() and set the state back to
6992 6992 * it's previous value.
6993 6993 */
6994 6994 (void) sd_pm_state_change(un, last_power_level,
6995 6995 SD_PM_STATE_ROLLBACK);
6996 6996 mutex_enter(SD_MUTEX(un));
6997 6997 un->un_last_state = save_state;
6998 6998 mutex_exit(SD_MUTEX(un));
6999 6999 } else if (un->un_f_monitor_media_state) {
7000 7000 /*
7001 7001 * The stop command from above succeeded.
7002 7002 * Terminate watch thread in case of removable media
7003 7003 * devices going into low power state. This is as per
7004 7004 * the requirements of pm framework, otherwise commands
7005 7005 * will be generated for the device (through watch
7006 7006 * thread), even when the device is in low power state.
7007 7007 */
7008 7008 mutex_enter(SD_MUTEX(un));
7009 7009 un->un_f_watcht_stopped = FALSE;
7010 7010 if (un->un_swr_token != NULL) {
7011 7011 opaque_t temp_token = un->un_swr_token;
7012 7012 un->un_f_watcht_stopped = TRUE;
7013 7013 un->un_swr_token = NULL;
7014 7014 mutex_exit(SD_MUTEX(un));
7015 7015 (void) scsi_watch_request_terminate(temp_token,
7016 7016 SCSI_WATCH_TERMINATE_ALL_WAIT);
7017 7017 } else {
7018 7018 mutex_exit(SD_MUTEX(un));
7019 7019 }
7020 7020 }
7021 7021 } else {
7022 7022 /*
7023 7023 * The level requested is I/O capable.
7024 7024 * Legacy behavior: return success on a failed spinup
7025 7025 * if there is no media in the drive.
7026 7026 * Do this by looking at medium_present here.
7027 7027 */
7028 7028 if ((sval != 0) && medium_present) {
7029 7029 /* The start command from above failed */
7030 7030 rval = DDI_FAILURE;
7031 7031 } else {
7032 7032 /*
7033 7033 * The start command from above succeeded
7034 7034 * PM resume the devices now that we have
7035 7035 * started the disks
7036 7036 */
7037 7037 (void) sd_pm_state_change(un, level,
7038 7038 SD_PM_STATE_CHANGE);
7039 7039
7040 7040 /*
7041 7041 * Resume the watch thread since it was suspended
7042 7042 * when the device went into low power mode.
7043 7043 */
7044 7044 if (un->un_f_monitor_media_state) {
7045 7045 mutex_enter(SD_MUTEX(un));
7046 7046 if (un->un_f_watcht_stopped == TRUE) {
7047 7047 opaque_t temp_token;
7048 7048
7049 7049 un->un_f_watcht_stopped = FALSE;
7050 7050 mutex_exit(SD_MUTEX(un));
7051 7051 temp_token =
7052 7052 sd_watch_request_submit(un);
7053 7053 mutex_enter(SD_MUTEX(un));
7054 7054 un->un_swr_token = temp_token;
7055 7055 }
7056 7056 mutex_exit(SD_MUTEX(un));
7057 7057 }
7058 7058 }
7059 7059 }
7060 7060
7061 7061 if (got_semaphore_here != 0) {
7062 7062 sema_v(&un->un_semoclose);
7063 7063 }
7064 7064 /*
7065 7065 * On exit put the state back to it's original value
7066 7066 * and broadcast to anyone waiting for the power
7067 7067 * change completion.
7068 7068 */
7069 7069 mutex_enter(SD_MUTEX(un));
7070 7070 un->un_state = state_before_pm;
7071 7071 cv_broadcast(&un->un_suspend_cv);
7072 7072 mutex_exit(SD_MUTEX(un));
7073 7073
7074 7074 SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7075 7075
7076 7076 sd_ssc_fini(ssc);
7077 7077 return (rval);
7078 7078
7079 7079 sdpower_failed:
7080 7080
7081 7081 sd_ssc_fini(ssc);
7082 7082 return (DDI_FAILURE);
7083 7083 }
7084 7084
7085 7085
7086 7086
7087 7087 /*
7088 7088 * Function: sdattach
7089 7089 *
7090 7090 * Description: Driver's attach(9e) entry point function.
7091 7091 *
7092 7092 * Arguments: devi - opaque device info handle
7093 7093 * cmd - attach type
7094 7094 *
7095 7095 * Return Code: DDI_SUCCESS
7096 7096 * DDI_FAILURE
7097 7097 *
7098 7098 * Context: Kernel thread context
7099 7099 */
7100 7100
7101 7101 static int
7102 7102 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7103 7103 {
7104 7104 switch (cmd) {
7105 7105 case DDI_ATTACH:
7106 7106 return (sd_unit_attach(devi));
7107 7107 case DDI_RESUME:
7108 7108 return (sd_ddi_resume(devi));
7109 7109 default:
7110 7110 break;
7111 7111 }
7112 7112 return (DDI_FAILURE);
7113 7113 }
7114 7114
7115 7115
7116 7116 /*
7117 7117 * Function: sddetach
7118 7118 *
7119 7119 * Description: Driver's detach(9E) entry point function.
7120 7120 *
7121 7121 * Arguments: devi - opaque device info handle
7122 7122 * cmd - detach type
7123 7123 *
7124 7124 * Return Code: DDI_SUCCESS
7125 7125 * DDI_FAILURE
7126 7126 *
7127 7127 * Context: Kernel thread context
7128 7128 */
7129 7129
7130 7130 static int
7131 7131 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7132 7132 {
7133 7133 switch (cmd) {
7134 7134 case DDI_DETACH:
7135 7135 return (sd_unit_detach(devi));
7136 7136 case DDI_SUSPEND:
7137 7137 return (sd_ddi_suspend(devi));
7138 7138 default:
7139 7139 break;
7140 7140 }
7141 7141 return (DDI_FAILURE);
7142 7142 }
7143 7143
7144 7144
7145 7145 /*
7146 7146 * Function: sd_sync_with_callback
7147 7147 *
7148 7148 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7149 7149 * state while the callback routine is active.
7150 7150 *
7151 7151 * Arguments: un: softstate structure for the instance
7152 7152 *
7153 7153 * Context: Kernel thread context
7154 7154 */
7155 7155
7156 7156 static void
7157 7157 sd_sync_with_callback(struct sd_lun *un)
7158 7158 {
7159 7159 ASSERT(un != NULL);
7160 7160
7161 7161 mutex_enter(SD_MUTEX(un));
7162 7162
7163 7163 ASSERT(un->un_in_callback >= 0);
7164 7164
7165 7165 while (un->un_in_callback > 0) {
7166 7166 mutex_exit(SD_MUTEX(un));
7167 7167 delay(2);
7168 7168 mutex_enter(SD_MUTEX(un));
7169 7169 }
7170 7170
7171 7171 mutex_exit(SD_MUTEX(un));
7172 7172 }
7173 7173
7174 7174 /*
7175 7175 * Function: sd_unit_attach
7176 7176 *
7177 7177 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7178 7178 * the soft state structure for the device and performs
7179 7179 * all necessary structure and device initializations.
7180 7180 *
7181 7181 * Arguments: devi: the system's dev_info_t for the device.
7182 7182 *
7183 7183 * Return Code: DDI_SUCCESS if attach is successful.
7184 7184 * DDI_FAILURE if any part of the attach fails.
7185 7185 *
7186 7186 * Context: Called at attach(9e) time for the DDI_ATTACH flag.
7187 7187 * Kernel thread context only. Can sleep.
7188 7188 */
7189 7189
7190 7190 static int
7191 7191 sd_unit_attach(dev_info_t *devi)
7192 7192 {
7193 7193 struct scsi_device *devp;
7194 7194 struct sd_lun *un;
7195 7195 char *variantp;
7196 7196 char name_str[48];
7197 7197 int reservation_flag = SD_TARGET_IS_UNRESERVED;
7198 7198 int instance;
7199 7199 int rval;
7200 7200 int wc_enabled;
7201 7201 int tgt;
7202 7202 uint64_t capacity;
7203 7203 uint_t lbasize = 0;
7204 7204 dev_info_t *pdip = ddi_get_parent(devi);
7205 7205 int offbyone = 0;
7206 7206 int geom_label_valid = 0;
7207 7207 sd_ssc_t *ssc;
7208 7208 int status;
7209 7209 struct sd_fm_internal *sfip = NULL;
7210 7210 int max_xfer_size;
7211 7211
7212 7212 /*
7213 7213 * Retrieve the target driver's private data area. This was set
7214 7214 * up by the HBA.
7215 7215 */
7216 7216 devp = ddi_get_driver_private(devi);
7217 7217
7218 7218 /*
7219 7219 * Retrieve the target ID of the device.
7220 7220 */
7221 7221 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7222 7222 SCSI_ADDR_PROP_TARGET, -1);
7223 7223
7224 7224 /*
7225 7225 * Since we have no idea what state things were left in by the last
7226 7226 * user of the device, set up some 'default' settings, ie. turn 'em
7227 7227 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7228 7228 * Do this before the scsi_probe, which sends an inquiry.
7229 7229 * This is a fix for bug (4430280).
7230 7230 * Of special importance is wide-xfer. The drive could have been left
7231 7231 * in wide transfer mode by the last driver to communicate with it,
7232 7232 * this includes us. If that's the case, and if the following is not
7233 7233 * setup properly or we don't re-negotiate with the drive prior to
7234 7234 * transferring data to/from the drive, it causes bus parity errors,
7235 7235 * data overruns, and unexpected interrupts. This first occurred when
7236 7236 * the fix for bug (4378686) was made.
7237 7237 */
7238 7238 (void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7239 7239 (void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7240 7240 (void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7241 7241
7242 7242 /*
7243 7243 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7244 7244 * on a target. Setting it per lun instance actually sets the
7245 7245 * capability of this target, which affects those luns already
7246 7246 * attached on the same target. So during attach, we can only disable
7247 7247 * this capability only when no other lun has been attached on this
7248 7248 * target. By doing this, we assume a target has the same tagged-qing
7249 7249 * capability for every lun. The condition can be removed when HBA
7250 7250 * is changed to support per lun based tagged-qing capability.
7251 7251 */
7252 7252 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7253 7253 (void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7254 7254 }
7255 7255
7256 7256 /*
7257 7257 * Use scsi_probe() to issue an INQUIRY command to the device.
7258 7258 * This call will allocate and fill in the scsi_inquiry structure
7259 7259 * and point the sd_inq member of the scsi_device structure to it.
7260 7260 * If the attach succeeds, then this memory will not be de-allocated
7261 7261 * (via scsi_unprobe()) until the instance is detached.
7262 7262 */
7263 7263 if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7264 7264 goto probe_failed;
7265 7265 }
7266 7266
7267 7267 /*
7268 7268 * Check the device type as specified in the inquiry data and
7269 7269 * claim it if it is of a type that we support.
7270 7270 */
7271 7271 switch (devp->sd_inq->inq_dtype) {
7272 7272 case DTYPE_DIRECT:
7273 7273 break;
7274 7274 case DTYPE_RODIRECT:
7275 7275 break;
7276 7276 case DTYPE_OPTICAL:
7277 7277 break;
7278 7278 case DTYPE_NOTPRESENT:
7279 7279 default:
7280 7280 /* Unsupported device type; fail the attach. */
7281 7281 goto probe_failed;
7282 7282 }
7283 7283
7284 7284 /*
7285 7285 * Allocate the soft state structure for this unit.
7286 7286 *
7287 7287 * We rely upon this memory being set to all zeroes by
7288 7288 * ddi_soft_state_zalloc(). We assume that any member of the
7289 7289 * soft state structure that is not explicitly initialized by
7290 7290 * this routine will have a value of zero.
7291 7291 */
7292 7292 instance = ddi_get_instance(devp->sd_dev);
7293 7293 #ifndef XPV_HVM_DRIVER
7294 7294 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7295 7295 goto probe_failed;
7296 7296 }
7297 7297 #endif /* !XPV_HVM_DRIVER */
7298 7298
7299 7299 /*
7300 7300 * Retrieve a pointer to the newly-allocated soft state.
7301 7301 *
7302 7302 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7303 7303 * was successful, unless something has gone horribly wrong and the
7304 7304 * ddi's soft state internals are corrupt (in which case it is
7305 7305 * probably better to halt here than just fail the attach....)
7306 7306 */
7307 7307 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7308 7308 panic("sd_unit_attach: NULL soft state on instance:0x%x",
7309 7309 instance);
7310 7310 /*NOTREACHED*/
7311 7311 }
7312 7312
7313 7313 /*
7314 7314 * Link the back ptr of the driver soft state to the scsi_device
7315 7315 * struct for this lun.
7316 7316 * Save a pointer to the softstate in the driver-private area of
7317 7317 * the scsi_device struct.
7318 7318 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7319 7319 * we first set un->un_sd below.
7320 7320 */
7321 7321 un->un_sd = devp;
7322 7322 devp->sd_private = (opaque_t)un;
7323 7323
7324 7324 /*
7325 7325 * The following must be after devp is stored in the soft state struct.
7326 7326 */
7327 7327 #ifdef SDDEBUG
7328 7328 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7329 7329 "%s_unit_attach: un:0x%p instance:%d\n",
7330 7330 ddi_driver_name(devi), un, instance);
7331 7331 #endif
7332 7332
7333 7333 /*
7334 7334 * Set up the device type and node type (for the minor nodes).
7335 7335 * By default we assume that the device can at least support the
7336 7336 * Common Command Set. Call it a CD-ROM if it reports itself
7337 7337 * as a RODIRECT device.
7338 7338 */
7339 7339 switch (devp->sd_inq->inq_dtype) {
7340 7340 case DTYPE_RODIRECT:
7341 7341 un->un_node_type = DDI_NT_CD_CHAN;
7342 7342 un->un_ctype = CTYPE_CDROM;
7343 7343 break;
7344 7344 case DTYPE_OPTICAL:
7345 7345 un->un_node_type = DDI_NT_BLOCK_CHAN;
7346 7346 un->un_ctype = CTYPE_ROD;
7347 7347 break;
7348 7348 default:
7349 7349 un->un_node_type = DDI_NT_BLOCK_CHAN;
7350 7350 un->un_ctype = CTYPE_CCS;
7351 7351 break;
7352 7352 }
7353 7353
7354 7354 /*
7355 7355 * Try to read the interconnect type from the HBA.
7356 7356 *
7357 7357 * Note: This driver is currently compiled as two binaries, a parallel
7358 7358 * scsi version (sd) and a fibre channel version (ssd). All functional
7359 7359 * differences are determined at compile time. In the future a single
7360 7360 * binary will be provided and the interconnect type will be used to
7361 7361 * differentiate between fibre and parallel scsi behaviors. At that time
7362 7362 * it will be necessary for all fibre channel HBAs to support this
7363 7363 * property.
7364 7364 *
7365 7365 * set un_f_is_fiber to TRUE ( default fiber )
7366 7366 */
7367 7367 un->un_f_is_fibre = TRUE;
7368 7368 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7369 7369 case INTERCONNECT_SSA:
7370 7370 un->un_interconnect_type = SD_INTERCONNECT_SSA;
7371 7371 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7372 7372 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7373 7373 break;
7374 7374 case INTERCONNECT_PARALLEL:
7375 7375 un->un_f_is_fibre = FALSE;
7376 7376 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7377 7377 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7378 7378 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7379 7379 break;
7380 7380 case INTERCONNECT_SAS:
7381 7381 un->un_f_is_fibre = FALSE;
7382 7382 un->un_interconnect_type = SD_INTERCONNECT_SAS;
7383 7383 un->un_node_type = DDI_NT_BLOCK_SAS;
7384 7384 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7385 7385 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7386 7386 break;
7387 7387 case INTERCONNECT_SATA:
7388 7388 un->un_f_is_fibre = FALSE;
7389 7389 un->un_interconnect_type = SD_INTERCONNECT_SATA;
7390 7390 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7391 7391 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7392 7392 break;
7393 7393 case INTERCONNECT_FIBRE:
7394 7394 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7395 7395 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7396 7396 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7397 7397 break;
7398 7398 case INTERCONNECT_FABRIC:
7399 7399 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7400 7400 un->un_node_type = DDI_NT_BLOCK_FABRIC;
7401 7401 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7402 7402 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7403 7403 break;
7404 7404 default:
7405 7405 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7406 7406 /*
7407 7407 * The HBA does not support the "interconnect-type" property
7408 7408 * (or did not provide a recognized type).
7409 7409 *
7410 7410 * Note: This will be obsoleted when a single fibre channel
7411 7411 * and parallel scsi driver is delivered. In the meantime the
7412 7412 * interconnect type will be set to the platform default.If that
7413 7413 * type is not parallel SCSI, it means that we should be
7414 7414 * assuming "ssd" semantics. However, here this also means that
7415 7415 * the FC HBA is not supporting the "interconnect-type" property
7416 7416 * like we expect it to, so log this occurrence.
7417 7417 */
7418 7418 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7419 7419 if (!SD_IS_PARALLEL_SCSI(un)) {
7420 7420 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7421 7421 "sd_unit_attach: un:0x%p Assuming "
7422 7422 "INTERCONNECT_FIBRE\n", un);
7423 7423 } else {
7424 7424 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7425 7425 "sd_unit_attach: un:0x%p Assuming "
7426 7426 "INTERCONNECT_PARALLEL\n", un);
7427 7427 un->un_f_is_fibre = FALSE;
7428 7428 }
7429 7429 #else
7430 7430 /*
7431 7431 * Note: This source will be implemented when a single fibre
7432 7432 * channel and parallel scsi driver is delivered. The default
7433 7433 * will be to assume that if a device does not support the
7434 7434 * "interconnect-type" property it is a parallel SCSI HBA and
7435 7435 * we will set the interconnect type for parallel scsi.
7436 7436 */
7437 7437 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7438 7438 un->un_f_is_fibre = FALSE;
7439 7439 #endif
7440 7440 break;
7441 7441 }
7442 7442
7443 7443 if (un->un_f_is_fibre == TRUE) {
7444 7444 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7445 7445 SCSI_VERSION_3) {
7446 7446 switch (un->un_interconnect_type) {
7447 7447 case SD_INTERCONNECT_FIBRE:
7448 7448 case SD_INTERCONNECT_SSA:
7449 7449 un->un_node_type = DDI_NT_BLOCK_WWN;
7450 7450 break;
7451 7451 default:
7452 7452 break;
7453 7453 }
7454 7454 }
7455 7455 }
7456 7456
7457 7457 /*
7458 7458 * Initialize the Request Sense command for the target
7459 7459 */
7460 7460 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7461 7461 goto alloc_rqs_failed;
7462 7462 }
7463 7463
7464 7464 /*
7465 7465 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7466 7466 * with separate binary for sd and ssd.
7467 7467 *
7468 7468 * x86 has 1 binary, un_retry_count is set base on connection type.
7469 7469 * The hardcoded values will go away when Sparc uses 1 binary
7470 7470 * for sd and ssd. This hardcoded values need to match
7471 7471 * SD_RETRY_COUNT in sddef.h
7472 7472 * The value used is base on interconnect type.
7473 7473 * fibre = 3, parallel = 5
7474 7474 */
7475 7475 #if defined(__i386) || defined(__amd64)
7476 7476 un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7477 7477 #else
7478 7478 un->un_retry_count = SD_RETRY_COUNT;
7479 7479 #endif
7480 7480
7481 7481 /*
7482 7482 * Set the per disk retry count to the default number of retries
7483 7483 * for disks and CDROMs. This value can be overridden by the
7484 7484 * disk property list or an entry in sd.conf.
7485 7485 */
7486 7486 un->un_notready_retry_count =
7487 7487 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7488 7488 : DISK_NOT_READY_RETRY_COUNT(un);
7489 7489
7490 7490 /*
7491 7491 * Set the busy retry count to the default value of un_retry_count.
7492 7492 * This can be overridden by entries in sd.conf or the device
7493 7493 * config table.
7494 7494 */
7495 7495 un->un_busy_retry_count = un->un_retry_count;
7496 7496
7497 7497 /*
7498 7498 * Init the reset threshold for retries. This number determines
7499 7499 * how many retries must be performed before a reset can be issued
7500 7500 * (for certain error conditions). This can be overridden by entries
7501 7501 * in sd.conf or the device config table.
7502 7502 */
7503 7503 un->un_reset_retry_count = (un->un_retry_count / 2);
7504 7504
7505 7505 /*
7506 7506 * Set the victim_retry_count to the default un_retry_count
7507 7507 */
7508 7508 un->un_victim_retry_count = (2 * un->un_retry_count);
7509 7509
7510 7510 /*
7511 7511 * Set the reservation release timeout to the default value of
7512 7512 * 5 seconds. This can be overridden by entries in ssd.conf or the
7513 7513 * device config table.
7514 7514 */
7515 7515 un->un_reserve_release_time = 5;
7516 7516
7517 7517 /*
7518 7518 * Set up the default maximum transfer size. Note that this may
7519 7519 * get updated later in the attach, when setting up default wide
7520 7520 * operations for disks.
7521 7521 */
7522 7522 #if defined(__i386) || defined(__amd64)
7523 7523 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7524 7524 un->un_partial_dma_supported = 1;
7525 7525 #else
7526 7526 un->un_max_xfer_size = (uint_t)maxphys;
7527 7527 #endif
7528 7528
7529 7529 /*
7530 7530 * Get "allow bus device reset" property (defaults to "enabled" if
7531 7531 * the property was not defined). This is to disable bus resets for
7532 7532 * certain kinds of error recovery. Note: In the future when a run-time
7533 7533 * fibre check is available the soft state flag should default to
7534 7534 * enabled.
7535 7535 */
7536 7536 if (un->un_f_is_fibre == TRUE) {
7537 7537 un->un_f_allow_bus_device_reset = TRUE;
7538 7538 } else {
7539 7539 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7540 7540 "allow-bus-device-reset", 1) != 0) {
7541 7541 un->un_f_allow_bus_device_reset = TRUE;
7542 7542 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7543 7543 "sd_unit_attach: un:0x%p Bus device reset "
7544 7544 "enabled\n", un);
7545 7545 } else {
7546 7546 un->un_f_allow_bus_device_reset = FALSE;
7547 7547 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7548 7548 "sd_unit_attach: un:0x%p Bus device reset "
7549 7549 "disabled\n", un);
7550 7550 }
7551 7551 }
7552 7552
7553 7553 /*
7554 7554 * Check if this is an ATAPI device. ATAPI devices use Group 1
7555 7555 * Read/Write commands and Group 2 Mode Sense/Select commands.
7556 7556 *
7557 7557 * Note: The "obsolete" way of doing this is to check for the "atapi"
7558 7558 * property. The new "variant" property with a value of "atapi" has been
7559 7559 * introduced so that future 'variants' of standard SCSI behavior (like
7560 7560 * atapi) could be specified by the underlying HBA drivers by supplying
7561 7561 * a new value for the "variant" property, instead of having to define a
7562 7562 * new property.
7563 7563 */
7564 7564 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7565 7565 un->un_f_cfg_is_atapi = TRUE;
7566 7566 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7567 7567 "sd_unit_attach: un:0x%p Atapi device\n", un);
7568 7568 }
7569 7569 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7570 7570 &variantp) == DDI_PROP_SUCCESS) {
7571 7571 if (strcmp(variantp, "atapi") == 0) {
7572 7572 un->un_f_cfg_is_atapi = TRUE;
7573 7573 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7574 7574 "sd_unit_attach: un:0x%p Atapi device\n", un);
7575 7575 }
7576 7576 ddi_prop_free(variantp);
7577 7577 }
7578 7578
7579 7579 un->un_cmd_timeout = SD_IO_TIME;
7580 7580
7581 7581 un->un_busy_timeout = SD_BSY_TIMEOUT;
7582 7582
7583 7583 /* Info on current states, statuses, etc. (Updated frequently) */
7584 7584 un->un_state = SD_STATE_NORMAL;
7585 7585 un->un_last_state = SD_STATE_NORMAL;
7586 7586
7587 7587 /* Control & status info for command throttling */
7588 7588 un->un_throttle = sd_max_throttle;
7589 7589 un->un_saved_throttle = sd_max_throttle;
7590 7590 un->un_min_throttle = sd_min_throttle;
7591 7591
7592 7592 if (un->un_f_is_fibre == TRUE) {
7593 7593 un->un_f_use_adaptive_throttle = TRUE;
7594 7594 } else {
7595 7595 un->un_f_use_adaptive_throttle = FALSE;
7596 7596 }
7597 7597
7598 7598 /* Removable media support. */
7599 7599 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7600 7600 un->un_mediastate = DKIO_NONE;
7601 7601 un->un_specified_mediastate = DKIO_NONE;
7602 7602
7603 7603 /* CVs for suspend/resume (PM or DR) */
7604 7604 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
7605 7605 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7606 7606
7607 7607 /* Power management support. */
7608 7608 un->un_power_level = SD_SPINDLE_UNINIT;
7609 7609
7610 7610 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL);
7611 7611 un->un_f_wcc_inprog = 0;
7612 7612
7613 7613 /*
7614 7614 * The open/close semaphore is used to serialize threads executing
7615 7615 * in the driver's open & close entry point routines for a given
7616 7616 * instance.
7617 7617 */
7618 7618 (void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7619 7619
7620 7620 /*
7621 7621 * The conf file entry and softstate variable is a forceful override,
7622 7622 * meaning a non-zero value must be entered to change the default.
7623 7623 */
7624 7624 un->un_f_disksort_disabled = FALSE;
7625 7625 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7626 7626 un->un_f_enable_rmw = FALSE;
7627 7627
7628 7628 /*
7629 7629 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7630 7630 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7631 7631 */
7632 7632 un->un_f_mmc_gesn_polling = TRUE;
7633 7633
7634 7634 /*
7635 7635 * physical sector size defaults to DEV_BSIZE currently. We can
7636 7636 * override this value via the driver configuration file so we must
7637 7637 * set it before calling sd_read_unit_properties().
7638 7638 */
7639 7639 un->un_phy_blocksize = DEV_BSIZE;
7640 7640
7641 7641 /*
7642 7642 * Retrieve the properties from the static driver table or the driver
7643 7643 * configuration file (.conf) for this unit and update the soft state
7644 7644 * for the device as needed for the indicated properties.
7645 7645 * Note: the property configuration needs to occur here as some of the
7646 7646 * following routines may have dependencies on soft state flags set
7647 7647 * as part of the driver property configuration.
7648 7648 */
7649 7649 sd_read_unit_properties(un);
7650 7650 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7651 7651 "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7652 7652
7653 7653 /*
7654 7654 * Only if a device has "hotpluggable" property, it is
7655 7655 * treated as hotpluggable device. Otherwise, it is
7656 7656 * regarded as non-hotpluggable one.
7657 7657 */
7658 7658 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7659 7659 -1) != -1) {
7660 7660 un->un_f_is_hotpluggable = TRUE;
7661 7661 }
7662 7662
7663 7663 /*
7664 7664 * set unit's attributes(flags) according to "hotpluggable" and
7665 7665 * RMB bit in INQUIRY data.
7666 7666 */
7667 7667 sd_set_unit_attributes(un, devi);
7668 7668
7669 7669 /*
7670 7670 * By default, we mark the capacity, lbasize, and geometry
7671 7671 * as invalid. Only if we successfully read a valid capacity
7672 7672 * will we update the un_blockcount and un_tgt_blocksize with the
7673 7673 * valid values (the geometry will be validated later).
7674 7674 */
7675 7675 un->un_f_blockcount_is_valid = FALSE;
7676 7676 un->un_f_tgt_blocksize_is_valid = FALSE;
7677 7677
7678 7678 /*
7679 7679 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7680 7680 * otherwise.
7681 7681 */
7682 7682 un->un_tgt_blocksize = un->un_sys_blocksize = DEV_BSIZE;
7683 7683 un->un_blockcount = 0;
7684 7684
7685 7685 /*
7686 7686 * Set up the per-instance info needed to determine the correct
7687 7687 * CDBs and other info for issuing commands to the target.
7688 7688 */
7689 7689 sd_init_cdb_limits(un);
7690 7690
7691 7691 /*
7692 7692 * Set up the IO chains to use, based upon the target type.
7693 7693 */
7694 7694 if (un->un_f_non_devbsize_supported) {
7695 7695 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7696 7696 } else {
7697 7697 un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7698 7698 }
7699 7699 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
7700 7700 un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7701 7701 un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7702 7702
7703 7703 un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7704 7704 sd_xbuf_strategy, un, sd_xbuf_active_limit, sd_xbuf_reserve_limit,
7705 7705 ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7706 7706 ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7707 7707
7708 7708
7709 7709 if (ISCD(un)) {
7710 7710 un->un_additional_codes = sd_additional_codes;
7711 7711 } else {
7712 7712 un->un_additional_codes = NULL;
7713 7713 }
7714 7714
7715 7715 /*
7716 7716 * Create the kstats here so they can be available for attach-time
7717 7717 * routines that send commands to the unit (either polled or via
7718 7718 * sd_send_scsi_cmd).
7719 7719 *
7720 7720 * Note: This is a critical sequence that needs to be maintained:
7721 7721 * 1) Instantiate the kstats here, before any routines using the
7722 7722 * iopath (i.e. sd_send_scsi_cmd).
7723 7723 * 2) Instantiate and initialize the partition stats
7724 7724 * (sd_set_pstats).
7725 7725 * 3) Initialize the error stats (sd_set_errstats), following
7726 7726 * sd_validate_geometry(),sd_register_devid(),
7727 7727 * and sd_cache_control().
7728 7728 */
7729 7729
7730 7730 un->un_stats = kstat_create(sd_label, instance,
7731 7731 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7732 7732 if (un->un_stats != NULL) {
7733 7733 un->un_stats->ks_lock = SD_MUTEX(un);
7734 7734 kstat_install(un->un_stats);
7735 7735 }
7736 7736 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7737 7737 "sd_unit_attach: un:0x%p un_stats created\n", un);
7738 7738
7739 7739 sd_create_errstats(un, instance);
7740 7740 if (un->un_errstats == NULL) {
7741 7741 goto create_errstats_failed;
7742 7742 }
7743 7743 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7744 7744 "sd_unit_attach: un:0x%p errstats created\n", un);
7745 7745
7746 7746 /*
7747 7747 * The following if/else code was relocated here from below as part
7748 7748 * of the fix for bug (4430280). However with the default setup added
7749 7749 * on entry to this routine, it's no longer absolutely necessary for
7750 7750 * this to be before the call to sd_spin_up_unit.
7751 7751 */
7752 7752 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7753 7753 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7754 7754 (devp->sd_inq->inq_ansi == 5)) &&
7755 7755 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7756 7756
7757 7757 /*
7758 7758 * If tagged queueing is supported by the target
7759 7759 * and by the host adapter then we will enable it
7760 7760 */
7761 7761 un->un_tagflags = 0;
7762 7762 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7763 7763 (un->un_f_arq_enabled == TRUE)) {
7764 7764 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7765 7765 1, 1) == 1) {
7766 7766 un->un_tagflags = FLAG_STAG;
7767 7767 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7768 7768 "sd_unit_attach: un:0x%p tag queueing "
7769 7769 "enabled\n", un);
7770 7770 } else if (scsi_ifgetcap(SD_ADDRESS(un),
7771 7771 "untagged-qing", 0) == 1) {
7772 7772 un->un_f_opt_queueing = TRUE;
7773 7773 un->un_saved_throttle = un->un_throttle =
7774 7774 min(un->un_throttle, 3);
7775 7775 } else {
7776 7776 un->un_f_opt_queueing = FALSE;
7777 7777 un->un_saved_throttle = un->un_throttle = 1;
7778 7778 }
7779 7779 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7780 7780 == 1) && (un->un_f_arq_enabled == TRUE)) {
7781 7781 /* The Host Adapter supports internal queueing. */
7782 7782 un->un_f_opt_queueing = TRUE;
7783 7783 un->un_saved_throttle = un->un_throttle =
7784 7784 min(un->un_throttle, 3);
7785 7785 } else {
7786 7786 un->un_f_opt_queueing = FALSE;
7787 7787 un->un_saved_throttle = un->un_throttle = 1;
7788 7788 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7789 7789 "sd_unit_attach: un:0x%p no tag queueing\n", un);
7790 7790 }
7791 7791
7792 7792 /*
7793 7793 * Enable large transfers for SATA/SAS drives
7794 7794 */
7795 7795 if (SD_IS_SERIAL(un)) {
7796 7796 un->un_max_xfer_size =
7797 7797 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7798 7798 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7799 7799 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7800 7800 "sd_unit_attach: un:0x%p max transfer "
7801 7801 "size=0x%x\n", un, un->un_max_xfer_size);
7802 7802
7803 7803 }
7804 7804
7805 7805 /* Setup or tear down default wide operations for disks */
7806 7806
7807 7807 /*
7808 7808 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7809 7809 * and "ssd_max_xfer_size" to exist simultaneously on the same
7810 7810 * system and be set to different values. In the future this
7811 7811 * code may need to be updated when the ssd module is
7812 7812 * obsoleted and removed from the system. (4299588)
7813 7813 */
7814 7814 if (SD_IS_PARALLEL_SCSI(un) &&
7815 7815 (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7816 7816 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7817 7817 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7818 7818 1, 1) == 1) {
7819 7819 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7820 7820 "sd_unit_attach: un:0x%p Wide Transfer "
7821 7821 "enabled\n", un);
7822 7822 }
7823 7823
7824 7824 /*
7825 7825 * If tagged queuing has also been enabled, then
7826 7826 * enable large xfers
7827 7827 */
7828 7828 if (un->un_saved_throttle == sd_max_throttle) {
7829 7829 un->un_max_xfer_size =
7830 7830 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7831 7831 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7832 7832 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7833 7833 "sd_unit_attach: un:0x%p max transfer "
7834 7834 "size=0x%x\n", un, un->un_max_xfer_size);
7835 7835 }
7836 7836 } else {
7837 7837 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7838 7838 0, 1) == 1) {
7839 7839 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7840 7840 "sd_unit_attach: un:0x%p "
7841 7841 "Wide Transfer disabled\n", un);
7842 7842 }
7843 7843 }
7844 7844 } else {
7845 7845 un->un_tagflags = FLAG_STAG;
7846 7846 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7847 7847 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7848 7848 }
7849 7849
7850 7850 /*
7851 7851 * If this target supports LUN reset, try to enable it.
7852 7852 */
7853 7853 if (un->un_f_lun_reset_enabled) {
7854 7854 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7855 7855 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7856 7856 "un:0x%p lun_reset capability set\n", un);
7857 7857 } else {
7858 7858 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7859 7859 "un:0x%p lun-reset capability not set\n", un);
7860 7860 }
7861 7861 }
7862 7862
7863 7863 /*
7864 7864 * Adjust the maximum transfer size. This is to fix
7865 7865 * the problem of partial DMA support on SPARC. Some
7866 7866 * HBA driver, like aac, has very small dma_attr_maxxfer
7867 7867 * size, which requires partial DMA support on SPARC.
7868 7868 * In the future the SPARC pci nexus driver may solve
7869 7869 * the problem instead of this fix.
7870 7870 */
7871 7871 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7872 7872 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7873 7873 /* We need DMA partial even on sparc to ensure sddump() works */
7874 7874 un->un_max_xfer_size = max_xfer_size;
7875 7875 if (un->un_partial_dma_supported == 0)
7876 7876 un->un_partial_dma_supported = 1;
7877 7877 }
7878 7878 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7879 7879 DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7880 7880 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7881 7881 un->un_max_xfer_size) == 1) {
7882 7882 un->un_buf_breakup_supported = 1;
7883 7883 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7884 7884 "un:0x%p Buf breakup enabled\n", un);
7885 7885 }
7886 7886 }
7887 7887
7888 7888 /*
7889 7889 * Set PKT_DMA_PARTIAL flag.
7890 7890 */
7891 7891 if (un->un_partial_dma_supported == 1) {
7892 7892 un->un_pkt_flags = PKT_DMA_PARTIAL;
7893 7893 } else {
7894 7894 un->un_pkt_flags = 0;
7895 7895 }
7896 7896
7897 7897 /* Initialize sd_ssc_t for internal uscsi commands */
7898 7898 ssc = sd_ssc_init(un);
7899 7899 scsi_fm_init(devp);
7900 7900
7901 7901 /*
7902 7902 * Allocate memory for SCSI FMA stuffs.
7903 7903 */
7904 7904 un->un_fm_private =
7905 7905 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7906 7906 sfip = (struct sd_fm_internal *)un->un_fm_private;
7907 7907 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7908 7908 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7909 7909 sfip->fm_ssc.ssc_un = un;
7910 7910
7911 7911 if (ISCD(un) ||
7912 7912 un->un_f_has_removable_media ||
7913 7913 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7914 7914 /*
7915 7915 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7916 7916 * Their log are unchanged.
7917 7917 */
7918 7918 sfip->fm_log_level = SD_FM_LOG_NSUP;
7919 7919 } else {
7920 7920 /*
7921 7921 * If enter here, it should be non-CDROM and FM-capable
7922 7922 * device, and it will not keep the old scsi_log as before
7923 7923 * in /var/adm/messages. However, the property
7924 7924 * "fm-scsi-log" will control whether the FM telemetry will
7925 7925 * be logged in /var/adm/messages.
7926 7926 */
7927 7927 int fm_scsi_log;
7928 7928 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7929 7929 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7930 7930
7931 7931 if (fm_scsi_log)
7932 7932 sfip->fm_log_level = SD_FM_LOG_EREPORT;
7933 7933 else
7934 7934 sfip->fm_log_level = SD_FM_LOG_SILENT;
7935 7935 }
7936 7936
7937 7937 /*
7938 7938 * At this point in the attach, we have enough info in the
7939 7939 * soft state to be able to issue commands to the target.
7940 7940 *
7941 7941 * All command paths used below MUST issue their commands as
7942 7942 * SD_PATH_DIRECT. This is important as intermediate layers
7943 7943 * are not all initialized yet (such as PM).
7944 7944 */
7945 7945
7946 7946 /*
7947 7947 * Send a TEST UNIT READY command to the device. This should clear
7948 7948 * any outstanding UNIT ATTENTION that may be present.
7949 7949 *
7950 7950 * Note: Don't check for success, just track if there is a reservation,
7951 7951 * this is a throw away command to clear any unit attentions.
7952 7952 *
7953 7953 * Note: This MUST be the first command issued to the target during
7954 7954 * attach to ensure power on UNIT ATTENTIONS are cleared.
7955 7955 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7956 7956 * with attempts at spinning up a device with no media.
7957 7957 */
7958 7958 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7959 7959 if (status != 0) {
7960 7960 if (status == EACCES)
7961 7961 reservation_flag = SD_TARGET_IS_RESERVED;
7962 7962 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7963 7963 }
7964 7964
7965 7965 /*
7966 7966 * If the device is NOT a removable media device, attempt to spin
7967 7967 * it up (using the START_STOP_UNIT command) and read its capacity
7968 7968 * (using the READ CAPACITY command). Note, however, that either
7969 7969 * of these could fail and in some cases we would continue with
7970 7970 * the attach despite the failure (see below).
7971 7971 */
7972 7972 if (un->un_f_descr_format_supported) {
7973 7973
7974 7974 switch (sd_spin_up_unit(ssc)) {
7975 7975 case 0:
7976 7976 /*
7977 7977 * Spin-up was successful; now try to read the
7978 7978 * capacity. If successful then save the results
7979 7979 * and mark the capacity & lbasize as valid.
7980 7980 */
7981 7981 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7982 7982 "sd_unit_attach: un:0x%p spin-up successful\n", un);
7983 7983
7984 7984 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7985 7985 &lbasize, SD_PATH_DIRECT);
7986 7986
7987 7987 switch (status) {
7988 7988 case 0: {
7989 7989 if (capacity > DK_MAX_BLOCKS) {
7990 7990 #ifdef _LP64
7991 7991 if ((capacity + 1) >
7992 7992 SD_GROUP1_MAX_ADDRESS) {
7993 7993 /*
7994 7994 * Enable descriptor format
7995 7995 * sense data so that we can
7996 7996 * get 64 bit sense data
7997 7997 * fields.
7998 7998 */
7999 7999 sd_enable_descr_sense(ssc);
8000 8000 }
8001 8001 #else
8002 8002 /* 32-bit kernels can't handle this */
8003 8003 scsi_log(SD_DEVINFO(un),
8004 8004 sd_label, CE_WARN,
8005 8005 "disk has %llu blocks, which "
8006 8006 "is too large for a 32-bit "
8007 8007 "kernel", capacity);
8008 8008
8009 8009 #if defined(__i386) || defined(__amd64)
8010 8010 /*
8011 8011 * 1TB disk was treated as (1T - 512)B
8012 8012 * in the past, so that it might have
8013 8013 * valid VTOC and solaris partitions,
8014 8014 * we have to allow it to continue to
8015 8015 * work.
8016 8016 */
8017 8017 if (capacity -1 > DK_MAX_BLOCKS)
8018 8018 #endif
8019 8019 goto spinup_failed;
8020 8020 #endif
8021 8021 }
8022 8022
8023 8023 /*
8024 8024 * Here it's not necessary to check the case:
8025 8025 * the capacity of the device is bigger than
8026 8026 * what the max hba cdb can support. Because
8027 8027 * sd_send_scsi_READ_CAPACITY will retrieve
8028 8028 * the capacity by sending USCSI command, which
8029 8029 * is constrained by the max hba cdb. Actually,
8030 8030 * sd_send_scsi_READ_CAPACITY will return
8031 8031 * EINVAL when using bigger cdb than required
8032 8032 * cdb length. Will handle this case in
8033 8033 * "case EINVAL".
8034 8034 */
8035 8035
8036 8036 /*
8037 8037 * The following relies on
8038 8038 * sd_send_scsi_READ_CAPACITY never
8039 8039 * returning 0 for capacity and/or lbasize.
8040 8040 */
8041 8041 sd_update_block_info(un, lbasize, capacity);
8042 8042
8043 8043 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8044 8044 "sd_unit_attach: un:0x%p capacity = %ld "
8045 8045 "blocks; lbasize= %ld.\n", un,
8046 8046 un->un_blockcount, un->un_tgt_blocksize);
8047 8047
8048 8048 break;
8049 8049 }
8050 8050 case EINVAL:
8051 8051 /*
8052 8052 * In the case where the max-cdb-length property
8053 8053 * is smaller than the required CDB length for
8054 8054 * a SCSI device, a target driver can fail to
8055 8055 * attach to that device.
8056 8056 */
8057 8057 scsi_log(SD_DEVINFO(un),
8058 8058 sd_label, CE_WARN,
8059 8059 "disk capacity is too large "
8060 8060 "for current cdb length");
8061 8061 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8062 8062
8063 8063 goto spinup_failed;
8064 8064 case EACCES:
8065 8065 /*
8066 8066 * Should never get here if the spin-up
8067 8067 * succeeded, but code it in anyway.
8068 8068 * From here, just continue with the attach...
8069 8069 */
8070 8070 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8071 8071 "sd_unit_attach: un:0x%p "
8072 8072 "sd_send_scsi_READ_CAPACITY "
8073 8073 "returned reservation conflict\n", un);
8074 8074 reservation_flag = SD_TARGET_IS_RESERVED;
8075 8075 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8076 8076 break;
8077 8077 default:
8078 8078 /*
8079 8079 * Likewise, should never get here if the
8080 8080 * spin-up succeeded. Just continue with
8081 8081 * the attach...
8082 8082 */
8083 8083 if (status == EIO)
8084 8084 sd_ssc_assessment(ssc,
8085 8085 SD_FMT_STATUS_CHECK);
8086 8086 else
8087 8087 sd_ssc_assessment(ssc,
8088 8088 SD_FMT_IGNORE);
8089 8089 break;
8090 8090 }
8091 8091 break;
8092 8092 case EACCES:
8093 8093 /*
8094 8094 * Device is reserved by another host. In this case
8095 8095 * we could not spin it up or read the capacity, but
8096 8096 * we continue with the attach anyway.
8097 8097 */
8098 8098 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8099 8099 "sd_unit_attach: un:0x%p spin-up reservation "
8100 8100 "conflict.\n", un);
8101 8101 reservation_flag = SD_TARGET_IS_RESERVED;
8102 8102 break;
8103 8103 default:
8104 8104 /* Fail the attach if the spin-up failed. */
8105 8105 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8106 8106 "sd_unit_attach: un:0x%p spin-up failed.", un);
8107 8107 goto spinup_failed;
8108 8108 }
8109 8109
8110 8110 }
8111 8111
8112 8112 /*
8113 8113 * Check to see if this is a MMC drive
8114 8114 */
8115 8115 if (ISCD(un)) {
8116 8116 sd_set_mmc_caps(ssc);
8117 8117 }
8118 8118
8119 8119 /*
8120 8120 * Add a zero-length attribute to tell the world we support
8121 8121 * kernel ioctls (for layered drivers)
8122 8122 */
8123 8123 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8124 8124 DDI_KERNEL_IOCTL, NULL, 0);
8125 8125
8126 8126 /*
8127 8127 * Add a boolean property to tell the world we support
8128 8128 * the B_FAILFAST flag (for layered drivers)
8129 8129 */
8130 8130 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8131 8131 "ddi-failfast-supported", NULL, 0);
8132 8132
8133 8133 /*
8134 8134 * Initialize power management
8135 8135 */
8136 8136 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8137 8137 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8138 8138 sd_setup_pm(ssc, devi);
8139 8139 if (un->un_f_pm_is_enabled == FALSE) {
8140 8140 /*
8141 8141 * For performance, point to a jump table that does
8142 8142 * not include pm.
8143 8143 * The direct and priority chains don't change with PM.
8144 8144 *
8145 8145 * Note: this is currently done based on individual device
8146 8146 * capabilities. When an interface for determining system
8147 8147 * power enabled state becomes available, or when additional
8148 8148 * layers are added to the command chain, these values will
8149 8149 * have to be re-evaluated for correctness.
8150 8150 */
8151 8151 if (un->un_f_non_devbsize_supported) {
8152 8152 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8153 8153 } else {
8154 8154 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8155 8155 }
8156 8156 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8157 8157 }
8158 8158
8159 8159 /*
8160 8160 * This property is set to 0 by HA software to avoid retries
8161 8161 * on a reserved disk. (The preferred property name is
8162 8162 * "retry-on-reservation-conflict") (1189689)
8163 8163 *
8164 8164 * Note: The use of a global here can have unintended consequences. A
8165 8165 * per instance variable is preferable to match the capabilities of
8166 8166 * different underlying hba's (4402600)
8167 8167 */
8168 8168 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8169 8169 DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8170 8170 sd_retry_on_reservation_conflict);
8171 8171 if (sd_retry_on_reservation_conflict != 0) {
8172 8172 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8173 8173 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8174 8174 sd_retry_on_reservation_conflict);
8175 8175 }
8176 8176
8177 8177 /* Set up options for QFULL handling. */
8178 8178 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8179 8179 "qfull-retries", -1)) != -1) {
8180 8180 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8181 8181 rval, 1);
8182 8182 }
8183 8183 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8184 8184 "qfull-retry-interval", -1)) != -1) {
8185 8185 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8186 8186 rval, 1);
8187 8187 }
8188 8188
8189 8189 /*
8190 8190 * This just prints a message that announces the existence of the
8191 8191 * device. The message is always printed in the system logfile, but
8192 8192 * only appears on the console if the system is booted with the
8193 8193 * -v (verbose) argument.
8194 8194 */
8195 8195 ddi_report_dev(devi);
8196 8196
8197 8197 un->un_mediastate = DKIO_NONE;
8198 8198
8199 8199 /*
8200 8200 * Check if this is a SSD(Solid State Drive).
8201 8201 */
8202 8202 sd_check_solid_state(ssc);
8203 8203
8204 8204 /*
8205 8205 * Check whether the drive is in emulation mode.
8206 8206 */
8207 8207 sd_check_emulation_mode(ssc);
8208 8208
8209 8209 cmlb_alloc_handle(&un->un_cmlbhandle);
8210 8210
8211 8211 #if defined(__i386) || defined(__amd64)
8212 8212 /*
8213 8213 * On x86, compensate for off-by-1 legacy error
8214 8214 */
8215 8215 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8216 8216 (lbasize == un->un_sys_blocksize))
8217 8217 offbyone = CMLB_OFF_BY_ONE;
8218 8218 #endif
8219 8219
8220 8220 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8221 8221 VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8222 8222 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8223 8223 un->un_node_type, offbyone, un->un_cmlbhandle,
8224 8224 (void *)SD_PATH_DIRECT) != 0) {
8225 8225 goto cmlb_attach_failed;
8226 8226 }
8227 8227
8228 8228
8229 8229 /*
8230 8230 * Read and validate the device's geometry (ie, disk label)
8231 8231 * A new unformatted drive will not have a valid geometry, but
8232 8232 * the driver needs to successfully attach to this device so
8233 8233 * the drive can be formatted via ioctls.
8234 8234 */
8235 8235 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8236 8236 (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8237 8237
8238 8238 mutex_enter(SD_MUTEX(un));
8239 8239
8240 8240 /*
8241 8241 * Read and initialize the devid for the unit.
8242 8242 */
8243 8243 if (un->un_f_devid_supported) {
8244 8244 sd_register_devid(ssc, devi, reservation_flag);
8245 8245 }
8246 8246 mutex_exit(SD_MUTEX(un));
8247 8247
8248 8248 #if (defined(__fibre))
8249 8249 /*
8250 8250 * Register callbacks for fibre only. You can't do this solely
8251 8251 * on the basis of the devid_type because this is hba specific.
8252 8252 * We need to query our hba capabilities to find out whether to
8253 8253 * register or not.
8254 8254 */
8255 8255 if (un->un_f_is_fibre) {
8256 8256 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8257 8257 sd_init_event_callbacks(un);
8258 8258 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8259 8259 "sd_unit_attach: un:0x%p event callbacks inserted",
8260 8260 un);
8261 8261 }
8262 8262 }
8263 8263 #endif
8264 8264
8265 8265 if (un->un_f_opt_disable_cache == TRUE) {
8266 8266 /*
8267 8267 * Disable both read cache and write cache. This is
8268 8268 * the historic behavior of the keywords in the config file.
8269 8269 */
8270 8270 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8271 8271 0) {
8272 8272 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8273 8273 "sd_unit_attach: un:0x%p Could not disable "
8274 8274 "caching", un);
8275 8275 goto devid_failed;
8276 8276 }
8277 8277 }
8278 8278
8279 8279 /*
8280 8280 * Check the value of the WCE bit now and
8281 8281 * set un_f_write_cache_enabled accordingly.
8282 8282 */
8283 8283 (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8284 8284 mutex_enter(SD_MUTEX(un));
8285 8285 un->un_f_write_cache_enabled = (wc_enabled != 0);
8286 8286 mutex_exit(SD_MUTEX(un));
8287 8287
8288 8288 if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8289 8289 un->un_tgt_blocksize != DEV_BSIZE) ||
8290 8290 un->un_f_enable_rmw) {
8291 8291 if (!(un->un_wm_cache)) {
8292 8292 (void) snprintf(name_str, sizeof (name_str),
8293 8293 "%s%d_cache",
8294 8294 ddi_driver_name(SD_DEVINFO(un)),
8295 8295 ddi_get_instance(SD_DEVINFO(un)));
8296 8296 un->un_wm_cache = kmem_cache_create(
8297 8297 name_str, sizeof (struct sd_w_map),
8298 8298 8, sd_wm_cache_constructor,
8299 8299 sd_wm_cache_destructor, NULL,
8300 8300 (void *)un, NULL, 0);
8301 8301 if (!(un->un_wm_cache)) {
8302 8302 goto wm_cache_failed;
8303 8303 }
8304 8304 }
8305 8305 }
8306 8306
8307 8307 /*
8308 8308 * Check the value of the NV_SUP bit and set
8309 8309 * un_f_suppress_cache_flush accordingly.
8310 8310 */
8311 8311 sd_get_nv_sup(ssc);
8312 8312
8313 8313 /*
8314 8314 * Find out what type of reservation this disk supports.
8315 8315 */
8316 8316 status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8317 8317
8318 8318 switch (status) {
8319 8319 case 0:
8320 8320 /*
8321 8321 * SCSI-3 reservations are supported.
8322 8322 */
8323 8323 un->un_reservation_type = SD_SCSI3_RESERVATION;
8324 8324 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8325 8325 "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8326 8326 break;
8327 8327 case ENOTSUP:
8328 8328 /*
8329 8329 * The PERSISTENT RESERVE IN command would not be recognized by
8330 8330 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8331 8331 */
8332 8332 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8333 8333 "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8334 8334 un->un_reservation_type = SD_SCSI2_RESERVATION;
8335 8335
8336 8336 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8337 8337 break;
8338 8338 default:
8339 8339 /*
8340 8340 * default to SCSI-3 reservations
8341 8341 */
8342 8342 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8343 8343 "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8344 8344 un->un_reservation_type = SD_SCSI3_RESERVATION;
8345 8345
8346 8346 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8347 8347 break;
8348 8348 }
8349 8349
8350 8350 /*
8351 8351 * Set the pstat and error stat values here, so data obtained during the
8352 8352 * previous attach-time routines is available.
8353 8353 *
8354 8354 * Note: This is a critical sequence that needs to be maintained:
8355 8355 * 1) Instantiate the kstats before any routines using the iopath
8356 8356 * (i.e. sd_send_scsi_cmd).
8357 8357 * 2) Initialize the error stats (sd_set_errstats) and partition
8358 8358 * stats (sd_set_pstats)here, following
8359 8359 * cmlb_validate_geometry(), sd_register_devid(), and
8360 8360 * sd_cache_control().
8361 8361 */
8362 8362
8363 8363 if (un->un_f_pkstats_enabled && geom_label_valid) {
8364 8364 sd_set_pstats(un);
8365 8365 SD_TRACE(SD_LOG_IO_PARTITION, un,
8366 8366 "sd_unit_attach: un:0x%p pstats created and set\n", un);
8367 8367 }
8368 8368
8369 8369 sd_set_errstats(un);
8370 8370 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8371 8371 "sd_unit_attach: un:0x%p errstats set\n", un);
8372 8372
8373 8373
8374 8374 /*
8375 8375 * After successfully attaching an instance, we record the information
8376 8376 * of how many luns have been attached on the relative target and
8377 8377 * controller for parallel SCSI. This information is used when sd tries
8378 8378 * to set the tagged queuing capability in HBA.
8379 8379 */
8380 8380 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8381 8381 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8382 8382 }
8383 8383
8384 8384 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8385 8385 "sd_unit_attach: un:0x%p exit success\n", un);
8386 8386
8387 8387 /* Uninitialize sd_ssc_t pointer */
8388 8388 sd_ssc_fini(ssc);
8389 8389
8390 8390 return (DDI_SUCCESS);
8391 8391
8392 8392 /*
8393 8393 * An error occurred during the attach; clean up & return failure.
8394 8394 */
8395 8395 wm_cache_failed:
8396 8396 devid_failed:
8397 8397
8398 8398 setup_pm_failed:
8399 8399 ddi_remove_minor_node(devi, NULL);
8400 8400
8401 8401 cmlb_attach_failed:
8402 8402 /*
8403 8403 * Cleanup from the scsi_ifsetcap() calls (437868)
8404 8404 */
8405 8405 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8406 8406 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8407 8407
8408 8408 /*
8409 8409 * Refer to the comments of setting tagged-qing in the beginning of
8410 8410 * sd_unit_attach. We can only disable tagged queuing when there is
8411 8411 * no lun attached on the target.
8412 8412 */
8413 8413 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8414 8414 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8415 8415 }
8416 8416
8417 8417 if (un->un_f_is_fibre == FALSE) {
8418 8418 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8419 8419 }
8420 8420
8421 8421 spinup_failed:
8422 8422
8423 8423 /* Uninitialize sd_ssc_t pointer */
8424 8424 sd_ssc_fini(ssc);
8425 8425
8426 8426 mutex_enter(SD_MUTEX(un));
8427 8427
8428 8428 /* Deallocate SCSI FMA memory spaces */
8429 8429 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8430 8430
8431 8431 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8432 8432 if (un->un_direct_priority_timeid != NULL) {
8433 8433 timeout_id_t temp_id = un->un_direct_priority_timeid;
8434 8434 un->un_direct_priority_timeid = NULL;
8435 8435 mutex_exit(SD_MUTEX(un));
8436 8436 (void) untimeout(temp_id);
8437 8437 mutex_enter(SD_MUTEX(un));
8438 8438 }
8439 8439
8440 8440 /* Cancel any pending start/stop timeouts */
8441 8441 if (un->un_startstop_timeid != NULL) {
8442 8442 timeout_id_t temp_id = un->un_startstop_timeid;
8443 8443 un->un_startstop_timeid = NULL;
8444 8444 mutex_exit(SD_MUTEX(un));
8445 8445 (void) untimeout(temp_id);
8446 8446 mutex_enter(SD_MUTEX(un));
8447 8447 }
8448 8448
8449 8449 /* Cancel any pending reset-throttle timeouts */
8450 8450 if (un->un_reset_throttle_timeid != NULL) {
8451 8451 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8452 8452 un->un_reset_throttle_timeid = NULL;
8453 8453 mutex_exit(SD_MUTEX(un));
8454 8454 (void) untimeout(temp_id);
8455 8455 mutex_enter(SD_MUTEX(un));
8456 8456 }
8457 8457
8458 8458 /* Cancel rmw warning message timeouts */
8459 8459 if (un->un_rmw_msg_timeid != NULL) {
8460 8460 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8461 8461 un->un_rmw_msg_timeid = NULL;
8462 8462 mutex_exit(SD_MUTEX(un));
8463 8463 (void) untimeout(temp_id);
8464 8464 mutex_enter(SD_MUTEX(un));
8465 8465 }
8466 8466
8467 8467 /* Cancel any pending retry timeouts */
8468 8468 if (un->un_retry_timeid != NULL) {
8469 8469 timeout_id_t temp_id = un->un_retry_timeid;
8470 8470 un->un_retry_timeid = NULL;
8471 8471 mutex_exit(SD_MUTEX(un));
8472 8472 (void) untimeout(temp_id);
8473 8473 mutex_enter(SD_MUTEX(un));
8474 8474 }
8475 8475
8476 8476 /* Cancel any pending delayed cv broadcast timeouts */
8477 8477 if (un->un_dcvb_timeid != NULL) {
8478 8478 timeout_id_t temp_id = un->un_dcvb_timeid;
8479 8479 un->un_dcvb_timeid = NULL;
8480 8480 mutex_exit(SD_MUTEX(un));
8481 8481 (void) untimeout(temp_id);
8482 8482 mutex_enter(SD_MUTEX(un));
8483 8483 }
8484 8484
8485 8485 mutex_exit(SD_MUTEX(un));
8486 8486
8487 8487 /* There should not be any in-progress I/O so ASSERT this check */
8488 8488 ASSERT(un->un_ncmds_in_transport == 0);
8489 8489 ASSERT(un->un_ncmds_in_driver == 0);
8490 8490
8491 8491 /* Do not free the softstate if the callback routine is active */
8492 8492 sd_sync_with_callback(un);
8493 8493
8494 8494 /*
8495 8495 * Partition stats apparently are not used with removables. These would
8496 8496 * not have been created during attach, so no need to clean them up...
8497 8497 */
8498 8498 if (un->un_errstats != NULL) {
8499 8499 kstat_delete(un->un_errstats);
8500 8500 un->un_errstats = NULL;
8501 8501 }
8502 8502
8503 8503 create_errstats_failed:
8504 8504
8505 8505 if (un->un_stats != NULL) {
8506 8506 kstat_delete(un->un_stats);
8507 8507 un->un_stats = NULL;
8508 8508 }
8509 8509
8510 8510 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8511 8511 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8512 8512
8513 8513 ddi_prop_remove_all(devi);
8514 8514 sema_destroy(&un->un_semoclose);
8515 8515 cv_destroy(&un->un_state_cv);
8516 8516
8517 8517 getrbuf_failed:
8518 8518
8519 8519 sd_free_rqs(un);
8520 8520
8521 8521 alloc_rqs_failed:
8522 8522
8523 8523 devp->sd_private = NULL;
8524 8524 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */
8525 8525
8526 8526 get_softstate_failed:
8527 8527 /*
8528 8528 * Note: the man pages are unclear as to whether or not doing a
8529 8529 * ddi_soft_state_free(sd_state, instance) is the right way to
8530 8530 * clean up after the ddi_soft_state_zalloc() if the subsequent
8531 8531 * ddi_get_soft_state() fails. The implication seems to be
8532 8532 * that the get_soft_state cannot fail if the zalloc succeeds.
8533 8533 */
8534 8534 #ifndef XPV_HVM_DRIVER
8535 8535 ddi_soft_state_free(sd_state, instance);
8536 8536 #endif /* !XPV_HVM_DRIVER */
8537 8537
8538 8538 probe_failed:
8539 8539 scsi_unprobe(devp);
8540 8540
8541 8541 return (DDI_FAILURE);
8542 8542 }
8543 8543
8544 8544
8545 8545 /*
8546 8546 * Function: sd_unit_detach
8547 8547 *
8548 8548 * Description: Performs DDI_DETACH processing for sddetach().
8549 8549 *
8550 8550 * Return Code: DDI_SUCCESS
8551 8551 * DDI_FAILURE
8552 8552 *
8553 8553 * Context: Kernel thread context
8554 8554 */
8555 8555
8556 8556 static int
8557 8557 sd_unit_detach(dev_info_t *devi)
8558 8558 {
8559 8559 struct scsi_device *devp;
8560 8560 struct sd_lun *un;
8561 8561 int i;
8562 8562 int tgt;
8563 8563 dev_t dev;
8564 8564 dev_info_t *pdip = ddi_get_parent(devi);
8565 8565 #ifndef XPV_HVM_DRIVER
8566 8566 int instance = ddi_get_instance(devi);
8567 8567 #endif /* !XPV_HVM_DRIVER */
8568 8568
8569 8569 mutex_enter(&sd_detach_mutex);
8570 8570
8571 8571 /*
8572 8572 * Fail the detach for any of the following:
8573 8573 * - Unable to get the sd_lun struct for the instance
8574 8574 * - A layered driver has an outstanding open on the instance
8575 8575 * - Another thread is already detaching this instance
8576 8576 * - Another thread is currently performing an open
8577 8577 */
8578 8578 devp = ddi_get_driver_private(devi);
8579 8579 if ((devp == NULL) ||
8580 8580 ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8581 8581 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8582 8582 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8583 8583 mutex_exit(&sd_detach_mutex);
8584 8584 return (DDI_FAILURE);
8585 8585 }
8586 8586
8587 8587 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8588 8588
8589 8589 /*
8590 8590 * Mark this instance as currently in a detach, to inhibit any
8591 8591 * opens from a layered driver.
8592 8592 */
8593 8593 un->un_detach_count++;
8594 8594 mutex_exit(&sd_detach_mutex);
8595 8595
8596 8596 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8597 8597 SCSI_ADDR_PROP_TARGET, -1);
8598 8598
8599 8599 dev = sd_make_device(SD_DEVINFO(un));
8600 8600
8601 8601 #ifndef lint
8602 8602 _NOTE(COMPETING_THREADS_NOW);
8603 8603 #endif
8604 8604
8605 8605 mutex_enter(SD_MUTEX(un));
8606 8606
8607 8607 /*
8608 8608 * Fail the detach if there are any outstanding layered
8609 8609 * opens on this device.
8610 8610 */
8611 8611 for (i = 0; i < NDKMAP; i++) {
8612 8612 if (un->un_ocmap.lyropen[i] != 0) {
8613 8613 goto err_notclosed;
8614 8614 }
8615 8615 }
8616 8616
8617 8617 /*
8618 8618 * Verify there are NO outstanding commands issued to this device.
8619 8619 * ie, un_ncmds_in_transport == 0.
8620 8620 * It's possible to have outstanding commands through the physio
8621 8621 * code path, even though everything's closed.
8622 8622 */
8623 8623 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8624 8624 (un->un_direct_priority_timeid != NULL) ||
8625 8625 (un->un_state == SD_STATE_RWAIT)) {
8626 8626 mutex_exit(SD_MUTEX(un));
8627 8627 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8628 8628 "sd_dr_detach: Detach failure due to outstanding cmds\n");
8629 8629 goto err_stillbusy;
8630 8630 }
8631 8631
8632 8632 /*
8633 8633 * If we have the device reserved, release the reservation.
8634 8634 */
8635 8635 if ((un->un_resvd_status & SD_RESERVE) &&
8636 8636 !(un->un_resvd_status & SD_LOST_RESERVE)) {
8637 8637 mutex_exit(SD_MUTEX(un));
8638 8638 /*
8639 8639 * Note: sd_reserve_release sends a command to the device
8640 8640 * via the sd_ioctlcmd() path, and can sleep.
8641 8641 */
8642 8642 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8643 8643 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8644 8644 "sd_dr_detach: Cannot release reservation \n");
8645 8645 }
8646 8646 } else {
8647 8647 mutex_exit(SD_MUTEX(un));
8648 8648 }
8649 8649
8650 8650 /*
8651 8651 * Untimeout any reserve recover, throttle reset, restart unit
8652 8652 * and delayed broadcast timeout threads. Protect the timeout pointer
8653 8653 * from getting nulled by their callback functions.
8654 8654 */
8655 8655 mutex_enter(SD_MUTEX(un));
8656 8656 if (un->un_resvd_timeid != NULL) {
8657 8657 timeout_id_t temp_id = un->un_resvd_timeid;
8658 8658 un->un_resvd_timeid = NULL;
8659 8659 mutex_exit(SD_MUTEX(un));
8660 8660 (void) untimeout(temp_id);
8661 8661 mutex_enter(SD_MUTEX(un));
8662 8662 }
8663 8663
8664 8664 if (un->un_reset_throttle_timeid != NULL) {
8665 8665 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8666 8666 un->un_reset_throttle_timeid = NULL;
8667 8667 mutex_exit(SD_MUTEX(un));
8668 8668 (void) untimeout(temp_id);
8669 8669 mutex_enter(SD_MUTEX(un));
8670 8670 }
8671 8671
8672 8672 if (un->un_startstop_timeid != NULL) {
8673 8673 timeout_id_t temp_id = un->un_startstop_timeid;
8674 8674 un->un_startstop_timeid = NULL;
8675 8675 mutex_exit(SD_MUTEX(un));
8676 8676 (void) untimeout(temp_id);
8677 8677 mutex_enter(SD_MUTEX(un));
8678 8678 }
8679 8679
8680 8680 if (un->un_rmw_msg_timeid != NULL) {
8681 8681 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8682 8682 un->un_rmw_msg_timeid = NULL;
8683 8683 mutex_exit(SD_MUTEX(un));
8684 8684 (void) untimeout(temp_id);
8685 8685 mutex_enter(SD_MUTEX(un));
8686 8686 }
8687 8687
8688 8688 if (un->un_dcvb_timeid != NULL) {
8689 8689 timeout_id_t temp_id = un->un_dcvb_timeid;
8690 8690 un->un_dcvb_timeid = NULL;
8691 8691 mutex_exit(SD_MUTEX(un));
8692 8692 (void) untimeout(temp_id);
8693 8693 } else {
8694 8694 mutex_exit(SD_MUTEX(un));
8695 8695 }
8696 8696
8697 8697 /* Remove any pending reservation reclaim requests for this device */
8698 8698 sd_rmv_resv_reclaim_req(dev);
8699 8699
8700 8700 mutex_enter(SD_MUTEX(un));
8701 8701
8702 8702 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8703 8703 if (un->un_direct_priority_timeid != NULL) {
8704 8704 timeout_id_t temp_id = un->un_direct_priority_timeid;
8705 8705 un->un_direct_priority_timeid = NULL;
8706 8706 mutex_exit(SD_MUTEX(un));
8707 8707 (void) untimeout(temp_id);
8708 8708 mutex_enter(SD_MUTEX(un));
8709 8709 }
8710 8710
8711 8711 /* Cancel any active multi-host disk watch thread requests */
8712 8712 if (un->un_mhd_token != NULL) {
8713 8713 mutex_exit(SD_MUTEX(un));
8714 8714 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8715 8715 if (scsi_watch_request_terminate(un->un_mhd_token,
8716 8716 SCSI_WATCH_TERMINATE_NOWAIT)) {
8717 8717 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8718 8718 "sd_dr_detach: Cannot cancel mhd watch request\n");
8719 8719 /*
8720 8720 * Note: We are returning here after having removed
8721 8721 * some driver timeouts above. This is consistent with
8722 8722 * the legacy implementation but perhaps the watch
8723 8723 * terminate call should be made with the wait flag set.
8724 8724 */
8725 8725 goto err_stillbusy;
8726 8726 }
8727 8727 mutex_enter(SD_MUTEX(un));
8728 8728 un->un_mhd_token = NULL;
8729 8729 }
8730 8730
8731 8731 if (un->un_swr_token != NULL) {
8732 8732 mutex_exit(SD_MUTEX(un));
8733 8733 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8734 8734 if (scsi_watch_request_terminate(un->un_swr_token,
8735 8735 SCSI_WATCH_TERMINATE_NOWAIT)) {
8736 8736 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8737 8737 "sd_dr_detach: Cannot cancel swr watch request\n");
8738 8738 /*
8739 8739 * Note: We are returning here after having removed
8740 8740 * some driver timeouts above. This is consistent with
8741 8741 * the legacy implementation but perhaps the watch
8742 8742 * terminate call should be made with the wait flag set.
8743 8743 */
8744 8744 goto err_stillbusy;
8745 8745 }
8746 8746 mutex_enter(SD_MUTEX(un));
8747 8747 un->un_swr_token = NULL;
8748 8748 }
8749 8749
8750 8750 mutex_exit(SD_MUTEX(un));
8751 8751
8752 8752 /*
8753 8753 * Clear any scsi_reset_notifies. We clear the reset notifies
8754 8754 * if we have not registered one.
8755 8755 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8756 8756 */
8757 8757 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8758 8758 sd_mhd_reset_notify_cb, (caddr_t)un);
8759 8759
8760 8760 /*
8761 8761 * protect the timeout pointers from getting nulled by
8762 8762 * their callback functions during the cancellation process.
8763 8763 * In such a scenario untimeout can be invoked with a null value.
8764 8764 */
8765 8765 _NOTE(NO_COMPETING_THREADS_NOW);
8766 8766
8767 8767 mutex_enter(&un->un_pm_mutex);
8768 8768 if (un->un_pm_idle_timeid != NULL) {
8769 8769 timeout_id_t temp_id = un->un_pm_idle_timeid;
8770 8770 un->un_pm_idle_timeid = NULL;
8771 8771 mutex_exit(&un->un_pm_mutex);
8772 8772
8773 8773 /*
8774 8774 * Timeout is active; cancel it.
8775 8775 * Note that it'll never be active on a device
8776 8776 * that does not support PM therefore we don't
8777 8777 * have to check before calling pm_idle_component.
8778 8778 */
8779 8779 (void) untimeout(temp_id);
8780 8780 (void) pm_idle_component(SD_DEVINFO(un), 0);
8781 8781 mutex_enter(&un->un_pm_mutex);
8782 8782 }
8783 8783
8784 8784 /*
8785 8785 * Check whether there is already a timeout scheduled for power
8786 8786 * management. If yes then don't lower the power here, that's.
8787 8787 * the timeout handler's job.
8788 8788 */
8789 8789 if (un->un_pm_timeid != NULL) {
8790 8790 timeout_id_t temp_id = un->un_pm_timeid;
8791 8791 un->un_pm_timeid = NULL;
8792 8792 mutex_exit(&un->un_pm_mutex);
8793 8793 /*
8794 8794 * Timeout is active; cancel it.
8795 8795 * Note that it'll never be active on a device
8796 8796 * that does not support PM therefore we don't
8797 8797 * have to check before calling pm_idle_component.
8798 8798 */
8799 8799 (void) untimeout(temp_id);
8800 8800 (void) pm_idle_component(SD_DEVINFO(un), 0);
8801 8801
8802 8802 } else {
8803 8803 mutex_exit(&un->un_pm_mutex);
8804 8804 if ((un->un_f_pm_is_enabled == TRUE) &&
8805 8805 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8806 8806 != DDI_SUCCESS)) {
8807 8807 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8808 8808 "sd_dr_detach: Lower power request failed, ignoring.\n");
8809 8809 /*
8810 8810 * Fix for bug: 4297749, item # 13
8811 8811 * The above test now includes a check to see if PM is
8812 8812 * supported by this device before call
8813 8813 * pm_lower_power().
8814 8814 * Note, the following is not dead code. The call to
8815 8815 * pm_lower_power above will generate a call back into
8816 8816 * our sdpower routine which might result in a timeout
8817 8817 * handler getting activated. Therefore the following
8818 8818 * code is valid and necessary.
8819 8819 */
8820 8820 mutex_enter(&un->un_pm_mutex);
8821 8821 if (un->un_pm_timeid != NULL) {
8822 8822 timeout_id_t temp_id = un->un_pm_timeid;
8823 8823 un->un_pm_timeid = NULL;
8824 8824 mutex_exit(&un->un_pm_mutex);
8825 8825 (void) untimeout(temp_id);
8826 8826 (void) pm_idle_component(SD_DEVINFO(un), 0);
8827 8827 } else {
8828 8828 mutex_exit(&un->un_pm_mutex);
8829 8829 }
8830 8830 }
8831 8831 }
8832 8832
8833 8833 /*
8834 8834 * Cleanup from the scsi_ifsetcap() calls (437868)
8835 8835 * Relocated here from above to be after the call to
8836 8836 * pm_lower_power, which was getting errors.
8837 8837 */
8838 8838 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8839 8839 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8840 8840
8841 8841 /*
8842 8842 * Currently, tagged queuing is supported per target based by HBA.
8843 8843 * Setting this per lun instance actually sets the capability of this
8844 8844 * target in HBA, which affects those luns already attached on the
8845 8845 * same target. So during detach, we can only disable this capability
8846 8846 * only when this is the only lun left on this target. By doing
8847 8847 * this, we assume a target has the same tagged queuing capability
8848 8848 * for every lun. The condition can be removed when HBA is changed to
8849 8849 * support per lun based tagged queuing capability.
8850 8850 */
8851 8851 if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8852 8852 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8853 8853 }
8854 8854
8855 8855 if (un->un_f_is_fibre == FALSE) {
8856 8856 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8857 8857 }
8858 8858
8859 8859 /*
8860 8860 * Remove any event callbacks, fibre only
8861 8861 */
8862 8862 if (un->un_f_is_fibre == TRUE) {
8863 8863 if ((un->un_insert_event != NULL) &&
8864 8864 (ddi_remove_event_handler(un->un_insert_cb_id) !=
8865 8865 DDI_SUCCESS)) {
8866 8866 /*
8867 8867 * Note: We are returning here after having done
8868 8868 * substantial cleanup above. This is consistent
8869 8869 * with the legacy implementation but this may not
8870 8870 * be the right thing to do.
8871 8871 */
8872 8872 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8873 8873 "sd_dr_detach: Cannot cancel insert event\n");
8874 8874 goto err_remove_event;
8875 8875 }
8876 8876 un->un_insert_event = NULL;
8877 8877
8878 8878 if ((un->un_remove_event != NULL) &&
8879 8879 (ddi_remove_event_handler(un->un_remove_cb_id) !=
8880 8880 DDI_SUCCESS)) {
8881 8881 /*
8882 8882 * Note: We are returning here after having done
8883 8883 * substantial cleanup above. This is consistent
8884 8884 * with the legacy implementation but this may not
8885 8885 * be the right thing to do.
8886 8886 */
8887 8887 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8888 8888 "sd_dr_detach: Cannot cancel remove event\n");
8889 8889 goto err_remove_event;
8890 8890 }
8891 8891 un->un_remove_event = NULL;
8892 8892 }
8893 8893
8894 8894 /* Do not free the softstate if the callback routine is active */
8895 8895 sd_sync_with_callback(un);
8896 8896
8897 8897 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8898 8898 cmlb_free_handle(&un->un_cmlbhandle);
8899 8899
8900 8900 /*
8901 8901 * Hold the detach mutex here, to make sure that no other threads ever
8902 8902 * can access a (partially) freed soft state structure.
8903 8903 */
8904 8904 mutex_enter(&sd_detach_mutex);
8905 8905
8906 8906 /*
8907 8907 * Clean up the soft state struct.
8908 8908 * Cleanup is done in reverse order of allocs/inits.
8909 8909 * At this point there should be no competing threads anymore.
8910 8910 */
8911 8911
8912 8912 scsi_fm_fini(devp);
8913 8913
8914 8914 /*
8915 8915 * Deallocate memory for SCSI FMA.
8916 8916 */
8917 8917 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8918 8918
8919 8919 /*
8920 8920 * Unregister and free device id if it was not registered
8921 8921 * by the transport.
8922 8922 */
8923 8923 if (un->un_f_devid_transport_defined == FALSE)
8924 8924 ddi_devid_unregister(devi);
8925 8925
8926 8926 /*
8927 8927 * free the devid structure if allocated before (by ddi_devid_init()
8928 8928 * or ddi_devid_get()).
8929 8929 */
8930 8930 if (un->un_devid) {
8931 8931 ddi_devid_free(un->un_devid);
8932 8932 un->un_devid = NULL;
8933 8933 }
8934 8934
8935 8935 /*
8936 8936 * Destroy wmap cache if it exists.
8937 8937 */
8938 8938 if (un->un_wm_cache != NULL) {
8939 8939 kmem_cache_destroy(un->un_wm_cache);
8940 8940 un->un_wm_cache = NULL;
8941 8941 }
8942 8942
8943 8943 /*
8944 8944 * kstat cleanup is done in detach for all device types (4363169).
8945 8945 * We do not want to fail detach if the device kstats are not deleted
8946 8946 * since there is a confusion about the devo_refcnt for the device.
8947 8947 * We just delete the kstats and let detach complete successfully.
8948 8948 */
8949 8949 if (un->un_stats != NULL) {
8950 8950 kstat_delete(un->un_stats);
8951 8951 un->un_stats = NULL;
8952 8952 }
8953 8953 if (un->un_errstats != NULL) {
8954 8954 kstat_delete(un->un_errstats);
8955 8955 un->un_errstats = NULL;
8956 8956 }
8957 8957
8958 8958 /* Remove partition stats */
8959 8959 if (un->un_f_pkstats_enabled) {
8960 8960 for (i = 0; i < NSDMAP; i++) {
8961 8961 if (un->un_pstats[i] != NULL) {
8962 8962 kstat_delete(un->un_pstats[i]);
8963 8963 un->un_pstats[i] = NULL;
8964 8964 }
8965 8965 }
8966 8966 }
8967 8967
8968 8968 /* Remove xbuf registration */
8969 8969 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8970 8970 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8971 8971
8972 8972 /* Remove driver properties */
8973 8973 ddi_prop_remove_all(devi);
8974 8974
8975 8975 mutex_destroy(&un->un_pm_mutex);
8976 8976 cv_destroy(&un->un_pm_busy_cv);
8977 8977
8978 8978 cv_destroy(&un->un_wcc_cv);
8979 8979
8980 8980 /* Open/close semaphore */
8981 8981 sema_destroy(&un->un_semoclose);
8982 8982
8983 8983 /* Removable media condvar. */
8984 8984 cv_destroy(&un->un_state_cv);
8985 8985
8986 8986 /* Suspend/resume condvar. */
8987 8987 cv_destroy(&un->un_suspend_cv);
8988 8988 cv_destroy(&un->un_disk_busy_cv);
8989 8989
8990 8990 sd_free_rqs(un);
8991 8991
8992 8992 /* Free up soft state */
8993 8993 devp->sd_private = NULL;
8994 8994
8995 8995 bzero(un, sizeof (struct sd_lun));
8996 8996 #ifndef XPV_HVM_DRIVER
8997 8997 ddi_soft_state_free(sd_state, instance);
8998 8998 #endif /* !XPV_HVM_DRIVER */
8999 8999
9000 9000 mutex_exit(&sd_detach_mutex);
9001 9001
9002 9002 /* This frees up the INQUIRY data associated with the device. */
9003 9003 scsi_unprobe(devp);
9004 9004
9005 9005 /*
9006 9006 * After successfully detaching an instance, we update the information
9007 9007 * of how many luns have been attached in the relative target and
9008 9008 * controller for parallel SCSI. This information is used when sd tries
9009 9009 * to set the tagged queuing capability in HBA.
9010 9010 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9011 9011 * check if the device is parallel SCSI. However, we don't need to
9012 9012 * check here because we've already checked during attach. No device
9013 9013 * that is not parallel SCSI is in the chain.
9014 9014 */
9015 9015 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9016 9016 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9017 9017 }
9018 9018
9019 9019 return (DDI_SUCCESS);
9020 9020
9021 9021 err_notclosed:
9022 9022 mutex_exit(SD_MUTEX(un));
9023 9023
9024 9024 err_stillbusy:
9025 9025 _NOTE(NO_COMPETING_THREADS_NOW);
9026 9026
9027 9027 err_remove_event:
9028 9028 mutex_enter(&sd_detach_mutex);
9029 9029 un->un_detach_count--;
9030 9030 mutex_exit(&sd_detach_mutex);
9031 9031
9032 9032 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9033 9033 return (DDI_FAILURE);
9034 9034 }
9035 9035
9036 9036
9037 9037 /*
9038 9038 * Function: sd_create_errstats
9039 9039 *
9040 9040 * Description: This routine instantiates the device error stats.
9041 9041 *
9042 9042 * Note: During attach the stats are instantiated first so they are
9043 9043 * available for attach-time routines that utilize the driver
9044 9044 * iopath to send commands to the device. The stats are initialized
9045 9045 * separately so data obtained during some attach-time routines is
9046 9046 * available. (4362483)
9047 9047 *
9048 9048 * Arguments: un - driver soft state (unit) structure
9049 9049 * instance - driver instance
9050 9050 *
9051 9051 * Context: Kernel thread context
9052 9052 */
9053 9053
9054 9054 static void
9055 9055 sd_create_errstats(struct sd_lun *un, int instance)
9056 9056 {
9057 9057 struct sd_errstats *stp;
9058 9058 char kstatmodule_err[KSTAT_STRLEN];
9059 9059 char kstatname[KSTAT_STRLEN];
9060 9060 int ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9061 9061
9062 9062 ASSERT(un != NULL);
9063 9063
9064 9064 if (un->un_errstats != NULL) {
9065 9065 return;
9066 9066 }
9067 9067
9068 9068 (void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9069 9069 "%serr", sd_label);
9070 9070 (void) snprintf(kstatname, sizeof (kstatname),
9071 9071 "%s%d,err", sd_label, instance);
9072 9072
9073 9073 un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9074 9074 "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9075 9075
9076 9076 if (un->un_errstats == NULL) {
9077 9077 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9078 9078 "sd_create_errstats: Failed kstat_create\n");
9079 9079 return;
9080 9080 }
9081 9081
9082 9082 stp = (struct sd_errstats *)un->un_errstats->ks_data;
9083 9083 kstat_named_init(&stp->sd_softerrs, "Soft Errors",
9084 9084 KSTAT_DATA_UINT32);
9085 9085 kstat_named_init(&stp->sd_harderrs, "Hard Errors",
9086 9086 KSTAT_DATA_UINT32);
9087 9087 kstat_named_init(&stp->sd_transerrs, "Transport Errors",
9088 9088 KSTAT_DATA_UINT32);
9089 9089 kstat_named_init(&stp->sd_vid, "Vendor",
9090 9090 KSTAT_DATA_CHAR);
9091 9091 kstat_named_init(&stp->sd_pid, "Product",
9092 9092 KSTAT_DATA_CHAR);
9093 9093 kstat_named_init(&stp->sd_revision, "Revision",
9094 9094 KSTAT_DATA_CHAR);
9095 9095 kstat_named_init(&stp->sd_serial, "Serial No",
9096 9096 KSTAT_DATA_CHAR);
9097 9097 kstat_named_init(&stp->sd_capacity, "Size",
9098 9098 KSTAT_DATA_ULONGLONG);
9099 9099 kstat_named_init(&stp->sd_rq_media_err, "Media Error",
9100 9100 KSTAT_DATA_UINT32);
9101 9101 kstat_named_init(&stp->sd_rq_ntrdy_err, "Device Not Ready",
9102 9102 KSTAT_DATA_UINT32);
9103 9103 kstat_named_init(&stp->sd_rq_nodev_err, "No Device",
9104 9104 KSTAT_DATA_UINT32);
9105 9105 kstat_named_init(&stp->sd_rq_recov_err, "Recoverable",
9106 9106 KSTAT_DATA_UINT32);
9107 9107 kstat_named_init(&stp->sd_rq_illrq_err, "Illegal Request",
9108 9108 KSTAT_DATA_UINT32);
9109 9109 kstat_named_init(&stp->sd_rq_pfa_err, "Predictive Failure Analysis",
9110 9110 KSTAT_DATA_UINT32);
9111 9111
9112 9112 un->un_errstats->ks_private = un;
9113 9113 un->un_errstats->ks_update = nulldev;
9114 9114
9115 9115 kstat_install(un->un_errstats);
9116 9116 }
9117 9117
9118 9118
9119 9119 /*
9120 9120 * Function: sd_set_errstats
9121 9121 *
9122 9122 * Description: This routine sets the value of the vendor id, product id,
9123 9123 * revision, serial number, and capacity device error stats.
9124 9124 *
9125 9125 * Note: During attach the stats are instantiated first so they are
9126 9126 * available for attach-time routines that utilize the driver
9127 9127 * iopath to send commands to the device. The stats are initialized
9128 9128 * separately so data obtained during some attach-time routines is
9129 9129 * available. (4362483)
9130 9130 *
9131 9131 * Arguments: un - driver soft state (unit) structure
9132 9132 *
9133 9133 * Context: Kernel thread context
9134 9134 */
9135 9135
9136 9136 static void
9137 9137 sd_set_errstats(struct sd_lun *un)
9138 9138 {
9139 9139 struct sd_errstats *stp;
9140 9140 char *sn;
9141 9141
9142 9142 ASSERT(un != NULL);
9143 9143 ASSERT(un->un_errstats != NULL);
9144 9144 stp = (struct sd_errstats *)un->un_errstats->ks_data;
9145 9145 ASSERT(stp != NULL);
9146 9146 (void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9147 9147 (void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9148 9148 (void) strncpy(stp->sd_revision.value.c,
9149 9149 un->un_sd->sd_inq->inq_revision, 4);
9150 9150
9151 9151 /*
9152 9152 * All the errstats are persistent across detach/attach,
9153 9153 * so reset all the errstats here in case of the hot
9154 9154 * replacement of disk drives, except for not changed
9155 9155 * Sun qualified drives.
9156 9156 */
9157 9157 if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9158 9158 (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9159 9159 sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9160 9160 stp->sd_softerrs.value.ui32 = 0;
9161 9161 stp->sd_harderrs.value.ui32 = 0;
9162 9162 stp->sd_transerrs.value.ui32 = 0;
9163 9163 stp->sd_rq_media_err.value.ui32 = 0;
9164 9164 stp->sd_rq_ntrdy_err.value.ui32 = 0;
9165 9165 stp->sd_rq_nodev_err.value.ui32 = 0;
9166 9166 stp->sd_rq_recov_err.value.ui32 = 0;
9167 9167 stp->sd_rq_illrq_err.value.ui32 = 0;
9168 9168 stp->sd_rq_pfa_err.value.ui32 = 0;
9169 9169 }
9170 9170
9171 9171 /*
9172 9172 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9173 9173 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9174 9174 * (4376302))
9175 9175 */
9176 9176 if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9177 9177 bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9178 9178 sizeof (SD_INQUIRY(un)->inq_serial));
9179 9179 } else {
9180 9180 /*
9181 9181 * Set the "Serial No" kstat for non-Sun qualified drives
9182 9182 */
9183 9183 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9184 9184 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9185 9185 INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9186 9186 (void) strlcpy(stp->sd_serial.value.c, sn,
9187 9187 sizeof (stp->sd_serial.value.c));
9188 9188 ddi_prop_free(sn);
9189 9189 }
9190 9190 }
9191 9191
9192 9192 if (un->un_f_blockcount_is_valid != TRUE) {
9193 9193 /*
9194 9194 * Set capacity error stat to 0 for no media. This ensures
9195 9195 * a valid capacity is displayed in response to 'iostat -E'
9196 9196 * when no media is present in the device.
9197 9197 */
9198 9198 stp->sd_capacity.value.ui64 = 0;
9199 9199 } else {
9200 9200 /*
9201 9201 * Multiply un_blockcount by un->un_sys_blocksize to get
9202 9202 * capacity.
9203 9203 *
9204 9204 * Note: for non-512 blocksize devices "un_blockcount" has been
9205 9205 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9206 9206 * (un_tgt_blocksize / un->un_sys_blocksize).
9207 9207 */
9208 9208 stp->sd_capacity.value.ui64 = (uint64_t)
9209 9209 ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9210 9210 }
9211 9211 }
9212 9212
9213 9213
9214 9214 /*
9215 9215 * Function: sd_set_pstats
9216 9216 *
9217 9217 * Description: This routine instantiates and initializes the partition
9218 9218 * stats for each partition with more than zero blocks.
9219 9219 * (4363169)
9220 9220 *
9221 9221 * Arguments: un - driver soft state (unit) structure
9222 9222 *
9223 9223 * Context: Kernel thread context
9224 9224 */
9225 9225
9226 9226 static void
9227 9227 sd_set_pstats(struct sd_lun *un)
9228 9228 {
9229 9229 char kstatname[KSTAT_STRLEN];
9230 9230 int instance;
9231 9231 int i;
9232 9232 diskaddr_t nblks = 0;
9233 9233 char *partname = NULL;
9234 9234
9235 9235 ASSERT(un != NULL);
9236 9236
9237 9237 instance = ddi_get_instance(SD_DEVINFO(un));
9238 9238
9239 9239 /* Note:x86: is this a VTOC8/VTOC16 difference? */
9240 9240 for (i = 0; i < NSDMAP; i++) {
9241 9241
9242 9242 if (cmlb_partinfo(un->un_cmlbhandle, i,
9243 9243 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9244 9244 continue;
9245 9245 mutex_enter(SD_MUTEX(un));
9246 9246
9247 9247 if ((un->un_pstats[i] == NULL) &&
9248 9248 (nblks != 0)) {
9249 9249
9250 9250 (void) snprintf(kstatname, sizeof (kstatname),
9251 9251 "%s%d,%s", sd_label, instance,
9252 9252 partname);
9253 9253
9254 9254 un->un_pstats[i] = kstat_create(sd_label,
9255 9255 instance, kstatname, "partition", KSTAT_TYPE_IO,
9256 9256 1, KSTAT_FLAG_PERSISTENT);
9257 9257 if (un->un_pstats[i] != NULL) {
9258 9258 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9259 9259 kstat_install(un->un_pstats[i]);
9260 9260 }
9261 9261 }
9262 9262 mutex_exit(SD_MUTEX(un));
9263 9263 }
9264 9264 }
9265 9265
9266 9266
9267 9267 #if (defined(__fibre))
9268 9268 /*
9269 9269 * Function: sd_init_event_callbacks
9270 9270 *
9271 9271 * Description: This routine initializes the insertion and removal event
9272 9272 * callbacks. (fibre only)
9273 9273 *
9274 9274 * Arguments: un - driver soft state (unit) structure
9275 9275 *
9276 9276 * Context: Kernel thread context
9277 9277 */
9278 9278
9279 9279 static void
9280 9280 sd_init_event_callbacks(struct sd_lun *un)
9281 9281 {
9282 9282 ASSERT(un != NULL);
9283 9283
9284 9284 if ((un->un_insert_event == NULL) &&
9285 9285 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9286 9286 &un->un_insert_event) == DDI_SUCCESS)) {
9287 9287 /*
9288 9288 * Add the callback for an insertion event
9289 9289 */
9290 9290 (void) ddi_add_event_handler(SD_DEVINFO(un),
9291 9291 un->un_insert_event, sd_event_callback, (void *)un,
9292 9292 &(un->un_insert_cb_id));
9293 9293 }
9294 9294
9295 9295 if ((un->un_remove_event == NULL) &&
9296 9296 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9297 9297 &un->un_remove_event) == DDI_SUCCESS)) {
9298 9298 /*
9299 9299 * Add the callback for a removal event
9300 9300 */
9301 9301 (void) ddi_add_event_handler(SD_DEVINFO(un),
9302 9302 un->un_remove_event, sd_event_callback, (void *)un,
9303 9303 &(un->un_remove_cb_id));
9304 9304 }
9305 9305 }
9306 9306
9307 9307
9308 9308 /*
9309 9309 * Function: sd_event_callback
9310 9310 *
9311 9311 * Description: This routine handles insert/remove events (photon). The
9312 9312 * state is changed to OFFLINE which can be used to supress
9313 9313 * error msgs. (fibre only)
9314 9314 *
9315 9315 * Arguments: un - driver soft state (unit) structure
9316 9316 *
9317 9317 * Context: Callout thread context
9318 9318 */
9319 9319 /* ARGSUSED */
9320 9320 static void
9321 9321 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9322 9322 void *bus_impldata)
9323 9323 {
9324 9324 struct sd_lun *un = (struct sd_lun *)arg;
9325 9325
9326 9326 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9327 9327 if (event == un->un_insert_event) {
9328 9328 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9329 9329 mutex_enter(SD_MUTEX(un));
9330 9330 if (un->un_state == SD_STATE_OFFLINE) {
9331 9331 if (un->un_last_state != SD_STATE_SUSPENDED) {
9332 9332 un->un_state = un->un_last_state;
9333 9333 } else {
9334 9334 /*
9335 9335 * We have gone through SUSPEND/RESUME while
9336 9336 * we were offline. Restore the last state
9337 9337 */
9338 9338 un->un_state = un->un_save_state;
9339 9339 }
9340 9340 }
9341 9341 mutex_exit(SD_MUTEX(un));
9342 9342
9343 9343 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9344 9344 } else if (event == un->un_remove_event) {
9345 9345 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9346 9346 mutex_enter(SD_MUTEX(un));
9347 9347 /*
9348 9348 * We need to handle an event callback that occurs during
9349 9349 * the suspend operation, since we don't prevent it.
9350 9350 */
9351 9351 if (un->un_state != SD_STATE_OFFLINE) {
9352 9352 if (un->un_state != SD_STATE_SUSPENDED) {
9353 9353 New_state(un, SD_STATE_OFFLINE);
9354 9354 } else {
9355 9355 un->un_last_state = SD_STATE_OFFLINE;
9356 9356 }
9357 9357 }
9358 9358 mutex_exit(SD_MUTEX(un));
9359 9359 } else {
9360 9360 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9361 9361 "!Unknown event\n");
9362 9362 }
9363 9363
9364 9364 }
9365 9365 #endif
9366 9366
9367 9367 /*
9368 9368 * Function: sd_cache_control()
9369 9369 *
9370 9370 * Description: This routine is the driver entry point for setting
9371 9371 * read and write caching by modifying the WCE (write cache
9372 9372 * enable) and RCD (read cache disable) bits of mode
9373 9373 * page 8 (MODEPAGE_CACHING).
9374 9374 *
9375 9375 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
9376 9376 * structure for this target.
9377 9377 * rcd_flag - flag for controlling the read cache
9378 9378 * wce_flag - flag for controlling the write cache
9379 9379 *
9380 9380 * Return Code: EIO
9381 9381 * code returned by sd_send_scsi_MODE_SENSE and
9382 9382 * sd_send_scsi_MODE_SELECT
9383 9383 *
9384 9384 * Context: Kernel Thread
9385 9385 */
9386 9386
9387 9387 static int
9388 9388 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9389 9389 {
9390 9390 struct mode_caching *mode_caching_page;
9391 9391 uchar_t *header;
9392 9392 size_t buflen;
9393 9393 int hdrlen;
9394 9394 int bd_len;
9395 9395 int rval = 0;
9396 9396 struct mode_header_grp2 *mhp;
9397 9397 struct sd_lun *un;
9398 9398 int status;
9399 9399
9400 9400 ASSERT(ssc != NULL);
9401 9401 un = ssc->ssc_un;
9402 9402 ASSERT(un != NULL);
9403 9403
9404 9404 /*
9405 9405 * Do a test unit ready, otherwise a mode sense may not work if this
9406 9406 * is the first command sent to the device after boot.
9407 9407 */
9408 9408 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9409 9409 if (status != 0)
9410 9410 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9411 9411
9412 9412 if (un->un_f_cfg_is_atapi == TRUE) {
9413 9413 hdrlen = MODE_HEADER_LENGTH_GRP2;
9414 9414 } else {
9415 9415 hdrlen = MODE_HEADER_LENGTH;
9416 9416 }
9417 9417
9418 9418 /*
9419 9419 * Allocate memory for the retrieved mode page and its headers. Set
9420 9420 * a pointer to the page itself. Use mode_cache_scsi3 to insure
9421 9421 * we get all of the mode sense data otherwise, the mode select
9422 9422 * will fail. mode_cache_scsi3 is a superset of mode_caching.
9423 9423 */
9424 9424 buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9425 9425 sizeof (struct mode_cache_scsi3);
9426 9426
9427 9427 header = kmem_zalloc(buflen, KM_SLEEP);
9428 9428
9429 9429 /* Get the information from the device. */
9430 9430 if (un->un_f_cfg_is_atapi == TRUE) {
9431 9431 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9432 9432 MODEPAGE_CACHING, SD_PATH_DIRECT);
9433 9433 } else {
9434 9434 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9435 9435 MODEPAGE_CACHING, SD_PATH_DIRECT);
9436 9436 }
9437 9437
9438 9438 if (rval != 0) {
9439 9439 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9440 9440 "sd_cache_control: Mode Sense Failed\n");
9441 9441 goto mode_sense_failed;
9442 9442 }
9443 9443
9444 9444 /*
9445 9445 * Determine size of Block Descriptors in order to locate
9446 9446 * the mode page data. ATAPI devices return 0, SCSI devices
9447 9447 * should return MODE_BLK_DESC_LENGTH.
9448 9448 */
9449 9449 if (un->un_f_cfg_is_atapi == TRUE) {
9450 9450 mhp = (struct mode_header_grp2 *)header;
9451 9451 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9452 9452 } else {
9453 9453 bd_len = ((struct mode_header *)header)->bdesc_length;
9454 9454 }
9455 9455
9456 9456 if (bd_len > MODE_BLK_DESC_LENGTH) {
9457 9457 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9458 9458 "sd_cache_control: Mode Sense returned invalid block "
9459 9459 "descriptor length\n");
9460 9460 rval = EIO;
9461 9461 goto mode_sense_failed;
9462 9462 }
9463 9463
9464 9464 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9465 9465 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9466 9466 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9467 9467 "sd_cache_control: Mode Sense caching page code mismatch "
9468 9468 "%d\n", mode_caching_page->mode_page.code);
9469 9469 rval = EIO;
9470 9470 goto mode_sense_failed;
9471 9471 }
9472 9472
9473 9473 /* Check the relevant bits on successful mode sense. */
9474 9474 if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9475 9475 (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9476 9476 (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9477 9477 (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9478 9478
9479 9479 size_t sbuflen;
9480 9480 uchar_t save_pg;
9481 9481
9482 9482 /*
9483 9483 * Construct select buffer length based on the
9484 9484 * length of the sense data returned.
9485 9485 */
9486 9486 sbuflen = hdrlen + bd_len +
9487 9487 sizeof (struct mode_page) +
9488 9488 (int)mode_caching_page->mode_page.length;
9489 9489
9490 9490 /*
9491 9491 * Set the caching bits as requested.
9492 9492 */
9493 9493 if (rcd_flag == SD_CACHE_ENABLE)
9494 9494 mode_caching_page->rcd = 0;
9495 9495 else if (rcd_flag == SD_CACHE_DISABLE)
9496 9496 mode_caching_page->rcd = 1;
9497 9497
9498 9498 if (wce_flag == SD_CACHE_ENABLE)
9499 9499 mode_caching_page->wce = 1;
9500 9500 else if (wce_flag == SD_CACHE_DISABLE)
9501 9501 mode_caching_page->wce = 0;
9502 9502
9503 9503 /*
9504 9504 * Save the page if the mode sense says the
9505 9505 * drive supports it.
9506 9506 */
9507 9507 save_pg = mode_caching_page->mode_page.ps ?
9508 9508 SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9509 9509
9510 9510 /* Clear reserved bits before mode select. */
9511 9511 mode_caching_page->mode_page.ps = 0;
9512 9512
9513 9513 /*
9514 9514 * Clear out mode header for mode select.
9515 9515 * The rest of the retrieved page will be reused.
9516 9516 */
9517 9517 bzero(header, hdrlen);
9518 9518
9519 9519 if (un->un_f_cfg_is_atapi == TRUE) {
9520 9520 mhp = (struct mode_header_grp2 *)header;
9521 9521 mhp->bdesc_length_hi = bd_len >> 8;
9522 9522 mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9523 9523 } else {
9524 9524 ((struct mode_header *)header)->bdesc_length = bd_len;
9525 9525 }
9526 9526
9527 9527 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9528 9528
9529 9529 /* Issue mode select to change the cache settings */
9530 9530 if (un->un_f_cfg_is_atapi == TRUE) {
9531 9531 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header,
9532 9532 sbuflen, save_pg, SD_PATH_DIRECT);
9533 9533 } else {
9534 9534 rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
9535 9535 sbuflen, save_pg, SD_PATH_DIRECT);
9536 9536 }
9537 9537
9538 9538 }
9539 9539
9540 9540
9541 9541 mode_sense_failed:
9542 9542
9543 9543 kmem_free(header, buflen);
9544 9544
9545 9545 if (rval != 0) {
9546 9546 if (rval == EIO)
9547 9547 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9548 9548 else
9549 9549 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9550 9550 }
9551 9551 return (rval);
9552 9552 }
9553 9553
9554 9554
9555 9555 /*
9556 9556 * Function: sd_get_write_cache_enabled()
9557 9557 *
9558 9558 * Description: This routine is the driver entry point for determining if
9559 9559 * write caching is enabled. It examines the WCE (write cache
9560 9560 * enable) bits of mode page 8 (MODEPAGE_CACHING).
9561 9561 *
9562 9562 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
9563 9563 * structure for this target.
9564 9564 * is_enabled - pointer to int where write cache enabled state
9565 9565 * is returned (non-zero -> write cache enabled)
9566 9566 *
9567 9567 *
9568 9568 * Return Code: EIO
9569 9569 * code returned by sd_send_scsi_MODE_SENSE
9570 9570 *
9571 9571 * Context: Kernel Thread
9572 9572 *
9573 9573 * NOTE: If ioctl is added to disable write cache, this sequence should
9574 9574 * be followed so that no locking is required for accesses to
9575 9575 * un->un_f_write_cache_enabled:
9576 9576 * do mode select to clear wce
9577 9577 * do synchronize cache to flush cache
9578 9578 * set un->un_f_write_cache_enabled = FALSE
9579 9579 *
9580 9580 * Conversely, an ioctl to enable the write cache should be done
9581 9581 * in this order:
9582 9582 * set un->un_f_write_cache_enabled = TRUE
9583 9583 * do mode select to set wce
9584 9584 */
9585 9585
9586 9586 static int
9587 9587 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9588 9588 {
9589 9589 struct mode_caching *mode_caching_page;
9590 9590 uchar_t *header;
9591 9591 size_t buflen;
9592 9592 int hdrlen;
9593 9593 int bd_len;
9594 9594 int rval = 0;
9595 9595 struct sd_lun *un;
9596 9596 int status;
9597 9597
9598 9598 ASSERT(ssc != NULL);
9599 9599 un = ssc->ssc_un;
9600 9600 ASSERT(un != NULL);
9601 9601 ASSERT(is_enabled != NULL);
9602 9602
9603 9603 /* in case of error, flag as enabled */
9604 9604 *is_enabled = TRUE;
9605 9605
9606 9606 /*
9607 9607 * Do a test unit ready, otherwise a mode sense may not work if this
9608 9608 * is the first command sent to the device after boot.
9609 9609 */
9610 9610 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9611 9611
9612 9612 if (status != 0)
9613 9613 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9614 9614
9615 9615 if (un->un_f_cfg_is_atapi == TRUE) {
9616 9616 hdrlen = MODE_HEADER_LENGTH_GRP2;
9617 9617 } else {
9618 9618 hdrlen = MODE_HEADER_LENGTH;
9619 9619 }
9620 9620
9621 9621 /*
9622 9622 * Allocate memory for the retrieved mode page and its headers. Set
9623 9623 * a pointer to the page itself.
9624 9624 */
9625 9625 buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9626 9626 header = kmem_zalloc(buflen, KM_SLEEP);
9627 9627
9628 9628 /* Get the information from the device. */
9629 9629 if (un->un_f_cfg_is_atapi == TRUE) {
9630 9630 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9631 9631 MODEPAGE_CACHING, SD_PATH_DIRECT);
9632 9632 } else {
9633 9633 rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9634 9634 MODEPAGE_CACHING, SD_PATH_DIRECT);
9635 9635 }
9636 9636
9637 9637 if (rval != 0) {
9638 9638 SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9639 9639 "sd_get_write_cache_enabled: Mode Sense Failed\n");
9640 9640 goto mode_sense_failed;
9641 9641 }
9642 9642
9643 9643 /*
9644 9644 * Determine size of Block Descriptors in order to locate
9645 9645 * the mode page data. ATAPI devices return 0, SCSI devices
9646 9646 * should return MODE_BLK_DESC_LENGTH.
9647 9647 */
9648 9648 if (un->un_f_cfg_is_atapi == TRUE) {
9649 9649 struct mode_header_grp2 *mhp;
9650 9650 mhp = (struct mode_header_grp2 *)header;
9651 9651 bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9652 9652 } else {
9653 9653 bd_len = ((struct mode_header *)header)->bdesc_length;
9654 9654 }
9655 9655
9656 9656 if (bd_len > MODE_BLK_DESC_LENGTH) {
9657 9657 /* FMA should make upset complain here */
9658 9658 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9659 9659 "sd_get_write_cache_enabled: Mode Sense returned invalid "
9660 9660 "block descriptor length\n");
9661 9661 rval = EIO;
9662 9662 goto mode_sense_failed;
9663 9663 }
9664 9664
9665 9665 mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9666 9666 if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9667 9667 /* FMA could make upset complain here */
9668 9668 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9669 9669 "sd_get_write_cache_enabled: Mode Sense caching page "
9670 9670 "code mismatch %d\n", mode_caching_page->mode_page.code);
9671 9671 rval = EIO;
9672 9672 goto mode_sense_failed;
9673 9673 }
9674 9674 *is_enabled = mode_caching_page->wce;
9675 9675
9676 9676 mode_sense_failed:
9677 9677 if (rval == 0) {
9678 9678 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9679 9679 } else if (rval == EIO) {
9680 9680 /*
9681 9681 * Some disks do not support mode sense(6), we
9682 9682 * should ignore this kind of error(sense key is
9683 9683 * 0x5 - illegal request).
9684 9684 */
9685 9685 uint8_t *sensep;
9686 9686 int senlen;
9687 9687
9688 9688 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9689 9689 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9690 9690 ssc->ssc_uscsi_cmd->uscsi_rqresid);
9691 9691
9692 9692 if (senlen > 0 &&
9693 9693 scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9694 9694 sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9695 9695 } else {
9696 9696 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9697 9697 }
9698 9698 } else {
9699 9699 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9700 9700 }
9701 9701 kmem_free(header, buflen);
9702 9702 return (rval);
9703 9703 }
9704 9704
9705 9705 /*
9706 9706 * Function: sd_get_nv_sup()
9707 9707 *
9708 9708 * Description: This routine is the driver entry point for
9709 9709 * determining whether non-volatile cache is supported. This
9710 9710 * determination process works as follows:
9711 9711 *
9712 9712 * 1. sd first queries sd.conf on whether
9713 9713 * suppress_cache_flush bit is set for this device.
9714 9714 *
9715 9715 * 2. if not there, then queries the internal disk table.
9716 9716 *
9717 9717 * 3. if either sd.conf or internal disk table specifies
9718 9718 * cache flush be suppressed, we don't bother checking
9719 9719 * NV_SUP bit.
9720 9720 *
9721 9721 * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9722 9722 * the optional INQUIRY VPD page 0x86. If the device
9723 9723 * supports VPD page 0x86, sd examines the NV_SUP
9724 9724 * (non-volatile cache support) bit in the INQUIRY VPD page
9725 9725 * 0x86:
9726 9726 * o If NV_SUP bit is set, sd assumes the device has a
9727 9727 * non-volatile cache and set the
9728 9728 * un_f_sync_nv_supported to TRUE.
9729 9729 * o Otherwise cache is not non-volatile,
9730 9730 * un_f_sync_nv_supported is set to FALSE.
9731 9731 *
9732 9732 * Arguments: un - driver soft state (unit) structure
9733 9733 *
9734 9734 * Return Code:
9735 9735 *
9736 9736 * Context: Kernel Thread
9737 9737 */
9738 9738
9739 9739 static void
9740 9740 sd_get_nv_sup(sd_ssc_t *ssc)
9741 9741 {
9742 9742 int rval = 0;
9743 9743 uchar_t *inq86 = NULL;
9744 9744 size_t inq86_len = MAX_INQUIRY_SIZE;
9745 9745 size_t inq86_resid = 0;
9746 9746 struct dk_callback *dkc;
9747 9747 struct sd_lun *un;
9748 9748
9749 9749 ASSERT(ssc != NULL);
9750 9750 un = ssc->ssc_un;
9751 9751 ASSERT(un != NULL);
9752 9752
9753 9753 mutex_enter(SD_MUTEX(un));
9754 9754
9755 9755 /*
9756 9756 * Be conservative on the device's support of
9757 9757 * SYNC_NV bit: un_f_sync_nv_supported is
9758 9758 * initialized to be false.
9759 9759 */
9760 9760 un->un_f_sync_nv_supported = FALSE;
9761 9761
9762 9762 /*
9763 9763 * If either sd.conf or internal disk table
9764 9764 * specifies cache flush be suppressed, then
9765 9765 * we don't bother checking NV_SUP bit.
9766 9766 */
9767 9767 if (un->un_f_suppress_cache_flush == TRUE) {
9768 9768 mutex_exit(SD_MUTEX(un));
9769 9769 return;
9770 9770 }
9771 9771
9772 9772 if (sd_check_vpd_page_support(ssc) == 0 &&
9773 9773 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9774 9774 mutex_exit(SD_MUTEX(un));
9775 9775 /* collect page 86 data if available */
9776 9776 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9777 9777
9778 9778 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9779 9779 0x01, 0x86, &inq86_resid);
9780 9780
9781 9781 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9782 9782 SD_TRACE(SD_LOG_COMMON, un,
9783 9783 "sd_get_nv_sup: \
9784 9784 successfully get VPD page: %x \
9785 9785 PAGE LENGTH: %x BYTE 6: %x\n",
9786 9786 inq86[1], inq86[3], inq86[6]);
9787 9787
9788 9788 mutex_enter(SD_MUTEX(un));
9789 9789 /*
9790 9790 * check the value of NV_SUP bit: only if the device
9791 9791 * reports NV_SUP bit to be 1, the
9792 9792 * un_f_sync_nv_supported bit will be set to true.
9793 9793 */
9794 9794 if (inq86[6] & SD_VPD_NV_SUP) {
9795 9795 un->un_f_sync_nv_supported = TRUE;
9796 9796 }
9797 9797 mutex_exit(SD_MUTEX(un));
9798 9798 } else if (rval != 0) {
9799 9799 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9800 9800 }
9801 9801
9802 9802 kmem_free(inq86, inq86_len);
9803 9803 } else {
9804 9804 mutex_exit(SD_MUTEX(un));
9805 9805 }
9806 9806
9807 9807 /*
9808 9808 * Send a SYNC CACHE command to check whether
9809 9809 * SYNC_NV bit is supported. This command should have
9810 9810 * un_f_sync_nv_supported set to correct value.
9811 9811 */
9812 9812 mutex_enter(SD_MUTEX(un));
9813 9813 if (un->un_f_sync_nv_supported) {
9814 9814 mutex_exit(SD_MUTEX(un));
9815 9815 dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9816 9816 dkc->dkc_flag = FLUSH_VOLATILE;
9817 9817 (void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9818 9818
9819 9819 /*
9820 9820 * Send a TEST UNIT READY command to the device. This should
9821 9821 * clear any outstanding UNIT ATTENTION that may be present.
9822 9822 */
9823 9823 rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9824 9824 if (rval != 0)
9825 9825 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9826 9826
9827 9827 kmem_free(dkc, sizeof (struct dk_callback));
9828 9828 } else {
9829 9829 mutex_exit(SD_MUTEX(un));
9830 9830 }
9831 9831
9832 9832 SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9833 9833 un_f_suppress_cache_flush is set to %d\n",
9834 9834 un->un_f_suppress_cache_flush);
9835 9835 }
9836 9836
9837 9837 /*
9838 9838 * Function: sd_make_device
9839 9839 *
9840 9840 * Description: Utility routine to return the Solaris device number from
9841 9841 * the data in the device's dev_info structure.
9842 9842 *
9843 9843 * Return Code: The Solaris device number
9844 9844 *
9845 9845 * Context: Any
9846 9846 */
9847 9847
9848 9848 static dev_t
9849 9849 sd_make_device(dev_info_t *devi)
9850 9850 {
9851 9851 return (makedevice(ddi_driver_major(devi),
9852 9852 ddi_get_instance(devi) << SDUNIT_SHIFT));
9853 9853 }
9854 9854
9855 9855
9856 9856 /*
9857 9857 * Function: sd_pm_entry
9858 9858 *
9859 9859 * Description: Called at the start of a new command to manage power
9860 9860 * and busy status of a device. This includes determining whether
9861 9861 * the current power state of the device is sufficient for
9862 9862 * performing the command or whether it must be changed.
9863 9863 * The PM framework is notified appropriately.
9864 9864 * Only with a return status of DDI_SUCCESS will the
9865 9865 * component be busy to the framework.
9866 9866 *
9867 9867 * All callers of sd_pm_entry must check the return status
9868 9868 * and only call sd_pm_exit it it was DDI_SUCCESS. A status
9869 9869 * of DDI_FAILURE indicates the device failed to power up.
9870 9870 * In this case un_pm_count has been adjusted so the result
9871 9871 * on exit is still powered down, ie. count is less than 0.
9872 9872 * Calling sd_pm_exit with this count value hits an ASSERT.
9873 9873 *
9874 9874 * Return Code: DDI_SUCCESS or DDI_FAILURE
9875 9875 *
9876 9876 * Context: Kernel thread context.
9877 9877 */
9878 9878
9879 9879 static int
9880 9880 sd_pm_entry(struct sd_lun *un)
9881 9881 {
9882 9882 int return_status = DDI_SUCCESS;
9883 9883
9884 9884 ASSERT(!mutex_owned(SD_MUTEX(un)));
9885 9885 ASSERT(!mutex_owned(&un->un_pm_mutex));
9886 9886
9887 9887 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9888 9888
9889 9889 if (un->un_f_pm_is_enabled == FALSE) {
9890 9890 SD_TRACE(SD_LOG_IO_PM, un,
9891 9891 "sd_pm_entry: exiting, PM not enabled\n");
9892 9892 return (return_status);
9893 9893 }
9894 9894
9895 9895 /*
9896 9896 * Just increment a counter if PM is enabled. On the transition from
9897 9897 * 0 ==> 1, mark the device as busy. The iodone side will decrement
9898 9898 * the count with each IO and mark the device as idle when the count
9899 9899 * hits 0.
9900 9900 *
9901 9901 * If the count is less than 0 the device is powered down. If a powered
9902 9902 * down device is successfully powered up then the count must be
9903 9903 * incremented to reflect the power up. Note that it'll get incremented
9904 9904 * a second time to become busy.
9905 9905 *
9906 9906 * Because the following has the potential to change the device state
9907 9907 * and must release the un_pm_mutex to do so, only one thread can be
9908 9908 * allowed through at a time.
9909 9909 */
9910 9910
9911 9911 mutex_enter(&un->un_pm_mutex);
9912 9912 while (un->un_pm_busy == TRUE) {
9913 9913 cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9914 9914 }
9915 9915 un->un_pm_busy = TRUE;
9916 9916
9917 9917 if (un->un_pm_count < 1) {
9918 9918
9919 9919 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9920 9920
9921 9921 /*
9922 9922 * Indicate we are now busy so the framework won't attempt to
9923 9923 * power down the device. This call will only fail if either
9924 9924 * we passed a bad component number or the device has no
9925 9925 * components. Neither of these should ever happen.
9926 9926 */
9927 9927 mutex_exit(&un->un_pm_mutex);
9928 9928 return_status = pm_busy_component(SD_DEVINFO(un), 0);
9929 9929 ASSERT(return_status == DDI_SUCCESS);
9930 9930
9931 9931 mutex_enter(&un->un_pm_mutex);
9932 9932
9933 9933 if (un->un_pm_count < 0) {
9934 9934 mutex_exit(&un->un_pm_mutex);
9935 9935
9936 9936 SD_TRACE(SD_LOG_IO_PM, un,
9937 9937 "sd_pm_entry: power up component\n");
9938 9938
9939 9939 /*
9940 9940 * pm_raise_power will cause sdpower to be called
9941 9941 * which brings the device power level to the
9942 9942 * desired state, If successful, un_pm_count and
9943 9943 * un_power_level will be updated appropriately.
9944 9944 */
9945 9945 return_status = pm_raise_power(SD_DEVINFO(un), 0,
9946 9946 SD_PM_STATE_ACTIVE(un));
9947 9947
9948 9948 mutex_enter(&un->un_pm_mutex);
9949 9949
9950 9950 if (return_status != DDI_SUCCESS) {
9951 9951 /*
9952 9952 * Power up failed.
9953 9953 * Idle the device and adjust the count
9954 9954 * so the result on exit is that we're
9955 9955 * still powered down, ie. count is less than 0.
9956 9956 */
9957 9957 SD_TRACE(SD_LOG_IO_PM, un,
9958 9958 "sd_pm_entry: power up failed,"
9959 9959 " idle the component\n");
9960 9960
9961 9961 (void) pm_idle_component(SD_DEVINFO(un), 0);
9962 9962 un->un_pm_count--;
9963 9963 } else {
9964 9964 /*
9965 9965 * Device is powered up, verify the
9966 9966 * count is non-negative.
9967 9967 * This is debug only.
9968 9968 */
9969 9969 ASSERT(un->un_pm_count == 0);
9970 9970 }
9971 9971 }
9972 9972
9973 9973 if (return_status == DDI_SUCCESS) {
9974 9974 /*
9975 9975 * For performance, now that the device has been tagged
9976 9976 * as busy, and it's known to be powered up, update the
9977 9977 * chain types to use jump tables that do not include
9978 9978 * pm. This significantly lowers the overhead and
9979 9979 * therefore improves performance.
9980 9980 */
9981 9981
9982 9982 mutex_exit(&un->un_pm_mutex);
9983 9983 mutex_enter(SD_MUTEX(un));
9984 9984 SD_TRACE(SD_LOG_IO_PM, un,
9985 9985 "sd_pm_entry: changing uscsi_chain_type from %d\n",
9986 9986 un->un_uscsi_chain_type);
9987 9987
9988 9988 if (un->un_f_non_devbsize_supported) {
9989 9989 un->un_buf_chain_type =
9990 9990 SD_CHAIN_INFO_RMMEDIA_NO_PM;
9991 9991 } else {
9992 9992 un->un_buf_chain_type =
9993 9993 SD_CHAIN_INFO_DISK_NO_PM;
9994 9994 }
9995 9995 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9996 9996
9997 9997 SD_TRACE(SD_LOG_IO_PM, un,
9998 9998 " changed uscsi_chain_type to %d\n",
9999 9999 un->un_uscsi_chain_type);
10000 10000 mutex_exit(SD_MUTEX(un));
10001 10001 mutex_enter(&un->un_pm_mutex);
10002 10002
10003 10003 if (un->un_pm_idle_timeid == NULL) {
10004 10004 /* 300 ms. */
10005 10005 un->un_pm_idle_timeid =
10006 10006 timeout(sd_pm_idletimeout_handler, un,
10007 10007 (drv_usectohz((clock_t)300000)));
10008 10008 /*
10009 10009 * Include an extra call to busy which keeps the
10010 10010 * device busy with-respect-to the PM layer
10011 10011 * until the timer fires, at which time it'll
10012 10012 * get the extra idle call.
10013 10013 */
10014 10014 (void) pm_busy_component(SD_DEVINFO(un), 0);
10015 10015 }
10016 10016 }
10017 10017 }
10018 10018 un->un_pm_busy = FALSE;
10019 10019 /* Next... */
10020 10020 cv_signal(&un->un_pm_busy_cv);
10021 10021
10022 10022 un->un_pm_count++;
10023 10023
10024 10024 SD_TRACE(SD_LOG_IO_PM, un,
10025 10025 "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10026 10026
10027 10027 mutex_exit(&un->un_pm_mutex);
10028 10028
10029 10029 return (return_status);
10030 10030 }
10031 10031
10032 10032
10033 10033 /*
10034 10034 * Function: sd_pm_exit
10035 10035 *
10036 10036 * Description: Called at the completion of a command to manage busy
10037 10037 * status for the device. If the device becomes idle the
10038 10038 * PM framework is notified.
10039 10039 *
10040 10040 * Context: Kernel thread context
10041 10041 */
10042 10042
10043 10043 static void
10044 10044 sd_pm_exit(struct sd_lun *un)
10045 10045 {
10046 10046 ASSERT(!mutex_owned(SD_MUTEX(un)));
10047 10047 ASSERT(!mutex_owned(&un->un_pm_mutex));
10048 10048
10049 10049 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10050 10050
10051 10051 /*
10052 10052 * After attach the following flag is only read, so don't
10053 10053 * take the penalty of acquiring a mutex for it.
10054 10054 */
10055 10055 if (un->un_f_pm_is_enabled == TRUE) {
10056 10056
10057 10057 mutex_enter(&un->un_pm_mutex);
10058 10058 un->un_pm_count--;
10059 10059
10060 10060 SD_TRACE(SD_LOG_IO_PM, un,
10061 10061 "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10062 10062
10063 10063 ASSERT(un->un_pm_count >= 0);
10064 10064 if (un->un_pm_count == 0) {
10065 10065 mutex_exit(&un->un_pm_mutex);
10066 10066
10067 10067 SD_TRACE(SD_LOG_IO_PM, un,
10068 10068 "sd_pm_exit: idle component\n");
10069 10069
10070 10070 (void) pm_idle_component(SD_DEVINFO(un), 0);
10071 10071
10072 10072 } else {
10073 10073 mutex_exit(&un->un_pm_mutex);
10074 10074 }
10075 10075 }
10076 10076
10077 10077 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10078 10078 }
10079 10079
10080 10080
10081 10081 /*
10082 10082 * Function: sdopen
10083 10083 *
10084 10084 * Description: Driver's open(9e) entry point function.
10085 10085 *
10086 10086 * Arguments: dev_i - pointer to device number
10087 10087 * flag - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10088 10088 * otyp - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10089 10089 * cred_p - user credential pointer
10090 10090 *
10091 10091 * Return Code: EINVAL
10092 10092 * ENXIO
10093 10093 * EIO
10094 10094 * EROFS
10095 10095 * EBUSY
10096 10096 *
10097 10097 * Context: Kernel thread context
10098 10098 */
10099 10099 /* ARGSUSED */
10100 10100 static int
10101 10101 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10102 10102 {
10103 10103 struct sd_lun *un;
10104 10104 int nodelay;
10105 10105 int part;
10106 10106 uint64_t partmask;
10107 10107 int instance;
10108 10108 dev_t dev;
10109 10109 int rval = EIO;
10110 10110 diskaddr_t nblks = 0;
10111 10111 diskaddr_t label_cap;
10112 10112
10113 10113 /* Validate the open type */
10114 10114 if (otyp >= OTYPCNT) {
10115 10115 return (EINVAL);
10116 10116 }
10117 10117
10118 10118 dev = *dev_p;
10119 10119 instance = SDUNIT(dev);
10120 10120 mutex_enter(&sd_detach_mutex);
10121 10121
10122 10122 /*
10123 10123 * Fail the open if there is no softstate for the instance, or
10124 10124 * if another thread somewhere is trying to detach the instance.
10125 10125 */
10126 10126 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10127 10127 (un->un_detach_count != 0)) {
10128 10128 mutex_exit(&sd_detach_mutex);
10129 10129 /*
10130 10130 * The probe cache only needs to be cleared when open (9e) fails
10131 10131 * with ENXIO (4238046).
10132 10132 */
10133 10133 /*
10134 10134 * un-conditionally clearing probe cache is ok with
10135 10135 * separate sd/ssd binaries
10136 10136 * x86 platform can be an issue with both parallel
10137 10137 * and fibre in 1 binary
10138 10138 */
10139 10139 sd_scsi_clear_probe_cache();
10140 10140 return (ENXIO);
10141 10141 }
10142 10142
10143 10143 /*
10144 10144 * The un_layer_count is to prevent another thread in specfs from
10145 10145 * trying to detach the instance, which can happen when we are
10146 10146 * called from a higher-layer driver instead of thru specfs.
10147 10147 * This will not be needed when DDI provides a layered driver
10148 10148 * interface that allows specfs to know that an instance is in
10149 10149 * use by a layered driver & should not be detached.
10150 10150 *
10151 10151 * Note: the semantics for layered driver opens are exactly one
10152 10152 * close for every open.
10153 10153 */
10154 10154 if (otyp == OTYP_LYR) {
10155 10155 un->un_layer_count++;
10156 10156 }
10157 10157
10158 10158 /*
10159 10159 * Keep a count of the current # of opens in progress. This is because
10160 10160 * some layered drivers try to call us as a regular open. This can
10161 10161 * cause problems that we cannot prevent, however by keeping this count
10162 10162 * we can at least keep our open and detach routines from racing against
10163 10163 * each other under such conditions.
10164 10164 */
10165 10165 un->un_opens_in_progress++;
10166 10166 mutex_exit(&sd_detach_mutex);
10167 10167
10168 10168 nodelay = (flag & (FNDELAY | FNONBLOCK));
10169 10169 part = SDPART(dev);
10170 10170 partmask = 1 << part;
10171 10171
10172 10172 /*
10173 10173 * We use a semaphore here in order to serialize
10174 10174 * open and close requests on the device.
10175 10175 */
10176 10176 sema_p(&un->un_semoclose);
10177 10177
10178 10178 mutex_enter(SD_MUTEX(un));
10179 10179
10180 10180 /*
10181 10181 * All device accesses go thru sdstrategy() where we check
10182 10182 * on suspend status but there could be a scsi_poll command,
10183 10183 * which bypasses sdstrategy(), so we need to check pm
10184 10184 * status.
10185 10185 */
10186 10186
10187 10187 if (!nodelay) {
10188 10188 while ((un->un_state == SD_STATE_SUSPENDED) ||
10189 10189 (un->un_state == SD_STATE_PM_CHANGING)) {
10190 10190 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10191 10191 }
10192 10192
10193 10193 mutex_exit(SD_MUTEX(un));
10194 10194 if (sd_pm_entry(un) != DDI_SUCCESS) {
10195 10195 rval = EIO;
10196 10196 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10197 10197 "sdopen: sd_pm_entry failed\n");
10198 10198 goto open_failed_with_pm;
10199 10199 }
10200 10200 mutex_enter(SD_MUTEX(un));
10201 10201 }
10202 10202
10203 10203 /* check for previous exclusive open */
10204 10204 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10205 10205 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10206 10206 "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10207 10207 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10208 10208
10209 10209 if (un->un_exclopen & (partmask)) {
10210 10210 goto excl_open_fail;
10211 10211 }
10212 10212
10213 10213 if (flag & FEXCL) {
10214 10214 int i;
10215 10215 if (un->un_ocmap.lyropen[part]) {
10216 10216 goto excl_open_fail;
10217 10217 }
10218 10218 for (i = 0; i < (OTYPCNT - 1); i++) {
10219 10219 if (un->un_ocmap.regopen[i] & (partmask)) {
10220 10220 goto excl_open_fail;
10221 10221 }
10222 10222 }
10223 10223 }
10224 10224
10225 10225 /*
10226 10226 * Check the write permission if this is a removable media device,
10227 10227 * NDELAY has not been set, and writable permission is requested.
10228 10228 *
10229 10229 * Note: If NDELAY was set and this is write-protected media the WRITE
10230 10230 * attempt will fail with EIO as part of the I/O processing. This is a
10231 10231 * more permissive implementation that allows the open to succeed and
10232 10232 * WRITE attempts to fail when appropriate.
10233 10233 */
10234 10234 if (un->un_f_chk_wp_open) {
10235 10235 if ((flag & FWRITE) && (!nodelay)) {
10236 10236 mutex_exit(SD_MUTEX(un));
10237 10237 /*
10238 10238 * Defer the check for write permission on writable
10239 10239 * DVD drive till sdstrategy and will not fail open even
10240 10240 * if FWRITE is set as the device can be writable
10241 10241 * depending upon the media and the media can change
10242 10242 * after the call to open().
10243 10243 */
10244 10244 if (un->un_f_dvdram_writable_device == FALSE) {
10245 10245 if (ISCD(un) || sr_check_wp(dev)) {
10246 10246 rval = EROFS;
10247 10247 mutex_enter(SD_MUTEX(un));
10248 10248 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10249 10249 "write to cd or write protected media\n");
10250 10250 goto open_fail;
10251 10251 }
10252 10252 }
10253 10253 mutex_enter(SD_MUTEX(un));
10254 10254 }
10255 10255 }
10256 10256
10257 10257 /*
10258 10258 * If opening in NDELAY/NONBLOCK mode, just return.
10259 10259 * Check if disk is ready and has a valid geometry later.
10260 10260 */
10261 10261 if (!nodelay) {
10262 10262 sd_ssc_t *ssc;
10263 10263
10264 10264 mutex_exit(SD_MUTEX(un));
10265 10265 ssc = sd_ssc_init(un);
10266 10266 rval = sd_ready_and_valid(ssc, part);
10267 10267 sd_ssc_fini(ssc);
10268 10268 mutex_enter(SD_MUTEX(un));
10269 10269 /*
10270 10270 * Fail if device is not ready or if the number of disk
10271 10271 * blocks is zero or negative for non CD devices.
10272 10272 */
10273 10273
10274 10274 nblks = 0;
10275 10275
10276 10276 if (rval == SD_READY_VALID && (!ISCD(un))) {
10277 10277 /* if cmlb_partinfo fails, nblks remains 0 */
10278 10278 mutex_exit(SD_MUTEX(un));
10279 10279 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10280 10280 NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10281 10281 mutex_enter(SD_MUTEX(un));
10282 10282 }
10283 10283
10284 10284 if ((rval != SD_READY_VALID) ||
10285 10285 (!ISCD(un) && nblks <= 0)) {
10286 10286 rval = un->un_f_has_removable_media ? ENXIO : EIO;
10287 10287 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10288 10288 "device not ready or invalid disk block value\n");
10289 10289 goto open_fail;
10290 10290 }
10291 10291 #if defined(__i386) || defined(__amd64)
10292 10292 } else {
10293 10293 uchar_t *cp;
10294 10294 /*
10295 10295 * x86 requires special nodelay handling, so that p0 is
10296 10296 * always defined and accessible.
10297 10297 * Invalidate geometry only if device is not already open.
10298 10298 */
10299 10299 cp = &un->un_ocmap.chkd[0];
10300 10300 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10301 10301 if (*cp != (uchar_t)0) {
10302 10302 break;
10303 10303 }
10304 10304 cp++;
10305 10305 }
10306 10306 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10307 10307 mutex_exit(SD_MUTEX(un));
10308 10308 cmlb_invalidate(un->un_cmlbhandle,
10309 10309 (void *)SD_PATH_DIRECT);
10310 10310 mutex_enter(SD_MUTEX(un));
10311 10311 }
10312 10312
10313 10313 #endif
10314 10314 }
10315 10315
10316 10316 if (otyp == OTYP_LYR) {
10317 10317 un->un_ocmap.lyropen[part]++;
10318 10318 } else {
10319 10319 un->un_ocmap.regopen[otyp] |= partmask;
10320 10320 }
10321 10321
10322 10322 /* Set up open and exclusive open flags */
10323 10323 if (flag & FEXCL) {
10324 10324 un->un_exclopen |= (partmask);
10325 10325 }
10326 10326
10327 10327 /*
10328 10328 * If the lun is EFI labeled and lun capacity is greater than the
10329 10329 * capacity contained in the label, log a sys-event to notify the
10330 10330 * interested module.
10331 10331 * To avoid an infinite loop of logging sys-event, we only log the
10332 10332 * event when the lun is not opened in NDELAY mode. The event handler
10333 10333 * should open the lun in NDELAY mode.
10334 10334 */
10335 10335 if (!nodelay) {
10336 10336 mutex_exit(SD_MUTEX(un));
10337 10337 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10338 10338 (void*)SD_PATH_DIRECT) == 0) {
10339 10339 mutex_enter(SD_MUTEX(un));
10340 10340 if (un->un_f_blockcount_is_valid &&
10341 10341 un->un_blockcount > label_cap &&
10342 10342 un->un_f_expnevent == B_FALSE) {
10343 10343 un->un_f_expnevent = B_TRUE;
10344 10344 mutex_exit(SD_MUTEX(un));
10345 10345 sd_log_lun_expansion_event(un,
10346 10346 (nodelay ? KM_NOSLEEP : KM_SLEEP));
10347 10347 mutex_enter(SD_MUTEX(un));
10348 10348 }
10349 10349 } else {
10350 10350 mutex_enter(SD_MUTEX(un));
10351 10351 }
10352 10352 }
10353 10353
10354 10354 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10355 10355 "open of part %d type %d\n", part, otyp);
10356 10356
10357 10357 mutex_exit(SD_MUTEX(un));
10358 10358 if (!nodelay) {
10359 10359 sd_pm_exit(un);
10360 10360 }
10361 10361
10362 10362 sema_v(&un->un_semoclose);
10363 10363
10364 10364 mutex_enter(&sd_detach_mutex);
10365 10365 un->un_opens_in_progress--;
10366 10366 mutex_exit(&sd_detach_mutex);
10367 10367
10368 10368 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10369 10369 return (DDI_SUCCESS);
10370 10370
10371 10371 excl_open_fail:
10372 10372 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10373 10373 rval = EBUSY;
10374 10374
10375 10375 open_fail:
10376 10376 mutex_exit(SD_MUTEX(un));
10377 10377
10378 10378 /*
10379 10379 * On a failed open we must exit the pm management.
10380 10380 */
10381 10381 if (!nodelay) {
10382 10382 sd_pm_exit(un);
10383 10383 }
10384 10384 open_failed_with_pm:
10385 10385 sema_v(&un->un_semoclose);
10386 10386
10387 10387 mutex_enter(&sd_detach_mutex);
10388 10388 un->un_opens_in_progress--;
10389 10389 if (otyp == OTYP_LYR) {
10390 10390 un->un_layer_count--;
10391 10391 }
10392 10392 mutex_exit(&sd_detach_mutex);
10393 10393
10394 10394 return (rval);
10395 10395 }
10396 10396
10397 10397
10398 10398 /*
10399 10399 * Function: sdclose
10400 10400 *
10401 10401 * Description: Driver's close(9e) entry point function.
10402 10402 *
10403 10403 * Arguments: dev - device number
10404 10404 * flag - file status flag, informational only
10405 10405 * otyp - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10406 10406 * cred_p - user credential pointer
10407 10407 *
10408 10408 * Return Code: ENXIO
10409 10409 *
10410 10410 * Context: Kernel thread context
10411 10411 */
10412 10412 /* ARGSUSED */
10413 10413 static int
10414 10414 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10415 10415 {
10416 10416 struct sd_lun *un;
10417 10417 uchar_t *cp;
10418 10418 int part;
10419 10419 int nodelay;
10420 10420 int rval = 0;
10421 10421
10422 10422 /* Validate the open type */
10423 10423 if (otyp >= OTYPCNT) {
10424 10424 return (ENXIO);
10425 10425 }
10426 10426
10427 10427 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10428 10428 return (ENXIO);
10429 10429 }
10430 10430
10431 10431 part = SDPART(dev);
10432 10432 nodelay = flag & (FNDELAY | FNONBLOCK);
10433 10433
10434 10434 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10435 10435 "sdclose: close of part %d type %d\n", part, otyp);
10436 10436
10437 10437 /*
10438 10438 * We use a semaphore here in order to serialize
10439 10439 * open and close requests on the device.
10440 10440 */
10441 10441 sema_p(&un->un_semoclose);
10442 10442
10443 10443 mutex_enter(SD_MUTEX(un));
10444 10444
10445 10445 /* Don't proceed if power is being changed. */
10446 10446 while (un->un_state == SD_STATE_PM_CHANGING) {
10447 10447 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10448 10448 }
10449 10449
10450 10450 if (un->un_exclopen & (1 << part)) {
10451 10451 un->un_exclopen &= ~(1 << part);
10452 10452 }
10453 10453
10454 10454 /* Update the open partition map */
10455 10455 if (otyp == OTYP_LYR) {
10456 10456 un->un_ocmap.lyropen[part] -= 1;
10457 10457 } else {
10458 10458 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10459 10459 }
10460 10460
10461 10461 cp = &un->un_ocmap.chkd[0];
10462 10462 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10463 10463 if (*cp != NULL) {
10464 10464 break;
10465 10465 }
10466 10466 cp++;
10467 10467 }
10468 10468
10469 10469 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10470 10470 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10471 10471
10472 10472 /*
10473 10473 * We avoid persistance upon the last close, and set
10474 10474 * the throttle back to the maximum.
10475 10475 */
10476 10476 un->un_throttle = un->un_saved_throttle;
10477 10477
10478 10478 if (un->un_state == SD_STATE_OFFLINE) {
10479 10479 if (un->un_f_is_fibre == FALSE) {
10480 10480 scsi_log(SD_DEVINFO(un), sd_label,
10481 10481 CE_WARN, "offline\n");
10482 10482 }
10483 10483 mutex_exit(SD_MUTEX(un));
10484 10484 cmlb_invalidate(un->un_cmlbhandle,
10485 10485 (void *)SD_PATH_DIRECT);
10486 10486 mutex_enter(SD_MUTEX(un));
10487 10487
10488 10488 } else {
10489 10489 /*
10490 10490 * Flush any outstanding writes in NVRAM cache.
10491 10491 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10492 10492 * cmd, it may not work for non-Pluto devices.
10493 10493 * SYNCHRONIZE CACHE is not required for removables,
10494 10494 * except DVD-RAM drives.
10495 10495 *
10496 10496 * Also note: because SYNCHRONIZE CACHE is currently
10497 10497 * the only command issued here that requires the
10498 10498 * drive be powered up, only do the power up before
10499 10499 * sending the Sync Cache command. If additional
10500 10500 * commands are added which require a powered up
10501 10501 * drive, the following sequence may have to change.
10502 10502 *
10503 10503 * And finally, note that parallel SCSI on SPARC
10504 10504 * only issues a Sync Cache to DVD-RAM, a newly
10505 10505 * supported device.
10506 10506 */
10507 10507 #if defined(__i386) || defined(__amd64)
10508 10508 if ((un->un_f_sync_cache_supported &&
10509 10509 un->un_f_sync_cache_required) ||
10510 10510 un->un_f_dvdram_writable_device == TRUE) {
10511 10511 #else
10512 10512 if (un->un_f_dvdram_writable_device == TRUE) {
10513 10513 #endif
10514 10514 mutex_exit(SD_MUTEX(un));
10515 10515 if (sd_pm_entry(un) == DDI_SUCCESS) {
10516 10516 rval =
10517 10517 sd_send_scsi_SYNCHRONIZE_CACHE(un,
10518 10518 NULL);
10519 10519 /* ignore error if not supported */
10520 10520 if (rval == ENOTSUP) {
10521 10521 rval = 0;
10522 10522 } else if (rval != 0) {
10523 10523 rval = EIO;
10524 10524 }
10525 10525 sd_pm_exit(un);
10526 10526 } else {
10527 10527 rval = EIO;
10528 10528 }
10529 10529 mutex_enter(SD_MUTEX(un));
10530 10530 }
10531 10531
10532 10532 /*
10533 10533 * For devices which supports DOOR_LOCK, send an ALLOW
10534 10534 * MEDIA REMOVAL command, but don't get upset if it
10535 10535 * fails. We need to raise the power of the drive before
10536 10536 * we can call sd_send_scsi_DOORLOCK()
10537 10537 */
10538 10538 if (un->un_f_doorlock_supported) {
10539 10539 mutex_exit(SD_MUTEX(un));
10540 10540 if (sd_pm_entry(un) == DDI_SUCCESS) {
10541 10541 sd_ssc_t *ssc;
10542 10542
10543 10543 ssc = sd_ssc_init(un);
10544 10544 rval = sd_send_scsi_DOORLOCK(ssc,
10545 10545 SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10546 10546 if (rval != 0)
10547 10547 sd_ssc_assessment(ssc,
10548 10548 SD_FMT_IGNORE);
10549 10549 sd_ssc_fini(ssc);
10550 10550
10551 10551 sd_pm_exit(un);
10552 10552 if (ISCD(un) && (rval != 0) &&
10553 10553 (nodelay != 0)) {
10554 10554 rval = ENXIO;
10555 10555 }
10556 10556 } else {
10557 10557 rval = EIO;
10558 10558 }
10559 10559 mutex_enter(SD_MUTEX(un));
10560 10560 }
10561 10561
10562 10562 /*
10563 10563 * If a device has removable media, invalidate all
10564 10564 * parameters related to media, such as geometry,
10565 10565 * blocksize, and blockcount.
10566 10566 */
10567 10567 if (un->un_f_has_removable_media) {
10568 10568 sr_ejected(un);
10569 10569 }
10570 10570
10571 10571 /*
10572 10572 * Destroy the cache (if it exists) which was
10573 10573 * allocated for the write maps since this is
10574 10574 * the last close for this media.
10575 10575 */
10576 10576 if (un->un_wm_cache) {
10577 10577 /*
10578 10578 * Check if there are pending commands.
10579 10579 * and if there are give a warning and
10580 10580 * do not destroy the cache.
10581 10581 */
10582 10582 if (un->un_ncmds_in_driver > 0) {
10583 10583 scsi_log(SD_DEVINFO(un),
10584 10584 sd_label, CE_WARN,
10585 10585 "Unable to clean up memory "
10586 10586 "because of pending I/O\n");
10587 10587 } else {
10588 10588 kmem_cache_destroy(
10589 10589 un->un_wm_cache);
10590 10590 un->un_wm_cache = NULL;
10591 10591 }
10592 10592 }
10593 10593 }
10594 10594 }
10595 10595
10596 10596 mutex_exit(SD_MUTEX(un));
10597 10597 sema_v(&un->un_semoclose);
10598 10598
10599 10599 if (otyp == OTYP_LYR) {
10600 10600 mutex_enter(&sd_detach_mutex);
10601 10601 /*
10602 10602 * The detach routine may run when the layer count
10603 10603 * drops to zero.
10604 10604 */
10605 10605 un->un_layer_count--;
10606 10606 mutex_exit(&sd_detach_mutex);
10607 10607 }
10608 10608
10609 10609 return (rval);
10610 10610 }
10611 10611
10612 10612
10613 10613 /*
10614 10614 * Function: sd_ready_and_valid
10615 10615 *
10616 10616 * Description: Test if device is ready and has a valid geometry.
10617 10617 *
10618 10618 * Arguments: ssc - sd_ssc_t will contain un
10619 10619 * un - driver soft state (unit) structure
10620 10620 *
10621 10621 * Return Code: SD_READY_VALID ready and valid label
10622 10622 * SD_NOT_READY_VALID not ready, no label
10623 10623 * SD_RESERVED_BY_OTHERS reservation conflict
10624 10624 *
10625 10625 * Context: Never called at interrupt context.
10626 10626 */
10627 10627
10628 10628 static int
10629 10629 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10630 10630 {
10631 10631 struct sd_errstats *stp;
10632 10632 uint64_t capacity;
10633 10633 uint_t lbasize;
10634 10634 int rval = SD_READY_VALID;
10635 10635 char name_str[48];
10636 10636 boolean_t is_valid;
10637 10637 struct sd_lun *un;
10638 10638 int status;
10639 10639
10640 10640 ASSERT(ssc != NULL);
10641 10641 un = ssc->ssc_un;
10642 10642 ASSERT(un != NULL);
10643 10643 ASSERT(!mutex_owned(SD_MUTEX(un)));
10644 10644
10645 10645 mutex_enter(SD_MUTEX(un));
10646 10646 /*
10647 10647 * If a device has removable media, we must check if media is
10648 10648 * ready when checking if this device is ready and valid.
10649 10649 */
10650 10650 if (un->un_f_has_removable_media) {
10651 10651 mutex_exit(SD_MUTEX(un));
10652 10652 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10653 10653
10654 10654 if (status != 0) {
10655 10655 rval = SD_NOT_READY_VALID;
10656 10656 mutex_enter(SD_MUTEX(un));
10657 10657
10658 10658 /* Ignore all failed status for removalbe media */
10659 10659 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10660 10660
10661 10661 goto done;
10662 10662 }
10663 10663
10664 10664 is_valid = SD_IS_VALID_LABEL(un);
10665 10665 mutex_enter(SD_MUTEX(un));
10666 10666 if (!is_valid ||
10667 10667 (un->un_f_blockcount_is_valid == FALSE) ||
10668 10668 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10669 10669
10670 10670 /* capacity has to be read every open. */
10671 10671 mutex_exit(SD_MUTEX(un));
10672 10672 status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10673 10673 &lbasize, SD_PATH_DIRECT);
10674 10674
10675 10675 if (status != 0) {
10676 10676 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10677 10677
10678 10678 cmlb_invalidate(un->un_cmlbhandle,
10679 10679 (void *)SD_PATH_DIRECT);
10680 10680 mutex_enter(SD_MUTEX(un));
10681 10681 rval = SD_NOT_READY_VALID;
10682 10682
10683 10683 goto done;
10684 10684 } else {
10685 10685 mutex_enter(SD_MUTEX(un));
10686 10686 sd_update_block_info(un, lbasize, capacity);
10687 10687 }
10688 10688 }
10689 10689
10690 10690 /*
10691 10691 * Check if the media in the device is writable or not.
10692 10692 */
10693 10693 if (!is_valid && ISCD(un)) {
10694 10694 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10695 10695 }
10696 10696
10697 10697 } else {
10698 10698 /*
10699 10699 * Do a test unit ready to clear any unit attention from non-cd
10700 10700 * devices.
10701 10701 */
10702 10702 mutex_exit(SD_MUTEX(un));
10703 10703
10704 10704 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10705 10705 if (status != 0) {
10706 10706 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10707 10707 }
10708 10708
10709 10709 mutex_enter(SD_MUTEX(un));
10710 10710 }
10711 10711
10712 10712
10713 10713 /*
10714 10714 * If this is a non 512 block device, allocate space for
10715 10715 * the wmap cache. This is being done here since every time
10716 10716 * a media is changed this routine will be called and the
10717 10717 * block size is a function of media rather than device.
10718 10718 */
10719 10719 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10720 10720 un->un_f_non_devbsize_supported) &&
10721 10721 un->un_tgt_blocksize != DEV_BSIZE) ||
10722 10722 un->un_f_enable_rmw) {
10723 10723 if (!(un->un_wm_cache)) {
10724 10724 (void) snprintf(name_str, sizeof (name_str),
10725 10725 "%s%d_cache",
10726 10726 ddi_driver_name(SD_DEVINFO(un)),
10727 10727 ddi_get_instance(SD_DEVINFO(un)));
10728 10728 un->un_wm_cache = kmem_cache_create(
10729 10729 name_str, sizeof (struct sd_w_map),
10730 10730 8, sd_wm_cache_constructor,
10731 10731 sd_wm_cache_destructor, NULL,
10732 10732 (void *)un, NULL, 0);
10733 10733 if (!(un->un_wm_cache)) {
10734 10734 rval = ENOMEM;
10735 10735 goto done;
10736 10736 }
10737 10737 }
10738 10738 }
10739 10739
10740 10740 if (un->un_state == SD_STATE_NORMAL) {
10741 10741 /*
10742 10742 * If the target is not yet ready here (defined by a TUR
10743 10743 * failure), invalidate the geometry and print an 'offline'
10744 10744 * message. This is a legacy message, as the state of the
10745 10745 * target is not actually changed to SD_STATE_OFFLINE.
10746 10746 *
10747 10747 * If the TUR fails for EACCES (Reservation Conflict),
10748 10748 * SD_RESERVED_BY_OTHERS will be returned to indicate
10749 10749 * reservation conflict. If the TUR fails for other
10750 10750 * reasons, SD_NOT_READY_VALID will be returned.
10751 10751 */
10752 10752 int err;
10753 10753
10754 10754 mutex_exit(SD_MUTEX(un));
10755 10755 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10756 10756 mutex_enter(SD_MUTEX(un));
10757 10757
10758 10758 if (err != 0) {
10759 10759 mutex_exit(SD_MUTEX(un));
10760 10760 cmlb_invalidate(un->un_cmlbhandle,
10761 10761 (void *)SD_PATH_DIRECT);
10762 10762 mutex_enter(SD_MUTEX(un));
10763 10763 if (err == EACCES) {
10764 10764 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10765 10765 "reservation conflict\n");
10766 10766 rval = SD_RESERVED_BY_OTHERS;
10767 10767 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10768 10768 } else {
10769 10769 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10770 10770 "drive offline\n");
10771 10771 rval = SD_NOT_READY_VALID;
10772 10772 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10773 10773 }
10774 10774 goto done;
10775 10775 }
10776 10776 }
10777 10777
10778 10778 if (un->un_f_format_in_progress == FALSE) {
10779 10779 mutex_exit(SD_MUTEX(un));
10780 10780
10781 10781 (void) cmlb_validate(un->un_cmlbhandle, 0,
10782 10782 (void *)SD_PATH_DIRECT);
10783 10783 if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10784 10784 NULL, (void *) SD_PATH_DIRECT) != 0) {
10785 10785 rval = SD_NOT_READY_VALID;
10786 10786 mutex_enter(SD_MUTEX(un));
10787 10787
10788 10788 goto done;
10789 10789 }
10790 10790 if (un->un_f_pkstats_enabled) {
10791 10791 sd_set_pstats(un);
10792 10792 SD_TRACE(SD_LOG_IO_PARTITION, un,
10793 10793 "sd_ready_and_valid: un:0x%p pstats created and "
10794 10794 "set\n", un);
10795 10795 }
10796 10796 mutex_enter(SD_MUTEX(un));
10797 10797 }
10798 10798
10799 10799 /*
10800 10800 * If this device supports DOOR_LOCK command, try and send
10801 10801 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10802 10802 * if it fails. For a CD, however, it is an error
10803 10803 */
10804 10804 if (un->un_f_doorlock_supported) {
10805 10805 mutex_exit(SD_MUTEX(un));
10806 10806 status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10807 10807 SD_PATH_DIRECT);
10808 10808
10809 10809 if ((status != 0) && ISCD(un)) {
10810 10810 rval = SD_NOT_READY_VALID;
10811 10811 mutex_enter(SD_MUTEX(un));
10812 10812
10813 10813 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10814 10814
10815 10815 goto done;
10816 10816 } else if (status != 0)
10817 10817 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10818 10818 mutex_enter(SD_MUTEX(un));
10819 10819 }
10820 10820
10821 10821 /* The state has changed, inform the media watch routines */
10822 10822 un->un_mediastate = DKIO_INSERTED;
10823 10823 cv_broadcast(&un->un_state_cv);
10824 10824 rval = SD_READY_VALID;
10825 10825
10826 10826 done:
10827 10827
10828 10828 /*
10829 10829 * Initialize the capacity kstat value, if no media previously
10830 10830 * (capacity kstat is 0) and a media has been inserted
10831 10831 * (un_blockcount > 0).
10832 10832 */
10833 10833 if (un->un_errstats != NULL) {
10834 10834 stp = (struct sd_errstats *)un->un_errstats->ks_data;
10835 10835 if ((stp->sd_capacity.value.ui64 == 0) &&
10836 10836 (un->un_f_blockcount_is_valid == TRUE)) {
10837 10837 stp->sd_capacity.value.ui64 =
10838 10838 (uint64_t)((uint64_t)un->un_blockcount *
10839 10839 un->un_sys_blocksize);
10840 10840 }
10841 10841 }
10842 10842
10843 10843 mutex_exit(SD_MUTEX(un));
10844 10844 return (rval);
10845 10845 }
10846 10846
10847 10847
10848 10848 /*
10849 10849 * Function: sdmin
10850 10850 *
10851 10851 * Description: Routine to limit the size of a data transfer. Used in
10852 10852 * conjunction with physio(9F).
10853 10853 *
10854 10854 * Arguments: bp - pointer to the indicated buf(9S) struct.
10855 10855 *
10856 10856 * Context: Kernel thread context.
10857 10857 */
10858 10858
10859 10859 static void
10860 10860 sdmin(struct buf *bp)
10861 10861 {
10862 10862 struct sd_lun *un;
10863 10863 int instance;
10864 10864
10865 10865 instance = SDUNIT(bp->b_edev);
10866 10866
10867 10867 un = ddi_get_soft_state(sd_state, instance);
10868 10868 ASSERT(un != NULL);
10869 10869
10870 10870 /*
10871 10871 * We depend on buf breakup to restrict
10872 10872 * IO size if it is enabled.
10873 10873 */
10874 10874 if (un->un_buf_breakup_supported) {
10875 10875 return;
10876 10876 }
10877 10877
10878 10878 if (bp->b_bcount > un->un_max_xfer_size) {
10879 10879 bp->b_bcount = un->un_max_xfer_size;
10880 10880 }
10881 10881 }
10882 10882
10883 10883
10884 10884 /*
10885 10885 * Function: sdread
10886 10886 *
10887 10887 * Description: Driver's read(9e) entry point function.
10888 10888 *
10889 10889 * Arguments: dev - device number
10890 10890 * uio - structure pointer describing where data is to be stored
10891 10891 * in user's space
10892 10892 * cred_p - user credential pointer
10893 10893 *
10894 10894 * Return Code: ENXIO
10895 10895 * EIO
10896 10896 * EINVAL
10897 10897 * value returned by physio
10898 10898 *
10899 10899 * Context: Kernel thread context.
10900 10900 */
10901 10901 /* ARGSUSED */
10902 10902 static int
10903 10903 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10904 10904 {
10905 10905 struct sd_lun *un = NULL;
10906 10906 int secmask;
10907 10907 int err = 0;
10908 10908 sd_ssc_t *ssc;
10909 10909
10910 10910 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10911 10911 return (ENXIO);
10912 10912 }
10913 10913
10914 10914 ASSERT(!mutex_owned(SD_MUTEX(un)));
10915 10915
10916 10916
10917 10917 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10918 10918 mutex_enter(SD_MUTEX(un));
10919 10919 /*
10920 10920 * Because the call to sd_ready_and_valid will issue I/O we
10921 10921 * must wait here if either the device is suspended or
10922 10922 * if it's power level is changing.
10923 10923 */
10924 10924 while ((un->un_state == SD_STATE_SUSPENDED) ||
10925 10925 (un->un_state == SD_STATE_PM_CHANGING)) {
10926 10926 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10927 10927 }
10928 10928 un->un_ncmds_in_driver++;
10929 10929 mutex_exit(SD_MUTEX(un));
10930 10930
10931 10931 /* Initialize sd_ssc_t for internal uscsi commands */
10932 10932 ssc = sd_ssc_init(un);
10933 10933 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10934 10934 err = EIO;
10935 10935 } else {
10936 10936 err = 0;
10937 10937 }
10938 10938 sd_ssc_fini(ssc);
10939 10939
10940 10940 mutex_enter(SD_MUTEX(un));
10941 10941 un->un_ncmds_in_driver--;
10942 10942 ASSERT(un->un_ncmds_in_driver >= 0);
10943 10943 mutex_exit(SD_MUTEX(un));
10944 10944 if (err != 0)
10945 10945 return (err);
10946 10946 }
10947 10947
10948 10948 /*
10949 10949 * Read requests are restricted to multiples of the system block size.
10950 10950 */
10951 10951 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10952 10952 !un->un_f_enable_rmw)
10953 10953 secmask = un->un_tgt_blocksize - 1;
10954 10954 else
10955 10955 secmask = DEV_BSIZE - 1;
10956 10956
10957 10957 if (uio->uio_loffset & ((offset_t)(secmask))) {
10958 10958 SD_ERROR(SD_LOG_READ_WRITE, un,
10959 10959 "sdread: file offset not modulo %d\n",
10960 10960 secmask + 1);
10961 10961 err = EINVAL;
10962 10962 } else if (uio->uio_iov->iov_len & (secmask)) {
10963 10963 SD_ERROR(SD_LOG_READ_WRITE, un,
10964 10964 "sdread: transfer length not modulo %d\n",
10965 10965 secmask + 1);
10966 10966 err = EINVAL;
10967 10967 } else {
10968 10968 err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10969 10969 }
10970 10970
10971 10971 return (err);
10972 10972 }
10973 10973
10974 10974
10975 10975 /*
10976 10976 * Function: sdwrite
10977 10977 *
10978 10978 * Description: Driver's write(9e) entry point function.
10979 10979 *
10980 10980 * Arguments: dev - device number
10981 10981 * uio - structure pointer describing where data is stored in
10982 10982 * user's space
10983 10983 * cred_p - user credential pointer
10984 10984 *
10985 10985 * Return Code: ENXIO
10986 10986 * EIO
10987 10987 * EINVAL
10988 10988 * value returned by physio
10989 10989 *
10990 10990 * Context: Kernel thread context.
10991 10991 */
10992 10992 /* ARGSUSED */
10993 10993 static int
10994 10994 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10995 10995 {
10996 10996 struct sd_lun *un = NULL;
10997 10997 int secmask;
10998 10998 int err = 0;
10999 10999 sd_ssc_t *ssc;
11000 11000
11001 11001 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11002 11002 return (ENXIO);
11003 11003 }
11004 11004
11005 11005 ASSERT(!mutex_owned(SD_MUTEX(un)));
11006 11006
11007 11007 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11008 11008 mutex_enter(SD_MUTEX(un));
11009 11009 /*
11010 11010 * Because the call to sd_ready_and_valid will issue I/O we
11011 11011 * must wait here if either the device is suspended or
11012 11012 * if it's power level is changing.
11013 11013 */
11014 11014 while ((un->un_state == SD_STATE_SUSPENDED) ||
11015 11015 (un->un_state == SD_STATE_PM_CHANGING)) {
11016 11016 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11017 11017 }
11018 11018 un->un_ncmds_in_driver++;
11019 11019 mutex_exit(SD_MUTEX(un));
11020 11020
11021 11021 /* Initialize sd_ssc_t for internal uscsi commands */
11022 11022 ssc = sd_ssc_init(un);
11023 11023 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11024 11024 err = EIO;
11025 11025 } else {
11026 11026 err = 0;
11027 11027 }
11028 11028 sd_ssc_fini(ssc);
11029 11029
11030 11030 mutex_enter(SD_MUTEX(un));
11031 11031 un->un_ncmds_in_driver--;
11032 11032 ASSERT(un->un_ncmds_in_driver >= 0);
11033 11033 mutex_exit(SD_MUTEX(un));
11034 11034 if (err != 0)
11035 11035 return (err);
11036 11036 }
11037 11037
11038 11038 /*
11039 11039 * Write requests are restricted to multiples of the system block size.
11040 11040 */
11041 11041 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11042 11042 !un->un_f_enable_rmw)
11043 11043 secmask = un->un_tgt_blocksize - 1;
11044 11044 else
11045 11045 secmask = DEV_BSIZE - 1;
11046 11046
11047 11047 if (uio->uio_loffset & ((offset_t)(secmask))) {
11048 11048 SD_ERROR(SD_LOG_READ_WRITE, un,
11049 11049 "sdwrite: file offset not modulo %d\n",
11050 11050 secmask + 1);
11051 11051 err = EINVAL;
11052 11052 } else if (uio->uio_iov->iov_len & (secmask)) {
11053 11053 SD_ERROR(SD_LOG_READ_WRITE, un,
11054 11054 "sdwrite: transfer length not modulo %d\n",
11055 11055 secmask + 1);
11056 11056 err = EINVAL;
11057 11057 } else {
11058 11058 err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11059 11059 }
11060 11060
11061 11061 return (err);
11062 11062 }
11063 11063
11064 11064
11065 11065 /*
11066 11066 * Function: sdaread
11067 11067 *
11068 11068 * Description: Driver's aread(9e) entry point function.
11069 11069 *
11070 11070 * Arguments: dev - device number
11071 11071 * aio - structure pointer describing where data is to be stored
11072 11072 * cred_p - user credential pointer
11073 11073 *
11074 11074 * Return Code: ENXIO
11075 11075 * EIO
11076 11076 * EINVAL
11077 11077 * value returned by aphysio
11078 11078 *
11079 11079 * Context: Kernel thread context.
11080 11080 */
11081 11081 /* ARGSUSED */
11082 11082 static int
11083 11083 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11084 11084 {
11085 11085 struct sd_lun *un = NULL;
11086 11086 struct uio *uio = aio->aio_uio;
11087 11087 int secmask;
11088 11088 int err = 0;
11089 11089 sd_ssc_t *ssc;
11090 11090
11091 11091 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11092 11092 return (ENXIO);
11093 11093 }
11094 11094
11095 11095 ASSERT(!mutex_owned(SD_MUTEX(un)));
11096 11096
11097 11097 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11098 11098 mutex_enter(SD_MUTEX(un));
11099 11099 /*
11100 11100 * Because the call to sd_ready_and_valid will issue I/O we
11101 11101 * must wait here if either the device is suspended or
11102 11102 * if it's power level is changing.
11103 11103 */
11104 11104 while ((un->un_state == SD_STATE_SUSPENDED) ||
11105 11105 (un->un_state == SD_STATE_PM_CHANGING)) {
11106 11106 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11107 11107 }
11108 11108 un->un_ncmds_in_driver++;
11109 11109 mutex_exit(SD_MUTEX(un));
11110 11110
11111 11111 /* Initialize sd_ssc_t for internal uscsi commands */
11112 11112 ssc = sd_ssc_init(un);
11113 11113 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11114 11114 err = EIO;
11115 11115 } else {
11116 11116 err = 0;
11117 11117 }
11118 11118 sd_ssc_fini(ssc);
11119 11119
11120 11120 mutex_enter(SD_MUTEX(un));
11121 11121 un->un_ncmds_in_driver--;
11122 11122 ASSERT(un->un_ncmds_in_driver >= 0);
11123 11123 mutex_exit(SD_MUTEX(un));
11124 11124 if (err != 0)
11125 11125 return (err);
11126 11126 }
11127 11127
11128 11128 /*
11129 11129 * Read requests are restricted to multiples of the system block size.
11130 11130 */
11131 11131 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11132 11132 !un->un_f_enable_rmw)
11133 11133 secmask = un->un_tgt_blocksize - 1;
11134 11134 else
11135 11135 secmask = DEV_BSIZE - 1;
11136 11136
11137 11137 if (uio->uio_loffset & ((offset_t)(secmask))) {
11138 11138 SD_ERROR(SD_LOG_READ_WRITE, un,
11139 11139 "sdaread: file offset not modulo %d\n",
11140 11140 secmask + 1);
11141 11141 err = EINVAL;
11142 11142 } else if (uio->uio_iov->iov_len & (secmask)) {
11143 11143 SD_ERROR(SD_LOG_READ_WRITE, un,
11144 11144 "sdaread: transfer length not modulo %d\n",
11145 11145 secmask + 1);
11146 11146 err = EINVAL;
11147 11147 } else {
11148 11148 err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11149 11149 }
11150 11150
11151 11151 return (err);
11152 11152 }
11153 11153
11154 11154
11155 11155 /*
11156 11156 * Function: sdawrite
11157 11157 *
11158 11158 * Description: Driver's awrite(9e) entry point function.
11159 11159 *
11160 11160 * Arguments: dev - device number
11161 11161 * aio - structure pointer describing where data is stored
11162 11162 * cred_p - user credential pointer
11163 11163 *
11164 11164 * Return Code: ENXIO
11165 11165 * EIO
11166 11166 * EINVAL
11167 11167 * value returned by aphysio
11168 11168 *
11169 11169 * Context: Kernel thread context.
11170 11170 */
11171 11171 /* ARGSUSED */
11172 11172 static int
11173 11173 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11174 11174 {
11175 11175 struct sd_lun *un = NULL;
11176 11176 struct uio *uio = aio->aio_uio;
11177 11177 int secmask;
11178 11178 int err = 0;
11179 11179 sd_ssc_t *ssc;
11180 11180
11181 11181 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11182 11182 return (ENXIO);
11183 11183 }
11184 11184
11185 11185 ASSERT(!mutex_owned(SD_MUTEX(un)));
11186 11186
11187 11187 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11188 11188 mutex_enter(SD_MUTEX(un));
11189 11189 /*
11190 11190 * Because the call to sd_ready_and_valid will issue I/O we
11191 11191 * must wait here if either the device is suspended or
11192 11192 * if it's power level is changing.
11193 11193 */
11194 11194 while ((un->un_state == SD_STATE_SUSPENDED) ||
11195 11195 (un->un_state == SD_STATE_PM_CHANGING)) {
11196 11196 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11197 11197 }
11198 11198 un->un_ncmds_in_driver++;
11199 11199 mutex_exit(SD_MUTEX(un));
11200 11200
11201 11201 /* Initialize sd_ssc_t for internal uscsi commands */
11202 11202 ssc = sd_ssc_init(un);
11203 11203 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11204 11204 err = EIO;
11205 11205 } else {
11206 11206 err = 0;
11207 11207 }
11208 11208 sd_ssc_fini(ssc);
11209 11209
11210 11210 mutex_enter(SD_MUTEX(un));
11211 11211 un->un_ncmds_in_driver--;
11212 11212 ASSERT(un->un_ncmds_in_driver >= 0);
11213 11213 mutex_exit(SD_MUTEX(un));
11214 11214 if (err != 0)
11215 11215 return (err);
11216 11216 }
11217 11217
11218 11218 /*
11219 11219 * Write requests are restricted to multiples of the system block size.
11220 11220 */
11221 11221 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11222 11222 !un->un_f_enable_rmw)
11223 11223 secmask = un->un_tgt_blocksize - 1;
11224 11224 else
11225 11225 secmask = DEV_BSIZE - 1;
11226 11226
11227 11227 if (uio->uio_loffset & ((offset_t)(secmask))) {
11228 11228 SD_ERROR(SD_LOG_READ_WRITE, un,
11229 11229 "sdawrite: file offset not modulo %d\n",
11230 11230 secmask + 1);
11231 11231 err = EINVAL;
11232 11232 } else if (uio->uio_iov->iov_len & (secmask)) {
11233 11233 SD_ERROR(SD_LOG_READ_WRITE, un,
11234 11234 "sdawrite: transfer length not modulo %d\n",
11235 11235 secmask + 1);
11236 11236 err = EINVAL;
11237 11237 } else {
11238 11238 err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11239 11239 }
11240 11240
11241 11241 return (err);
11242 11242 }
11243 11243
11244 11244
11245 11245
11246 11246
11247 11247
11248 11248 /*
11249 11249 * Driver IO processing follows the following sequence:
11250 11250 *
11251 11251 * sdioctl(9E) sdstrategy(9E) biodone(9F)
11252 11252 * | | ^
11253 11253 * v v |
11254 11254 * sd_send_scsi_cmd() ddi_xbuf_qstrategy() +-------------------+
11255 11255 * | | | |
11256 11256 * v | | |
11257 11257 * sd_uscsi_strategy() sd_xbuf_strategy() sd_buf_iodone() sd_uscsi_iodone()
11258 11258 * | | ^ ^
11259 11259 * v v | |
11260 11260 * SD_BEGIN_IOSTART() SD_BEGIN_IOSTART() | |
11261 11261 * | | | |
11262 11262 * +---+ | +------------+ +-------+
11263 11263 * | | | |
11264 11264 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
11265 11265 * | v | |
11266 11266 * | sd_mapblockaddr_iostart() sd_mapblockaddr_iodone() |
11267 11267 * | | ^ |
11268 11268 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
11269 11269 * | v | |
11270 11270 * | sd_mapblocksize_iostart() sd_mapblocksize_iodone() |
11271 11271 * | | ^ |
11272 11272 * | SD_NEXT_IOSTART()| SD_NEXT_IODONE()| |
11273 11273 * | v | |
11274 11274 * | sd_checksum_iostart() sd_checksum_iodone() |
11275 11275 * | | ^ |
11276 11276 * +-> SD_NEXT_IOSTART()| SD_NEXT_IODONE()+------------->+
11277 11277 * | v | |
11278 11278 * | sd_pm_iostart() sd_pm_iodone() |
11279 11279 * | | ^ |
11280 11280 * | | | |
11281 11281 * +-> SD_NEXT_IOSTART()| SD_BEGIN_IODONE()--+--------------+
11282 11282 * | ^
11283 11283 * v |
11284 11284 * sd_core_iostart() |
11285 11285 * | |
11286 11286 * | +------>(*destroypkt)()
11287 11287 * +-> sd_start_cmds() <-+ | |
11288 11288 * | | | v
11289 11289 * | | | scsi_destroy_pkt(9F)
11290 11290 * | | |
11291 11291 * +->(*initpkt)() +- sdintr()
11292 11292 * | | | |
11293 11293 * | +-> scsi_init_pkt(9F) | +-> sd_handle_xxx()
11294 11294 * | +-> scsi_setup_cdb(9F) |
11295 11295 * | |
11296 11296 * +--> scsi_transport(9F) |
11297 11297 * | |
11298 11298 * +----> SCSA ---->+
11299 11299 *
11300 11300 *
11301 11301 * This code is based upon the following presumptions:
11302 11302 *
11303 11303 * - iostart and iodone functions operate on buf(9S) structures. These
11304 11304 * functions perform the necessary operations on the buf(9S) and pass
11305 11305 * them along to the next function in the chain by using the macros
11306 11306 * SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11307 11307 * (for iodone side functions).
11308 11308 *
11309 11309 * - The iostart side functions may sleep. The iodone side functions
11310 11310 * are called under interrupt context and may NOT sleep. Therefore
11311 11311 * iodone side functions also may not call iostart side functions.
11312 11312 * (NOTE: iostart side functions should NOT sleep for memory, as
11313 11313 * this could result in deadlock.)
11314 11314 *
11315 11315 * - An iostart side function may call its corresponding iodone side
11316 11316 * function directly (if necessary).
11317 11317 *
11318 11318 * - In the event of an error, an iostart side function can return a buf(9S)
11319 11319 * to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11320 11320 * b_error in the usual way of course).
11321 11321 *
11322 11322 * - The taskq mechanism may be used by the iodone side functions to dispatch
11323 11323 * requests to the iostart side functions. The iostart side functions in
11324 11324 * this case would be called under the context of a taskq thread, so it's
11325 11325 * OK for them to block/sleep/spin in this case.
11326 11326 *
11327 11327 * - iostart side functions may allocate "shadow" buf(9S) structs and
11328 11328 * pass them along to the next function in the chain. The corresponding
11329 11329 * iodone side functions must coalesce the "shadow" bufs and return
11330 11330 * the "original" buf to the next higher layer.
11331 11331 *
11332 11332 * - The b_private field of the buf(9S) struct holds a pointer to
11333 11333 * an sd_xbuf struct, which contains information needed to
11334 11334 * construct the scsi_pkt for the command.
11335 11335 *
11336 11336 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11337 11337 * layer must acquire & release the SD_MUTEX(un) as needed.
11338 11338 */
11339 11339
11340 11340
11341 11341 /*
11342 11342 * Create taskq for all targets in the system. This is created at
11343 11343 * _init(9E) and destroyed at _fini(9E).
11344 11344 *
11345 11345 * Note: here we set the minalloc to a reasonably high number to ensure that
11346 11346 * we will have an adequate supply of task entries available at interrupt time.
11347 11347 * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11348 11348 * sd_create_taskq(). Since we do not want to sleep for allocations at
11349 11349 * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11350 11350 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11351 11351 * requests any one instant in time.
11352 11352 */
11353 11353 #define SD_TASKQ_NUMTHREADS 8
11354 11354 #define SD_TASKQ_MINALLOC 256
11355 11355 #define SD_TASKQ_MAXALLOC 256
11356 11356
11357 11357 static taskq_t *sd_tq = NULL;
11358 11358 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11359 11359
11360 11360 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11361 11361 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11362 11362
11363 11363 /*
11364 11364 * The following task queue is being created for the write part of
11365 11365 * read-modify-write of non-512 block size devices.
11366 11366 * Limit the number of threads to 1 for now. This number has been chosen
11367 11367 * considering the fact that it applies only to dvd ram drives/MO drives
11368 11368 * currently. Performance for which is not main criteria at this stage.
11369 11369 * Note: It needs to be explored if we can use a single taskq in future
11370 11370 */
11371 11371 #define SD_WMR_TASKQ_NUMTHREADS 1
11372 11372 static taskq_t *sd_wmr_tq = NULL;
11373 11373 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11374 11374
11375 11375 /*
11376 11376 * Function: sd_taskq_create
11377 11377 *
11378 11378 * Description: Create taskq thread(s) and preallocate task entries
11379 11379 *
11380 11380 * Return Code: Returns a pointer to the allocated taskq_t.
11381 11381 *
11382 11382 * Context: Can sleep. Requires blockable context.
11383 11383 *
11384 11384 * Notes: - The taskq() facility currently is NOT part of the DDI.
11385 11385 * (definitely NOT recommeded for 3rd-party drivers!) :-)
11386 11386 * - taskq_create() will block for memory, also it will panic
11387 11387 * if it cannot create the requested number of threads.
11388 11388 * - Currently taskq_create() creates threads that cannot be
11389 11389 * swapped.
11390 11390 * - We use TASKQ_PREPOPULATE to ensure we have an adequate
11391 11391 * supply of taskq entries at interrupt time (ie, so that we
11392 11392 * do not have to sleep for memory)
11393 11393 */
11394 11394
11395 11395 static void
11396 11396 sd_taskq_create(void)
11397 11397 {
11398 11398 char taskq_name[TASKQ_NAMELEN];
11399 11399
11400 11400 ASSERT(sd_tq == NULL);
11401 11401 ASSERT(sd_wmr_tq == NULL);
11402 11402
11403 11403 (void) snprintf(taskq_name, sizeof (taskq_name),
11404 11404 "%s_drv_taskq", sd_label);
11405 11405 sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11406 11406 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11407 11407 TASKQ_PREPOPULATE));
11408 11408
11409 11409 (void) snprintf(taskq_name, sizeof (taskq_name),
11410 11410 "%s_rmw_taskq", sd_label);
11411 11411 sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11412 11412 (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11413 11413 TASKQ_PREPOPULATE));
11414 11414 }
11415 11415
11416 11416
11417 11417 /*
11418 11418 * Function: sd_taskq_delete
11419 11419 *
11420 11420 * Description: Complementary cleanup routine for sd_taskq_create().
11421 11421 *
11422 11422 * Context: Kernel thread context.
11423 11423 */
11424 11424
11425 11425 static void
11426 11426 sd_taskq_delete(void)
11427 11427 {
11428 11428 ASSERT(sd_tq != NULL);
11429 11429 ASSERT(sd_wmr_tq != NULL);
11430 11430 taskq_destroy(sd_tq);
11431 11431 taskq_destroy(sd_wmr_tq);
11432 11432 sd_tq = NULL;
11433 11433 sd_wmr_tq = NULL;
11434 11434 }
11435 11435
11436 11436
11437 11437 /*
11438 11438 * Function: sdstrategy
11439 11439 *
11440 11440 * Description: Driver's strategy (9E) entry point function.
11441 11441 *
11442 11442 * Arguments: bp - pointer to buf(9S)
11443 11443 *
11444 11444 * Return Code: Always returns zero
11445 11445 *
11446 11446 * Context: Kernel thread context.
11447 11447 */
11448 11448
11449 11449 static int
11450 11450 sdstrategy(struct buf *bp)
11451 11451 {
11452 11452 struct sd_lun *un;
11453 11453
11454 11454 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11455 11455 if (un == NULL) {
11456 11456 bioerror(bp, EIO);
11457 11457 bp->b_resid = bp->b_bcount;
11458 11458 biodone(bp);
11459 11459 return (0);
11460 11460 }
11461 11461
11462 11462 /* As was done in the past, fail new cmds. if state is dumping. */
11463 11463 if (un->un_state == SD_STATE_DUMPING) {
11464 11464 bioerror(bp, ENXIO);
11465 11465 bp->b_resid = bp->b_bcount;
11466 11466 biodone(bp);
11467 11467 return (0);
11468 11468 }
11469 11469
11470 11470 ASSERT(!mutex_owned(SD_MUTEX(un)));
11471 11471
11472 11472 /*
11473 11473 * Commands may sneak in while we released the mutex in
11474 11474 * DDI_SUSPEND, we should block new commands. However, old
11475 11475 * commands that are still in the driver at this point should
11476 11476 * still be allowed to drain.
11477 11477 */
11478 11478 mutex_enter(SD_MUTEX(un));
11479 11479 /*
11480 11480 * Must wait here if either the device is suspended or
11481 11481 * if it's power level is changing.
11482 11482 */
11483 11483 while ((un->un_state == SD_STATE_SUSPENDED) ||
11484 11484 (un->un_state == SD_STATE_PM_CHANGING)) {
11485 11485 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11486 11486 }
11487 11487
11488 11488 un->un_ncmds_in_driver++;
11489 11489
11490 11490 /*
11491 11491 * atapi: Since we are running the CD for now in PIO mode we need to
11492 11492 * call bp_mapin here to avoid bp_mapin called interrupt context under
11493 11493 * the HBA's init_pkt routine.
11494 11494 */
11495 11495 if (un->un_f_cfg_is_atapi == TRUE) {
11496 11496 mutex_exit(SD_MUTEX(un));
11497 11497 bp_mapin(bp);
11498 11498 mutex_enter(SD_MUTEX(un));
11499 11499 }
11500 11500 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11501 11501 un->un_ncmds_in_driver);
11502 11502
11503 11503 if (bp->b_flags & B_WRITE)
11504 11504 un->un_f_sync_cache_required = TRUE;
11505 11505
11506 11506 mutex_exit(SD_MUTEX(un));
11507 11507
11508 11508 /*
11509 11509 * This will (eventually) allocate the sd_xbuf area and
11510 11510 * call sd_xbuf_strategy(). We just want to return the
11511 11511 * result of ddi_xbuf_qstrategy so that we have an opt-
11512 11512 * imized tail call which saves us a stack frame.
11513 11513 */
11514 11514 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11515 11515 }
11516 11516
11517 11517
11518 11518 /*
11519 11519 * Function: sd_xbuf_strategy
11520 11520 *
11521 11521 * Description: Function for initiating IO operations via the
11522 11522 * ddi_xbuf_qstrategy() mechanism.
11523 11523 *
11524 11524 * Context: Kernel thread context.
11525 11525 */
11526 11526
11527 11527 static void
11528 11528 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11529 11529 {
11530 11530 struct sd_lun *un = arg;
11531 11531
11532 11532 ASSERT(bp != NULL);
11533 11533 ASSERT(xp != NULL);
11534 11534 ASSERT(un != NULL);
11535 11535 ASSERT(!mutex_owned(SD_MUTEX(un)));
11536 11536
11537 11537 /*
11538 11538 * Initialize the fields in the xbuf and save a pointer to the
11539 11539 * xbuf in bp->b_private.
11540 11540 */
11541 11541 sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11542 11542
11543 11543 /* Send the buf down the iostart chain */
11544 11544 SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11545 11545 }
11546 11546
11547 11547
11548 11548 /*
11549 11549 * Function: sd_xbuf_init
11550 11550 *
11551 11551 * Description: Prepare the given sd_xbuf struct for use.
11552 11552 *
11553 11553 * Arguments: un - ptr to softstate
11554 11554 * bp - ptr to associated buf(9S)
11555 11555 * xp - ptr to associated sd_xbuf
11556 11556 * chain_type - IO chain type to use:
11557 11557 * SD_CHAIN_NULL
11558 11558 * SD_CHAIN_BUFIO
11559 11559 * SD_CHAIN_USCSI
11560 11560 * SD_CHAIN_DIRECT
11561 11561 * SD_CHAIN_DIRECT_PRIORITY
11562 11562 * pktinfop - ptr to private data struct for scsi_pkt(9S)
11563 11563 * initialization; may be NULL if none.
11564 11564 *
11565 11565 * Context: Kernel thread context
11566 11566 */
11567 11567
11568 11568 static void
11569 11569 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11570 11570 uchar_t chain_type, void *pktinfop)
11571 11571 {
11572 11572 int index;
11573 11573
11574 11574 ASSERT(un != NULL);
11575 11575 ASSERT(bp != NULL);
11576 11576 ASSERT(xp != NULL);
11577 11577
11578 11578 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11579 11579 bp, chain_type);
11580 11580
11581 11581 xp->xb_un = un;
11582 11582 xp->xb_pktp = NULL;
11583 11583 xp->xb_pktinfo = pktinfop;
11584 11584 xp->xb_private = bp->b_private;
11585 11585 xp->xb_blkno = (daddr_t)bp->b_blkno;
11586 11586
11587 11587 /*
11588 11588 * Set up the iostart and iodone chain indexes in the xbuf, based
11589 11589 * upon the specified chain type to use.
11590 11590 */
11591 11591 switch (chain_type) {
11592 11592 case SD_CHAIN_NULL:
11593 11593 /*
11594 11594 * Fall thru to just use the values for the buf type, even
11595 11595 * tho for the NULL chain these values will never be used.
11596 11596 */
11597 11597 /* FALLTHRU */
11598 11598 case SD_CHAIN_BUFIO:
11599 11599 index = un->un_buf_chain_type;
11600 11600 if ((!un->un_f_has_removable_media) &&
11601 11601 (un->un_tgt_blocksize != 0) &&
11602 11602 (un->un_tgt_blocksize != DEV_BSIZE ||
11603 11603 un->un_f_enable_rmw)) {
11604 11604 int secmask = 0, blknomask = 0;
11605 11605 if (un->un_f_enable_rmw) {
11606 11606 blknomask =
11607 11607 (un->un_phy_blocksize / DEV_BSIZE) - 1;
11608 11608 secmask = un->un_phy_blocksize - 1;
11609 11609 } else {
11610 11610 blknomask =
11611 11611 (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11612 11612 secmask = un->un_tgt_blocksize - 1;
11613 11613 }
11614 11614
11615 11615 if ((bp->b_lblkno & (blknomask)) ||
11616 11616 (bp->b_bcount & (secmask))) {
11617 11617 if ((un->un_f_rmw_type !=
11618 11618 SD_RMW_TYPE_RETURN_ERROR) ||
11619 11619 un->un_f_enable_rmw) {
11620 11620 if (un->un_f_pm_is_enabled == FALSE)
11621 11621 index =
11622 11622 SD_CHAIN_INFO_MSS_DSK_NO_PM;
11623 11623 else
11624 11624 index =
11625 11625 SD_CHAIN_INFO_MSS_DISK;
11626 11626 }
11627 11627 }
11628 11628 }
11629 11629 break;
11630 11630 case SD_CHAIN_USCSI:
11631 11631 index = un->un_uscsi_chain_type;
11632 11632 break;
11633 11633 case SD_CHAIN_DIRECT:
11634 11634 index = un->un_direct_chain_type;
11635 11635 break;
11636 11636 case SD_CHAIN_DIRECT_PRIORITY:
11637 11637 index = un->un_priority_chain_type;
11638 11638 break;
11639 11639 default:
11640 11640 /* We're really broken if we ever get here... */
11641 11641 panic("sd_xbuf_init: illegal chain type!");
11642 11642 /*NOTREACHED*/
11643 11643 }
11644 11644
11645 11645 xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11646 11646 xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11647 11647
11648 11648 /*
11649 11649 * It might be a bit easier to simply bzero the entire xbuf above,
11650 11650 * but it turns out that since we init a fair number of members anyway,
11651 11651 * we save a fair number cycles by doing explicit assignment of zero.
11652 11652 */
11653 11653 xp->xb_pkt_flags = 0;
11654 11654 xp->xb_dma_resid = 0;
11655 11655 xp->xb_retry_count = 0;
11656 11656 xp->xb_victim_retry_count = 0;
11657 11657 xp->xb_ua_retry_count = 0;
11658 11658 xp->xb_nr_retry_count = 0;
11659 11659 xp->xb_sense_bp = NULL;
11660 11660 xp->xb_sense_status = 0;
11661 11661 xp->xb_sense_state = 0;
11662 11662 xp->xb_sense_resid = 0;
11663 11663 xp->xb_ena = 0;
11664 11664
11665 11665 bp->b_private = xp;
11666 11666 bp->b_flags &= ~(B_DONE | B_ERROR);
11667 11667 bp->b_resid = 0;
11668 11668 bp->av_forw = NULL;
11669 11669 bp->av_back = NULL;
11670 11670 bioerror(bp, 0);
11671 11671
11672 11672 SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11673 11673 }
11674 11674
11675 11675
11676 11676 /*
11677 11677 * Function: sd_uscsi_strategy
11678 11678 *
11679 11679 * Description: Wrapper for calling into the USCSI chain via physio(9F)
11680 11680 *
11681 11681 * Arguments: bp - buf struct ptr
11682 11682 *
11683 11683 * Return Code: Always returns 0
11684 11684 *
11685 11685 * Context: Kernel thread context
11686 11686 */
11687 11687
11688 11688 static int
11689 11689 sd_uscsi_strategy(struct buf *bp)
11690 11690 {
11691 11691 struct sd_lun *un;
11692 11692 struct sd_uscsi_info *uip;
11693 11693 struct sd_xbuf *xp;
11694 11694 uchar_t chain_type;
11695 11695 uchar_t cmd;
11696 11696
11697 11697 ASSERT(bp != NULL);
11698 11698
11699 11699 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11700 11700 if (un == NULL) {
11701 11701 bioerror(bp, EIO);
11702 11702 bp->b_resid = bp->b_bcount;
11703 11703 biodone(bp);
11704 11704 return (0);
11705 11705 }
11706 11706
11707 11707 ASSERT(!mutex_owned(SD_MUTEX(un)));
11708 11708
11709 11709 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11710 11710
11711 11711 /*
11712 11712 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11713 11713 */
11714 11714 ASSERT(bp->b_private != NULL);
11715 11715 uip = (struct sd_uscsi_info *)bp->b_private;
11716 11716 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11717 11717
11718 11718 mutex_enter(SD_MUTEX(un));
11719 11719 /*
11720 11720 * atapi: Since we are running the CD for now in PIO mode we need to
11721 11721 * call bp_mapin here to avoid bp_mapin called interrupt context under
11722 11722 * the HBA's init_pkt routine.
11723 11723 */
11724 11724 if (un->un_f_cfg_is_atapi == TRUE) {
11725 11725 mutex_exit(SD_MUTEX(un));
11726 11726 bp_mapin(bp);
11727 11727 mutex_enter(SD_MUTEX(un));
11728 11728 }
11729 11729 un->un_ncmds_in_driver++;
11730 11730 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11731 11731 un->un_ncmds_in_driver);
11732 11732
11733 11733 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11734 11734 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11735 11735 un->un_f_sync_cache_required = TRUE;
11736 11736
11737 11737 mutex_exit(SD_MUTEX(un));
11738 11738
11739 11739 switch (uip->ui_flags) {
11740 11740 case SD_PATH_DIRECT:
11741 11741 chain_type = SD_CHAIN_DIRECT;
11742 11742 break;
11743 11743 case SD_PATH_DIRECT_PRIORITY:
11744 11744 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11745 11745 break;
11746 11746 default:
11747 11747 chain_type = SD_CHAIN_USCSI;
11748 11748 break;
11749 11749 }
11750 11750
11751 11751 /*
11752 11752 * We may allocate extra buf for external USCSI commands. If the
11753 11753 * application asks for bigger than 20-byte sense data via USCSI,
11754 11754 * SCSA layer will allocate 252 bytes sense buf for that command.
11755 11755 */
11756 11756 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11757 11757 SENSE_LENGTH) {
11758 11758 xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11759 11759 MAX_SENSE_LENGTH, KM_SLEEP);
11760 11760 } else {
11761 11761 xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11762 11762 }
11763 11763
11764 11764 sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11765 11765
11766 11766 /* Use the index obtained within xbuf_init */
11767 11767 SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11768 11768
11769 11769 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11770 11770
11771 11771 return (0);
11772 11772 }
11773 11773
11774 11774 /*
11775 11775 * Function: sd_send_scsi_cmd
11776 11776 *
11777 11777 * Description: Runs a USCSI command for user (when called thru sdioctl),
11778 11778 * or for the driver
11779 11779 *
11780 11780 * Arguments: dev - the dev_t for the device
11781 11781 * incmd - ptr to a valid uscsi_cmd struct
11782 11782 * flag - bit flag, indicating open settings, 32/64 bit type
11783 11783 * dataspace - UIO_USERSPACE or UIO_SYSSPACE
11784 11784 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11785 11785 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11786 11786 * to use the USCSI "direct" chain and bypass the normal
11787 11787 * command waitq.
11788 11788 *
11789 11789 * Return Code: 0 - successful completion of the given command
11790 11790 * EIO - scsi_uscsi_handle_command() failed
11791 11791 * ENXIO - soft state not found for specified dev
11792 11792 * EINVAL
11793 11793 * EFAULT - copyin/copyout error
11794 11794 * return code of scsi_uscsi_handle_command():
11795 11795 * EIO
11796 11796 * ENXIO
11797 11797 * EACCES
11798 11798 *
11799 11799 * Context: Waits for command to complete. Can sleep.
11800 11800 */
11801 11801
11802 11802 static int
11803 11803 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11804 11804 enum uio_seg dataspace, int path_flag)
11805 11805 {
11806 11806 struct sd_lun *un;
11807 11807 sd_ssc_t *ssc;
11808 11808 int rval;
11809 11809
11810 11810 un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11811 11811 if (un == NULL) {
11812 11812 return (ENXIO);
11813 11813 }
11814 11814
11815 11815 /*
11816 11816 * Using sd_ssc_send to handle uscsi cmd
11817 11817 */
11818 11818 ssc = sd_ssc_init(un);
11819 11819 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11820 11820 sd_ssc_fini(ssc);
11821 11821
11822 11822 return (rval);
11823 11823 }
11824 11824
11825 11825 /*
11826 11826 * Function: sd_ssc_init
11827 11827 *
11828 11828 * Description: Uscsi end-user call this function to initialize necessary
11829 11829 * fields, such as uscsi_cmd and sd_uscsi_info struct.
11830 11830 *
11831 11831 * The return value of sd_send_scsi_cmd will be treated as a
11832 11832 * fault in various conditions. Even it is not Zero, some
11833 11833 * callers may ignore the return value. That is to say, we can
11834 11834 * not make an accurate assessment in sdintr, since if a
11835 11835 * command is failed in sdintr it does not mean the caller of
11836 11836 * sd_send_scsi_cmd will treat it as a real failure.
11837 11837 *
11838 11838 * To avoid printing too many error logs for a failed uscsi
11839 11839 * packet that the caller may not treat it as a failure, the
11840 11840 * sd will keep silent for handling all uscsi commands.
11841 11841 *
11842 11842 * During detach->attach and attach-open, for some types of
11843 11843 * problems, the driver should be providing information about
11844 11844 * the problem encountered. Device use USCSI_SILENT, which
11845 11845 * suppresses all driver information. The result is that no
11846 11846 * information about the problem is available. Being
11847 11847 * completely silent during this time is inappropriate. The
11848 11848 * driver needs a more selective filter than USCSI_SILENT, so
11849 11849 * that information related to faults is provided.
11850 11850 *
11851 11851 * To make the accurate accessment, the caller of
11852 11852 * sd_send_scsi_USCSI_CMD should take the ownership and
11853 11853 * get necessary information to print error messages.
11854 11854 *
11855 11855 * If we want to print necessary info of uscsi command, we need to
11856 11856 * keep the uscsi_cmd and sd_uscsi_info till we can make the
11857 11857 * assessment. We use sd_ssc_init to alloc necessary
11858 11858 * structs for sending an uscsi command and we are also
11859 11859 * responsible for free the memory by calling
11860 11860 * sd_ssc_fini.
11861 11861 *
11862 11862 * The calling secquences will look like:
11863 11863 * sd_ssc_init->
11864 11864 *
11865 11865 * ...
11866 11866 *
11867 11867 * sd_send_scsi_USCSI_CMD->
11868 11868 * sd_ssc_send-> - - - sdintr
11869 11869 * ...
11870 11870 *
11871 11871 * if we think the return value should be treated as a
11872 11872 * failure, we make the accessment here and print out
11873 11873 * necessary by retrieving uscsi_cmd and sd_uscsi_info'
11874 11874 *
11875 11875 * ...
11876 11876 *
11877 11877 * sd_ssc_fini
11878 11878 *
11879 11879 *
11880 11880 * Arguments: un - pointer to driver soft state (unit) structure for this
11881 11881 * target.
11882 11882 *
11883 11883 * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11884 11884 * uscsi_cmd and sd_uscsi_info.
11885 11885 * NULL - if can not alloc memory for sd_ssc_t struct
11886 11886 *
11887 11887 * Context: Kernel Thread.
11888 11888 */
11889 11889 static sd_ssc_t *
11890 11890 sd_ssc_init(struct sd_lun *un)
11891 11891 {
11892 11892 sd_ssc_t *ssc;
11893 11893 struct uscsi_cmd *ucmdp;
11894 11894 struct sd_uscsi_info *uip;
11895 11895
11896 11896 ASSERT(un != NULL);
11897 11897 ASSERT(!mutex_owned(SD_MUTEX(un)));
11898 11898
11899 11899 /*
11900 11900 * Allocate sd_ssc_t structure
11901 11901 */
11902 11902 ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11903 11903
11904 11904 /*
11905 11905 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11906 11906 */
11907 11907 ucmdp = scsi_uscsi_alloc();
11908 11908
11909 11909 /*
11910 11910 * Allocate sd_uscsi_info structure
11911 11911 */
11912 11912 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11913 11913
11914 11914 ssc->ssc_uscsi_cmd = ucmdp;
11915 11915 ssc->ssc_uscsi_info = uip;
11916 11916 ssc->ssc_un = un;
11917 11917
11918 11918 return (ssc);
11919 11919 }
11920 11920
11921 11921 /*
11922 11922 * Function: sd_ssc_fini
11923 11923 *
11924 11924 * Description: To free sd_ssc_t and it's hanging off
11925 11925 *
11926 11926 * Arguments: ssc - struct pointer of sd_ssc_t.
11927 11927 */
11928 11928 static void
11929 11929 sd_ssc_fini(sd_ssc_t *ssc)
11930 11930 {
11931 11931 scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11932 11932
11933 11933 if (ssc->ssc_uscsi_info != NULL) {
11934 11934 kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11935 11935 ssc->ssc_uscsi_info = NULL;
11936 11936 }
11937 11937
11938 11938 kmem_free(ssc, sizeof (sd_ssc_t));
11939 11939 ssc = NULL;
11940 11940 }
11941 11941
11942 11942 /*
11943 11943 * Function: sd_ssc_send
11944 11944 *
11945 11945 * Description: Runs a USCSI command for user when called through sdioctl,
11946 11946 * or for the driver.
11947 11947 *
11948 11948 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11949 11949 * sd_uscsi_info in.
11950 11950 * incmd - ptr to a valid uscsi_cmd struct
11951 11951 * flag - bit flag, indicating open settings, 32/64 bit type
11952 11952 * dataspace - UIO_USERSPACE or UIO_SYSSPACE
11953 11953 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11954 11954 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11955 11955 * to use the USCSI "direct" chain and bypass the normal
11956 11956 * command waitq.
11957 11957 *
11958 11958 * Return Code: 0 - successful completion of the given command
11959 11959 * EIO - scsi_uscsi_handle_command() failed
11960 11960 * ENXIO - soft state not found for specified dev
11961 11961 * ECANCELED - command cancelled due to low power
11962 11962 * EINVAL
11963 11963 * EFAULT - copyin/copyout error
11964 11964 * return code of scsi_uscsi_handle_command():
11965 11965 * EIO
11966 11966 * ENXIO
11967 11967 * EACCES
11968 11968 *
11969 11969 * Context: Kernel Thread;
11970 11970 * Waits for command to complete. Can sleep.
11971 11971 */
11972 11972 static int
11973 11973 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11974 11974 enum uio_seg dataspace, int path_flag)
11975 11975 {
11976 11976 struct sd_uscsi_info *uip;
11977 11977 struct uscsi_cmd *uscmd;
11978 11978 struct sd_lun *un;
11979 11979 dev_t dev;
11980 11980
11981 11981 int format = 0;
11982 11982 int rval;
11983 11983
11984 11984 ASSERT(ssc != NULL);
11985 11985 un = ssc->ssc_un;
11986 11986 ASSERT(un != NULL);
11987 11987 uscmd = ssc->ssc_uscsi_cmd;
11988 11988 ASSERT(uscmd != NULL);
11989 11989 ASSERT(!mutex_owned(SD_MUTEX(un)));
11990 11990 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11991 11991 /*
11992 11992 * If enter here, it indicates that the previous uscsi
11993 11993 * command has not been processed by sd_ssc_assessment.
11994 11994 * This is violating our rules of FMA telemetry processing.
11995 11995 * We should print out this message and the last undisposed
11996 11996 * uscsi command.
11997 11997 */
11998 11998 if (uscmd->uscsi_cdb != NULL) {
11999 11999 SD_INFO(SD_LOG_SDTEST, un,
12000 12000 "sd_ssc_send is missing the alternative "
12001 12001 "sd_ssc_assessment when running command 0x%x.\n",
12002 12002 uscmd->uscsi_cdb[0]);
12003 12003 }
12004 12004 /*
12005 12005 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
12006 12006 * the initial status.
12007 12007 */
12008 12008 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12009 12009 }
12010 12010
12011 12011 /*
12012 12012 * We need to make sure sd_ssc_send will have sd_ssc_assessment
12013 12013 * followed to avoid missing FMA telemetries.
12014 12014 */
12015 12015 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
12016 12016
12017 12017 /*
12018 12018 * if USCSI_PMFAILFAST is set and un is in low power, fail the
12019 12019 * command immediately.
12020 12020 */
12021 12021 mutex_enter(SD_MUTEX(un));
12022 12022 mutex_enter(&un->un_pm_mutex);
12023 12023 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
12024 12024 SD_DEVICE_IS_IN_LOW_POWER(un)) {
12025 12025 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
12026 12026 "un:0x%p is in low power\n", un);
12027 12027 mutex_exit(&un->un_pm_mutex);
12028 12028 mutex_exit(SD_MUTEX(un));
12029 12029 return (ECANCELED);
12030 12030 }
12031 12031 mutex_exit(&un->un_pm_mutex);
12032 12032 mutex_exit(SD_MUTEX(un));
12033 12033
12034 12034 #ifdef SDDEBUG
12035 12035 switch (dataspace) {
12036 12036 case UIO_USERSPACE:
12037 12037 SD_TRACE(SD_LOG_IO, un,
12038 12038 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12039 12039 break;
12040 12040 case UIO_SYSSPACE:
12041 12041 SD_TRACE(SD_LOG_IO, un,
12042 12042 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12043 12043 break;
12044 12044 default:
12045 12045 SD_TRACE(SD_LOG_IO, un,
12046 12046 "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12047 12047 break;
12048 12048 }
12049 12049 #endif
12050 12050
12051 12051 rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12052 12052 SD_ADDRESS(un), &uscmd);
12053 12053 if (rval != 0) {
12054 12054 SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12055 12055 "scsi_uscsi_alloc_and_copyin failed\n", un);
12056 12056 return (rval);
12057 12057 }
12058 12058
12059 12059 if ((uscmd->uscsi_cdb != NULL) &&
12060 12060 (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12061 12061 mutex_enter(SD_MUTEX(un));
12062 12062 un->un_f_format_in_progress = TRUE;
12063 12063 mutex_exit(SD_MUTEX(un));
12064 12064 format = 1;
12065 12065 }
12066 12066
12067 12067 /*
12068 12068 * Allocate an sd_uscsi_info struct and fill it with the info
12069 12069 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
12070 12070 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
12071 12071 * since we allocate the buf here in this function, we do not
12072 12072 * need to preserve the prior contents of b_private.
12073 12073 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12074 12074 */
12075 12075 uip = ssc->ssc_uscsi_info;
12076 12076 uip->ui_flags = path_flag;
12077 12077 uip->ui_cmdp = uscmd;
12078 12078
12079 12079 /*
12080 12080 * Commands sent with priority are intended for error recovery
12081 12081 * situations, and do not have retries performed.
12082 12082 */
12083 12083 if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12084 12084 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12085 12085 }
12086 12086 uscmd->uscsi_flags &= ~USCSI_NOINTR;
12087 12087
12088 12088 dev = SD_GET_DEV(un);
12089 12089 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12090 12090 sd_uscsi_strategy, NULL, uip);
12091 12091
12092 12092 /*
12093 12093 * mark ssc_flags right after handle_cmd to make sure
12094 12094 * the uscsi has been sent
12095 12095 */
12096 12096 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12097 12097
12098 12098 #ifdef SDDEBUG
12099 12099 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12100 12100 "uscsi_status: 0x%02x uscsi_resid:0x%x\n",
12101 12101 uscmd->uscsi_status, uscmd->uscsi_resid);
12102 12102 if (uscmd->uscsi_bufaddr != NULL) {
12103 12103 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12104 12104 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n",
12105 12105 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12106 12106 if (dataspace == UIO_SYSSPACE) {
12107 12107 SD_DUMP_MEMORY(un, SD_LOG_IO,
12108 12108 "data", (uchar_t *)uscmd->uscsi_bufaddr,
12109 12109 uscmd->uscsi_buflen, SD_LOG_HEX);
12110 12110 }
12111 12111 }
12112 12112 #endif
12113 12113
12114 12114 if (format == 1) {
12115 12115 mutex_enter(SD_MUTEX(un));
12116 12116 un->un_f_format_in_progress = FALSE;
12117 12117 mutex_exit(SD_MUTEX(un));
12118 12118 }
12119 12119
12120 12120 (void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12121 12121
12122 12122 return (rval);
12123 12123 }
12124 12124
12125 12125 /*
12126 12126 * Function: sd_ssc_print
12127 12127 *
12128 12128 * Description: Print information available to the console.
12129 12129 *
12130 12130 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12131 12131 * sd_uscsi_info in.
12132 12132 * sd_severity - log level.
12133 12133 * Context: Kernel thread or interrupt context.
12134 12134 */
12135 12135 static void
12136 12136 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12137 12137 {
12138 12138 struct uscsi_cmd *ucmdp;
12139 12139 struct scsi_device *devp;
12140 12140 dev_info_t *devinfo;
12141 12141 uchar_t *sensep;
12142 12142 int senlen;
12143 12143 union scsi_cdb *cdbp;
12144 12144 uchar_t com;
12145 12145 extern struct scsi_key_strings scsi_cmds[];
12146 12146
12147 12147 ASSERT(ssc != NULL);
12148 12148 ASSERT(ssc->ssc_un != NULL);
12149 12149
12150 12150 if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12151 12151 return;
12152 12152 ucmdp = ssc->ssc_uscsi_cmd;
12153 12153 devp = SD_SCSI_DEVP(ssc->ssc_un);
12154 12154 devinfo = SD_DEVINFO(ssc->ssc_un);
12155 12155 ASSERT(ucmdp != NULL);
12156 12156 ASSERT(devp != NULL);
12157 12157 ASSERT(devinfo != NULL);
12158 12158 sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12159 12159 senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12160 12160 cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12161 12161
12162 12162 /* In certain case (like DOORLOCK), the cdb could be NULL. */
12163 12163 if (cdbp == NULL)
12164 12164 return;
12165 12165 /* We don't print log if no sense data available. */
12166 12166 if (senlen == 0)
12167 12167 sensep = NULL;
12168 12168 com = cdbp->scc_cmd;
12169 12169 scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12170 12170 scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12171 12171 }
12172 12172
12173 12173 /*
12174 12174 * Function: sd_ssc_assessment
12175 12175 *
12176 12176 * Description: We use this function to make an assessment at the point
12177 12177 * where SD driver may encounter a potential error.
12178 12178 *
12179 12179 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12180 12180 * sd_uscsi_info in.
12181 12181 * tp_assess - a hint of strategy for ereport posting.
12182 12182 * Possible values of tp_assess include:
12183 12183 * SD_FMT_IGNORE - we don't post any ereport because we're
12184 12184 * sure that it is ok to ignore the underlying problems.
12185 12185 * SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12186 12186 * but it might be not correct to ignore the underlying hardware
12187 12187 * error.
12188 12188 * SD_FMT_STATUS_CHECK - we will post an ereport with the
12189 12189 * payload driver-assessment of value "fail" or
12190 12190 * "fatal"(depending on what information we have here). This
12191 12191 * assessment value is usually set when SD driver think there
12192 12192 * is a potential error occurred(Typically, when return value
12193 12193 * of the SCSI command is EIO).
12194 12194 * SD_FMT_STANDARD - we will post an ereport with the payload
12195 12195 * driver-assessment of value "info". This assessment value is
12196 12196 * set when the SCSI command returned successfully and with
12197 12197 * sense data sent back.
12198 12198 *
12199 12199 * Context: Kernel thread.
12200 12200 */
12201 12201 static void
12202 12202 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12203 12203 {
12204 12204 int senlen = 0;
12205 12205 struct uscsi_cmd *ucmdp = NULL;
12206 12206 struct sd_lun *un;
12207 12207
12208 12208 ASSERT(ssc != NULL);
12209 12209 un = ssc->ssc_un;
12210 12210 ASSERT(un != NULL);
12211 12211 ucmdp = ssc->ssc_uscsi_cmd;
12212 12212 ASSERT(ucmdp != NULL);
12213 12213
12214 12214 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12215 12215 ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12216 12216 } else {
12217 12217 /*
12218 12218 * If enter here, it indicates that we have a wrong
12219 12219 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12220 12220 * both of which should be called in a pair in case of
12221 12221 * loss of FMA telemetries.
12222 12222 */
12223 12223 if (ucmdp->uscsi_cdb != NULL) {
12224 12224 SD_INFO(SD_LOG_SDTEST, un,
12225 12225 "sd_ssc_assessment is missing the "
12226 12226 "alternative sd_ssc_send when running 0x%x, "
12227 12227 "or there are superfluous sd_ssc_assessment for "
12228 12228 "the same sd_ssc_send.\n",
12229 12229 ucmdp->uscsi_cdb[0]);
12230 12230 }
12231 12231 /*
12232 12232 * Set the ssc_flags to the initial value to avoid passing
12233 12233 * down dirty flags to the following sd_ssc_send function.
12234 12234 */
12235 12235 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12236 12236 return;
12237 12237 }
12238 12238
12239 12239 /*
12240 12240 * Only handle an issued command which is waiting for assessment.
12241 12241 * A command which is not issued will not have
12242 12242 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12243 12243 */
12244 12244 if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12245 12245 sd_ssc_print(ssc, SCSI_ERR_INFO);
12246 12246 return;
12247 12247 } else {
12248 12248 /*
12249 12249 * For an issued command, we should clear this flag in
12250 12250 * order to make the sd_ssc_t structure be used off
12251 12251 * multiple uscsi commands.
12252 12252 */
12253 12253 ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12254 12254 }
12255 12255
12256 12256 /*
12257 12257 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12258 12258 * commands here. And we should clear the ssc_flags before return.
12259 12259 */
12260 12260 if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12261 12261 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12262 12262 return;
12263 12263 }
12264 12264
12265 12265 switch (tp_assess) {
12266 12266 case SD_FMT_IGNORE:
12267 12267 case SD_FMT_IGNORE_COMPROMISE:
12268 12268 break;
12269 12269 case SD_FMT_STATUS_CHECK:
12270 12270 /*
12271 12271 * For a failed command(including the succeeded command
12272 12272 * with invalid data sent back).
12273 12273 */
12274 12274 sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12275 12275 break;
12276 12276 case SD_FMT_STANDARD:
12277 12277 /*
12278 12278 * Always for the succeeded commands probably with sense
12279 12279 * data sent back.
12280 12280 * Limitation:
12281 12281 * We can only handle a succeeded command with sense
12282 12282 * data sent back when auto-request-sense is enabled.
12283 12283 */
12284 12284 senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12285 12285 ssc->ssc_uscsi_cmd->uscsi_rqresid;
12286 12286 if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12287 12287 (un->un_f_arq_enabled == TRUE) &&
12288 12288 senlen > 0 &&
12289 12289 ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12290 12290 sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12291 12291 }
12292 12292 break;
12293 12293 default:
12294 12294 /*
12295 12295 * Should not have other type of assessment.
12296 12296 */
12297 12297 scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12298 12298 "sd_ssc_assessment got wrong "
12299 12299 "sd_type_assessment %d.\n", tp_assess);
12300 12300 break;
12301 12301 }
12302 12302 /*
12303 12303 * Clear up the ssc_flags before return.
12304 12304 */
12305 12305 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12306 12306 }
12307 12307
12308 12308 /*
12309 12309 * Function: sd_ssc_post
12310 12310 *
12311 12311 * Description: 1. read the driver property to get fm-scsi-log flag.
12312 12312 * 2. print log if fm_log_capable is non-zero.
12313 12313 * 3. call sd_ssc_ereport_post to post ereport if possible.
12314 12314 *
12315 12315 * Context: May be called from kernel thread or interrupt context.
12316 12316 */
12317 12317 static void
12318 12318 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12319 12319 {
12320 12320 struct sd_lun *un;
12321 12321 int sd_severity;
12322 12322
12323 12323 ASSERT(ssc != NULL);
12324 12324 un = ssc->ssc_un;
12325 12325 ASSERT(un != NULL);
12326 12326
12327 12327 /*
12328 12328 * We may enter here from sd_ssc_assessment(for USCSI command) or
12329 12329 * by directly called from sdintr context.
12330 12330 * We don't handle a non-disk drive(CD-ROM, removable media).
12331 12331 * Clear the ssc_flags before return in case we've set
12332 12332 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12333 12333 * driver.
12334 12334 */
12335 12335 if (ISCD(un) || un->un_f_has_removable_media) {
12336 12336 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12337 12337 return;
12338 12338 }
12339 12339
12340 12340 switch (sd_assess) {
12341 12341 case SD_FM_DRV_FATAL:
12342 12342 sd_severity = SCSI_ERR_FATAL;
12343 12343 break;
12344 12344 case SD_FM_DRV_RECOVERY:
12345 12345 sd_severity = SCSI_ERR_RECOVERED;
12346 12346 break;
12347 12347 case SD_FM_DRV_RETRY:
12348 12348 sd_severity = SCSI_ERR_RETRYABLE;
12349 12349 break;
12350 12350 case SD_FM_DRV_NOTICE:
12351 12351 sd_severity = SCSI_ERR_INFO;
12352 12352 break;
12353 12353 default:
12354 12354 sd_severity = SCSI_ERR_UNKNOWN;
12355 12355 }
12356 12356 /* print log */
12357 12357 sd_ssc_print(ssc, sd_severity);
12358 12358
12359 12359 /* always post ereport */
12360 12360 sd_ssc_ereport_post(ssc, sd_assess);
12361 12361 }
12362 12362
12363 12363 /*
12364 12364 * Function: sd_ssc_set_info
12365 12365 *
12366 12366 * Description: Mark ssc_flags and set ssc_info which would be the
12367 12367 * payload of uderr ereport. This function will cause
12368 12368 * sd_ssc_ereport_post to post uderr ereport only.
12369 12369 * Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12370 12370 * the function will also call SD_ERROR or scsi_log for a
12371 12371 * CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12372 12372 *
12373 12373 * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12374 12374 * sd_uscsi_info in.
12375 12375 * ssc_flags - indicate the sub-category of a uderr.
12376 12376 * comp - this argument is meaningful only when
12377 12377 * ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12378 12378 * values include:
12379 12379 * > 0, SD_ERROR is used with comp as the driver logging
12380 12380 * component;
12381 12381 * = 0, scsi-log is used to log error telemetries;
12382 12382 * < 0, no log available for this telemetry.
12383 12383 *
12384 12384 * Context: Kernel thread or interrupt context
12385 12385 */
12386 12386 static void
12387 12387 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12388 12388 {
12389 12389 va_list ap;
12390 12390
12391 12391 ASSERT(ssc != NULL);
12392 12392 ASSERT(ssc->ssc_un != NULL);
12393 12393
12394 12394 ssc->ssc_flags |= ssc_flags;
12395 12395 va_start(ap, fmt);
12396 12396 (void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12397 12397 va_end(ap);
12398 12398
12399 12399 /*
12400 12400 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12401 12401 * with invalid data sent back. For non-uscsi command, the
12402 12402 * following code will be bypassed.
12403 12403 */
12404 12404 if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12405 12405 if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12406 12406 /*
12407 12407 * If the error belong to certain component and we
12408 12408 * do not want it to show up on the console, we
12409 12409 * will use SD_ERROR, otherwise scsi_log is
12410 12410 * preferred.
12411 12411 */
12412 12412 if (comp > 0) {
12413 12413 SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12414 12414 } else if (comp == 0) {
12415 12415 scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12416 12416 CE_WARN, ssc->ssc_info);
12417 12417 }
12418 12418 }
12419 12419 }
12420 12420 }
12421 12421
12422 12422 /*
12423 12423 * Function: sd_buf_iodone
12424 12424 *
12425 12425 * Description: Frees the sd_xbuf & returns the buf to its originator.
12426 12426 *
12427 12427 * Context: May be called from interrupt context.
12428 12428 */
12429 12429 /* ARGSUSED */
12430 12430 static void
12431 12431 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12432 12432 {
12433 12433 struct sd_xbuf *xp;
12434 12434
12435 12435 ASSERT(un != NULL);
12436 12436 ASSERT(bp != NULL);
12437 12437 ASSERT(!mutex_owned(SD_MUTEX(un)));
12438 12438
12439 12439 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12440 12440
12441 12441 xp = SD_GET_XBUF(bp);
12442 12442 ASSERT(xp != NULL);
12443 12443
12444 12444 /* xbuf is gone after this */
12445 12445 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12446 12446 mutex_enter(SD_MUTEX(un));
12447 12447
12448 12448 /*
12449 12449 * Grab time when the cmd completed.
12450 12450 * This is used for determining if the system has been
12451 12451 * idle long enough to make it idle to the PM framework.
12452 12452 * This is for lowering the overhead, and therefore improving
12453 12453 * performance per I/O operation.
12454 12454 */
12455 12455 un->un_pm_idle_time = ddi_get_time();
12456 12456
12457 12457 un->un_ncmds_in_driver--;
12458 12458 ASSERT(un->un_ncmds_in_driver >= 0);
12459 12459 SD_INFO(SD_LOG_IO, un,
12460 12460 "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12461 12461 un->un_ncmds_in_driver);
12462 12462
12463 12463 mutex_exit(SD_MUTEX(un));
12464 12464 }
12465 12465
12466 12466 biodone(bp); /* bp is gone after this */
12467 12467
12468 12468 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12469 12469 }
12470 12470
12471 12471
12472 12472 /*
12473 12473 * Function: sd_uscsi_iodone
12474 12474 *
12475 12475 * Description: Frees the sd_xbuf & returns the buf to its originator.
12476 12476 *
12477 12477 * Context: May be called from interrupt context.
12478 12478 */
12479 12479 /* ARGSUSED */
12480 12480 static void
12481 12481 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12482 12482 {
12483 12483 struct sd_xbuf *xp;
12484 12484
12485 12485 ASSERT(un != NULL);
12486 12486 ASSERT(bp != NULL);
12487 12487
12488 12488 xp = SD_GET_XBUF(bp);
12489 12489 ASSERT(xp != NULL);
12490 12490 ASSERT(!mutex_owned(SD_MUTEX(un)));
12491 12491
12492 12492 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12493 12493
12494 12494 bp->b_private = xp->xb_private;
12495 12495
12496 12496 mutex_enter(SD_MUTEX(un));
12497 12497
12498 12498 /*
12499 12499 * Grab time when the cmd completed.
12500 12500 * This is used for determining if the system has been
12501 12501 * idle long enough to make it idle to the PM framework.
12502 12502 * This is for lowering the overhead, and therefore improving
12503 12503 * performance per I/O operation.
12504 12504 */
12505 12505 un->un_pm_idle_time = ddi_get_time();
12506 12506
12507 12507 un->un_ncmds_in_driver--;
12508 12508 ASSERT(un->un_ncmds_in_driver >= 0);
12509 12509 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12510 12510 un->un_ncmds_in_driver);
12511 12511
12512 12512 mutex_exit(SD_MUTEX(un));
12513 12513
12514 12514 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12515 12515 SENSE_LENGTH) {
12516 12516 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12517 12517 MAX_SENSE_LENGTH);
12518 12518 } else {
12519 12519 kmem_free(xp, sizeof (struct sd_xbuf));
12520 12520 }
12521 12521
12522 12522 biodone(bp);
12523 12523
12524 12524 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12525 12525 }
12526 12526
12527 12527
12528 12528 /*
12529 12529 * Function: sd_mapblockaddr_iostart
12530 12530 *
12531 12531 * Description: Verify request lies within the partition limits for
12532 12532 * the indicated minor device. Issue "overrun" buf if
12533 12533 * request would exceed partition range. Converts
12534 12534 * partition-relative block address to absolute.
12535 12535 *
12536 12536 * Upon exit of this function:
12537 12537 * 1.I/O is aligned
12538 12538 * xp->xb_blkno represents the absolute sector address
12539 12539 * 2.I/O is misaligned
12540 12540 * xp->xb_blkno represents the absolute logical block address
12541 12541 * based on DEV_BSIZE. The logical block address will be
12542 12542 * converted to physical sector address in sd_mapblocksize_\
12543 12543 * iostart.
12544 12544 * 3.I/O is misaligned but is aligned in "overrun" buf
12545 12545 * xp->xb_blkno represents the absolute logical block address
12546 12546 * based on DEV_BSIZE. The logical block address will be
12547 12547 * converted to physical sector address in sd_mapblocksize_\
12548 12548 * iostart. But no RMW will be issued in this case.
12549 12549 *
12550 12550 * Context: Can sleep
12551 12551 *
12552 12552 * Issues: This follows what the old code did, in terms of accessing
12553 12553 * some of the partition info in the unit struct without holding
12554 12554 * the mutext. This is a general issue, if the partition info
12555 12555 * can be altered while IO is in progress... as soon as we send
12556 12556 * a buf, its partitioning can be invalid before it gets to the
12557 12557 * device. Probably the right fix is to move partitioning out
12558 12558 * of the driver entirely.
12559 12559 */
12560 12560
12561 12561 static void
12562 12562 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12563 12563 {
12564 12564 diskaddr_t nblocks; /* #blocks in the given partition */
12565 12565 daddr_t blocknum; /* Block number specified by the buf */
12566 12566 size_t requested_nblocks;
12567 12567 size_t available_nblocks;
12568 12568 int partition;
12569 12569 diskaddr_t partition_offset;
12570 12570 struct sd_xbuf *xp;
12571 12571 int secmask = 0, blknomask = 0;
12572 12572 ushort_t is_aligned = TRUE;
12573 12573
12574 12574 ASSERT(un != NULL);
12575 12575 ASSERT(bp != NULL);
12576 12576 ASSERT(!mutex_owned(SD_MUTEX(un)));
12577 12577
12578 12578 SD_TRACE(SD_LOG_IO_PARTITION, un,
12579 12579 "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12580 12580
12581 12581 xp = SD_GET_XBUF(bp);
12582 12582 ASSERT(xp != NULL);
12583 12583
12584 12584 /*
12585 12585 * If the geometry is not indicated as valid, attempt to access
12586 12586 * the unit & verify the geometry/label. This can be the case for
12587 12587 * removable-media devices, of if the device was opened in
12588 12588 * NDELAY/NONBLOCK mode.
12589 12589 */
12590 12590 partition = SDPART(bp->b_edev);
12591 12591
12592 12592 if (!SD_IS_VALID_LABEL(un)) {
12593 12593 sd_ssc_t *ssc;
12594 12594 /*
12595 12595 * Initialize sd_ssc_t for internal uscsi commands
12596 12596 * In case of potential porformance issue, we need
12597 12597 * to alloc memory only if there is invalid label
12598 12598 */
12599 12599 ssc = sd_ssc_init(un);
12600 12600
12601 12601 if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12602 12602 /*
12603 12603 * For removable devices it is possible to start an
12604 12604 * I/O without a media by opening the device in nodelay
12605 12605 * mode. Also for writable CDs there can be many
12606 12606 * scenarios where there is no geometry yet but volume
12607 12607 * manager is trying to issue a read() just because
12608 12608 * it can see TOC on the CD. So do not print a message
12609 12609 * for removables.
12610 12610 */
12611 12611 if (!un->un_f_has_removable_media) {
12612 12612 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12613 12613 "i/o to invalid geometry\n");
12614 12614 }
12615 12615 bioerror(bp, EIO);
12616 12616 bp->b_resid = bp->b_bcount;
12617 12617 SD_BEGIN_IODONE(index, un, bp);
12618 12618
12619 12619 sd_ssc_fini(ssc);
12620 12620 return;
12621 12621 }
12622 12622 sd_ssc_fini(ssc);
12623 12623 }
12624 12624
12625 12625 nblocks = 0;
12626 12626 (void) cmlb_partinfo(un->un_cmlbhandle, partition,
12627 12627 &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12628 12628
12629 12629 if (un->un_f_enable_rmw) {
12630 12630 blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12631 12631 secmask = un->un_phy_blocksize - 1;
12632 12632 } else {
12633 12633 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12634 12634 secmask = un->un_tgt_blocksize - 1;
12635 12635 }
12636 12636
12637 12637 if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12638 12638 is_aligned = FALSE;
12639 12639 }
↓ open down ↓ |
12601 lines elided |
↑ open up ↑ |
12640 12640
12641 12641 if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12642 12642 /*
12643 12643 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12644 12644 * Convert the logical block number to target's physical sector
12645 12645 * number.
12646 12646 */
12647 12647 if (is_aligned) {
12648 12648 xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12649 12649 } else {
12650 - switch (un->un_f_rmw_type) {
12651 - case SD_RMW_TYPE_RETURN_ERROR:
12652 - if (un->un_f_enable_rmw)
12653 - break;
12654 - else {
12655 - bp->b_flags |= B_ERROR;
12656 - goto error_exit;
12657 - }
12658 -
12659 - case SD_RMW_TYPE_DEFAULT:
12650 + /*
12651 + * There is no RMW if we're just reading, so don't
12652 + * warn or error out because of it.
12653 + */
12654 + if (bp->b_flags & B_READ) {
12655 + /*EMPTY*/
12656 + } else if (!un->un_f_enable_rmw &&
12657 + un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12658 + bp->b_flags |= B_ERROR;
12659 + goto error_exit;
12660 + } else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12660 12661 mutex_enter(SD_MUTEX(un));
12661 12662 if (!un->un_f_enable_rmw &&
12662 12663 un->un_rmw_msg_timeid == NULL) {
12663 12664 scsi_log(SD_DEVINFO(un), sd_label,
12664 12665 CE_WARN, "I/O request is not "
12665 12666 "aligned with %d disk sector size. "
12666 12667 "It is handled through Read Modify "
12667 12668 "Write but the performance is "
12668 12669 "very low.\n",
12669 12670 un->un_tgt_blocksize);
12670 12671 un->un_rmw_msg_timeid =
12671 12672 timeout(sd_rmw_msg_print_handler,
12672 12673 un, SD_RMW_MSG_PRINT_TIMEOUT);
12673 12674 } else {
12674 12675 un->un_rmw_incre_count ++;
12675 12676 }
12676 12677 mutex_exit(SD_MUTEX(un));
12677 - break;
12678 -
12679 - case SD_RMW_TYPE_NO_WARNING:
12680 - default:
12681 - break;
12682 12678 }
12683 12679
12684 12680 nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12685 12681 partition_offset = SD_TGT2SYSBLOCK(un,
12686 12682 partition_offset);
12687 12683 }
12688 12684 }
12689 12685
12690 12686 /*
12691 12687 * blocknum is the starting block number of the request. At this
12692 12688 * point it is still relative to the start of the minor device.
12693 12689 */
12694 12690 blocknum = xp->xb_blkno;
12695 12691
12696 12692 /*
12697 12693 * Legacy: If the starting block number is one past the last block
12698 12694 * in the partition, do not set B_ERROR in the buf.
12699 12695 */
12700 12696 if (blocknum == nblocks) {
12701 12697 goto error_exit;
12702 12698 }
12703 12699
12704 12700 /*
12705 12701 * Confirm that the first block of the request lies within the
12706 12702 * partition limits. Also the requested number of bytes must be
12707 12703 * a multiple of the system block size.
12708 12704 */
12709 12705 if ((blocknum < 0) || (blocknum >= nblocks) ||
12710 12706 ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12711 12707 bp->b_flags |= B_ERROR;
12712 12708 goto error_exit;
12713 12709 }
12714 12710
12715 12711 /*
12716 12712 * If the requsted # blocks exceeds the available # blocks, that
12717 12713 * is an overrun of the partition.
12718 12714 */
12719 12715 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12720 12716 requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12721 12717 } else {
12722 12718 requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12723 12719 }
12724 12720
12725 12721 available_nblocks = (size_t)(nblocks - blocknum);
12726 12722 ASSERT(nblocks >= blocknum);
12727 12723
12728 12724 if (requested_nblocks > available_nblocks) {
12729 12725 size_t resid;
12730 12726
12731 12727 /*
12732 12728 * Allocate an "overrun" buf to allow the request to proceed
12733 12729 * for the amount of space available in the partition. The
12734 12730 * amount not transferred will be added into the b_resid
12735 12731 * when the operation is complete. The overrun buf
12736 12732 * replaces the original buf here, and the original buf
12737 12733 * is saved inside the overrun buf, for later use.
12738 12734 */
12739 12735 if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12740 12736 resid = SD_TGTBLOCKS2BYTES(un,
12741 12737 (offset_t)(requested_nblocks - available_nblocks));
12742 12738 } else {
12743 12739 resid = SD_SYSBLOCKS2BYTES(
12744 12740 (offset_t)(requested_nblocks - available_nblocks));
12745 12741 }
12746 12742
12747 12743 size_t count = bp->b_bcount - resid;
12748 12744 /*
12749 12745 * Note: count is an unsigned entity thus it'll NEVER
12750 12746 * be less than 0 so ASSERT the original values are
12751 12747 * correct.
12752 12748 */
12753 12749 ASSERT(bp->b_bcount >= resid);
12754 12750
12755 12751 bp = sd_bioclone_alloc(bp, count, blocknum,
12756 12752 (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12757 12753 xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12758 12754 ASSERT(xp != NULL);
12759 12755 }
12760 12756
12761 12757 /* At this point there should be no residual for this buf. */
12762 12758 ASSERT(bp->b_resid == 0);
12763 12759
12764 12760 /* Convert the block number to an absolute address. */
12765 12761 xp->xb_blkno += partition_offset;
12766 12762
12767 12763 SD_NEXT_IOSTART(index, un, bp);
12768 12764
12769 12765 SD_TRACE(SD_LOG_IO_PARTITION, un,
12770 12766 "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12771 12767
12772 12768 return;
12773 12769
12774 12770 error_exit:
12775 12771 bp->b_resid = bp->b_bcount;
12776 12772 SD_BEGIN_IODONE(index, un, bp);
12777 12773 SD_TRACE(SD_LOG_IO_PARTITION, un,
12778 12774 "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12779 12775 }
12780 12776
12781 12777
12782 12778 /*
12783 12779 * Function: sd_mapblockaddr_iodone
12784 12780 *
12785 12781 * Description: Completion-side processing for partition management.
12786 12782 *
12787 12783 * Context: May be called under interrupt context
12788 12784 */
12789 12785
12790 12786 static void
12791 12787 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12792 12788 {
12793 12789 /* int partition; */ /* Not used, see below. */
12794 12790 ASSERT(un != NULL);
12795 12791 ASSERT(bp != NULL);
12796 12792 ASSERT(!mutex_owned(SD_MUTEX(un)));
12797 12793
12798 12794 SD_TRACE(SD_LOG_IO_PARTITION, un,
12799 12795 "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12800 12796
12801 12797 if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12802 12798 /*
12803 12799 * We have an "overrun" buf to deal with...
12804 12800 */
12805 12801 struct sd_xbuf *xp;
12806 12802 struct buf *obp; /* ptr to the original buf */
12807 12803
12808 12804 xp = SD_GET_XBUF(bp);
12809 12805 ASSERT(xp != NULL);
12810 12806
12811 12807 /* Retrieve the pointer to the original buf */
12812 12808 obp = (struct buf *)xp->xb_private;
12813 12809 ASSERT(obp != NULL);
12814 12810
12815 12811 obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12816 12812 bioerror(obp, bp->b_error);
12817 12813
12818 12814 sd_bioclone_free(bp);
12819 12815
12820 12816 /*
12821 12817 * Get back the original buf.
12822 12818 * Note that since the restoration of xb_blkno below
12823 12819 * was removed, the sd_xbuf is not needed.
12824 12820 */
12825 12821 bp = obp;
12826 12822 /*
12827 12823 * xp = SD_GET_XBUF(bp);
12828 12824 * ASSERT(xp != NULL);
12829 12825 */
12830 12826 }
12831 12827
12832 12828 /*
12833 12829 * Convert sd->xb_blkno back to a minor-device relative value.
12834 12830 * Note: this has been commented out, as it is not needed in the
12835 12831 * current implementation of the driver (ie, since this function
12836 12832 * is at the top of the layering chains, so the info will be
12837 12833 * discarded) and it is in the "hot" IO path.
12838 12834 *
12839 12835 * partition = getminor(bp->b_edev) & SDPART_MASK;
12840 12836 * xp->xb_blkno -= un->un_offset[partition];
12841 12837 */
12842 12838
12843 12839 SD_NEXT_IODONE(index, un, bp);
12844 12840
12845 12841 SD_TRACE(SD_LOG_IO_PARTITION, un,
12846 12842 "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12847 12843 }
12848 12844
12849 12845
12850 12846 /*
12851 12847 * Function: sd_mapblocksize_iostart
12852 12848 *
12853 12849 * Description: Convert between system block size (un->un_sys_blocksize)
12854 12850 * and target block size (un->un_tgt_blocksize).
12855 12851 *
12856 12852 * Context: Can sleep to allocate resources.
12857 12853 *
12858 12854 * Assumptions: A higher layer has already performed any partition validation,
12859 12855 * and converted the xp->xb_blkno to an absolute value relative
12860 12856 * to the start of the device.
12861 12857 *
12862 12858 * It is also assumed that the higher layer has implemented
12863 12859 * an "overrun" mechanism for the case where the request would
12864 12860 * read/write beyond the end of a partition. In this case we
12865 12861 * assume (and ASSERT) that bp->b_resid == 0.
12866 12862 *
12867 12863 * Note: The implementation for this routine assumes the target
12868 12864 * block size remains constant between allocation and transport.
12869 12865 */
12870 12866
12871 12867 static void
12872 12868 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12873 12869 {
12874 12870 struct sd_mapblocksize_info *bsp;
12875 12871 struct sd_xbuf *xp;
12876 12872 offset_t first_byte;
12877 12873 daddr_t start_block, end_block;
12878 12874 daddr_t request_bytes;
12879 12875 ushort_t is_aligned = FALSE;
12880 12876
12881 12877 ASSERT(un != NULL);
12882 12878 ASSERT(bp != NULL);
12883 12879 ASSERT(!mutex_owned(SD_MUTEX(un)));
12884 12880 ASSERT(bp->b_resid == 0);
12885 12881
12886 12882 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12887 12883 "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12888 12884
12889 12885 /*
12890 12886 * For a non-writable CD, a write request is an error
12891 12887 */
12892 12888 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12893 12889 (un->un_f_mmc_writable_media == FALSE)) {
12894 12890 bioerror(bp, EIO);
12895 12891 bp->b_resid = bp->b_bcount;
12896 12892 SD_BEGIN_IODONE(index, un, bp);
12897 12893 return;
12898 12894 }
12899 12895
12900 12896 /*
12901 12897 * We do not need a shadow buf if the device is using
12902 12898 * un->un_sys_blocksize as its block size or if bcount == 0.
12903 12899 * In this case there is no layer-private data block allocated.
12904 12900 */
12905 12901 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12906 12902 (bp->b_bcount == 0)) {
12907 12903 goto done;
12908 12904 }
12909 12905
12910 12906 #if defined(__i386) || defined(__amd64)
12911 12907 /* We do not support non-block-aligned transfers for ROD devices */
12912 12908 ASSERT(!ISROD(un));
12913 12909 #endif
12914 12910
12915 12911 xp = SD_GET_XBUF(bp);
12916 12912 ASSERT(xp != NULL);
12917 12913
12918 12914 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12919 12915 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12920 12916 un->un_tgt_blocksize, DEV_BSIZE);
12921 12917 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12922 12918 "request start block:0x%x\n", xp->xb_blkno);
12923 12919 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12924 12920 "request len:0x%x\n", bp->b_bcount);
12925 12921
12926 12922 /*
12927 12923 * Allocate the layer-private data area for the mapblocksize layer.
12928 12924 * Layers are allowed to use the xp_private member of the sd_xbuf
12929 12925 * struct to store the pointer to their layer-private data block, but
12930 12926 * each layer also has the responsibility of restoring the prior
12931 12927 * contents of xb_private before returning the buf/xbuf to the
12932 12928 * higher layer that sent it.
12933 12929 *
12934 12930 * Here we save the prior contents of xp->xb_private into the
12935 12931 * bsp->mbs_oprivate field of our layer-private data area. This value
12936 12932 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12937 12933 * the layer-private area and returning the buf/xbuf to the layer
12938 12934 * that sent it.
12939 12935 *
12940 12936 * Note that here we use kmem_zalloc for the allocation as there are
12941 12937 * parts of the mapblocksize code that expect certain fields to be
12942 12938 * zero unless explicitly set to a required value.
12943 12939 */
12944 12940 bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12945 12941 bsp->mbs_oprivate = xp->xb_private;
12946 12942 xp->xb_private = bsp;
12947 12943
12948 12944 /*
12949 12945 * This treats the data on the disk (target) as an array of bytes.
12950 12946 * first_byte is the byte offset, from the beginning of the device,
12951 12947 * to the location of the request. This is converted from a
12952 12948 * un->un_sys_blocksize block address to a byte offset, and then back
12953 12949 * to a block address based upon a un->un_tgt_blocksize block size.
12954 12950 *
12955 12951 * xp->xb_blkno should be absolute upon entry into this function,
12956 12952 * but, but it is based upon partitions that use the "system"
12957 12953 * block size. It must be adjusted to reflect the block size of
12958 12954 * the target.
12959 12955 *
12960 12956 * Note that end_block is actually the block that follows the last
12961 12957 * block of the request, but that's what is needed for the computation.
12962 12958 */
12963 12959 first_byte = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12964 12960 if (un->un_f_enable_rmw) {
12965 12961 start_block = xp->xb_blkno =
12966 12962 (first_byte / un->un_phy_blocksize) *
12967 12963 (un->un_phy_blocksize / DEV_BSIZE);
12968 12964 end_block = ((first_byte + bp->b_bcount +
12969 12965 un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12970 12966 (un->un_phy_blocksize / DEV_BSIZE);
12971 12967 } else {
12972 12968 start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12973 12969 end_block = (first_byte + bp->b_bcount +
12974 12970 un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12975 12971 }
12976 12972
12977 12973 /* request_bytes is rounded up to a multiple of the target block size */
12978 12974 request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12979 12975
12980 12976 /*
12981 12977 * See if the starting address of the request and the request
12982 12978 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12983 12979 * then we do not need to allocate a shadow buf to handle the request.
12984 12980 */
12985 12981 if (un->un_f_enable_rmw) {
12986 12982 if (((first_byte % un->un_phy_blocksize) == 0) &&
12987 12983 ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12988 12984 is_aligned = TRUE;
12989 12985 }
12990 12986 } else {
12991 12987 if (((first_byte % un->un_tgt_blocksize) == 0) &&
12992 12988 ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12993 12989 is_aligned = TRUE;
12994 12990 }
12995 12991 }
12996 12992
12997 12993 if ((bp->b_flags & B_READ) == 0) {
12998 12994 /*
12999 12995 * Lock the range for a write operation. An aligned request is
13000 12996 * considered a simple write; otherwise the request must be a
13001 12997 * read-modify-write.
13002 12998 */
13003 12999 bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
13004 13000 (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
13005 13001 }
13006 13002
13007 13003 /*
13008 13004 * Alloc a shadow buf if the request is not aligned. Also, this is
13009 13005 * where the READ command is generated for a read-modify-write. (The
13010 13006 * write phase is deferred until after the read completes.)
13011 13007 */
13012 13008 if (is_aligned == FALSE) {
13013 13009
13014 13010 struct sd_mapblocksize_info *shadow_bsp;
13015 13011 struct sd_xbuf *shadow_xp;
13016 13012 struct buf *shadow_bp;
13017 13013
13018 13014 /*
13019 13015 * Allocate the shadow buf and it associated xbuf. Note that
13020 13016 * after this call the xb_blkno value in both the original
13021 13017 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
13022 13018 * same: absolute relative to the start of the device, and
13023 13019 * adjusted for the target block size. The b_blkno in the
13024 13020 * shadow buf will also be set to this value. We should never
13025 13021 * change b_blkno in the original bp however.
13026 13022 *
13027 13023 * Note also that the shadow buf will always need to be a
13028 13024 * READ command, regardless of whether the incoming command
13029 13025 * is a READ or a WRITE.
13030 13026 */
13031 13027 shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13032 13028 xp->xb_blkno,
13033 13029 (int (*)(struct buf *)) sd_mapblocksize_iodone);
13034 13030
13035 13031 shadow_xp = SD_GET_XBUF(shadow_bp);
13036 13032
13037 13033 /*
13038 13034 * Allocate the layer-private data for the shadow buf.
13039 13035 * (No need to preserve xb_private in the shadow xbuf.)
13040 13036 */
13041 13037 shadow_xp->xb_private = shadow_bsp =
13042 13038 kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13043 13039
13044 13040 /*
13045 13041 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13046 13042 * to figure out where the start of the user data is (based upon
13047 13043 * the system block size) in the data returned by the READ
13048 13044 * command (which will be based upon the target blocksize). Note
13049 13045 * that this is only really used if the request is unaligned.
13050 13046 */
13051 13047 if (un->un_f_enable_rmw) {
13052 13048 bsp->mbs_copy_offset = (ssize_t)(first_byte -
13053 13049 ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13054 13050 ASSERT((bsp->mbs_copy_offset >= 0) &&
13055 13051 (bsp->mbs_copy_offset < un->un_phy_blocksize));
13056 13052 } else {
13057 13053 bsp->mbs_copy_offset = (ssize_t)(first_byte -
13058 13054 ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13059 13055 ASSERT((bsp->mbs_copy_offset >= 0) &&
13060 13056 (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13061 13057 }
13062 13058
13063 13059 shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13064 13060
13065 13061 shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13066 13062
13067 13063 /* Transfer the wmap (if any) to the shadow buf */
13068 13064 shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13069 13065 bsp->mbs_wmp = NULL;
13070 13066
13071 13067 /*
13072 13068 * The shadow buf goes on from here in place of the
13073 13069 * original buf.
13074 13070 */
13075 13071 shadow_bsp->mbs_orig_bp = bp;
13076 13072 bp = shadow_bp;
13077 13073 }
13078 13074
13079 13075 SD_INFO(SD_LOG_IO_RMMEDIA, un,
13080 13076 "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13081 13077 SD_INFO(SD_LOG_IO_RMMEDIA, un,
13082 13078 "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13083 13079 request_bytes);
13084 13080 SD_INFO(SD_LOG_IO_RMMEDIA, un,
13085 13081 "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13086 13082
13087 13083 done:
13088 13084 SD_NEXT_IOSTART(index, un, bp);
13089 13085
13090 13086 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13091 13087 "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13092 13088 }
13093 13089
13094 13090
13095 13091 /*
13096 13092 * Function: sd_mapblocksize_iodone
13097 13093 *
13098 13094 * Description: Completion side processing for block-size mapping.
13099 13095 *
13100 13096 * Context: May be called under interrupt context
13101 13097 */
13102 13098
13103 13099 static void
13104 13100 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13105 13101 {
13106 13102 struct sd_mapblocksize_info *bsp;
13107 13103 struct sd_xbuf *xp;
13108 13104 struct sd_xbuf *orig_xp; /* sd_xbuf for the original buf */
13109 13105 struct buf *orig_bp; /* ptr to the original buf */
13110 13106 offset_t shadow_end;
13111 13107 offset_t request_end;
13112 13108 offset_t shadow_start;
13113 13109 ssize_t copy_offset;
13114 13110 size_t copy_length;
13115 13111 size_t shortfall;
13116 13112 uint_t is_write; /* TRUE if this bp is a WRITE */
13117 13113 uint_t has_wmap; /* TRUE is this bp has a wmap */
13118 13114
13119 13115 ASSERT(un != NULL);
13120 13116 ASSERT(bp != NULL);
13121 13117
13122 13118 SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13123 13119 "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13124 13120
13125 13121 /*
13126 13122 * There is no shadow buf or layer-private data if the target is
13127 13123 * using un->un_sys_blocksize as its block size or if bcount == 0.
13128 13124 */
13129 13125 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13130 13126 (bp->b_bcount == 0)) {
13131 13127 goto exit;
13132 13128 }
13133 13129
13134 13130 xp = SD_GET_XBUF(bp);
13135 13131 ASSERT(xp != NULL);
13136 13132
13137 13133 /* Retrieve the pointer to the layer-private data area from the xbuf. */
13138 13134 bsp = xp->xb_private;
13139 13135
13140 13136 is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13141 13137 has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13142 13138
13143 13139 if (is_write) {
13144 13140 /*
13145 13141 * For a WRITE request we must free up the block range that
13146 13142 * we have locked up. This holds regardless of whether this is
13147 13143 * an aligned write request or a read-modify-write request.
13148 13144 */
13149 13145 sd_range_unlock(un, bsp->mbs_wmp);
13150 13146 bsp->mbs_wmp = NULL;
13151 13147 }
13152 13148
13153 13149 if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13154 13150 /*
13155 13151 * An aligned read or write command will have no shadow buf;
13156 13152 * there is not much else to do with it.
13157 13153 */
13158 13154 goto done;
13159 13155 }
13160 13156
13161 13157 orig_bp = bsp->mbs_orig_bp;
13162 13158 ASSERT(orig_bp != NULL);
13163 13159 orig_xp = SD_GET_XBUF(orig_bp);
13164 13160 ASSERT(orig_xp != NULL);
13165 13161 ASSERT(!mutex_owned(SD_MUTEX(un)));
13166 13162
13167 13163 if (!is_write && has_wmap) {
13168 13164 /*
13169 13165 * A READ with a wmap means this is the READ phase of a
13170 13166 * read-modify-write. If an error occurred on the READ then
13171 13167 * we do not proceed with the WRITE phase or copy any data.
13172 13168 * Just release the write maps and return with an error.
13173 13169 */
13174 13170 if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13175 13171 orig_bp->b_resid = orig_bp->b_bcount;
13176 13172 bioerror(orig_bp, bp->b_error);
13177 13173 sd_range_unlock(un, bsp->mbs_wmp);
13178 13174 goto freebuf_done;
13179 13175 }
13180 13176 }
13181 13177
13182 13178 /*
13183 13179 * Here is where we set up to copy the data from the shadow buf
13184 13180 * into the space associated with the original buf.
13185 13181 *
13186 13182 * To deal with the conversion between block sizes, these
13187 13183 * computations treat the data as an array of bytes, with the
13188 13184 * first byte (byte 0) corresponding to the first byte in the
13189 13185 * first block on the disk.
13190 13186 */
13191 13187
13192 13188 /*
13193 13189 * shadow_start and shadow_len indicate the location and size of
13194 13190 * the data returned with the shadow IO request.
13195 13191 */
13196 13192 if (un->un_f_enable_rmw) {
13197 13193 shadow_start = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13198 13194 } else {
13199 13195 shadow_start = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13200 13196 }
13201 13197 shadow_end = shadow_start + bp->b_bcount - bp->b_resid;
13202 13198
13203 13199 /*
13204 13200 * copy_offset gives the offset (in bytes) from the start of the first
13205 13201 * block of the READ request to the beginning of the data. We retrieve
13206 13202 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13207 13203 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13208 13204 * data to be copied (in bytes).
13209 13205 */
13210 13206 copy_offset = bsp->mbs_copy_offset;
13211 13207 if (un->un_f_enable_rmw) {
13212 13208 ASSERT((copy_offset >= 0) &&
13213 13209 (copy_offset < un->un_phy_blocksize));
13214 13210 } else {
13215 13211 ASSERT((copy_offset >= 0) &&
13216 13212 (copy_offset < un->un_tgt_blocksize));
13217 13213 }
13218 13214
13219 13215 copy_length = orig_bp->b_bcount;
13220 13216 request_end = shadow_start + copy_offset + orig_bp->b_bcount;
13221 13217
13222 13218 /*
13223 13219 * Set up the resid and error fields of orig_bp as appropriate.
13224 13220 */
13225 13221 if (shadow_end >= request_end) {
13226 13222 /* We got all the requested data; set resid to zero */
13227 13223 orig_bp->b_resid = 0;
13228 13224 } else {
13229 13225 /*
13230 13226 * We failed to get enough data to fully satisfy the original
13231 13227 * request. Just copy back whatever data we got and set
13232 13228 * up the residual and error code as required.
13233 13229 *
13234 13230 * 'shortfall' is the amount by which the data received with the
13235 13231 * shadow buf has "fallen short" of the requested amount.
13236 13232 */
13237 13233 shortfall = (size_t)(request_end - shadow_end);
13238 13234
13239 13235 if (shortfall > orig_bp->b_bcount) {
13240 13236 /*
13241 13237 * We did not get enough data to even partially
13242 13238 * fulfill the original request. The residual is
13243 13239 * equal to the amount requested.
13244 13240 */
13245 13241 orig_bp->b_resid = orig_bp->b_bcount;
13246 13242 } else {
13247 13243 /*
13248 13244 * We did not get all the data that we requested
13249 13245 * from the device, but we will try to return what
13250 13246 * portion we did get.
13251 13247 */
13252 13248 orig_bp->b_resid = shortfall;
13253 13249 }
13254 13250 ASSERT(copy_length >= orig_bp->b_resid);
13255 13251 copy_length -= orig_bp->b_resid;
13256 13252 }
13257 13253
13258 13254 /* Propagate the error code from the shadow buf to the original buf */
13259 13255 bioerror(orig_bp, bp->b_error);
13260 13256
13261 13257 if (is_write) {
13262 13258 goto freebuf_done; /* No data copying for a WRITE */
13263 13259 }
13264 13260
13265 13261 if (has_wmap) {
13266 13262 /*
13267 13263 * This is a READ command from the READ phase of a
13268 13264 * read-modify-write request. We have to copy the data given
13269 13265 * by the user OVER the data returned by the READ command,
13270 13266 * then convert the command from a READ to a WRITE and send
13271 13267 * it back to the target.
13272 13268 */
13273 13269 bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13274 13270 copy_length);
13275 13271
13276 13272 bp->b_flags &= ~((int)B_READ); /* Convert to a WRITE */
13277 13273
13278 13274 /*
13279 13275 * Dispatch the WRITE command to the taskq thread, which
13280 13276 * will in turn send the command to the target. When the
13281 13277 * WRITE command completes, we (sd_mapblocksize_iodone())
13282 13278 * will get called again as part of the iodone chain
13283 13279 * processing for it. Note that we will still be dealing
13284 13280 * with the shadow buf at that point.
13285 13281 */
13286 13282 if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13287 13283 KM_NOSLEEP) != 0) {
13288 13284 /*
13289 13285 * Dispatch was successful so we are done. Return
13290 13286 * without going any higher up the iodone chain. Do
13291 13287 * not free up any layer-private data until after the
13292 13288 * WRITE completes.
13293 13289 */
13294 13290 return;
13295 13291 }
13296 13292
13297 13293 /*
13298 13294 * Dispatch of the WRITE command failed; set up the error
13299 13295 * condition and send this IO back up the iodone chain.
13300 13296 */
13301 13297 bioerror(orig_bp, EIO);
13302 13298 orig_bp->b_resid = orig_bp->b_bcount;
13303 13299
13304 13300 } else {
13305 13301 /*
13306 13302 * This is a regular READ request (ie, not a RMW). Copy the
13307 13303 * data from the shadow buf into the original buf. The
13308 13304 * copy_offset compensates for any "misalignment" between the
13309 13305 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13310 13306 * original buf (with its un->un_sys_blocksize blocks).
13311 13307 */
13312 13308 bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13313 13309 copy_length);
13314 13310 }
13315 13311
13316 13312 freebuf_done:
13317 13313
13318 13314 /*
13319 13315 * At this point we still have both the shadow buf AND the original
13320 13316 * buf to deal with, as well as the layer-private data area in each.
13321 13317 * Local variables are as follows:
13322 13318 *
13323 13319 * bp -- points to shadow buf
13324 13320 * xp -- points to xbuf of shadow buf
13325 13321 * bsp -- points to layer-private data area of shadow buf
13326 13322 * orig_bp -- points to original buf
13327 13323 *
13328 13324 * First free the shadow buf and its associated xbuf, then free the
13329 13325 * layer-private data area from the shadow buf. There is no need to
13330 13326 * restore xb_private in the shadow xbuf.
13331 13327 */
13332 13328 sd_shadow_buf_free(bp);
13333 13329 kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13334 13330
13335 13331 /*
13336 13332 * Now update the local variables to point to the original buf, xbuf,
13337 13333 * and layer-private area.
13338 13334 */
13339 13335 bp = orig_bp;
13340 13336 xp = SD_GET_XBUF(bp);
13341 13337 ASSERT(xp != NULL);
13342 13338 ASSERT(xp == orig_xp);
13343 13339 bsp = xp->xb_private;
13344 13340 ASSERT(bsp != NULL);
13345 13341
13346 13342 done:
13347 13343 /*
13348 13344 * Restore xb_private to whatever it was set to by the next higher
13349 13345 * layer in the chain, then free the layer-private data area.
13350 13346 */
13351 13347 xp->xb_private = bsp->mbs_oprivate;
13352 13348 kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13353 13349
13354 13350 exit:
13355 13351 SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13356 13352 "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13357 13353
13358 13354 SD_NEXT_IODONE(index, un, bp);
13359 13355 }
13360 13356
13361 13357
13362 13358 /*
13363 13359 * Function: sd_checksum_iostart
13364 13360 *
13365 13361 * Description: A stub function for a layer that's currently not used.
13366 13362 * For now just a placeholder.
13367 13363 *
13368 13364 * Context: Kernel thread context
13369 13365 */
13370 13366
13371 13367 static void
13372 13368 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13373 13369 {
13374 13370 ASSERT(un != NULL);
13375 13371 ASSERT(bp != NULL);
13376 13372 ASSERT(!mutex_owned(SD_MUTEX(un)));
13377 13373 SD_NEXT_IOSTART(index, un, bp);
13378 13374 }
13379 13375
13380 13376
13381 13377 /*
13382 13378 * Function: sd_checksum_iodone
13383 13379 *
13384 13380 * Description: A stub function for a layer that's currently not used.
13385 13381 * For now just a placeholder.
13386 13382 *
13387 13383 * Context: May be called under interrupt context
13388 13384 */
13389 13385
13390 13386 static void
13391 13387 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13392 13388 {
13393 13389 ASSERT(un != NULL);
13394 13390 ASSERT(bp != NULL);
13395 13391 ASSERT(!mutex_owned(SD_MUTEX(un)));
13396 13392 SD_NEXT_IODONE(index, un, bp);
13397 13393 }
13398 13394
13399 13395
13400 13396 /*
13401 13397 * Function: sd_checksum_uscsi_iostart
13402 13398 *
13403 13399 * Description: A stub function for a layer that's currently not used.
13404 13400 * For now just a placeholder.
13405 13401 *
13406 13402 * Context: Kernel thread context
13407 13403 */
13408 13404
13409 13405 static void
13410 13406 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13411 13407 {
13412 13408 ASSERT(un != NULL);
13413 13409 ASSERT(bp != NULL);
13414 13410 ASSERT(!mutex_owned(SD_MUTEX(un)));
13415 13411 SD_NEXT_IOSTART(index, un, bp);
13416 13412 }
13417 13413
13418 13414
13419 13415 /*
13420 13416 * Function: sd_checksum_uscsi_iodone
13421 13417 *
13422 13418 * Description: A stub function for a layer that's currently not used.
13423 13419 * For now just a placeholder.
13424 13420 *
13425 13421 * Context: May be called under interrupt context
13426 13422 */
13427 13423
13428 13424 static void
13429 13425 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13430 13426 {
13431 13427 ASSERT(un != NULL);
13432 13428 ASSERT(bp != NULL);
13433 13429 ASSERT(!mutex_owned(SD_MUTEX(un)));
13434 13430 SD_NEXT_IODONE(index, un, bp);
13435 13431 }
13436 13432
13437 13433
13438 13434 /*
13439 13435 * Function: sd_pm_iostart
13440 13436 *
13441 13437 * Description: iostart-side routine for Power mangement.
13442 13438 *
13443 13439 * Context: Kernel thread context
13444 13440 */
13445 13441
13446 13442 static void
13447 13443 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13448 13444 {
13449 13445 ASSERT(un != NULL);
13450 13446 ASSERT(bp != NULL);
13451 13447 ASSERT(!mutex_owned(SD_MUTEX(un)));
13452 13448 ASSERT(!mutex_owned(&un->un_pm_mutex));
13453 13449
13454 13450 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13455 13451
13456 13452 if (sd_pm_entry(un) != DDI_SUCCESS) {
13457 13453 /*
13458 13454 * Set up to return the failed buf back up the 'iodone'
13459 13455 * side of the calling chain.
13460 13456 */
13461 13457 bioerror(bp, EIO);
13462 13458 bp->b_resid = bp->b_bcount;
13463 13459
13464 13460 SD_BEGIN_IODONE(index, un, bp);
13465 13461
13466 13462 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13467 13463 return;
13468 13464 }
13469 13465
13470 13466 SD_NEXT_IOSTART(index, un, bp);
13471 13467
13472 13468 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13473 13469 }
13474 13470
13475 13471
13476 13472 /*
13477 13473 * Function: sd_pm_iodone
13478 13474 *
13479 13475 * Description: iodone-side routine for power mangement.
13480 13476 *
13481 13477 * Context: may be called from interrupt context
13482 13478 */
13483 13479
13484 13480 static void
13485 13481 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13486 13482 {
13487 13483 ASSERT(un != NULL);
13488 13484 ASSERT(bp != NULL);
13489 13485 ASSERT(!mutex_owned(&un->un_pm_mutex));
13490 13486
13491 13487 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13492 13488
13493 13489 /*
13494 13490 * After attach the following flag is only read, so don't
13495 13491 * take the penalty of acquiring a mutex for it.
13496 13492 */
13497 13493 if (un->un_f_pm_is_enabled == TRUE) {
13498 13494 sd_pm_exit(un);
13499 13495 }
13500 13496
13501 13497 SD_NEXT_IODONE(index, un, bp);
13502 13498
13503 13499 SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13504 13500 }
13505 13501
13506 13502
13507 13503 /*
13508 13504 * Function: sd_core_iostart
13509 13505 *
13510 13506 * Description: Primary driver function for enqueuing buf(9S) structs from
13511 13507 * the system and initiating IO to the target device
13512 13508 *
13513 13509 * Context: Kernel thread context. Can sleep.
13514 13510 *
13515 13511 * Assumptions: - The given xp->xb_blkno is absolute
13516 13512 * (ie, relative to the start of the device).
13517 13513 * - The IO is to be done using the native blocksize of
13518 13514 * the device, as specified in un->un_tgt_blocksize.
13519 13515 */
13520 13516 /* ARGSUSED */
13521 13517 static void
13522 13518 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13523 13519 {
13524 13520 struct sd_xbuf *xp;
13525 13521
13526 13522 ASSERT(un != NULL);
13527 13523 ASSERT(bp != NULL);
13528 13524 ASSERT(!mutex_owned(SD_MUTEX(un)));
13529 13525 ASSERT(bp->b_resid == 0);
13530 13526
13531 13527 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13532 13528
13533 13529 xp = SD_GET_XBUF(bp);
13534 13530 ASSERT(xp != NULL);
13535 13531
13536 13532 mutex_enter(SD_MUTEX(un));
13537 13533
13538 13534 /*
13539 13535 * If we are currently in the failfast state, fail any new IO
13540 13536 * that has B_FAILFAST set, then return.
13541 13537 */
13542 13538 if ((bp->b_flags & B_FAILFAST) &&
13543 13539 (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13544 13540 mutex_exit(SD_MUTEX(un));
13545 13541 bioerror(bp, EIO);
13546 13542 bp->b_resid = bp->b_bcount;
13547 13543 SD_BEGIN_IODONE(index, un, bp);
13548 13544 return;
13549 13545 }
13550 13546
13551 13547 if (SD_IS_DIRECT_PRIORITY(xp)) {
13552 13548 /*
13553 13549 * Priority command -- transport it immediately.
13554 13550 *
13555 13551 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13556 13552 * because all direct priority commands should be associated
13557 13553 * with error recovery actions which we don't want to retry.
13558 13554 */
13559 13555 sd_start_cmds(un, bp);
13560 13556 } else {
13561 13557 /*
13562 13558 * Normal command -- add it to the wait queue, then start
13563 13559 * transporting commands from the wait queue.
13564 13560 */
13565 13561 sd_add_buf_to_waitq(un, bp);
13566 13562 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13567 13563 sd_start_cmds(un, NULL);
13568 13564 }
13569 13565
13570 13566 mutex_exit(SD_MUTEX(un));
13571 13567
13572 13568 SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13573 13569 }
13574 13570
13575 13571
13576 13572 /*
13577 13573 * Function: sd_init_cdb_limits
13578 13574 *
13579 13575 * Description: This is to handle scsi_pkt initialization differences
13580 13576 * between the driver platforms.
13581 13577 *
13582 13578 * Legacy behaviors:
13583 13579 *
13584 13580 * If the block number or the sector count exceeds the
13585 13581 * capabilities of a Group 0 command, shift over to a
13586 13582 * Group 1 command. We don't blindly use Group 1
13587 13583 * commands because a) some drives (CDC Wren IVs) get a
13588 13584 * bit confused, and b) there is probably a fair amount
13589 13585 * of speed difference for a target to receive and decode
13590 13586 * a 10 byte command instead of a 6 byte command.
13591 13587 *
13592 13588 * The xfer time difference of 6 vs 10 byte CDBs is
13593 13589 * still significant so this code is still worthwhile.
13594 13590 * 10 byte CDBs are very inefficient with the fas HBA driver
13595 13591 * and older disks. Each CDB byte took 1 usec with some
13596 13592 * popular disks.
13597 13593 *
13598 13594 * Context: Must be called at attach time
13599 13595 */
13600 13596
13601 13597 static void
13602 13598 sd_init_cdb_limits(struct sd_lun *un)
13603 13599 {
13604 13600 int hba_cdb_limit;
13605 13601
13606 13602 /*
13607 13603 * Use CDB_GROUP1 commands for most devices except for
13608 13604 * parallel SCSI fixed drives in which case we get better
13609 13605 * performance using CDB_GROUP0 commands (where applicable).
13610 13606 */
13611 13607 un->un_mincdb = SD_CDB_GROUP1;
13612 13608 #if !defined(__fibre)
13613 13609 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13614 13610 !un->un_f_has_removable_media) {
13615 13611 un->un_mincdb = SD_CDB_GROUP0;
13616 13612 }
13617 13613 #endif
13618 13614
13619 13615 /*
13620 13616 * Try to read the max-cdb-length supported by HBA.
13621 13617 */
13622 13618 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13623 13619 if (0 >= un->un_max_hba_cdb) {
13624 13620 un->un_max_hba_cdb = CDB_GROUP4;
13625 13621 hba_cdb_limit = SD_CDB_GROUP4;
13626 13622 } else if (0 < un->un_max_hba_cdb &&
13627 13623 un->un_max_hba_cdb < CDB_GROUP1) {
13628 13624 hba_cdb_limit = SD_CDB_GROUP0;
13629 13625 } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13630 13626 un->un_max_hba_cdb < CDB_GROUP5) {
13631 13627 hba_cdb_limit = SD_CDB_GROUP1;
13632 13628 } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13633 13629 un->un_max_hba_cdb < CDB_GROUP4) {
13634 13630 hba_cdb_limit = SD_CDB_GROUP5;
13635 13631 } else {
13636 13632 hba_cdb_limit = SD_CDB_GROUP4;
13637 13633 }
13638 13634
13639 13635 /*
13640 13636 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4
13641 13637 * commands for fixed disks unless we are building for a 32 bit
13642 13638 * kernel.
13643 13639 */
13644 13640 #ifdef _LP64
13645 13641 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13646 13642 min(hba_cdb_limit, SD_CDB_GROUP4);
13647 13643 #else
13648 13644 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13649 13645 min(hba_cdb_limit, SD_CDB_GROUP1);
13650 13646 #endif
13651 13647
13652 13648 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13653 13649 ? sizeof (struct scsi_arq_status) : 1);
13654 13650 un->un_cmd_timeout = (ushort_t)sd_io_time;
13655 13651 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13656 13652 }
13657 13653
13658 13654
13659 13655 /*
13660 13656 * Function: sd_initpkt_for_buf
13661 13657 *
13662 13658 * Description: Allocate and initialize for transport a scsi_pkt struct,
13663 13659 * based upon the info specified in the given buf struct.
13664 13660 *
13665 13661 * Assumes the xb_blkno in the request is absolute (ie,
13666 13662 * relative to the start of the device (NOT partition!).
13667 13663 * Also assumes that the request is using the native block
13668 13664 * size of the device (as returned by the READ CAPACITY
13669 13665 * command).
13670 13666 *
13671 13667 * Return Code: SD_PKT_ALLOC_SUCCESS
13672 13668 * SD_PKT_ALLOC_FAILURE
13673 13669 * SD_PKT_ALLOC_FAILURE_NO_DMA
13674 13670 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13675 13671 *
13676 13672 * Context: Kernel thread and may be called from software interrupt context
13677 13673 * as part of a sdrunout callback. This function may not block or
13678 13674 * call routines that block
13679 13675 */
13680 13676
13681 13677 static int
13682 13678 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13683 13679 {
13684 13680 struct sd_xbuf *xp;
13685 13681 struct scsi_pkt *pktp = NULL;
13686 13682 struct sd_lun *un;
13687 13683 size_t blockcount;
13688 13684 daddr_t startblock;
13689 13685 int rval;
13690 13686 int cmd_flags;
13691 13687
13692 13688 ASSERT(bp != NULL);
13693 13689 ASSERT(pktpp != NULL);
13694 13690 xp = SD_GET_XBUF(bp);
13695 13691 ASSERT(xp != NULL);
13696 13692 un = SD_GET_UN(bp);
13697 13693 ASSERT(un != NULL);
13698 13694 ASSERT(mutex_owned(SD_MUTEX(un)));
13699 13695 ASSERT(bp->b_resid == 0);
13700 13696
13701 13697 SD_TRACE(SD_LOG_IO_CORE, un,
13702 13698 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13703 13699
13704 13700 mutex_exit(SD_MUTEX(un));
13705 13701
13706 13702 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13707 13703 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13708 13704 /*
13709 13705 * Already have a scsi_pkt -- just need DMA resources.
13710 13706 * We must recompute the CDB in case the mapping returns
13711 13707 * a nonzero pkt_resid.
13712 13708 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13713 13709 * that is being retried, the unmap/remap of the DMA resouces
13714 13710 * will result in the entire transfer starting over again
13715 13711 * from the very first block.
13716 13712 */
13717 13713 ASSERT(xp->xb_pktp != NULL);
13718 13714 pktp = xp->xb_pktp;
13719 13715 } else {
13720 13716 pktp = NULL;
13721 13717 }
13722 13718 #endif /* __i386 || __amd64 */
13723 13719
13724 13720 startblock = xp->xb_blkno; /* Absolute block num. */
13725 13721 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13726 13722
13727 13723 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13728 13724
13729 13725 /*
13730 13726 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13731 13727 * call scsi_init_pkt, and build the CDB.
13732 13728 */
13733 13729 rval = sd_setup_rw_pkt(un, &pktp, bp,
13734 13730 cmd_flags, sdrunout, (caddr_t)un,
13735 13731 startblock, blockcount);
13736 13732
13737 13733 if (rval == 0) {
13738 13734 /*
13739 13735 * Success.
13740 13736 *
13741 13737 * If partial DMA is being used and required for this transfer.
13742 13738 * set it up here.
13743 13739 */
13744 13740 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13745 13741 (pktp->pkt_resid != 0)) {
13746 13742
13747 13743 /*
13748 13744 * Save the CDB length and pkt_resid for the
13749 13745 * next xfer
13750 13746 */
13751 13747 xp->xb_dma_resid = pktp->pkt_resid;
13752 13748
13753 13749 /* rezero resid */
13754 13750 pktp->pkt_resid = 0;
13755 13751
13756 13752 } else {
13757 13753 xp->xb_dma_resid = 0;
13758 13754 }
13759 13755
13760 13756 pktp->pkt_flags = un->un_tagflags;
13761 13757 pktp->pkt_time = un->un_cmd_timeout;
13762 13758 pktp->pkt_comp = sdintr;
13763 13759
13764 13760 pktp->pkt_private = bp;
13765 13761 *pktpp = pktp;
13766 13762
13767 13763 SD_TRACE(SD_LOG_IO_CORE, un,
13768 13764 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13769 13765
13770 13766 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13771 13767 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13772 13768 #endif
13773 13769
13774 13770 mutex_enter(SD_MUTEX(un));
13775 13771 return (SD_PKT_ALLOC_SUCCESS);
13776 13772
13777 13773 }
13778 13774
13779 13775 /*
13780 13776 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13781 13777 * from sd_setup_rw_pkt.
13782 13778 */
13783 13779 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13784 13780
13785 13781 if (rval == SD_PKT_ALLOC_FAILURE) {
13786 13782 *pktpp = NULL;
13787 13783 /*
13788 13784 * Set the driver state to RWAIT to indicate the driver
13789 13785 * is waiting on resource allocations. The driver will not
13790 13786 * suspend, pm_suspend, or detatch while the state is RWAIT.
13791 13787 */
13792 13788 mutex_enter(SD_MUTEX(un));
13793 13789 New_state(un, SD_STATE_RWAIT);
13794 13790
13795 13791 SD_ERROR(SD_LOG_IO_CORE, un,
13796 13792 "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13797 13793
13798 13794 if ((bp->b_flags & B_ERROR) != 0) {
13799 13795 return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13800 13796 }
13801 13797 return (SD_PKT_ALLOC_FAILURE);
13802 13798 } else {
13803 13799 /*
13804 13800 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13805 13801 *
13806 13802 * This should never happen. Maybe someone messed with the
13807 13803 * kernel's minphys?
13808 13804 */
13809 13805 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13810 13806 "Request rejected: too large for CDB: "
13811 13807 "lba:0x%08lx len:0x%08lx\n", startblock, blockcount);
13812 13808 SD_ERROR(SD_LOG_IO_CORE, un,
13813 13809 "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13814 13810 mutex_enter(SD_MUTEX(un));
13815 13811 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13816 13812
13817 13813 }
13818 13814 }
13819 13815
13820 13816
13821 13817 /*
13822 13818 * Function: sd_destroypkt_for_buf
13823 13819 *
13824 13820 * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13825 13821 *
13826 13822 * Context: Kernel thread or interrupt context
13827 13823 */
13828 13824
13829 13825 static void
13830 13826 sd_destroypkt_for_buf(struct buf *bp)
13831 13827 {
13832 13828 ASSERT(bp != NULL);
13833 13829 ASSERT(SD_GET_UN(bp) != NULL);
13834 13830
13835 13831 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13836 13832 "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13837 13833
13838 13834 ASSERT(SD_GET_PKTP(bp) != NULL);
13839 13835 scsi_destroy_pkt(SD_GET_PKTP(bp));
13840 13836
13841 13837 SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13842 13838 "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13843 13839 }
13844 13840
13845 13841 /*
13846 13842 * Function: sd_setup_rw_pkt
13847 13843 *
13848 13844 * Description: Determines appropriate CDB group for the requested LBA
13849 13845 * and transfer length, calls scsi_init_pkt, and builds
13850 13846 * the CDB. Do not use for partial DMA transfers except
13851 13847 * for the initial transfer since the CDB size must
13852 13848 * remain constant.
13853 13849 *
13854 13850 * Context: Kernel thread and may be called from software interrupt
13855 13851 * context as part of a sdrunout callback. This function may not
13856 13852 * block or call routines that block
13857 13853 */
13858 13854
13859 13855
13860 13856 int
13861 13857 sd_setup_rw_pkt(struct sd_lun *un,
13862 13858 struct scsi_pkt **pktpp, struct buf *bp, int flags,
13863 13859 int (*callback)(caddr_t), caddr_t callback_arg,
13864 13860 diskaddr_t lba, uint32_t blockcount)
13865 13861 {
13866 13862 struct scsi_pkt *return_pktp;
13867 13863 union scsi_cdb *cdbp;
13868 13864 struct sd_cdbinfo *cp = NULL;
13869 13865 int i;
13870 13866
13871 13867 /*
13872 13868 * See which size CDB to use, based upon the request.
13873 13869 */
13874 13870 for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13875 13871
13876 13872 /*
13877 13873 * Check lba and block count against sd_cdbtab limits.
13878 13874 * In the partial DMA case, we have to use the same size
13879 13875 * CDB for all the transfers. Check lba + blockcount
13880 13876 * against the max LBA so we know that segment of the
13881 13877 * transfer can use the CDB we select.
13882 13878 */
13883 13879 if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13884 13880 (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13885 13881
13886 13882 /*
13887 13883 * The command will fit into the CDB type
13888 13884 * specified by sd_cdbtab[i].
13889 13885 */
13890 13886 cp = sd_cdbtab + i;
13891 13887
13892 13888 /*
13893 13889 * Call scsi_init_pkt so we can fill in the
13894 13890 * CDB.
13895 13891 */
13896 13892 return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13897 13893 bp, cp->sc_grpcode, un->un_status_len, 0,
13898 13894 flags, callback, callback_arg);
13899 13895
13900 13896 if (return_pktp != NULL) {
13901 13897
13902 13898 /*
13903 13899 * Return new value of pkt
13904 13900 */
13905 13901 *pktpp = return_pktp;
13906 13902
13907 13903 /*
13908 13904 * To be safe, zero the CDB insuring there is
13909 13905 * no leftover data from a previous command.
13910 13906 */
13911 13907 bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13912 13908
13913 13909 /*
13914 13910 * Handle partial DMA mapping
13915 13911 */
13916 13912 if (return_pktp->pkt_resid != 0) {
13917 13913
13918 13914 /*
13919 13915 * Not going to xfer as many blocks as
13920 13916 * originally expected
13921 13917 */
13922 13918 blockcount -=
13923 13919 SD_BYTES2TGTBLOCKS(un,
13924 13920 return_pktp->pkt_resid);
13925 13921 }
13926 13922
13927 13923 cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13928 13924
13929 13925 /*
13930 13926 * Set command byte based on the CDB
13931 13927 * type we matched.
13932 13928 */
13933 13929 cdbp->scc_cmd = cp->sc_grpmask |
13934 13930 ((bp->b_flags & B_READ) ?
13935 13931 SCMD_READ : SCMD_WRITE);
13936 13932
13937 13933 SD_FILL_SCSI1_LUN(un, return_pktp);
13938 13934
13939 13935 /*
13940 13936 * Fill in LBA and length
13941 13937 */
13942 13938 ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13943 13939 (cp->sc_grpcode == CDB_GROUP4) ||
13944 13940 (cp->sc_grpcode == CDB_GROUP0) ||
13945 13941 (cp->sc_grpcode == CDB_GROUP5));
13946 13942
13947 13943 if (cp->sc_grpcode == CDB_GROUP1) {
13948 13944 FORMG1ADDR(cdbp, lba);
13949 13945 FORMG1COUNT(cdbp, blockcount);
13950 13946 return (0);
13951 13947 } else if (cp->sc_grpcode == CDB_GROUP4) {
13952 13948 FORMG4LONGADDR(cdbp, lba);
13953 13949 FORMG4COUNT(cdbp, blockcount);
13954 13950 return (0);
13955 13951 } else if (cp->sc_grpcode == CDB_GROUP0) {
13956 13952 FORMG0ADDR(cdbp, lba);
13957 13953 FORMG0COUNT(cdbp, blockcount);
13958 13954 return (0);
13959 13955 } else if (cp->sc_grpcode == CDB_GROUP5) {
13960 13956 FORMG5ADDR(cdbp, lba);
13961 13957 FORMG5COUNT(cdbp, blockcount);
13962 13958 return (0);
13963 13959 }
13964 13960
13965 13961 /*
13966 13962 * It should be impossible to not match one
13967 13963 * of the CDB types above, so we should never
13968 13964 * reach this point. Set the CDB command byte
13969 13965 * to test-unit-ready to avoid writing
13970 13966 * to somewhere we don't intend.
13971 13967 */
13972 13968 cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13973 13969 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13974 13970 } else {
13975 13971 /*
13976 13972 * Couldn't get scsi_pkt
13977 13973 */
13978 13974 return (SD_PKT_ALLOC_FAILURE);
13979 13975 }
13980 13976 }
13981 13977 }
13982 13978
13983 13979 /*
13984 13980 * None of the available CDB types were suitable. This really
13985 13981 * should never happen: on a 64 bit system we support
13986 13982 * READ16/WRITE16 which will hold an entire 64 bit disk address
13987 13983 * and on a 32 bit system we will refuse to bind to a device
13988 13984 * larger than 2TB so addresses will never be larger than 32 bits.
13989 13985 */
13990 13986 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13991 13987 }
13992 13988
13993 13989 /*
13994 13990 * Function: sd_setup_next_rw_pkt
13995 13991 *
13996 13992 * Description: Setup packet for partial DMA transfers, except for the
13997 13993 * initial transfer. sd_setup_rw_pkt should be used for
13998 13994 * the initial transfer.
13999 13995 *
14000 13996 * Context: Kernel thread and may be called from interrupt context.
14001 13997 */
14002 13998
14003 13999 int
14004 14000 sd_setup_next_rw_pkt(struct sd_lun *un,
14005 14001 struct scsi_pkt *pktp, struct buf *bp,
14006 14002 diskaddr_t lba, uint32_t blockcount)
14007 14003 {
14008 14004 uchar_t com;
14009 14005 union scsi_cdb *cdbp;
14010 14006 uchar_t cdb_group_id;
14011 14007
14012 14008 ASSERT(pktp != NULL);
14013 14009 ASSERT(pktp->pkt_cdbp != NULL);
14014 14010
14015 14011 cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
14016 14012 com = cdbp->scc_cmd;
14017 14013 cdb_group_id = CDB_GROUPID(com);
14018 14014
14019 14015 ASSERT((cdb_group_id == CDB_GROUPID_0) ||
14020 14016 (cdb_group_id == CDB_GROUPID_1) ||
14021 14017 (cdb_group_id == CDB_GROUPID_4) ||
14022 14018 (cdb_group_id == CDB_GROUPID_5));
14023 14019
14024 14020 /*
14025 14021 * Move pkt to the next portion of the xfer.
14026 14022 * func is NULL_FUNC so we do not have to release
14027 14023 * the disk mutex here.
14028 14024 */
14029 14025 if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
14030 14026 NULL_FUNC, NULL) == pktp) {
14031 14027 /* Success. Handle partial DMA */
14032 14028 if (pktp->pkt_resid != 0) {
14033 14029 blockcount -=
14034 14030 SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14035 14031 }
14036 14032
14037 14033 cdbp->scc_cmd = com;
14038 14034 SD_FILL_SCSI1_LUN(un, pktp);
14039 14035 if (cdb_group_id == CDB_GROUPID_1) {
14040 14036 FORMG1ADDR(cdbp, lba);
14041 14037 FORMG1COUNT(cdbp, blockcount);
14042 14038 return (0);
14043 14039 } else if (cdb_group_id == CDB_GROUPID_4) {
14044 14040 FORMG4LONGADDR(cdbp, lba);
14045 14041 FORMG4COUNT(cdbp, blockcount);
14046 14042 return (0);
14047 14043 } else if (cdb_group_id == CDB_GROUPID_0) {
14048 14044 FORMG0ADDR(cdbp, lba);
14049 14045 FORMG0COUNT(cdbp, blockcount);
14050 14046 return (0);
14051 14047 } else if (cdb_group_id == CDB_GROUPID_5) {
14052 14048 FORMG5ADDR(cdbp, lba);
14053 14049 FORMG5COUNT(cdbp, blockcount);
14054 14050 return (0);
14055 14051 }
14056 14052
14057 14053 /* Unreachable */
14058 14054 return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14059 14055 }
14060 14056
14061 14057 /*
14062 14058 * Error setting up next portion of cmd transfer.
14063 14059 * Something is definitely very wrong and this
14064 14060 * should not happen.
14065 14061 */
14066 14062 return (SD_PKT_ALLOC_FAILURE);
14067 14063 }
14068 14064
14069 14065 /*
14070 14066 * Function: sd_initpkt_for_uscsi
14071 14067 *
14072 14068 * Description: Allocate and initialize for transport a scsi_pkt struct,
14073 14069 * based upon the info specified in the given uscsi_cmd struct.
14074 14070 *
14075 14071 * Return Code: SD_PKT_ALLOC_SUCCESS
14076 14072 * SD_PKT_ALLOC_FAILURE
14077 14073 * SD_PKT_ALLOC_FAILURE_NO_DMA
14078 14074 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14079 14075 *
14080 14076 * Context: Kernel thread and may be called from software interrupt context
14081 14077 * as part of a sdrunout callback. This function may not block or
14082 14078 * call routines that block
14083 14079 */
14084 14080
14085 14081 static int
14086 14082 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14087 14083 {
14088 14084 struct uscsi_cmd *uscmd;
14089 14085 struct sd_xbuf *xp;
14090 14086 struct scsi_pkt *pktp;
14091 14087 struct sd_lun *un;
14092 14088 uint32_t flags = 0;
14093 14089
14094 14090 ASSERT(bp != NULL);
14095 14091 ASSERT(pktpp != NULL);
14096 14092 xp = SD_GET_XBUF(bp);
14097 14093 ASSERT(xp != NULL);
14098 14094 un = SD_GET_UN(bp);
14099 14095 ASSERT(un != NULL);
14100 14096 ASSERT(mutex_owned(SD_MUTEX(un)));
14101 14097
14102 14098 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14103 14099 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14104 14100 ASSERT(uscmd != NULL);
14105 14101
14106 14102 SD_TRACE(SD_LOG_IO_CORE, un,
14107 14103 "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14108 14104
14109 14105 /*
14110 14106 * Allocate the scsi_pkt for the command.
14111 14107 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14112 14108 * during scsi_init_pkt time and will continue to use the
14113 14109 * same path as long as the same scsi_pkt is used without
14114 14110 * intervening scsi_dma_free(). Since uscsi command does
14115 14111 * not call scsi_dmafree() before retry failed command, it
14116 14112 * is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14117 14113 * set such that scsi_vhci can use other available path for
14118 14114 * retry. Besides, ucsci command does not allow DMA breakup,
14119 14115 * so there is no need to set PKT_DMA_PARTIAL flag.
14120 14116 */
14121 14117 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14122 14118 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14123 14119 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14124 14120 ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14125 14121 - sizeof (struct scsi_extended_sense)), 0,
14126 14122 (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14127 14123 sdrunout, (caddr_t)un);
14128 14124 } else {
14129 14125 pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14130 14126 ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14131 14127 sizeof (struct scsi_arq_status), 0,
14132 14128 (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14133 14129 sdrunout, (caddr_t)un);
14134 14130 }
14135 14131
14136 14132 if (pktp == NULL) {
14137 14133 *pktpp = NULL;
14138 14134 /*
14139 14135 * Set the driver state to RWAIT to indicate the driver
14140 14136 * is waiting on resource allocations. The driver will not
14141 14137 * suspend, pm_suspend, or detatch while the state is RWAIT.
14142 14138 */
14143 14139 New_state(un, SD_STATE_RWAIT);
14144 14140
14145 14141 SD_ERROR(SD_LOG_IO_CORE, un,
14146 14142 "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14147 14143
14148 14144 if ((bp->b_flags & B_ERROR) != 0) {
14149 14145 return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14150 14146 }
14151 14147 return (SD_PKT_ALLOC_FAILURE);
14152 14148 }
14153 14149
14154 14150 /*
14155 14151 * We do not do DMA breakup for USCSI commands, so return failure
14156 14152 * here if all the needed DMA resources were not allocated.
14157 14153 */
14158 14154 if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14159 14155 (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14160 14156 scsi_destroy_pkt(pktp);
14161 14157 SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14162 14158 "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14163 14159 return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14164 14160 }
14165 14161
14166 14162 /* Init the cdb from the given uscsi struct */
14167 14163 (void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14168 14164 uscmd->uscsi_cdb[0], 0, 0, 0);
14169 14165
14170 14166 SD_FILL_SCSI1_LUN(un, pktp);
14171 14167
14172 14168 /*
14173 14169 * Set up the optional USCSI flags. See the uscsi (7I) man page
14174 14170 * for listing of the supported flags.
14175 14171 */
14176 14172
14177 14173 if (uscmd->uscsi_flags & USCSI_SILENT) {
14178 14174 flags |= FLAG_SILENT;
14179 14175 }
14180 14176
14181 14177 if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14182 14178 flags |= FLAG_DIAGNOSE;
14183 14179 }
14184 14180
14185 14181 if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14186 14182 flags |= FLAG_ISOLATE;
14187 14183 }
14188 14184
14189 14185 if (un->un_f_is_fibre == FALSE) {
14190 14186 if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14191 14187 flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14192 14188 }
14193 14189 }
14194 14190
14195 14191 /*
14196 14192 * Set the pkt flags here so we save time later.
14197 14193 * Note: These flags are NOT in the uscsi man page!!!
14198 14194 */
14199 14195 if (uscmd->uscsi_flags & USCSI_HEAD) {
14200 14196 flags |= FLAG_HEAD;
14201 14197 }
14202 14198
14203 14199 if (uscmd->uscsi_flags & USCSI_NOINTR) {
14204 14200 flags |= FLAG_NOINTR;
14205 14201 }
14206 14202
14207 14203 /*
14208 14204 * For tagged queueing, things get a bit complicated.
14209 14205 * Check first for head of queue and last for ordered queue.
14210 14206 * If neither head nor order, use the default driver tag flags.
14211 14207 */
14212 14208 if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14213 14209 if (uscmd->uscsi_flags & USCSI_HTAG) {
14214 14210 flags |= FLAG_HTAG;
14215 14211 } else if (uscmd->uscsi_flags & USCSI_OTAG) {
14216 14212 flags |= FLAG_OTAG;
14217 14213 } else {
14218 14214 flags |= un->un_tagflags & FLAG_TAGMASK;
14219 14215 }
14220 14216 }
14221 14217
14222 14218 if (uscmd->uscsi_flags & USCSI_NODISCON) {
14223 14219 flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14224 14220 }
14225 14221
14226 14222 pktp->pkt_flags = flags;
14227 14223
14228 14224 /* Transfer uscsi information to scsi_pkt */
14229 14225 (void) scsi_uscsi_pktinit(uscmd, pktp);
14230 14226
14231 14227 /* Copy the caller's CDB into the pkt... */
14232 14228 bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14233 14229
14234 14230 if (uscmd->uscsi_timeout == 0) {
14235 14231 pktp->pkt_time = un->un_uscsi_timeout;
14236 14232 } else {
14237 14233 pktp->pkt_time = uscmd->uscsi_timeout;
14238 14234 }
14239 14235
14240 14236 /* need it later to identify USCSI request in sdintr */
14241 14237 xp->xb_pkt_flags |= SD_XB_USCSICMD;
14242 14238
14243 14239 xp->xb_sense_resid = uscmd->uscsi_rqresid;
14244 14240
14245 14241 pktp->pkt_private = bp;
14246 14242 pktp->pkt_comp = sdintr;
14247 14243 *pktpp = pktp;
14248 14244
14249 14245 SD_TRACE(SD_LOG_IO_CORE, un,
14250 14246 "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14251 14247
14252 14248 return (SD_PKT_ALLOC_SUCCESS);
14253 14249 }
14254 14250
14255 14251
14256 14252 /*
14257 14253 * Function: sd_destroypkt_for_uscsi
14258 14254 *
14259 14255 * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14260 14256 * IOs.. Also saves relevant info into the associated uscsi_cmd
14261 14257 * struct.
14262 14258 *
14263 14259 * Context: May be called under interrupt context
14264 14260 */
14265 14261
14266 14262 static void
14267 14263 sd_destroypkt_for_uscsi(struct buf *bp)
14268 14264 {
14269 14265 struct uscsi_cmd *uscmd;
14270 14266 struct sd_xbuf *xp;
14271 14267 struct scsi_pkt *pktp;
14272 14268 struct sd_lun *un;
14273 14269 struct sd_uscsi_info *suip;
14274 14270
14275 14271 ASSERT(bp != NULL);
14276 14272 xp = SD_GET_XBUF(bp);
14277 14273 ASSERT(xp != NULL);
14278 14274 un = SD_GET_UN(bp);
14279 14275 ASSERT(un != NULL);
14280 14276 ASSERT(!mutex_owned(SD_MUTEX(un)));
14281 14277 pktp = SD_GET_PKTP(bp);
14282 14278 ASSERT(pktp != NULL);
14283 14279
14284 14280 SD_TRACE(SD_LOG_IO_CORE, un,
14285 14281 "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14286 14282
14287 14283 /* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14288 14284 uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14289 14285 ASSERT(uscmd != NULL);
14290 14286
14291 14287 /* Save the status and the residual into the uscsi_cmd struct */
14292 14288 uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14293 14289 uscmd->uscsi_resid = bp->b_resid;
14294 14290
14295 14291 /* Transfer scsi_pkt information to uscsi */
14296 14292 (void) scsi_uscsi_pktfini(pktp, uscmd);
14297 14293
14298 14294 /*
14299 14295 * If enabled, copy any saved sense data into the area specified
14300 14296 * by the uscsi command.
14301 14297 */
14302 14298 if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14303 14299 (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14304 14300 /*
14305 14301 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14306 14302 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14307 14303 */
14308 14304 uscmd->uscsi_rqstatus = xp->xb_sense_status;
14309 14305 uscmd->uscsi_rqresid = xp->xb_sense_resid;
14310 14306 if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14311 14307 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14312 14308 MAX_SENSE_LENGTH);
14313 14309 } else {
14314 14310 bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14315 14311 SENSE_LENGTH);
14316 14312 }
14317 14313 }
14318 14314 /*
14319 14315 * The following assignments are for SCSI FMA.
14320 14316 */
14321 14317 ASSERT(xp->xb_private != NULL);
14322 14318 suip = (struct sd_uscsi_info *)xp->xb_private;
14323 14319 suip->ui_pkt_reason = pktp->pkt_reason;
14324 14320 suip->ui_pkt_state = pktp->pkt_state;
14325 14321 suip->ui_pkt_statistics = pktp->pkt_statistics;
14326 14322 suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14327 14323
14328 14324 /* We are done with the scsi_pkt; free it now */
14329 14325 ASSERT(SD_GET_PKTP(bp) != NULL);
14330 14326 scsi_destroy_pkt(SD_GET_PKTP(bp));
14331 14327
14332 14328 SD_TRACE(SD_LOG_IO_CORE, un,
14333 14329 "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14334 14330 }
14335 14331
14336 14332
14337 14333 /*
14338 14334 * Function: sd_bioclone_alloc
14339 14335 *
14340 14336 * Description: Allocate a buf(9S) and init it as per the given buf
14341 14337 * and the various arguments. The associated sd_xbuf
14342 14338 * struct is (nearly) duplicated. The struct buf *bp
14343 14339 * argument is saved in new_xp->xb_private.
14344 14340 *
14345 14341 * Arguments: bp - ptr the the buf(9S) to be "shadowed"
14346 14342 * datalen - size of data area for the shadow bp
14347 14343 * blkno - starting LBA
14348 14344 * func - function pointer for b_iodone in the shadow buf. (May
14349 14345 * be NULL if none.)
14350 14346 *
14351 14347 * Return Code: Pointer to allocates buf(9S) struct
14352 14348 *
14353 14349 * Context: Can sleep.
14354 14350 */
14355 14351
14356 14352 static struct buf *
14357 14353 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14358 14354 daddr_t blkno, int (*func)(struct buf *))
14359 14355 {
14360 14356 struct sd_lun *un;
14361 14357 struct sd_xbuf *xp;
14362 14358 struct sd_xbuf *new_xp;
14363 14359 struct buf *new_bp;
14364 14360
14365 14361 ASSERT(bp != NULL);
14366 14362 xp = SD_GET_XBUF(bp);
14367 14363 ASSERT(xp != NULL);
14368 14364 un = SD_GET_UN(bp);
14369 14365 ASSERT(un != NULL);
14370 14366 ASSERT(!mutex_owned(SD_MUTEX(un)));
14371 14367
14372 14368 new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14373 14369 NULL, KM_SLEEP);
14374 14370
14375 14371 new_bp->b_lblkno = blkno;
14376 14372
14377 14373 /*
14378 14374 * Allocate an xbuf for the shadow bp and copy the contents of the
14379 14375 * original xbuf into it.
14380 14376 */
14381 14377 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14382 14378 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14383 14379
14384 14380 /*
14385 14381 * The given bp is automatically saved in the xb_private member
14386 14382 * of the new xbuf. Callers are allowed to depend on this.
14387 14383 */
14388 14384 new_xp->xb_private = bp;
14389 14385
14390 14386 new_bp->b_private = new_xp;
14391 14387
14392 14388 return (new_bp);
14393 14389 }
14394 14390
14395 14391 /*
14396 14392 * Function: sd_shadow_buf_alloc
14397 14393 *
14398 14394 * Description: Allocate a buf(9S) and init it as per the given buf
14399 14395 * and the various arguments. The associated sd_xbuf
14400 14396 * struct is (nearly) duplicated. The struct buf *bp
14401 14397 * argument is saved in new_xp->xb_private.
14402 14398 *
14403 14399 * Arguments: bp - ptr the the buf(9S) to be "shadowed"
14404 14400 * datalen - size of data area for the shadow bp
14405 14401 * bflags - B_READ or B_WRITE (pseudo flag)
14406 14402 * blkno - starting LBA
14407 14403 * func - function pointer for b_iodone in the shadow buf. (May
14408 14404 * be NULL if none.)
14409 14405 *
14410 14406 * Return Code: Pointer to allocates buf(9S) struct
14411 14407 *
14412 14408 * Context: Can sleep.
14413 14409 */
14414 14410
14415 14411 static struct buf *
14416 14412 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14417 14413 daddr_t blkno, int (*func)(struct buf *))
14418 14414 {
14419 14415 struct sd_lun *un;
14420 14416 struct sd_xbuf *xp;
14421 14417 struct sd_xbuf *new_xp;
14422 14418 struct buf *new_bp;
14423 14419
14424 14420 ASSERT(bp != NULL);
14425 14421 xp = SD_GET_XBUF(bp);
14426 14422 ASSERT(xp != NULL);
14427 14423 un = SD_GET_UN(bp);
14428 14424 ASSERT(un != NULL);
14429 14425 ASSERT(!mutex_owned(SD_MUTEX(un)));
14430 14426
14431 14427 if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14432 14428 bp_mapin(bp);
14433 14429 }
14434 14430
14435 14431 bflags &= (B_READ | B_WRITE);
14436 14432 #if defined(__i386) || defined(__amd64)
14437 14433 new_bp = getrbuf(KM_SLEEP);
14438 14434 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14439 14435 new_bp->b_bcount = datalen;
14440 14436 new_bp->b_flags = bflags |
14441 14437 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14442 14438 #else
14443 14439 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14444 14440 datalen, bflags, SLEEP_FUNC, NULL);
14445 14441 #endif
14446 14442 new_bp->av_forw = NULL;
14447 14443 new_bp->av_back = NULL;
14448 14444 new_bp->b_dev = bp->b_dev;
14449 14445 new_bp->b_blkno = blkno;
14450 14446 new_bp->b_iodone = func;
14451 14447 new_bp->b_edev = bp->b_edev;
14452 14448 new_bp->b_resid = 0;
14453 14449
14454 14450 /* We need to preserve the B_FAILFAST flag */
14455 14451 if (bp->b_flags & B_FAILFAST) {
14456 14452 new_bp->b_flags |= B_FAILFAST;
14457 14453 }
14458 14454
14459 14455 /*
14460 14456 * Allocate an xbuf for the shadow bp and copy the contents of the
14461 14457 * original xbuf into it.
14462 14458 */
14463 14459 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14464 14460 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14465 14461
14466 14462 /* Need later to copy data between the shadow buf & original buf! */
14467 14463 new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14468 14464
14469 14465 /*
14470 14466 * The given bp is automatically saved in the xb_private member
14471 14467 * of the new xbuf. Callers are allowed to depend on this.
14472 14468 */
14473 14469 new_xp->xb_private = bp;
14474 14470
14475 14471 new_bp->b_private = new_xp;
14476 14472
14477 14473 return (new_bp);
14478 14474 }
14479 14475
14480 14476 /*
14481 14477 * Function: sd_bioclone_free
14482 14478 *
14483 14479 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14484 14480 * in the larger than partition operation.
14485 14481 *
14486 14482 * Context: May be called under interrupt context
14487 14483 */
14488 14484
14489 14485 static void
14490 14486 sd_bioclone_free(struct buf *bp)
14491 14487 {
14492 14488 struct sd_xbuf *xp;
14493 14489
14494 14490 ASSERT(bp != NULL);
14495 14491 xp = SD_GET_XBUF(bp);
14496 14492 ASSERT(xp != NULL);
14497 14493
14498 14494 /*
14499 14495 * Call bp_mapout() before freeing the buf, in case a lower
14500 14496 * layer or HBA had done a bp_mapin(). we must do this here
14501 14497 * as we are the "originator" of the shadow buf.
14502 14498 */
14503 14499 bp_mapout(bp);
14504 14500
14505 14501 /*
14506 14502 * Null out b_iodone before freeing the bp, to ensure that the driver
14507 14503 * never gets confused by a stale value in this field. (Just a little
14508 14504 * extra defensiveness here.)
14509 14505 */
14510 14506 bp->b_iodone = NULL;
14511 14507
14512 14508 freerbuf(bp);
14513 14509
14514 14510 kmem_free(xp, sizeof (struct sd_xbuf));
14515 14511 }
14516 14512
14517 14513 /*
14518 14514 * Function: sd_shadow_buf_free
14519 14515 *
14520 14516 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14521 14517 *
14522 14518 * Context: May be called under interrupt context
14523 14519 */
14524 14520
14525 14521 static void
14526 14522 sd_shadow_buf_free(struct buf *bp)
14527 14523 {
14528 14524 struct sd_xbuf *xp;
14529 14525
14530 14526 ASSERT(bp != NULL);
14531 14527 xp = SD_GET_XBUF(bp);
14532 14528 ASSERT(xp != NULL);
14533 14529
14534 14530 #if defined(__sparc)
14535 14531 /*
14536 14532 * Call bp_mapout() before freeing the buf, in case a lower
14537 14533 * layer or HBA had done a bp_mapin(). we must do this here
14538 14534 * as we are the "originator" of the shadow buf.
14539 14535 */
14540 14536 bp_mapout(bp);
14541 14537 #endif
14542 14538
14543 14539 /*
14544 14540 * Null out b_iodone before freeing the bp, to ensure that the driver
14545 14541 * never gets confused by a stale value in this field. (Just a little
14546 14542 * extra defensiveness here.)
14547 14543 */
14548 14544 bp->b_iodone = NULL;
14549 14545
14550 14546 #if defined(__i386) || defined(__amd64)
14551 14547 kmem_free(bp->b_un.b_addr, bp->b_bcount);
14552 14548 freerbuf(bp);
14553 14549 #else
14554 14550 scsi_free_consistent_buf(bp);
14555 14551 #endif
14556 14552
14557 14553 kmem_free(xp, sizeof (struct sd_xbuf));
14558 14554 }
14559 14555
14560 14556
14561 14557 /*
14562 14558 * Function: sd_print_transport_rejected_message
14563 14559 *
14564 14560 * Description: This implements the ludicrously complex rules for printing
14565 14561 * a "transport rejected" message. This is to address the
14566 14562 * specific problem of having a flood of this error message
14567 14563 * produced when a failover occurs.
14568 14564 *
14569 14565 * Context: Any.
14570 14566 */
14571 14567
14572 14568 static void
14573 14569 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14574 14570 int code)
14575 14571 {
14576 14572 ASSERT(un != NULL);
14577 14573 ASSERT(mutex_owned(SD_MUTEX(un)));
14578 14574 ASSERT(xp != NULL);
14579 14575
14580 14576 /*
14581 14577 * Print the "transport rejected" message under the following
14582 14578 * conditions:
14583 14579 *
14584 14580 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14585 14581 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14586 14582 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14587 14583 * printed the FIRST time a TRAN_FATAL_ERROR is returned from
14588 14584 * scsi_transport(9F) (which indicates that the target might have
14589 14585 * gone off-line). This uses the un->un_tran_fatal_count
14590 14586 * count, which is incremented whenever a TRAN_FATAL_ERROR is
14591 14587 * received, and reset to zero whenver a TRAN_ACCEPT is returned
14592 14588 * from scsi_transport().
14593 14589 *
14594 14590 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14595 14591 * the preceeding cases in order for the message to be printed.
14596 14592 */
14597 14593 if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14598 14594 (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14599 14595 if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14600 14596 (code != TRAN_FATAL_ERROR) ||
14601 14597 (un->un_tran_fatal_count == 1)) {
14602 14598 switch (code) {
14603 14599 case TRAN_BADPKT:
14604 14600 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14605 14601 "transport rejected bad packet\n");
14606 14602 break;
14607 14603 case TRAN_FATAL_ERROR:
14608 14604 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14609 14605 "transport rejected fatal error\n");
14610 14606 break;
14611 14607 default:
14612 14608 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14613 14609 "transport rejected (%d)\n", code);
14614 14610 break;
14615 14611 }
14616 14612 }
14617 14613 }
14618 14614 }
14619 14615
14620 14616
14621 14617 /*
14622 14618 * Function: sd_add_buf_to_waitq
14623 14619 *
14624 14620 * Description: Add the given buf(9S) struct to the wait queue for the
14625 14621 * instance. If sorting is enabled, then the buf is added
14626 14622 * to the queue via an elevator sort algorithm (a la
14627 14623 * disksort(9F)). The SD_GET_BLKNO(bp) is used as the sort key.
14628 14624 * If sorting is not enabled, then the buf is just added
14629 14625 * to the end of the wait queue.
14630 14626 *
14631 14627 * Return Code: void
14632 14628 *
14633 14629 * Context: Does not sleep/block, therefore technically can be called
14634 14630 * from any context. However if sorting is enabled then the
14635 14631 * execution time is indeterminate, and may take long if
14636 14632 * the wait queue grows large.
14637 14633 */
14638 14634
14639 14635 static void
14640 14636 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14641 14637 {
14642 14638 struct buf *ap;
14643 14639
14644 14640 ASSERT(bp != NULL);
14645 14641 ASSERT(un != NULL);
14646 14642 ASSERT(mutex_owned(SD_MUTEX(un)));
14647 14643
14648 14644 /* If the queue is empty, add the buf as the only entry & return. */
14649 14645 if (un->un_waitq_headp == NULL) {
14650 14646 ASSERT(un->un_waitq_tailp == NULL);
14651 14647 un->un_waitq_headp = un->un_waitq_tailp = bp;
14652 14648 bp->av_forw = NULL;
14653 14649 return;
14654 14650 }
14655 14651
14656 14652 ASSERT(un->un_waitq_tailp != NULL);
14657 14653
14658 14654 /*
14659 14655 * If sorting is disabled, just add the buf to the tail end of
14660 14656 * the wait queue and return.
14661 14657 */
14662 14658 if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14663 14659 un->un_waitq_tailp->av_forw = bp;
14664 14660 un->un_waitq_tailp = bp;
14665 14661 bp->av_forw = NULL;
14666 14662 return;
14667 14663 }
14668 14664
14669 14665 /*
14670 14666 * Sort thru the list of requests currently on the wait queue
14671 14667 * and add the new buf request at the appropriate position.
14672 14668 *
14673 14669 * The un->un_waitq_headp is an activity chain pointer on which
14674 14670 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14675 14671 * first queue holds those requests which are positioned after
14676 14672 * the current SD_GET_BLKNO() (in the first request); the second holds
14677 14673 * requests which came in after their SD_GET_BLKNO() number was passed.
14678 14674 * Thus we implement a one way scan, retracting after reaching
14679 14675 * the end of the drive to the first request on the second
14680 14676 * queue, at which time it becomes the first queue.
14681 14677 * A one-way scan is natural because of the way UNIX read-ahead
14682 14678 * blocks are allocated.
14683 14679 *
14684 14680 * If we lie after the first request, then we must locate the
14685 14681 * second request list and add ourselves to it.
14686 14682 */
14687 14683 ap = un->un_waitq_headp;
14688 14684 if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14689 14685 while (ap->av_forw != NULL) {
14690 14686 /*
14691 14687 * Look for an "inversion" in the (normally
14692 14688 * ascending) block numbers. This indicates
14693 14689 * the start of the second request list.
14694 14690 */
14695 14691 if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14696 14692 /*
14697 14693 * Search the second request list for the
14698 14694 * first request at a larger block number.
14699 14695 * We go before that; however if there is
14700 14696 * no such request, we go at the end.
14701 14697 */
14702 14698 do {
14703 14699 if (SD_GET_BLKNO(bp) <
14704 14700 SD_GET_BLKNO(ap->av_forw)) {
14705 14701 goto insert;
14706 14702 }
14707 14703 ap = ap->av_forw;
14708 14704 } while (ap->av_forw != NULL);
14709 14705 goto insert; /* after last */
14710 14706 }
14711 14707 ap = ap->av_forw;
14712 14708 }
14713 14709
14714 14710 /*
14715 14711 * No inversions... we will go after the last, and
14716 14712 * be the first request in the second request list.
14717 14713 */
14718 14714 goto insert;
14719 14715 }
14720 14716
14721 14717 /*
14722 14718 * Request is at/after the current request...
14723 14719 * sort in the first request list.
14724 14720 */
14725 14721 while (ap->av_forw != NULL) {
14726 14722 /*
14727 14723 * We want to go after the current request (1) if
14728 14724 * there is an inversion after it (i.e. it is the end
14729 14725 * of the first request list), or (2) if the next
14730 14726 * request is a larger block no. than our request.
14731 14727 */
14732 14728 if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14733 14729 (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14734 14730 goto insert;
14735 14731 }
14736 14732 ap = ap->av_forw;
14737 14733 }
14738 14734
14739 14735 /*
14740 14736 * Neither a second list nor a larger request, therefore
14741 14737 * we go at the end of the first list (which is the same
14742 14738 * as the end of the whole schebang).
14743 14739 */
14744 14740 insert:
14745 14741 bp->av_forw = ap->av_forw;
14746 14742 ap->av_forw = bp;
14747 14743
14748 14744 /*
14749 14745 * If we inserted onto the tail end of the waitq, make sure the
14750 14746 * tail pointer is updated.
14751 14747 */
14752 14748 if (ap == un->un_waitq_tailp) {
14753 14749 un->un_waitq_tailp = bp;
14754 14750 }
14755 14751 }
14756 14752
14757 14753
14758 14754 /*
14759 14755 * Function: sd_start_cmds
14760 14756 *
14761 14757 * Description: Remove and transport cmds from the driver queues.
14762 14758 *
14763 14759 * Arguments: un - pointer to the unit (soft state) struct for the target.
14764 14760 *
14765 14761 * immed_bp - ptr to a buf to be transported immediately. Only
14766 14762 * the immed_bp is transported; bufs on the waitq are not
14767 14763 * processed and the un_retry_bp is not checked. If immed_bp is
14768 14764 * NULL, then normal queue processing is performed.
14769 14765 *
14770 14766 * Context: May be called from kernel thread context, interrupt context,
14771 14767 * or runout callback context. This function may not block or
14772 14768 * call routines that block.
14773 14769 */
14774 14770
14775 14771 static void
14776 14772 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14777 14773 {
14778 14774 struct sd_xbuf *xp;
14779 14775 struct buf *bp;
14780 14776 void (*statp)(kstat_io_t *);
14781 14777 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14782 14778 void (*saved_statp)(kstat_io_t *);
14783 14779 #endif
14784 14780 int rval;
14785 14781 struct sd_fm_internal *sfip = NULL;
14786 14782
14787 14783 ASSERT(un != NULL);
14788 14784 ASSERT(mutex_owned(SD_MUTEX(un)));
14789 14785 ASSERT(un->un_ncmds_in_transport >= 0);
14790 14786 ASSERT(un->un_throttle >= 0);
14791 14787
14792 14788 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14793 14789
14794 14790 do {
14795 14791 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14796 14792 saved_statp = NULL;
14797 14793 #endif
14798 14794
14799 14795 /*
14800 14796 * If we are syncing or dumping, fail the command to
14801 14797 * avoid recursively calling back into scsi_transport().
14802 14798 * The dump I/O itself uses a separate code path so this
14803 14799 * only prevents non-dump I/O from being sent while dumping.
14804 14800 * File system sync takes place before dumping begins.
14805 14801 * During panic, filesystem I/O is allowed provided
14806 14802 * un_in_callback is <= 1. This is to prevent recursion
14807 14803 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14808 14804 * sd_start_cmds and so on. See panic.c for more information
14809 14805 * about the states the system can be in during panic.
14810 14806 */
14811 14807 if ((un->un_state == SD_STATE_DUMPING) ||
14812 14808 (ddi_in_panic() && (un->un_in_callback > 1))) {
14813 14809 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14814 14810 "sd_start_cmds: panicking\n");
14815 14811 goto exit;
14816 14812 }
14817 14813
14818 14814 if ((bp = immed_bp) != NULL) {
14819 14815 /*
14820 14816 * We have a bp that must be transported immediately.
14821 14817 * It's OK to transport the immed_bp here without doing
14822 14818 * the throttle limit check because the immed_bp is
14823 14819 * always used in a retry/recovery case. This means
14824 14820 * that we know we are not at the throttle limit by
14825 14821 * virtue of the fact that to get here we must have
14826 14822 * already gotten a command back via sdintr(). This also
14827 14823 * relies on (1) the command on un_retry_bp preventing
14828 14824 * further commands from the waitq from being issued;
14829 14825 * and (2) the code in sd_retry_command checking the
14830 14826 * throttle limit before issuing a delayed or immediate
14831 14827 * retry. This holds even if the throttle limit is
14832 14828 * currently ratcheted down from its maximum value.
14833 14829 */
14834 14830 statp = kstat_runq_enter;
14835 14831 if (bp == un->un_retry_bp) {
14836 14832 ASSERT((un->un_retry_statp == NULL) ||
14837 14833 (un->un_retry_statp == kstat_waitq_enter) ||
14838 14834 (un->un_retry_statp ==
14839 14835 kstat_runq_back_to_waitq));
14840 14836 /*
14841 14837 * If the waitq kstat was incremented when
14842 14838 * sd_set_retry_bp() queued this bp for a retry,
14843 14839 * then we must set up statp so that the waitq
14844 14840 * count will get decremented correctly below.
14845 14841 * Also we must clear un->un_retry_statp to
14846 14842 * ensure that we do not act on a stale value
14847 14843 * in this field.
14848 14844 */
14849 14845 if ((un->un_retry_statp == kstat_waitq_enter) ||
14850 14846 (un->un_retry_statp ==
14851 14847 kstat_runq_back_to_waitq)) {
14852 14848 statp = kstat_waitq_to_runq;
14853 14849 }
14854 14850 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14855 14851 saved_statp = un->un_retry_statp;
14856 14852 #endif
14857 14853 un->un_retry_statp = NULL;
14858 14854
14859 14855 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14860 14856 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14861 14857 "un_throttle:%d un_ncmds_in_transport:%d\n",
14862 14858 un, un->un_retry_bp, un->un_throttle,
14863 14859 un->un_ncmds_in_transport);
14864 14860 } else {
14865 14861 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14866 14862 "processing priority bp:0x%p\n", bp);
14867 14863 }
14868 14864
14869 14865 } else if ((bp = un->un_waitq_headp) != NULL) {
14870 14866 /*
14871 14867 * A command on the waitq is ready to go, but do not
14872 14868 * send it if:
14873 14869 *
14874 14870 * (1) the throttle limit has been reached, or
14875 14871 * (2) a retry is pending, or
14876 14872 * (3) a START_STOP_UNIT callback pending, or
14877 14873 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14878 14874 * command is pending.
14879 14875 *
14880 14876 * For all of these conditions, IO processing will
14881 14877 * restart after the condition is cleared.
14882 14878 */
14883 14879 if (un->un_ncmds_in_transport >= un->un_throttle) {
14884 14880 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14885 14881 "sd_start_cmds: exiting, "
14886 14882 "throttle limit reached!\n");
14887 14883 goto exit;
14888 14884 }
14889 14885 if (un->un_retry_bp != NULL) {
14890 14886 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14891 14887 "sd_start_cmds: exiting, retry pending!\n");
14892 14888 goto exit;
14893 14889 }
14894 14890 if (un->un_startstop_timeid != NULL) {
14895 14891 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14896 14892 "sd_start_cmds: exiting, "
14897 14893 "START_STOP pending!\n");
14898 14894 goto exit;
14899 14895 }
14900 14896 if (un->un_direct_priority_timeid != NULL) {
14901 14897 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14902 14898 "sd_start_cmds: exiting, "
14903 14899 "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14904 14900 goto exit;
14905 14901 }
14906 14902
14907 14903 /* Dequeue the command */
14908 14904 un->un_waitq_headp = bp->av_forw;
14909 14905 if (un->un_waitq_headp == NULL) {
14910 14906 un->un_waitq_tailp = NULL;
14911 14907 }
14912 14908 bp->av_forw = NULL;
14913 14909 statp = kstat_waitq_to_runq;
14914 14910 SD_TRACE(SD_LOG_IO_CORE, un,
14915 14911 "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14916 14912
14917 14913 } else {
14918 14914 /* No work to do so bail out now */
14919 14915 SD_TRACE(SD_LOG_IO_CORE, un,
14920 14916 "sd_start_cmds: no more work, exiting!\n");
14921 14917 goto exit;
14922 14918 }
14923 14919
14924 14920 /*
14925 14921 * Reset the state to normal. This is the mechanism by which
14926 14922 * the state transitions from either SD_STATE_RWAIT or
14927 14923 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14928 14924 * If state is SD_STATE_PM_CHANGING then this command is
14929 14925 * part of the device power control and the state must
14930 14926 * not be put back to normal. Doing so would would
14931 14927 * allow new commands to proceed when they shouldn't,
14932 14928 * the device may be going off.
14933 14929 */
14934 14930 if ((un->un_state != SD_STATE_SUSPENDED) &&
14935 14931 (un->un_state != SD_STATE_PM_CHANGING)) {
14936 14932 New_state(un, SD_STATE_NORMAL);
14937 14933 }
14938 14934
14939 14935 xp = SD_GET_XBUF(bp);
14940 14936 ASSERT(xp != NULL);
14941 14937
14942 14938 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14943 14939 /*
14944 14940 * Allocate the scsi_pkt if we need one, or attach DMA
14945 14941 * resources if we have a scsi_pkt that needs them. The
14946 14942 * latter should only occur for commands that are being
14947 14943 * retried.
14948 14944 */
14949 14945 if ((xp->xb_pktp == NULL) ||
14950 14946 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14951 14947 #else
14952 14948 if (xp->xb_pktp == NULL) {
14953 14949 #endif
14954 14950 /*
14955 14951 * There is no scsi_pkt allocated for this buf. Call
14956 14952 * the initpkt function to allocate & init one.
14957 14953 *
14958 14954 * The scsi_init_pkt runout callback functionality is
14959 14955 * implemented as follows:
14960 14956 *
14961 14957 * 1) The initpkt function always calls
14962 14958 * scsi_init_pkt(9F) with sdrunout specified as the
14963 14959 * callback routine.
14964 14960 * 2) A successful packet allocation is initialized and
14965 14961 * the I/O is transported.
14966 14962 * 3) The I/O associated with an allocation resource
14967 14963 * failure is left on its queue to be retried via
14968 14964 * runout or the next I/O.
14969 14965 * 4) The I/O associated with a DMA error is removed
14970 14966 * from the queue and failed with EIO. Processing of
14971 14967 * the transport queues is also halted to be
14972 14968 * restarted via runout or the next I/O.
14973 14969 * 5) The I/O associated with a CDB size or packet
14974 14970 * size error is removed from the queue and failed
14975 14971 * with EIO. Processing of the transport queues is
14976 14972 * continued.
14977 14973 *
14978 14974 * Note: there is no interface for canceling a runout
14979 14975 * callback. To prevent the driver from detaching or
14980 14976 * suspending while a runout is pending the driver
14981 14977 * state is set to SD_STATE_RWAIT
14982 14978 *
14983 14979 * Note: using the scsi_init_pkt callback facility can
14984 14980 * result in an I/O request persisting at the head of
14985 14981 * the list which cannot be satisfied even after
14986 14982 * multiple retries. In the future the driver may
14987 14983 * implement some kind of maximum runout count before
14988 14984 * failing an I/O.
14989 14985 *
14990 14986 * Note: the use of funcp below may seem superfluous,
14991 14987 * but it helps warlock figure out the correct
14992 14988 * initpkt function calls (see [s]sd.wlcmd).
14993 14989 */
14994 14990 struct scsi_pkt *pktp;
14995 14991 int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14996 14992
14997 14993 ASSERT(bp != un->un_rqs_bp);
14998 14994
14999 14995 funcp = sd_initpkt_map[xp->xb_chain_iostart];
15000 14996 switch ((*funcp)(bp, &pktp)) {
15001 14997 case SD_PKT_ALLOC_SUCCESS:
15002 14998 xp->xb_pktp = pktp;
15003 14999 SD_TRACE(SD_LOG_IO_CORE, un,
15004 15000 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
15005 15001 pktp);
15006 15002 goto got_pkt;
15007 15003
15008 15004 case SD_PKT_ALLOC_FAILURE:
15009 15005 /*
15010 15006 * Temporary (hopefully) resource depletion.
15011 15007 * Since retries and RQS commands always have a
15012 15008 * scsi_pkt allocated, these cases should never
15013 15009 * get here. So the only cases this needs to
15014 15010 * handle is a bp from the waitq (which we put
15015 15011 * back onto the waitq for sdrunout), or a bp
15016 15012 * sent as an immed_bp (which we just fail).
15017 15013 */
15018 15014 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15019 15015 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
15020 15016
15021 15017 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15022 15018
15023 15019 if (bp == immed_bp) {
15024 15020 /*
15025 15021 * If SD_XB_DMA_FREED is clear, then
15026 15022 * this is a failure to allocate a
15027 15023 * scsi_pkt, and we must fail the
15028 15024 * command.
15029 15025 */
15030 15026 if ((xp->xb_pkt_flags &
15031 15027 SD_XB_DMA_FREED) == 0) {
15032 15028 break;
15033 15029 }
15034 15030
15035 15031 /*
15036 15032 * If this immediate command is NOT our
15037 15033 * un_retry_bp, then we must fail it.
15038 15034 */
15039 15035 if (bp != un->un_retry_bp) {
15040 15036 break;
15041 15037 }
15042 15038
15043 15039 /*
15044 15040 * We get here if this cmd is our
15045 15041 * un_retry_bp that was DMAFREED, but
15046 15042 * scsi_init_pkt() failed to reallocate
15047 15043 * DMA resources when we attempted to
15048 15044 * retry it. This can happen when an
15049 15045 * mpxio failover is in progress, but
15050 15046 * we don't want to just fail the
15051 15047 * command in this case.
15052 15048 *
15053 15049 * Use timeout(9F) to restart it after
15054 15050 * a 100ms delay. We don't want to
15055 15051 * let sdrunout() restart it, because
15056 15052 * sdrunout() is just supposed to start
15057 15053 * commands that are sitting on the
15058 15054 * wait queue. The un_retry_bp stays
15059 15055 * set until the command completes, but
15060 15056 * sdrunout can be called many times
15061 15057 * before that happens. Since sdrunout
15062 15058 * cannot tell if the un_retry_bp is
15063 15059 * already in the transport, it could
15064 15060 * end up calling scsi_transport() for
15065 15061 * the un_retry_bp multiple times.
15066 15062 *
15067 15063 * Also: don't schedule the callback
15068 15064 * if some other callback is already
15069 15065 * pending.
15070 15066 */
15071 15067 if (un->un_retry_statp == NULL) {
15072 15068 /*
15073 15069 * restore the kstat pointer to
15074 15070 * keep kstat counts coherent
15075 15071 * when we do retry the command.
15076 15072 */
15077 15073 un->un_retry_statp =
15078 15074 saved_statp;
15079 15075 }
15080 15076
15081 15077 if ((un->un_startstop_timeid == NULL) &&
15082 15078 (un->un_retry_timeid == NULL) &&
15083 15079 (un->un_direct_priority_timeid ==
15084 15080 NULL)) {
15085 15081
15086 15082 un->un_retry_timeid =
15087 15083 timeout(
15088 15084 sd_start_retry_command,
15089 15085 un, SD_RESTART_TIMEOUT);
15090 15086 }
15091 15087 goto exit;
15092 15088 }
15093 15089
15094 15090 #else
15095 15091 if (bp == immed_bp) {
15096 15092 break; /* Just fail the command */
15097 15093 }
15098 15094 #endif
15099 15095
15100 15096 /* Add the buf back to the head of the waitq */
15101 15097 bp->av_forw = un->un_waitq_headp;
15102 15098 un->un_waitq_headp = bp;
15103 15099 if (un->un_waitq_tailp == NULL) {
15104 15100 un->un_waitq_tailp = bp;
15105 15101 }
15106 15102 goto exit;
15107 15103
15108 15104 case SD_PKT_ALLOC_FAILURE_NO_DMA:
15109 15105 /*
15110 15106 * HBA DMA resource failure. Fail the command
15111 15107 * and continue processing of the queues.
15112 15108 */
15113 15109 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15114 15110 "sd_start_cmds: "
15115 15111 "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15116 15112 break;
15117 15113
15118 15114 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15119 15115 /*
15120 15116 * Note:x86: Partial DMA mapping not supported
15121 15117 * for USCSI commands, and all the needed DMA
15122 15118 * resources were not allocated.
15123 15119 */
15124 15120 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15125 15121 "sd_start_cmds: "
15126 15122 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15127 15123 break;
15128 15124
15129 15125 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15130 15126 /*
15131 15127 * Note:x86: Request cannot fit into CDB based
15132 15128 * on lba and len.
15133 15129 */
15134 15130 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15135 15131 "sd_start_cmds: "
15136 15132 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15137 15133 break;
15138 15134
15139 15135 default:
15140 15136 /* Should NEVER get here! */
15141 15137 panic("scsi_initpkt error");
15142 15138 /*NOTREACHED*/
15143 15139 }
15144 15140
15145 15141 /*
15146 15142 * Fatal error in allocating a scsi_pkt for this buf.
15147 15143 * Update kstats & return the buf with an error code.
15148 15144 * We must use sd_return_failed_command_no_restart() to
15149 15145 * avoid a recursive call back into sd_start_cmds().
15150 15146 * However this also means that we must keep processing
15151 15147 * the waitq here in order to avoid stalling.
15152 15148 */
15153 15149 if (statp == kstat_waitq_to_runq) {
15154 15150 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15155 15151 }
15156 15152 sd_return_failed_command_no_restart(un, bp, EIO);
15157 15153 if (bp == immed_bp) {
15158 15154 /* immed_bp is gone by now, so clear this */
15159 15155 immed_bp = NULL;
15160 15156 }
15161 15157 continue;
15162 15158 }
15163 15159 got_pkt:
15164 15160 if (bp == immed_bp) {
15165 15161 /* goto the head of the class.... */
15166 15162 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15167 15163 }
15168 15164
15169 15165 un->un_ncmds_in_transport++;
15170 15166 SD_UPDATE_KSTATS(un, statp, bp);
15171 15167
15172 15168 /*
15173 15169 * Call scsi_transport() to send the command to the target.
15174 15170 * According to SCSA architecture, we must drop the mutex here
15175 15171 * before calling scsi_transport() in order to avoid deadlock.
15176 15172 * Note that the scsi_pkt's completion routine can be executed
15177 15173 * (from interrupt context) even before the call to
15178 15174 * scsi_transport() returns.
15179 15175 */
15180 15176 SD_TRACE(SD_LOG_IO_CORE, un,
15181 15177 "sd_start_cmds: calling scsi_transport()\n");
15182 15178 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15183 15179
15184 15180 mutex_exit(SD_MUTEX(un));
15185 15181 rval = scsi_transport(xp->xb_pktp);
15186 15182 mutex_enter(SD_MUTEX(un));
15187 15183
15188 15184 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15189 15185 "sd_start_cmds: scsi_transport() returned %d\n", rval);
15190 15186
15191 15187 switch (rval) {
15192 15188 case TRAN_ACCEPT:
15193 15189 /* Clear this with every pkt accepted by the HBA */
15194 15190 un->un_tran_fatal_count = 0;
15195 15191 break; /* Success; try the next cmd (if any) */
15196 15192
15197 15193 case TRAN_BUSY:
15198 15194 un->un_ncmds_in_transport--;
15199 15195 ASSERT(un->un_ncmds_in_transport >= 0);
15200 15196
15201 15197 /*
15202 15198 * Don't retry request sense, the sense data
15203 15199 * is lost when another request is sent.
15204 15200 * Free up the rqs buf and retry
15205 15201 * the original failed cmd. Update kstat.
15206 15202 */
15207 15203 if (bp == un->un_rqs_bp) {
15208 15204 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15209 15205 bp = sd_mark_rqs_idle(un, xp);
15210 15206 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15211 15207 NULL, NULL, EIO, un->un_busy_timeout / 500,
15212 15208 kstat_waitq_enter);
15213 15209 goto exit;
15214 15210 }
15215 15211
15216 15212 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15217 15213 /*
15218 15214 * Free the DMA resources for the scsi_pkt. This will
15219 15215 * allow mpxio to select another path the next time
15220 15216 * we call scsi_transport() with this scsi_pkt.
15221 15217 * See sdintr() for the rationalization behind this.
15222 15218 */
15223 15219 if ((un->un_f_is_fibre == TRUE) &&
15224 15220 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15225 15221 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15226 15222 scsi_dmafree(xp->xb_pktp);
15227 15223 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15228 15224 }
15229 15225 #endif
15230 15226
15231 15227 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15232 15228 /*
15233 15229 * Commands that are SD_PATH_DIRECT_PRIORITY
15234 15230 * are for error recovery situations. These do
15235 15231 * not use the normal command waitq, so if they
15236 15232 * get a TRAN_BUSY we cannot put them back onto
15237 15233 * the waitq for later retry. One possible
15238 15234 * problem is that there could already be some
15239 15235 * other command on un_retry_bp that is waiting
15240 15236 * for this one to complete, so we would be
15241 15237 * deadlocked if we put this command back onto
15242 15238 * the waitq for later retry (since un_retry_bp
15243 15239 * must complete before the driver gets back to
15244 15240 * commands on the waitq).
15245 15241 *
15246 15242 * To avoid deadlock we must schedule a callback
15247 15243 * that will restart this command after a set
15248 15244 * interval. This should keep retrying for as
15249 15245 * long as the underlying transport keeps
15250 15246 * returning TRAN_BUSY (just like for other
15251 15247 * commands). Use the same timeout interval as
15252 15248 * for the ordinary TRAN_BUSY retry.
15253 15249 */
15254 15250 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15255 15251 "sd_start_cmds: scsi_transport() returned "
15256 15252 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15257 15253
15258 15254 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15259 15255 un->un_direct_priority_timeid =
15260 15256 timeout(sd_start_direct_priority_command,
15261 15257 bp, un->un_busy_timeout / 500);
15262 15258
15263 15259 goto exit;
15264 15260 }
15265 15261
15266 15262 /*
15267 15263 * For TRAN_BUSY, we want to reduce the throttle value,
15268 15264 * unless we are retrying a command.
15269 15265 */
15270 15266 if (bp != un->un_retry_bp) {
15271 15267 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15272 15268 }
15273 15269
15274 15270 /*
15275 15271 * Set up the bp to be tried again 10 ms later.
15276 15272 * Note:x86: Is there a timeout value in the sd_lun
15277 15273 * for this condition?
15278 15274 */
15279 15275 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15280 15276 kstat_runq_back_to_waitq);
15281 15277 goto exit;
15282 15278
15283 15279 case TRAN_FATAL_ERROR:
15284 15280 un->un_tran_fatal_count++;
15285 15281 /* FALLTHRU */
15286 15282
15287 15283 case TRAN_BADPKT:
15288 15284 default:
15289 15285 un->un_ncmds_in_transport--;
15290 15286 ASSERT(un->un_ncmds_in_transport >= 0);
15291 15287
15292 15288 /*
15293 15289 * If this is our REQUEST SENSE command with a
15294 15290 * transport error, we must get back the pointers
15295 15291 * to the original buf, and mark the REQUEST
15296 15292 * SENSE command as "available".
15297 15293 */
15298 15294 if (bp == un->un_rqs_bp) {
15299 15295 bp = sd_mark_rqs_idle(un, xp);
15300 15296 xp = SD_GET_XBUF(bp);
15301 15297 } else {
15302 15298 /*
15303 15299 * Legacy behavior: do not update transport
15304 15300 * error count for request sense commands.
15305 15301 */
15306 15302 SD_UPDATE_ERRSTATS(un, sd_transerrs);
15307 15303 }
15308 15304
15309 15305 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15310 15306 sd_print_transport_rejected_message(un, xp, rval);
15311 15307
15312 15308 /*
15313 15309 * This command will be terminated by SD driver due
15314 15310 * to a fatal transport error. We should post
15315 15311 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15316 15312 * of "fail" for any command to indicate this
15317 15313 * situation.
15318 15314 */
15319 15315 if (xp->xb_ena > 0) {
15320 15316 ASSERT(un->un_fm_private != NULL);
15321 15317 sfip = un->un_fm_private;
15322 15318 sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15323 15319 sd_ssc_extract_info(&sfip->fm_ssc, un,
15324 15320 xp->xb_pktp, bp, xp);
15325 15321 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15326 15322 }
15327 15323
15328 15324 /*
15329 15325 * We must use sd_return_failed_command_no_restart() to
15330 15326 * avoid a recursive call back into sd_start_cmds().
15331 15327 * However this also means that we must keep processing
15332 15328 * the waitq here in order to avoid stalling.
15333 15329 */
15334 15330 sd_return_failed_command_no_restart(un, bp, EIO);
15335 15331
15336 15332 /*
15337 15333 * Notify any threads waiting in sd_ddi_suspend() that
15338 15334 * a command completion has occurred.
15339 15335 */
15340 15336 if (un->un_state == SD_STATE_SUSPENDED) {
15341 15337 cv_broadcast(&un->un_disk_busy_cv);
15342 15338 }
15343 15339
15344 15340 if (bp == immed_bp) {
15345 15341 /* immed_bp is gone by now, so clear this */
15346 15342 immed_bp = NULL;
15347 15343 }
15348 15344 break;
15349 15345 }
15350 15346
15351 15347 } while (immed_bp == NULL);
15352 15348
15353 15349 exit:
15354 15350 ASSERT(mutex_owned(SD_MUTEX(un)));
15355 15351 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15356 15352 }
15357 15353
15358 15354
15359 15355 /*
15360 15356 * Function: sd_return_command
15361 15357 *
15362 15358 * Description: Returns a command to its originator (with or without an
15363 15359 * error). Also starts commands waiting to be transported
15364 15360 * to the target.
15365 15361 *
15366 15362 * Context: May be called from interrupt, kernel, or timeout context
15367 15363 */
15368 15364
15369 15365 static void
15370 15366 sd_return_command(struct sd_lun *un, struct buf *bp)
15371 15367 {
15372 15368 struct sd_xbuf *xp;
15373 15369 struct scsi_pkt *pktp;
15374 15370 struct sd_fm_internal *sfip;
15375 15371
15376 15372 ASSERT(bp != NULL);
15377 15373 ASSERT(un != NULL);
15378 15374 ASSERT(mutex_owned(SD_MUTEX(un)));
15379 15375 ASSERT(bp != un->un_rqs_bp);
15380 15376 xp = SD_GET_XBUF(bp);
15381 15377 ASSERT(xp != NULL);
15382 15378
15383 15379 pktp = SD_GET_PKTP(bp);
15384 15380 sfip = (struct sd_fm_internal *)un->un_fm_private;
15385 15381 ASSERT(sfip != NULL);
15386 15382
15387 15383 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15388 15384
15389 15385 /*
15390 15386 * Note: check for the "sdrestart failed" case.
15391 15387 */
15392 15388 if ((un->un_partial_dma_supported == 1) &&
15393 15389 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15394 15390 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15395 15391 (xp->xb_pktp->pkt_resid == 0)) {
15396 15392
15397 15393 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15398 15394 /*
15399 15395 * Successfully set up next portion of cmd
15400 15396 * transfer, try sending it
15401 15397 */
15402 15398 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15403 15399 NULL, NULL, 0, (clock_t)0, NULL);
15404 15400 sd_start_cmds(un, NULL);
15405 15401 return; /* Note:x86: need a return here? */
15406 15402 }
15407 15403 }
15408 15404
15409 15405 /*
15410 15406 * If this is the failfast bp, clear it from un_failfast_bp. This
15411 15407 * can happen if upon being re-tried the failfast bp either
15412 15408 * succeeded or encountered another error (possibly even a different
15413 15409 * error than the one that precipitated the failfast state, but in
15414 15410 * that case it would have had to exhaust retries as well). Regardless,
15415 15411 * this should not occur whenever the instance is in the active
15416 15412 * failfast state.
15417 15413 */
15418 15414 if (bp == un->un_failfast_bp) {
15419 15415 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15420 15416 un->un_failfast_bp = NULL;
15421 15417 }
15422 15418
15423 15419 /*
15424 15420 * Clear the failfast state upon successful completion of ANY cmd.
15425 15421 */
15426 15422 if (bp->b_error == 0) {
15427 15423 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15428 15424 /*
15429 15425 * If this is a successful command, but used to be retried,
15430 15426 * we will take it as a recovered command and post an
15431 15427 * ereport with driver-assessment of "recovered".
15432 15428 */
15433 15429 if (xp->xb_ena > 0) {
15434 15430 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15435 15431 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15436 15432 }
15437 15433 } else {
15438 15434 /*
15439 15435 * If this is a failed non-USCSI command we will post an
15440 15436 * ereport with driver-assessment set accordingly("fail" or
15441 15437 * "fatal").
15442 15438 */
15443 15439 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15444 15440 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15445 15441 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15446 15442 }
15447 15443 }
15448 15444
15449 15445 /*
15450 15446 * This is used if the command was retried one or more times. Show that
15451 15447 * we are done with it, and allow processing of the waitq to resume.
15452 15448 */
15453 15449 if (bp == un->un_retry_bp) {
15454 15450 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15455 15451 "sd_return_command: un:0x%p: "
15456 15452 "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15457 15453 un->un_retry_bp = NULL;
15458 15454 un->un_retry_statp = NULL;
15459 15455 }
15460 15456
15461 15457 SD_UPDATE_RDWR_STATS(un, bp);
15462 15458 SD_UPDATE_PARTITION_STATS(un, bp);
15463 15459
15464 15460 switch (un->un_state) {
15465 15461 case SD_STATE_SUSPENDED:
15466 15462 /*
15467 15463 * Notify any threads waiting in sd_ddi_suspend() that
15468 15464 * a command completion has occurred.
15469 15465 */
15470 15466 cv_broadcast(&un->un_disk_busy_cv);
15471 15467 break;
15472 15468 default:
15473 15469 sd_start_cmds(un, NULL);
15474 15470 break;
15475 15471 }
15476 15472
15477 15473 /* Return this command up the iodone chain to its originator. */
15478 15474 mutex_exit(SD_MUTEX(un));
15479 15475
15480 15476 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15481 15477 xp->xb_pktp = NULL;
15482 15478
15483 15479 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15484 15480
15485 15481 ASSERT(!mutex_owned(SD_MUTEX(un)));
15486 15482 mutex_enter(SD_MUTEX(un));
15487 15483
15488 15484 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15489 15485 }
15490 15486
15491 15487
15492 15488 /*
15493 15489 * Function: sd_return_failed_command
15494 15490 *
15495 15491 * Description: Command completion when an error occurred.
15496 15492 *
15497 15493 * Context: May be called from interrupt context
15498 15494 */
15499 15495
15500 15496 static void
15501 15497 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15502 15498 {
15503 15499 ASSERT(bp != NULL);
15504 15500 ASSERT(un != NULL);
15505 15501 ASSERT(mutex_owned(SD_MUTEX(un)));
15506 15502
15507 15503 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15508 15504 "sd_return_failed_command: entry\n");
15509 15505
15510 15506 /*
15511 15507 * b_resid could already be nonzero due to a partial data
15512 15508 * transfer, so do not change it here.
15513 15509 */
15514 15510 SD_BIOERROR(bp, errcode);
15515 15511
15516 15512 sd_return_command(un, bp);
15517 15513 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15518 15514 "sd_return_failed_command: exit\n");
15519 15515 }
15520 15516
15521 15517
15522 15518 /*
15523 15519 * Function: sd_return_failed_command_no_restart
15524 15520 *
15525 15521 * Description: Same as sd_return_failed_command, but ensures that no
15526 15522 * call back into sd_start_cmds will be issued.
15527 15523 *
15528 15524 * Context: May be called from interrupt context
15529 15525 */
15530 15526
15531 15527 static void
15532 15528 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15533 15529 int errcode)
15534 15530 {
15535 15531 struct sd_xbuf *xp;
15536 15532
15537 15533 ASSERT(bp != NULL);
15538 15534 ASSERT(un != NULL);
15539 15535 ASSERT(mutex_owned(SD_MUTEX(un)));
15540 15536 xp = SD_GET_XBUF(bp);
15541 15537 ASSERT(xp != NULL);
15542 15538 ASSERT(errcode != 0);
15543 15539
15544 15540 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15545 15541 "sd_return_failed_command_no_restart: entry\n");
15546 15542
15547 15543 /*
15548 15544 * b_resid could already be nonzero due to a partial data
15549 15545 * transfer, so do not change it here.
15550 15546 */
15551 15547 SD_BIOERROR(bp, errcode);
15552 15548
15553 15549 /*
15554 15550 * If this is the failfast bp, clear it. This can happen if the
15555 15551 * failfast bp encounterd a fatal error when we attempted to
15556 15552 * re-try it (such as a scsi_transport(9F) failure). However
15557 15553 * we should NOT be in an active failfast state if the failfast
15558 15554 * bp is not NULL.
15559 15555 */
15560 15556 if (bp == un->un_failfast_bp) {
15561 15557 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15562 15558 un->un_failfast_bp = NULL;
15563 15559 }
15564 15560
15565 15561 if (bp == un->un_retry_bp) {
15566 15562 /*
15567 15563 * This command was retried one or more times. Show that we are
15568 15564 * done with it, and allow processing of the waitq to resume.
15569 15565 */
15570 15566 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15571 15567 "sd_return_failed_command_no_restart: "
15572 15568 " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15573 15569 un->un_retry_bp = NULL;
15574 15570 un->un_retry_statp = NULL;
15575 15571 }
15576 15572
15577 15573 SD_UPDATE_RDWR_STATS(un, bp);
15578 15574 SD_UPDATE_PARTITION_STATS(un, bp);
15579 15575
15580 15576 mutex_exit(SD_MUTEX(un));
15581 15577
15582 15578 if (xp->xb_pktp != NULL) {
15583 15579 (*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15584 15580 xp->xb_pktp = NULL;
15585 15581 }
15586 15582
15587 15583 SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15588 15584
15589 15585 mutex_enter(SD_MUTEX(un));
15590 15586
15591 15587 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15592 15588 "sd_return_failed_command_no_restart: exit\n");
15593 15589 }
15594 15590
15595 15591
15596 15592 /*
15597 15593 * Function: sd_retry_command
15598 15594 *
15599 15595 * Description: queue up a command for retry, or (optionally) fail it
15600 15596 * if retry counts are exhausted.
15601 15597 *
15602 15598 * Arguments: un - Pointer to the sd_lun struct for the target.
15603 15599 *
15604 15600 * bp - Pointer to the buf for the command to be retried.
15605 15601 *
15606 15602 * retry_check_flag - Flag to see which (if any) of the retry
15607 15603 * counts should be decremented/checked. If the indicated
15608 15604 * retry count is exhausted, then the command will not be
15609 15605 * retried; it will be failed instead. This should use a
15610 15606 * value equal to one of the following:
15611 15607 *
15612 15608 * SD_RETRIES_NOCHECK
15613 15609 * SD_RESD_RETRIES_STANDARD
15614 15610 * SD_RETRIES_VICTIM
15615 15611 *
15616 15612 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15617 15613 * if the check should be made to see of FLAG_ISOLATE is set
15618 15614 * in the pkt. If FLAG_ISOLATE is set, then the command is
15619 15615 * not retried, it is simply failed.
15620 15616 *
15621 15617 * user_funcp - Ptr to function to call before dispatching the
15622 15618 * command. May be NULL if no action needs to be performed.
15623 15619 * (Primarily intended for printing messages.)
15624 15620 *
15625 15621 * user_arg - Optional argument to be passed along to
15626 15622 * the user_funcp call.
15627 15623 *
15628 15624 * failure_code - errno return code to set in the bp if the
15629 15625 * command is going to be failed.
15630 15626 *
15631 15627 * retry_delay - Retry delay interval in (clock_t) units. May
15632 15628 * be zero which indicates that the retry should be retried
15633 15629 * immediately (ie, without an intervening delay).
15634 15630 *
15635 15631 * statp - Ptr to kstat function to be updated if the command
15636 15632 * is queued for a delayed retry. May be NULL if no kstat
15637 15633 * update is desired.
15638 15634 *
15639 15635 * Context: May be called from interrupt context.
15640 15636 */
15641 15637
15642 15638 static void
15643 15639 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15644 15640 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15645 15641 code), void *user_arg, int failure_code, clock_t retry_delay,
15646 15642 void (*statp)(kstat_io_t *))
15647 15643 {
15648 15644 struct sd_xbuf *xp;
15649 15645 struct scsi_pkt *pktp;
15650 15646 struct sd_fm_internal *sfip;
15651 15647
15652 15648 ASSERT(un != NULL);
15653 15649 ASSERT(mutex_owned(SD_MUTEX(un)));
15654 15650 ASSERT(bp != NULL);
15655 15651 xp = SD_GET_XBUF(bp);
15656 15652 ASSERT(xp != NULL);
15657 15653 pktp = SD_GET_PKTP(bp);
15658 15654 ASSERT(pktp != NULL);
15659 15655
15660 15656 sfip = (struct sd_fm_internal *)un->un_fm_private;
15661 15657 ASSERT(sfip != NULL);
15662 15658
15663 15659 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15664 15660 "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15665 15661
15666 15662 /*
15667 15663 * If we are syncing or dumping, fail the command to avoid
15668 15664 * recursively calling back into scsi_transport().
15669 15665 */
15670 15666 if (ddi_in_panic()) {
15671 15667 goto fail_command_no_log;
15672 15668 }
15673 15669
15674 15670 /*
15675 15671 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15676 15672 * log an error and fail the command.
15677 15673 */
15678 15674 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15679 15675 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15680 15676 "ERROR, retrying FLAG_DIAGNOSE command.\n");
15681 15677 sd_dump_memory(un, SD_LOG_IO, "CDB",
15682 15678 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15683 15679 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15684 15680 (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15685 15681 goto fail_command;
15686 15682 }
15687 15683
15688 15684 /*
15689 15685 * If we are suspended, then put the command onto head of the
15690 15686 * wait queue since we don't want to start more commands, and
15691 15687 * clear the un_retry_bp. Next time when we are resumed, will
15692 15688 * handle the command in the wait queue.
15693 15689 */
15694 15690 switch (un->un_state) {
15695 15691 case SD_STATE_SUSPENDED:
15696 15692 case SD_STATE_DUMPING:
15697 15693 bp->av_forw = un->un_waitq_headp;
15698 15694 un->un_waitq_headp = bp;
15699 15695 if (un->un_waitq_tailp == NULL) {
15700 15696 un->un_waitq_tailp = bp;
15701 15697 }
15702 15698 if (bp == un->un_retry_bp) {
15703 15699 un->un_retry_bp = NULL;
15704 15700 un->un_retry_statp = NULL;
15705 15701 }
15706 15702 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15707 15703 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15708 15704 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15709 15705 return;
15710 15706 default:
15711 15707 break;
15712 15708 }
15713 15709
15714 15710 /*
15715 15711 * If the caller wants us to check FLAG_ISOLATE, then see if that
15716 15712 * is set; if it is then we do not want to retry the command.
15717 15713 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15718 15714 */
15719 15715 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15720 15716 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15721 15717 goto fail_command;
15722 15718 }
15723 15719 }
15724 15720
15725 15721
15726 15722 /*
15727 15723 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15728 15724 * command timeout or a selection timeout has occurred. This means
15729 15725 * that we were unable to establish an kind of communication with
15730 15726 * the target, and subsequent retries and/or commands are likely
15731 15727 * to encounter similar results and take a long time to complete.
15732 15728 *
15733 15729 * If this is a failfast error condition, we need to update the
15734 15730 * failfast state, even if this bp does not have B_FAILFAST set.
15735 15731 */
15736 15732 if (retry_check_flag & SD_RETRIES_FAILFAST) {
15737 15733 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15738 15734 ASSERT(un->un_failfast_bp == NULL);
15739 15735 /*
15740 15736 * If we are already in the active failfast state, and
15741 15737 * another failfast error condition has been detected,
15742 15738 * then fail this command if it has B_FAILFAST set.
15743 15739 * If B_FAILFAST is clear, then maintain the legacy
15744 15740 * behavior of retrying heroically, even tho this will
15745 15741 * take a lot more time to fail the command.
15746 15742 */
15747 15743 if (bp->b_flags & B_FAILFAST) {
15748 15744 goto fail_command;
15749 15745 }
15750 15746 } else {
15751 15747 /*
15752 15748 * We're not in the active failfast state, but we
15753 15749 * have a failfast error condition, so we must begin
15754 15750 * transition to the next state. We do this regardless
15755 15751 * of whether or not this bp has B_FAILFAST set.
15756 15752 */
15757 15753 if (un->un_failfast_bp == NULL) {
15758 15754 /*
15759 15755 * This is the first bp to meet a failfast
15760 15756 * condition so save it on un_failfast_bp &
15761 15757 * do normal retry processing. Do not enter
15762 15758 * active failfast state yet. This marks
15763 15759 * entry into the "failfast pending" state.
15764 15760 */
15765 15761 un->un_failfast_bp = bp;
15766 15762
15767 15763 } else if (un->un_failfast_bp == bp) {
15768 15764 /*
15769 15765 * This is the second time *this* bp has
15770 15766 * encountered a failfast error condition,
15771 15767 * so enter active failfast state & flush
15772 15768 * queues as appropriate.
15773 15769 */
15774 15770 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15775 15771 un->un_failfast_bp = NULL;
15776 15772 sd_failfast_flushq(un);
15777 15773
15778 15774 /*
15779 15775 * Fail this bp now if B_FAILFAST set;
15780 15776 * otherwise continue with retries. (It would
15781 15777 * be pretty ironic if this bp succeeded on a
15782 15778 * subsequent retry after we just flushed all
15783 15779 * the queues).
15784 15780 */
15785 15781 if (bp->b_flags & B_FAILFAST) {
15786 15782 goto fail_command;
15787 15783 }
15788 15784
15789 15785 #if !defined(lint) && !defined(__lint)
15790 15786 } else {
15791 15787 /*
15792 15788 * If neither of the preceeding conditionals
15793 15789 * was true, it means that there is some
15794 15790 * *other* bp that has met an inital failfast
15795 15791 * condition and is currently either being
15796 15792 * retried or is waiting to be retried. In
15797 15793 * that case we should perform normal retry
15798 15794 * processing on *this* bp, since there is a
15799 15795 * chance that the current failfast condition
15800 15796 * is transient and recoverable. If that does
15801 15797 * not turn out to be the case, then retries
15802 15798 * will be cleared when the wait queue is
15803 15799 * flushed anyway.
15804 15800 */
15805 15801 #endif
15806 15802 }
15807 15803 }
15808 15804 } else {
15809 15805 /*
15810 15806 * SD_RETRIES_FAILFAST is clear, which indicates that we
15811 15807 * likely were able to at least establish some level of
15812 15808 * communication with the target and subsequent commands
15813 15809 * and/or retries are likely to get through to the target,
15814 15810 * In this case we want to be aggressive about clearing
15815 15811 * the failfast state. Note that this does not affect
15816 15812 * the "failfast pending" condition.
15817 15813 */
15818 15814 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15819 15815 }
15820 15816
15821 15817
15822 15818 /*
15823 15819 * Check the specified retry count to see if we can still do
15824 15820 * any retries with this pkt before we should fail it.
15825 15821 */
15826 15822 switch (retry_check_flag & SD_RETRIES_MASK) {
15827 15823 case SD_RETRIES_VICTIM:
15828 15824 /*
15829 15825 * Check the victim retry count. If exhausted, then fall
15830 15826 * thru & check against the standard retry count.
15831 15827 */
15832 15828 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15833 15829 /* Increment count & proceed with the retry */
15834 15830 xp->xb_victim_retry_count++;
15835 15831 break;
15836 15832 }
15837 15833 /* Victim retries exhausted, fall back to std. retries... */
15838 15834 /* FALLTHRU */
15839 15835
15840 15836 case SD_RETRIES_STANDARD:
15841 15837 if (xp->xb_retry_count >= un->un_retry_count) {
15842 15838 /* Retries exhausted, fail the command */
15843 15839 SD_TRACE(SD_LOG_IO_CORE, un,
15844 15840 "sd_retry_command: retries exhausted!\n");
15845 15841 /*
15846 15842 * update b_resid for failed SCMD_READ & SCMD_WRITE
15847 15843 * commands with nonzero pkt_resid.
15848 15844 */
15849 15845 if ((pktp->pkt_reason == CMD_CMPLT) &&
15850 15846 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15851 15847 (pktp->pkt_resid != 0)) {
15852 15848 uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15853 15849 if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15854 15850 SD_UPDATE_B_RESID(bp, pktp);
15855 15851 }
15856 15852 }
15857 15853 goto fail_command;
15858 15854 }
15859 15855 xp->xb_retry_count++;
15860 15856 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15861 15857 "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15862 15858 break;
15863 15859
15864 15860 case SD_RETRIES_UA:
15865 15861 if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15866 15862 /* Retries exhausted, fail the command */
15867 15863 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15868 15864 "Unit Attention retries exhausted. "
15869 15865 "Check the target.\n");
15870 15866 goto fail_command;
15871 15867 }
15872 15868 xp->xb_ua_retry_count++;
15873 15869 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15874 15870 "sd_retry_command: retry count:%d\n",
15875 15871 xp->xb_ua_retry_count);
15876 15872 break;
15877 15873
15878 15874 case SD_RETRIES_BUSY:
15879 15875 if (xp->xb_retry_count >= un->un_busy_retry_count) {
15880 15876 /* Retries exhausted, fail the command */
15881 15877 SD_TRACE(SD_LOG_IO_CORE, un,
15882 15878 "sd_retry_command: retries exhausted!\n");
15883 15879 goto fail_command;
15884 15880 }
15885 15881 xp->xb_retry_count++;
15886 15882 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15887 15883 "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15888 15884 break;
15889 15885
15890 15886 case SD_RETRIES_NOCHECK:
15891 15887 default:
15892 15888 /* No retry count to check. Just proceed with the retry */
15893 15889 break;
15894 15890 }
15895 15891
15896 15892 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15897 15893
15898 15894 /*
15899 15895 * If this is a non-USCSI command being retried
15900 15896 * during execution last time, we should post an ereport with
15901 15897 * driver-assessment of the value "retry".
15902 15898 * For partial DMA, request sense and STATUS_QFULL, there are no
15903 15899 * hardware errors, we bypass ereport posting.
15904 15900 */
15905 15901 if (failure_code != 0) {
15906 15902 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15907 15903 sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15908 15904 sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15909 15905 }
15910 15906 }
15911 15907
15912 15908 /*
15913 15909 * If we were given a zero timeout, we must attempt to retry the
15914 15910 * command immediately (ie, without a delay).
15915 15911 */
15916 15912 if (retry_delay == 0) {
15917 15913 /*
15918 15914 * Check some limiting conditions to see if we can actually
15919 15915 * do the immediate retry. If we cannot, then we must
15920 15916 * fall back to queueing up a delayed retry.
15921 15917 */
15922 15918 if (un->un_ncmds_in_transport >= un->un_throttle) {
15923 15919 /*
15924 15920 * We are at the throttle limit for the target,
15925 15921 * fall back to delayed retry.
15926 15922 */
15927 15923 retry_delay = un->un_busy_timeout;
15928 15924 statp = kstat_waitq_enter;
15929 15925 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15930 15926 "sd_retry_command: immed. retry hit "
15931 15927 "throttle!\n");
15932 15928 } else {
15933 15929 /*
15934 15930 * We're clear to proceed with the immediate retry.
15935 15931 * First call the user-provided function (if any)
15936 15932 */
15937 15933 if (user_funcp != NULL) {
15938 15934 (*user_funcp)(un, bp, user_arg,
15939 15935 SD_IMMEDIATE_RETRY_ISSUED);
15940 15936 #ifdef __lock_lint
15941 15937 sd_print_incomplete_msg(un, bp, user_arg,
15942 15938 SD_IMMEDIATE_RETRY_ISSUED);
15943 15939 sd_print_cmd_incomplete_msg(un, bp, user_arg,
15944 15940 SD_IMMEDIATE_RETRY_ISSUED);
15945 15941 sd_print_sense_failed_msg(un, bp, user_arg,
15946 15942 SD_IMMEDIATE_RETRY_ISSUED);
15947 15943 #endif
15948 15944 }
15949 15945
15950 15946 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15951 15947 "sd_retry_command: issuing immediate retry\n");
15952 15948
15953 15949 /*
15954 15950 * Call sd_start_cmds() to transport the command to
15955 15951 * the target.
15956 15952 */
15957 15953 sd_start_cmds(un, bp);
15958 15954
15959 15955 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15960 15956 "sd_retry_command exit\n");
15961 15957 return;
15962 15958 }
15963 15959 }
15964 15960
15965 15961 /*
15966 15962 * Set up to retry the command after a delay.
15967 15963 * First call the user-provided function (if any)
15968 15964 */
15969 15965 if (user_funcp != NULL) {
15970 15966 (*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15971 15967 }
15972 15968
15973 15969 sd_set_retry_bp(un, bp, retry_delay, statp);
15974 15970
15975 15971 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15976 15972 return;
15977 15973
15978 15974 fail_command:
15979 15975
15980 15976 if (user_funcp != NULL) {
15981 15977 (*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15982 15978 }
15983 15979
15984 15980 fail_command_no_log:
15985 15981
15986 15982 SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15987 15983 "sd_retry_command: returning failed command\n");
15988 15984
15989 15985 sd_return_failed_command(un, bp, failure_code);
15990 15986
15991 15987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15992 15988 }
15993 15989
15994 15990
15995 15991 /*
15996 15992 * Function: sd_set_retry_bp
15997 15993 *
15998 15994 * Description: Set up the given bp for retry.
15999 15995 *
16000 15996 * Arguments: un - ptr to associated softstate
16001 15997 * bp - ptr to buf(9S) for the command
16002 15998 * retry_delay - time interval before issuing retry (may be 0)
16003 15999 * statp - optional pointer to kstat function
16004 16000 *
16005 16001 * Context: May be called under interrupt context
16006 16002 */
16007 16003
16008 16004 static void
16009 16005 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
16010 16006 void (*statp)(kstat_io_t *))
16011 16007 {
16012 16008 ASSERT(un != NULL);
16013 16009 ASSERT(mutex_owned(SD_MUTEX(un)));
16014 16010 ASSERT(bp != NULL);
16015 16011
16016 16012 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16017 16013 "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
16018 16014
16019 16015 /*
16020 16016 * Indicate that the command is being retried. This will not allow any
16021 16017 * other commands on the wait queue to be transported to the target
16022 16018 * until this command has been completed (success or failure). The
16023 16019 * "retry command" is not transported to the target until the given
16024 16020 * time delay expires, unless the user specified a 0 retry_delay.
16025 16021 *
16026 16022 * Note: the timeout(9F) callback routine is what actually calls
16027 16023 * sd_start_cmds() to transport the command, with the exception of a
16028 16024 * zero retry_delay. The only current implementor of a zero retry delay
16029 16025 * is the case where a START_STOP_UNIT is sent to spin-up a device.
16030 16026 */
16031 16027 if (un->un_retry_bp == NULL) {
16032 16028 ASSERT(un->un_retry_statp == NULL);
16033 16029 un->un_retry_bp = bp;
16034 16030
16035 16031 /*
16036 16032 * If the user has not specified a delay the command should
16037 16033 * be queued and no timeout should be scheduled.
16038 16034 */
16039 16035 if (retry_delay == 0) {
16040 16036 /*
16041 16037 * Save the kstat pointer that will be used in the
16042 16038 * call to SD_UPDATE_KSTATS() below, so that
16043 16039 * sd_start_cmds() can correctly decrement the waitq
16044 16040 * count when it is time to transport this command.
16045 16041 */
16046 16042 un->un_retry_statp = statp;
16047 16043 goto done;
16048 16044 }
16049 16045 }
16050 16046
16051 16047 if (un->un_retry_bp == bp) {
16052 16048 /*
16053 16049 * Save the kstat pointer that will be used in the call to
16054 16050 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16055 16051 * correctly decrement the waitq count when it is time to
16056 16052 * transport this command.
16057 16053 */
16058 16054 un->un_retry_statp = statp;
16059 16055
16060 16056 /*
16061 16057 * Schedule a timeout if:
16062 16058 * 1) The user has specified a delay.
16063 16059 * 2) There is not a START_STOP_UNIT callback pending.
16064 16060 *
16065 16061 * If no delay has been specified, then it is up to the caller
16066 16062 * to ensure that IO processing continues without stalling.
16067 16063 * Effectively, this means that the caller will issue the
16068 16064 * required call to sd_start_cmds(). The START_STOP_UNIT
16069 16065 * callback does this after the START STOP UNIT command has
16070 16066 * completed. In either of these cases we should not schedule
16071 16067 * a timeout callback here. Also don't schedule the timeout if
16072 16068 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16073 16069 */
16074 16070 if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16075 16071 (un->un_direct_priority_timeid == NULL)) {
16076 16072 un->un_retry_timeid =
16077 16073 timeout(sd_start_retry_command, un, retry_delay);
16078 16074 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16079 16075 "sd_set_retry_bp: setting timeout: un: 0x%p"
16080 16076 " bp:0x%p un_retry_timeid:0x%p\n",
16081 16077 un, bp, un->un_retry_timeid);
16082 16078 }
16083 16079 } else {
16084 16080 /*
16085 16081 * We only get in here if there is already another command
16086 16082 * waiting to be retried. In this case, we just put the
16087 16083 * given command onto the wait queue, so it can be transported
16088 16084 * after the current retry command has completed.
16089 16085 *
16090 16086 * Also we have to make sure that if the command at the head
16091 16087 * of the wait queue is the un_failfast_bp, that we do not
16092 16088 * put ahead of it any other commands that are to be retried.
16093 16089 */
16094 16090 if ((un->un_failfast_bp != NULL) &&
16095 16091 (un->un_failfast_bp == un->un_waitq_headp)) {
16096 16092 /*
16097 16093 * Enqueue this command AFTER the first command on
16098 16094 * the wait queue (which is also un_failfast_bp).
16099 16095 */
16100 16096 bp->av_forw = un->un_waitq_headp->av_forw;
16101 16097 un->un_waitq_headp->av_forw = bp;
16102 16098 if (un->un_waitq_headp == un->un_waitq_tailp) {
16103 16099 un->un_waitq_tailp = bp;
16104 16100 }
16105 16101 } else {
16106 16102 /* Enqueue this command at the head of the waitq. */
16107 16103 bp->av_forw = un->un_waitq_headp;
16108 16104 un->un_waitq_headp = bp;
16109 16105 if (un->un_waitq_tailp == NULL) {
16110 16106 un->un_waitq_tailp = bp;
16111 16107 }
16112 16108 }
16113 16109
16114 16110 if (statp == NULL) {
16115 16111 statp = kstat_waitq_enter;
16116 16112 }
16117 16113 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16118 16114 "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16119 16115 }
16120 16116
16121 16117 done:
16122 16118 if (statp != NULL) {
16123 16119 SD_UPDATE_KSTATS(un, statp, bp);
16124 16120 }
16125 16121
16126 16122 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16127 16123 "sd_set_retry_bp: exit un:0x%p\n", un);
16128 16124 }
16129 16125
16130 16126
16131 16127 /*
16132 16128 * Function: sd_start_retry_command
16133 16129 *
16134 16130 * Description: Start the command that has been waiting on the target's
16135 16131 * retry queue. Called from timeout(9F) context after the
16136 16132 * retry delay interval has expired.
16137 16133 *
16138 16134 * Arguments: arg - pointer to associated softstate for the device.
16139 16135 *
16140 16136 * Context: timeout(9F) thread context. May not sleep.
16141 16137 */
16142 16138
16143 16139 static void
16144 16140 sd_start_retry_command(void *arg)
16145 16141 {
16146 16142 struct sd_lun *un = arg;
16147 16143
16148 16144 ASSERT(un != NULL);
16149 16145 ASSERT(!mutex_owned(SD_MUTEX(un)));
16150 16146
16151 16147 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16152 16148 "sd_start_retry_command: entry\n");
16153 16149
16154 16150 mutex_enter(SD_MUTEX(un));
16155 16151
16156 16152 un->un_retry_timeid = NULL;
16157 16153
16158 16154 if (un->un_retry_bp != NULL) {
16159 16155 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16160 16156 "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16161 16157 un, un->un_retry_bp);
16162 16158 sd_start_cmds(un, un->un_retry_bp);
16163 16159 }
16164 16160
16165 16161 mutex_exit(SD_MUTEX(un));
16166 16162
16167 16163 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16168 16164 "sd_start_retry_command: exit\n");
16169 16165 }
16170 16166
16171 16167 /*
16172 16168 * Function: sd_rmw_msg_print_handler
16173 16169 *
16174 16170 * Description: If RMW mode is enabled and warning message is triggered
16175 16171 * print I/O count during a fixed interval.
16176 16172 *
16177 16173 * Arguments: arg - pointer to associated softstate for the device.
16178 16174 *
16179 16175 * Context: timeout(9F) thread context. May not sleep.
16180 16176 */
16181 16177 static void
16182 16178 sd_rmw_msg_print_handler(void *arg)
16183 16179 {
16184 16180 struct sd_lun *un = arg;
16185 16181
16186 16182 ASSERT(un != NULL);
16187 16183 ASSERT(!mutex_owned(SD_MUTEX(un)));
16188 16184
16189 16185 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16190 16186 "sd_rmw_msg_print_handler: entry\n");
16191 16187
16192 16188 mutex_enter(SD_MUTEX(un));
16193 16189
16194 16190 if (un->un_rmw_incre_count > 0) {
16195 16191 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16196 16192 "%"PRIu64" I/O requests are not aligned with %d disk "
16197 16193 "sector size in %ld seconds. They are handled through "
16198 16194 "Read Modify Write but the performance is very low!\n",
16199 16195 un->un_rmw_incre_count, un->un_tgt_blocksize,
16200 16196 drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16201 16197 un->un_rmw_incre_count = 0;
16202 16198 un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16203 16199 un, SD_RMW_MSG_PRINT_TIMEOUT);
16204 16200 } else {
16205 16201 un->un_rmw_msg_timeid = NULL;
16206 16202 }
16207 16203
16208 16204 mutex_exit(SD_MUTEX(un));
16209 16205
16210 16206 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16211 16207 "sd_rmw_msg_print_handler: exit\n");
16212 16208 }
16213 16209
16214 16210 /*
16215 16211 * Function: sd_start_direct_priority_command
16216 16212 *
16217 16213 * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16218 16214 * received TRAN_BUSY when we called scsi_transport() to send it
16219 16215 * to the underlying HBA. This function is called from timeout(9F)
16220 16216 * context after the delay interval has expired.
16221 16217 *
16222 16218 * Arguments: arg - pointer to associated buf(9S) to be restarted.
16223 16219 *
16224 16220 * Context: timeout(9F) thread context. May not sleep.
16225 16221 */
16226 16222
16227 16223 static void
16228 16224 sd_start_direct_priority_command(void *arg)
16229 16225 {
16230 16226 struct buf *priority_bp = arg;
16231 16227 struct sd_lun *un;
16232 16228
16233 16229 ASSERT(priority_bp != NULL);
16234 16230 un = SD_GET_UN(priority_bp);
16235 16231 ASSERT(un != NULL);
16236 16232 ASSERT(!mutex_owned(SD_MUTEX(un)));
16237 16233
16238 16234 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16239 16235 "sd_start_direct_priority_command: entry\n");
16240 16236
16241 16237 mutex_enter(SD_MUTEX(un));
16242 16238 un->un_direct_priority_timeid = NULL;
16243 16239 sd_start_cmds(un, priority_bp);
16244 16240 mutex_exit(SD_MUTEX(un));
16245 16241
16246 16242 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16247 16243 "sd_start_direct_priority_command: exit\n");
16248 16244 }
16249 16245
16250 16246
16251 16247 /*
16252 16248 * Function: sd_send_request_sense_command
16253 16249 *
16254 16250 * Description: Sends a REQUEST SENSE command to the target
16255 16251 *
16256 16252 * Context: May be called from interrupt context.
16257 16253 */
16258 16254
16259 16255 static void
16260 16256 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16261 16257 struct scsi_pkt *pktp)
16262 16258 {
16263 16259 ASSERT(bp != NULL);
16264 16260 ASSERT(un != NULL);
16265 16261 ASSERT(mutex_owned(SD_MUTEX(un)));
16266 16262
16267 16263 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16268 16264 "entry: buf:0x%p\n", bp);
16269 16265
16270 16266 /*
16271 16267 * If we are syncing or dumping, then fail the command to avoid a
16272 16268 * recursive callback into scsi_transport(). Also fail the command
16273 16269 * if we are suspended (legacy behavior).
16274 16270 */
16275 16271 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16276 16272 (un->un_state == SD_STATE_DUMPING)) {
16277 16273 sd_return_failed_command(un, bp, EIO);
16278 16274 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16279 16275 "sd_send_request_sense_command: syncing/dumping, exit\n");
16280 16276 return;
16281 16277 }
16282 16278
16283 16279 /*
16284 16280 * Retry the failed command and don't issue the request sense if:
16285 16281 * 1) the sense buf is busy
16286 16282 * 2) we have 1 or more outstanding commands on the target
16287 16283 * (the sense data will be cleared or invalidated any way)
16288 16284 *
16289 16285 * Note: There could be an issue with not checking a retry limit here,
16290 16286 * the problem is determining which retry limit to check.
16291 16287 */
16292 16288 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16293 16289 /* Don't retry if the command is flagged as non-retryable */
16294 16290 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16295 16291 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16296 16292 NULL, NULL, 0, un->un_busy_timeout,
16297 16293 kstat_waitq_enter);
16298 16294 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16299 16295 "sd_send_request_sense_command: "
16300 16296 "at full throttle, retrying exit\n");
16301 16297 } else {
16302 16298 sd_return_failed_command(un, bp, EIO);
16303 16299 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16304 16300 "sd_send_request_sense_command: "
16305 16301 "at full throttle, non-retryable exit\n");
16306 16302 }
16307 16303 return;
16308 16304 }
16309 16305
16310 16306 sd_mark_rqs_busy(un, bp);
16311 16307 sd_start_cmds(un, un->un_rqs_bp);
16312 16308
16313 16309 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16314 16310 "sd_send_request_sense_command: exit\n");
16315 16311 }
16316 16312
16317 16313
16318 16314 /*
16319 16315 * Function: sd_mark_rqs_busy
16320 16316 *
16321 16317 * Description: Indicate that the request sense bp for this instance is
16322 16318 * in use.
16323 16319 *
16324 16320 * Context: May be called under interrupt context
16325 16321 */
16326 16322
16327 16323 static void
16328 16324 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16329 16325 {
16330 16326 struct sd_xbuf *sense_xp;
16331 16327
16332 16328 ASSERT(un != NULL);
16333 16329 ASSERT(bp != NULL);
16334 16330 ASSERT(mutex_owned(SD_MUTEX(un)));
16335 16331 ASSERT(un->un_sense_isbusy == 0);
16336 16332
16337 16333 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16338 16334 "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16339 16335
16340 16336 sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16341 16337 ASSERT(sense_xp != NULL);
16342 16338
16343 16339 SD_INFO(SD_LOG_IO, un,
16344 16340 "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16345 16341
16346 16342 ASSERT(sense_xp->xb_pktp != NULL);
16347 16343 ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16348 16344 == (FLAG_SENSING | FLAG_HEAD));
16349 16345
16350 16346 un->un_sense_isbusy = 1;
16351 16347 un->un_rqs_bp->b_resid = 0;
16352 16348 sense_xp->xb_pktp->pkt_resid = 0;
16353 16349 sense_xp->xb_pktp->pkt_reason = 0;
16354 16350
16355 16351 /* So we can get back the bp at interrupt time! */
16356 16352 sense_xp->xb_sense_bp = bp;
16357 16353
16358 16354 bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16359 16355
16360 16356 /*
16361 16357 * Mark this buf as awaiting sense data. (This is already set in
16362 16358 * the pkt_flags for the RQS packet.)
16363 16359 */
16364 16360 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16365 16361
16366 16362 /* Request sense down same path */
16367 16363 if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16368 16364 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16369 16365 sense_xp->xb_pktp->pkt_path_instance =
16370 16366 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16371 16367
16372 16368 sense_xp->xb_retry_count = 0;
16373 16369 sense_xp->xb_victim_retry_count = 0;
16374 16370 sense_xp->xb_ua_retry_count = 0;
16375 16371 sense_xp->xb_nr_retry_count = 0;
16376 16372 sense_xp->xb_dma_resid = 0;
16377 16373
16378 16374 /* Clean up the fields for auto-request sense */
16379 16375 sense_xp->xb_sense_status = 0;
16380 16376 sense_xp->xb_sense_state = 0;
16381 16377 sense_xp->xb_sense_resid = 0;
16382 16378 bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16383 16379
16384 16380 SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16385 16381 }
16386 16382
16387 16383
16388 16384 /*
16389 16385 * Function: sd_mark_rqs_idle
16390 16386 *
16391 16387 * Description: SD_MUTEX must be held continuously through this routine
16392 16388 * to prevent reuse of the rqs struct before the caller can
16393 16389 * complete it's processing.
16394 16390 *
16395 16391 * Return Code: Pointer to the RQS buf
16396 16392 *
16397 16393 * Context: May be called under interrupt context
16398 16394 */
16399 16395
16400 16396 static struct buf *
16401 16397 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16402 16398 {
16403 16399 struct buf *bp;
16404 16400 ASSERT(un != NULL);
16405 16401 ASSERT(sense_xp != NULL);
16406 16402 ASSERT(mutex_owned(SD_MUTEX(un)));
16407 16403 ASSERT(un->un_sense_isbusy != 0);
16408 16404
16409 16405 un->un_sense_isbusy = 0;
16410 16406 bp = sense_xp->xb_sense_bp;
16411 16407 sense_xp->xb_sense_bp = NULL;
16412 16408
16413 16409 /* This pkt is no longer interested in getting sense data */
16414 16410 ((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16415 16411
16416 16412 return (bp);
16417 16413 }
16418 16414
16419 16415
16420 16416
16421 16417 /*
16422 16418 * Function: sd_alloc_rqs
16423 16419 *
16424 16420 * Description: Set up the unit to receive auto request sense data
16425 16421 *
16426 16422 * Return Code: DDI_SUCCESS or DDI_FAILURE
16427 16423 *
16428 16424 * Context: Called under attach(9E) context
16429 16425 */
16430 16426
16431 16427 static int
16432 16428 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16433 16429 {
16434 16430 struct sd_xbuf *xp;
16435 16431
16436 16432 ASSERT(un != NULL);
16437 16433 ASSERT(!mutex_owned(SD_MUTEX(un)));
16438 16434 ASSERT(un->un_rqs_bp == NULL);
16439 16435 ASSERT(un->un_rqs_pktp == NULL);
16440 16436
16441 16437 /*
16442 16438 * First allocate the required buf and scsi_pkt structs, then set up
16443 16439 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16444 16440 */
16445 16441 un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16446 16442 MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16447 16443 if (un->un_rqs_bp == NULL) {
16448 16444 return (DDI_FAILURE);
16449 16445 }
16450 16446
16451 16447 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16452 16448 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16453 16449
16454 16450 if (un->un_rqs_pktp == NULL) {
16455 16451 sd_free_rqs(un);
16456 16452 return (DDI_FAILURE);
16457 16453 }
16458 16454
16459 16455 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16460 16456 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16461 16457 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16462 16458
16463 16459 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16464 16460
16465 16461 /* Set up the other needed members in the ARQ scsi_pkt. */
16466 16462 un->un_rqs_pktp->pkt_comp = sdintr;
16467 16463 un->un_rqs_pktp->pkt_time = sd_io_time;
16468 16464 un->un_rqs_pktp->pkt_flags |=
16469 16465 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */
16470 16466
16471 16467 /*
16472 16468 * Allocate & init the sd_xbuf struct for the RQS command. Do not
16473 16469 * provide any intpkt, destroypkt routines as we take care of
16474 16470 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16475 16471 */
16476 16472 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16477 16473 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16478 16474 xp->xb_pktp = un->un_rqs_pktp;
16479 16475 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16480 16476 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n",
16481 16477 un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16482 16478
16483 16479 /*
16484 16480 * Save the pointer to the request sense private bp so it can
16485 16481 * be retrieved in sdintr.
16486 16482 */
16487 16483 un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16488 16484 ASSERT(un->un_rqs_bp->b_private == xp);
16489 16485
16490 16486 /*
16491 16487 * See if the HBA supports auto-request sense for the specified
16492 16488 * target/lun. If it does, then try to enable it (if not already
16493 16489 * enabled).
16494 16490 *
16495 16491 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16496 16492 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16497 16493 * return success. However, in both of these cases ARQ is always
16498 16494 * enabled and scsi_ifgetcap will always return true. The best approach
16499 16495 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16500 16496 *
16501 16497 * The 3rd case is the HBA (adp) always return enabled on
16502 16498 * scsi_ifgetgetcap even when it's not enable, the best approach
16503 16499 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16504 16500 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16505 16501 */
16506 16502
16507 16503 if (un->un_f_is_fibre == TRUE) {
16508 16504 un->un_f_arq_enabled = TRUE;
16509 16505 } else {
16510 16506 #if defined(__i386) || defined(__amd64)
16511 16507 /*
16512 16508 * Circumvent the Adaptec bug, remove this code when
16513 16509 * the bug is fixed
16514 16510 */
16515 16511 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16516 16512 #endif
16517 16513 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16518 16514 case 0:
16519 16515 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16520 16516 "sd_alloc_rqs: HBA supports ARQ\n");
16521 16517 /*
16522 16518 * ARQ is supported by this HBA but currently is not
16523 16519 * enabled. Attempt to enable it and if successful then
16524 16520 * mark this instance as ARQ enabled.
16525 16521 */
16526 16522 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16527 16523 == 1) {
16528 16524 /* Successfully enabled ARQ in the HBA */
16529 16525 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16530 16526 "sd_alloc_rqs: ARQ enabled\n");
16531 16527 un->un_f_arq_enabled = TRUE;
16532 16528 } else {
16533 16529 /* Could not enable ARQ in the HBA */
16534 16530 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16535 16531 "sd_alloc_rqs: failed ARQ enable\n");
16536 16532 un->un_f_arq_enabled = FALSE;
16537 16533 }
16538 16534 break;
16539 16535 case 1:
16540 16536 /*
16541 16537 * ARQ is supported by this HBA and is already enabled.
16542 16538 * Just mark ARQ as enabled for this instance.
16543 16539 */
16544 16540 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16545 16541 "sd_alloc_rqs: ARQ already enabled\n");
16546 16542 un->un_f_arq_enabled = TRUE;
16547 16543 break;
16548 16544 default:
16549 16545 /*
16550 16546 * ARQ is not supported by this HBA; disable it for this
16551 16547 * instance.
16552 16548 */
16553 16549 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16554 16550 "sd_alloc_rqs: HBA does not support ARQ\n");
16555 16551 un->un_f_arq_enabled = FALSE;
16556 16552 break;
16557 16553 }
16558 16554 }
16559 16555
16560 16556 return (DDI_SUCCESS);
16561 16557 }
16562 16558
16563 16559
16564 16560 /*
16565 16561 * Function: sd_free_rqs
16566 16562 *
16567 16563 * Description: Cleanup for the pre-instance RQS command.
16568 16564 *
16569 16565 * Context: Kernel thread context
16570 16566 */
16571 16567
16572 16568 static void
16573 16569 sd_free_rqs(struct sd_lun *un)
16574 16570 {
16575 16571 ASSERT(un != NULL);
16576 16572
16577 16573 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16578 16574
16579 16575 /*
16580 16576 * If consistent memory is bound to a scsi_pkt, the pkt
16581 16577 * has to be destroyed *before* freeing the consistent memory.
16582 16578 * Don't change the sequence of this operations.
16583 16579 * scsi_destroy_pkt() might access memory, which isn't allowed,
16584 16580 * after it was freed in scsi_free_consistent_buf().
16585 16581 */
16586 16582 if (un->un_rqs_pktp != NULL) {
16587 16583 scsi_destroy_pkt(un->un_rqs_pktp);
16588 16584 un->un_rqs_pktp = NULL;
16589 16585 }
16590 16586
16591 16587 if (un->un_rqs_bp != NULL) {
16592 16588 struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16593 16589 if (xp != NULL) {
16594 16590 kmem_free(xp, sizeof (struct sd_xbuf));
16595 16591 }
16596 16592 scsi_free_consistent_buf(un->un_rqs_bp);
16597 16593 un->un_rqs_bp = NULL;
16598 16594 }
16599 16595 SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16600 16596 }
16601 16597
16602 16598
16603 16599
16604 16600 /*
16605 16601 * Function: sd_reduce_throttle
16606 16602 *
16607 16603 * Description: Reduces the maximum # of outstanding commands on a
16608 16604 * target to the current number of outstanding commands.
16609 16605 * Queues a tiemout(9F) callback to restore the limit
16610 16606 * after a specified interval has elapsed.
16611 16607 * Typically used when we get a TRAN_BUSY return code
16612 16608 * back from scsi_transport().
16613 16609 *
16614 16610 * Arguments: un - ptr to the sd_lun softstate struct
16615 16611 * throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16616 16612 *
16617 16613 * Context: May be called from interrupt context
16618 16614 */
16619 16615
16620 16616 static void
16621 16617 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16622 16618 {
16623 16619 ASSERT(un != NULL);
16624 16620 ASSERT(mutex_owned(SD_MUTEX(un)));
16625 16621 ASSERT(un->un_ncmds_in_transport >= 0);
16626 16622
16627 16623 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16628 16624 "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16629 16625 un, un->un_throttle, un->un_ncmds_in_transport);
16630 16626
16631 16627 if (un->un_throttle > 1) {
16632 16628 if (un->un_f_use_adaptive_throttle == TRUE) {
16633 16629 switch (throttle_type) {
16634 16630 case SD_THROTTLE_TRAN_BUSY:
16635 16631 if (un->un_busy_throttle == 0) {
16636 16632 un->un_busy_throttle = un->un_throttle;
16637 16633 }
16638 16634 break;
16639 16635 case SD_THROTTLE_QFULL:
16640 16636 un->un_busy_throttle = 0;
16641 16637 break;
16642 16638 default:
16643 16639 ASSERT(FALSE);
16644 16640 }
16645 16641
16646 16642 if (un->un_ncmds_in_transport > 0) {
16647 16643 un->un_throttle = un->un_ncmds_in_transport;
16648 16644 }
16649 16645
16650 16646 } else {
16651 16647 if (un->un_ncmds_in_transport == 0) {
16652 16648 un->un_throttle = 1;
16653 16649 } else {
16654 16650 un->un_throttle = un->un_ncmds_in_transport;
16655 16651 }
16656 16652 }
16657 16653 }
16658 16654
16659 16655 /* Reschedule the timeout if none is currently active */
16660 16656 if (un->un_reset_throttle_timeid == NULL) {
16661 16657 un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16662 16658 un, SD_THROTTLE_RESET_INTERVAL);
16663 16659 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16664 16660 "sd_reduce_throttle: timeout scheduled!\n");
16665 16661 }
16666 16662
16667 16663 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16668 16664 "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16669 16665 }
16670 16666
16671 16667
16672 16668
16673 16669 /*
16674 16670 * Function: sd_restore_throttle
16675 16671 *
16676 16672 * Description: Callback function for timeout(9F). Resets the current
16677 16673 * value of un->un_throttle to its default.
16678 16674 *
16679 16675 * Arguments: arg - pointer to associated softstate for the device.
16680 16676 *
16681 16677 * Context: May be called from interrupt context
16682 16678 */
16683 16679
16684 16680 static void
16685 16681 sd_restore_throttle(void *arg)
16686 16682 {
16687 16683 struct sd_lun *un = arg;
16688 16684
16689 16685 ASSERT(un != NULL);
16690 16686 ASSERT(!mutex_owned(SD_MUTEX(un)));
16691 16687
16692 16688 mutex_enter(SD_MUTEX(un));
16693 16689
16694 16690 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16695 16691 "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16696 16692
16697 16693 un->un_reset_throttle_timeid = NULL;
16698 16694
16699 16695 if (un->un_f_use_adaptive_throttle == TRUE) {
16700 16696 /*
16701 16697 * If un_busy_throttle is nonzero, then it contains the
16702 16698 * value that un_throttle was when we got a TRAN_BUSY back
16703 16699 * from scsi_transport(). We want to revert back to this
16704 16700 * value.
16705 16701 *
16706 16702 * In the QFULL case, the throttle limit will incrementally
16707 16703 * increase until it reaches max throttle.
16708 16704 */
16709 16705 if (un->un_busy_throttle > 0) {
16710 16706 un->un_throttle = un->un_busy_throttle;
16711 16707 un->un_busy_throttle = 0;
16712 16708 } else {
16713 16709 /*
16714 16710 * increase throttle by 10% open gate slowly, schedule
16715 16711 * another restore if saved throttle has not been
16716 16712 * reached
16717 16713 */
16718 16714 short throttle;
16719 16715 if (sd_qfull_throttle_enable) {
16720 16716 throttle = un->un_throttle +
16721 16717 max((un->un_throttle / 10), 1);
16722 16718 un->un_throttle =
16723 16719 (throttle < un->un_saved_throttle) ?
16724 16720 throttle : un->un_saved_throttle;
16725 16721 if (un->un_throttle < un->un_saved_throttle) {
16726 16722 un->un_reset_throttle_timeid =
16727 16723 timeout(sd_restore_throttle,
16728 16724 un,
16729 16725 SD_QFULL_THROTTLE_RESET_INTERVAL);
16730 16726 }
16731 16727 }
16732 16728 }
16733 16729
16734 16730 /*
16735 16731 * If un_throttle has fallen below the low-water mark, we
16736 16732 * restore the maximum value here (and allow it to ratchet
16737 16733 * down again if necessary).
16738 16734 */
16739 16735 if (un->un_throttle < un->un_min_throttle) {
16740 16736 un->un_throttle = un->un_saved_throttle;
16741 16737 }
16742 16738 } else {
16743 16739 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16744 16740 "restoring limit from 0x%x to 0x%x\n",
16745 16741 un->un_throttle, un->un_saved_throttle);
16746 16742 un->un_throttle = un->un_saved_throttle;
16747 16743 }
16748 16744
16749 16745 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16750 16746 "sd_restore_throttle: calling sd_start_cmds!\n");
16751 16747
16752 16748 sd_start_cmds(un, NULL);
16753 16749
16754 16750 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16755 16751 "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16756 16752 un, un->un_throttle);
16757 16753
16758 16754 mutex_exit(SD_MUTEX(un));
16759 16755
16760 16756 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16761 16757 }
16762 16758
16763 16759 /*
16764 16760 * Function: sdrunout
16765 16761 *
16766 16762 * Description: Callback routine for scsi_init_pkt when a resource allocation
16767 16763 * fails.
16768 16764 *
16769 16765 * Arguments: arg - a pointer to the sd_lun unit struct for the particular
16770 16766 * soft state instance.
16771 16767 *
16772 16768 * Return Code: The scsi_init_pkt routine allows for the callback function to
16773 16769 * return a 0 indicating the callback should be rescheduled or a 1
16774 16770 * indicating not to reschedule. This routine always returns 1
16775 16771 * because the driver always provides a callback function to
16776 16772 * scsi_init_pkt. This results in a callback always being scheduled
16777 16773 * (via the scsi_init_pkt callback implementation) if a resource
16778 16774 * failure occurs.
16779 16775 *
16780 16776 * Context: This callback function may not block or call routines that block
16781 16777 *
16782 16778 * Note: Using the scsi_init_pkt callback facility can result in an I/O
16783 16779 * request persisting at the head of the list which cannot be
16784 16780 * satisfied even after multiple retries. In the future the driver
16785 16781 * may implement some time of maximum runout count before failing
16786 16782 * an I/O.
16787 16783 */
16788 16784
16789 16785 static int
16790 16786 sdrunout(caddr_t arg)
16791 16787 {
16792 16788 struct sd_lun *un = (struct sd_lun *)arg;
16793 16789
16794 16790 ASSERT(un != NULL);
16795 16791 ASSERT(!mutex_owned(SD_MUTEX(un)));
16796 16792
16797 16793 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16798 16794
16799 16795 mutex_enter(SD_MUTEX(un));
16800 16796 sd_start_cmds(un, NULL);
16801 16797 mutex_exit(SD_MUTEX(un));
16802 16798 /*
16803 16799 * This callback routine always returns 1 (i.e. do not reschedule)
16804 16800 * because we always specify sdrunout as the callback handler for
16805 16801 * scsi_init_pkt inside the call to sd_start_cmds.
16806 16802 */
16807 16803 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16808 16804 return (1);
16809 16805 }
16810 16806
16811 16807
16812 16808 /*
16813 16809 * Function: sdintr
16814 16810 *
16815 16811 * Description: Completion callback routine for scsi_pkt(9S) structs
16816 16812 * sent to the HBA driver via scsi_transport(9F).
16817 16813 *
16818 16814 * Context: Interrupt context
16819 16815 */
16820 16816
16821 16817 static void
16822 16818 sdintr(struct scsi_pkt *pktp)
16823 16819 {
16824 16820 struct buf *bp;
16825 16821 struct sd_xbuf *xp;
16826 16822 struct sd_lun *un;
16827 16823 size_t actual_len;
16828 16824 sd_ssc_t *sscp;
16829 16825
16830 16826 ASSERT(pktp != NULL);
16831 16827 bp = (struct buf *)pktp->pkt_private;
16832 16828 ASSERT(bp != NULL);
16833 16829 xp = SD_GET_XBUF(bp);
16834 16830 ASSERT(xp != NULL);
16835 16831 ASSERT(xp->xb_pktp != NULL);
16836 16832 un = SD_GET_UN(bp);
16837 16833 ASSERT(un != NULL);
16838 16834 ASSERT(!mutex_owned(SD_MUTEX(un)));
16839 16835
16840 16836 #ifdef SD_FAULT_INJECTION
16841 16837
16842 16838 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16843 16839 /* SD FaultInjection */
16844 16840 sd_faultinjection(pktp);
16845 16841
16846 16842 #endif /* SD_FAULT_INJECTION */
16847 16843
16848 16844 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16849 16845 " xp:0x%p, un:0x%p\n", bp, xp, un);
16850 16846
16851 16847 mutex_enter(SD_MUTEX(un));
16852 16848
16853 16849 ASSERT(un->un_fm_private != NULL);
16854 16850 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16855 16851 ASSERT(sscp != NULL);
16856 16852
16857 16853 /* Reduce the count of the #commands currently in transport */
16858 16854 un->un_ncmds_in_transport--;
16859 16855 ASSERT(un->un_ncmds_in_transport >= 0);
16860 16856
16861 16857 /* Increment counter to indicate that the callback routine is active */
16862 16858 un->un_in_callback++;
16863 16859
16864 16860 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16865 16861
16866 16862 #ifdef SDDEBUG
16867 16863 if (bp == un->un_retry_bp) {
16868 16864 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16869 16865 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16870 16866 un, un->un_retry_bp, un->un_ncmds_in_transport);
16871 16867 }
16872 16868 #endif
16873 16869
16874 16870 /*
16875 16871 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16876 16872 * state if needed.
16877 16873 */
16878 16874 if (pktp->pkt_reason == CMD_DEV_GONE) {
16879 16875 /* Prevent multiple console messages for the same failure. */
16880 16876 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16881 16877 un->un_last_pkt_reason = CMD_DEV_GONE;
16882 16878 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16883 16879 "Command failed to complete...Device is gone\n");
16884 16880 }
16885 16881 if (un->un_mediastate != DKIO_DEV_GONE) {
16886 16882 un->un_mediastate = DKIO_DEV_GONE;
16887 16883 cv_broadcast(&un->un_state_cv);
16888 16884 }
16889 16885 /*
16890 16886 * If the command happens to be the REQUEST SENSE command,
16891 16887 * free up the rqs buf and fail the original command.
16892 16888 */
16893 16889 if (bp == un->un_rqs_bp) {
16894 16890 bp = sd_mark_rqs_idle(un, xp);
16895 16891 }
16896 16892 sd_return_failed_command(un, bp, EIO);
16897 16893 goto exit;
16898 16894 }
16899 16895
16900 16896 if (pktp->pkt_state & STATE_XARQ_DONE) {
16901 16897 SD_TRACE(SD_LOG_COMMON, un,
16902 16898 "sdintr: extra sense data received. pkt=%p\n", pktp);
16903 16899 }
16904 16900
16905 16901 /*
16906 16902 * First see if the pkt has auto-request sense data with it....
16907 16903 * Look at the packet state first so we don't take a performance
16908 16904 * hit looking at the arq enabled flag unless absolutely necessary.
16909 16905 */
16910 16906 if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16911 16907 (un->un_f_arq_enabled == TRUE)) {
16912 16908 /*
16913 16909 * The HBA did an auto request sense for this command so check
16914 16910 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16915 16911 * driver command that should not be retried.
16916 16912 */
16917 16913 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16918 16914 /*
16919 16915 * Save the relevant sense info into the xp for the
16920 16916 * original cmd.
16921 16917 */
16922 16918 struct scsi_arq_status *asp;
16923 16919 asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16924 16920 xp->xb_sense_status =
16925 16921 *((uchar_t *)(&(asp->sts_rqpkt_status)));
16926 16922 xp->xb_sense_state = asp->sts_rqpkt_state;
16927 16923 xp->xb_sense_resid = asp->sts_rqpkt_resid;
16928 16924 if (pktp->pkt_state & STATE_XARQ_DONE) {
16929 16925 actual_len = MAX_SENSE_LENGTH -
16930 16926 xp->xb_sense_resid;
16931 16927 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16932 16928 MAX_SENSE_LENGTH);
16933 16929 } else {
16934 16930 if (xp->xb_sense_resid > SENSE_LENGTH) {
16935 16931 actual_len = MAX_SENSE_LENGTH -
16936 16932 xp->xb_sense_resid;
16937 16933 } else {
16938 16934 actual_len = SENSE_LENGTH -
16939 16935 xp->xb_sense_resid;
16940 16936 }
16941 16937 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16942 16938 if ((((struct uscsi_cmd *)
16943 16939 (xp->xb_pktinfo))->uscsi_rqlen) >
16944 16940 actual_len) {
16945 16941 xp->xb_sense_resid =
16946 16942 (((struct uscsi_cmd *)
16947 16943 (xp->xb_pktinfo))->
16948 16944 uscsi_rqlen) - actual_len;
16949 16945 } else {
16950 16946 xp->xb_sense_resid = 0;
16951 16947 }
16952 16948 }
16953 16949 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16954 16950 SENSE_LENGTH);
16955 16951 }
16956 16952
16957 16953 /* fail the command */
16958 16954 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16959 16955 "sdintr: arq done and FLAG_DIAGNOSE set\n");
16960 16956 sd_return_failed_command(un, bp, EIO);
16961 16957 goto exit;
16962 16958 }
16963 16959
16964 16960 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
16965 16961 /*
16966 16962 * We want to either retry or fail this command, so free
16967 16963 * the DMA resources here. If we retry the command then
16968 16964 * the DMA resources will be reallocated in sd_start_cmds().
16969 16965 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16970 16966 * causes the *entire* transfer to start over again from the
16971 16967 * beginning of the request, even for PARTIAL chunks that
16972 16968 * have already transferred successfully.
16973 16969 */
16974 16970 if ((un->un_f_is_fibre == TRUE) &&
16975 16971 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16976 16972 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16977 16973 scsi_dmafree(pktp);
16978 16974 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16979 16975 }
16980 16976 #endif
16981 16977
16982 16978 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16983 16979 "sdintr: arq done, sd_handle_auto_request_sense\n");
16984 16980
16985 16981 sd_handle_auto_request_sense(un, bp, xp, pktp);
16986 16982 goto exit;
16987 16983 }
16988 16984
16989 16985 /* Next see if this is the REQUEST SENSE pkt for the instance */
16990 16986 if (pktp->pkt_flags & FLAG_SENSING) {
16991 16987 /* This pktp is from the unit's REQUEST_SENSE command */
16992 16988 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16993 16989 "sdintr: sd_handle_request_sense\n");
16994 16990 sd_handle_request_sense(un, bp, xp, pktp);
16995 16991 goto exit;
16996 16992 }
16997 16993
16998 16994 /*
16999 16995 * Check to see if the command successfully completed as requested;
17000 16996 * this is the most common case (and also the hot performance path).
17001 16997 *
17002 16998 * Requirements for successful completion are:
17003 16999 * pkt_reason is CMD_CMPLT and packet status is status good.
17004 17000 * In addition:
17005 17001 * - A residual of zero indicates successful completion no matter what
17006 17002 * the command is.
17007 17003 * - If the residual is not zero and the command is not a read or
17008 17004 * write, then it's still defined as successful completion. In other
17009 17005 * words, if the command is a read or write the residual must be
17010 17006 * zero for successful completion.
17011 17007 * - If the residual is not zero and the command is a read or
17012 17008 * write, and it's a USCSICMD, then it's still defined as
17013 17009 * successful completion.
17014 17010 */
17015 17011 if ((pktp->pkt_reason == CMD_CMPLT) &&
17016 17012 (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
17017 17013
17018 17014 /*
17019 17015 * Since this command is returned with a good status, we
17020 17016 * can reset the count for Sonoma failover.
17021 17017 */
17022 17018 un->un_sonoma_failure_count = 0;
17023 17019
17024 17020 /*
17025 17021 * Return all USCSI commands on good status
17026 17022 */
17027 17023 if (pktp->pkt_resid == 0) {
17028 17024 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17029 17025 "sdintr: returning command for resid == 0\n");
17030 17026 } else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17031 17027 ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17032 17028 SD_UPDATE_B_RESID(bp, pktp);
17033 17029 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17034 17030 "sdintr: returning command for resid != 0\n");
17035 17031 } else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17036 17032 SD_UPDATE_B_RESID(bp, pktp);
17037 17033 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17038 17034 "sdintr: returning uscsi command\n");
17039 17035 } else {
17040 17036 goto not_successful;
17041 17037 }
17042 17038 sd_return_command(un, bp);
17043 17039
17044 17040 /*
17045 17041 * Decrement counter to indicate that the callback routine
17046 17042 * is done.
17047 17043 */
17048 17044 un->un_in_callback--;
17049 17045 ASSERT(un->un_in_callback >= 0);
17050 17046 mutex_exit(SD_MUTEX(un));
17051 17047
17052 17048 return;
17053 17049 }
17054 17050
17055 17051 not_successful:
17056 17052
17057 17053 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
17058 17054 /*
17059 17055 * The following is based upon knowledge of the underlying transport
17060 17056 * and its use of DMA resources. This code should be removed when
17061 17057 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17062 17058 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17063 17059 * and sd_start_cmds().
17064 17060 *
17065 17061 * Free any DMA resources associated with this command if there
17066 17062 * is a chance it could be retried or enqueued for later retry.
17067 17063 * If we keep the DMA binding then mpxio cannot reissue the
17068 17064 * command on another path whenever a path failure occurs.
17069 17065 *
17070 17066 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17071 17067 * causes the *entire* transfer to start over again from the
17072 17068 * beginning of the request, even for PARTIAL chunks that
17073 17069 * have already transferred successfully.
17074 17070 *
17075 17071 * This is only done for non-uscsi commands (and also skipped for the
17076 17072 * driver's internal RQS command). Also just do this for Fibre Channel
17077 17073 * devices as these are the only ones that support mpxio.
17078 17074 */
17079 17075 if ((un->un_f_is_fibre == TRUE) &&
17080 17076 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17081 17077 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
17082 17078 scsi_dmafree(pktp);
17083 17079 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17084 17080 }
17085 17081 #endif
17086 17082
17087 17083 /*
17088 17084 * The command did not successfully complete as requested so check
17089 17085 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17090 17086 * driver command that should not be retried so just return. If
17091 17087 * FLAG_DIAGNOSE is not set the error will be processed below.
17092 17088 */
17093 17089 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17094 17090 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17095 17091 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17096 17092 /*
17097 17093 * Issue a request sense if a check condition caused the error
17098 17094 * (we handle the auto request sense case above), otherwise
17099 17095 * just fail the command.
17100 17096 */
17101 17097 if ((pktp->pkt_reason == CMD_CMPLT) &&
17102 17098 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17103 17099 sd_send_request_sense_command(un, bp, pktp);
17104 17100 } else {
17105 17101 sd_return_failed_command(un, bp, EIO);
17106 17102 }
17107 17103 goto exit;
17108 17104 }
17109 17105
17110 17106 /*
17111 17107 * The command did not successfully complete as requested so process
17112 17108 * the error, retry, and/or attempt recovery.
17113 17109 */
17114 17110 switch (pktp->pkt_reason) {
17115 17111 case CMD_CMPLT:
17116 17112 switch (SD_GET_PKT_STATUS(pktp)) {
17117 17113 case STATUS_GOOD:
17118 17114 /*
17119 17115 * The command completed successfully with a non-zero
17120 17116 * residual
17121 17117 */
17122 17118 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17123 17119 "sdintr: STATUS_GOOD \n");
17124 17120 sd_pkt_status_good(un, bp, xp, pktp);
17125 17121 break;
17126 17122
17127 17123 case STATUS_CHECK:
17128 17124 case STATUS_TERMINATED:
17129 17125 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17130 17126 "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17131 17127 sd_pkt_status_check_condition(un, bp, xp, pktp);
17132 17128 break;
17133 17129
17134 17130 case STATUS_BUSY:
17135 17131 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17136 17132 "sdintr: STATUS_BUSY\n");
17137 17133 sd_pkt_status_busy(un, bp, xp, pktp);
17138 17134 break;
17139 17135
17140 17136 case STATUS_RESERVATION_CONFLICT:
17141 17137 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17142 17138 "sdintr: STATUS_RESERVATION_CONFLICT\n");
17143 17139 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17144 17140 break;
17145 17141
17146 17142 case STATUS_QFULL:
17147 17143 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17148 17144 "sdintr: STATUS_QFULL\n");
17149 17145 sd_pkt_status_qfull(un, bp, xp, pktp);
17150 17146 break;
17151 17147
17152 17148 case STATUS_MET:
17153 17149 case STATUS_INTERMEDIATE:
17154 17150 case STATUS_SCSI2:
17155 17151 case STATUS_INTERMEDIATE_MET:
17156 17152 case STATUS_ACA_ACTIVE:
17157 17153 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17158 17154 "Unexpected SCSI status received: 0x%x\n",
17159 17155 SD_GET_PKT_STATUS(pktp));
17160 17156 /*
17161 17157 * Mark the ssc_flags when detected invalid status
17162 17158 * code for non-USCSI command.
17163 17159 */
17164 17160 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17165 17161 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17166 17162 0, "stat-code");
17167 17163 }
17168 17164 sd_return_failed_command(un, bp, EIO);
17169 17165 break;
17170 17166
17171 17167 default:
17172 17168 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17173 17169 "Invalid SCSI status received: 0x%x\n",
17174 17170 SD_GET_PKT_STATUS(pktp));
17175 17171 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17176 17172 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17177 17173 0, "stat-code");
17178 17174 }
17179 17175 sd_return_failed_command(un, bp, EIO);
17180 17176 break;
17181 17177
17182 17178 }
17183 17179 break;
17184 17180
17185 17181 case CMD_INCOMPLETE:
17186 17182 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17187 17183 "sdintr: CMD_INCOMPLETE\n");
17188 17184 sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17189 17185 break;
17190 17186 case CMD_TRAN_ERR:
17191 17187 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17192 17188 "sdintr: CMD_TRAN_ERR\n");
17193 17189 sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17194 17190 break;
17195 17191 case CMD_RESET:
17196 17192 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17197 17193 "sdintr: CMD_RESET \n");
17198 17194 sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17199 17195 break;
17200 17196 case CMD_ABORTED:
17201 17197 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17202 17198 "sdintr: CMD_ABORTED \n");
17203 17199 sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17204 17200 break;
17205 17201 case CMD_TIMEOUT:
17206 17202 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17207 17203 "sdintr: CMD_TIMEOUT\n");
17208 17204 sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17209 17205 break;
17210 17206 case CMD_UNX_BUS_FREE:
17211 17207 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17212 17208 "sdintr: CMD_UNX_BUS_FREE \n");
17213 17209 sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17214 17210 break;
17215 17211 case CMD_TAG_REJECT:
17216 17212 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17217 17213 "sdintr: CMD_TAG_REJECT\n");
17218 17214 sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17219 17215 break;
17220 17216 default:
17221 17217 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17222 17218 "sdintr: default\n");
17223 17219 /*
17224 17220 * Mark the ssc_flags for detecting invliad pkt_reason.
17225 17221 */
17226 17222 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17227 17223 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17228 17224 0, "pkt-reason");
17229 17225 }
17230 17226 sd_pkt_reason_default(un, bp, xp, pktp);
17231 17227 break;
17232 17228 }
17233 17229
17234 17230 exit:
17235 17231 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17236 17232
17237 17233 /* Decrement counter to indicate that the callback routine is done. */
17238 17234 un->un_in_callback--;
17239 17235 ASSERT(un->un_in_callback >= 0);
17240 17236
17241 17237 /*
17242 17238 * At this point, the pkt has been dispatched, ie, it is either
17243 17239 * being re-tried or has been returned to its caller and should
17244 17240 * not be referenced.
17245 17241 */
17246 17242
17247 17243 mutex_exit(SD_MUTEX(un));
17248 17244 }
17249 17245
17250 17246
17251 17247 /*
17252 17248 * Function: sd_print_incomplete_msg
17253 17249 *
17254 17250 * Description: Prints the error message for a CMD_INCOMPLETE error.
17255 17251 *
17256 17252 * Arguments: un - ptr to associated softstate for the device.
17257 17253 * bp - ptr to the buf(9S) for the command.
17258 17254 * arg - message string ptr
17259 17255 * code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17260 17256 * or SD_NO_RETRY_ISSUED.
17261 17257 *
17262 17258 * Context: May be called under interrupt context
17263 17259 */
17264 17260
17265 17261 static void
17266 17262 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17267 17263 {
17268 17264 struct scsi_pkt *pktp;
17269 17265 char *msgp;
17270 17266 char *cmdp = arg;
17271 17267
17272 17268 ASSERT(un != NULL);
17273 17269 ASSERT(mutex_owned(SD_MUTEX(un)));
17274 17270 ASSERT(bp != NULL);
17275 17271 ASSERT(arg != NULL);
17276 17272 pktp = SD_GET_PKTP(bp);
17277 17273 ASSERT(pktp != NULL);
17278 17274
17279 17275 switch (code) {
17280 17276 case SD_DELAYED_RETRY_ISSUED:
17281 17277 case SD_IMMEDIATE_RETRY_ISSUED:
17282 17278 msgp = "retrying";
17283 17279 break;
17284 17280 case SD_NO_RETRY_ISSUED:
17285 17281 default:
17286 17282 msgp = "giving up";
17287 17283 break;
17288 17284 }
17289 17285
17290 17286 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17291 17287 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17292 17288 "incomplete %s- %s\n", cmdp, msgp);
17293 17289 }
17294 17290 }
17295 17291
17296 17292
17297 17293
17298 17294 /*
17299 17295 * Function: sd_pkt_status_good
17300 17296 *
17301 17297 * Description: Processing for a STATUS_GOOD code in pkt_status.
17302 17298 *
17303 17299 * Context: May be called under interrupt context
17304 17300 */
17305 17301
17306 17302 static void
17307 17303 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17308 17304 struct sd_xbuf *xp, struct scsi_pkt *pktp)
17309 17305 {
17310 17306 char *cmdp;
17311 17307
17312 17308 ASSERT(un != NULL);
17313 17309 ASSERT(mutex_owned(SD_MUTEX(un)));
17314 17310 ASSERT(bp != NULL);
17315 17311 ASSERT(xp != NULL);
17316 17312 ASSERT(pktp != NULL);
17317 17313 ASSERT(pktp->pkt_reason == CMD_CMPLT);
17318 17314 ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17319 17315 ASSERT(pktp->pkt_resid != 0);
17320 17316
17321 17317 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17322 17318
17323 17319 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17324 17320 switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17325 17321 case SCMD_READ:
17326 17322 cmdp = "read";
17327 17323 break;
17328 17324 case SCMD_WRITE:
17329 17325 cmdp = "write";
17330 17326 break;
17331 17327 default:
17332 17328 SD_UPDATE_B_RESID(bp, pktp);
17333 17329 sd_return_command(un, bp);
17334 17330 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17335 17331 return;
17336 17332 }
17337 17333
17338 17334 /*
17339 17335 * See if we can retry the read/write, preferrably immediately.
17340 17336 * If retries are exhaused, then sd_retry_command() will update
17341 17337 * the b_resid count.
17342 17338 */
17343 17339 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17344 17340 cmdp, EIO, (clock_t)0, NULL);
17345 17341
17346 17342 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17347 17343 }
17348 17344
17349 17345
17350 17346
17351 17347
17352 17348
17353 17349 /*
17354 17350 * Function: sd_handle_request_sense
17355 17351 *
17356 17352 * Description: Processing for non-auto Request Sense command.
17357 17353 *
17358 17354 * Arguments: un - ptr to associated softstate
17359 17355 * sense_bp - ptr to buf(9S) for the RQS command
17360 17356 * sense_xp - ptr to the sd_xbuf for the RQS command
17361 17357 * sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17362 17358 *
17363 17359 * Context: May be called under interrupt context
17364 17360 */
17365 17361
17366 17362 static void
17367 17363 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17368 17364 struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17369 17365 {
17370 17366 struct buf *cmd_bp; /* buf for the original command */
17371 17367 struct sd_xbuf *cmd_xp; /* sd_xbuf for the original command */
17372 17368 struct scsi_pkt *cmd_pktp; /* pkt for the original command */
17373 17369 size_t actual_len; /* actual sense data length */
17374 17370
17375 17371 ASSERT(un != NULL);
17376 17372 ASSERT(mutex_owned(SD_MUTEX(un)));
17377 17373 ASSERT(sense_bp != NULL);
17378 17374 ASSERT(sense_xp != NULL);
17379 17375 ASSERT(sense_pktp != NULL);
17380 17376
17381 17377 /*
17382 17378 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17383 17379 * RQS command and not the original command.
17384 17380 */
17385 17381 ASSERT(sense_pktp == un->un_rqs_pktp);
17386 17382 ASSERT(sense_bp == un->un_rqs_bp);
17387 17383 ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17388 17384 (FLAG_SENSING | FLAG_HEAD));
17389 17385 ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17390 17386 FLAG_SENSING) == FLAG_SENSING);
17391 17387
17392 17388 /* These are the bp, xp, and pktp for the original command */
17393 17389 cmd_bp = sense_xp->xb_sense_bp;
17394 17390 cmd_xp = SD_GET_XBUF(cmd_bp);
17395 17391 cmd_pktp = SD_GET_PKTP(cmd_bp);
17396 17392
17397 17393 if (sense_pktp->pkt_reason != CMD_CMPLT) {
17398 17394 /*
17399 17395 * The REQUEST SENSE command failed. Release the REQUEST
17400 17396 * SENSE command for re-use, get back the bp for the original
17401 17397 * command, and attempt to re-try the original command if
17402 17398 * FLAG_DIAGNOSE is not set in the original packet.
17403 17399 */
17404 17400 SD_UPDATE_ERRSTATS(un, sd_harderrs);
17405 17401 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17406 17402 cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17407 17403 sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17408 17404 NULL, NULL, EIO, (clock_t)0, NULL);
17409 17405 return;
17410 17406 }
17411 17407 }
17412 17408
17413 17409 /*
17414 17410 * Save the relevant sense info into the xp for the original cmd.
17415 17411 *
17416 17412 * Note: if the request sense failed the state info will be zero
17417 17413 * as set in sd_mark_rqs_busy()
17418 17414 */
17419 17415 cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17420 17416 cmd_xp->xb_sense_state = sense_pktp->pkt_state;
17421 17417 actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17422 17418 if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17423 17419 (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17424 17420 SENSE_LENGTH)) {
17425 17421 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17426 17422 MAX_SENSE_LENGTH);
17427 17423 cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17428 17424 } else {
17429 17425 bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17430 17426 SENSE_LENGTH);
17431 17427 if (actual_len < SENSE_LENGTH) {
17432 17428 cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17433 17429 } else {
17434 17430 cmd_xp->xb_sense_resid = 0;
17435 17431 }
17436 17432 }
17437 17433
17438 17434 /*
17439 17435 * Free up the RQS command....
17440 17436 * NOTE:
17441 17437 * Must do this BEFORE calling sd_validate_sense_data!
17442 17438 * sd_validate_sense_data may return the original command in
17443 17439 * which case the pkt will be freed and the flags can no
17444 17440 * longer be touched.
17445 17441 * SD_MUTEX is held through this process until the command
17446 17442 * is dispatched based upon the sense data, so there are
17447 17443 * no race conditions.
17448 17444 */
17449 17445 (void) sd_mark_rqs_idle(un, sense_xp);
17450 17446
17451 17447 /*
17452 17448 * For a retryable command see if we have valid sense data, if so then
17453 17449 * turn it over to sd_decode_sense() to figure out the right course of
17454 17450 * action. Just fail a non-retryable command.
17455 17451 */
17456 17452 if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17457 17453 if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17458 17454 SD_SENSE_DATA_IS_VALID) {
17459 17455 sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17460 17456 }
17461 17457 } else {
17462 17458 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17463 17459 (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17464 17460 SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17465 17461 (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17466 17462 sd_return_failed_command(un, cmd_bp, EIO);
17467 17463 }
17468 17464 }
17469 17465
17470 17466
17471 17467
17472 17468
17473 17469 /*
17474 17470 * Function: sd_handle_auto_request_sense
17475 17471 *
17476 17472 * Description: Processing for auto-request sense information.
17477 17473 *
17478 17474 * Arguments: un - ptr to associated softstate
17479 17475 * bp - ptr to buf(9S) for the command
17480 17476 * xp - ptr to the sd_xbuf for the command
17481 17477 * pktp - ptr to the scsi_pkt(9S) for the command
17482 17478 *
17483 17479 * Context: May be called under interrupt context
17484 17480 */
17485 17481
17486 17482 static void
17487 17483 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17488 17484 struct sd_xbuf *xp, struct scsi_pkt *pktp)
17489 17485 {
17490 17486 struct scsi_arq_status *asp;
17491 17487 size_t actual_len;
17492 17488
17493 17489 ASSERT(un != NULL);
17494 17490 ASSERT(mutex_owned(SD_MUTEX(un)));
17495 17491 ASSERT(bp != NULL);
17496 17492 ASSERT(xp != NULL);
17497 17493 ASSERT(pktp != NULL);
17498 17494 ASSERT(pktp != un->un_rqs_pktp);
17499 17495 ASSERT(bp != un->un_rqs_bp);
17500 17496
17501 17497 /*
17502 17498 * For auto-request sense, we get a scsi_arq_status back from
17503 17499 * the HBA, with the sense data in the sts_sensedata member.
17504 17500 * The pkt_scbp of the packet points to this scsi_arq_status.
17505 17501 */
17506 17502 asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17507 17503
17508 17504 if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17509 17505 /*
17510 17506 * The auto REQUEST SENSE failed; see if we can re-try
17511 17507 * the original command.
17512 17508 */
17513 17509 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17514 17510 "auto request sense failed (reason=%s)\n",
17515 17511 scsi_rname(asp->sts_rqpkt_reason));
17516 17512
17517 17513 sd_reset_target(un, pktp);
17518 17514
17519 17515 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17520 17516 NULL, NULL, EIO, (clock_t)0, NULL);
17521 17517 return;
17522 17518 }
17523 17519
17524 17520 /* Save the relevant sense info into the xp for the original cmd. */
17525 17521 xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17526 17522 xp->xb_sense_state = asp->sts_rqpkt_state;
17527 17523 xp->xb_sense_resid = asp->sts_rqpkt_resid;
17528 17524 if (xp->xb_sense_state & STATE_XARQ_DONE) {
17529 17525 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17530 17526 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17531 17527 MAX_SENSE_LENGTH);
17532 17528 } else {
17533 17529 if (xp->xb_sense_resid > SENSE_LENGTH) {
17534 17530 actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17535 17531 } else {
17536 17532 actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17537 17533 }
17538 17534 if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17539 17535 if ((((struct uscsi_cmd *)
17540 17536 (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17541 17537 xp->xb_sense_resid = (((struct uscsi_cmd *)
17542 17538 (xp->xb_pktinfo))->uscsi_rqlen) -
17543 17539 actual_len;
17544 17540 } else {
17545 17541 xp->xb_sense_resid = 0;
17546 17542 }
17547 17543 }
17548 17544 bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17549 17545 }
17550 17546
17551 17547 /*
17552 17548 * See if we have valid sense data, if so then turn it over to
17553 17549 * sd_decode_sense() to figure out the right course of action.
17554 17550 */
17555 17551 if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17556 17552 SD_SENSE_DATA_IS_VALID) {
17557 17553 sd_decode_sense(un, bp, xp, pktp);
17558 17554 }
17559 17555 }
17560 17556
17561 17557
17562 17558 /*
17563 17559 * Function: sd_print_sense_failed_msg
17564 17560 *
17565 17561 * Description: Print log message when RQS has failed.
17566 17562 *
17567 17563 * Arguments: un - ptr to associated softstate
17568 17564 * bp - ptr to buf(9S) for the command
17569 17565 * arg - generic message string ptr
17570 17566 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17571 17567 * or SD_NO_RETRY_ISSUED
17572 17568 *
17573 17569 * Context: May be called from interrupt context
17574 17570 */
17575 17571
17576 17572 static void
17577 17573 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17578 17574 int code)
17579 17575 {
17580 17576 char *msgp = arg;
17581 17577
17582 17578 ASSERT(un != NULL);
17583 17579 ASSERT(mutex_owned(SD_MUTEX(un)));
17584 17580 ASSERT(bp != NULL);
17585 17581
17586 17582 if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17587 17583 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17588 17584 }
17589 17585 }
17590 17586
17591 17587
17592 17588 /*
17593 17589 * Function: sd_validate_sense_data
17594 17590 *
17595 17591 * Description: Check the given sense data for validity.
17596 17592 * If the sense data is not valid, the command will
17597 17593 * be either failed or retried!
17598 17594 *
17599 17595 * Return Code: SD_SENSE_DATA_IS_INVALID
17600 17596 * SD_SENSE_DATA_IS_VALID
17601 17597 *
17602 17598 * Context: May be called from interrupt context
17603 17599 */
17604 17600
17605 17601 static int
17606 17602 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17607 17603 size_t actual_len)
17608 17604 {
17609 17605 struct scsi_extended_sense *esp;
17610 17606 struct scsi_pkt *pktp;
17611 17607 char *msgp = NULL;
17612 17608 sd_ssc_t *sscp;
17613 17609
17614 17610 ASSERT(un != NULL);
17615 17611 ASSERT(mutex_owned(SD_MUTEX(un)));
17616 17612 ASSERT(bp != NULL);
17617 17613 ASSERT(bp != un->un_rqs_bp);
17618 17614 ASSERT(xp != NULL);
17619 17615 ASSERT(un->un_fm_private != NULL);
17620 17616
17621 17617 pktp = SD_GET_PKTP(bp);
17622 17618 ASSERT(pktp != NULL);
17623 17619
17624 17620 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17625 17621 ASSERT(sscp != NULL);
17626 17622
17627 17623 /*
17628 17624 * Check the status of the RQS command (auto or manual).
17629 17625 */
17630 17626 switch (xp->xb_sense_status & STATUS_MASK) {
17631 17627 case STATUS_GOOD:
17632 17628 break;
17633 17629
17634 17630 case STATUS_RESERVATION_CONFLICT:
17635 17631 sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17636 17632 return (SD_SENSE_DATA_IS_INVALID);
17637 17633
17638 17634 case STATUS_BUSY:
17639 17635 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17640 17636 "Busy Status on REQUEST SENSE\n");
17641 17637 sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17642 17638 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17643 17639 return (SD_SENSE_DATA_IS_INVALID);
17644 17640
17645 17641 case STATUS_QFULL:
17646 17642 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17647 17643 "QFULL Status on REQUEST SENSE\n");
17648 17644 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17649 17645 NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17650 17646 return (SD_SENSE_DATA_IS_INVALID);
17651 17647
17652 17648 case STATUS_CHECK:
17653 17649 case STATUS_TERMINATED:
17654 17650 msgp = "Check Condition on REQUEST SENSE\n";
17655 17651 goto sense_failed;
17656 17652
17657 17653 default:
17658 17654 msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17659 17655 goto sense_failed;
17660 17656 }
17661 17657
17662 17658 /*
17663 17659 * See if we got the minimum required amount of sense data.
17664 17660 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17665 17661 * or less.
17666 17662 */
17667 17663 if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17668 17664 (actual_len == 0)) {
17669 17665 msgp = "Request Sense couldn't get sense data\n";
17670 17666 goto sense_failed;
17671 17667 }
17672 17668
17673 17669 if (actual_len < SUN_MIN_SENSE_LENGTH) {
17674 17670 msgp = "Not enough sense information\n";
17675 17671 /* Mark the ssc_flags for detecting invalid sense data */
17676 17672 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17677 17673 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17678 17674 "sense-data");
17679 17675 }
17680 17676 goto sense_failed;
17681 17677 }
17682 17678
17683 17679 /*
17684 17680 * We require the extended sense data
17685 17681 */
17686 17682 esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17687 17683 if (esp->es_class != CLASS_EXTENDED_SENSE) {
17688 17684 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17689 17685 static char tmp[8];
17690 17686 static char buf[148];
17691 17687 char *p = (char *)(xp->xb_sense_data);
17692 17688 int i;
17693 17689
17694 17690 mutex_enter(&sd_sense_mutex);
17695 17691 (void) strcpy(buf, "undecodable sense information:");
17696 17692 for (i = 0; i < actual_len; i++) {
17697 17693 (void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17698 17694 (void) strcpy(&buf[strlen(buf)], tmp);
17699 17695 }
17700 17696 i = strlen(buf);
17701 17697 (void) strcpy(&buf[i], "-(assumed fatal)\n");
17702 17698
17703 17699 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17704 17700 scsi_log(SD_DEVINFO(un), sd_label,
17705 17701 CE_WARN, buf);
17706 17702 }
17707 17703 mutex_exit(&sd_sense_mutex);
17708 17704 }
17709 17705
17710 17706 /* Mark the ssc_flags for detecting invalid sense data */
17711 17707 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17712 17708 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17713 17709 "sense-data");
17714 17710 }
17715 17711
17716 17712 /* Note: Legacy behavior, fail the command with no retry */
17717 17713 sd_return_failed_command(un, bp, EIO);
17718 17714 return (SD_SENSE_DATA_IS_INVALID);
17719 17715 }
17720 17716
17721 17717 /*
17722 17718 * Check that es_code is valid (es_class concatenated with es_code
17723 17719 * make up the "response code" field. es_class will always be 7, so
17724 17720 * make sure es_code is 0, 1, 2, 3 or 0xf. es_code will indicate the
17725 17721 * format.
17726 17722 */
17727 17723 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17728 17724 (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17729 17725 (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17730 17726 (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17731 17727 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17732 17728 /* Mark the ssc_flags for detecting invalid sense data */
17733 17729 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17734 17730 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17735 17731 "sense-data");
17736 17732 }
17737 17733 goto sense_failed;
17738 17734 }
17739 17735
17740 17736 return (SD_SENSE_DATA_IS_VALID);
17741 17737
17742 17738 sense_failed:
17743 17739 /*
17744 17740 * If the request sense failed (for whatever reason), attempt
17745 17741 * to retry the original command.
17746 17742 */
17747 17743 #if defined(__i386) || defined(__amd64)
17748 17744 /*
17749 17745 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17750 17746 * sddef.h for Sparc platform, and x86 uses 1 binary
17751 17747 * for both SCSI/FC.
17752 17748 * The SD_RETRY_DELAY value need to be adjusted here
17753 17749 * when SD_RETRY_DELAY change in sddef.h
17754 17750 */
17755 17751 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17756 17752 sd_print_sense_failed_msg, msgp, EIO,
17757 17753 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17758 17754 #else
17759 17755 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17760 17756 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17761 17757 #endif
17762 17758
17763 17759 return (SD_SENSE_DATA_IS_INVALID);
17764 17760 }
17765 17761
17766 17762 /*
17767 17763 * Function: sd_decode_sense
17768 17764 *
17769 17765 * Description: Take recovery action(s) when SCSI Sense Data is received.
17770 17766 *
17771 17767 * Context: Interrupt context.
17772 17768 */
17773 17769
17774 17770 static void
17775 17771 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17776 17772 struct scsi_pkt *pktp)
17777 17773 {
17778 17774 uint8_t sense_key;
17779 17775
17780 17776 ASSERT(un != NULL);
17781 17777 ASSERT(mutex_owned(SD_MUTEX(un)));
17782 17778 ASSERT(bp != NULL);
17783 17779 ASSERT(bp != un->un_rqs_bp);
17784 17780 ASSERT(xp != NULL);
17785 17781 ASSERT(pktp != NULL);
17786 17782
17787 17783 sense_key = scsi_sense_key(xp->xb_sense_data);
17788 17784
17789 17785 switch (sense_key) {
17790 17786 case KEY_NO_SENSE:
17791 17787 sd_sense_key_no_sense(un, bp, xp, pktp);
17792 17788 break;
17793 17789 case KEY_RECOVERABLE_ERROR:
17794 17790 sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17795 17791 bp, xp, pktp);
17796 17792 break;
17797 17793 case KEY_NOT_READY:
17798 17794 sd_sense_key_not_ready(un, xp->xb_sense_data,
17799 17795 bp, xp, pktp);
17800 17796 break;
17801 17797 case KEY_MEDIUM_ERROR:
17802 17798 case KEY_HARDWARE_ERROR:
17803 17799 sd_sense_key_medium_or_hardware_error(un,
17804 17800 xp->xb_sense_data, bp, xp, pktp);
17805 17801 break;
17806 17802 case KEY_ILLEGAL_REQUEST:
17807 17803 sd_sense_key_illegal_request(un, bp, xp, pktp);
17808 17804 break;
17809 17805 case KEY_UNIT_ATTENTION:
17810 17806 sd_sense_key_unit_attention(un, xp->xb_sense_data,
17811 17807 bp, xp, pktp);
17812 17808 break;
17813 17809 case KEY_WRITE_PROTECT:
17814 17810 case KEY_VOLUME_OVERFLOW:
17815 17811 case KEY_MISCOMPARE:
17816 17812 sd_sense_key_fail_command(un, bp, xp, pktp);
17817 17813 break;
17818 17814 case KEY_BLANK_CHECK:
17819 17815 sd_sense_key_blank_check(un, bp, xp, pktp);
17820 17816 break;
17821 17817 case KEY_ABORTED_COMMAND:
17822 17818 sd_sense_key_aborted_command(un, bp, xp, pktp);
17823 17819 break;
17824 17820 case KEY_VENDOR_UNIQUE:
17825 17821 case KEY_COPY_ABORTED:
17826 17822 case KEY_EQUAL:
17827 17823 case KEY_RESERVED:
17828 17824 default:
17829 17825 sd_sense_key_default(un, xp->xb_sense_data,
17830 17826 bp, xp, pktp);
17831 17827 break;
17832 17828 }
17833 17829 }
17834 17830
17835 17831
17836 17832 /*
17837 17833 * Function: sd_dump_memory
17838 17834 *
17839 17835 * Description: Debug logging routine to print the contents of a user provided
17840 17836 * buffer. The output of the buffer is broken up into 256 byte
17841 17837 * segments due to a size constraint of the scsi_log.
17842 17838 * implementation.
17843 17839 *
17844 17840 * Arguments: un - ptr to softstate
17845 17841 * comp - component mask
17846 17842 * title - "title" string to preceed data when printed
17847 17843 * data - ptr to data block to be printed
17848 17844 * len - size of data block to be printed
17849 17845 * fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17850 17846 *
17851 17847 * Context: May be called from interrupt context
17852 17848 */
17853 17849
17854 17850 #define SD_DUMP_MEMORY_BUF_SIZE 256
17855 17851
17856 17852 static char *sd_dump_format_string[] = {
17857 17853 " 0x%02x",
17858 17854 " %c"
17859 17855 };
17860 17856
17861 17857 static void
17862 17858 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17863 17859 int len, int fmt)
17864 17860 {
17865 17861 int i, j;
17866 17862 int avail_count;
17867 17863 int start_offset;
17868 17864 int end_offset;
17869 17865 size_t entry_len;
17870 17866 char *bufp;
17871 17867 char *local_buf;
17872 17868 char *format_string;
17873 17869
17874 17870 ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17875 17871
17876 17872 /*
17877 17873 * In the debug version of the driver, this function is called from a
17878 17874 * number of places which are NOPs in the release driver.
17879 17875 * The debug driver therefore has additional methods of filtering
17880 17876 * debug output.
17881 17877 */
17882 17878 #ifdef SDDEBUG
17883 17879 /*
17884 17880 * In the debug version of the driver we can reduce the amount of debug
17885 17881 * messages by setting sd_error_level to something other than
17886 17882 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17887 17883 * sd_component_mask.
17888 17884 */
17889 17885 if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17890 17886 (sd_error_level != SCSI_ERR_ALL)) {
17891 17887 return;
17892 17888 }
17893 17889 if (((sd_component_mask & comp) == 0) ||
17894 17890 (sd_error_level != SCSI_ERR_ALL)) {
17895 17891 return;
17896 17892 }
17897 17893 #else
17898 17894 if (sd_error_level != SCSI_ERR_ALL) {
17899 17895 return;
17900 17896 }
17901 17897 #endif
17902 17898
17903 17899 local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17904 17900 bufp = local_buf;
17905 17901 /*
17906 17902 * Available length is the length of local_buf[], minus the
17907 17903 * length of the title string, minus one for the ":", minus
17908 17904 * one for the newline, minus one for the NULL terminator.
17909 17905 * This gives the #bytes available for holding the printed
17910 17906 * values from the given data buffer.
17911 17907 */
17912 17908 if (fmt == SD_LOG_HEX) {
17913 17909 format_string = sd_dump_format_string[0];
17914 17910 } else /* SD_LOG_CHAR */ {
17915 17911 format_string = sd_dump_format_string[1];
17916 17912 }
17917 17913 /*
17918 17914 * Available count is the number of elements from the given
17919 17915 * data buffer that we can fit into the available length.
17920 17916 * This is based upon the size of the format string used.
17921 17917 * Make one entry and find it's size.
17922 17918 */
17923 17919 (void) sprintf(bufp, format_string, data[0]);
17924 17920 entry_len = strlen(bufp);
17925 17921 avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17926 17922
17927 17923 j = 0;
17928 17924 while (j < len) {
17929 17925 bufp = local_buf;
17930 17926 bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17931 17927 start_offset = j;
17932 17928
17933 17929 end_offset = start_offset + avail_count;
17934 17930
17935 17931 (void) sprintf(bufp, "%s:", title);
17936 17932 bufp += strlen(bufp);
17937 17933 for (i = start_offset; ((i < end_offset) && (j < len));
17938 17934 i++, j++) {
17939 17935 (void) sprintf(bufp, format_string, data[i]);
17940 17936 bufp += entry_len;
17941 17937 }
17942 17938 (void) sprintf(bufp, "\n");
17943 17939
17944 17940 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17945 17941 }
17946 17942 kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17947 17943 }
17948 17944
17949 17945 /*
17950 17946 * Function: sd_print_sense_msg
17951 17947 *
17952 17948 * Description: Log a message based upon the given sense data.
17953 17949 *
17954 17950 * Arguments: un - ptr to associated softstate
17955 17951 * bp - ptr to buf(9S) for the command
17956 17952 * arg - ptr to associate sd_sense_info struct
17957 17953 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17958 17954 * or SD_NO_RETRY_ISSUED
17959 17955 *
17960 17956 * Context: May be called from interrupt context
17961 17957 */
17962 17958
17963 17959 static void
17964 17960 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17965 17961 {
17966 17962 struct sd_xbuf *xp;
17967 17963 struct scsi_pkt *pktp;
17968 17964 uint8_t *sensep;
17969 17965 daddr_t request_blkno;
17970 17966 diskaddr_t err_blkno;
17971 17967 int severity;
17972 17968 int pfa_flag;
17973 17969 extern struct scsi_key_strings scsi_cmds[];
17974 17970
17975 17971 ASSERT(un != NULL);
17976 17972 ASSERT(mutex_owned(SD_MUTEX(un)));
17977 17973 ASSERT(bp != NULL);
17978 17974 xp = SD_GET_XBUF(bp);
17979 17975 ASSERT(xp != NULL);
17980 17976 pktp = SD_GET_PKTP(bp);
17981 17977 ASSERT(pktp != NULL);
17982 17978 ASSERT(arg != NULL);
17983 17979
17984 17980 severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17985 17981 pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17986 17982
17987 17983 if ((code == SD_DELAYED_RETRY_ISSUED) ||
17988 17984 (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17989 17985 severity = SCSI_ERR_RETRYABLE;
17990 17986 }
17991 17987
17992 17988 /* Use absolute block number for the request block number */
17993 17989 request_blkno = xp->xb_blkno;
17994 17990
17995 17991 /*
17996 17992 * Now try to get the error block number from the sense data
17997 17993 */
17998 17994 sensep = xp->xb_sense_data;
17999 17995
18000 17996 if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
18001 17997 (uint64_t *)&err_blkno)) {
18002 17998 /*
18003 17999 * We retrieved the error block number from the information
18004 18000 * portion of the sense data.
18005 18001 *
18006 18002 * For USCSI commands we are better off using the error
18007 18003 * block no. as the requested block no. (This is the best
18008 18004 * we can estimate.)
18009 18005 */
18010 18006 if ((SD_IS_BUFIO(xp) == FALSE) &&
18011 18007 ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
18012 18008 request_blkno = err_blkno;
18013 18009 }
18014 18010 } else {
18015 18011 /*
18016 18012 * Without the es_valid bit set (for fixed format) or an
18017 18013 * information descriptor (for descriptor format) we cannot
18018 18014 * be certain of the error blkno, so just use the
18019 18015 * request_blkno.
18020 18016 */
18021 18017 err_blkno = (diskaddr_t)request_blkno;
18022 18018 }
18023 18019
18024 18020 /*
18025 18021 * The following will log the buffer contents for the release driver
18026 18022 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
18027 18023 * level is set to verbose.
18028 18024 */
18029 18025 sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
18030 18026 (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18031 18027 sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18032 18028 (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18033 18029
18034 18030 if (pfa_flag == FALSE) {
18035 18031 /* This is normally only set for USCSI */
18036 18032 if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18037 18033 return;
18038 18034 }
18039 18035
18040 18036 if ((SD_IS_BUFIO(xp) == TRUE) &&
18041 18037 (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18042 18038 (severity < sd_error_level))) {
18043 18039 return;
18044 18040 }
18045 18041 }
18046 18042 /*
18047 18043 * Check for Sonoma Failover and keep a count of how many failed I/O's
18048 18044 */
18049 18045 if ((SD_IS_LSI(un)) &&
18050 18046 (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18051 18047 (scsi_sense_asc(sensep) == 0x94) &&
18052 18048 (scsi_sense_ascq(sensep) == 0x01)) {
18053 18049 un->un_sonoma_failure_count++;
18054 18050 if (un->un_sonoma_failure_count > 1) {
18055 18051 return;
18056 18052 }
18057 18053 }
18058 18054
18059 18055 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18060 18056 ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18061 18057 (pktp->pkt_resid == 0))) {
18062 18058 scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18063 18059 request_blkno, err_blkno, scsi_cmds,
18064 18060 (struct scsi_extended_sense *)sensep,
18065 18061 un->un_additional_codes, NULL);
18066 18062 }
18067 18063 }
18068 18064
18069 18065 /*
18070 18066 * Function: sd_sense_key_no_sense
18071 18067 *
18072 18068 * Description: Recovery action when sense data was not received.
18073 18069 *
18074 18070 * Context: May be called from interrupt context
18075 18071 */
18076 18072
18077 18073 static void
18078 18074 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
18079 18075 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18080 18076 {
18081 18077 struct sd_sense_info si;
18082 18078
18083 18079 ASSERT(un != NULL);
18084 18080 ASSERT(mutex_owned(SD_MUTEX(un)));
18085 18081 ASSERT(bp != NULL);
18086 18082 ASSERT(xp != NULL);
18087 18083 ASSERT(pktp != NULL);
18088 18084
18089 18085 si.ssi_severity = SCSI_ERR_FATAL;
18090 18086 si.ssi_pfa_flag = FALSE;
18091 18087
18092 18088 SD_UPDATE_ERRSTATS(un, sd_softerrs);
18093 18089
18094 18090 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18095 18091 &si, EIO, (clock_t)0, NULL);
18096 18092 }
18097 18093
18098 18094
18099 18095 /*
18100 18096 * Function: sd_sense_key_recoverable_error
18101 18097 *
18102 18098 * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18103 18099 *
18104 18100 * Context: May be called from interrupt context
18105 18101 */
18106 18102
18107 18103 static void
18108 18104 sd_sense_key_recoverable_error(struct sd_lun *un,
18109 18105 uint8_t *sense_datap,
18110 18106 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18111 18107 {
18112 18108 struct sd_sense_info si;
18113 18109 uint8_t asc = scsi_sense_asc(sense_datap);
18114 18110
18115 18111 ASSERT(un != NULL);
18116 18112 ASSERT(mutex_owned(SD_MUTEX(un)));
18117 18113 ASSERT(bp != NULL);
18118 18114 ASSERT(xp != NULL);
18119 18115 ASSERT(pktp != NULL);
18120 18116
18121 18117 /*
18122 18118 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18123 18119 */
18124 18120 if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18125 18121 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18126 18122 si.ssi_severity = SCSI_ERR_INFO;
18127 18123 si.ssi_pfa_flag = TRUE;
18128 18124 } else {
18129 18125 SD_UPDATE_ERRSTATS(un, sd_softerrs);
18130 18126 SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18131 18127 si.ssi_severity = SCSI_ERR_RECOVERED;
18132 18128 si.ssi_pfa_flag = FALSE;
18133 18129 }
18134 18130
18135 18131 if (pktp->pkt_resid == 0) {
18136 18132 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18137 18133 sd_return_command(un, bp);
18138 18134 return;
18139 18135 }
18140 18136
18141 18137 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18142 18138 &si, EIO, (clock_t)0, NULL);
18143 18139 }
18144 18140
18145 18141
18146 18142
18147 18143
18148 18144 /*
18149 18145 * Function: sd_sense_key_not_ready
18150 18146 *
18151 18147 * Description: Recovery actions for a SCSI "Not Ready" sense key.
18152 18148 *
18153 18149 * Context: May be called from interrupt context
18154 18150 */
18155 18151
18156 18152 static void
18157 18153 sd_sense_key_not_ready(struct sd_lun *un,
18158 18154 uint8_t *sense_datap,
18159 18155 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18160 18156 {
18161 18157 struct sd_sense_info si;
18162 18158 uint8_t asc = scsi_sense_asc(sense_datap);
18163 18159 uint8_t ascq = scsi_sense_ascq(sense_datap);
18164 18160
18165 18161 ASSERT(un != NULL);
18166 18162 ASSERT(mutex_owned(SD_MUTEX(un)));
18167 18163 ASSERT(bp != NULL);
18168 18164 ASSERT(xp != NULL);
18169 18165 ASSERT(pktp != NULL);
18170 18166
18171 18167 si.ssi_severity = SCSI_ERR_FATAL;
18172 18168 si.ssi_pfa_flag = FALSE;
18173 18169
18174 18170 /*
18175 18171 * Update error stats after first NOT READY error. Disks may have
18176 18172 * been powered down and may need to be restarted. For CDROMs,
18177 18173 * report NOT READY errors only if media is present.
18178 18174 */
18179 18175 if ((ISCD(un) && (asc == 0x3A)) ||
18180 18176 (xp->xb_nr_retry_count > 0)) {
18181 18177 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18182 18178 SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18183 18179 }
18184 18180
18185 18181 /*
18186 18182 * Just fail if the "not ready" retry limit has been reached.
18187 18183 */
18188 18184 if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18189 18185 /* Special check for error message printing for removables. */
18190 18186 if (un->un_f_has_removable_media && (asc == 0x04) &&
18191 18187 (ascq >= 0x04)) {
18192 18188 si.ssi_severity = SCSI_ERR_ALL;
18193 18189 }
18194 18190 goto fail_command;
18195 18191 }
18196 18192
18197 18193 /*
18198 18194 * Check the ASC and ASCQ in the sense data as needed, to determine
18199 18195 * what to do.
18200 18196 */
18201 18197 switch (asc) {
18202 18198 case 0x04: /* LOGICAL UNIT NOT READY */
18203 18199 /*
18204 18200 * disk drives that don't spin up result in a very long delay
18205 18201 * in format without warning messages. We will log a message
18206 18202 * if the error level is set to verbose.
18207 18203 */
18208 18204 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18209 18205 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18210 18206 "logical unit not ready, resetting disk\n");
18211 18207 }
18212 18208
18213 18209 /*
18214 18210 * There are different requirements for CDROMs and disks for
18215 18211 * the number of retries. If a CD-ROM is giving this, it is
18216 18212 * probably reading TOC and is in the process of getting
18217 18213 * ready, so we should keep on trying for a long time to make
18218 18214 * sure that all types of media are taken in account (for
18219 18215 * some media the drive takes a long time to read TOC). For
18220 18216 * disks we do not want to retry this too many times as this
18221 18217 * can cause a long hang in format when the drive refuses to
18222 18218 * spin up (a very common failure).
18223 18219 */
18224 18220 switch (ascq) {
18225 18221 case 0x00: /* LUN NOT READY, CAUSE NOT REPORTABLE */
18226 18222 /*
18227 18223 * Disk drives frequently refuse to spin up which
18228 18224 * results in a very long hang in format without
18229 18225 * warning messages.
18230 18226 *
18231 18227 * Note: This code preserves the legacy behavior of
18232 18228 * comparing xb_nr_retry_count against zero for fibre
18233 18229 * channel targets instead of comparing against the
18234 18230 * un_reset_retry_count value. The reason for this
18235 18231 * discrepancy has been so utterly lost beneath the
18236 18232 * Sands of Time that even Indiana Jones could not
18237 18233 * find it.
18238 18234 */
18239 18235 if (un->un_f_is_fibre == TRUE) {
18240 18236 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18241 18237 (xp->xb_nr_retry_count > 0)) &&
18242 18238 (un->un_startstop_timeid == NULL)) {
18243 18239 scsi_log(SD_DEVINFO(un), sd_label,
18244 18240 CE_WARN, "logical unit not ready, "
18245 18241 "resetting disk\n");
18246 18242 sd_reset_target(un, pktp);
18247 18243 }
18248 18244 } else {
18249 18245 if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18250 18246 (xp->xb_nr_retry_count >
18251 18247 un->un_reset_retry_count)) &&
18252 18248 (un->un_startstop_timeid == NULL)) {
18253 18249 scsi_log(SD_DEVINFO(un), sd_label,
18254 18250 CE_WARN, "logical unit not ready, "
18255 18251 "resetting disk\n");
18256 18252 sd_reset_target(un, pktp);
18257 18253 }
18258 18254 }
18259 18255 break;
18260 18256
18261 18257 case 0x01: /* LUN IS IN PROCESS OF BECOMING READY */
18262 18258 /*
18263 18259 * If the target is in the process of becoming
18264 18260 * ready, just proceed with the retry. This can
18265 18261 * happen with CD-ROMs that take a long time to
18266 18262 * read TOC after a power cycle or reset.
18267 18263 */
18268 18264 goto do_retry;
18269 18265
18270 18266 case 0x02: /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18271 18267 break;
18272 18268
18273 18269 case 0x03: /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18274 18270 /*
18275 18271 * Retries cannot help here so just fail right away.
18276 18272 */
18277 18273 goto fail_command;
18278 18274
18279 18275 case 0x88:
18280 18276 /*
18281 18277 * Vendor-unique code for T3/T4: it indicates a
18282 18278 * path problem in a mutipathed config, but as far as
18283 18279 * the target driver is concerned it equates to a fatal
18284 18280 * error, so we should just fail the command right away
18285 18281 * (without printing anything to the console). If this
18286 18282 * is not a T3/T4, fall thru to the default recovery
18287 18283 * action.
18288 18284 * T3/T4 is FC only, don't need to check is_fibre
18289 18285 */
18290 18286 if (SD_IS_T3(un) || SD_IS_T4(un)) {
18291 18287 sd_return_failed_command(un, bp, EIO);
18292 18288 return;
18293 18289 }
18294 18290 /* FALLTHRU */
18295 18291
18296 18292 case 0x04: /* LUN NOT READY, FORMAT IN PROGRESS */
18297 18293 case 0x05: /* LUN NOT READY, REBUILD IN PROGRESS */
18298 18294 case 0x06: /* LUN NOT READY, RECALCULATION IN PROGRESS */
18299 18295 case 0x07: /* LUN NOT READY, OPERATION IN PROGRESS */
18300 18296 case 0x08: /* LUN NOT READY, LONG WRITE IN PROGRESS */
18301 18297 default: /* Possible future codes in SCSI spec? */
18302 18298 /*
18303 18299 * For removable-media devices, do not retry if
18304 18300 * ASCQ > 2 as these result mostly from USCSI commands
18305 18301 * on MMC devices issued to check status of an
18306 18302 * operation initiated in immediate mode. Also for
18307 18303 * ASCQ >= 4 do not print console messages as these
18308 18304 * mainly represent a user-initiated operation
18309 18305 * instead of a system failure.
18310 18306 */
18311 18307 if (un->un_f_has_removable_media) {
18312 18308 si.ssi_severity = SCSI_ERR_ALL;
18313 18309 goto fail_command;
18314 18310 }
18315 18311 break;
18316 18312 }
18317 18313
18318 18314 /*
18319 18315 * As part of our recovery attempt for the NOT READY
18320 18316 * condition, we issue a START STOP UNIT command. However
18321 18317 * we want to wait for a short delay before attempting this
18322 18318 * as there may still be more commands coming back from the
18323 18319 * target with the check condition. To do this we use
18324 18320 * timeout(9F) to call sd_start_stop_unit_callback() after
18325 18321 * the delay interval expires. (sd_start_stop_unit_callback()
18326 18322 * dispatches sd_start_stop_unit_task(), which will issue
18327 18323 * the actual START STOP UNIT command. The delay interval
18328 18324 * is one-half of the delay that we will use to retry the
18329 18325 * command that generated the NOT READY condition.
18330 18326 *
18331 18327 * Note that we could just dispatch sd_start_stop_unit_task()
18332 18328 * from here and allow it to sleep for the delay interval,
18333 18329 * but then we would be tying up the taskq thread
18334 18330 * uncesessarily for the duration of the delay.
18335 18331 *
18336 18332 * Do not issue the START STOP UNIT if the current command
18337 18333 * is already a START STOP UNIT.
18338 18334 */
18339 18335 if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18340 18336 break;
18341 18337 }
18342 18338
18343 18339 /*
18344 18340 * Do not schedule the timeout if one is already pending.
18345 18341 */
18346 18342 if (un->un_startstop_timeid != NULL) {
18347 18343 SD_INFO(SD_LOG_ERROR, un,
18348 18344 "sd_sense_key_not_ready: restart already issued to"
18349 18345 " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18350 18346 ddi_get_instance(SD_DEVINFO(un)));
18351 18347 break;
18352 18348 }
18353 18349
18354 18350 /*
18355 18351 * Schedule the START STOP UNIT command, then queue the command
18356 18352 * for a retry.
18357 18353 *
18358 18354 * Note: A timeout is not scheduled for this retry because we
18359 18355 * want the retry to be serial with the START_STOP_UNIT. The
18360 18356 * retry will be started when the START_STOP_UNIT is completed
18361 18357 * in sd_start_stop_unit_task.
18362 18358 */
18363 18359 un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18364 18360 un, un->un_busy_timeout / 2);
18365 18361 xp->xb_nr_retry_count++;
18366 18362 sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18367 18363 return;
18368 18364
18369 18365 case 0x05: /* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18370 18366 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18371 18367 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18372 18368 "unit does not respond to selection\n");
18373 18369 }
18374 18370 break;
18375 18371
18376 18372 case 0x3A: /* MEDIUM NOT PRESENT */
18377 18373 if (sd_error_level >= SCSI_ERR_FATAL) {
18378 18374 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18379 18375 "Caddy not inserted in drive\n");
18380 18376 }
18381 18377
18382 18378 sr_ejected(un);
18383 18379 un->un_mediastate = DKIO_EJECTED;
18384 18380 /* The state has changed, inform the media watch routines */
18385 18381 cv_broadcast(&un->un_state_cv);
18386 18382 /* Just fail if no media is present in the drive. */
18387 18383 goto fail_command;
18388 18384
18389 18385 default:
18390 18386 if (sd_error_level < SCSI_ERR_RETRYABLE) {
18391 18387 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18392 18388 "Unit not Ready. Additional sense code 0x%x\n",
18393 18389 asc);
18394 18390 }
18395 18391 break;
18396 18392 }
18397 18393
18398 18394 do_retry:
18399 18395
18400 18396 /*
18401 18397 * Retry the command, as some targets may report NOT READY for
18402 18398 * several seconds after being reset.
18403 18399 */
18404 18400 xp->xb_nr_retry_count++;
18405 18401 si.ssi_severity = SCSI_ERR_RETRYABLE;
18406 18402 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18407 18403 &si, EIO, un->un_busy_timeout, NULL);
18408 18404
18409 18405 return;
18410 18406
18411 18407 fail_command:
18412 18408 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18413 18409 sd_return_failed_command(un, bp, EIO);
18414 18410 }
18415 18411
18416 18412
18417 18413
18418 18414 /*
18419 18415 * Function: sd_sense_key_medium_or_hardware_error
18420 18416 *
18421 18417 * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18422 18418 * sense key.
18423 18419 *
18424 18420 * Context: May be called from interrupt context
18425 18421 */
18426 18422
18427 18423 static void
18428 18424 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18429 18425 uint8_t *sense_datap,
18430 18426 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18431 18427 {
18432 18428 struct sd_sense_info si;
18433 18429 uint8_t sense_key = scsi_sense_key(sense_datap);
18434 18430 uint8_t asc = scsi_sense_asc(sense_datap);
18435 18431
18436 18432 ASSERT(un != NULL);
18437 18433 ASSERT(mutex_owned(SD_MUTEX(un)));
18438 18434 ASSERT(bp != NULL);
18439 18435 ASSERT(xp != NULL);
18440 18436 ASSERT(pktp != NULL);
18441 18437
18442 18438 si.ssi_severity = SCSI_ERR_FATAL;
18443 18439 si.ssi_pfa_flag = FALSE;
18444 18440
18445 18441 if (sense_key == KEY_MEDIUM_ERROR) {
18446 18442 SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18447 18443 }
18448 18444
18449 18445 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18450 18446
18451 18447 if ((un->un_reset_retry_count != 0) &&
18452 18448 (xp->xb_retry_count == un->un_reset_retry_count)) {
18453 18449 mutex_exit(SD_MUTEX(un));
18454 18450 /* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18455 18451 if (un->un_f_allow_bus_device_reset == TRUE) {
18456 18452
18457 18453 boolean_t try_resetting_target = B_TRUE;
18458 18454
18459 18455 /*
18460 18456 * We need to be able to handle specific ASC when we are
18461 18457 * handling a KEY_HARDWARE_ERROR. In particular
18462 18458 * taking the default action of resetting the target may
18463 18459 * not be the appropriate way to attempt recovery.
18464 18460 * Resetting a target because of a single LUN failure
18465 18461 * victimizes all LUNs on that target.
18466 18462 *
18467 18463 * This is true for the LSI arrays, if an LSI
18468 18464 * array controller returns an ASC of 0x84 (LUN Dead) we
18469 18465 * should trust it.
18470 18466 */
18471 18467
18472 18468 if (sense_key == KEY_HARDWARE_ERROR) {
18473 18469 switch (asc) {
18474 18470 case 0x84:
18475 18471 if (SD_IS_LSI(un)) {
18476 18472 try_resetting_target = B_FALSE;
18477 18473 }
18478 18474 break;
18479 18475 default:
18480 18476 break;
18481 18477 }
18482 18478 }
18483 18479
18484 18480 if (try_resetting_target == B_TRUE) {
18485 18481 int reset_retval = 0;
18486 18482 if (un->un_f_lun_reset_enabled == TRUE) {
18487 18483 SD_TRACE(SD_LOG_IO_CORE, un,
18488 18484 "sd_sense_key_medium_or_hardware_"
18489 18485 "error: issuing RESET_LUN\n");
18490 18486 reset_retval =
18491 18487 scsi_reset(SD_ADDRESS(un),
18492 18488 RESET_LUN);
18493 18489 }
18494 18490 if (reset_retval == 0) {
18495 18491 SD_TRACE(SD_LOG_IO_CORE, un,
18496 18492 "sd_sense_key_medium_or_hardware_"
18497 18493 "error: issuing RESET_TARGET\n");
18498 18494 (void) scsi_reset(SD_ADDRESS(un),
18499 18495 RESET_TARGET);
18500 18496 }
18501 18497 }
18502 18498 }
18503 18499 mutex_enter(SD_MUTEX(un));
18504 18500 }
18505 18501
18506 18502 /*
18507 18503 * This really ought to be a fatal error, but we will retry anyway
18508 18504 * as some drives report this as a spurious error.
18509 18505 */
18510 18506 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18511 18507 &si, EIO, (clock_t)0, NULL);
18512 18508 }
18513 18509
18514 18510
18515 18511
18516 18512 /*
18517 18513 * Function: sd_sense_key_illegal_request
18518 18514 *
18519 18515 * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18520 18516 *
18521 18517 * Context: May be called from interrupt context
18522 18518 */
18523 18519
18524 18520 static void
18525 18521 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18526 18522 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18527 18523 {
18528 18524 struct sd_sense_info si;
18529 18525
18530 18526 ASSERT(un != NULL);
18531 18527 ASSERT(mutex_owned(SD_MUTEX(un)));
18532 18528 ASSERT(bp != NULL);
18533 18529 ASSERT(xp != NULL);
18534 18530 ASSERT(pktp != NULL);
18535 18531
18536 18532 SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18537 18533
18538 18534 si.ssi_severity = SCSI_ERR_INFO;
18539 18535 si.ssi_pfa_flag = FALSE;
18540 18536
18541 18537 /* Pointless to retry if the target thinks it's an illegal request */
18542 18538 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18543 18539 sd_return_failed_command(un, bp, EIO);
18544 18540 }
18545 18541
18546 18542
18547 18543
18548 18544
18549 18545 /*
18550 18546 * Function: sd_sense_key_unit_attention
18551 18547 *
18552 18548 * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18553 18549 *
18554 18550 * Context: May be called from interrupt context
18555 18551 */
18556 18552
18557 18553 static void
18558 18554 sd_sense_key_unit_attention(struct sd_lun *un,
18559 18555 uint8_t *sense_datap,
18560 18556 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18561 18557 {
18562 18558 /*
18563 18559 * For UNIT ATTENTION we allow retries for one minute. Devices
18564 18560 * like Sonoma can return UNIT ATTENTION close to a minute
18565 18561 * under certain conditions.
18566 18562 */
18567 18563 int retry_check_flag = SD_RETRIES_UA;
18568 18564 boolean_t kstat_updated = B_FALSE;
18569 18565 struct sd_sense_info si;
18570 18566 uint8_t asc = scsi_sense_asc(sense_datap);
18571 18567 uint8_t ascq = scsi_sense_ascq(sense_datap);
18572 18568
18573 18569 ASSERT(un != NULL);
18574 18570 ASSERT(mutex_owned(SD_MUTEX(un)));
18575 18571 ASSERT(bp != NULL);
18576 18572 ASSERT(xp != NULL);
18577 18573 ASSERT(pktp != NULL);
18578 18574
18579 18575 si.ssi_severity = SCSI_ERR_INFO;
18580 18576 si.ssi_pfa_flag = FALSE;
18581 18577
18582 18578
18583 18579 switch (asc) {
18584 18580 case 0x5D: /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18585 18581 if (sd_report_pfa != 0) {
18586 18582 SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18587 18583 si.ssi_pfa_flag = TRUE;
18588 18584 retry_check_flag = SD_RETRIES_STANDARD;
18589 18585 goto do_retry;
18590 18586 }
18591 18587
18592 18588 break;
18593 18589
18594 18590 case 0x29: /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18595 18591 if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18596 18592 un->un_resvd_status |=
18597 18593 (SD_LOST_RESERVE | SD_WANT_RESERVE);
18598 18594 }
18599 18595 #ifdef _LP64
18600 18596 if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18601 18597 if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18602 18598 un, KM_NOSLEEP) == 0) {
18603 18599 /*
18604 18600 * If we can't dispatch the task we'll just
18605 18601 * live without descriptor sense. We can
18606 18602 * try again on the next "unit attention"
18607 18603 */
18608 18604 SD_ERROR(SD_LOG_ERROR, un,
18609 18605 "sd_sense_key_unit_attention: "
18610 18606 "Could not dispatch "
18611 18607 "sd_reenable_dsense_task\n");
18612 18608 }
18613 18609 }
18614 18610 #endif /* _LP64 */
18615 18611 /* FALLTHRU */
18616 18612
18617 18613 case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18618 18614 if (!un->un_f_has_removable_media) {
18619 18615 break;
18620 18616 }
18621 18617
18622 18618 /*
18623 18619 * When we get a unit attention from a removable-media device,
18624 18620 * it may be in a state that will take a long time to recover
18625 18621 * (e.g., from a reset). Since we are executing in interrupt
18626 18622 * context here, we cannot wait around for the device to come
18627 18623 * back. So hand this command off to sd_media_change_task()
18628 18624 * for deferred processing under taskq thread context. (Note
18629 18625 * that the command still may be failed if a problem is
18630 18626 * encountered at a later time.)
18631 18627 */
18632 18628 if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18633 18629 KM_NOSLEEP) == 0) {
18634 18630 /*
18635 18631 * Cannot dispatch the request so fail the command.
18636 18632 */
18637 18633 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18638 18634 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18639 18635 si.ssi_severity = SCSI_ERR_FATAL;
18640 18636 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18641 18637 sd_return_failed_command(un, bp, EIO);
18642 18638 }
18643 18639
18644 18640 /*
18645 18641 * If failed to dispatch sd_media_change_task(), we already
18646 18642 * updated kstat. If succeed to dispatch sd_media_change_task(),
18647 18643 * we should update kstat later if it encounters an error. So,
18648 18644 * we update kstat_updated flag here.
18649 18645 */
18650 18646 kstat_updated = B_TRUE;
18651 18647
18652 18648 /*
18653 18649 * Either the command has been successfully dispatched to a
18654 18650 * task Q for retrying, or the dispatch failed. In either case
18655 18651 * do NOT retry again by calling sd_retry_command. This sets up
18656 18652 * two retries of the same command and when one completes and
18657 18653 * frees the resources the other will access freed memory,
18658 18654 * a bad thing.
18659 18655 */
18660 18656 return;
18661 18657
18662 18658 default:
18663 18659 break;
18664 18660 }
18665 18661
18666 18662 /*
18667 18663 * ASC ASCQ
18668 18664 * 2A 09 Capacity data has changed
18669 18665 * 2A 01 Mode parameters changed
18670 18666 * 3F 0E Reported luns data has changed
18671 18667 * Arrays that support logical unit expansion should report
18672 18668 * capacity changes(2Ah/09). Mode parameters changed and
18673 18669 * reported luns data has changed are the approximation.
18674 18670 */
18675 18671 if (((asc == 0x2a) && (ascq == 0x09)) ||
18676 18672 ((asc == 0x2a) && (ascq == 0x01)) ||
18677 18673 ((asc == 0x3f) && (ascq == 0x0e))) {
18678 18674 if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18679 18675 KM_NOSLEEP) == 0) {
18680 18676 SD_ERROR(SD_LOG_ERROR, un,
18681 18677 "sd_sense_key_unit_attention: "
18682 18678 "Could not dispatch sd_target_change_task\n");
18683 18679 }
18684 18680 }
18685 18681
18686 18682 /*
18687 18683 * Update kstat if we haven't done that.
18688 18684 */
18689 18685 if (!kstat_updated) {
18690 18686 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18691 18687 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18692 18688 }
18693 18689
18694 18690 do_retry:
18695 18691 sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18696 18692 EIO, SD_UA_RETRY_DELAY, NULL);
18697 18693 }
18698 18694
18699 18695
18700 18696
18701 18697 /*
18702 18698 * Function: sd_sense_key_fail_command
18703 18699 *
18704 18700 * Description: Use to fail a command when we don't like the sense key that
18705 18701 * was returned.
18706 18702 *
18707 18703 * Context: May be called from interrupt context
18708 18704 */
18709 18705
18710 18706 static void
18711 18707 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18712 18708 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18713 18709 {
18714 18710 struct sd_sense_info si;
18715 18711
18716 18712 ASSERT(un != NULL);
18717 18713 ASSERT(mutex_owned(SD_MUTEX(un)));
18718 18714 ASSERT(bp != NULL);
18719 18715 ASSERT(xp != NULL);
18720 18716 ASSERT(pktp != NULL);
18721 18717
18722 18718 si.ssi_severity = SCSI_ERR_FATAL;
18723 18719 si.ssi_pfa_flag = FALSE;
18724 18720
18725 18721 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18726 18722 sd_return_failed_command(un, bp, EIO);
18727 18723 }
18728 18724
18729 18725
18730 18726
18731 18727 /*
18732 18728 * Function: sd_sense_key_blank_check
18733 18729 *
18734 18730 * Description: Recovery actions for a SCSI "Blank Check" sense key.
18735 18731 * Has no monetary connotation.
18736 18732 *
18737 18733 * Context: May be called from interrupt context
18738 18734 */
18739 18735
18740 18736 static void
18741 18737 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18742 18738 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18743 18739 {
18744 18740 struct sd_sense_info si;
18745 18741
18746 18742 ASSERT(un != NULL);
18747 18743 ASSERT(mutex_owned(SD_MUTEX(un)));
18748 18744 ASSERT(bp != NULL);
18749 18745 ASSERT(xp != NULL);
18750 18746 ASSERT(pktp != NULL);
18751 18747
18752 18748 /*
18753 18749 * Blank check is not fatal for removable devices, therefore
18754 18750 * it does not require a console message.
18755 18751 */
18756 18752 si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18757 18753 SCSI_ERR_FATAL;
18758 18754 si.ssi_pfa_flag = FALSE;
18759 18755
18760 18756 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18761 18757 sd_return_failed_command(un, bp, EIO);
18762 18758 }
18763 18759
18764 18760
18765 18761
18766 18762
18767 18763 /*
18768 18764 * Function: sd_sense_key_aborted_command
18769 18765 *
18770 18766 * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18771 18767 *
18772 18768 * Context: May be called from interrupt context
18773 18769 */
18774 18770
18775 18771 static void
18776 18772 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18777 18773 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18778 18774 {
18779 18775 struct sd_sense_info si;
18780 18776
18781 18777 ASSERT(un != NULL);
18782 18778 ASSERT(mutex_owned(SD_MUTEX(un)));
18783 18779 ASSERT(bp != NULL);
18784 18780 ASSERT(xp != NULL);
18785 18781 ASSERT(pktp != NULL);
18786 18782
18787 18783 si.ssi_severity = SCSI_ERR_FATAL;
18788 18784 si.ssi_pfa_flag = FALSE;
18789 18785
18790 18786 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18791 18787
18792 18788 /*
18793 18789 * This really ought to be a fatal error, but we will retry anyway
18794 18790 * as some drives report this as a spurious error.
18795 18791 */
18796 18792 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18797 18793 &si, EIO, drv_usectohz(100000), NULL);
18798 18794 }
18799 18795
18800 18796
18801 18797
18802 18798 /*
18803 18799 * Function: sd_sense_key_default
18804 18800 *
18805 18801 * Description: Default recovery action for several SCSI sense keys (basically
18806 18802 * attempts a retry).
18807 18803 *
18808 18804 * Context: May be called from interrupt context
18809 18805 */
18810 18806
18811 18807 static void
18812 18808 sd_sense_key_default(struct sd_lun *un,
18813 18809 uint8_t *sense_datap,
18814 18810 struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18815 18811 {
18816 18812 struct sd_sense_info si;
18817 18813 uint8_t sense_key = scsi_sense_key(sense_datap);
18818 18814
18819 18815 ASSERT(un != NULL);
18820 18816 ASSERT(mutex_owned(SD_MUTEX(un)));
18821 18817 ASSERT(bp != NULL);
18822 18818 ASSERT(xp != NULL);
18823 18819 ASSERT(pktp != NULL);
18824 18820
18825 18821 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18826 18822
18827 18823 /*
18828 18824 * Undecoded sense key. Attempt retries and hope that will fix
18829 18825 * the problem. Otherwise, we're dead.
18830 18826 */
18831 18827 if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18832 18828 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18833 18829 "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18834 18830 }
18835 18831
18836 18832 si.ssi_severity = SCSI_ERR_FATAL;
18837 18833 si.ssi_pfa_flag = FALSE;
18838 18834
18839 18835 sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18840 18836 &si, EIO, (clock_t)0, NULL);
18841 18837 }
18842 18838
18843 18839
18844 18840
18845 18841 /*
18846 18842 * Function: sd_print_retry_msg
18847 18843 *
18848 18844 * Description: Print a message indicating the retry action being taken.
18849 18845 *
18850 18846 * Arguments: un - ptr to associated softstate
18851 18847 * bp - ptr to buf(9S) for the command
18852 18848 * arg - not used.
18853 18849 * flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18854 18850 * or SD_NO_RETRY_ISSUED
18855 18851 *
18856 18852 * Context: May be called from interrupt context
18857 18853 */
18858 18854 /* ARGSUSED */
18859 18855 static void
18860 18856 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18861 18857 {
18862 18858 struct sd_xbuf *xp;
18863 18859 struct scsi_pkt *pktp;
18864 18860 char *reasonp;
18865 18861 char *msgp;
18866 18862
18867 18863 ASSERT(un != NULL);
18868 18864 ASSERT(mutex_owned(SD_MUTEX(un)));
18869 18865 ASSERT(bp != NULL);
18870 18866 pktp = SD_GET_PKTP(bp);
18871 18867 ASSERT(pktp != NULL);
18872 18868 xp = SD_GET_XBUF(bp);
18873 18869 ASSERT(xp != NULL);
18874 18870
18875 18871 ASSERT(!mutex_owned(&un->un_pm_mutex));
18876 18872 mutex_enter(&un->un_pm_mutex);
18877 18873 if ((un->un_state == SD_STATE_SUSPENDED) ||
18878 18874 (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18879 18875 (pktp->pkt_flags & FLAG_SILENT)) {
18880 18876 mutex_exit(&un->un_pm_mutex);
18881 18877 goto update_pkt_reason;
18882 18878 }
18883 18879 mutex_exit(&un->un_pm_mutex);
18884 18880
18885 18881 /*
18886 18882 * Suppress messages if they are all the same pkt_reason; with
18887 18883 * TQ, many (up to 256) are returned with the same pkt_reason.
18888 18884 * If we are in panic, then suppress the retry messages.
18889 18885 */
18890 18886 switch (flag) {
18891 18887 case SD_NO_RETRY_ISSUED:
18892 18888 msgp = "giving up";
18893 18889 break;
18894 18890 case SD_IMMEDIATE_RETRY_ISSUED:
18895 18891 case SD_DELAYED_RETRY_ISSUED:
18896 18892 if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18897 18893 ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18898 18894 (sd_error_level != SCSI_ERR_ALL))) {
18899 18895 return;
18900 18896 }
18901 18897 msgp = "retrying command";
18902 18898 break;
18903 18899 default:
18904 18900 goto update_pkt_reason;
18905 18901 }
18906 18902
18907 18903 reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18908 18904 scsi_rname(pktp->pkt_reason));
18909 18905
18910 18906 if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18911 18907 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18912 18908 "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18913 18909 }
18914 18910
18915 18911 update_pkt_reason:
18916 18912 /*
18917 18913 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18918 18914 * This is to prevent multiple console messages for the same failure
18919 18915 * condition. Note that un->un_last_pkt_reason is NOT restored if &
18920 18916 * when the command is retried successfully because there still may be
18921 18917 * more commands coming back with the same value of pktp->pkt_reason.
18922 18918 */
18923 18919 if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18924 18920 un->un_last_pkt_reason = pktp->pkt_reason;
18925 18921 }
18926 18922 }
18927 18923
18928 18924
18929 18925 /*
18930 18926 * Function: sd_print_cmd_incomplete_msg
18931 18927 *
18932 18928 * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18933 18929 *
18934 18930 * Arguments: un - ptr to associated softstate
18935 18931 * bp - ptr to buf(9S) for the command
18936 18932 * arg - passed to sd_print_retry_msg()
18937 18933 * code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18938 18934 * or SD_NO_RETRY_ISSUED
18939 18935 *
18940 18936 * Context: May be called from interrupt context
18941 18937 */
18942 18938
18943 18939 static void
18944 18940 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18945 18941 int code)
18946 18942 {
18947 18943 dev_info_t *dip;
18948 18944
18949 18945 ASSERT(un != NULL);
18950 18946 ASSERT(mutex_owned(SD_MUTEX(un)));
18951 18947 ASSERT(bp != NULL);
18952 18948
18953 18949 switch (code) {
18954 18950 case SD_NO_RETRY_ISSUED:
18955 18951 /* Command was failed. Someone turned off this target? */
18956 18952 if (un->un_state != SD_STATE_OFFLINE) {
18957 18953 /*
18958 18954 * Suppress message if we are detaching and
18959 18955 * device has been disconnected
18960 18956 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18961 18957 * private interface and not part of the DDI
18962 18958 */
18963 18959 dip = un->un_sd->sd_dev;
18964 18960 if (!(DEVI_IS_DETACHING(dip) &&
18965 18961 DEVI_IS_DEVICE_REMOVED(dip))) {
18966 18962 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18967 18963 "disk not responding to selection\n");
18968 18964 }
18969 18965 New_state(un, SD_STATE_OFFLINE);
18970 18966 }
18971 18967 break;
18972 18968
18973 18969 case SD_DELAYED_RETRY_ISSUED:
18974 18970 case SD_IMMEDIATE_RETRY_ISSUED:
18975 18971 default:
18976 18972 /* Command was successfully queued for retry */
18977 18973 sd_print_retry_msg(un, bp, arg, code);
18978 18974 break;
18979 18975 }
18980 18976 }
18981 18977
18982 18978
18983 18979 /*
18984 18980 * Function: sd_pkt_reason_cmd_incomplete
18985 18981 *
18986 18982 * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18987 18983 *
18988 18984 * Context: May be called from interrupt context
18989 18985 */
18990 18986
18991 18987 static void
18992 18988 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18993 18989 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18994 18990 {
18995 18991 int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18996 18992
18997 18993 ASSERT(un != NULL);
18998 18994 ASSERT(mutex_owned(SD_MUTEX(un)));
18999 18995 ASSERT(bp != NULL);
19000 18996 ASSERT(xp != NULL);
19001 18997 ASSERT(pktp != NULL);
19002 18998
19003 18999 /* Do not do a reset if selection did not complete */
19004 19000 /* Note: Should this not just check the bit? */
19005 19001 if (pktp->pkt_state != STATE_GOT_BUS) {
19006 19002 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19007 19003 sd_reset_target(un, pktp);
19008 19004 }
19009 19005
19010 19006 /*
19011 19007 * If the target was not successfully selected, then set
19012 19008 * SD_RETRIES_FAILFAST to indicate that we lost communication
19013 19009 * with the target, and further retries and/or commands are
19014 19010 * likely to take a long time.
19015 19011 */
19016 19012 if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
19017 19013 flag |= SD_RETRIES_FAILFAST;
19018 19014 }
19019 19015
19020 19016 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19021 19017
19022 19018 sd_retry_command(un, bp, flag,
19023 19019 sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19024 19020 }
19025 19021
19026 19022
19027 19023
19028 19024 /*
19029 19025 * Function: sd_pkt_reason_cmd_tran_err
19030 19026 *
19031 19027 * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19032 19028 *
19033 19029 * Context: May be called from interrupt context
19034 19030 */
19035 19031
19036 19032 static void
19037 19033 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19038 19034 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19039 19035 {
19040 19036 ASSERT(un != NULL);
19041 19037 ASSERT(mutex_owned(SD_MUTEX(un)));
19042 19038 ASSERT(bp != NULL);
19043 19039 ASSERT(xp != NULL);
19044 19040 ASSERT(pktp != NULL);
19045 19041
19046 19042 /*
19047 19043 * Do not reset if we got a parity error, or if
19048 19044 * selection did not complete.
19049 19045 */
19050 19046 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19051 19047 /* Note: Should this not just check the bit for pkt_state? */
19052 19048 if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19053 19049 (pktp->pkt_state != STATE_GOT_BUS)) {
19054 19050 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19055 19051 sd_reset_target(un, pktp);
19056 19052 }
19057 19053
19058 19054 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19059 19055
19060 19056 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19061 19057 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19062 19058 }
19063 19059
19064 19060
19065 19061
19066 19062 /*
19067 19063 * Function: sd_pkt_reason_cmd_reset
19068 19064 *
19069 19065 * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19070 19066 *
19071 19067 * Context: May be called from interrupt context
19072 19068 */
19073 19069
19074 19070 static void
19075 19071 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
19076 19072 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19077 19073 {
19078 19074 ASSERT(un != NULL);
19079 19075 ASSERT(mutex_owned(SD_MUTEX(un)));
19080 19076 ASSERT(bp != NULL);
19081 19077 ASSERT(xp != NULL);
19082 19078 ASSERT(pktp != NULL);
19083 19079
19084 19080 /* The target may still be running the command, so try to reset. */
19085 19081 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19086 19082 sd_reset_target(un, pktp);
19087 19083
19088 19084 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19089 19085
19090 19086 /*
19091 19087 * If pkt_reason is CMD_RESET chances are that this pkt got
19092 19088 * reset because another target on this bus caused it. The target
19093 19089 * that caused it should get CMD_TIMEOUT with pkt_statistics
19094 19090 * of STAT_TIMEOUT/STAT_DEV_RESET.
19095 19091 */
19096 19092
19097 19093 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19098 19094 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19099 19095 }
19100 19096
19101 19097
19102 19098
19103 19099
19104 19100 /*
19105 19101 * Function: sd_pkt_reason_cmd_aborted
19106 19102 *
19107 19103 * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19108 19104 *
19109 19105 * Context: May be called from interrupt context
19110 19106 */
19111 19107
19112 19108 static void
19113 19109 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
19114 19110 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19115 19111 {
19116 19112 ASSERT(un != NULL);
19117 19113 ASSERT(mutex_owned(SD_MUTEX(un)));
19118 19114 ASSERT(bp != NULL);
19119 19115 ASSERT(xp != NULL);
19120 19116 ASSERT(pktp != NULL);
19121 19117
19122 19118 /* The target may still be running the command, so try to reset. */
19123 19119 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19124 19120 sd_reset_target(un, pktp);
19125 19121
19126 19122 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19127 19123
19128 19124 /*
19129 19125 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19130 19126 * aborted because another target on this bus caused it. The target
19131 19127 * that caused it should get CMD_TIMEOUT with pkt_statistics
19132 19128 * of STAT_TIMEOUT/STAT_DEV_RESET.
19133 19129 */
19134 19130
19135 19131 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19136 19132 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19137 19133 }
19138 19134
19139 19135
19140 19136
19141 19137 /*
19142 19138 * Function: sd_pkt_reason_cmd_timeout
19143 19139 *
19144 19140 * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19145 19141 *
19146 19142 * Context: May be called from interrupt context
19147 19143 */
19148 19144
19149 19145 static void
19150 19146 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19151 19147 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19152 19148 {
19153 19149 ASSERT(un != NULL);
19154 19150 ASSERT(mutex_owned(SD_MUTEX(un)));
19155 19151 ASSERT(bp != NULL);
19156 19152 ASSERT(xp != NULL);
19157 19153 ASSERT(pktp != NULL);
19158 19154
19159 19155
19160 19156 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19161 19157 sd_reset_target(un, pktp);
19162 19158
19163 19159 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19164 19160
19165 19161 /*
19166 19162 * A command timeout indicates that we could not establish
19167 19163 * communication with the target, so set SD_RETRIES_FAILFAST
19168 19164 * as further retries/commands are likely to take a long time.
19169 19165 */
19170 19166 sd_retry_command(un, bp,
19171 19167 (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19172 19168 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19173 19169 }
19174 19170
19175 19171
19176 19172
19177 19173 /*
19178 19174 * Function: sd_pkt_reason_cmd_unx_bus_free
19179 19175 *
19180 19176 * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19181 19177 *
19182 19178 * Context: May be called from interrupt context
19183 19179 */
19184 19180
19185 19181 static void
19186 19182 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19187 19183 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19188 19184 {
19189 19185 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19190 19186
19191 19187 ASSERT(un != NULL);
19192 19188 ASSERT(mutex_owned(SD_MUTEX(un)));
19193 19189 ASSERT(bp != NULL);
19194 19190 ASSERT(xp != NULL);
19195 19191 ASSERT(pktp != NULL);
19196 19192
19197 19193 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19198 19194 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19199 19195
19200 19196 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19201 19197 sd_print_retry_msg : NULL;
19202 19198
19203 19199 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19204 19200 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19205 19201 }
19206 19202
19207 19203
19208 19204 /*
19209 19205 * Function: sd_pkt_reason_cmd_tag_reject
19210 19206 *
19211 19207 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19212 19208 *
19213 19209 * Context: May be called from interrupt context
19214 19210 */
19215 19211
19216 19212 static void
19217 19213 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19218 19214 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19219 19215 {
19220 19216 ASSERT(un != NULL);
19221 19217 ASSERT(mutex_owned(SD_MUTEX(un)));
19222 19218 ASSERT(bp != NULL);
19223 19219 ASSERT(xp != NULL);
19224 19220 ASSERT(pktp != NULL);
19225 19221
19226 19222 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19227 19223 pktp->pkt_flags = 0;
19228 19224 un->un_tagflags = 0;
19229 19225 if (un->un_f_opt_queueing == TRUE) {
19230 19226 un->un_throttle = min(un->un_throttle, 3);
19231 19227 } else {
19232 19228 un->un_throttle = 1;
19233 19229 }
19234 19230 mutex_exit(SD_MUTEX(un));
19235 19231 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19236 19232 mutex_enter(SD_MUTEX(un));
19237 19233
19238 19234 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19239 19235
19240 19236 /* Legacy behavior not to check retry counts here. */
19241 19237 sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19242 19238 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19243 19239 }
19244 19240
19245 19241
19246 19242 /*
19247 19243 * Function: sd_pkt_reason_default
19248 19244 *
19249 19245 * Description: Default recovery actions for SCSA pkt_reason values that
19250 19246 * do not have more explicit recovery actions.
19251 19247 *
19252 19248 * Context: May be called from interrupt context
19253 19249 */
19254 19250
19255 19251 static void
19256 19252 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19257 19253 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19258 19254 {
19259 19255 ASSERT(un != NULL);
19260 19256 ASSERT(mutex_owned(SD_MUTEX(un)));
19261 19257 ASSERT(bp != NULL);
19262 19258 ASSERT(xp != NULL);
19263 19259 ASSERT(pktp != NULL);
19264 19260
19265 19261 SD_UPDATE_ERRSTATS(un, sd_transerrs);
19266 19262 sd_reset_target(un, pktp);
19267 19263
19268 19264 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19269 19265
19270 19266 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19271 19267 sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19272 19268 }
19273 19269
19274 19270
19275 19271
19276 19272 /*
19277 19273 * Function: sd_pkt_status_check_condition
19278 19274 *
19279 19275 * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19280 19276 *
19281 19277 * Context: May be called from interrupt context
19282 19278 */
19283 19279
19284 19280 static void
19285 19281 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19286 19282 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19287 19283 {
19288 19284 ASSERT(un != NULL);
19289 19285 ASSERT(mutex_owned(SD_MUTEX(un)));
19290 19286 ASSERT(bp != NULL);
19291 19287 ASSERT(xp != NULL);
19292 19288 ASSERT(pktp != NULL);
19293 19289
19294 19290 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19295 19291 "entry: buf:0x%p xp:0x%p\n", bp, xp);
19296 19292
19297 19293 /*
19298 19294 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19299 19295 * command will be retried after the request sense). Otherwise, retry
19300 19296 * the command. Note: we are issuing the request sense even though the
19301 19297 * retry limit may have been reached for the failed command.
19302 19298 */
19303 19299 if (un->un_f_arq_enabled == FALSE) {
19304 19300 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19305 19301 "no ARQ, sending request sense command\n");
19306 19302 sd_send_request_sense_command(un, bp, pktp);
19307 19303 } else {
19308 19304 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19309 19305 "ARQ,retrying request sense command\n");
19310 19306 #if defined(__i386) || defined(__amd64)
19311 19307 /*
19312 19308 * The SD_RETRY_DELAY value need to be adjusted here
19313 19309 * when SD_RETRY_DELAY change in sddef.h
19314 19310 */
19315 19311 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19316 19312 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19317 19313 NULL);
19318 19314 #else
19319 19315 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19320 19316 EIO, SD_RETRY_DELAY, NULL);
19321 19317 #endif
19322 19318 }
19323 19319
19324 19320 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19325 19321 }
19326 19322
19327 19323
19328 19324 /*
19329 19325 * Function: sd_pkt_status_busy
19330 19326 *
19331 19327 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19332 19328 *
19333 19329 * Context: May be called from interrupt context
19334 19330 */
19335 19331
19336 19332 static void
19337 19333 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19338 19334 struct scsi_pkt *pktp)
19339 19335 {
19340 19336 ASSERT(un != NULL);
19341 19337 ASSERT(mutex_owned(SD_MUTEX(un)));
19342 19338 ASSERT(bp != NULL);
19343 19339 ASSERT(xp != NULL);
19344 19340 ASSERT(pktp != NULL);
19345 19341
19346 19342 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19347 19343 "sd_pkt_status_busy: entry\n");
19348 19344
19349 19345 /* If retries are exhausted, just fail the command. */
19350 19346 if (xp->xb_retry_count >= un->un_busy_retry_count) {
19351 19347 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19352 19348 "device busy too long\n");
19353 19349 sd_return_failed_command(un, bp, EIO);
19354 19350 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19355 19351 "sd_pkt_status_busy: exit\n");
19356 19352 return;
19357 19353 }
19358 19354 xp->xb_retry_count++;
19359 19355
19360 19356 /*
19361 19357 * Try to reset the target. However, we do not want to perform
19362 19358 * more than one reset if the device continues to fail. The reset
19363 19359 * will be performed when the retry count reaches the reset
19364 19360 * threshold. This threshold should be set such that at least
19365 19361 * one retry is issued before the reset is performed.
19366 19362 */
19367 19363 if (xp->xb_retry_count ==
19368 19364 ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19369 19365 int rval = 0;
19370 19366 mutex_exit(SD_MUTEX(un));
19371 19367 if (un->un_f_allow_bus_device_reset == TRUE) {
19372 19368 /*
19373 19369 * First try to reset the LUN; if we cannot then
19374 19370 * try to reset the target.
19375 19371 */
19376 19372 if (un->un_f_lun_reset_enabled == TRUE) {
19377 19373 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19378 19374 "sd_pkt_status_busy: RESET_LUN\n");
19379 19375 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19380 19376 }
19381 19377 if (rval == 0) {
19382 19378 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19383 19379 "sd_pkt_status_busy: RESET_TARGET\n");
19384 19380 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19385 19381 }
19386 19382 }
19387 19383 if (rval == 0) {
19388 19384 /*
19389 19385 * If the RESET_LUN and/or RESET_TARGET failed,
19390 19386 * try RESET_ALL
19391 19387 */
19392 19388 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19393 19389 "sd_pkt_status_busy: RESET_ALL\n");
19394 19390 rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19395 19391 }
19396 19392 mutex_enter(SD_MUTEX(un));
19397 19393 if (rval == 0) {
19398 19394 /*
19399 19395 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19400 19396 * At this point we give up & fail the command.
19401 19397 */
19402 19398 sd_return_failed_command(un, bp, EIO);
19403 19399 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19404 19400 "sd_pkt_status_busy: exit (failed cmd)\n");
19405 19401 return;
19406 19402 }
19407 19403 }
19408 19404
19409 19405 /*
19410 19406 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19411 19407 * we have already checked the retry counts above.
19412 19408 */
19413 19409 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19414 19410 EIO, un->un_busy_timeout, NULL);
19415 19411
19416 19412 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19417 19413 "sd_pkt_status_busy: exit\n");
19418 19414 }
19419 19415
19420 19416
19421 19417 /*
19422 19418 * Function: sd_pkt_status_reservation_conflict
19423 19419 *
19424 19420 * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19425 19421 * command status.
19426 19422 *
19427 19423 * Context: May be called from interrupt context
19428 19424 */
19429 19425
19430 19426 static void
19431 19427 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19432 19428 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19433 19429 {
19434 19430 ASSERT(un != NULL);
19435 19431 ASSERT(mutex_owned(SD_MUTEX(un)));
19436 19432 ASSERT(bp != NULL);
19437 19433 ASSERT(xp != NULL);
19438 19434 ASSERT(pktp != NULL);
19439 19435
19440 19436 /*
19441 19437 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19442 19438 * conflict could be due to various reasons like incorrect keys, not
19443 19439 * registered or not reserved etc. So, we return EACCES to the caller.
19444 19440 */
19445 19441 if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19446 19442 int cmd = SD_GET_PKT_OPCODE(pktp);
19447 19443 if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19448 19444 (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19449 19445 sd_return_failed_command(un, bp, EACCES);
19450 19446 return;
19451 19447 }
19452 19448 }
19453 19449
19454 19450 un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19455 19451
19456 19452 if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19457 19453 if (sd_failfast_enable != 0) {
19458 19454 /* By definition, we must panic here.... */
19459 19455 sd_panic_for_res_conflict(un);
19460 19456 /*NOTREACHED*/
19461 19457 }
19462 19458 SD_ERROR(SD_LOG_IO, un,
19463 19459 "sd_handle_resv_conflict: Disk Reserved\n");
19464 19460 sd_return_failed_command(un, bp, EACCES);
19465 19461 return;
19466 19462 }
19467 19463
19468 19464 /*
19469 19465 * 1147670: retry only if sd_retry_on_reservation_conflict
19470 19466 * property is set (default is 1). Retries will not succeed
19471 19467 * on a disk reserved by another initiator. HA systems
19472 19468 * may reset this via sd.conf to avoid these retries.
19473 19469 *
19474 19470 * Note: The legacy return code for this failure is EIO, however EACCES
19475 19471 * seems more appropriate for a reservation conflict.
19476 19472 */
19477 19473 if (sd_retry_on_reservation_conflict == 0) {
19478 19474 SD_ERROR(SD_LOG_IO, un,
19479 19475 "sd_handle_resv_conflict: Device Reserved\n");
19480 19476 sd_return_failed_command(un, bp, EIO);
19481 19477 return;
19482 19478 }
19483 19479
19484 19480 /*
19485 19481 * Retry the command if we can.
19486 19482 *
19487 19483 * Note: The legacy return code for this failure is EIO, however EACCES
19488 19484 * seems more appropriate for a reservation conflict.
19489 19485 */
19490 19486 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19491 19487 (clock_t)2, NULL);
19492 19488 }
19493 19489
19494 19490
19495 19491
19496 19492 /*
19497 19493 * Function: sd_pkt_status_qfull
19498 19494 *
19499 19495 * Description: Handle a QUEUE FULL condition from the target. This can
19500 19496 * occur if the HBA does not handle the queue full condition.
19501 19497 * (Basically this means third-party HBAs as Sun HBAs will
19502 19498 * handle the queue full condition.) Note that if there are
19503 19499 * some commands already in the transport, then the queue full
19504 19500 * has occurred because the queue for this nexus is actually
19505 19501 * full. If there are no commands in the transport, then the
19506 19502 * queue full is resulting from some other initiator or lun
19507 19503 * consuming all the resources at the target.
19508 19504 *
19509 19505 * Context: May be called from interrupt context
19510 19506 */
19511 19507
19512 19508 static void
19513 19509 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19514 19510 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19515 19511 {
19516 19512 ASSERT(un != NULL);
19517 19513 ASSERT(mutex_owned(SD_MUTEX(un)));
19518 19514 ASSERT(bp != NULL);
19519 19515 ASSERT(xp != NULL);
19520 19516 ASSERT(pktp != NULL);
19521 19517
19522 19518 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19523 19519 "sd_pkt_status_qfull: entry\n");
19524 19520
19525 19521 /*
19526 19522 * Just lower the QFULL throttle and retry the command. Note that
19527 19523 * we do not limit the number of retries here.
19528 19524 */
19529 19525 sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19530 19526 sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19531 19527 SD_RESTART_TIMEOUT, NULL);
19532 19528
19533 19529 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19534 19530 "sd_pkt_status_qfull: exit\n");
19535 19531 }
19536 19532
19537 19533
19538 19534 /*
19539 19535 * Function: sd_reset_target
19540 19536 *
19541 19537 * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19542 19538 * RESET_TARGET, or RESET_ALL.
19543 19539 *
19544 19540 * Context: May be called under interrupt context.
19545 19541 */
19546 19542
19547 19543 static void
19548 19544 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19549 19545 {
19550 19546 int rval = 0;
19551 19547
19552 19548 ASSERT(un != NULL);
19553 19549 ASSERT(mutex_owned(SD_MUTEX(un)));
19554 19550 ASSERT(pktp != NULL);
19555 19551
19556 19552 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19557 19553
19558 19554 /*
19559 19555 * No need to reset if the transport layer has already done so.
19560 19556 */
19561 19557 if ((pktp->pkt_statistics &
19562 19558 (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19563 19559 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19564 19560 "sd_reset_target: no reset\n");
19565 19561 return;
19566 19562 }
19567 19563
19568 19564 mutex_exit(SD_MUTEX(un));
19569 19565
19570 19566 if (un->un_f_allow_bus_device_reset == TRUE) {
19571 19567 if (un->un_f_lun_reset_enabled == TRUE) {
19572 19568 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19573 19569 "sd_reset_target: RESET_LUN\n");
19574 19570 rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19575 19571 }
19576 19572 if (rval == 0) {
19577 19573 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19578 19574 "sd_reset_target: RESET_TARGET\n");
19579 19575 rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19580 19576 }
19581 19577 }
19582 19578
19583 19579 if (rval == 0) {
19584 19580 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19585 19581 "sd_reset_target: RESET_ALL\n");
19586 19582 (void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19587 19583 }
19588 19584
19589 19585 mutex_enter(SD_MUTEX(un));
19590 19586
19591 19587 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19592 19588 }
19593 19589
19594 19590 /*
19595 19591 * Function: sd_target_change_task
19596 19592 *
19597 19593 * Description: Handle dynamic target change
19598 19594 *
19599 19595 * Context: Executes in a taskq() thread context
19600 19596 */
19601 19597 static void
19602 19598 sd_target_change_task(void *arg)
19603 19599 {
19604 19600 struct sd_lun *un = arg;
19605 19601 uint64_t capacity;
19606 19602 diskaddr_t label_cap;
19607 19603 uint_t lbasize;
19608 19604 sd_ssc_t *ssc;
19609 19605
19610 19606 ASSERT(un != NULL);
19611 19607 ASSERT(!mutex_owned(SD_MUTEX(un)));
19612 19608
19613 19609 if ((un->un_f_blockcount_is_valid == FALSE) ||
19614 19610 (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19615 19611 return;
19616 19612 }
19617 19613
19618 19614 ssc = sd_ssc_init(un);
19619 19615
19620 19616 if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19621 19617 &lbasize, SD_PATH_DIRECT) != 0) {
19622 19618 SD_ERROR(SD_LOG_ERROR, un,
19623 19619 "sd_target_change_task: fail to read capacity\n");
19624 19620 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19625 19621 goto task_exit;
19626 19622 }
19627 19623
19628 19624 mutex_enter(SD_MUTEX(un));
19629 19625 if (capacity <= un->un_blockcount) {
19630 19626 mutex_exit(SD_MUTEX(un));
19631 19627 goto task_exit;
19632 19628 }
19633 19629
19634 19630 sd_update_block_info(un, lbasize, capacity);
19635 19631 mutex_exit(SD_MUTEX(un));
19636 19632
19637 19633 /*
19638 19634 * If lun is EFI labeled and lun capacity is greater than the
19639 19635 * capacity contained in the label, log a sys event.
19640 19636 */
19641 19637 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19642 19638 (void*)SD_PATH_DIRECT) == 0) {
19643 19639 mutex_enter(SD_MUTEX(un));
19644 19640 if (un->un_f_blockcount_is_valid &&
19645 19641 un->un_blockcount > label_cap) {
19646 19642 mutex_exit(SD_MUTEX(un));
19647 19643 sd_log_lun_expansion_event(un, KM_SLEEP);
19648 19644 } else {
19649 19645 mutex_exit(SD_MUTEX(un));
19650 19646 }
19651 19647 }
19652 19648
19653 19649 task_exit:
19654 19650 sd_ssc_fini(ssc);
19655 19651 }
19656 19652
19657 19653
19658 19654 /*
19659 19655 * Function: sd_log_dev_status_event
19660 19656 *
19661 19657 * Description: Log EC_dev_status sysevent
19662 19658 *
19663 19659 * Context: Never called from interrupt context
19664 19660 */
19665 19661 static void
19666 19662 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19667 19663 {
19668 19664 int err;
19669 19665 char *path;
19670 19666 nvlist_t *attr_list;
19671 19667
19672 19668 /* Allocate and build sysevent attribute list */
19673 19669 err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19674 19670 if (err != 0) {
19675 19671 SD_ERROR(SD_LOG_ERROR, un,
19676 19672 "sd_log_dev_status_event: fail to allocate space\n");
19677 19673 return;
19678 19674 }
19679 19675
19680 19676 path = kmem_alloc(MAXPATHLEN, km_flag);
19681 19677 if (path == NULL) {
19682 19678 nvlist_free(attr_list);
19683 19679 SD_ERROR(SD_LOG_ERROR, un,
19684 19680 "sd_log_dev_status_event: fail to allocate space\n");
19685 19681 return;
19686 19682 }
19687 19683 /*
19688 19684 * Add path attribute to identify the lun.
19689 19685 * We are using minor node 'a' as the sysevent attribute.
19690 19686 */
19691 19687 (void) snprintf(path, MAXPATHLEN, "/devices");
19692 19688 (void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19693 19689 (void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19694 19690 ":a");
19695 19691
19696 19692 err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19697 19693 if (err != 0) {
19698 19694 nvlist_free(attr_list);
19699 19695 kmem_free(path, MAXPATHLEN);
19700 19696 SD_ERROR(SD_LOG_ERROR, un,
19701 19697 "sd_log_dev_status_event: fail to add attribute\n");
19702 19698 return;
19703 19699 }
19704 19700
19705 19701 /* Log dynamic lun expansion sysevent */
19706 19702 err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19707 19703 esc, attr_list, NULL, km_flag);
19708 19704 if (err != DDI_SUCCESS) {
19709 19705 SD_ERROR(SD_LOG_ERROR, un,
19710 19706 "sd_log_dev_status_event: fail to log sysevent\n");
19711 19707 }
19712 19708
19713 19709 nvlist_free(attr_list);
19714 19710 kmem_free(path, MAXPATHLEN);
19715 19711 }
19716 19712
19717 19713
19718 19714 /*
19719 19715 * Function: sd_log_lun_expansion_event
19720 19716 *
19721 19717 * Description: Log lun expansion sys event
19722 19718 *
19723 19719 * Context: Never called from interrupt context
19724 19720 */
19725 19721 static void
19726 19722 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19727 19723 {
19728 19724 sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19729 19725 }
19730 19726
19731 19727
19732 19728 /*
19733 19729 * Function: sd_log_eject_request_event
19734 19730 *
19735 19731 * Description: Log eject request sysevent
19736 19732 *
19737 19733 * Context: Never called from interrupt context
19738 19734 */
19739 19735 static void
19740 19736 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19741 19737 {
19742 19738 sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19743 19739 }
19744 19740
19745 19741
19746 19742 /*
19747 19743 * Function: sd_media_change_task
19748 19744 *
19749 19745 * Description: Recovery action for CDROM to become available.
19750 19746 *
19751 19747 * Context: Executes in a taskq() thread context
19752 19748 */
19753 19749
19754 19750 static void
19755 19751 sd_media_change_task(void *arg)
19756 19752 {
19757 19753 struct scsi_pkt *pktp = arg;
19758 19754 struct sd_lun *un;
19759 19755 struct buf *bp;
19760 19756 struct sd_xbuf *xp;
19761 19757 int err = 0;
19762 19758 int retry_count = 0;
19763 19759 int retry_limit = SD_UNIT_ATTENTION_RETRY/10;
19764 19760 struct sd_sense_info si;
19765 19761
19766 19762 ASSERT(pktp != NULL);
19767 19763 bp = (struct buf *)pktp->pkt_private;
19768 19764 ASSERT(bp != NULL);
19769 19765 xp = SD_GET_XBUF(bp);
19770 19766 ASSERT(xp != NULL);
19771 19767 un = SD_GET_UN(bp);
19772 19768 ASSERT(un != NULL);
19773 19769 ASSERT(!mutex_owned(SD_MUTEX(un)));
19774 19770 ASSERT(un->un_f_monitor_media_state);
19775 19771
19776 19772 si.ssi_severity = SCSI_ERR_INFO;
19777 19773 si.ssi_pfa_flag = FALSE;
19778 19774
19779 19775 /*
19780 19776 * When a reset is issued on a CDROM, it takes a long time to
19781 19777 * recover. First few attempts to read capacity and other things
19782 19778 * related to handling unit attention fail (with a ASC 0x4 and
19783 19779 * ASCQ 0x1). In that case we want to do enough retries and we want
19784 19780 * to limit the retries in other cases of genuine failures like
19785 19781 * no media in drive.
19786 19782 */
19787 19783 while (retry_count++ < retry_limit) {
19788 19784 if ((err = sd_handle_mchange(un)) == 0) {
19789 19785 break;
19790 19786 }
19791 19787 if (err == EAGAIN) {
19792 19788 retry_limit = SD_UNIT_ATTENTION_RETRY;
19793 19789 }
19794 19790 /* Sleep for 0.5 sec. & try again */
19795 19791 delay(drv_usectohz(500000));
19796 19792 }
19797 19793
19798 19794 /*
19799 19795 * Dispatch (retry or fail) the original command here,
19800 19796 * along with appropriate console messages....
19801 19797 *
19802 19798 * Must grab the mutex before calling sd_retry_command,
19803 19799 * sd_print_sense_msg and sd_return_failed_command.
19804 19800 */
19805 19801 mutex_enter(SD_MUTEX(un));
19806 19802 if (err != SD_CMD_SUCCESS) {
19807 19803 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19808 19804 SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19809 19805 si.ssi_severity = SCSI_ERR_FATAL;
19810 19806 sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19811 19807 sd_return_failed_command(un, bp, EIO);
19812 19808 } else {
19813 19809 sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19814 19810 &si, EIO, (clock_t)0, NULL);
19815 19811 }
19816 19812 mutex_exit(SD_MUTEX(un));
19817 19813 }
19818 19814
19819 19815
19820 19816
19821 19817 /*
19822 19818 * Function: sd_handle_mchange
19823 19819 *
19824 19820 * Description: Perform geometry validation & other recovery when CDROM
19825 19821 * has been removed from drive.
19826 19822 *
19827 19823 * Return Code: 0 for success
19828 19824 * errno-type return code of either sd_send_scsi_DOORLOCK() or
19829 19825 * sd_send_scsi_READ_CAPACITY()
19830 19826 *
19831 19827 * Context: Executes in a taskq() thread context
19832 19828 */
19833 19829
19834 19830 static int
19835 19831 sd_handle_mchange(struct sd_lun *un)
19836 19832 {
19837 19833 uint64_t capacity;
19838 19834 uint32_t lbasize;
19839 19835 int rval;
19840 19836 sd_ssc_t *ssc;
19841 19837
19842 19838 ASSERT(!mutex_owned(SD_MUTEX(un)));
19843 19839 ASSERT(un->un_f_monitor_media_state);
19844 19840
19845 19841 ssc = sd_ssc_init(un);
19846 19842 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19847 19843 SD_PATH_DIRECT_PRIORITY);
19848 19844
19849 19845 if (rval != 0)
19850 19846 goto failed;
19851 19847
19852 19848 mutex_enter(SD_MUTEX(un));
19853 19849 sd_update_block_info(un, lbasize, capacity);
19854 19850
19855 19851 if (un->un_errstats != NULL) {
19856 19852 struct sd_errstats *stp =
19857 19853 (struct sd_errstats *)un->un_errstats->ks_data;
19858 19854 stp->sd_capacity.value.ui64 = (uint64_t)
19859 19855 ((uint64_t)un->un_blockcount *
19860 19856 (uint64_t)un->un_tgt_blocksize);
19861 19857 }
19862 19858
19863 19859 /*
19864 19860 * Check if the media in the device is writable or not
19865 19861 */
19866 19862 if (ISCD(un)) {
19867 19863 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19868 19864 }
19869 19865
19870 19866 /*
19871 19867 * Note: Maybe let the strategy/partitioning chain worry about getting
19872 19868 * valid geometry.
19873 19869 */
19874 19870 mutex_exit(SD_MUTEX(un));
19875 19871 cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19876 19872
19877 19873
19878 19874 if (cmlb_validate(un->un_cmlbhandle, 0,
19879 19875 (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19880 19876 sd_ssc_fini(ssc);
19881 19877 return (EIO);
19882 19878 } else {
19883 19879 if (un->un_f_pkstats_enabled) {
19884 19880 sd_set_pstats(un);
19885 19881 SD_TRACE(SD_LOG_IO_PARTITION, un,
19886 19882 "sd_handle_mchange: un:0x%p pstats created and "
19887 19883 "set\n", un);
19888 19884 }
19889 19885 }
19890 19886
19891 19887 /*
19892 19888 * Try to lock the door
19893 19889 */
19894 19890 rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19895 19891 SD_PATH_DIRECT_PRIORITY);
19896 19892 failed:
19897 19893 if (rval != 0)
19898 19894 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19899 19895 sd_ssc_fini(ssc);
19900 19896 return (rval);
19901 19897 }
19902 19898
19903 19899
19904 19900 /*
19905 19901 * Function: sd_send_scsi_DOORLOCK
19906 19902 *
19907 19903 * Description: Issue the scsi DOOR LOCK command
19908 19904 *
19909 19905 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
19910 19906 * structure for this target.
19911 19907 * flag - SD_REMOVAL_ALLOW
19912 19908 * SD_REMOVAL_PREVENT
19913 19909 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19914 19910 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19915 19911 * to use the USCSI "direct" chain and bypass the normal
19916 19912 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19917 19913 * command is issued as part of an error recovery action.
19918 19914 *
19919 19915 * Return Code: 0 - Success
19920 19916 * errno return code from sd_ssc_send()
19921 19917 *
19922 19918 * Context: Can sleep.
19923 19919 */
19924 19920
19925 19921 static int
19926 19922 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19927 19923 {
19928 19924 struct scsi_extended_sense sense_buf;
19929 19925 union scsi_cdb cdb;
19930 19926 struct uscsi_cmd ucmd_buf;
19931 19927 int status;
19932 19928 struct sd_lun *un;
19933 19929
19934 19930 ASSERT(ssc != NULL);
19935 19931 un = ssc->ssc_un;
19936 19932 ASSERT(un != NULL);
19937 19933 ASSERT(!mutex_owned(SD_MUTEX(un)));
19938 19934
19939 19935 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19940 19936
19941 19937 /* already determined doorlock is not supported, fake success */
19942 19938 if (un->un_f_doorlock_supported == FALSE) {
19943 19939 return (0);
19944 19940 }
19945 19941
19946 19942 /*
19947 19943 * If we are ejecting and see an SD_REMOVAL_PREVENT
19948 19944 * ignore the command so we can complete the eject
19949 19945 * operation.
19950 19946 */
19951 19947 if (flag == SD_REMOVAL_PREVENT) {
19952 19948 mutex_enter(SD_MUTEX(un));
19953 19949 if (un->un_f_ejecting == TRUE) {
19954 19950 mutex_exit(SD_MUTEX(un));
19955 19951 return (EAGAIN);
19956 19952 }
19957 19953 mutex_exit(SD_MUTEX(un));
19958 19954 }
19959 19955
19960 19956 bzero(&cdb, sizeof (cdb));
19961 19957 bzero(&ucmd_buf, sizeof (ucmd_buf));
19962 19958
19963 19959 cdb.scc_cmd = SCMD_DOORLOCK;
19964 19960 cdb.cdb_opaque[4] = (uchar_t)flag;
19965 19961
19966 19962 ucmd_buf.uscsi_cdb = (char *)&cdb;
19967 19963 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
19968 19964 ucmd_buf.uscsi_bufaddr = NULL;
19969 19965 ucmd_buf.uscsi_buflen = 0;
19970 19966 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
19971 19967 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
19972 19968 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
19973 19969 ucmd_buf.uscsi_timeout = 15;
19974 19970
19975 19971 SD_TRACE(SD_LOG_IO, un,
19976 19972 "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19977 19973
19978 19974 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19979 19975 UIO_SYSSPACE, path_flag);
19980 19976
19981 19977 if (status == 0)
19982 19978 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19983 19979
19984 19980 if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19985 19981 (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19986 19982 (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19987 19983 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19988 19984
19989 19985 /* fake success and skip subsequent doorlock commands */
19990 19986 un->un_f_doorlock_supported = FALSE;
19991 19987 return (0);
19992 19988 }
19993 19989
19994 19990 return (status);
19995 19991 }
19996 19992
19997 19993 /*
19998 19994 * Function: sd_send_scsi_READ_CAPACITY
19999 19995 *
20000 19996 * Description: This routine uses the scsi READ CAPACITY command to determine
20001 19997 * the device capacity in number of blocks and the device native
20002 19998 * block size. If this function returns a failure, then the
20003 19999 * values in *capp and *lbap are undefined. If the capacity
20004 20000 * returned is 0xffffffff then the lun is too large for a
20005 20001 * normal READ CAPACITY command and the results of a
20006 20002 * READ CAPACITY 16 will be used instead.
20007 20003 *
20008 20004 * Arguments: ssc - ssc contains ptr to soft state struct for the target
20009 20005 * capp - ptr to unsigned 64-bit variable to receive the
20010 20006 * capacity value from the command.
20011 20007 * lbap - ptr to unsigned 32-bit varaible to receive the
20012 20008 * block size value from the command
20013 20009 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20014 20010 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20015 20011 * to use the USCSI "direct" chain and bypass the normal
20016 20012 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20017 20013 * command is issued as part of an error recovery action.
20018 20014 *
20019 20015 * Return Code: 0 - Success
20020 20016 * EIO - IO error
20021 20017 * EACCES - Reservation conflict detected
20022 20018 * EAGAIN - Device is becoming ready
20023 20019 * errno return code from sd_ssc_send()
20024 20020 *
20025 20021 * Context: Can sleep. Blocks until command completes.
20026 20022 */
20027 20023
20028 20024 #define SD_CAPACITY_SIZE sizeof (struct scsi_capacity)
20029 20025
20030 20026 static int
20031 20027 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20032 20028 int path_flag)
20033 20029 {
20034 20030 struct scsi_extended_sense sense_buf;
20035 20031 struct uscsi_cmd ucmd_buf;
20036 20032 union scsi_cdb cdb;
20037 20033 uint32_t *capacity_buf;
20038 20034 uint64_t capacity;
20039 20035 uint32_t lbasize;
20040 20036 uint32_t pbsize;
20041 20037 int status;
20042 20038 struct sd_lun *un;
20043 20039
20044 20040 ASSERT(ssc != NULL);
20045 20041
20046 20042 un = ssc->ssc_un;
20047 20043 ASSERT(un != NULL);
20048 20044 ASSERT(!mutex_owned(SD_MUTEX(un)));
20049 20045 ASSERT(capp != NULL);
20050 20046 ASSERT(lbap != NULL);
20051 20047
20052 20048 SD_TRACE(SD_LOG_IO, un,
20053 20049 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20054 20050
20055 20051 /*
20056 20052 * First send a READ_CAPACITY command to the target.
20057 20053 * (This command is mandatory under SCSI-2.)
20058 20054 *
20059 20055 * Set up the CDB for the READ_CAPACITY command. The Partial
20060 20056 * Medium Indicator bit is cleared. The address field must be
20061 20057 * zero if the PMI bit is zero.
20062 20058 */
20063 20059 bzero(&cdb, sizeof (cdb));
20064 20060 bzero(&ucmd_buf, sizeof (ucmd_buf));
20065 20061
20066 20062 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20067 20063
20068 20064 cdb.scc_cmd = SCMD_READ_CAPACITY;
20069 20065
20070 20066 ucmd_buf.uscsi_cdb = (char *)&cdb;
20071 20067 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20072 20068 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf;
20073 20069 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE;
20074 20070 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20075 20071 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20076 20072 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20077 20073 ucmd_buf.uscsi_timeout = 60;
20078 20074
20079 20075 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20080 20076 UIO_SYSSPACE, path_flag);
20081 20077
20082 20078 switch (status) {
20083 20079 case 0:
20084 20080 /* Return failure if we did not get valid capacity data. */
20085 20081 if (ucmd_buf.uscsi_resid != 0) {
20086 20082 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20087 20083 "sd_send_scsi_READ_CAPACITY received invalid "
20088 20084 "capacity data");
20089 20085 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20090 20086 return (EIO);
20091 20087 }
20092 20088 /*
20093 20089 * Read capacity and block size from the READ CAPACITY 10 data.
20094 20090 * This data may be adjusted later due to device specific
20095 20091 * issues.
20096 20092 *
20097 20093 * According to the SCSI spec, the READ CAPACITY 10
20098 20094 * command returns the following:
20099 20095 *
20100 20096 * bytes 0-3: Maximum logical block address available.
20101 20097 * (MSB in byte:0 & LSB in byte:3)
20102 20098 *
20103 20099 * bytes 4-7: Block length in bytes
20104 20100 * (MSB in byte:4 & LSB in byte:7)
20105 20101 *
20106 20102 */
20107 20103 capacity = BE_32(capacity_buf[0]);
20108 20104 lbasize = BE_32(capacity_buf[1]);
20109 20105
20110 20106 /*
20111 20107 * Done with capacity_buf
20112 20108 */
20113 20109 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20114 20110
20115 20111 /*
20116 20112 * if the reported capacity is set to all 0xf's, then
20117 20113 * this disk is too large and requires SBC-2 commands.
20118 20114 * Reissue the request using READ CAPACITY 16.
20119 20115 */
20120 20116 if (capacity == 0xffffffff) {
20121 20117 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20122 20118 status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20123 20119 &lbasize, &pbsize, path_flag);
20124 20120 if (status != 0) {
20125 20121 return (status);
20126 20122 } else {
20127 20123 goto rc16_done;
20128 20124 }
20129 20125 }
20130 20126 break; /* Success! */
20131 20127 case EIO:
20132 20128 switch (ucmd_buf.uscsi_status) {
20133 20129 case STATUS_RESERVATION_CONFLICT:
20134 20130 status = EACCES;
20135 20131 break;
20136 20132 case STATUS_CHECK:
20137 20133 /*
20138 20134 * Check condition; look for ASC/ASCQ of 0x04/0x01
20139 20135 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20140 20136 */
20141 20137 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20142 20138 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20143 20139 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20144 20140 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20145 20141 return (EAGAIN);
20146 20142 }
20147 20143 break;
20148 20144 default:
20149 20145 break;
20150 20146 }
20151 20147 /* FALLTHRU */
20152 20148 default:
20153 20149 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20154 20150 return (status);
20155 20151 }
20156 20152
20157 20153 /*
20158 20154 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20159 20155 * (2352 and 0 are common) so for these devices always force the value
20160 20156 * to 2048 as required by the ATAPI specs.
20161 20157 */
20162 20158 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20163 20159 lbasize = 2048;
20164 20160 }
20165 20161
20166 20162 /*
20167 20163 * Get the maximum LBA value from the READ CAPACITY data.
20168 20164 * Here we assume that the Partial Medium Indicator (PMI) bit
20169 20165 * was cleared when issuing the command. This means that the LBA
20170 20166 * returned from the device is the LBA of the last logical block
20171 20167 * on the logical unit. The actual logical block count will be
20172 20168 * this value plus one.
20173 20169 */
20174 20170 capacity += 1;
20175 20171
20176 20172 /*
20177 20173 * Currently, for removable media, the capacity is saved in terms
20178 20174 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20179 20175 */
20180 20176 if (un->un_f_has_removable_media)
20181 20177 capacity *= (lbasize / un->un_sys_blocksize);
20182 20178
20183 20179 rc16_done:
20184 20180
20185 20181 /*
20186 20182 * Copy the values from the READ CAPACITY command into the space
20187 20183 * provided by the caller.
20188 20184 */
20189 20185 *capp = capacity;
20190 20186 *lbap = lbasize;
20191 20187
20192 20188 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20193 20189 "capacity:0x%llx lbasize:0x%x\n", capacity, lbasize);
20194 20190
20195 20191 /*
20196 20192 * Both the lbasize and capacity from the device must be nonzero,
20197 20193 * otherwise we assume that the values are not valid and return
20198 20194 * failure to the caller. (4203735)
20199 20195 */
20200 20196 if ((capacity == 0) || (lbasize == 0)) {
20201 20197 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20202 20198 "sd_send_scsi_READ_CAPACITY received invalid value "
20203 20199 "capacity %llu lbasize %d", capacity, lbasize);
20204 20200 return (EIO);
20205 20201 }
20206 20202 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20207 20203 return (0);
20208 20204 }
20209 20205
20210 20206 /*
20211 20207 * Function: sd_send_scsi_READ_CAPACITY_16
20212 20208 *
20213 20209 * Description: This routine uses the scsi READ CAPACITY 16 command to
20214 20210 * determine the device capacity in number of blocks and the
20215 20211 * device native block size. If this function returns a failure,
20216 20212 * then the values in *capp and *lbap are undefined.
20217 20213 * This routine should be called by sd_send_scsi_READ_CAPACITY
20218 20214 * which will apply any device specific adjustments to capacity
20219 20215 * and lbasize. One exception is it is also called by
20220 20216 * sd_get_media_info_ext. In that function, there is no need to
20221 20217 * adjust the capacity and lbasize.
20222 20218 *
20223 20219 * Arguments: ssc - ssc contains ptr to soft state struct for the target
20224 20220 * capp - ptr to unsigned 64-bit variable to receive the
20225 20221 * capacity value from the command.
20226 20222 * lbap - ptr to unsigned 32-bit varaible to receive the
20227 20223 * block size value from the command
20228 20224 * psp - ptr to unsigned 32-bit variable to receive the
20229 20225 * physical block size value from the command
20230 20226 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20231 20227 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20232 20228 * to use the USCSI "direct" chain and bypass the normal
20233 20229 * command waitq. SD_PATH_DIRECT_PRIORITY is used when
20234 20230 * this command is issued as part of an error recovery
20235 20231 * action.
20236 20232 *
20237 20233 * Return Code: 0 - Success
20238 20234 * EIO - IO error
20239 20235 * EACCES - Reservation conflict detected
20240 20236 * EAGAIN - Device is becoming ready
20241 20237 * errno return code from sd_ssc_send()
20242 20238 *
20243 20239 * Context: Can sleep. Blocks until command completes.
20244 20240 */
20245 20241
20246 20242 #define SD_CAPACITY_16_SIZE sizeof (struct scsi_capacity_16)
20247 20243
20248 20244 static int
20249 20245 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20250 20246 uint32_t *lbap, uint32_t *psp, int path_flag)
20251 20247 {
20252 20248 struct scsi_extended_sense sense_buf;
20253 20249 struct uscsi_cmd ucmd_buf;
20254 20250 union scsi_cdb cdb;
20255 20251 uint64_t *capacity16_buf;
20256 20252 uint64_t capacity;
20257 20253 uint32_t lbasize;
20258 20254 uint32_t pbsize;
20259 20255 uint32_t lbpb_exp;
20260 20256 int status;
20261 20257 struct sd_lun *un;
20262 20258
20263 20259 ASSERT(ssc != NULL);
20264 20260
20265 20261 un = ssc->ssc_un;
20266 20262 ASSERT(un != NULL);
20267 20263 ASSERT(!mutex_owned(SD_MUTEX(un)));
20268 20264 ASSERT(capp != NULL);
20269 20265 ASSERT(lbap != NULL);
20270 20266
20271 20267 SD_TRACE(SD_LOG_IO, un,
20272 20268 "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20273 20269
20274 20270 /*
20275 20271 * First send a READ_CAPACITY_16 command to the target.
20276 20272 *
20277 20273 * Set up the CDB for the READ_CAPACITY_16 command. The Partial
20278 20274 * Medium Indicator bit is cleared. The address field must be
20279 20275 * zero if the PMI bit is zero.
20280 20276 */
20281 20277 bzero(&cdb, sizeof (cdb));
20282 20278 bzero(&ucmd_buf, sizeof (ucmd_buf));
20283 20279
20284 20280 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20285 20281
20286 20282 ucmd_buf.uscsi_cdb = (char *)&cdb;
20287 20283 ucmd_buf.uscsi_cdblen = CDB_GROUP4;
20288 20284 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf;
20289 20285 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE;
20290 20286 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20291 20287 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20292 20288 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20293 20289 ucmd_buf.uscsi_timeout = 60;
20294 20290
20295 20291 /*
20296 20292 * Read Capacity (16) is a Service Action In command. One
20297 20293 * command byte (0x9E) is overloaded for multiple operations,
20298 20294 * with the second CDB byte specifying the desired operation
20299 20295 */
20300 20296 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20301 20297 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20302 20298
20303 20299 /*
20304 20300 * Fill in allocation length field
20305 20301 */
20306 20302 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20307 20303
20308 20304 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20309 20305 UIO_SYSSPACE, path_flag);
20310 20306
20311 20307 switch (status) {
20312 20308 case 0:
20313 20309 /* Return failure if we did not get valid capacity data. */
20314 20310 if (ucmd_buf.uscsi_resid > 20) {
20315 20311 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20316 20312 "sd_send_scsi_READ_CAPACITY_16 received invalid "
20317 20313 "capacity data");
20318 20314 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20319 20315 return (EIO);
20320 20316 }
20321 20317
20322 20318 /*
20323 20319 * Read capacity and block size from the READ CAPACITY 16 data.
20324 20320 * This data may be adjusted later due to device specific
20325 20321 * issues.
20326 20322 *
20327 20323 * According to the SCSI spec, the READ CAPACITY 16
20328 20324 * command returns the following:
20329 20325 *
20330 20326 * bytes 0-7: Maximum logical block address available.
20331 20327 * (MSB in byte:0 & LSB in byte:7)
20332 20328 *
20333 20329 * bytes 8-11: Block length in bytes
20334 20330 * (MSB in byte:8 & LSB in byte:11)
20335 20331 *
20336 20332 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20337 20333 */
20338 20334 capacity = BE_64(capacity16_buf[0]);
20339 20335 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20340 20336 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20341 20337
20342 20338 pbsize = lbasize << lbpb_exp;
20343 20339
20344 20340 /*
20345 20341 * Done with capacity16_buf
20346 20342 */
20347 20343 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20348 20344
20349 20345 /*
20350 20346 * if the reported capacity is set to all 0xf's, then
20351 20347 * this disk is too large. This could only happen with
20352 20348 * a device that supports LBAs larger than 64 bits which
20353 20349 * are not defined by any current T10 standards.
20354 20350 */
20355 20351 if (capacity == 0xffffffffffffffff) {
20356 20352 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20357 20353 "disk is too large");
20358 20354 return (EIO);
20359 20355 }
20360 20356 break; /* Success! */
20361 20357 case EIO:
20362 20358 switch (ucmd_buf.uscsi_status) {
20363 20359 case STATUS_RESERVATION_CONFLICT:
20364 20360 status = EACCES;
20365 20361 break;
20366 20362 case STATUS_CHECK:
20367 20363 /*
20368 20364 * Check condition; look for ASC/ASCQ of 0x04/0x01
20369 20365 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20370 20366 */
20371 20367 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20372 20368 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20373 20369 (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20374 20370 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20375 20371 return (EAGAIN);
20376 20372 }
20377 20373 break;
20378 20374 default:
20379 20375 break;
20380 20376 }
20381 20377 /* FALLTHRU */
20382 20378 default:
20383 20379 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20384 20380 return (status);
20385 20381 }
20386 20382
20387 20383 /*
20388 20384 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20389 20385 * (2352 and 0 are common) so for these devices always force the value
20390 20386 * to 2048 as required by the ATAPI specs.
20391 20387 */
20392 20388 if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20393 20389 lbasize = 2048;
20394 20390 }
20395 20391
20396 20392 /*
20397 20393 * Get the maximum LBA value from the READ CAPACITY 16 data.
20398 20394 * Here we assume that the Partial Medium Indicator (PMI) bit
20399 20395 * was cleared when issuing the command. This means that the LBA
20400 20396 * returned from the device is the LBA of the last logical block
20401 20397 * on the logical unit. The actual logical block count will be
20402 20398 * this value plus one.
20403 20399 */
20404 20400 capacity += 1;
20405 20401
20406 20402 /*
20407 20403 * Currently, for removable media, the capacity is saved in terms
20408 20404 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20409 20405 */
20410 20406 if (un->un_f_has_removable_media)
20411 20407 capacity *= (lbasize / un->un_sys_blocksize);
20412 20408
20413 20409 *capp = capacity;
20414 20410 *lbap = lbasize;
20415 20411 *psp = pbsize;
20416 20412
20417 20413 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20418 20414 "capacity:0x%llx lbasize:0x%x, pbsize: 0x%x\n",
20419 20415 capacity, lbasize, pbsize);
20420 20416
20421 20417 if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20422 20418 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20423 20419 "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20424 20420 "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20425 20421 return (EIO);
20426 20422 }
20427 20423
20428 20424 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20429 20425 return (0);
20430 20426 }
20431 20427
20432 20428
20433 20429 /*
20434 20430 * Function: sd_send_scsi_START_STOP_UNIT
20435 20431 *
20436 20432 * Description: Issue a scsi START STOP UNIT command to the target.
20437 20433 *
20438 20434 * Arguments: ssc - ssc contatins pointer to driver soft state (unit)
20439 20435 * structure for this target.
20440 20436 * pc_flag - SD_POWER_CONDITION
20441 20437 * SD_START_STOP
20442 20438 * flag - SD_TARGET_START
20443 20439 * SD_TARGET_STOP
20444 20440 * SD_TARGET_EJECT
20445 20441 * SD_TARGET_CLOSE
20446 20442 * path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20447 20443 * the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20448 20444 * to use the USCSI "direct" chain and bypass the normal
20449 20445 * command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20450 20446 * command is issued as part of an error recovery action.
20451 20447 *
20452 20448 * Return Code: 0 - Success
20453 20449 * EIO - IO error
20454 20450 * EACCES - Reservation conflict detected
20455 20451 * ENXIO - Not Ready, medium not present
20456 20452 * errno return code from sd_ssc_send()
20457 20453 *
20458 20454 * Context: Can sleep.
20459 20455 */
20460 20456
20461 20457 static int
20462 20458 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20463 20459 int path_flag)
20464 20460 {
20465 20461 struct scsi_extended_sense sense_buf;
20466 20462 union scsi_cdb cdb;
20467 20463 struct uscsi_cmd ucmd_buf;
20468 20464 int status;
20469 20465 struct sd_lun *un;
20470 20466
20471 20467 ASSERT(ssc != NULL);
20472 20468 un = ssc->ssc_un;
20473 20469 ASSERT(un != NULL);
20474 20470 ASSERT(!mutex_owned(SD_MUTEX(un)));
20475 20471
20476 20472 SD_TRACE(SD_LOG_IO, un,
20477 20473 "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20478 20474
20479 20475 if (un->un_f_check_start_stop &&
20480 20476 (pc_flag == SD_START_STOP) &&
20481 20477 ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20482 20478 (un->un_f_start_stop_supported != TRUE)) {
20483 20479