5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25 /*
26 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
27 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
29 * Copyright 2017 Nexenta Systems, Inc.
30 */
31 /*
32 * Copyright 2011 cyril.galibern@opensvc.com
33 */
34
35 /*
36 * SCSI disk target driver.
37 */
38 #include <sys/scsi/scsi.h>
39 #include <sys/dkbad.h>
40 #include <sys/dklabel.h>
41 #include <sys/dkio.h>
42 #include <sys/fdio.h>
43 #include <sys/cdio.h>
44 #include <sys/mhd.h>
45 #include <sys/vtoc.h>
46 #include <sys/dktp/fdisk.h>
47 #include <sys/kstat.h>
48 #include <sys/vtrace.h>
49 #include <sys/note.h>
50 #include <sys/thread.h>
51 #include <sys/proc.h>
52 #include <sys/efi_partition.h>
53 #include <sys/var.h>
54 #include <sys/aio_req.h>
55
56 #ifdef __lock_lint
57 #define _LP64
58 #define __amd64
59 #endif
60
61 #if (defined(__fibre))
62 /* Note: is there a leadville version of the following? */
63 #include <sys/fc4/fcal_linkapp.h>
64 #endif
65 #include <sys/taskq.h>
66 #include <sys/uuid.h>
67 #include <sys/byteorder.h>
68 #include <sys/sdt.h>
69
70 #include "sd_xbuf.h"
71
72 #include <sys/scsi/targets/sddef.h>
73 #include <sys/cmlb.h>
74 #include <sys/sysevent/eventdefs.h>
75 #include <sys/sysevent/dev.h>
76
77 #include <sys/fm/protocol.h>
78
79 /*
80 * Loadable module info.
81 */
82 #if (defined(__fibre))
83 #define SD_MODULE_NAME "SCSI SSA/FCAL Disk Driver"
84 #else /* !__fibre */
85 #define SD_MODULE_NAME "SCSI Disk Driver"
86 #endif /* !__fibre */
87
88 /*
89 * Define the interconnect type, to allow the driver to distinguish
90 * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
91 *
92 * This is really for backward compatibility. In the future, the driver
93 * should actually check the "interconnect-type" property as reported by
94 * the HBA; however at present this property is not defined by all HBAs,
95 * so we will use this #define (1) to permit the driver to run in
96 * backward-compatibility mode; and (2) to print a notification message
97 * if an FC HBA does not support the "interconnect-type" property. The
98 * behavior of the driver will be to assume parallel SCSI behaviors unless
99 * the "interconnect-type" property is defined by the HBA **AND** has a
100 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
101 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
102 * Channel behaviors (as per the old ssd). (Note that the
103 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
104 * will result in the driver assuming parallel SCSI behaviors.)
105 *
106 * (see common/sys/scsi/impl/services.h)
107 *
108 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
109 * since some FC HBAs may already support that, and there is some code in
110 * the driver that already looks for it. Using INTERCONNECT_FABRIC as the
111 * default would confuse that code, and besides things should work fine
112 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
113 * "interconnect_type" property.
114 *
115 */
116 #if (defined(__fibre))
117 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_FIBRE
118 #else
119 #define SD_DEFAULT_INTERCONNECT_TYPE SD_INTERCONNECT_PARALLEL
120 #endif
121
122 /*
123 * The name of the driver, established from the module name in _init.
124 */
125 static char *sd_label = NULL;
126
127 /*
128 * Driver name is unfortunately prefixed on some driver.conf properties.
129 */
130 #if (defined(__fibre))
131 #define sd_max_xfer_size ssd_max_xfer_size
132 #define sd_config_list ssd_config_list
133 static char *sd_max_xfer_size = "ssd_max_xfer_size";
134 static char *sd_config_list = "ssd-config-list";
135 #else
136 static char *sd_max_xfer_size = "sd_max_xfer_size";
137 static char *sd_config_list = "sd-config-list";
138 #endif
139
140 /*
141 * Driver global variables
142 */
143
144 #if (defined(__fibre))
145 /*
146 * These #defines are to avoid namespace collisions that occur because this
147 * code is currently used to compile two separate driver modules: sd and ssd.
148 * All global variables need to be treated this way (even if declared static)
149 * in order to allow the debugger to resolve the names properly.
150 * It is anticipated that in the near future the ssd module will be obsoleted,
151 * at which time this namespace issue should go away.
152 */
153 #define sd_state ssd_state
154 #define sd_io_time ssd_io_time
155 #define sd_failfast_enable ssd_failfast_enable
156 #define sd_ua_retry_count ssd_ua_retry_count
157 #define sd_report_pfa ssd_report_pfa
158 #define sd_max_throttle ssd_max_throttle
159 #define sd_min_throttle ssd_min_throttle
160 #define sd_rot_delay ssd_rot_delay
161
162 #define sd_retry_on_reservation_conflict \
163 ssd_retry_on_reservation_conflict
164 #define sd_reinstate_resv_delay ssd_reinstate_resv_delay
165 #define sd_resv_conflict_name ssd_resv_conflict_name
166
167 #define sd_component_mask ssd_component_mask
168 #define sd_level_mask ssd_level_mask
169 #define sd_debug_un ssd_debug_un
170 #define sd_error_level ssd_error_level
171
172 #define sd_xbuf_active_limit ssd_xbuf_active_limit
173 #define sd_xbuf_reserve_limit ssd_xbuf_reserve_limit
174
175 #define sd_tr ssd_tr
176 #define sd_reset_throttle_timeout ssd_reset_throttle_timeout
177 #define sd_qfull_throttle_timeout ssd_qfull_throttle_timeout
178 #define sd_qfull_throttle_enable ssd_qfull_throttle_enable
179 #define sd_check_media_time ssd_check_media_time
180 #define sd_wait_cmds_complete ssd_wait_cmds_complete
181 #define sd_label_mutex ssd_label_mutex
182 #define sd_detach_mutex ssd_detach_mutex
183 #define sd_log_buf ssd_log_buf
184 #define sd_log_mutex ssd_log_mutex
185
186 #define sd_disk_table ssd_disk_table
187 #define sd_disk_table_size ssd_disk_table_size
188 #define sd_sense_mutex ssd_sense_mutex
189 #define sd_cdbtab ssd_cdbtab
190
191 #define sd_cb_ops ssd_cb_ops
192 #define sd_ops ssd_ops
193 #define sd_additional_codes ssd_additional_codes
194 #define sd_tgops ssd_tgops
195
196 #define sd_minor_data ssd_minor_data
197 #define sd_minor_data_efi ssd_minor_data_efi
198
199 #define sd_tq ssd_tq
200 #define sd_wmr_tq ssd_wmr_tq
201 #define sd_taskq_name ssd_taskq_name
202 #define sd_wmr_taskq_name ssd_wmr_taskq_name
203 #define sd_taskq_minalloc ssd_taskq_minalloc
204 #define sd_taskq_maxalloc ssd_taskq_maxalloc
205
206 #define sd_dump_format_string ssd_dump_format_string
207
208 #define sd_iostart_chain ssd_iostart_chain
209 #define sd_iodone_chain ssd_iodone_chain
210
211 #define sd_pm_idletime ssd_pm_idletime
212
213 #define sd_force_pm_supported ssd_force_pm_supported
214
215 #define sd_dtype_optical_bind ssd_dtype_optical_bind
216
217 #define sd_ssc_init ssd_ssc_init
218 #define sd_ssc_send ssd_ssc_send
219 #define sd_ssc_fini ssd_ssc_fini
220 #define sd_ssc_assessment ssd_ssc_assessment
221 #define sd_ssc_post ssd_ssc_post
222 #define sd_ssc_print ssd_ssc_print
223 #define sd_ssc_ereport_post ssd_ssc_ereport_post
224 #define sd_ssc_set_info ssd_ssc_set_info
225 #define sd_ssc_extract_info ssd_ssc_extract_info
226
227 #endif
228
229 #ifdef SDDEBUG
230 int sd_force_pm_supported = 0;
231 #endif /* SDDEBUG */
232
233 void *sd_state = NULL;
234 int sd_io_time = SD_IO_TIME;
235 int sd_failfast_enable = 1;
236 int sd_ua_retry_count = SD_UA_RETRY_COUNT;
237 int sd_report_pfa = 1;
238 int sd_max_throttle = SD_MAX_THROTTLE;
239 int sd_min_throttle = SD_MIN_THROTTLE;
240 int sd_rot_delay = 4; /* Default 4ms Rotation delay */
241 int sd_qfull_throttle_enable = TRUE;
242
243 int sd_retry_on_reservation_conflict = 1;
244 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
245 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
246
247 static int sd_dtype_optical_bind = -1;
248
249 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
250 static char *sd_resv_conflict_name = "sd_retry_on_reservation_conflict";
251
252 /*
253 * Global data for debug logging. To enable debug printing, sd_component_mask
254 * and sd_level_mask should be set to the desired bit patterns as outlined in
255 * sddef.h.
256 */
257 uint_t sd_component_mask = 0x0;
258 uint_t sd_level_mask = 0x0;
259 struct sd_lun *sd_debug_un = NULL;
260 uint_t sd_error_level = SCSI_ERR_RETRYABLE;
261
262 /* Note: these may go away in the future... */
263 static uint32_t sd_xbuf_active_limit = 512;
264 static uint32_t sd_xbuf_reserve_limit = 16;
265
266 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
267
268 /*
269 * Timer value used to reset the throttle after it has been reduced
270 * (typically in response to TRAN_BUSY or STATUS_QFULL)
271 */
277 */
278 static int sd_check_media_time = 3000000;
279
280 /*
281 * Wait value used for in progress operations during a DDI_SUSPEND
282 */
283 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE;
284
285 /*
286 * sd_label_mutex protects a static buffer used in the disk label
287 * component of the driver
288 */
289 static kmutex_t sd_label_mutex;
290
291 /*
292 * sd_detach_mutex protects un_layer_count, un_detach_count, and
293 * un_opens_in_progress in the sd_lun structure.
294 */
295 static kmutex_t sd_detach_mutex;
296
297 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
298 sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
299
300 /*
301 * Global buffer and mutex for debug logging
302 */
303 static char sd_log_buf[1024];
304 static kmutex_t sd_log_mutex;
305
306 /*
307 * Structs and globals for recording attached lun information.
308 * This maintains a chain. Each node in the chain represents a SCSI controller.
309 * The structure records the number of luns attached to each target connected
310 * with the controller.
311 * For parallel scsi device only.
312 */
313 struct sd_scsi_hba_tgt_lun {
314 struct sd_scsi_hba_tgt_lun *next;
315 dev_info_t *pdip;
316 int nlun[NTARGETS_WIDE];
317 };
318
319 /*
320 * Flag to indicate the lun is attached or detached
321 */
322 #define SD_SCSI_LUN_ATTACH 0
323 #define SD_SCSI_LUN_DETACH 1
324
325 static kmutex_t sd_scsi_target_lun_mutex;
326 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL;
327
328 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
329 sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
330
331 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
332 sd_scsi_target_lun_head))
333
334 /*
335 * "Smart" Probe Caching structs, globals, #defines, etc.
336 * For parallel scsi and non-self-identify device only.
337 */
338
339 /*
340 * The following resources and routines are implemented to support
341 * "smart" probing, which caches the scsi_probe() results in an array,
342 * in order to help avoid long probe times.
343 */
344 struct sd_scsi_probe_cache {
345 struct sd_scsi_probe_cache *next;
346 dev_info_t *pdip;
347 int cache[NTARGETS_WIDE];
348 };
349
350 static kmutex_t sd_scsi_probe_cache_mutex;
351 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
352
353 /*
354 * Really we only need protection on the head of the linked list, but
355 * better safe than sorry.
356 */
357 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
358 sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
359
360 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
361 sd_scsi_probe_cache_head))
362
363 /*
364 * Power attribute table
365 */
366 static sd_power_attr_ss sd_pwr_ss = {
367 { "NAME=spindle-motor", "0=off", "1=on", NULL },
368 {0, 100},
369 {30, 0},
370 {20000, 0}
371 };
372
373 static sd_power_attr_pc sd_pwr_pc = {
374 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
375 "3=active", NULL },
376 {0, 0, 0, 100},
377 {90, 90, 20, 0},
378 {15000, 15000, 1000, 0}
379 };
380
381 /*
382 * Power level to power condition
383 */
384 static int sd_pl2pc[] = {
385 SD_TARGET_START_VALID,
386 SD_TARGET_STANDBY,
387 SD_TARGET_IDLE,
388 SD_TARGET_ACTIVE
389 };
390
391 /*
392 * Vendor specific data name property declarations
393 */
394
395 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
396
397 static sd_tunables seagate_properties = {
398 SEAGATE_THROTTLE_VALUE,
399 0,
400 0,
401 0,
402 0,
403 0,
404 0,
405 0,
406 0
407 };
408
409
410 static sd_tunables fujitsu_properties = {
411 FUJITSU_THROTTLE_VALUE,
412 0,
413 0,
414 0,
415 0,
416 0,
417 0,
418 0,
419 0
420 };
421
422 static sd_tunables ibm_properties = {
423 IBM_THROTTLE_VALUE,
424 0,
425 0,
426 0,
427 0,
428 0,
429 0,
462 0,
463 0,
464 0,
465 0,
466 MASERATI_DISKSORT_DISABLED_FLAG,
467 MASERATI_LUN_RESET_ENABLED_FLAG
468 };
469
470 static sd_tunables pirus_properties = {
471 PIRUS_THROTTLE_VALUE,
472 0,
473 PIRUS_NRR_COUNT,
474 PIRUS_BUSY_RETRIES,
475 PIRUS_RESET_RETRY_COUNT,
476 0,
477 PIRUS_MIN_THROTTLE_VALUE,
478 PIRUS_DISKSORT_DISABLED_FLAG,
479 PIRUS_LUN_RESET_ENABLED_FLAG
480 };
481
482 #endif
483
484 #if (defined(__sparc) && !defined(__fibre)) || \
485 (defined(__i386) || defined(__amd64))
486
487
488 static sd_tunables elite_properties = {
489 ELITE_THROTTLE_VALUE,
490 0,
491 0,
492 0,
493 0,
494 0,
495 0,
496 0,
497 0
498 };
499
500 static sd_tunables st31200n_properties = {
501 ST31200N_THROTTLE_VALUE,
502 0,
503 0,
504 0,
505 0,
506 0,
507 0,
508 0,
509 0
510 };
511
512 #endif /* Fibre or not */
513
514 static sd_tunables lsi_properties_scsi = {
515 LSI_THROTTLE_VALUE,
516 0,
517 LSI_NOTREADY_RETRIES,
518 0,
519 0,
520 0,
521 0,
522 0,
523 0
524 };
525
526 static sd_tunables symbios_properties = {
527 SYMBIOS_THROTTLE_VALUE,
528 0,
529 SYMBIOS_NOTREADY_RETRIES,
530 0,
531 0,
532 0,
533 0,
543 0,
544 0,
545 0,
546 0,
547 0
548 };
549
550 static sd_tunables lsi_oem_properties = {
551 0,
552 0,
553 LSI_OEM_NOTREADY_RETRIES,
554 0,
555 0,
556 0,
557 0,
558 0,
559 0,
560 1
561 };
562
563
564
565 #if (defined(SD_PROP_TST))
566
567 #define SD_TST_CTYPE_VAL CTYPE_CDROM
568 #define SD_TST_THROTTLE_VAL 16
569 #define SD_TST_NOTREADY_VAL 12
570 #define SD_TST_BUSY_VAL 60
571 #define SD_TST_RST_RETRY_VAL 36
572 #define SD_TST_RSV_REL_TIME 60
573
574 static sd_tunables tst_properties = {
575 SD_TST_THROTTLE_VAL,
576 SD_TST_CTYPE_VAL,
577 SD_TST_NOTREADY_VAL,
578 SD_TST_BUSY_VAL,
579 SD_TST_RST_RETRY_VAL,
580 SD_TST_RSV_REL_TIME,
581 0,
582 0,
583 0
584 };
585 #endif
586
587 /* This is similar to the ANSI toupper implementation */
588 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
589
590 /*
591 * Static Driver Configuration Table
592 *
593 * This is the table of disks which need throttle adjustment (or, perhaps
598 * structure. Device type is searched as far as the device_id string is
599 * defined. Flags defines which values are to be set in the driver from the
600 * properties list.
601 *
602 * Entries below which begin and end with a "*" are a special case.
603 * These do not have a specific vendor, and the string which follows
604 * can appear anywhere in the 16 byte PID portion of the inquiry data.
605 *
606 * Entries below which begin and end with a " " (blank) are a special
607 * case. The comparison function will treat multiple consecutive blanks
608 * as equivalent to a single blank. For example, this causes a
609 * sd_disk_table entry of " NEC CDROM " to match a device's id string
610 * of "NEC CDROM".
611 *
612 * Note: The MD21 controller type has been obsoleted.
613 * ST318202F is a Legacy device
614 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
615 * made with an FC connection. The entries here are a legacy.
616 */
617 static sd_disk_config_t sd_disk_table[] = {
618 #if defined(__fibre) || defined(__i386) || defined(__amd64)
619 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
632 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
633 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
648 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
667 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
668 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT |
669 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
670 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT |
671 SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
672 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
673 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties },
674 { "SUN T3", SD_CONF_BSET_THROTTLE |
675 SD_CONF_BSET_BSY_RETRY_COUNT|
676 SD_CONF_BSET_RST_RETRIES|
677 SD_CONF_BSET_RSV_REL_TIME,
678 &purple_properties },
679 { "SUN SESS01", SD_CONF_BSET_THROTTLE |
680 SD_CONF_BSET_BSY_RETRY_COUNT|
681 SD_CONF_BSET_RST_RETRIES|
682 SD_CONF_BSET_RSV_REL_TIME|
683 SD_CONF_BSET_MIN_THROTTLE|
684 SD_CONF_BSET_DISKSORT_DISABLED,
685 &sve_properties },
686 { "SUN T4", SD_CONF_BSET_THROTTLE |
687 SD_CONF_BSET_BSY_RETRY_COUNT|
688 SD_CONF_BSET_RST_RETRIES|
689 SD_CONF_BSET_RSV_REL_TIME,
690 &purple_properties },
691 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
692 SD_CONF_BSET_LUN_RESET_ENABLED,
693 &maserati_properties },
694 { "SUN SE6920", SD_CONF_BSET_THROTTLE |
695 SD_CONF_BSET_NRR_COUNT|
696 SD_CONF_BSET_BSY_RETRY_COUNT|
697 SD_CONF_BSET_RST_RETRIES|
698 SD_CONF_BSET_MIN_THROTTLE|
699 SD_CONF_BSET_DISKSORT_DISABLED|
700 SD_CONF_BSET_LUN_RESET_ENABLED,
701 &pirus_properties },
702 { "SUN SE6940", SD_CONF_BSET_THROTTLE |
703 SD_CONF_BSET_NRR_COUNT|
704 SD_CONF_BSET_BSY_RETRY_COUNT|
705 SD_CONF_BSET_RST_RETRIES|
706 SD_CONF_BSET_MIN_THROTTLE|
707 SD_CONF_BSET_DISKSORT_DISABLED|
708 SD_CONF_BSET_LUN_RESET_ENABLED,
709 &pirus_properties },
710 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE |
711 SD_CONF_BSET_NRR_COUNT|
712 SD_CONF_BSET_BSY_RETRY_COUNT|
713 SD_CONF_BSET_RST_RETRIES|
714 SD_CONF_BSET_MIN_THROTTLE|
715 SD_CONF_BSET_DISKSORT_DISABLED|
716 SD_CONF_BSET_LUN_RESET_ENABLED,
717 &pirus_properties },
718 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE |
719 SD_CONF_BSET_NRR_COUNT|
720 SD_CONF_BSET_BSY_RETRY_COUNT|
721 SD_CONF_BSET_RST_RETRIES|
722 SD_CONF_BSET_MIN_THROTTLE|
723 SD_CONF_BSET_DISKSORT_DISABLED|
724 SD_CONF_BSET_LUN_RESET_ENABLED,
725 &pirus_properties },
726 { "SUN PSX1000", SD_CONF_BSET_THROTTLE |
727 SD_CONF_BSET_NRR_COUNT|
728 SD_CONF_BSET_BSY_RETRY_COUNT|
729 SD_CONF_BSET_RST_RETRIES|
730 SD_CONF_BSET_MIN_THROTTLE|
731 SD_CONF_BSET_DISKSORT_DISABLED|
732 SD_CONF_BSET_LUN_RESET_ENABLED,
733 &pirus_properties },
734 { "SUN SE6330", SD_CONF_BSET_THROTTLE |
735 SD_CONF_BSET_NRR_COUNT|
736 SD_CONF_BSET_BSY_RETRY_COUNT|
737 SD_CONF_BSET_RST_RETRIES|
738 SD_CONF_BSET_MIN_THROTTLE|
739 SD_CONF_BSET_DISKSORT_DISABLED|
740 SD_CONF_BSET_LUN_RESET_ENABLED,
741 &pirus_properties },
742 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
743 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
744 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
747 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
748 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
749 #endif /* fibre or NON-sparc platforms */
750 #if ((defined(__sparc) && !defined(__fibre)) ||\
751 (defined(__i386) || defined(__amd64)))
752 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
753 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
754 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
755 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL },
756 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
757 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
758 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
759 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
760 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
761 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
762 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
763 { "SYMBIOS INF-01-00 ", SD_CONF_BSET_FAB_DEVID, NULL },
764 { "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
765 &symbios_properties },
766 { "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
767 &lsi_properties_scsi },
768 #if defined(__i386) || defined(__amd64)
769 { " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
770 | SD_CONF_BSET_READSUB_BCD
771 | SD_CONF_BSET_READ_TOC_ADDR_BCD
772 | SD_CONF_BSET_NO_READ_HEADER
773 | SD_CONF_BSET_READ_CD_XD4), NULL },
774
775 { " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
776 | SD_CONF_BSET_READSUB_BCD
777 | SD_CONF_BSET_READ_TOC_ADDR_BCD
778 | SD_CONF_BSET_NO_READ_HEADER
779 | SD_CONF_BSET_READ_CD_XD4), NULL },
780 #endif /* __i386 || __amd64 */
781 #endif /* sparc NON-fibre or NON-sparc platforms */
782
783 #if (defined(SD_PROP_TST))
784 { "VENDOR PRODUCT ", (SD_CONF_BSET_THROTTLE
785 | SD_CONF_BSET_CTYPE
786 | SD_CONF_BSET_NRR_COUNT
787 | SD_CONF_BSET_FAB_DEVID
788 | SD_CONF_BSET_NOCACHE
789 | SD_CONF_BSET_BSY_RETRY_COUNT
790 | SD_CONF_BSET_PLAYMSF_BCD
791 | SD_CONF_BSET_READSUB_BCD
792 | SD_CONF_BSET_READ_TOC_TRK_BCD
793 | SD_CONF_BSET_READ_TOC_ADDR_BCD
794 | SD_CONF_BSET_NO_READ_HEADER
795 | SD_CONF_BSET_READ_CD_XD4
796 | SD_CONF_BSET_RST_RETRIES
797 | SD_CONF_BSET_RSV_REL_TIME
798 | SD_CONF_BSET_TUR_CHECK), &tst_properties},
799 #endif
800 };
801
802 static const int sd_disk_table_size =
803 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
804
805 /*
806 * Emulation mode disk drive VID/PID table
807 */
808 static char sd_flash_dev_table[][25] = {
809 "ATA MARVELL SD88SA02",
810 "MARVELL SD88SA02",
811 "TOSHIBA THNSNV05",
812 };
813
814 static const int sd_flash_dev_table_size =
815 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
816
817 #define SD_INTERCONNECT_PARALLEL 0
818 #define SD_INTERCONNECT_FABRIC 1
843 (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
844 #define Restore_state(un) \
845 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
846
847 static struct sd_cdbinfo sd_cdbtab[] = {
848 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, },
849 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, },
850 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, },
851 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
852 };
853
854 /*
855 * Specifies the number of seconds that must have elapsed since the last
856 * cmd. has completed for a device to be declared idle to the PM framework.
857 */
858 static int sd_pm_idletime = 1;
859
860 /*
861 * Internal function prototypes
862 */
863
864 #if (defined(__fibre))
865 /*
866 * These #defines are to avoid namespace collisions that occur because this
867 * code is currently used to compile two separate driver modules: sd and ssd.
868 * All function names need to be treated this way (even if declared static)
869 * in order to allow the debugger to resolve the names properly.
870 * It is anticipated that in the near future the ssd module will be obsoleted,
871 * at which time this ugliness should go away.
872 */
873 #define sd_log_trace ssd_log_trace
874 #define sd_log_info ssd_log_info
875 #define sd_log_err ssd_log_err
876 #define sdprobe ssdprobe
877 #define sdinfo ssdinfo
878 #define sd_prop_op ssd_prop_op
879 #define sd_scsi_probe_cache_init ssd_scsi_probe_cache_init
880 #define sd_scsi_probe_cache_fini ssd_scsi_probe_cache_fini
881 #define sd_scsi_clear_probe_cache ssd_scsi_clear_probe_cache
882 #define sd_scsi_probe_with_cache ssd_scsi_probe_with_cache
883 #define sd_scsi_target_lun_init ssd_scsi_target_lun_init
884 #define sd_scsi_target_lun_fini ssd_scsi_target_lun_fini
885 #define sd_scsi_get_target_lun_count ssd_scsi_get_target_lun_count
886 #define sd_scsi_update_lun_on_target ssd_scsi_update_lun_on_target
887 #define sd_spin_up_unit ssd_spin_up_unit
888 #define sd_enable_descr_sense ssd_enable_descr_sense
889 #define sd_reenable_dsense_task ssd_reenable_dsense_task
890 #define sd_set_mmc_caps ssd_set_mmc_caps
891 #define sd_read_unit_properties ssd_read_unit_properties
892 #define sd_process_sdconf_file ssd_process_sdconf_file
893 #define sd_process_sdconf_table ssd_process_sdconf_table
894 #define sd_sdconf_id_match ssd_sdconf_id_match
895 #define sd_blank_cmp ssd_blank_cmp
896 #define sd_chk_vers1_data ssd_chk_vers1_data
897 #define sd_set_vers1_properties ssd_set_vers1_properties
898 #define sd_check_bdc_vpd ssd_check_bdc_vpd
899 #define sd_check_emulation_mode ssd_check_emulation_mode
900
901 #define sd_get_physical_geometry ssd_get_physical_geometry
902 #define sd_get_virtual_geometry ssd_get_virtual_geometry
903 #define sd_update_block_info ssd_update_block_info
904 #define sd_register_devid ssd_register_devid
905 #define sd_get_devid ssd_get_devid
906 #define sd_create_devid ssd_create_devid
907 #define sd_write_deviceid ssd_write_deviceid
908 #define sd_check_vpd_page_support ssd_check_vpd_page_support
909 #define sd_setup_pm ssd_setup_pm
910 #define sd_create_pm_components ssd_create_pm_components
911 #define sd_ddi_suspend ssd_ddi_suspend
912 #define sd_ddi_resume ssd_ddi_resume
913 #define sd_pm_state_change ssd_pm_state_change
914 #define sdpower ssdpower
915 #define sdattach ssdattach
916 #define sddetach ssddetach
917 #define sd_unit_attach ssd_unit_attach
918 #define sd_unit_detach ssd_unit_detach
919 #define sd_set_unit_attributes ssd_set_unit_attributes
920 #define sd_create_errstats ssd_create_errstats
921 #define sd_set_errstats ssd_set_errstats
922 #define sd_set_pstats ssd_set_pstats
923 #define sddump ssddump
924 #define sd_scsi_poll ssd_scsi_poll
925 #define sd_send_polled_RQS ssd_send_polled_RQS
926 #define sd_ddi_scsi_poll ssd_ddi_scsi_poll
927 #define sd_init_event_callbacks ssd_init_event_callbacks
928 #define sd_event_callback ssd_event_callback
929 #define sd_cache_control ssd_cache_control
930 #define sd_get_write_cache_enabled ssd_get_write_cache_enabled
931 #define sd_get_write_cache_changeable ssd_get_write_cache_changeable
932 #define sd_get_nv_sup ssd_get_nv_sup
933 #define sd_make_device ssd_make_device
934 #define sdopen ssdopen
935 #define sdclose ssdclose
936 #define sd_ready_and_valid ssd_ready_and_valid
937 #define sdmin ssdmin
938 #define sdread ssdread
939 #define sdwrite ssdwrite
940 #define sdaread ssdaread
941 #define sdawrite ssdawrite
942 #define sdstrategy ssdstrategy
943 #define sdioctl ssdioctl
944 #define sd_mapblockaddr_iostart ssd_mapblockaddr_iostart
945 #define sd_mapblocksize_iostart ssd_mapblocksize_iostart
946 #define sd_checksum_iostart ssd_checksum_iostart
947 #define sd_checksum_uscsi_iostart ssd_checksum_uscsi_iostart
948 #define sd_pm_iostart ssd_pm_iostart
949 #define sd_core_iostart ssd_core_iostart
950 #define sd_mapblockaddr_iodone ssd_mapblockaddr_iodone
951 #define sd_mapblocksize_iodone ssd_mapblocksize_iodone
952 #define sd_checksum_iodone ssd_checksum_iodone
953 #define sd_checksum_uscsi_iodone ssd_checksum_uscsi_iodone
954 #define sd_pm_iodone ssd_pm_iodone
955 #define sd_initpkt_for_buf ssd_initpkt_for_buf
956 #define sd_destroypkt_for_buf ssd_destroypkt_for_buf
957 #define sd_setup_rw_pkt ssd_setup_rw_pkt
958 #define sd_setup_next_rw_pkt ssd_setup_next_rw_pkt
959 #define sd_buf_iodone ssd_buf_iodone
960 #define sd_uscsi_strategy ssd_uscsi_strategy
961 #define sd_initpkt_for_uscsi ssd_initpkt_for_uscsi
962 #define sd_destroypkt_for_uscsi ssd_destroypkt_for_uscsi
963 #define sd_uscsi_iodone ssd_uscsi_iodone
964 #define sd_xbuf_strategy ssd_xbuf_strategy
965 #define sd_xbuf_init ssd_xbuf_init
966 #define sd_pm_entry ssd_pm_entry
967 #define sd_pm_exit ssd_pm_exit
968
969 #define sd_pm_idletimeout_handler ssd_pm_idletimeout_handler
970 #define sd_pm_timeout_handler ssd_pm_timeout_handler
971
972 #define sd_add_buf_to_waitq ssd_add_buf_to_waitq
973 #define sdintr ssdintr
974 #define sd_start_cmds ssd_start_cmds
975 #define sd_send_scsi_cmd ssd_send_scsi_cmd
976 #define sd_bioclone_alloc ssd_bioclone_alloc
977 #define sd_bioclone_free ssd_bioclone_free
978 #define sd_shadow_buf_alloc ssd_shadow_buf_alloc
979 #define sd_shadow_buf_free ssd_shadow_buf_free
980 #define sd_print_transport_rejected_message \
981 ssd_print_transport_rejected_message
982 #define sd_retry_command ssd_retry_command
983 #define sd_set_retry_bp ssd_set_retry_bp
984 #define sd_send_request_sense_command ssd_send_request_sense_command
985 #define sd_start_retry_command ssd_start_retry_command
986 #define sd_start_direct_priority_command \
987 ssd_start_direct_priority_command
988 #define sd_return_failed_command ssd_return_failed_command
989 #define sd_return_failed_command_no_restart \
990 ssd_return_failed_command_no_restart
991 #define sd_return_command ssd_return_command
992 #define sd_sync_with_callback ssd_sync_with_callback
993 #define sdrunout ssdrunout
994 #define sd_mark_rqs_busy ssd_mark_rqs_busy
995 #define sd_mark_rqs_idle ssd_mark_rqs_idle
996 #define sd_reduce_throttle ssd_reduce_throttle
997 #define sd_restore_throttle ssd_restore_throttle
998 #define sd_print_incomplete_msg ssd_print_incomplete_msg
999 #define sd_init_cdb_limits ssd_init_cdb_limits
1000 #define sd_pkt_status_good ssd_pkt_status_good
1001 #define sd_pkt_status_check_condition ssd_pkt_status_check_condition
1002 #define sd_pkt_status_busy ssd_pkt_status_busy
1003 #define sd_pkt_status_reservation_conflict \
1004 ssd_pkt_status_reservation_conflict
1005 #define sd_pkt_status_qfull ssd_pkt_status_qfull
1006 #define sd_handle_request_sense ssd_handle_request_sense
1007 #define sd_handle_auto_request_sense ssd_handle_auto_request_sense
1008 #define sd_print_sense_failed_msg ssd_print_sense_failed_msg
1009 #define sd_validate_sense_data ssd_validate_sense_data
1010 #define sd_decode_sense ssd_decode_sense
1011 #define sd_print_sense_msg ssd_print_sense_msg
1012 #define sd_sense_key_no_sense ssd_sense_key_no_sense
1013 #define sd_sense_key_recoverable_error ssd_sense_key_recoverable_error
1014 #define sd_sense_key_not_ready ssd_sense_key_not_ready
1015 #define sd_sense_key_medium_or_hardware_error \
1016 ssd_sense_key_medium_or_hardware_error
1017 #define sd_sense_key_illegal_request ssd_sense_key_illegal_request
1018 #define sd_sense_key_unit_attention ssd_sense_key_unit_attention
1019 #define sd_sense_key_fail_command ssd_sense_key_fail_command
1020 #define sd_sense_key_blank_check ssd_sense_key_blank_check
1021 #define sd_sense_key_aborted_command ssd_sense_key_aborted_command
1022 #define sd_sense_key_default ssd_sense_key_default
1023 #define sd_print_retry_msg ssd_print_retry_msg
1024 #define sd_print_cmd_incomplete_msg ssd_print_cmd_incomplete_msg
1025 #define sd_pkt_reason_cmd_incomplete ssd_pkt_reason_cmd_incomplete
1026 #define sd_pkt_reason_cmd_tran_err ssd_pkt_reason_cmd_tran_err
1027 #define sd_pkt_reason_cmd_reset ssd_pkt_reason_cmd_reset
1028 #define sd_pkt_reason_cmd_aborted ssd_pkt_reason_cmd_aborted
1029 #define sd_pkt_reason_cmd_timeout ssd_pkt_reason_cmd_timeout
1030 #define sd_pkt_reason_cmd_unx_bus_free ssd_pkt_reason_cmd_unx_bus_free
1031 #define sd_pkt_reason_cmd_tag_reject ssd_pkt_reason_cmd_tag_reject
1032 #define sd_pkt_reason_default ssd_pkt_reason_default
1033 #define sd_reset_target ssd_reset_target
1034 #define sd_start_stop_unit_callback ssd_start_stop_unit_callback
1035 #define sd_start_stop_unit_task ssd_start_stop_unit_task
1036 #define sd_taskq_create ssd_taskq_create
1037 #define sd_taskq_delete ssd_taskq_delete
1038 #define sd_target_change_task ssd_target_change_task
1039 #define sd_log_dev_status_event ssd_log_dev_status_event
1040 #define sd_log_lun_expansion_event ssd_log_lun_expansion_event
1041 #define sd_log_eject_request_event ssd_log_eject_request_event
1042 #define sd_media_change_task ssd_media_change_task
1043 #define sd_handle_mchange ssd_handle_mchange
1044 #define sd_send_scsi_DOORLOCK ssd_send_scsi_DOORLOCK
1045 #define sd_send_scsi_READ_CAPACITY ssd_send_scsi_READ_CAPACITY
1046 #define sd_send_scsi_READ_CAPACITY_16 ssd_send_scsi_READ_CAPACITY_16
1047 #define sd_send_scsi_GET_CONFIGURATION ssd_send_scsi_GET_CONFIGURATION
1048 #define sd_send_scsi_feature_GET_CONFIGURATION \
1049 sd_send_scsi_feature_GET_CONFIGURATION
1050 #define sd_send_scsi_START_STOP_UNIT ssd_send_scsi_START_STOP_UNIT
1051 #define sd_send_scsi_INQUIRY ssd_send_scsi_INQUIRY
1052 #define sd_send_scsi_TEST_UNIT_READY ssd_send_scsi_TEST_UNIT_READY
1053 #define sd_send_scsi_PERSISTENT_RESERVE_IN \
1054 ssd_send_scsi_PERSISTENT_RESERVE_IN
1055 #define sd_send_scsi_PERSISTENT_RESERVE_OUT \
1056 ssd_send_scsi_PERSISTENT_RESERVE_OUT
1057 #define sd_send_scsi_SYNCHRONIZE_CACHE ssd_send_scsi_SYNCHRONIZE_CACHE
1058 #define sd_send_scsi_SYNCHRONIZE_CACHE_biodone \
1059 ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1060 #define sd_send_scsi_MODE_SENSE ssd_send_scsi_MODE_SENSE
1061 #define sd_send_scsi_MODE_SELECT ssd_send_scsi_MODE_SELECT
1062 #define sd_send_scsi_RDWR ssd_send_scsi_RDWR
1063 #define sd_send_scsi_LOG_SENSE ssd_send_scsi_LOG_SENSE
1064 #define sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION \
1065 ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1066 #define sd_gesn_media_data_valid ssd_gesn_media_data_valid
1067 #define sd_alloc_rqs ssd_alloc_rqs
1068 #define sd_free_rqs ssd_free_rqs
1069 #define sd_dump_memory ssd_dump_memory
1070 #define sd_get_media_info_com ssd_get_media_info_com
1071 #define sd_get_media_info ssd_get_media_info
1072 #define sd_get_media_info_ext ssd_get_media_info_ext
1073 #define sd_dkio_ctrl_info ssd_dkio_ctrl_info
1074 #define sd_nvpair_str_decode ssd_nvpair_str_decode
1075 #define sd_strtok_r ssd_strtok_r
1076 #define sd_set_properties ssd_set_properties
1077 #define sd_get_tunables_from_conf ssd_get_tunables_from_conf
1078 #define sd_setup_next_xfer ssd_setup_next_xfer
1079 #define sd_dkio_get_temp ssd_dkio_get_temp
1080 #define sd_check_mhd ssd_check_mhd
1081 #define sd_mhd_watch_cb ssd_mhd_watch_cb
1082 #define sd_mhd_watch_incomplete ssd_mhd_watch_incomplete
1083 #define sd_sname ssd_sname
1084 #define sd_mhd_resvd_recover ssd_mhd_resvd_recover
1085 #define sd_resv_reclaim_thread ssd_resv_reclaim_thread
1086 #define sd_take_ownership ssd_take_ownership
1087 #define sd_reserve_release ssd_reserve_release
1088 #define sd_rmv_resv_reclaim_req ssd_rmv_resv_reclaim_req
1089 #define sd_mhd_reset_notify_cb ssd_mhd_reset_notify_cb
1090 #define sd_persistent_reservation_in_read_keys \
1091 ssd_persistent_reservation_in_read_keys
1092 #define sd_persistent_reservation_in_read_resv \
1093 ssd_persistent_reservation_in_read_resv
1094 #define sd_mhdioc_takeown ssd_mhdioc_takeown
1095 #define sd_mhdioc_failfast ssd_mhdioc_failfast
1096 #define sd_mhdioc_release ssd_mhdioc_release
1097 #define sd_mhdioc_register_devid ssd_mhdioc_register_devid
1098 #define sd_mhdioc_inkeys ssd_mhdioc_inkeys
1099 #define sd_mhdioc_inresv ssd_mhdioc_inresv
1100 #define sr_change_blkmode ssr_change_blkmode
1101 #define sr_change_speed ssr_change_speed
1102 #define sr_atapi_change_speed ssr_atapi_change_speed
1103 #define sr_pause_resume ssr_pause_resume
1104 #define sr_play_msf ssr_play_msf
1105 #define sr_play_trkind ssr_play_trkind
1106 #define sr_read_all_subcodes ssr_read_all_subcodes
1107 #define sr_read_subchannel ssr_read_subchannel
1108 #define sr_read_tocentry ssr_read_tocentry
1109 #define sr_read_tochdr ssr_read_tochdr
1110 #define sr_read_cdda ssr_read_cdda
1111 #define sr_read_cdxa ssr_read_cdxa
1112 #define sr_read_mode1 ssr_read_mode1
1113 #define sr_read_mode2 ssr_read_mode2
1114 #define sr_read_cd_mode2 ssr_read_cd_mode2
1115 #define sr_sector_mode ssr_sector_mode
1116 #define sr_eject ssr_eject
1117 #define sr_ejected ssr_ejected
1118 #define sr_check_wp ssr_check_wp
1119 #define sd_watch_request_submit ssd_watch_request_submit
1120 #define sd_check_media ssd_check_media
1121 #define sd_media_watch_cb ssd_media_watch_cb
1122 #define sd_delayed_cv_broadcast ssd_delayed_cv_broadcast
1123 #define sr_volume_ctrl ssr_volume_ctrl
1124 #define sr_read_sony_session_offset ssr_read_sony_session_offset
1125 #define sd_log_page_supported ssd_log_page_supported
1126 #define sd_check_for_writable_cd ssd_check_for_writable_cd
1127 #define sd_wm_cache_constructor ssd_wm_cache_constructor
1128 #define sd_wm_cache_destructor ssd_wm_cache_destructor
1129 #define sd_range_lock ssd_range_lock
1130 #define sd_get_range ssd_get_range
1131 #define sd_free_inlist_wmap ssd_free_inlist_wmap
1132 #define sd_range_unlock ssd_range_unlock
1133 #define sd_read_modify_write_task ssd_read_modify_write_task
1134 #define sddump_do_read_of_rmw ssddump_do_read_of_rmw
1135
1136 #define sd_iostart_chain ssd_iostart_chain
1137 #define sd_iodone_chain ssd_iodone_chain
1138 #define sd_initpkt_map ssd_initpkt_map
1139 #define sd_destroypkt_map ssd_destroypkt_map
1140 #define sd_chain_type_map ssd_chain_type_map
1141 #define sd_chain_index_map ssd_chain_index_map
1142
1143 #define sd_failfast_flushctl ssd_failfast_flushctl
1144 #define sd_failfast_flushq ssd_failfast_flushq
1145 #define sd_failfast_flushq_callback ssd_failfast_flushq_callback
1146
1147 #define sd_is_lsi ssd_is_lsi
1148 #define sd_tg_rdwr ssd_tg_rdwr
1149 #define sd_tg_getinfo ssd_tg_getinfo
1150 #define sd_rmw_msg_print_handler ssd_rmw_msg_print_handler
1151
1152 #endif /* #if (defined(__fibre)) */
1153
1154
1155 int _init(void);
1156 int _fini(void);
1157 int _info(struct modinfo *modinfop);
1158
1159 /*PRINTFLIKE3*/
1160 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1161 /*PRINTFLIKE3*/
1162 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1163 /*PRINTFLIKE3*/
1164 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1165
1166 static int sdprobe(dev_info_t *devi);
1167 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1168 void **result);
1169 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1170 int mod_flags, char *name, caddr_t valuep, int *lengthp);
1171
1172 /*
1173 * Smart probe for parallel scsi
1174 */
1184 static void sd_scsi_target_lun_fini(void);
1185 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1186 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1187
1188 static int sd_spin_up_unit(sd_ssc_t *ssc);
1189
1190 /*
1191 * Using sd_ssc_init to establish sd_ssc_t struct
1192 * Using sd_ssc_send to send uscsi internal command
1193 * Using sd_ssc_fini to free sd_ssc_t struct
1194 */
1195 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1196 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1197 int flag, enum uio_seg dataspace, int path_flag);
1198 static void sd_ssc_fini(sd_ssc_t *ssc);
1199
1200 /*
1201 * Using sd_ssc_assessment to set correct type-of-assessment
1202 * Using sd_ssc_post to post ereport & system log
1203 * sd_ssc_post will call sd_ssc_print to print system log
1204 * sd_ssc_post will call sd_ssd_ereport_post to post ereport
1205 */
1206 static void sd_ssc_assessment(sd_ssc_t *ssc,
1207 enum sd_type_assessment tp_assess);
1208
1209 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1210 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1211 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1212 enum sd_driver_assessment drv_assess);
1213
1214 /*
1215 * Using sd_ssc_set_info to mark an un-decodable-data error.
1216 * Using sd_ssc_extract_info to transfer information from internal
1217 * data structures to sd_ssc_t.
1218 */
1219 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1220 const char *fmt, ...);
1221 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1222 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1223
1224 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1236 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1237 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1238 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1239 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1240 int *data_list, sd_tunables *values);
1241 static void sd_process_sdconf_table(struct sd_lun *un);
1242 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1243 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1244 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1245 int list_len, char *dataname_ptr);
1246 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1247 sd_tunables *prop_list);
1248
1249 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1250 int reservation_flag);
1251 static int sd_get_devid(sd_ssc_t *ssc);
1252 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1253 static int sd_write_deviceid(sd_ssc_t *ssc);
1254 static int sd_check_vpd_page_support(sd_ssc_t *ssc);
1255
1256 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1257 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1258
1259 static int sd_ddi_suspend(dev_info_t *devi);
1260 static int sd_ddi_resume(dev_info_t *devi);
1261 static int sd_pm_state_change(struct sd_lun *un, int level, int flag);
1262 static int sdpower(dev_info_t *devi, int component, int level);
1263
1264 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1265 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1266 static int sd_unit_attach(dev_info_t *devi);
1267 static int sd_unit_detach(dev_info_t *devi);
1268
1269 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1270 static void sd_create_errstats(struct sd_lun *un, int instance);
1271 static void sd_set_errstats(struct sd_lun *un);
1272 static void sd_set_pstats(struct sd_lun *un);
1273
1274 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1275 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1276 static int sd_send_polled_RQS(struct sd_lun *un);
1277 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1278
1279 #if (defined(__fibre))
1280 /*
1281 * Event callbacks (photon)
1282 */
1283 static void sd_init_event_callbacks(struct sd_lun *un);
1284 static void sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1285 #endif
1286
1287 /*
1288 * Defines for sd_cache_control
1289 */
1290
1291 #define SD_CACHE_ENABLE 1
1292 #define SD_CACHE_DISABLE 0
1293 #define SD_CACHE_NOCHANGE -1
1294
1295 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1296 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1297 static void sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
1298 static void sd_get_nv_sup(sd_ssc_t *ssc);
1299 static dev_t sd_make_device(dev_info_t *devi);
1300 static void sd_check_bdc_vpd(sd_ssc_t *ssc);
1301 static void sd_check_emulation_mode(sd_ssc_t *ssc);
1302 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1303 uint64_t capacity);
1304
1305 /*
1306 * Driver entry point functions.
1307 */
1308 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1309 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1310 static int sd_ready_and_valid(sd_ssc_t *ssc, int part);
1393 static void sd_print_transport_rejected_message(struct sd_lun *un,
1394 struct sd_xbuf *xp, int code);
1395 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1396 void *arg, int code);
1397 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1398 void *arg, int code);
1399 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1400 void *arg, int code);
1401
1402 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1403 int retry_check_flag,
1404 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1405 int c),
1406 void *user_arg, int failure_code, clock_t retry_delay,
1407 void (*statp)(kstat_io_t *));
1408
1409 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1410 clock_t retry_delay, void (*statp)(kstat_io_t *));
1411
1412 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1413 struct scsi_pkt *pktp);
1414 static void sd_start_retry_command(void *arg);
1415 static void sd_start_direct_priority_command(void *arg);
1416 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1417 int errcode);
1418 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1419 struct buf *bp, int errcode);
1420 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1421 static void sd_sync_with_callback(struct sd_lun *un);
1422 static int sdrunout(caddr_t arg);
1423
1424 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1425 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1426
1427 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1428 static void sd_restore_throttle(void *arg);
1429
1430 static void sd_init_cdb_limits(struct sd_lun *un);
1431
1432 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1433 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1516 static void sd_media_change_task(void *arg);
1517
1518 static int sd_handle_mchange(struct sd_lun *un);
1519 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1520 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1521 uint32_t *lbap, int path_flag);
1522 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1523 uint32_t *lbap, uint32_t *psp, int path_flag);
1524 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1525 int flag, int path_flag);
1526 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1527 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1528 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1529 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1530 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1531 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1532 uchar_t usr_cmd, uchar_t *usr_bufp);
1533 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1534 struct dk_callback *dkc);
1535 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1536 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1537 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1538 uchar_t *bufaddr, uint_t buflen, int path_flag);
1539 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1540 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1541 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1542 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1543 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1544 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1545 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1546 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1547 size_t buflen, daddr_t start_block, int path_flag);
1548 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1549 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1550 path_flag)
1551 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1552 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1553 path_flag)
1554
1555 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1610 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1616 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1617 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1618 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1619 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1620 static int sr_eject(dev_t dev);
1621 static void sr_ejected(register struct sd_lun *un);
1622 static int sr_check_wp(dev_t dev);
1623 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1624 static int sd_check_media(dev_t dev, enum dkio_state state);
1625 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1626 static void sd_delayed_cv_broadcast(void *arg);
1627 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1628 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1629
1630 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1631
1632 /*
1633 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1634 */
1635 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1636 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1637 static void sd_wm_cache_destructor(void *wm, void *un);
1638 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1639 daddr_t endb, ushort_t typ);
1640 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1641 daddr_t endb);
1642 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1643 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1644 static void sd_read_modify_write_task(void * arg);
1645 static int
1646 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1647 struct buf **bpp);
1648
1649
1650 /*
1651 * Function prototypes for failfast support.
1652 */
1653 static void sd_failfast_flushq(struct sd_lun *un);
1654 static int sd_failfast_flushq_callback(struct buf *bp);
1655
1656 /*
1657 * Function prototypes to check for lsi devices
1658 */
1659 static void sd_is_lsi(struct sd_lun *un);
1660
1661 /*
1662 * Function prototypes for partial DMA support
1663 */
1664 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1665 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1666
1667
1668 /* Function prototypes for cmlb */
1669 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1670 diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1671
1672 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1673
1674 /*
1675 * For printing RMW warning message timely
1676 */
1677 static void sd_rmw_msg_print_handler(void *arg);
1678
1679 /*
1680 * Constants for failfast support:
1681 *
1682 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1683 * failfast processing being performed.
1684 *
1685 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1686 * failfast processing on all bufs with B_FAILFAST set.
1687 */
1688
1689 #define SD_FAILFAST_INACTIVE 0
1690 #define SD_FAILFAST_ACTIVE 1
1691
1692 /*
1693 * Bitmask to control behavior of buf(9S) flushes when a transition to
1694 * the failfast state occurs. Optional bits include:
1695 *
1696 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1697 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1698 * be flushed.
1699 *
1700 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1701 * driver, in addition to the regular wait queue. This includes the xbuf
1702 * queues. When clear, only the driver's wait queue will be flushed.
1703 */
1704 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01
1705 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02
1706
1707 /*
1708 * The default behavior is to only flush bufs that have B_FAILFAST set, but
1709 * to flush all queues within the driver.
1710 */
1711 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1712
1713
1714 /*
1715 * SD Testing Fault Injection
1716 */
1717 #ifdef SD_FAULT_INJECTION
1718 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1719 static void sd_faultinjection(struct scsi_pkt *pktp);
1720 static void sd_injection_log(char *buf, struct sd_lun *un);
1721 #endif
1722
1723 /*
1724 * Device driver ops vector
1725 */
1726 static struct cb_ops sd_cb_ops = {
1727 sdopen, /* open */
1728 sdclose, /* close */
1729 sdstrategy, /* strategy */
1730 nodev, /* print */
1731 sddump, /* dump */
1732 sdread, /* read */
1733 sdwrite, /* write */
1734 sdioctl, /* ioctl */
1735 nodev, /* devmap */
1736 nodev, /* mmap */
1737 nodev, /* segmap */
1738 nochpoll, /* poll */
1739 sd_prop_op, /* cb_prop_op */
2355 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11
2356
2357 /* un->un_uscsi_chain_type must be set to one of these */
2358 #define SD_CHAIN_INFO_USCSI_CMD 6
2359 /* USCSI with PM disabled is the same as DIRECT */
2360 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8
2361 #define SD_CHAIN_INFO_USCSI_CHKSUM 7
2362
2363 /* un->un_direct_chain_type must be set to one of these */
2364 #define SD_CHAIN_INFO_DIRECT_CMD 8
2365
2366 /* un->un_priority_chain_type must be set to one of these */
2367 #define SD_CHAIN_INFO_PRIORITY_CMD 9
2368
2369 /* size for devid inquiries */
2370 #define MAX_INQUIRY_SIZE 0xF0
2371
2372 /*
2373 * Macros used by functions to pass a given buf(9S) struct along to the
2374 * next function in the layering chain for further processing.
2375 *
2376 * In the following macros, passing more than three arguments to the called
2377 * routines causes the optimizer for the SPARC compiler to stop doing tail
2378 * call elimination which results in significant performance degradation.
2379 */
2380 #define SD_BEGIN_IOSTART(index, un, bp) \
2381 ((*(sd_iostart_chain[index]))(index, un, bp))
2382
2383 #define SD_BEGIN_IODONE(index, un, bp) \
2384 ((*(sd_iodone_chain[index]))(index, un, bp))
2385
2386 #define SD_NEXT_IOSTART(index, un, bp) \
2387 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2388
2389 #define SD_NEXT_IODONE(index, un, bp) \
2390 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2391
2392 /*
2393 * Function: _init
2394 *
2395 * Description: This is the driver _init(9E) entry point.
2396 *
2397 * Return Code: Returns the value from mod_install(9F) or
2398 * ddi_soft_state_init(9F) as appropriate.
2551
2552 ASSERT(un != NULL);
2553 dev = SD_DEVINFO(un);
2554 ASSERT(dev != NULL);
2555
2556 /*
2557 * Filter messages based on the global component and level masks.
2558 * Also print if un matches the value of sd_debug_un, or if
2559 * sd_debug_un is set to NULL.
2560 */
2561 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2562 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2563 mutex_enter(&sd_log_mutex);
2564 va_start(ap, fmt);
2565 (void) vsprintf(sd_log_buf, fmt, ap);
2566 va_end(ap);
2567 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2568 mutex_exit(&sd_log_mutex);
2569 }
2570 #ifdef SD_FAULT_INJECTION
2571 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2572 if (un->sd_injection_mask & comp) {
2573 mutex_enter(&sd_log_mutex);
2574 va_start(ap, fmt);
2575 (void) vsprintf(sd_log_buf, fmt, ap);
2576 va_end(ap);
2577 sd_injection_log(sd_log_buf, un);
2578 mutex_exit(&sd_log_mutex);
2579 }
2580 #endif
2581 }
2582
2583
2584 /*
2585 * Function: sd_log_info
2586 *
2587 * Description: This routine is called by the SD_INFO macro for debug
2588 * logging of general purpose informational conditions.
2589 *
2590 * Arguments: comp - driver component being logged
2591 * dev - pointer to driver info structure
2601 ASSERT(un != NULL);
2602 dev = SD_DEVINFO(un);
2603 ASSERT(dev != NULL);
2604
2605 /*
2606 * Filter messages based on the global component and level masks.
2607 * Also print if un matches the value of sd_debug_un, or if
2608 * sd_debug_un is set to NULL.
2609 */
2610 if ((sd_component_mask & component) &&
2611 (sd_level_mask & SD_LOGMASK_INFO) &&
2612 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2613 mutex_enter(&sd_log_mutex);
2614 va_start(ap, fmt);
2615 (void) vsprintf(sd_log_buf, fmt, ap);
2616 va_end(ap);
2617 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2618 mutex_exit(&sd_log_mutex);
2619 }
2620 #ifdef SD_FAULT_INJECTION
2621 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2622 if (un->sd_injection_mask & component) {
2623 mutex_enter(&sd_log_mutex);
2624 va_start(ap, fmt);
2625 (void) vsprintf(sd_log_buf, fmt, ap);
2626 va_end(ap);
2627 sd_injection_log(sd_log_buf, un);
2628 mutex_exit(&sd_log_mutex);
2629 }
2630 #endif
2631 }
2632
2633
2634 /*
2635 * Function: sd_log_trace
2636 *
2637 * Description: This routine is called by the SD_TRACE macro for debug
2638 * logging of trace conditions (i.e. function entry/exit).
2639 *
2640 * Arguments: comp - driver component being logged
2641 * dev - pointer to driver info structure
2651 ASSERT(un != NULL);
2652 dev = SD_DEVINFO(un);
2653 ASSERT(dev != NULL);
2654
2655 /*
2656 * Filter messages based on the global component and level masks.
2657 * Also print if un matches the value of sd_debug_un, or if
2658 * sd_debug_un is set to NULL.
2659 */
2660 if ((sd_component_mask & component) &&
2661 (sd_level_mask & SD_LOGMASK_TRACE) &&
2662 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2663 mutex_enter(&sd_log_mutex);
2664 va_start(ap, fmt);
2665 (void) vsprintf(sd_log_buf, fmt, ap);
2666 va_end(ap);
2667 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2668 mutex_exit(&sd_log_mutex);
2669 }
2670 #ifdef SD_FAULT_INJECTION
2671 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2672 if (un->sd_injection_mask & component) {
2673 mutex_enter(&sd_log_mutex);
2674 va_start(ap, fmt);
2675 (void) vsprintf(sd_log_buf, fmt, ap);
2676 va_end(ap);
2677 sd_injection_log(sd_log_buf, un);
2678 mutex_exit(&sd_log_mutex);
2679 }
2680 #endif
2681 }
2682
2683
2684 /*
2685 * Function: sdprobe
2686 *
2687 * Description: This is the driver probe(9e) entry point function.
2688 *
2689 * Arguments: devi - opaque device info handle
2690 *
2691 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2692 * DDI_PROBE_FAILURE: If the probe failed.
2693 * DDI_PROBE_PARTIAL: If the instance is not present now,
2694 * but may be present in the future.
2695 */
2696
2697 static int
2698 sdprobe(dev_info_t *devi)
2699 {
2700 struct scsi_device *devp;
2701 int rval;
2702 int instance = ddi_get_instance(devi);
2703
2704 /*
2705 * if it wasn't for pln, sdprobe could actually be nulldev
2706 * in the "__fibre" case.
2707 */
2708 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2709 return (DDI_PROBE_DONTCARE);
2710 }
2711
2712 devp = ddi_get_driver_private(devi);
2713
2714 if (devp == NULL) {
2715 /* Ooops... nexus driver is mis-configured... */
2716 return (DDI_PROBE_FAILURE);
2717 }
2718
2719 if (ddi_get_soft_state(sd_state, instance) != NULL) {
2720 return (DDI_PROBE_PARTIAL);
2721 }
2722
2723 /*
2724 * Call the SCSA utility probe routine to see if we actually
2725 * have a target at this SCSI nexus.
2726 */
2727 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2728 case SCSIPROBE_EXISTS:
2729 switch (devp->sd_inq->inq_dtype) {
2730 case DTYPE_DIRECT:
2731 rval = DDI_PROBE_SUCCESS;
2732 break;
2733 case DTYPE_RODIRECT:
2734 /* CDs etc. Can be removable media */
2735 rval = DDI_PROBE_SUCCESS;
2736 break;
2737 case DTYPE_OPTICAL:
2738 /*
2739 * Rewritable optical driver HP115AA
2740 * Can also be removable media
2741 */
2742
2743 /*
2744 * Do not attempt to bind to DTYPE_OPTICAL if
2745 * pre solaris 9 sparc sd behavior is required
2746 *
2747 * If first time through and sd_dtype_optical_bind
2748 * has not been set in /etc/system check properties
2749 */
2750
2751 if (sd_dtype_optical_bind < 0) {
2752 sd_dtype_optical_bind = ddi_prop_get_int
2753 (DDI_DEV_T_ANY, devi, 0,
2754 "optical-device-bind", 1);
2755 }
2756
2757 if (sd_dtype_optical_bind == 0) {
2758 rval = DDI_PROBE_FAILURE;
2759 } else {
2760 rval = DDI_PROBE_SUCCESS;
2761 }
2762 break;
2763
2764 case DTYPE_NOTPRESENT:
2765 default:
2766 rval = DDI_PROBE_FAILURE;
2767 break;
2768 }
2769 break;
2770 default:
2771 rval = DDI_PROBE_PARTIAL;
2772 break;
2773 }
2774
2775 /*
2776 * This routine checks for resource allocation prior to freeing,
2777 * so it will take care of the "smart probing" case where a
2778 * scsi_probe() may or may not have been issued and will *not*
2779 * free previously-freed resources.
2780 */
2781 scsi_unprobe(devp);
2782 return (rval);
2783 }
2843 * prop_op - property operator
2844 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2845 * name - pointer to property name
2846 * valuep - pointer or address of the user buffer
2847 * lengthp - property length
2848 *
2849 * Return Code: DDI_PROP_SUCCESS
2850 * DDI_PROP_NOT_FOUND
2851 * DDI_PROP_UNDEFINED
2852 * DDI_PROP_NO_MEMORY
2853 * DDI_PROP_BUF_TOO_SMALL
2854 */
2855
2856 static int
2857 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2858 char *name, caddr_t valuep, int *lengthp)
2859 {
2860 struct sd_lun *un;
2861
2862 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2863 return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2864 name, valuep, lengthp));
2865
2866 return (cmlb_prop_op(un->un_cmlbhandle,
2867 dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2868 SDPART(dev), (void *)SD_PATH_DIRECT));
2869 }
2870
2871 /*
2872 * The following functions are for smart probing:
2873 * sd_scsi_probe_cache_init()
2874 * sd_scsi_probe_cache_fini()
2875 * sd_scsi_clear_probe_cache()
2876 * sd_scsi_probe_with_cache()
2877 */
2878
2879 /*
2880 * Function: sd_scsi_probe_cache_init
2881 *
2882 * Description: Initializes the probe response cache mutex and head pointer.
2883 *
2884 * Context: Kernel thread context
2885 */
2886
2887 static void
2888 sd_scsi_probe_cache_init(void)
3196 struct sd_lun *un;
3197
3198 ASSERT(ssc != NULL);
3199 un = ssc->ssc_un;
3200 ASSERT(un != NULL);
3201
3202 /*
3203 * Send a throwaway START UNIT command.
3204 *
3205 * If we fail on this, we don't care presently what precisely
3206 * is wrong. EMC's arrays will also fail this with a check
3207 * condition (0x2/0x4/0x3) if the device is "inactive," but
3208 * we don't want to fail the attach because it may become
3209 * "active" later.
3210 * We don't know if power condition is supported or not at
3211 * this stage, use START STOP bit.
3212 */
3213 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3214 SD_TARGET_START, SD_PATH_DIRECT);
3215
3216 if (status != 0) {
3217 if (status == EACCES)
3218 has_conflict = TRUE;
3219 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3220 }
3221
3222 /*
3223 * Send another INQUIRY command to the target. This is necessary for
3224 * non-removable media direct access devices because their INQUIRY data
3225 * may not be fully qualified until they are spun up (perhaps via the
3226 * START command above). Note: This seems to be needed for some
3227 * legacy devices only.) The INQUIRY command should succeed even if a
3228 * Reservation Conflict is present.
3229 */
3230 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3231
3232 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3233 != 0) {
3234 kmem_free(bufaddr, SUN_INQSIZE);
3235 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3236 return (EIO);
3237 }
3238
3817
3818 static int
3819 sd_process_sdconf_file(struct sd_lun *un)
3820 {
3821 char **config_list = NULL;
3822 uint_t nelements;
3823 char *vidptr;
3824 int vidlen;
3825 char *dnlist_ptr;
3826 char *dataname_ptr;
3827 char *dataname_lasts;
3828 int *data_list = NULL;
3829 uint_t data_list_len;
3830 int rval = SD_FAILURE;
3831 int i;
3832
3833 ASSERT(un != NULL);
3834
3835 /* Obtain the configuration list associated with the .conf file */
3836 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3837 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3838 &config_list, &nelements) != DDI_PROP_SUCCESS) {
3839 return (SD_FAILURE);
3840 }
3841
3842 /*
3843 * Compare vids in each duplet to the inquiry vid - if a match is
3844 * made, get the data value and update the soft state structure
3845 * accordingly.
3846 *
3847 * Each duplet should show as a pair of strings, return SD_FAILURE
3848 * otherwise.
3849 */
3850 if (nelements & 1) {
3851 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3852 "sd-config-list should show as pairs of strings.\n");
3853 if (config_list)
3854 ddi_prop_free(config_list);
3855 return (SD_FAILURE);
3856 }
3857
4181 }
4182 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4183 "min throttle set to %d\n", un->un_min_throttle);
4184 }
4185
4186 if (strcasecmp(name, "rmw-type") == 0) {
4187 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4188 un->un_f_rmw_type = val;
4189 } else {
4190 goto value_invalid;
4191 }
4192 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4193 "RMW type set to %d\n", un->un_f_rmw_type);
4194 }
4195
4196 if (strcasecmp(name, "physical-block-size") == 0) {
4197 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4198 ISP2(val) && val >= un->un_tgt_blocksize &&
4199 val >= un->un_sys_blocksize) {
4200 un->un_phy_blocksize = val;
4201 } else {
4202 goto value_invalid;
4203 }
4204 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4205 "physical block size set to %d\n", un->un_phy_blocksize);
4206 }
4207
4208 if (strcasecmp(name, "retries-victim") == 0) {
4209 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4210 un->un_victim_retry_count = val;
4211 } else {
4212 goto value_invalid;
4213 }
4214 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4215 "victim retry count set to %d\n",
4216 un->un_victim_retry_count);
4217 return;
4218 }
4219
4220 /*
4221 * Validate the throttle values.
4222 * If any of the numbers are invalid, set everything to defaults.
4223 */
4224 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4225 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4226 (un->un_min_throttle > un->un_throttle)) {
4227 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4949 } else {
4950 lbasize = un->un_sys_blocksize;
4951 }
4952 }
4953 pgeom_p->g_secsize = (unsigned short)lbasize;
4954
4955 /*
4956 * If the unit is a cd/dvd drive MODE SENSE page three
4957 * and MODE SENSE page four are reserved (see SBC spec
4958 * and MMC spec). To prevent soft errors just return
4959 * using the default LBA size.
4960 *
4961 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4962 * implement support for mode pages 3 and 4 return here to prevent
4963 * illegal requests on SATA drives.
4964 *
4965 * These pages are also reserved in SBC-2 and later. We assume SBC-2
4966 * or later for a direct-attached block device if the SCSI version is
4967 * at least SPC-3.
4968 */
4969
4970 if (ISCD(un) ||
4971 un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4972 (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4973 return (ret);
4974
4975 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4976
4977 /*
4978 * Retrieve MODE SENSE page 3 - Format Device Page
4979 */
4980 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4981 ssc = sd_ssc_init(un);
4982 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4983 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4984 if (status != 0) {
4985 SD_ERROR(SD_LOG_COMMON, un,
4986 "sd_get_physical_geometry: mode sense page 3 failed\n");
4987 goto page3_exit;
4988 }
4989
5291 }
5292
5293 if (capacity != 0) {
5294 un->un_blockcount = capacity;
5295 un->un_f_blockcount_is_valid = TRUE;
5296
5297 /*
5298 * The capacity has changed so update the errstats.
5299 */
5300 if (un->un_errstats != NULL) {
5301 struct sd_errstats *stp;
5302
5303 capacity *= un->un_sys_blocksize;
5304 stp = (struct sd_errstats *)un->un_errstats->ks_data;
5305 if (stp->sd_capacity.value.ui64 < capacity)
5306 stp->sd_capacity.value.ui64 = capacity;
5307 }
5308 }
5309 }
5310
5311
5312 /*
5313 * Function: sd_register_devid
5314 *
5315 * Description: This routine will obtain the device id information from the
5316 * target, obtain the serial number, and register the device
5317 * id with the ddi framework.
5318 *
5319 * Arguments: devi - the system's dev_info_t for the device.
5320 * un - driver soft state (unit) structure
5321 * reservation_flag - indicates if a reservation conflict
5322 * occurred during attach
5323 *
5324 * Context: Kernel Thread
5325 */
5326 static void
5327 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5328 {
5329 int rval = 0;
5330 uchar_t *inq80 = NULL;
5331 size_t inq80_len = MAX_INQUIRY_SIZE;
5332 size_t inq80_resid = 0;
5423 * then that takes precedence over the driver's determination
5424 * of the devid.
5425 *
5426 * NOTE: The reason this check is done here instead of at the beginning
5427 * of the function is to allow the code above to create the
5428 * 'inquiry-serial-no' property.
5429 */
5430 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5431 ASSERT(un->un_devid);
5432 un->un_f_devid_transport_defined = TRUE;
5433 goto cleanup; /* use devid registered by the transport */
5434 }
5435
5436 /*
5437 * This is the case of antiquated Sun disk drives that have the
5438 * FAB_DEVID property set in the disk_table. These drives
5439 * manage the devid's by storing them in last 2 available sectors
5440 * on the drive and have them fabricated by the ddi layer by calling
5441 * ddi_devid_init and passing the DEVID_FAB flag.
5442 */
5443 if (un->un_f_opt_fab_devid == TRUE) {
5444 /*
5445 * Depending on EINVAL isn't reliable, since a reserved disk
5446 * may result in invalid geometry, so check to make sure a
5447 * reservation conflict did not occur during attach.
5448 */
5449 if ((sd_get_devid(ssc) == EINVAL) &&
5450 (reservation_flag != SD_TARGET_IS_RESERVED)) {
5451 /*
5452 * The devid is invalid AND there is no reservation
5453 * conflict. Fabricate a new devid.
5454 */
5455 (void) sd_create_devid(ssc);
5456 }
5457
5458 /* Register the devid if it exists */
5459 if (un->un_devid != NULL) {
5460 (void) ddi_devid_register(SD_DEVINFO(un),
5461 un->un_devid);
5462 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5463 "sd_register_devid: Devid Fabricated\n");
5464 }
5465 goto cleanup;
5466 }
5467
5468 /* encode best devid possible based on data available */
5469 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5470 (char *)ddi_driver_name(SD_DEVINFO(un)),
5471 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5472 inq80, inq80_len - inq80_resid, inq83, inq83_len -
5473 inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5474
5475 /* devid successfully encoded, register devid */
5476 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5477
5478 } else {
5479 /*
5480 * Unable to encode a devid based on data available.
5481 * This is not a Sun qualified disk. Older Sun disk
5482 * drives that have the SD_FAB_DEVID property
5483 * set in the disk_table and non Sun qualified
5484 * disks are treated in the same manner. These
5485 * drives manage the devid's by storing them in
5486 * last 2 available sectors on the drive and
5487 * have them fabricated by the ddi layer by
5488 * calling ddi_devid_init and passing the
5489 * DEVID_FAB flag.
5490 * Create a fabricate devid only if there's no
5491 * fabricate devid existed.
5492 */
5493 if (sd_get_devid(ssc) == EINVAL) {
5494 (void) sd_create_devid(ssc);
5495 }
5496 un->un_f_opt_fab_devid = TRUE;
5497
5498 /* Register the devid if it exists */
5820
5821 switch (page_list[counter]) {
5822 case 0x00:
5823 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5824 break;
5825 case 0x80:
5826 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5827 break;
5828 case 0x81:
5829 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5830 break;
5831 case 0x82:
5832 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5833 break;
5834 case 0x83:
5835 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5836 break;
5837 case 0x86:
5838 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5839 break;
5840 case 0xB1:
5841 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5842 break;
5843 }
5844 counter++;
5845 }
5846
5847 } else {
5848 rval = -1;
5849
5850 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5851 "sd_check_vpd_page_support: This drive does not implement "
5852 "VPD pages.\n");
5853 }
5854
5855 kmem_free(page_list, page_length);
5856
5857 return (rval);
5858 }
5859
5860
5861 /*
5862 * Function: sd_setup_pm
5863 *
5864 * Description: Initialize Power Management on the device
5865 *
5866 * Context: Kernel Thread
5867 */
5868
5869 static void
5870 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5871 {
5872 uint_t log_page_size;
5873 uchar_t *log_page_data;
5874 int rval = 0;
5875 struct sd_lun *un;
5876
5877 ASSERT(ssc != NULL);
5878 un = ssc->ssc_un;
5879 ASSERT(un != NULL);
5880
5881 /*
5882 * Since we are called from attach, holding a mutex for
5883 * un is unnecessary. Because some of the routines called
5884 * from here require SD_MUTEX to not be held, assert this
5885 * right up front.
5886 */
5887 ASSERT(!mutex_owned(SD_MUTEX(un)));
5888 /*
5903 * the value of this property is bigger than 0.
5904 */
5905 if (un->un_f_pm_supported) {
5906 /*
5907 * not all devices have a motor, try it first.
5908 * some devices may return ILLEGAL REQUEST, some
5909 * will hang
5910 * The following START_STOP_UNIT is used to check if target
5911 * device has a motor.
5912 */
5913 un->un_f_start_stop_supported = TRUE;
5914
5915 if (un->un_f_power_condition_supported) {
5916 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5917 SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5918 SD_PATH_DIRECT);
5919 if (rval != 0) {
5920 un->un_f_power_condition_supported = FALSE;
5921 }
5922 }
5923 if (!un->un_f_power_condition_supported) {
5924 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5925 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5926 }
5927 if (rval != 0) {
5928 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5929 un->un_f_start_stop_supported = FALSE;
5930 }
5931
5932 /*
5933 * create pm properties anyways otherwise the parent can't
5934 * go to sleep
5935 */
5936 un->un_f_pm_is_enabled = TRUE;
5937 (void) sd_create_pm_components(devi, un);
5938
5939 /*
5940 * If it claims that log sense is supported, check it out.
5941 */
5942 if (un->un_f_log_sense_supported) {
6104 * fails. In the case of removable media, the start/stop
6105 * will fail if the media is not present.
6106 */
6107 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6108 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6109 mutex_enter(SD_MUTEX(un));
6110 un->un_power_level = SD_PM_STATE_ACTIVE(un);
6111 mutex_enter(&un->un_pm_mutex);
6112 /* Set to on and not busy. */
6113 un->un_pm_count = 0;
6114 } else {
6115 mutex_enter(SD_MUTEX(un));
6116 un->un_power_level = SD_PM_STATE_STOPPED(un);
6117 mutex_enter(&un->un_pm_mutex);
6118 /* Set to off. */
6119 un->un_pm_count = -1;
6120 }
6121 mutex_exit(&un->un_pm_mutex);
6122 mutex_exit(SD_MUTEX(un));
6123 }
6124
6125
6126 /*
6127 * Function: sd_ddi_suspend
6128 *
6129 * Description: Performs system power-down operations. This includes
6130 * setting the drive state to indicate its suspended so
6131 * that no new commands will be accepted. Also, wait for
6132 * all commands that are in transport or queued to a timer
6133 * for retry to complete. All timeout threads are cancelled.
6134 *
6135 * Return Code: DDI_FAILURE or DDI_SUCCESS
6136 *
6137 * Context: Kernel thread context
6138 */
6139
6140 static int
6141 sd_ddi_suspend(dev_info_t *devi)
6142 {
6143 struct sd_lun *un;
6144 clock_t wait_cmds_complete;
6145
6278
6279 if (un->un_retry_bp != NULL) {
6280 un->un_retry_bp->av_forw = un->un_waitq_headp;
6281 un->un_waitq_headp = un->un_retry_bp;
6282 if (un->un_waitq_tailp == NULL) {
6283 un->un_waitq_tailp = un->un_retry_bp;
6284 }
6285 un->un_retry_bp = NULL;
6286 un->un_retry_statp = NULL;
6287 }
6288 }
6289
6290 if (un->un_direct_priority_timeid != NULL) {
6291 timeout_id_t temp_id = un->un_direct_priority_timeid;
6292 un->un_direct_priority_timeid = NULL;
6293 mutex_exit(SD_MUTEX(un));
6294 (void) untimeout(temp_id);
6295 mutex_enter(SD_MUTEX(un));
6296 }
6297
6298 if (un->un_f_is_fibre == TRUE) {
6299 /*
6300 * Remove callbacks for insert and remove events
6301 */
6302 if (un->un_insert_event != NULL) {
6303 mutex_exit(SD_MUTEX(un));
6304 (void) ddi_remove_event_handler(un->un_insert_cb_id);
6305 mutex_enter(SD_MUTEX(un));
6306 un->un_insert_event = NULL;
6307 }
6308
6309 if (un->un_remove_event != NULL) {
6310 mutex_exit(SD_MUTEX(un));
6311 (void) ddi_remove_event_handler(un->un_remove_cb_id);
6312 mutex_enter(SD_MUTEX(un));
6313 un->un_remove_event = NULL;
6314 }
6315 }
6316
6317 mutex_exit(SD_MUTEX(un));
6318
6319 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6320
6321 return (DDI_SUCCESS);
6322 }
6323
6324
6325 /*
6326 * Function: sd_ddi_resume
6327 *
6328 * Description: Performs system power-up operations..
6329 *
6330 * Return Code: DDI_SUCCESS
6331 * DDI_FAILURE
6332 *
6333 * Context: Kernel thread context
6334 */
6335
6336 static int
6337 sd_ddi_resume(dev_info_t *devi)
6338 {
6378 */
6379 if (un->un_f_attach_spinup) {
6380 mutex_exit(SD_MUTEX(un));
6381 (void) pm_raise_power(SD_DEVINFO(un), 0,
6382 SD_PM_STATE_ACTIVE(un));
6383 mutex_enter(SD_MUTEX(un));
6384 }
6385
6386 /*
6387 * Don't broadcast to the suspend cv and therefore possibly
6388 * start I/O until after power has been restored.
6389 */
6390 cv_broadcast(&un->un_suspend_cv);
6391 cv_broadcast(&un->un_state_cv);
6392
6393 /* restart thread */
6394 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6395 scsi_watch_resume(un->un_swr_token);
6396 }
6397
6398 #if (defined(__fibre))
6399 if (un->un_f_is_fibre == TRUE) {
6400 /*
6401 * Add callbacks for insert and remove events
6402 */
6403 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6404 sd_init_event_callbacks(un);
6405 }
6406 }
6407 #endif
6408
6409 /*
6410 * Transport any pending commands to the target.
6411 *
6412 * If this is a low-activity device commands in queue will have to wait
6413 * until new commands come in, which may take awhile. Also, we
6414 * specifically don't check un_ncmds_in_transport because we know that
6415 * there really are no commands in progress after the unit was
6416 * suspended and we could have reached the throttle level, been
6417 * suspended, and have no new commands coming in for awhile. Highly
6418 * unlikely, but so is the low-activity disk scenario.
6419 */
6420 ddi_xbuf_dispatch(un->un_xbuf_attr);
6421
6422 sd_start_cmds(un, NULL);
6423 mutex_exit(SD_MUTEX(un));
6424
6425 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6426
6427 return (DDI_SUCCESS);
6428 }
6429
7075
7076
7077
7078 /*
7079 * Function: sdattach
7080 *
7081 * Description: Driver's attach(9e) entry point function.
7082 *
7083 * Arguments: devi - opaque device info handle
7084 * cmd - attach type
7085 *
7086 * Return Code: DDI_SUCCESS
7087 * DDI_FAILURE
7088 *
7089 * Context: Kernel thread context
7090 */
7091
7092 static int
7093 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7094 {
7095 switch (cmd) {
7096 case DDI_ATTACH:
7097 return (sd_unit_attach(devi));
7098 case DDI_RESUME:
7099 return (sd_ddi_resume(devi));
7100 default:
7101 break;
7102 }
7103 return (DDI_FAILURE);
7104 }
7105
7106
7107 /*
7108 * Function: sddetach
7109 *
7110 * Description: Driver's detach(9E) entry point function.
7111 *
7112 * Arguments: devi - opaque device info handle
7113 * cmd - detach type
7114 *
7115 * Return Code: DDI_SUCCESS
7116 * DDI_FAILURE
7117 *
7118 * Context: Kernel thread context
7119 */
7120
7121 static int
7122 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7123 {
7124 switch (cmd) {
7125 case DDI_DETACH:
7126 return (sd_unit_detach(devi));
7127 case DDI_SUSPEND:
7128 return (sd_ddi_suspend(devi));
7129 default:
7130 break;
7131 }
7132 return (DDI_FAILURE);
7133 }
7134
7135
7136 /*
7137 * Function: sd_sync_with_callback
7138 *
7139 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7140 * state while the callback routine is active.
7141 *
7142 * Arguments: un: softstate structure for the instance
7143 *
7144 * Context: Kernel thread context
7145 */
7146
7147 static void
7148 sd_sync_with_callback(struct sd_lun *un)
7149 {
7150 ASSERT(un != NULL);
7151
7152 mutex_enter(SD_MUTEX(un));
7153
7154 ASSERT(un->un_in_callback >= 0);
7155
7156 while (un->un_in_callback > 0) {
7157 mutex_exit(SD_MUTEX(un));
7158 delay(2);
7159 mutex_enter(SD_MUTEX(un));
7160 }
7161
7162 mutex_exit(SD_MUTEX(un));
7163 }
7164
7165 /*
7166 * Function: sd_unit_attach
7167 *
7168 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7169 * the soft state structure for the device and performs
7170 * all necessary structure and device initializations.
7171 *
7172 * Arguments: devi: the system's dev_info_t for the device.
7173 *
7174 * Return Code: DDI_SUCCESS if attach is successful.
7175 * DDI_FAILURE if any part of the attach fails.
7176 *
7177 * Context: Called at attach(9e) time for the DDI_ATTACH flag.
7178 * Kernel thread context only. Can sleep.
7179 */
7180
7181 static int
7182 sd_unit_attach(dev_info_t *devi)
7183 {
7184 struct scsi_device *devp;
7185 struct sd_lun *un;
7186 char *variantp;
7187 char name_str[48];
7188 int reservation_flag = SD_TARGET_IS_UNRESERVED;
7189 int instance;
7190 int rval;
7191 int wc_enabled;
7192 int wc_changeable;
7193 int tgt;
7194 uint64_t capacity;
7195 uint_t lbasize = 0;
7196 dev_info_t *pdip = ddi_get_parent(devi);
7197 int offbyone = 0;
7198 int geom_label_valid = 0;
7199 sd_ssc_t *ssc;
7200 int status;
7201 struct sd_fm_internal *sfip = NULL;
7202 int max_xfer_size;
7203
7204 /*
7205 * Retrieve the target driver's private data area. This was set
7206 * up by the HBA.
7207 */
7208 devp = ddi_get_driver_private(devi);
7209
7210 /*
7211 * Retrieve the target ID of the device.
7212 */
7213 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7214 SCSI_ADDR_PROP_TARGET, -1);
7215
7216 /*
7217 * Since we have no idea what state things were left in by the last
7218 * user of the device, set up some 'default' settings, ie. turn 'em
7219 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7220 * Do this before the scsi_probe, which sends an inquiry.
7221 * This is a fix for bug (4430280).
7222 * Of special importance is wide-xfer. The drive could have been left
7223 * in wide transfer mode by the last driver to communicate with it,
7278 *
7279 * We rely upon this memory being set to all zeroes by
7280 * ddi_soft_state_zalloc(). We assume that any member of the
7281 * soft state structure that is not explicitly initialized by
7282 * this routine will have a value of zero.
7283 */
7284 instance = ddi_get_instance(devp->sd_dev);
7285 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7286 goto probe_failed;
7287 }
7288
7289 /*
7290 * Retrieve a pointer to the newly-allocated soft state.
7291 *
7292 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7293 * was successful, unless something has gone horribly wrong and the
7294 * ddi's soft state internals are corrupt (in which case it is
7295 * probably better to halt here than just fail the attach....)
7296 */
7297 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7298 panic("sd_unit_attach: NULL soft state on instance:0x%x",
7299 instance);
7300 /*NOTREACHED*/
7301 }
7302
7303 /*
7304 * Link the back ptr of the driver soft state to the scsi_device
7305 * struct for this lun.
7306 * Save a pointer to the softstate in the driver-private area of
7307 * the scsi_device struct.
7308 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7309 * we first set un->un_sd below.
7310 */
7311 un->un_sd = devp;
7312 devp->sd_private = (opaque_t)un;
7313
7314 /*
7315 * The following must be after devp is stored in the soft state struct.
7316 */
7317 #ifdef SDDEBUG
7318 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7324 * Set up the device type and node type (for the minor nodes).
7325 * By default we assume that the device can at least support the
7326 * Common Command Set. Call it a CD-ROM if it reports itself
7327 * as a RODIRECT device.
7328 */
7329 switch (devp->sd_inq->inq_dtype) {
7330 case DTYPE_RODIRECT:
7331 un->un_node_type = DDI_NT_CD_CHAN;
7332 un->un_ctype = CTYPE_CDROM;
7333 break;
7334 case DTYPE_OPTICAL:
7335 un->un_node_type = DDI_NT_BLOCK_CHAN;
7336 un->un_ctype = CTYPE_ROD;
7337 break;
7338 default:
7339 un->un_node_type = DDI_NT_BLOCK_CHAN;
7340 un->un_ctype = CTYPE_CCS;
7341 break;
7342 }
7343
7344 /*
7345 * Try to read the interconnect type from the HBA.
7346 *
7347 * Note: This driver is currently compiled as two binaries, a parallel
7348 * scsi version (sd) and a fibre channel version (ssd). All functional
7349 * differences are determined at compile time. In the future a single
7350 * binary will be provided and the interconnect type will be used to
7351 * differentiate between fibre and parallel scsi behaviors. At that time
7352 * it will be necessary for all fibre channel HBAs to support this
7353 * property.
7354 *
7355 * set un_f_is_fiber to TRUE ( default fiber )
7356 */
7357 un->un_f_is_fibre = TRUE;
7358 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7359 case INTERCONNECT_SSA:
7360 un->un_interconnect_type = SD_INTERCONNECT_SSA;
7361 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7362 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7363 break;
7364 case INTERCONNECT_PARALLEL:
7365 un->un_f_is_fibre = FALSE;
7366 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7367 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7368 "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7369 break;
7370 case INTERCONNECT_SAS:
7371 un->un_f_is_fibre = FALSE;
7372 un->un_interconnect_type = SD_INTERCONNECT_SAS;
7373 un->un_node_type = DDI_NT_BLOCK_SAS;
7374 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7375 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7376 break;
7377 case INTERCONNECT_SATA:
7378 un->un_f_is_fibre = FALSE;
7379 un->un_interconnect_type = SD_INTERCONNECT_SATA;
7380 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7381 "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7382 break;
7383 case INTERCONNECT_FIBRE:
7384 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7385 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7386 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7387 break;
7388 case INTERCONNECT_FABRIC:
7389 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7390 un->un_node_type = DDI_NT_BLOCK_FABRIC;
7391 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7392 "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7393 break;
7394 default:
7395 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7396 /*
7397 * The HBA does not support the "interconnect-type" property
7398 * (or did not provide a recognized type).
7399 *
7400 * Note: This will be obsoleted when a single fibre channel
7401 * and parallel scsi driver is delivered. In the meantime the
7402 * interconnect type will be set to the platform default.If that
7403 * type is not parallel SCSI, it means that we should be
7404 * assuming "ssd" semantics. However, here this also means that
7405 * the FC HBA is not supporting the "interconnect-type" property
7406 * like we expect it to, so log this occurrence.
7407 */
7408 un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7409 if (!SD_IS_PARALLEL_SCSI(un)) {
7410 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7411 "sd_unit_attach: un:0x%p Assuming "
7412 "INTERCONNECT_FIBRE\n", un);
7413 } else {
7414 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7415 "sd_unit_attach: un:0x%p Assuming "
7416 "INTERCONNECT_PARALLEL\n", un);
7417 un->un_f_is_fibre = FALSE;
7418 }
7419 #else
7420 /*
7421 * Note: This source will be implemented when a single fibre
7422 * channel and parallel scsi driver is delivered. The default
7423 * will be to assume that if a device does not support the
7424 * "interconnect-type" property it is a parallel SCSI HBA and
7425 * we will set the interconnect type for parallel scsi.
7426 */
7427 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7428 un->un_f_is_fibre = FALSE;
7429 #endif
7430 break;
7431 }
7432
7433 if (un->un_f_is_fibre == TRUE) {
7434 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7435 SCSI_VERSION_3) {
7436 switch (un->un_interconnect_type) {
7437 case SD_INTERCONNECT_FIBRE:
7438 case SD_INTERCONNECT_SSA:
7439 un->un_node_type = DDI_NT_BLOCK_WWN;
7440 break;
7441 default:
7442 break;
7443 }
7444 }
7445 }
7446
7447 /*
7448 * Initialize the Request Sense command for the target
7449 */
7450 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7451 goto alloc_rqs_failed;
7452 }
7453
7454 /*
7455 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7456 * with separate binary for sd and ssd.
7457 *
7458 * x86 has 1 binary, un_retry_count is set base on connection type.
7459 * The hardcoded values will go away when Sparc uses 1 binary
7460 * for sd and ssd. This hardcoded values need to match
7461 * SD_RETRY_COUNT in sddef.h
7462 * The value used is base on interconnect type.
7463 * fibre = 3, parallel = 5
7464 */
7465 #if defined(__i386) || defined(__amd64)
7466 un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7467 #else
7468 un->un_retry_count = SD_RETRY_COUNT;
7469 #endif
7470
7471 /*
7472 * Set the per disk retry count to the default number of retries
7473 * for disks and CDROMs. This value can be overridden by the
7474 * disk property list or an entry in sd.conf.
7475 */
7476 un->un_notready_retry_count =
7477 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7478 : DISK_NOT_READY_RETRY_COUNT(un);
7479
7480 /*
7481 * Set the busy retry count to the default value of un_retry_count.
7482 * This can be overridden by entries in sd.conf or the device
7483 * config table.
7484 */
7485 un->un_busy_retry_count = un->un_retry_count;
7486
7487 /*
7488 * Init the reset threshold for retries. This number determines
7489 * how many retries must be performed before a reset can be issued
7490 * (for certain error conditions). This can be overridden by entries
7491 * in sd.conf or the device config table.
7492 */
7493 un->un_reset_retry_count = (un->un_retry_count / 2);
7494
7495 /*
7496 * Set the victim_retry_count to the default un_retry_count
7497 */
7498 un->un_victim_retry_count = (2 * un->un_retry_count);
7499
7500 /*
7501 * Set the reservation release timeout to the default value of
7502 * 5 seconds. This can be overridden by entries in ssd.conf or the
7503 * device config table.
7504 */
7505 un->un_reserve_release_time = 5;
7506
7507 /*
7508 * Set up the default maximum transfer size. Note that this may
7509 * get updated later in the attach, when setting up default wide
7510 * operations for disks.
7511 */
7512 #if defined(__i386) || defined(__amd64)
7513 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7514 un->un_partial_dma_supported = 1;
7515 #else
7516 un->un_max_xfer_size = (uint_t)maxphys;
7517 #endif
7518
7519 /*
7520 * Get "allow bus device reset" property (defaults to "enabled" if
7521 * the property was not defined). This is to disable bus resets for
7522 * certain kinds of error recovery. Note: In the future when a run-time
7523 * fibre check is available the soft state flag should default to
7524 * enabled.
7525 */
7526 if (un->un_f_is_fibre == TRUE) {
7527 un->un_f_allow_bus_device_reset = TRUE;
7528 } else {
7529 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7530 "allow-bus-device-reset", 1) != 0) {
7531 un->un_f_allow_bus_device_reset = TRUE;
7532 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7533 "sd_unit_attach: un:0x%p Bus device reset "
7534 "enabled\n", un);
7535 } else {
7536 un->un_f_allow_bus_device_reset = FALSE;
7537 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7538 "sd_unit_attach: un:0x%p Bus device reset "
7539 "disabled\n", un);
7540 }
7541 }
7542
7543 /*
7544 * Check if this is an ATAPI device. ATAPI devices use Group 1
7545 * Read/Write commands and Group 2 Mode Sense/Select commands.
7546 *
7547 * Note: The "obsolete" way of doing this is to check for the "atapi"
7548 * property. The new "variant" property with a value of "atapi" has been
7549 * introduced so that future 'variants' of standard SCSI behavior (like
7550 * atapi) could be specified by the underlying HBA drivers by supplying
7551 * a new value for the "variant" property, instead of having to define a
7552 * new property.
7553 */
7554 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7555 un->un_f_cfg_is_atapi = TRUE;
7556 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7557 "sd_unit_attach: un:0x%p Atapi device\n", un);
7558 }
7559 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7560 &variantp) == DDI_PROP_SUCCESS) {
7561 if (strcmp(variantp, "atapi") == 0) {
7562 un->un_f_cfg_is_atapi = TRUE;
7563 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7564 "sd_unit_attach: un:0x%p Atapi device\n", un);
7565 }
7566 ddi_prop_free(variantp);
7567 }
7568
7569 un->un_cmd_timeout = SD_IO_TIME;
7570
7571 un->un_busy_timeout = SD_BSY_TIMEOUT;
7572
7573 /* Info on current states, statuses, etc. (Updated frequently) */
7574 un->un_state = SD_STATE_NORMAL;
7575 un->un_last_state = SD_STATE_NORMAL;
7576
7577 /* Control & status info for command throttling */
7578 un->un_throttle = sd_max_throttle;
7579 un->un_saved_throttle = sd_max_throttle;
7580 un->un_min_throttle = sd_min_throttle;
7581
7582 if (un->un_f_is_fibre == TRUE) {
7583 un->un_f_use_adaptive_throttle = TRUE;
7584 } else {
7585 un->un_f_use_adaptive_throttle = FALSE;
7586 }
7587
7588 /* Removable media support. */
7589 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7590 un->un_mediastate = DKIO_NONE;
7591 un->un_specified_mediastate = DKIO_NONE;
7592
7593 /* CVs for suspend/resume (PM or DR) */
7594 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
7595 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7596
7597 /* Power management support. */
7598 un->un_power_level = SD_SPINDLE_UNINIT;
7599
7600 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL);
7601 un->un_f_wcc_inprog = 0;
7602
7603 /*
7604 * The open/close semaphore is used to serialize threads executing
7605 * in the driver's open & close entry point routines for a given
7606 * instance.
7607 */
7610 /*
7611 * The conf file entry and softstate variable is a forceful override,
7612 * meaning a non-zero value must be entered to change the default.
7613 */
7614 un->un_f_disksort_disabled = FALSE;
7615 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7616 un->un_f_enable_rmw = FALSE;
7617
7618 /*
7619 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7620 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7621 */
7622 un->un_f_mmc_gesn_polling = TRUE;
7623
7624 /*
7625 * physical sector size defaults to DEV_BSIZE currently. We can
7626 * override this value via the driver configuration file so we must
7627 * set it before calling sd_read_unit_properties().
7628 */
7629 un->un_phy_blocksize = DEV_BSIZE;
7630
7631 /*
7632 * Retrieve the properties from the static driver table or the driver
7633 * configuration file (.conf) for this unit and update the soft state
7634 * for the device as needed for the indicated properties.
7635 * Note: the property configuration needs to occur here as some of the
7636 * following routines may have dependencies on soft state flags set
7637 * as part of the driver property configuration.
7638 */
7639 sd_read_unit_properties(un);
7640 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7641 "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7642
7643 /*
7644 * Only if a device has "hotpluggable" property, it is
7645 * treated as hotpluggable device. Otherwise, it is
7646 * regarded as non-hotpluggable one.
7647 */
7648 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7649 -1) != -1) {
7650 un->un_f_is_hotpluggable = TRUE;
7651 }
7652
7653 /*
7654 * set unit's attributes(flags) according to "hotpluggable" and
7655 * RMB bit in INQUIRY data.
7656 */
7657 sd_set_unit_attributes(un, devi);
7658
7659 /*
7660 * By default, we mark the capacity, lbasize, and geometry
7661 * as invalid. Only if we successfully read a valid capacity
7707 * routines that send commands to the unit (either polled or via
7708 * sd_send_scsi_cmd).
7709 *
7710 * Note: This is a critical sequence that needs to be maintained:
7711 * 1) Instantiate the kstats here, before any routines using the
7712 * iopath (i.e. sd_send_scsi_cmd).
7713 * 2) Instantiate and initialize the partition stats
7714 * (sd_set_pstats).
7715 * 3) Initialize the error stats (sd_set_errstats), following
7716 * sd_validate_geometry(),sd_register_devid(),
7717 * and sd_cache_control().
7718 */
7719
7720 un->un_stats = kstat_create(sd_label, instance,
7721 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7722 if (un->un_stats != NULL) {
7723 un->un_stats->ks_lock = SD_MUTEX(un);
7724 kstat_install(un->un_stats);
7725 }
7726 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7727 "sd_unit_attach: un:0x%p un_stats created\n", un);
7728
7729 sd_create_errstats(un, instance);
7730 if (un->un_errstats == NULL) {
7731 goto create_errstats_failed;
7732 }
7733 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7734 "sd_unit_attach: un:0x%p errstats created\n", un);
7735
7736 /*
7737 * The following if/else code was relocated here from below as part
7738 * of the fix for bug (4430280). However with the default setup added
7739 * on entry to this routine, it's no longer absolutely necessary for
7740 * this to be before the call to sd_spin_up_unit.
7741 */
7742 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7743 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7744 (devp->sd_inq->inq_ansi == 5)) &&
7745 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7746
7747 /*
7748 * If tagged queueing is supported by the target
7749 * and by the host adapter then we will enable it
7750 */
7751 un->un_tagflags = 0;
7752 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7753 (un->un_f_arq_enabled == TRUE)) {
7754 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7755 1, 1) == 1) {
7756 un->un_tagflags = FLAG_STAG;
7757 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7758 "sd_unit_attach: un:0x%p tag queueing "
7759 "enabled\n", un);
7760 } else if (scsi_ifgetcap(SD_ADDRESS(un),
7761 "untagged-qing", 0) == 1) {
7762 un->un_f_opt_queueing = TRUE;
7763 un->un_saved_throttle = un->un_throttle =
7764 min(un->un_throttle, 3);
7765 } else {
7766 un->un_f_opt_queueing = FALSE;
7767 un->un_saved_throttle = un->un_throttle = 1;
7768 }
7769 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7770 == 1) && (un->un_f_arq_enabled == TRUE)) {
7771 /* The Host Adapter supports internal queueing. */
7772 un->un_f_opt_queueing = TRUE;
7773 un->un_saved_throttle = un->un_throttle =
7774 min(un->un_throttle, 3);
7775 } else {
7776 un->un_f_opt_queueing = FALSE;
7777 un->un_saved_throttle = un->un_throttle = 1;
7778 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7779 "sd_unit_attach: un:0x%p no tag queueing\n", un);
7780 }
7781
7782 /*
7783 * Enable large transfers for SATA/SAS drives
7784 */
7785 if (SD_IS_SERIAL(un)) {
7786 un->un_max_xfer_size =
7787 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7788 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7789 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7790 "sd_unit_attach: un:0x%p max transfer "
7791 "size=0x%x\n", un, un->un_max_xfer_size);
7792
7793 }
7794
7795 /* Setup or tear down default wide operations for disks */
7796
7797 /*
7798 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7799 * and "ssd_max_xfer_size" to exist simultaneously on the same
7800 * system and be set to different values. In the future this
7801 * code may need to be updated when the ssd module is
7802 * obsoleted and removed from the system. (4299588)
7803 */
7804 if (SD_IS_PARALLEL_SCSI(un) &&
7805 (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7806 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7807 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7808 1, 1) == 1) {
7809 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7810 "sd_unit_attach: un:0x%p Wide Transfer "
7811 "enabled\n", un);
7812 }
7813
7814 /*
7815 * If tagged queuing has also been enabled, then
7816 * enable large xfers
7817 */
7818 if (un->un_saved_throttle == sd_max_throttle) {
7819 un->un_max_xfer_size =
7820 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7821 sd_max_xfer_size, SD_MAX_XFER_SIZE);
7822 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7823 "sd_unit_attach: un:0x%p max transfer "
7824 "size=0x%x\n", un, un->un_max_xfer_size);
7825 }
7826 } else {
7827 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7828 0, 1) == 1) {
7829 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7830 "sd_unit_attach: un:0x%p "
7831 "Wide Transfer disabled\n", un);
7832 }
7833 }
7834 } else {
7835 un->un_tagflags = FLAG_STAG;
7836 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7837 devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7838 }
7839
7840 /*
7841 * If this target supports LUN reset, try to enable it.
7842 */
7843 if (un->un_f_lun_reset_enabled) {
7844 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7845 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7846 "un:0x%p lun_reset capability set\n", un);
7847 } else {
7848 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7849 "un:0x%p lun-reset capability not set\n", un);
7850 }
7851 }
7852
7853 /*
7854 * Adjust the maximum transfer size. This is to fix
7855 * the problem of partial DMA support on SPARC. Some
7856 * HBA driver, like aac, has very small dma_attr_maxxfer
7857 * size, which requires partial DMA support on SPARC.
7858 * In the future the SPARC pci nexus driver may solve
7859 * the problem instead of this fix.
7860 */
7861 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7862 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7863 /* We need DMA partial even on sparc to ensure sddump() works */
7864 un->un_max_xfer_size = max_xfer_size;
7865 if (un->un_partial_dma_supported == 0)
7866 un->un_partial_dma_supported = 1;
7867 }
7868 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7869 DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7870 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7871 un->un_max_xfer_size) == 1) {
7872 un->un_buf_breakup_supported = 1;
7873 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7874 "un:0x%p Buf breakup enabled\n", un);
7875 }
7876 }
7877
7878 /*
7879 * Set PKT_DMA_PARTIAL flag.
7880 */
7881 if (un->un_partial_dma_supported == 1) {
7882 un->un_pkt_flags = PKT_DMA_PARTIAL;
7883 } else {
7884 un->un_pkt_flags = 0;
7885 }
7886
7887 /* Initialize sd_ssc_t for internal uscsi commands */
7888 ssc = sd_ssc_init(un);
7889 scsi_fm_init(devp);
7890
7891 /*
7892 * Allocate memory for SCSI FMA stuffs.
7893 */
7894 un->un_fm_private =
7895 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7896 sfip = (struct sd_fm_internal *)un->un_fm_private;
7897 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7898 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7899 sfip->fm_ssc.ssc_un = un;
7900
7901 if (ISCD(un) ||
7902 un->un_f_has_removable_media ||
7903 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7904 /*
7905 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7906 * Their log are unchanged.
7907 */
7908 sfip->fm_log_level = SD_FM_LOG_NSUP;
7909 } else {
7910 /*
7911 * If enter here, it should be non-CDROM and FM-capable
7912 * device, and it will not keep the old scsi_log as before
7913 * in /var/adm/messages. However, the property
7914 * "fm-scsi-log" will control whether the FM telemetry will
7915 * be logged in /var/adm/messages.
7916 */
7917 int fm_scsi_log;
7918 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7919 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7920
7921 if (fm_scsi_log)
7922 sfip->fm_log_level = SD_FM_LOG_EREPORT;
7923 else
7924 sfip->fm_log_level = SD_FM_LOG_SILENT;
7925 }
7926
7927 /*
7928 * At this point in the attach, we have enough info in the
7929 * soft state to be able to issue commands to the target.
7930 *
7931 * All command paths used below MUST issue their commands as
7932 * SD_PATH_DIRECT. This is important as intermediate layers
7933 * are not all initialized yet (such as PM).
7934 */
7935
7936 /*
7937 * Send a TEST UNIT READY command to the device. This should clear
7938 * any outstanding UNIT ATTENTION that may be present.
7939 *
7940 * Note: Don't check for success, just track if there is a reservation,
7941 * this is a throw away command to clear any unit attentions.
7942 *
7943 * Note: This MUST be the first command issued to the target during
7944 * attach to ensure power on UNIT ATTENTIONS are cleared.
7945 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7946 * with attempts at spinning up a device with no media.
7947 */
7948 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7949 if (status != 0) {
7950 if (status == EACCES)
7951 reservation_flag = SD_TARGET_IS_RESERVED;
7952 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7953 }
7954
7955 /*
7979 if (capacity > DK_MAX_BLOCKS) {
7980 #ifdef _LP64
7981 if ((capacity + 1) >
7982 SD_GROUP1_MAX_ADDRESS) {
7983 /*
7984 * Enable descriptor format
7985 * sense data so that we can
7986 * get 64 bit sense data
7987 * fields.
7988 */
7989 sd_enable_descr_sense(ssc);
7990 }
7991 #else
7992 /* 32-bit kernels can't handle this */
7993 scsi_log(SD_DEVINFO(un),
7994 sd_label, CE_WARN,
7995 "disk has %llu blocks, which "
7996 "is too large for a 32-bit "
7997 "kernel", capacity);
7998
7999 #if defined(__i386) || defined(__amd64)
8000 /*
8001 * 1TB disk was treated as (1T - 512)B
8002 * in the past, so that it might have
8003 * valid VTOC and solaris partitions,
8004 * we have to allow it to continue to
8005 * work.
8006 */
8007 if (capacity -1 > DK_MAX_BLOCKS)
8008 #endif
8009 goto spinup_failed;
8010 #endif
8011 }
8012
8013 /*
8014 * Here it's not necessary to check the case:
8015 * the capacity of the device is bigger than
8016 * what the max hba cdb can support. Because
8017 * sd_send_scsi_READ_CAPACITY will retrieve
8018 * the capacity by sending USCSI command, which
8019 * is constrained by the max hba cdb. Actually,
8020 * sd_send_scsi_READ_CAPACITY will return
8021 * EINVAL when using bigger cdb than required
8022 * cdb length. Will handle this case in
8023 * "case EINVAL".
8024 */
8025
8026 /*
8027 * The following relies on
8028 * sd_send_scsi_READ_CAPACITY never
8053 goto spinup_failed;
8054 case EACCES:
8055 /*
8056 * Should never get here if the spin-up
8057 * succeeded, but code it in anyway.
8058 * From here, just continue with the attach...
8059 */
8060 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8061 "sd_unit_attach: un:0x%p "
8062 "sd_send_scsi_READ_CAPACITY "
8063 "returned reservation conflict\n", un);
8064 reservation_flag = SD_TARGET_IS_RESERVED;
8065 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8066 break;
8067 default:
8068 /*
8069 * Likewise, should never get here if the
8070 * spin-up succeeded. Just continue with
8071 * the attach...
8072 */
8073 if (status == EIO)
8074 sd_ssc_assessment(ssc,
8075 SD_FMT_STATUS_CHECK);
8076 else
8077 sd_ssc_assessment(ssc,
8078 SD_FMT_IGNORE);
8079 break;
8080 }
8081 break;
8082 case EACCES:
8083 /*
8084 * Device is reserved by another host. In this case
8085 * we could not spin it up or read the capacity, but
8086 * we continue with the attach anyway.
8087 */
8088 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8089 "sd_unit_attach: un:0x%p spin-up reservation "
8090 "conflict.\n", un);
8091 reservation_flag = SD_TARGET_IS_RESERVED;
8092 break;
8093 default:
8094 /* Fail the attach if the spin-up failed. */
8095 SD_INFO(SD_LOG_ATTACH_DETACH, un,
8096 "sd_unit_attach: un:0x%p spin-up failed.", un);
8097 goto spinup_failed;
8098 }
8108
8109 /*
8110 * Add a zero-length attribute to tell the world we support
8111 * kernel ioctls (for layered drivers)
8112 */
8113 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8114 DDI_KERNEL_IOCTL, NULL, 0);
8115
8116 /*
8117 * Add a boolean property to tell the world we support
8118 * the B_FAILFAST flag (for layered drivers)
8119 */
8120 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8121 "ddi-failfast-supported", NULL, 0);
8122
8123 /*
8124 * Initialize power management
8125 */
8126 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8127 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8128 sd_setup_pm(ssc, devi);
8129 if (un->un_f_pm_is_enabled == FALSE) {
8130 /*
8131 * For performance, point to a jump table that does
8132 * not include pm.
8133 * The direct and priority chains don't change with PM.
8134 *
8135 * Note: this is currently done based on individual device
8136 * capabilities. When an interface for determining system
8137 * power enabled state becomes available, or when additional
8138 * layers are added to the command chain, these values will
8139 * have to be re-evaluated for correctness.
8140 */
8141 if (un->un_f_non_devbsize_supported) {
8142 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8143 } else {
8144 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8145 }
8146 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8147 }
8148
8149 /*
8150 * This property is set to 0 by HA software to avoid retries
8151 * on a reserved disk. (The preferred property name is
8152 * "retry-on-reservation-conflict") (1189689)
8153 *
8154 * Note: The use of a global here can have unintended consequences. A
8155 * per instance variable is preferable to match the capabilities of
8156 * different underlying hba's (4402600)
8157 */
8158 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8159 DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8160 sd_retry_on_reservation_conflict);
8161 if (sd_retry_on_reservation_conflict != 0) {
8162 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8163 devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8164 sd_retry_on_reservation_conflict);
8165 }
8166
8167 /* Set up options for QFULL handling. */
8168 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8169 "qfull-retries", -1)) != -1) {
8170 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8171 rval, 1);
8172 }
8173 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8174 "qfull-retry-interval", -1)) != -1) {
8175 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8176 rval, 1);
8177 }
8178
8179 /*
8180 * This just prints a message that announces the existence of the
8181 * device. The message is always printed in the system logfile, but
8182 * only appears on the console if the system is booted with the
8183 * -v (verbose) argument.
8184 */
8185 ddi_report_dev(devi);
8186
8187 un->un_mediastate = DKIO_NONE;
8188
8189 /*
8190 * Check Block Device Characteristics VPD.
8191 */
8192 sd_check_bdc_vpd(ssc);
8193
8194 /*
8195 * Check whether the drive is in emulation mode.
8196 */
8197 sd_check_emulation_mode(ssc);
8198
8199 cmlb_alloc_handle(&un->un_cmlbhandle);
8200
8201 #if defined(__i386) || defined(__amd64)
8202 /*
8203 * On x86, compensate for off-by-1 legacy error
8204 */
8205 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8206 (lbasize == un->un_sys_blocksize))
8207 offbyone = CMLB_OFF_BY_ONE;
8208 #endif
8209
8210 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8211 VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8212 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8213 un->un_node_type, offbyone, un->un_cmlbhandle,
8214 (void *)SD_PATH_DIRECT) != 0) {
8215 goto cmlb_attach_failed;
8216 }
8217
8218
8219 /*
8220 * Read and validate the device's geometry (ie, disk label)
8221 * A new unformatted drive will not have a valid geometry, but
8222 * the driver needs to successfully attach to this device so
8223 * the drive can be formatted via ioctls.
8224 */
8225 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8226 (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8227
8228 mutex_enter(SD_MUTEX(un));
8229
8230 /*
8231 * Read and initialize the devid for the unit.
8232 */
8233 if (un->un_f_devid_supported) {
8234 sd_register_devid(ssc, devi, reservation_flag);
8235 }
8236 mutex_exit(SD_MUTEX(un));
8237
8238 #if (defined(__fibre))
8239 /*
8240 * Register callbacks for fibre only. You can't do this solely
8241 * on the basis of the devid_type because this is hba specific.
8242 * We need to query our hba capabilities to find out whether to
8243 * register or not.
8244 */
8245 if (un->un_f_is_fibre) {
8246 if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8247 sd_init_event_callbacks(un);
8248 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8249 "sd_unit_attach: un:0x%p event callbacks inserted",
8250 un);
8251 }
8252 }
8253 #endif
8254
8255 if (un->un_f_opt_disable_cache == TRUE) {
8256 /*
8257 * Disable both read cache and write cache. This is
8258 * the historic behavior of the keywords in the config file.
8259 */
8260 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8261 0) {
8262 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8263 "sd_unit_attach: un:0x%p Could not disable "
8264 "caching", un);
8265 goto devid_failed;
8266 }
8267 }
8268
8269 /*
8270 * Check the value of the WCE bit and if it's allowed to be changed,
8271 * set un_f_write_cache_enabled and un_f_cache_mode_changeable
8272 * accordingly.
8273 */
8274 (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8346 *
8347 * Note: This is a critical sequence that needs to be maintained:
8348 * 1) Instantiate the kstats before any routines using the iopath
8349 * (i.e. sd_send_scsi_cmd).
8350 * 2) Initialize the error stats (sd_set_errstats) and partition
8351 * stats (sd_set_pstats)here, following
8352 * cmlb_validate_geometry(), sd_register_devid(), and
8353 * sd_cache_control().
8354 */
8355
8356 if (un->un_f_pkstats_enabled && geom_label_valid) {
8357 sd_set_pstats(un);
8358 SD_TRACE(SD_LOG_IO_PARTITION, un,
8359 "sd_unit_attach: un:0x%p pstats created and set\n", un);
8360 }
8361
8362 sd_set_errstats(un);
8363 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8364 "sd_unit_attach: un:0x%p errstats set\n", un);
8365
8366
8367 /*
8368 * After successfully attaching an instance, we record the information
8369 * of how many luns have been attached on the relative target and
8370 * controller for parallel SCSI. This information is used when sd tries
8371 * to set the tagged queuing capability in HBA.
8372 */
8373 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8374 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8375 }
8376
8377 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8378 "sd_unit_attach: un:0x%p exit success\n", un);
8379
8380 /* Uninitialize sd_ssc_t pointer */
8381 sd_ssc_fini(ssc);
8382
8383 return (DDI_SUCCESS);
8384
8385 /*
8386 * An error occurred during the attach; clean up & return failure.
8387 */
8388 wm_cache_failed:
8389 devid_failed:
8390 ddi_remove_minor_node(devi, NULL);
8391
8392 cmlb_attach_failed:
8393 /*
8394 * Cleanup from the scsi_ifsetcap() calls (437868)
8395 */
8396 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8397 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8398
8399 /*
8400 * Refer to the comments of setting tagged-qing in the beginning of
8401 * sd_unit_attach. We can only disable tagged queuing when there is
8402 * no lun attached on the target.
8403 */
8404 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8405 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8406 }
8407
8408 if (un->un_f_is_fibre == FALSE) {
8409 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8410 }
8411
8412 spinup_failed:
8413
8414 /* Uninitialize sd_ssc_t pointer */
8415 sd_ssc_fini(ssc);
8416
8417 mutex_enter(SD_MUTEX(un));
8418
8419 /* Deallocate SCSI FMA memory spaces */
8420 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8421
8422 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8423 if (un->un_direct_priority_timeid != NULL) {
8424 timeout_id_t temp_id = un->un_direct_priority_timeid;
8425 un->un_direct_priority_timeid = NULL;
8426 mutex_exit(SD_MUTEX(un));
8427 (void) untimeout(temp_id);
8428 mutex_enter(SD_MUTEX(un));
8429 }
8430
8431 /* Cancel any pending start/stop timeouts */
8432 if (un->un_startstop_timeid != NULL) {
8433 timeout_id_t temp_id = un->un_startstop_timeid;
8434 un->un_startstop_timeid = NULL;
8435 mutex_exit(SD_MUTEX(un));
8436 (void) untimeout(temp_id);
8437 mutex_enter(SD_MUTEX(un));
8438 }
8439
8440 /* Cancel any pending reset-throttle timeouts */
8441 if (un->un_reset_throttle_timeid != NULL) {
8442 timeout_id_t temp_id = un->un_reset_throttle_timeid;
8443 un->un_reset_throttle_timeid = NULL;
8444 mutex_exit(SD_MUTEX(un));
8445 (void) untimeout(temp_id);
8446 mutex_enter(SD_MUTEX(un));
8447 }
8448
8449 /* Cancel rmw warning message timeouts */
8450 if (un->un_rmw_msg_timeid != NULL) {
8451 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8452 un->un_rmw_msg_timeid = NULL;
8453 mutex_exit(SD_MUTEX(un));
8454 (void) untimeout(temp_id);
8455 mutex_enter(SD_MUTEX(un));
8456 }
8457
8458 /* Cancel any pending retry timeouts */
8459 if (un->un_retry_timeid != NULL) {
8460 timeout_id_t temp_id = un->un_retry_timeid;
8461 un->un_retry_timeid = NULL;
8462 mutex_exit(SD_MUTEX(un));
8463 (void) untimeout(temp_id);
8464 mutex_enter(SD_MUTEX(un));
8465 }
8466
8467 /* Cancel any pending delayed cv broadcast timeouts */
8468 if (un->un_dcvb_timeid != NULL) {
8469 timeout_id_t temp_id = un->un_dcvb_timeid;
8470 un->un_dcvb_timeid = NULL;
8471 mutex_exit(SD_MUTEX(un));
8472 (void) untimeout(temp_id);
8473 mutex_enter(SD_MUTEX(un));
8474 }
8475
8476 mutex_exit(SD_MUTEX(un));
8477
8478 /* There should not be any in-progress I/O so ASSERT this check */
8479 ASSERT(un->un_ncmds_in_transport == 0);
8480 ASSERT(un->un_ncmds_in_driver == 0);
8481
8482 /* Do not free the softstate if the callback routine is active */
8483 sd_sync_with_callback(un);
8484
8485 /*
8486 * Partition stats apparently are not used with removables. These would
8487 * not have been created during attach, so no need to clean them up...
8488 */
8489 if (un->un_errstats != NULL) {
8490 kstat_delete(un->un_errstats);
8491 un->un_errstats = NULL;
8492 }
8493
8494 create_errstats_failed:
8495
8496 if (un->un_stats != NULL) {
8497 kstat_delete(un->un_stats);
8498 un->un_stats = NULL;
8499 }
8500
8501 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8502 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8503
8504 ddi_prop_remove_all(devi);
8505 sema_destroy(&un->un_semoclose);
8506 cv_destroy(&un->un_state_cv);
8507
8508 sd_free_rqs(un);
8509
8510 alloc_rqs_failed:
8511
8512 devp->sd_private = NULL;
8513 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */
8514
8515 /*
8516 * Note: the man pages are unclear as to whether or not doing a
8517 * ddi_soft_state_free(sd_state, instance) is the right way to
8518 * clean up after the ddi_soft_state_zalloc() if the subsequent
8519 * ddi_get_soft_state() fails. The implication seems to be
8520 * that the get_soft_state cannot fail if the zalloc succeeds.
8521 */
8522 #ifndef XPV_HVM_DRIVER
8523 ddi_soft_state_free(sd_state, instance);
8524 #endif /* !XPV_HVM_DRIVER */
8525
8526 probe_failed:
8527 scsi_unprobe(devp);
8528
8529 return (DDI_FAILURE);
8530 }
8531
8532
8533 /*
8534 * Function: sd_unit_detach
8535 *
8536 * Description: Performs DDI_DETACH processing for sddetach().
8537 *
8538 * Return Code: DDI_SUCCESS
8539 * DDI_FAILURE
8540 *
8541 * Context: Kernel thread context
8542 */
8543
8544 static int
8545 sd_unit_detach(dev_info_t *devi)
8546 {
8547 struct scsi_device *devp;
8548 struct sd_lun *un;
8549 int i;
8550 int tgt;
8551 dev_t dev;
8552 dev_info_t *pdip = ddi_get_parent(devi);
8553 int instance = ddi_get_instance(devi);
8554
8555 mutex_enter(&sd_detach_mutex);
8556
8557 /*
8558 * Fail the detach for any of the following:
8559 * - Unable to get the sd_lun struct for the instance
8560 * - A layered driver has an outstanding open on the instance
8561 * - Another thread is already detaching this instance
8562 * - Another thread is currently performing an open
8563 */
8564 devp = ddi_get_driver_private(devi);
8565 if ((devp == NULL) ||
8566 ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8567 (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8568 (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8569 mutex_exit(&sd_detach_mutex);
8570 return (DDI_FAILURE);
8571 }
8572
8573 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8574
8575 /*
8576 * Mark this instance as currently in a detach, to inhibit any
8577 * opens from a layered driver.
8578 */
8579 un->un_detach_count++;
8580 mutex_exit(&sd_detach_mutex);
8581
8582 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8583 SCSI_ADDR_PROP_TARGET, -1);
8584
8585 dev = sd_make_device(SD_DEVINFO(un));
8586
8587 #ifndef lint
8588 _NOTE(COMPETING_THREADS_NOW);
8589 #endif
8590
8591 mutex_enter(SD_MUTEX(un));
8592
8593 /*
8594 * Fail the detach if there are any outstanding layered
8595 * opens on this device.
8596 */
8597 for (i = 0; i < NDKMAP; i++) {
8598 if (un->un_ocmap.lyropen[i] != 0) {
8599 goto err_notclosed;
8600 }
8601 }
8602
8603 /*
8604 * Verify there are NO outstanding commands issued to this device.
8605 * ie, un_ncmds_in_transport == 0.
8606 * It's possible to have outstanding commands through the physio
8607 * code path, even though everything's closed.
8608 */
8609 if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8610 (un->un_direct_priority_timeid != NULL) ||
8611 (un->un_state == SD_STATE_RWAIT)) {
8612 mutex_exit(SD_MUTEX(un));
8613 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8614 "sd_dr_detach: Detach failure due to outstanding cmds\n");
8615 goto err_stillbusy;
8616 }
8617
8618 /*
8619 * If we have the device reserved, release the reservation.
8620 */
8621 if ((un->un_resvd_status & SD_RESERVE) &&
8622 !(un->un_resvd_status & SD_LOST_RESERVE)) {
8623 mutex_exit(SD_MUTEX(un));
8624 /*
8625 * Note: sd_reserve_release sends a command to the device
8626 * via the sd_ioctlcmd() path, and can sleep.
8627 */
8628 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8629 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8630 "sd_dr_detach: Cannot release reservation \n");
8631 }
8632 } else {
8633 mutex_exit(SD_MUTEX(un));
8634 }
8635
8636 /*
8637 * Untimeout any reserve recover, throttle reset, restart unit
8638 * and delayed broadcast timeout threads. Protect the timeout pointer
8639 * from getting nulled by their callback functions.
8640 */
8641 mutex_enter(SD_MUTEX(un));
8642 if (un->un_resvd_timeid != NULL) {
8643 timeout_id_t temp_id = un->un_resvd_timeid;
8644 un->un_resvd_timeid = NULL;
8645 mutex_exit(SD_MUTEX(un));
8646 (void) untimeout(temp_id);
8647 mutex_enter(SD_MUTEX(un));
8648 }
8649
8650 if (un->un_reset_throttle_timeid != NULL) {
8667 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8668 un->un_rmw_msg_timeid = NULL;
8669 mutex_exit(SD_MUTEX(un));
8670 (void) untimeout(temp_id);
8671 mutex_enter(SD_MUTEX(un));
8672 }
8673
8674 if (un->un_dcvb_timeid != NULL) {
8675 timeout_id_t temp_id = un->un_dcvb_timeid;
8676 un->un_dcvb_timeid = NULL;
8677 mutex_exit(SD_MUTEX(un));
8678 (void) untimeout(temp_id);
8679 } else {
8680 mutex_exit(SD_MUTEX(un));
8681 }
8682
8683 /* Remove any pending reservation reclaim requests for this device */
8684 sd_rmv_resv_reclaim_req(dev);
8685
8686 mutex_enter(SD_MUTEX(un));
8687
8688 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8689 if (un->un_direct_priority_timeid != NULL) {
8690 timeout_id_t temp_id = un->un_direct_priority_timeid;
8691 un->un_direct_priority_timeid = NULL;
8692 mutex_exit(SD_MUTEX(un));
8693 (void) untimeout(temp_id);
8694 mutex_enter(SD_MUTEX(un));
8695 }
8696
8697 /* Cancel any active multi-host disk watch thread requests */
8698 if (un->un_mhd_token != NULL) {
8699 mutex_exit(SD_MUTEX(un));
8700 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8701 if (scsi_watch_request_terminate(un->un_mhd_token,
8702 SCSI_WATCH_TERMINATE_NOWAIT)) {
8703 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8704 "sd_dr_detach: Cannot cancel mhd watch request\n");
8705 /*
8706 * Note: We are returning here after having removed
8707 * some driver timeouts above. This is consistent with
8708 * the legacy implementation but perhaps the watch
8709 * terminate call should be made with the wait flag set.
8710 */
8711 goto err_stillbusy;
8712 }
8713 mutex_enter(SD_MUTEX(un));
8714 un->un_mhd_token = NULL;
8715 }
8716
8717 if (un->un_swr_token != NULL) {
8718 mutex_exit(SD_MUTEX(un));
8719 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8720 if (scsi_watch_request_terminate(un->un_swr_token,
8721 SCSI_WATCH_TERMINATE_NOWAIT)) {
8722 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8723 "sd_dr_detach: Cannot cancel swr watch request\n");
8724 /*
8725 * Note: We are returning here after having removed
8726 * some driver timeouts above. This is consistent with
8727 * the legacy implementation but perhaps the watch
8728 * terminate call should be made with the wait flag set.
8729 */
8730 goto err_stillbusy;
8731 }
8732 mutex_enter(SD_MUTEX(un));
8733 un->un_swr_token = NULL;
8734 }
8735
8736 mutex_exit(SD_MUTEX(un));
8737
8738 /*
8739 * Clear any scsi_reset_notifies. We clear the reset notifies
8740 * if we have not registered one.
8741 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8742 */
8743 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8744 sd_mhd_reset_notify_cb, (caddr_t)un);
8745
8746 /*
8747 * protect the timeout pointers from getting nulled by
8748 * their callback functions during the cancellation process.
8749 * In such a scenario untimeout can be invoked with a null value.
8750 */
8751 _NOTE(NO_COMPETING_THREADS_NOW);
8752
8753 mutex_enter(&un->un_pm_mutex);
8754 if (un->un_pm_idle_timeid != NULL) {
8755 timeout_id_t temp_id = un->un_pm_idle_timeid;
8756 un->un_pm_idle_timeid = NULL;
8757 mutex_exit(&un->un_pm_mutex);
8758
8759 /*
8760 * Timeout is active; cancel it.
8761 * Note that it'll never be active on a device
8762 * that does not support PM therefore we don't
8763 * have to check before calling pm_idle_component.
8764 */
8765 (void) untimeout(temp_id);
8766 (void) pm_idle_component(SD_DEVINFO(un), 0);
8767 mutex_enter(&un->un_pm_mutex);
8768 }
8769
8770 /*
8771 * Check whether there is already a timeout scheduled for power
8772 * management. If yes then don't lower the power here, that's.
8774 */
8775 if (un->un_pm_timeid != NULL) {
8776 timeout_id_t temp_id = un->un_pm_timeid;
8777 un->un_pm_timeid = NULL;
8778 mutex_exit(&un->un_pm_mutex);
8779 /*
8780 * Timeout is active; cancel it.
8781 * Note that it'll never be active on a device
8782 * that does not support PM therefore we don't
8783 * have to check before calling pm_idle_component.
8784 */
8785 (void) untimeout(temp_id);
8786 (void) pm_idle_component(SD_DEVINFO(un), 0);
8787
8788 } else {
8789 mutex_exit(&un->un_pm_mutex);
8790 if ((un->un_f_pm_is_enabled == TRUE) &&
8791 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8792 != DDI_SUCCESS)) {
8793 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8794 "sd_dr_detach: Lower power request failed, ignoring.\n");
8795 /*
8796 * Fix for bug: 4297749, item # 13
8797 * The above test now includes a check to see if PM is
8798 * supported by this device before call
8799 * pm_lower_power().
8800 * Note, the following is not dead code. The call to
8801 * pm_lower_power above will generate a call back into
8802 * our sdpower routine which might result in a timeout
8803 * handler getting activated. Therefore the following
8804 * code is valid and necessary.
8805 */
8806 mutex_enter(&un->un_pm_mutex);
8807 if (un->un_pm_timeid != NULL) {
8808 timeout_id_t temp_id = un->un_pm_timeid;
8809 un->un_pm_timeid = NULL;
8810 mutex_exit(&un->un_pm_mutex);
8811 (void) untimeout(temp_id);
8812 (void) pm_idle_component(SD_DEVINFO(un), 0);
8813 } else {
8814 mutex_exit(&un->un_pm_mutex);
8815 }
8816 }
8839 }
8840
8841 if (un->un_f_is_fibre == FALSE) {
8842 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8843 }
8844
8845 /*
8846 * Remove any event callbacks, fibre only
8847 */
8848 if (un->un_f_is_fibre == TRUE) {
8849 if ((un->un_insert_event != NULL) &&
8850 (ddi_remove_event_handler(un->un_insert_cb_id) !=
8851 DDI_SUCCESS)) {
8852 /*
8853 * Note: We are returning here after having done
8854 * substantial cleanup above. This is consistent
8855 * with the legacy implementation but this may not
8856 * be the right thing to do.
8857 */
8858 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8859 "sd_dr_detach: Cannot cancel insert event\n");
8860 goto err_remove_event;
8861 }
8862 un->un_insert_event = NULL;
8863
8864 if ((un->un_remove_event != NULL) &&
8865 (ddi_remove_event_handler(un->un_remove_cb_id) !=
8866 DDI_SUCCESS)) {
8867 /*
8868 * Note: We are returning here after having done
8869 * substantial cleanup above. This is consistent
8870 * with the legacy implementation but this may not
8871 * be the right thing to do.
8872 */
8873 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8874 "sd_dr_detach: Cannot cancel remove event\n");
8875 goto err_remove_event;
8876 }
8877 un->un_remove_event = NULL;
8878 }
8879
8880 /* Do not free the softstate if the callback routine is active */
8881 sd_sync_with_callback(un);
8882
8883 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8884 cmlb_free_handle(&un->un_cmlbhandle);
8885
8886 /*
8887 * Hold the detach mutex here, to make sure that no other threads ever
8888 * can access a (partially) freed soft state structure.
8889 */
8890 mutex_enter(&sd_detach_mutex);
8891
8892 /*
8893 * Clean up the soft state struct.
8894 * Cleanup is done in reverse order of allocs/inits.
8895 * At this point there should be no competing threads anymore.
8896 */
8897
8898 scsi_fm_fini(devp);
8899
8900 /*
8901 * Deallocate memory for SCSI FMA.
8902 */
8919 }
8920
8921 /*
8922 * Destroy wmap cache if it exists.
8923 */
8924 if (un->un_wm_cache != NULL) {
8925 kmem_cache_destroy(un->un_wm_cache);
8926 un->un_wm_cache = NULL;
8927 }
8928
8929 /*
8930 * kstat cleanup is done in detach for all device types (4363169).
8931 * We do not want to fail detach if the device kstats are not deleted
8932 * since there is a confusion about the devo_refcnt for the device.
8933 * We just delete the kstats and let detach complete successfully.
8934 */
8935 if (un->un_stats != NULL) {
8936 kstat_delete(un->un_stats);
8937 un->un_stats = NULL;
8938 }
8939 if (un->un_errstats != NULL) {
8940 kstat_delete(un->un_errstats);
8941 un->un_errstats = NULL;
8942 }
8943
8944 /* Remove partition stats */
8945 if (un->un_f_pkstats_enabled) {
8946 for (i = 0; i < NSDMAP; i++) {
8947 if (un->un_pstats[i] != NULL) {
8948 kstat_delete(un->un_pstats[i]);
8949 un->un_pstats[i] = NULL;
8950 }
8951 }
8952 }
8953
8954 /* Remove xbuf registration */
8955 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8956 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8957
8958 /* Remove driver properties */
8959 ddi_prop_remove_all(devi);
8960
8961 mutex_destroy(&un->un_pm_mutex);
8962 cv_destroy(&un->un_pm_busy_cv);
8963
8964 cv_destroy(&un->un_wcc_cv);
8965
8966 /* Open/close semaphore */
8967 sema_destroy(&un->un_semoclose);
8968
8969 /* Removable media condvar. */
8970 cv_destroy(&un->un_state_cv);
8971
8972 /* Suspend/resume condvar. */
8973 cv_destroy(&un->un_suspend_cv);
8974 cv_destroy(&un->un_disk_busy_cv);
8975
8976 sd_free_rqs(un);
8977
8978 /* Free up soft state */
8979 devp->sd_private = NULL;
8980
8981 bzero(un, sizeof (struct sd_lun));
8982
8983 ddi_soft_state_free(sd_state, instance);
8984
8985 mutex_exit(&sd_detach_mutex);
8986
8987 /* This frees up the INQUIRY data associated with the device. */
8988 scsi_unprobe(devp);
8989
8990 /*
8991 * After successfully detaching an instance, we update the information
8992 * of how many luns have been attached in the relative target and
8993 * controller for parallel SCSI. This information is used when sd tries
8994 * to set the tagged queuing capability in HBA.
8995 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8996 * check if the device is parallel SCSI. However, we don't need to
8997 * check here because we've already checked during attach. No device
8998 * that is not parallel SCSI is in the chain.
8999 */
9000 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9001 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9002 }
9003
9004 return (DDI_SUCCESS);
9005
9006 err_notclosed:
9007 mutex_exit(SD_MUTEX(un));
9008
9009 err_stillbusy:
9010 _NOTE(NO_COMPETING_THREADS_NOW);
9011
9012 err_remove_event:
9013 mutex_enter(&sd_detach_mutex);
9014 un->un_detach_count--;
9015 mutex_exit(&sd_detach_mutex);
9016
9017 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9018 return (DDI_FAILURE);
9019 }
9020
9021
9022 /*
9023 * Function: sd_create_errstats
9024 *
9025 * Description: This routine instantiates the device error stats.
9026 *
9027 * Note: During attach the stats are instantiated first so they are
9028 * available for attach-time routines that utilize the driver
9029 * iopath to send commands to the device. The stats are initialized
9030 * separately so data obtained during some attach-time routines is
9031 * available. (4362483)
9032 *
9033 * Arguments: un - driver soft state (unit) structure
9034 * instance - driver instance
9035 *
9036 * Context: Kernel thread context
9037 */
9204 * (4363169)
9205 *
9206 * Arguments: un - driver soft state (unit) structure
9207 *
9208 * Context: Kernel thread context
9209 */
9210
9211 static void
9212 sd_set_pstats(struct sd_lun *un)
9213 {
9214 char kstatname[KSTAT_STRLEN];
9215 int instance;
9216 int i;
9217 diskaddr_t nblks = 0;
9218 char *partname = NULL;
9219
9220 ASSERT(un != NULL);
9221
9222 instance = ddi_get_instance(SD_DEVINFO(un));
9223
9224 /* Note:x86: is this a VTOC8/VTOC16 difference? */
9225 for (i = 0; i < NSDMAP; i++) {
9226
9227 if (cmlb_partinfo(un->un_cmlbhandle, i,
9228 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9229 continue;
9230 mutex_enter(SD_MUTEX(un));
9231
9232 if ((un->un_pstats[i] == NULL) &&
9233 (nblks != 0)) {
9234
9235 (void) snprintf(kstatname, sizeof (kstatname),
9236 "%s%d,%s", sd_label, instance,
9237 partname);
9238
9239 un->un_pstats[i] = kstat_create(sd_label,
9240 instance, kstatname, "partition", KSTAT_TYPE_IO,
9241 1, KSTAT_FLAG_PERSISTENT);
9242 if (un->un_pstats[i] != NULL) {
9243 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9244 kstat_install(un->un_pstats[i]);
9245 }
9246 }
9247 mutex_exit(SD_MUTEX(un));
9248 }
9249 }
9250
9251
9252 #if (defined(__fibre))
9253 /*
9254 * Function: sd_init_event_callbacks
9255 *
9256 * Description: This routine initializes the insertion and removal event
9257 * callbacks. (fibre only)
9258 *
9259 * Arguments: un - driver soft state (unit) structure
9260 *
9261 * Context: Kernel thread context
9262 */
9263
9264 static void
9265 sd_init_event_callbacks(struct sd_lun *un)
9266 {
9267 ASSERT(un != NULL);
9268
9269 if ((un->un_insert_event == NULL) &&
9270 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9271 &un->un_insert_event) == DDI_SUCCESS)) {
9272 /*
9273 * Add the callback for an insertion event
9274 */
9275 (void) ddi_add_event_handler(SD_DEVINFO(un),
9276 un->un_insert_event, sd_event_callback, (void *)un,
9277 &(un->un_insert_cb_id));
9278 }
9279
9280 if ((un->un_remove_event == NULL) &&
9281 (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9282 &un->un_remove_event) == DDI_SUCCESS)) {
9283 /*
9284 * Add the callback for a removal event
9285 */
9286 (void) ddi_add_event_handler(SD_DEVINFO(un),
9287 un->un_remove_event, sd_event_callback, (void *)un,
9288 &(un->un_remove_cb_id));
9289 }
9290 }
9291
9292
9293 /*
9294 * Function: sd_event_callback
9295 *
9296 * Description: This routine handles insert/remove events (photon). The
9297 * state is changed to OFFLINE which can be used to supress
9298 * error msgs. (fibre only)
9299 *
9300 * Arguments: un - driver soft state (unit) structure
9301 *
9302 * Context: Callout thread context
9303 */
9304 /* ARGSUSED */
9305 static void
9306 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9307 void *bus_impldata)
9308 {
9309 struct sd_lun *un = (struct sd_lun *)arg;
9310
9311 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9312 if (event == un->un_insert_event) {
9313 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9314 mutex_enter(SD_MUTEX(un));
9315 if (un->un_state == SD_STATE_OFFLINE) {
9316 if (un->un_last_state != SD_STATE_SUSPENDED) {
9317 un->un_state = un->un_last_state;
9318 } else {
9319 /*
9320 * We have gone through SUSPEND/RESUME while
9321 * we were offline. Restore the last state
9322 */
9323 un->un_state = un->un_save_state;
9324 }
9325 }
9326 mutex_exit(SD_MUTEX(un));
9327
9328 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9329 } else if (event == un->un_remove_event) {
9330 SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9331 mutex_enter(SD_MUTEX(un));
9332 /*
9333 * We need to handle an event callback that occurs during
9334 * the suspend operation, since we don't prevent it.
9335 */
9336 if (un->un_state != SD_STATE_OFFLINE) {
9337 if (un->un_state != SD_STATE_SUSPENDED) {
9338 New_state(un, SD_STATE_OFFLINE);
9339 } else {
9340 un->un_last_state = SD_STATE_OFFLINE;
9341 }
9342 }
9343 mutex_exit(SD_MUTEX(un));
9344 } else {
9345 scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9346 "!Unknown event\n");
9347 }
9348
9349 }
9350 #endif
9351
9352 /*
9353 * Values related to caching mode page depending on whether the unit is ATAPI.
9354 */
9355 #define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9356 CDB_GROUP1 : CDB_GROUP0)
9357 #define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9358 MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9359 /*
9360 * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9361 * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9362 */
9363 #define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9364 sizeof (struct mode_cache_scsi3))
9365
9366 static int
9367 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9368 int *bdlen)
9369 {
9370 struct sd_lun *un = ssc->ssc_un;
9371 struct mode_caching *mode_caching_page;
9372 size_t buflen = SDC_BUFLEN(un);
9735 * If either sd.conf or internal disk table
9736 * specifies cache flush be suppressed, then
9737 * we don't bother checking NV_SUP bit.
9738 */
9739 if (un->un_f_suppress_cache_flush == TRUE) {
9740 mutex_exit(SD_MUTEX(un));
9741 return;
9742 }
9743
9744 if (sd_check_vpd_page_support(ssc) == 0 &&
9745 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9746 mutex_exit(SD_MUTEX(un));
9747 /* collect page 86 data if available */
9748 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9749
9750 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9751 0x01, 0x86, &inq86_resid);
9752
9753 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9754 SD_TRACE(SD_LOG_COMMON, un,
9755 "sd_get_nv_sup: \
9756 successfully get VPD page: %x \
9757 PAGE LENGTH: %x BYTE 6: %x\n",
9758 inq86[1], inq86[3], inq86[6]);
9759
9760 mutex_enter(SD_MUTEX(un));
9761 /*
9762 * check the value of NV_SUP bit: only if the device
9763 * reports NV_SUP bit to be 1, the
9764 * un_f_sync_nv_supported bit will be set to true.
9765 */
9766 if (inq86[6] & SD_VPD_NV_SUP) {
9767 un->un_f_sync_nv_supported = TRUE;
9768 }
9769 mutex_exit(SD_MUTEX(un));
9770 } else if (rval != 0) {
9771 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9772 }
9773
9774 kmem_free(inq86, inq86_len);
9775 } else {
9776 mutex_exit(SD_MUTEX(un));
9777 }
10079 int instance;
10080 dev_t dev;
10081 int rval = EIO;
10082 diskaddr_t nblks = 0;
10083 diskaddr_t label_cap;
10084
10085 /* Validate the open type */
10086 if (otyp >= OTYPCNT) {
10087 return (EINVAL);
10088 }
10089
10090 dev = *dev_p;
10091 instance = SDUNIT(dev);
10092 mutex_enter(&sd_detach_mutex);
10093
10094 /*
10095 * Fail the open if there is no softstate for the instance, or
10096 * if another thread somewhere is trying to detach the instance.
10097 */
10098 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10099 (un->un_detach_count != 0)) {
10100 mutex_exit(&sd_detach_mutex);
10101 /*
10102 * The probe cache only needs to be cleared when open (9e) fails
10103 * with ENXIO (4238046).
10104 */
10105 /*
10106 * un-conditionally clearing probe cache is ok with
10107 * separate sd/ssd binaries
10108 * x86 platform can be an issue with both parallel
10109 * and fibre in 1 binary
10110 */
10111 sd_scsi_clear_probe_cache();
10112 return (ENXIO);
10113 }
10114
10115 /*
10116 * The un_layer_count is to prevent another thread in specfs from
10117 * trying to detach the instance, which can happen when we are
10118 * called from a higher-layer driver instead of thru specfs.
10119 * This will not be needed when DDI provides a layered driver
10120 * interface that allows specfs to know that an instance is in
10121 * use by a layered driver & should not be detached.
10122 *
10123 * Note: the semantics for layered driver opens are exactly one
10124 * close for every open.
10125 */
10126 if (otyp == OTYP_LYR) {
10127 un->un_layer_count++;
10128 }
10129
10130 /*
10141 part = SDPART(dev);
10142 partmask = 1 << part;
10143
10144 /*
10145 * We use a semaphore here in order to serialize
10146 * open and close requests on the device.
10147 */
10148 sema_p(&un->un_semoclose);
10149
10150 mutex_enter(SD_MUTEX(un));
10151
10152 /*
10153 * All device accesses go thru sdstrategy() where we check
10154 * on suspend status but there could be a scsi_poll command,
10155 * which bypasses sdstrategy(), so we need to check pm
10156 * status.
10157 */
10158
10159 if (!nodelay) {
10160 while ((un->un_state == SD_STATE_SUSPENDED) ||
10161 (un->un_state == SD_STATE_PM_CHANGING)) {
10162 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10163 }
10164
10165 mutex_exit(SD_MUTEX(un));
10166 if (sd_pm_entry(un) != DDI_SUCCESS) {
10167 rval = EIO;
10168 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10169 "sdopen: sd_pm_entry failed\n");
10170 goto open_failed_with_pm;
10171 }
10172 mutex_enter(SD_MUTEX(un));
10173 }
10174
10175 /* check for previous exclusive open */
10176 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10177 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10178 "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10179 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10180
10181 if (un->un_exclopen & (partmask)) {
10182 goto excl_open_fail;
10183 }
10184
10185 if (flag & FEXCL) {
10186 int i;
10187 if (un->un_ocmap.lyropen[part]) {
10188 goto excl_open_fail;
10189 }
10190 for (i = 0; i < (OTYPCNT - 1); i++) {
10191 if (un->un_ocmap.regopen[i] & (partmask)) {
10192 goto excl_open_fail;
10243 * blocks is zero or negative for non CD devices.
10244 */
10245
10246 nblks = 0;
10247
10248 if (rval == SD_READY_VALID && (!ISCD(un))) {
10249 /* if cmlb_partinfo fails, nblks remains 0 */
10250 mutex_exit(SD_MUTEX(un));
10251 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10252 NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10253 mutex_enter(SD_MUTEX(un));
10254 }
10255
10256 if ((rval != SD_READY_VALID) ||
10257 (!ISCD(un) && nblks <= 0)) {
10258 rval = un->un_f_has_removable_media ? ENXIO : EIO;
10259 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10260 "device not ready or invalid disk block value\n");
10261 goto open_fail;
10262 }
10263 #if defined(__i386) || defined(__amd64)
10264 } else {
10265 uchar_t *cp;
10266 /*
10267 * x86 requires special nodelay handling, so that p0 is
10268 * always defined and accessible.
10269 * Invalidate geometry only if device is not already open.
10270 */
10271 cp = &un->un_ocmap.chkd[0];
10272 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10273 if (*cp != (uchar_t)0) {
10274 break;
10275 }
10276 cp++;
10277 }
10278 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10279 mutex_exit(SD_MUTEX(un));
10280 cmlb_invalidate(un->un_cmlbhandle,
10281 (void *)SD_PATH_DIRECT);
10282 mutex_enter(SD_MUTEX(un));
10283 }
10284
10285 #endif
10286 }
10287
10288 if (otyp == OTYP_LYR) {
10289 un->un_ocmap.lyropen[part]++;
10290 } else {
10291 un->un_ocmap.regopen[otyp] |= partmask;
10292 }
10293
10294 /* Set up open and exclusive open flags */
10295 if (flag & FEXCL) {
10296 un->un_exclopen |= (partmask);
10297 }
10298
10299 /*
10300 * If the lun is EFI labeled and lun capacity is greater than the
10301 * capacity contained in the label, log a sys-event to notify the
10302 * interested module.
10303 * To avoid an infinite loop of logging sys-event, we only log the
10304 * event when the lun is not opened in NDELAY mode. The event handler
10305 * should open the lun in NDELAY mode.
10309 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10310 (void*)SD_PATH_DIRECT) == 0) {
10311 mutex_enter(SD_MUTEX(un));
10312 if (un->un_f_blockcount_is_valid &&
10313 un->un_blockcount > label_cap &&
10314 un->un_f_expnevent == B_FALSE) {
10315 un->un_f_expnevent = B_TRUE;
10316 mutex_exit(SD_MUTEX(un));
10317 sd_log_lun_expansion_event(un,
10318 (nodelay ? KM_NOSLEEP : KM_SLEEP));
10319 mutex_enter(SD_MUTEX(un));
10320 }
10321 } else {
10322 mutex_enter(SD_MUTEX(un));
10323 }
10324 }
10325
10326 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10327 "open of part %d type %d\n", part, otyp);
10328
10329 mutex_exit(SD_MUTEX(un));
10330 if (!nodelay) {
10331 sd_pm_exit(un);
10332 }
10333
10334 sema_v(&un->un_semoclose);
10335
10336 mutex_enter(&sd_detach_mutex);
10337 un->un_opens_in_progress--;
10338 mutex_exit(&sd_detach_mutex);
10339
10340 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10341 return (DDI_SUCCESS);
10342
10343 excl_open_fail:
10344 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10345 rval = EBUSY;
10346
10347 open_fail:
10348 mutex_exit(SD_MUTEX(un));
10349
10350 /*
10351 * On a failed open we must exit the pm management.
10352 */
10353 if (!nodelay) {
10354 sd_pm_exit(un);
10355 }
10356 open_failed_with_pm:
10357 sema_v(&un->un_semoclose);
10358
10359 mutex_enter(&sd_detach_mutex);
10360 un->un_opens_in_progress--;
10361 if (otyp == OTYP_LYR) {
10362 un->un_layer_count--;
10363 }
10364 mutex_exit(&sd_detach_mutex);
10365
10366 return (rval);
10367 }
10368
10369
10370 /*
10371 * Function: sdclose
10372 *
10373 * Description: Driver's close(9e) entry point function.
10374 *
10375 * Arguments: dev - device number
10376 * flag - file status flag, informational only
10379 *
10380 * Return Code: ENXIO
10381 *
10382 * Context: Kernel thread context
10383 */
10384 /* ARGSUSED */
10385 static int
10386 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10387 {
10388 struct sd_lun *un;
10389 uchar_t *cp;
10390 int part;
10391 int nodelay;
10392 int rval = 0;
10393
10394 /* Validate the open type */
10395 if (otyp >= OTYPCNT) {
10396 return (ENXIO);
10397 }
10398
10399 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10400 return (ENXIO);
10401 }
10402
10403 part = SDPART(dev);
10404 nodelay = flag & (FNDELAY | FNONBLOCK);
10405
10406 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10407 "sdclose: close of part %d type %d\n", part, otyp);
10408
10409 /*
10410 * We use a semaphore here in order to serialize
10411 * open and close requests on the device.
10412 */
10413 sema_p(&un->un_semoclose);
10414
10415 mutex_enter(SD_MUTEX(un));
10416
10417 /* Don't proceed if power is being changed. */
10418 while (un->un_state == SD_STATE_PM_CHANGING) {
10419 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10420 }
10421
10422 if (un->un_exclopen & (1 << part)) {
10423 un->un_exclopen &= ~(1 << part);
10424 }
10425
10426 /* Update the open partition map */
10427 if (otyp == OTYP_LYR) {
10428 un->un_ocmap.lyropen[part] -= 1;
10429 } else {
10430 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10431 }
10432
10433 cp = &un->un_ocmap.chkd[0];
10434 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10435 if (*cp != NULL) {
10436 break;
10437 }
10438 cp++;
10439 }
10440
10441 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10442 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10443
10444 /*
10445 * We avoid persistance upon the last close, and set
10446 * the throttle back to the maximum.
10447 */
10448 un->un_throttle = un->un_saved_throttle;
10449
10450 if (un->un_state == SD_STATE_OFFLINE) {
10451 if (un->un_f_is_fibre == FALSE) {
10452 scsi_log(SD_DEVINFO(un), sd_label,
10453 CE_WARN, "offline\n");
10454 }
10455 mutex_exit(SD_MUTEX(un));
10456 cmlb_invalidate(un->un_cmlbhandle,
10457 (void *)SD_PATH_DIRECT);
10458 mutex_enter(SD_MUTEX(un));
10459
10460 } else {
10461 /*
10462 * Flush any outstanding writes in NVRAM cache.
10463 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10464 * cmd, it may not work for non-Pluto devices.
10465 * SYNCHRONIZE CACHE is not required for removables,
10466 * except DVD-RAM drives.
10467 *
10468 * Also note: because SYNCHRONIZE CACHE is currently
10469 * the only command issued here that requires the
10470 * drive be powered up, only do the power up before
10471 * sending the Sync Cache command. If additional
10472 * commands are added which require a powered up
10473 * drive, the following sequence may have to change.
10474 *
10475 * And finally, note that parallel SCSI on SPARC
10476 * only issues a Sync Cache to DVD-RAM, a newly
10477 * supported device.
10478 */
10479 #if defined(__i386) || defined(__amd64)
10480 if ((un->un_f_sync_cache_supported &&
10481 un->un_f_sync_cache_required) ||
10482 un->un_f_dvdram_writable_device == TRUE) {
10483 #else
10484 if (un->un_f_dvdram_writable_device == TRUE) {
10485 #endif
10486 mutex_exit(SD_MUTEX(un));
10487 if (sd_pm_entry(un) == DDI_SUCCESS) {
10488 rval =
10489 sd_send_scsi_SYNCHRONIZE_CACHE(un,
10490 NULL);
10491 /* ignore error if not supported */
10492 if (rval == ENOTSUP) {
10493 rval = 0;
10494 } else if (rval != 0) {
10495 rval = EIO;
10496 }
10497 sd_pm_exit(un);
10498 } else {
10499 rval = EIO;
10500 }
10501 mutex_enter(SD_MUTEX(un));
10502 }
10503
10504 /*
10505 * For devices which supports DOOR_LOCK, send an ALLOW
10515 ssc = sd_ssc_init(un);
10516 rval = sd_send_scsi_DOORLOCK(ssc,
10517 SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10518 if (rval != 0)
10519 sd_ssc_assessment(ssc,
10520 SD_FMT_IGNORE);
10521 sd_ssc_fini(ssc);
10522
10523 sd_pm_exit(un);
10524 if (ISCD(un) && (rval != 0) &&
10525 (nodelay != 0)) {
10526 rval = ENXIO;
10527 }
10528 } else {
10529 rval = EIO;
10530 }
10531 mutex_enter(SD_MUTEX(un));
10532 }
10533
10534 /*
10535 * If a device has removable media, invalidate all
10536 * parameters related to media, such as geometry,
10537 * blocksize, and blockcount.
10538 */
10539 if (un->un_f_has_removable_media) {
10540 sr_ejected(un);
10541 }
10542
10543 /*
10544 * Destroy the cache (if it exists) which was
10545 * allocated for the write maps since this is
10546 * the last close for this media.
10547 */
10548 if (un->un_wm_cache) {
10549 /*
10550 * Check if there are pending commands.
10551 * and if there are give a warning and
10552 * do not destroy the cache.
10553 */
10554 if (un->un_ncmds_in_driver > 0) {
10555 scsi_log(SD_DEVINFO(un),
10556 sd_label, CE_WARN,
10557 "Unable to clean up memory "
10558 "because of pending I/O\n");
10559 } else {
10560 kmem_cache_destroy(
10561 un->un_wm_cache);
10562 un->un_wm_cache = NULL;
10563 }
10564 }
10565 }
10566 }
10567
10568 mutex_exit(SD_MUTEX(un));
10569 sema_v(&un->un_semoclose);
10570
10571 if (otyp == OTYP_LYR) {
10572 mutex_enter(&sd_detach_mutex);
10573 /*
10574 * The detach routine may run when the layer count
10575 * drops to zero.
10576 */
10577 un->un_layer_count--;
10578 mutex_exit(&sd_detach_mutex);
10579 }
10580
10581 return (rval);
10582 }
10583
10584
10585 /*
10586 * Function: sd_ready_and_valid
10587 *
10588 * Description: Test if device is ready and has a valid geometry.
10589 *
10590 * Arguments: ssc - sd_ssc_t will contain un
10591 * un - driver soft state (unit) structure
10592 *
10593 * Return Code: SD_READY_VALID ready and valid label
10594 * SD_NOT_READY_VALID not ready, no label
10595 * SD_RESERVED_BY_OTHERS reservation conflict
10596 *
10597 * Context: Never called at interrupt context.
10598 */
10599
10656 } else {
10657 mutex_enter(SD_MUTEX(un));
10658 sd_update_block_info(un, lbasize, capacity);
10659 }
10660 }
10661
10662 /*
10663 * Check if the media in the device is writable or not.
10664 */
10665 if (!is_valid && ISCD(un)) {
10666 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10667 }
10668
10669 } else {
10670 /*
10671 * Do a test unit ready to clear any unit attention from non-cd
10672 * devices.
10673 */
10674 mutex_exit(SD_MUTEX(un));
10675
10676 status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10677 if (status != 0) {
10678 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10679 }
10680
10681 mutex_enter(SD_MUTEX(un));
10682 }
10683
10684
10685 /*
10686 * If this is a non 512 block device, allocate space for
10687 * the wmap cache. This is being done here since every time
10688 * a media is changed this routine will be called and the
10689 * block size is a function of media rather than device.
10690 */
10691 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10692 un->un_f_non_devbsize_supported) &&
10693 un->un_tgt_blocksize != DEV_BSIZE) ||
10694 un->un_f_enable_rmw) {
10695 if (!(un->un_wm_cache)) {
10696 (void) snprintf(name_str, sizeof (name_str),
10862 * uio - structure pointer describing where data is to be stored
10863 * in user's space
10864 * cred_p - user credential pointer
10865 *
10866 * Return Code: ENXIO
10867 * EIO
10868 * EINVAL
10869 * value returned by physio
10870 *
10871 * Context: Kernel thread context.
10872 */
10873 /* ARGSUSED */
10874 static int
10875 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10876 {
10877 struct sd_lun *un = NULL;
10878 int secmask;
10879 int err = 0;
10880 sd_ssc_t *ssc;
10881
10882 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10883 return (ENXIO);
10884 }
10885
10886 ASSERT(!mutex_owned(SD_MUTEX(un)));
10887
10888
10889 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10890 mutex_enter(SD_MUTEX(un));
10891 /*
10892 * Because the call to sd_ready_and_valid will issue I/O we
10893 * must wait here if either the device is suspended or
10894 * if it's power level is changing.
10895 */
10896 while ((un->un_state == SD_STATE_SUSPENDED) ||
10897 (un->un_state == SD_STATE_PM_CHANGING)) {
10898 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10899 }
10900 un->un_ncmds_in_driver++;
10901 mutex_exit(SD_MUTEX(un));
10902
10903 /* Initialize sd_ssc_t for internal uscsi commands */
10904 ssc = sd_ssc_init(un);
10905 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10906 err = EIO;
10907 } else {
10908 err = 0;
10909 }
10910 sd_ssc_fini(ssc);
10911
10912 mutex_enter(SD_MUTEX(un));
10913 un->un_ncmds_in_driver--;
10914 ASSERT(un->un_ncmds_in_driver >= 0);
10915 mutex_exit(SD_MUTEX(un));
10916 if (err != 0)
10917 return (err);
10918 }
10919
10920 /*
10921 * Read requests are restricted to multiples of the system block size.
10922 */
10923 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10924 !un->un_f_enable_rmw)
10925 secmask = un->un_tgt_blocksize - 1;
10926 else
10927 secmask = DEV_BSIZE - 1;
10928
10929 if (uio->uio_loffset & ((offset_t)(secmask))) {
10930 SD_ERROR(SD_LOG_READ_WRITE, un,
10931 "sdread: file offset not modulo %d\n",
10932 secmask + 1);
10933 err = EINVAL;
10953 * uio - structure pointer describing where data is stored in
10954 * user's space
10955 * cred_p - user credential pointer
10956 *
10957 * Return Code: ENXIO
10958 * EIO
10959 * EINVAL
10960 * value returned by physio
10961 *
10962 * Context: Kernel thread context.
10963 */
10964 /* ARGSUSED */
10965 static int
10966 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10967 {
10968 struct sd_lun *un = NULL;
10969 int secmask;
10970 int err = 0;
10971 sd_ssc_t *ssc;
10972
10973 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10974 return (ENXIO);
10975 }
10976
10977 ASSERT(!mutex_owned(SD_MUTEX(un)));
10978
10979 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10980 mutex_enter(SD_MUTEX(un));
10981 /*
10982 * Because the call to sd_ready_and_valid will issue I/O we
10983 * must wait here if either the device is suspended or
10984 * if it's power level is changing.
10985 */
10986 while ((un->un_state == SD_STATE_SUSPENDED) ||
10987 (un->un_state == SD_STATE_PM_CHANGING)) {
10988 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10989 }
10990 un->un_ncmds_in_driver++;
10991 mutex_exit(SD_MUTEX(un));
10992
10993 /* Initialize sd_ssc_t for internal uscsi commands */
10994 ssc = sd_ssc_init(un);
10995 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10996 err = EIO;
10997 } else {
10998 err = 0;
10999 }
11000 sd_ssc_fini(ssc);
11001
11002 mutex_enter(SD_MUTEX(un));
11003 un->un_ncmds_in_driver--;
11004 ASSERT(un->un_ncmds_in_driver >= 0);
11005 mutex_exit(SD_MUTEX(un));
11006 if (err != 0)
11007 return (err);
11008 }
11009
11010 /*
11011 * Write requests are restricted to multiples of the system block size.
11012 */
11013 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11014 !un->un_f_enable_rmw)
11015 secmask = un->un_tgt_blocksize - 1;
11016 else
11017 secmask = DEV_BSIZE - 1;
11018
11019 if (uio->uio_loffset & ((offset_t)(secmask))) {
11020 SD_ERROR(SD_LOG_READ_WRITE, un,
11021 "sdwrite: file offset not modulo %d\n",
11022 secmask + 1);
11023 err = EINVAL;
11024 } else if (uio->uio_iov->iov_len & (secmask)) {
11043 * aio - structure pointer describing where data is to be stored
11044 * cred_p - user credential pointer
11045 *
11046 * Return Code: ENXIO
11047 * EIO
11048 * EINVAL
11049 * value returned by aphysio
11050 *
11051 * Context: Kernel thread context.
11052 */
11053 /* ARGSUSED */
11054 static int
11055 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11056 {
11057 struct sd_lun *un = NULL;
11058 struct uio *uio = aio->aio_uio;
11059 int secmask;
11060 int err = 0;
11061 sd_ssc_t *ssc;
11062
11063 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11064 return (ENXIO);
11065 }
11066
11067 ASSERT(!mutex_owned(SD_MUTEX(un)));
11068
11069 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11070 mutex_enter(SD_MUTEX(un));
11071 /*
11072 * Because the call to sd_ready_and_valid will issue I/O we
11073 * must wait here if either the device is suspended or
11074 * if it's power level is changing.
11075 */
11076 while ((un->un_state == SD_STATE_SUSPENDED) ||
11077 (un->un_state == SD_STATE_PM_CHANGING)) {
11078 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11079 }
11080 un->un_ncmds_in_driver++;
11081 mutex_exit(SD_MUTEX(un));
11082
11083 /* Initialize sd_ssc_t for internal uscsi commands */
11084 ssc = sd_ssc_init(un);
11085 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11086 err = EIO;
11087 } else {
11088 err = 0;
11089 }
11090 sd_ssc_fini(ssc);
11091
11092 mutex_enter(SD_MUTEX(un));
11093 un->un_ncmds_in_driver--;
11094 ASSERT(un->un_ncmds_in_driver >= 0);
11095 mutex_exit(SD_MUTEX(un));
11096 if (err != 0)
11097 return (err);
11098 }
11099
11100 /*
11101 * Read requests are restricted to multiples of the system block size.
11102 */
11103 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11104 !un->un_f_enable_rmw)
11105 secmask = un->un_tgt_blocksize - 1;
11106 else
11107 secmask = DEV_BSIZE - 1;
11108
11109 if (uio->uio_loffset & ((offset_t)(secmask))) {
11110 SD_ERROR(SD_LOG_READ_WRITE, un,
11111 "sdaread: file offset not modulo %d\n",
11112 secmask + 1);
11113 err = EINVAL;
11114 } else if (uio->uio_iov->iov_len & (secmask)) {
11133 * aio - structure pointer describing where data is stored
11134 * cred_p - user credential pointer
11135 *
11136 * Return Code: ENXIO
11137 * EIO
11138 * EINVAL
11139 * value returned by aphysio
11140 *
11141 * Context: Kernel thread context.
11142 */
11143 /* ARGSUSED */
11144 static int
11145 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11146 {
11147 struct sd_lun *un = NULL;
11148 struct uio *uio = aio->aio_uio;
11149 int secmask;
11150 int err = 0;
11151 sd_ssc_t *ssc;
11152
11153 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11154 return (ENXIO);
11155 }
11156
11157 ASSERT(!mutex_owned(SD_MUTEX(un)));
11158
11159 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11160 mutex_enter(SD_MUTEX(un));
11161 /*
11162 * Because the call to sd_ready_and_valid will issue I/O we
11163 * must wait here if either the device is suspended or
11164 * if it's power level is changing.
11165 */
11166 while ((un->un_state == SD_STATE_SUSPENDED) ||
11167 (un->un_state == SD_STATE_PM_CHANGING)) {
11168 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11169 }
11170 un->un_ncmds_in_driver++;
11171 mutex_exit(SD_MUTEX(un));
11172
11173 /* Initialize sd_ssc_t for internal uscsi commands */
11174 ssc = sd_ssc_init(un);
11175 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11176 err = EIO;
11177 } else {
11178 err = 0;
11179 }
11180 sd_ssc_fini(ssc);
11181
11182 mutex_enter(SD_MUTEX(un));
11183 un->un_ncmds_in_driver--;
11184 ASSERT(un->un_ncmds_in_driver >= 0);
11185 mutex_exit(SD_MUTEX(un));
11186 if (err != 0)
11187 return (err);
11188 }
11189
11190 /*
11191 * Write requests are restricted to multiples of the system block size.
11192 */
11193 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11194 !un->un_f_enable_rmw)
11195 secmask = un->un_tgt_blocksize - 1;
11196 else
11197 secmask = DEV_BSIZE - 1;
11198
11199 if (uio->uio_loffset & ((offset_t)(secmask))) {
11200 SD_ERROR(SD_LOG_READ_WRITE, un,
11201 "sdawrite: file offset not modulo %d\n",
11202 secmask + 1);
11203 err = EINVAL;
11204 } else if (uio->uio_iov->iov_len & (secmask)) {
11294 * - The taskq mechanism may be used by the iodone side functions to dispatch
11295 * requests to the iostart side functions. The iostart side functions in
11296 * this case would be called under the context of a taskq thread, so it's
11297 * OK for them to block/sleep/spin in this case.
11298 *
11299 * - iostart side functions may allocate "shadow" buf(9S) structs and
11300 * pass them along to the next function in the chain. The corresponding
11301 * iodone side functions must coalesce the "shadow" bufs and return
11302 * the "original" buf to the next higher layer.
11303 *
11304 * - The b_private field of the buf(9S) struct holds a pointer to
11305 * an sd_xbuf struct, which contains information needed to
11306 * construct the scsi_pkt for the command.
11307 *
11308 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11309 * layer must acquire & release the SD_MUTEX(un) as needed.
11310 */
11311
11312
11313 /*
11314 * Create taskq for all targets in the system. This is created at
11315 * _init(9E) and destroyed at _fini(9E).
11316 *
11317 * Note: here we set the minalloc to a reasonably high number to ensure that
11318 * we will have an adequate supply of task entries available at interrupt time.
11319 * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11320 * sd_create_taskq(). Since we do not want to sleep for allocations at
11321 * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11322 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11323 * requests any one instant in time.
11324 */
11325 #define SD_TASKQ_NUMTHREADS 8
11326 #define SD_TASKQ_MINALLOC 256
11327 #define SD_TASKQ_MAXALLOC 256
11328
11329 static taskq_t *sd_tq = NULL;
11330 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11331
11332 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11333 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11334
11335 /*
11336 * The following task queue is being created for the write part of
11337 * read-modify-write of non-512 block size devices.
11338 * Limit the number of threads to 1 for now. This number has been chosen
11339 * considering the fact that it applies only to dvd ram drives/MO drives
11340 * currently. Performance for which is not main criteria at this stage.
11341 * Note: It needs to be explored if we can use a single taskq in future
11342 */
11343 #define SD_WMR_TASKQ_NUMTHREADS 1
11344 static taskq_t *sd_wmr_tq = NULL;
11345 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11346
11347 /*
11348 * Function: sd_taskq_create
11349 *
11350 * Description: Create taskq thread(s) and preallocate task entries
11351 *
11352 * Return Code: Returns a pointer to the allocated taskq_t.
11353 *
11354 * Context: Can sleep. Requires blockable context.
11355 *
11356 * Notes: - The taskq() facility currently is NOT part of the DDI.
11357 * (definitely NOT recommeded for 3rd-party drivers!) :-)
11358 * - taskq_create() will block for memory, also it will panic
11359 * if it cannot create the requested number of threads.
11360 * - Currently taskq_create() creates threads that cannot be
11361 * swapped.
11362 * - We use TASKQ_PREPOPULATE to ensure we have an adequate
11363 * supply of taskq entries at interrupt time (ie, so that we
11364 * do not have to sleep for memory)
11365 */
11366
11367 static void
11405 sd_wmr_tq = NULL;
11406 }
11407
11408
11409 /*
11410 * Function: sdstrategy
11411 *
11412 * Description: Driver's strategy (9E) entry point function.
11413 *
11414 * Arguments: bp - pointer to buf(9S)
11415 *
11416 * Return Code: Always returns zero
11417 *
11418 * Context: Kernel thread context.
11419 */
11420
11421 static int
11422 sdstrategy(struct buf *bp)
11423 {
11424 struct sd_lun *un;
11425
11426 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11427 if (un == NULL) {
11428 bioerror(bp, EIO);
11429 bp->b_resid = bp->b_bcount;
11430 biodone(bp);
11431 return (0);
11432 }
11433
11434 /* As was done in the past, fail new cmds. if state is dumping. */
11435 if (un->un_state == SD_STATE_DUMPING) {
11436 bioerror(bp, ENXIO);
11437 bp->b_resid = bp->b_bcount;
11438 biodone(bp);
11439 return (0);
11440 }
11441
11442 ASSERT(!mutex_owned(SD_MUTEX(un)));
11443
11444 /*
11445 * Commands may sneak in while we released the mutex in
11446 * DDI_SUSPEND, we should block new commands. However, old
11447 * commands that are still in the driver at this point should
11448 * still be allowed to drain.
11449 */
11450 mutex_enter(SD_MUTEX(un));
11451 /*
11452 * Must wait here if either the device is suspended or
11453 * if it's power level is changing.
11454 */
11455 while ((un->un_state == SD_STATE_SUSPENDED) ||
11456 (un->un_state == SD_STATE_PM_CHANGING)) {
11457 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11458 }
11459
11460 un->un_ncmds_in_driver++;
11461
11462 /*
11463 * atapi: Since we are running the CD for now in PIO mode we need to
11464 * call bp_mapin here to avoid bp_mapin called interrupt context under
11465 * the HBA's init_pkt routine.
11466 */
11467 if (un->un_f_cfg_is_atapi == TRUE) {
11468 mutex_exit(SD_MUTEX(un));
11469 bp_mapin(bp);
11470 mutex_enter(SD_MUTEX(un));
11471 }
11472 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11473 un->un_ncmds_in_driver);
11474
11475 if (bp->b_flags & B_WRITE)
11476 un->un_f_sync_cache_required = TRUE;
11477
11478 mutex_exit(SD_MUTEX(un));
11479
11480 /*
11481 * This will (eventually) allocate the sd_xbuf area and
11482 * call sd_xbuf_strategy(). We just want to return the
11483 * result of ddi_xbuf_qstrategy so that we have an opt-
11484 * imized tail call which saves us a stack frame.
11485 */
11486 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11487 }
11488
11489
11490 /*
11491 * Function: sd_xbuf_strategy
11492 *
11493 * Description: Function for initiating IO operations via the
11494 * ddi_xbuf_qstrategy() mechanism.
11495 *
11496 * Context: Kernel thread context.
11497 */
11498
11499 static void
11500 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11501 {
11502 struct sd_lun *un = arg;
11503
11504 ASSERT(bp != NULL);
11505 ASSERT(xp != NULL);
11506 ASSERT(un != NULL);
11507 ASSERT(!mutex_owned(SD_MUTEX(un)));
11508
11509 /*
11652 *
11653 * Arguments: bp - buf struct ptr
11654 *
11655 * Return Code: Always returns 0
11656 *
11657 * Context: Kernel thread context
11658 */
11659
11660 static int
11661 sd_uscsi_strategy(struct buf *bp)
11662 {
11663 struct sd_lun *un;
11664 struct sd_uscsi_info *uip;
11665 struct sd_xbuf *xp;
11666 uchar_t chain_type;
11667 uchar_t cmd;
11668
11669 ASSERT(bp != NULL);
11670
11671 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11672 if (un == NULL) {
11673 bioerror(bp, EIO);
11674 bp->b_resid = bp->b_bcount;
11675 biodone(bp);
11676 return (0);
11677 }
11678
11679 ASSERT(!mutex_owned(SD_MUTEX(un)));
11680
11681 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11682
11683 /*
11684 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11685 */
11686 ASSERT(bp->b_private != NULL);
11687 uip = (struct sd_uscsi_info *)bp->b_private;
11688 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11689
11690 mutex_enter(SD_MUTEX(un));
11691 /*
11692 * atapi: Since we are running the CD for now in PIO mode we need to
11693 * call bp_mapin here to avoid bp_mapin called interrupt context under
11694 * the HBA's init_pkt routine.
11695 */
11696 if (un->un_f_cfg_is_atapi == TRUE) {
11697 mutex_exit(SD_MUTEX(un));
11698 bp_mapin(bp);
11699 mutex_enter(SD_MUTEX(un));
11700 }
11701 un->un_ncmds_in_driver++;
11702 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11703 un->un_ncmds_in_driver);
11704
11705 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11706 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11707 un->un_f_sync_cache_required = TRUE;
11708
11709 mutex_exit(SD_MUTEX(un));
11710
11711 switch (uip->ui_flags) {
11712 case SD_PATH_DIRECT:
11713 chain_type = SD_CHAIN_DIRECT;
11714 break;
11715 case SD_PATH_DIRECT_PRIORITY:
11716 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11717 break;
11718 default:
11719 chain_type = SD_CHAIN_USCSI;
11720 break;
11721 }
11722
11723 /*
11724 * We may allocate extra buf for external USCSI commands. If the
11725 * application asks for bigger than 20-byte sense data via USCSI,
11726 * SCSA layer will allocate 252 bytes sense buf for that command.
11727 */
11728 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11763 * ENXIO - soft state not found for specified dev
11764 * EINVAL
11765 * EFAULT - copyin/copyout error
11766 * return code of scsi_uscsi_handle_command():
11767 * EIO
11768 * ENXIO
11769 * EACCES
11770 *
11771 * Context: Waits for command to complete. Can sleep.
11772 */
11773
11774 static int
11775 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11776 enum uio_seg dataspace, int path_flag)
11777 {
11778 struct sd_lun *un;
11779 sd_ssc_t *ssc;
11780 int rval;
11781
11782 un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11783 if (un == NULL) {
11784 return (ENXIO);
11785 }
11786
11787 /*
11788 * Using sd_ssc_send to handle uscsi cmd
11789 */
11790 ssc = sd_ssc_init(un);
11791 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11792 sd_ssc_fini(ssc);
11793
11794 return (rval);
11795 }
11796
11797 /*
11798 * Function: sd_ssc_init
11799 *
11800 * Description: Uscsi end-user call this function to initialize necessary
11801 * fields, such as uscsi_cmd and sd_uscsi_info struct.
11802 *
11803 * The return value of sd_send_scsi_cmd will be treated as a
11804 * fault in various conditions. Even it is not Zero, some
11805 * callers may ignore the return value. That is to say, we can
11932 * ENXIO - soft state not found for specified dev
11933 * ECANCELED - command cancelled due to low power
11934 * EINVAL
11935 * EFAULT - copyin/copyout error
11936 * return code of scsi_uscsi_handle_command():
11937 * EIO
11938 * ENXIO
11939 * EACCES
11940 *
11941 * Context: Kernel Thread;
11942 * Waits for command to complete. Can sleep.
11943 */
11944 static int
11945 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11946 enum uio_seg dataspace, int path_flag)
11947 {
11948 struct sd_uscsi_info *uip;
11949 struct uscsi_cmd *uscmd;
11950 struct sd_lun *un;
11951 dev_t dev;
11952
11953 int format = 0;
11954 int rval;
11955
11956 ASSERT(ssc != NULL);
11957 un = ssc->ssc_un;
11958 ASSERT(un != NULL);
11959 uscmd = ssc->ssc_uscsi_cmd;
11960 ASSERT(uscmd != NULL);
11961 ASSERT(!mutex_owned(SD_MUTEX(un)));
11962 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11963 /*
11964 * If enter here, it indicates that the previous uscsi
11965 * command has not been processed by sd_ssc_assessment.
11966 * This is violating our rules of FMA telemetry processing.
11967 * We should print out this message and the last undisposed
11968 * uscsi command.
11969 */
11970 if (uscmd->uscsi_cdb != NULL) {
11971 SD_INFO(SD_LOG_SDTEST, un,
11975 }
11976 /*
11977 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11978 * the initial status.
11979 */
11980 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11981 }
11982
11983 /*
11984 * We need to make sure sd_ssc_send will have sd_ssc_assessment
11985 * followed to avoid missing FMA telemetries.
11986 */
11987 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11988
11989 /*
11990 * if USCSI_PMFAILFAST is set and un is in low power, fail the
11991 * command immediately.
11992 */
11993 mutex_enter(SD_MUTEX(un));
11994 mutex_enter(&un->un_pm_mutex);
11995 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11996 SD_DEVICE_IS_IN_LOW_POWER(un)) {
11997 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11998 "un:0x%p is in low power\n", un);
11999 mutex_exit(&un->un_pm_mutex);
12000 mutex_exit(SD_MUTEX(un));
12001 return (ECANCELED);
12002 }
12003 mutex_exit(&un->un_pm_mutex);
12004 mutex_exit(SD_MUTEX(un));
12005
12006 #ifdef SDDEBUG
12007 switch (dataspace) {
12008 case UIO_USERSPACE:
12009 SD_TRACE(SD_LOG_IO, un,
12010 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12011 break;
12012 case UIO_SYSSPACE:
12013 SD_TRACE(SD_LOG_IO, un,
12014 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12043 * since we allocate the buf here in this function, we do not
12044 * need to preserve the prior contents of b_private.
12045 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12046 */
12047 uip = ssc->ssc_uscsi_info;
12048 uip->ui_flags = path_flag;
12049 uip->ui_cmdp = uscmd;
12050
12051 /*
12052 * Commands sent with priority are intended for error recovery
12053 * situations, and do not have retries performed.
12054 */
12055 if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12056 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12057 }
12058 uscmd->uscsi_flags &= ~USCSI_NOINTR;
12059
12060 dev = SD_GET_DEV(un);
12061 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12062 sd_uscsi_strategy, NULL, uip);
12063
12064 /*
12065 * mark ssc_flags right after handle_cmd to make sure
12066 * the uscsi has been sent
12067 */
12068 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12069
12070 #ifdef SDDEBUG
12071 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12072 "uscsi_status: 0x%02x uscsi_resid:0x%x\n",
12073 uscmd->uscsi_status, uscmd->uscsi_resid);
12074 if (uscmd->uscsi_bufaddr != NULL) {
12075 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12076 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n",
12077 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12078 if (dataspace == UIO_SYSSPACE) {
12079 SD_DUMP_MEMORY(un, SD_LOG_IO,
12080 "data", (uchar_t *)uscmd->uscsi_bufaddr,
12081 uscmd->uscsi_buflen, SD_LOG_HEX);
12082 }
12411 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12412
12413 xp = SD_GET_XBUF(bp);
12414 ASSERT(xp != NULL);
12415
12416 /* xbuf is gone after this */
12417 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12418 mutex_enter(SD_MUTEX(un));
12419
12420 /*
12421 * Grab time when the cmd completed.
12422 * This is used for determining if the system has been
12423 * idle long enough to make it idle to the PM framework.
12424 * This is for lowering the overhead, and therefore improving
12425 * performance per I/O operation.
12426 */
12427 un->un_pm_idle_time = gethrtime();
12428
12429 un->un_ncmds_in_driver--;
12430 ASSERT(un->un_ncmds_in_driver >= 0);
12431 SD_INFO(SD_LOG_IO, un,
12432 "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12433 un->un_ncmds_in_driver);
12434
12435 mutex_exit(SD_MUTEX(un));
12436 }
12437
12438 biodone(bp); /* bp is gone after this */
12439
12440 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12441 }
12442
12443
12444 /*
12445 * Function: sd_uscsi_iodone
12446 *
12447 * Description: Frees the sd_xbuf & returns the buf to its originator.
12448 *
12449 * Context: May be called from interrupt context.
12450 */
12461 ASSERT(xp != NULL);
12462 ASSERT(!mutex_owned(SD_MUTEX(un)));
12463
12464 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12465
12466 bp->b_private = xp->xb_private;
12467
12468 mutex_enter(SD_MUTEX(un));
12469
12470 /*
12471 * Grab time when the cmd completed.
12472 * This is used for determining if the system has been
12473 * idle long enough to make it idle to the PM framework.
12474 * This is for lowering the overhead, and therefore improving
12475 * performance per I/O operation.
12476 */
12477 un->un_pm_idle_time = gethrtime();
12478
12479 un->un_ncmds_in_driver--;
12480 ASSERT(un->un_ncmds_in_driver >= 0);
12481 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12482 un->un_ncmds_in_driver);
12483
12484 mutex_exit(SD_MUTEX(un));
12485
12486 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12487 SENSE_LENGTH) {
12488 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12489 MAX_SENSE_LENGTH);
12490 } else {
12491 kmem_free(xp, sizeof (struct sd_xbuf));
12492 }
12493
12494 biodone(bp);
12495
12496 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12497 }
12498
12499
12500 /*
12858 * For a non-writable CD, a write request is an error
12859 */
12860 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12861 (un->un_f_mmc_writable_media == FALSE)) {
12862 bioerror(bp, EIO);
12863 bp->b_resid = bp->b_bcount;
12864 SD_BEGIN_IODONE(index, un, bp);
12865 return;
12866 }
12867
12868 /*
12869 * We do not need a shadow buf if the device is using
12870 * un->un_sys_blocksize as its block size or if bcount == 0.
12871 * In this case there is no layer-private data block allocated.
12872 */
12873 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12874 (bp->b_bcount == 0)) {
12875 goto done;
12876 }
12877
12878 #if defined(__i386) || defined(__amd64)
12879 /* We do not support non-block-aligned transfers for ROD devices */
12880 ASSERT(!ISROD(un));
12881 #endif
12882
12883 xp = SD_GET_XBUF(bp);
12884 ASSERT(xp != NULL);
12885
12886 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12887 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12888 un->un_tgt_blocksize, DEV_BSIZE);
12889 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12890 "request start block:0x%x\n", xp->xb_blkno);
12891 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12892 "request len:0x%x\n", bp->b_bcount);
12893
12894 /*
12895 * Allocate the layer-private data area for the mapblocksize layer.
12896 * Layers are allowed to use the xp_private member of the sd_xbuf
12897 * struct to store the pointer to their layer-private data block, but
12898 * each layer also has the responsibility of restoring the prior
12899 * contents of xb_private before returning the buf/xbuf to the
12900 * higher layer that sent it.
12901 *
13560 * The xfer time difference of 6 vs 10 byte CDBs is
13561 * still significant so this code is still worthwhile.
13562 * 10 byte CDBs are very inefficient with the fas HBA driver
13563 * and older disks. Each CDB byte took 1 usec with some
13564 * popular disks.
13565 *
13566 * Context: Must be called at attach time
13567 */
13568
13569 static void
13570 sd_init_cdb_limits(struct sd_lun *un)
13571 {
13572 int hba_cdb_limit;
13573
13574 /*
13575 * Use CDB_GROUP1 commands for most devices except for
13576 * parallel SCSI fixed drives in which case we get better
13577 * performance using CDB_GROUP0 commands (where applicable).
13578 */
13579 un->un_mincdb = SD_CDB_GROUP1;
13580 #if !defined(__fibre)
13581 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13582 !un->un_f_has_removable_media) {
13583 un->un_mincdb = SD_CDB_GROUP0;
13584 }
13585 #endif
13586
13587 /*
13588 * Try to read the max-cdb-length supported by HBA.
13589 */
13590 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13591 if (0 >= un->un_max_hba_cdb) {
13592 un->un_max_hba_cdb = CDB_GROUP4;
13593 hba_cdb_limit = SD_CDB_GROUP4;
13594 } else if (0 < un->un_max_hba_cdb &&
13595 un->un_max_hba_cdb < CDB_GROUP1) {
13596 hba_cdb_limit = SD_CDB_GROUP0;
13597 } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13598 un->un_max_hba_cdb < CDB_GROUP5) {
13599 hba_cdb_limit = SD_CDB_GROUP1;
13600 } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13601 un->un_max_hba_cdb < CDB_GROUP4) {
13602 hba_cdb_limit = SD_CDB_GROUP5;
13603 } else {
13604 hba_cdb_limit = SD_CDB_GROUP4;
13605 }
13606
13607 /*
13608 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4
13609 * commands for fixed disks unless we are building for a 32 bit
13610 * kernel.
13611 */
13612 #ifdef _LP64
13613 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13614 min(hba_cdb_limit, SD_CDB_GROUP4);
13615 #else
13616 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13617 min(hba_cdb_limit, SD_CDB_GROUP1);
13618 #endif
13619
13620 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13621 ? sizeof (struct scsi_arq_status) : 1);
13622 if (!ISCD(un))
13623 un->un_cmd_timeout = (ushort_t)sd_io_time;
13624 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13625 }
13626
13627
13628 /*
13629 * Function: sd_initpkt_for_buf
13630 *
13631 * Description: Allocate and initialize for transport a scsi_pkt struct,
13632 * based upon the info specified in the given buf struct.
13633 *
13634 * Assumes the xb_blkno in the request is absolute (ie,
13635 * relative to the start of the device (NOT partition!).
13636 * Also assumes that the request is using the native block
13637 * size of the device (as returned by the READ CAPACITY
13638 * command).
13639 *
13640 * Return Code: SD_PKT_ALLOC_SUCCESS
13641 * SD_PKT_ALLOC_FAILURE
13642 * SD_PKT_ALLOC_FAILURE_NO_DMA
13643 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13655 struct sd_lun *un;
13656 size_t blockcount;
13657 daddr_t startblock;
13658 int rval;
13659 int cmd_flags;
13660
13661 ASSERT(bp != NULL);
13662 ASSERT(pktpp != NULL);
13663 xp = SD_GET_XBUF(bp);
13664 ASSERT(xp != NULL);
13665 un = SD_GET_UN(bp);
13666 ASSERT(un != NULL);
13667 ASSERT(mutex_owned(SD_MUTEX(un)));
13668 ASSERT(bp->b_resid == 0);
13669
13670 SD_TRACE(SD_LOG_IO_CORE, un,
13671 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13672
13673 mutex_exit(SD_MUTEX(un));
13674
13675 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13676 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13677 /*
13678 * Already have a scsi_pkt -- just need DMA resources.
13679 * We must recompute the CDB in case the mapping returns
13680 * a nonzero pkt_resid.
13681 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13682 * that is being retried, the unmap/remap of the DMA resouces
13683 * will result in the entire transfer starting over again
13684 * from the very first block.
13685 */
13686 ASSERT(xp->xb_pktp != NULL);
13687 pktp = xp->xb_pktp;
13688 } else {
13689 pktp = NULL;
13690 }
13691 #endif /* __i386 || __amd64 */
13692
13693 startblock = xp->xb_blkno; /* Absolute block num. */
13694 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13695
13696 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13697
13698 /*
13699 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13700 * call scsi_init_pkt, and build the CDB.
13701 */
13702 rval = sd_setup_rw_pkt(un, &pktp, bp,
13703 cmd_flags, sdrunout, (caddr_t)un,
13704 startblock, blockcount);
13705
13706 if (rval == 0) {
13707 /*
13708 * Success.
13709 *
13710 * If partial DMA is being used and required for this transfer.
13711 * set it up here.
13719 */
13720 xp->xb_dma_resid = pktp->pkt_resid;
13721
13722 /* rezero resid */
13723 pktp->pkt_resid = 0;
13724
13725 } else {
13726 xp->xb_dma_resid = 0;
13727 }
13728
13729 pktp->pkt_flags = un->un_tagflags;
13730 pktp->pkt_time = un->un_cmd_timeout;
13731 pktp->pkt_comp = sdintr;
13732
13733 pktp->pkt_private = bp;
13734 *pktpp = pktp;
13735
13736 SD_TRACE(SD_LOG_IO_CORE, un,
13737 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13738
13739 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
13740 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13741 #endif
13742
13743 mutex_enter(SD_MUTEX(un));
13744 return (SD_PKT_ALLOC_SUCCESS);
13745
13746 }
13747
13748 /*
13749 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13750 * from sd_setup_rw_pkt.
13751 */
13752 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13753
13754 if (rval == SD_PKT_ALLOC_FAILURE) {
13755 *pktpp = NULL;
13756 /*
13757 * Set the driver state to RWAIT to indicate the driver
13758 * is waiting on resource allocations. The driver will not
13759 * suspend, pm_suspend, or detatch while the state is RWAIT.
13760 */
13761 mutex_enter(SD_MUTEX(un));
14385 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14386 daddr_t blkno, int (*func)(struct buf *))
14387 {
14388 struct sd_lun *un;
14389 struct sd_xbuf *xp;
14390 struct sd_xbuf *new_xp;
14391 struct buf *new_bp;
14392
14393 ASSERT(bp != NULL);
14394 xp = SD_GET_XBUF(bp);
14395 ASSERT(xp != NULL);
14396 un = SD_GET_UN(bp);
14397 ASSERT(un != NULL);
14398 ASSERT(!mutex_owned(SD_MUTEX(un)));
14399
14400 if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14401 bp_mapin(bp);
14402 }
14403
14404 bflags &= (B_READ | B_WRITE);
14405 #if defined(__i386) || defined(__amd64)
14406 new_bp = getrbuf(KM_SLEEP);
14407 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14408 new_bp->b_bcount = datalen;
14409 new_bp->b_flags = bflags |
14410 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14411 #else
14412 new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14413 datalen, bflags, SLEEP_FUNC, NULL);
14414 #endif
14415 new_bp->av_forw = NULL;
14416 new_bp->av_back = NULL;
14417 new_bp->b_dev = bp->b_dev;
14418 new_bp->b_blkno = blkno;
14419 new_bp->b_iodone = func;
14420 new_bp->b_edev = bp->b_edev;
14421 new_bp->b_resid = 0;
14422
14423 /* We need to preserve the B_FAILFAST flag */
14424 if (bp->b_flags & B_FAILFAST) {
14425 new_bp->b_flags |= B_FAILFAST;
14426 }
14427
14428 /*
14429 * Allocate an xbuf for the shadow bp and copy the contents of the
14430 * original xbuf into it.
14431 */
14432 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14433 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14434
14483 kmem_free(xp, sizeof (struct sd_xbuf));
14484 }
14485
14486 /*
14487 * Function: sd_shadow_buf_free
14488 *
14489 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14490 *
14491 * Context: May be called under interrupt context
14492 */
14493
14494 static void
14495 sd_shadow_buf_free(struct buf *bp)
14496 {
14497 struct sd_xbuf *xp;
14498
14499 ASSERT(bp != NULL);
14500 xp = SD_GET_XBUF(bp);
14501 ASSERT(xp != NULL);
14502
14503 #if defined(__sparc)
14504 /*
14505 * Call bp_mapout() before freeing the buf, in case a lower
14506 * layer or HBA had done a bp_mapin(). we must do this here
14507 * as we are the "originator" of the shadow buf.
14508 */
14509 bp_mapout(bp);
14510 #endif
14511
14512 /*
14513 * Null out b_iodone before freeing the bp, to ensure that the driver
14514 * never gets confused by a stale value in this field. (Just a little
14515 * extra defensiveness here.)
14516 */
14517 bp->b_iodone = NULL;
14518
14519 #if defined(__i386) || defined(__amd64)
14520 kmem_free(bp->b_un.b_addr, bp->b_bcount);
14521 freerbuf(bp);
14522 #else
14523 scsi_free_consistent_buf(bp);
14524 #endif
14525
14526 kmem_free(xp, sizeof (struct sd_xbuf));
14527 }
14528
14529
14530 /*
14531 * Function: sd_print_transport_rejected_message
14532 *
14533 * Description: This implements the ludicrously complex rules for printing
14534 * a "transport rejected" message. This is to address the
14535 * specific problem of having a flood of this error message
14536 * produced when a failover occurs.
14537 *
14538 * Context: Any.
14539 */
14540
14541 static void
14542 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14543 int code)
14544 {
14730 * Description: Remove and transport cmds from the driver queues.
14731 *
14732 * Arguments: un - pointer to the unit (soft state) struct for the target.
14733 *
14734 * immed_bp - ptr to a buf to be transported immediately. Only
14735 * the immed_bp is transported; bufs on the waitq are not
14736 * processed and the un_retry_bp is not checked. If immed_bp is
14737 * NULL, then normal queue processing is performed.
14738 *
14739 * Context: May be called from kernel thread context, interrupt context,
14740 * or runout callback context. This function may not block or
14741 * call routines that block.
14742 */
14743
14744 static void
14745 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14746 {
14747 struct sd_xbuf *xp;
14748 struct buf *bp;
14749 void (*statp)(kstat_io_t *);
14750 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14751 void (*saved_statp)(kstat_io_t *);
14752 #endif
14753 int rval;
14754 struct sd_fm_internal *sfip = NULL;
14755
14756 ASSERT(un != NULL);
14757 ASSERT(mutex_owned(SD_MUTEX(un)));
14758 ASSERT(un->un_ncmds_in_transport >= 0);
14759 ASSERT(un->un_throttle >= 0);
14760
14761 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14762
14763 do {
14764 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14765 saved_statp = NULL;
14766 #endif
14767
14768 /*
14769 * If we are syncing or dumping, fail the command to
14770 * avoid recursively calling back into scsi_transport().
14771 * The dump I/O itself uses a separate code path so this
14772 * only prevents non-dump I/O from being sent while dumping.
14773 * File system sync takes place before dumping begins.
14774 * During panic, filesystem I/O is allowed provided
14775 * un_in_callback is <= 1. This is to prevent recursion
14776 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14777 * sd_start_cmds and so on. See panic.c for more information
14778 * about the states the system can be in during panic.
14779 */
14780 if ((un->un_state == SD_STATE_DUMPING) ||
14781 (ddi_in_panic() && (un->un_in_callback > 1))) {
14782 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14783 "sd_start_cmds: panicking\n");
14784 goto exit;
14785 }
14786
14803 statp = kstat_runq_enter;
14804 if (bp == un->un_retry_bp) {
14805 ASSERT((un->un_retry_statp == NULL) ||
14806 (un->un_retry_statp == kstat_waitq_enter) ||
14807 (un->un_retry_statp ==
14808 kstat_runq_back_to_waitq));
14809 /*
14810 * If the waitq kstat was incremented when
14811 * sd_set_retry_bp() queued this bp for a retry,
14812 * then we must set up statp so that the waitq
14813 * count will get decremented correctly below.
14814 * Also we must clear un->un_retry_statp to
14815 * ensure that we do not act on a stale value
14816 * in this field.
14817 */
14818 if ((un->un_retry_statp == kstat_waitq_enter) ||
14819 (un->un_retry_statp ==
14820 kstat_runq_back_to_waitq)) {
14821 statp = kstat_waitq_to_runq;
14822 }
14823 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14824 saved_statp = un->un_retry_statp;
14825 #endif
14826 un->un_retry_statp = NULL;
14827
14828 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14829 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14830 "un_throttle:%d un_ncmds_in_transport:%d\n",
14831 un, un->un_retry_bp, un->un_throttle,
14832 un->un_ncmds_in_transport);
14833 } else {
14834 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14835 "processing priority bp:0x%p\n", bp);
14836 }
14837
14838 } else if ((bp = un->un_waitq_headp) != NULL) {
14839 /*
14840 * A command on the waitq is ready to go, but do not
14841 * send it if:
14842 *
14843 * (1) the throttle limit has been reached, or
14844 * (2) a retry is pending, or
14845 * (3) a START_STOP_UNIT callback pending, or
14882 statp = kstat_waitq_to_runq;
14883 SD_TRACE(SD_LOG_IO_CORE, un,
14884 "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14885
14886 } else {
14887 /* No work to do so bail out now */
14888 SD_TRACE(SD_LOG_IO_CORE, un,
14889 "sd_start_cmds: no more work, exiting!\n");
14890 goto exit;
14891 }
14892
14893 /*
14894 * Reset the state to normal. This is the mechanism by which
14895 * the state transitions from either SD_STATE_RWAIT or
14896 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14897 * If state is SD_STATE_PM_CHANGING then this command is
14898 * part of the device power control and the state must
14899 * not be put back to normal. Doing so would would
14900 * allow new commands to proceed when they shouldn't,
14901 * the device may be going off.
14902 */
14903 if ((un->un_state != SD_STATE_SUSPENDED) &&
14904 (un->un_state != SD_STATE_PM_CHANGING)) {
14905 New_state(un, SD_STATE_NORMAL);
14906 }
14907
14908 xp = SD_GET_XBUF(bp);
14909 ASSERT(xp != NULL);
14910
14911 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14912 /*
14913 * Allocate the scsi_pkt if we need one, or attach DMA
14914 * resources if we have a scsi_pkt that needs them. The
14915 * latter should only occur for commands that are being
14916 * retried.
14917 */
14918 if ((xp->xb_pktp == NULL) ||
14919 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14920 #else
14921 if (xp->xb_pktp == NULL) {
14922 #endif
14923 /*
14924 * There is no scsi_pkt allocated for this buf. Call
14925 * the initpkt function to allocate & init one.
14926 *
14927 * The scsi_init_pkt runout callback functionality is
14928 * implemented as follows:
14929 *
14930 * 1) The initpkt function always calls
14931 * scsi_init_pkt(9F) with sdrunout specified as the
14932 * callback routine.
14933 * 2) A successful packet allocation is initialized and
14934 * the I/O is transported.
14935 * 3) The I/O associated with an allocation resource
14936 * failure is left on its queue to be retried via
14937 * runout or the next I/O.
14938 * 4) The I/O associated with a DMA error is removed
14939 * from the queue and failed with EIO. Processing of
14940 * the transport queues is also halted to be
14941 * restarted via runout or the next I/O.
14942 * 5) The I/O associated with a CDB size or packet
14970 case SD_PKT_ALLOC_SUCCESS:
14971 xp->xb_pktp = pktp;
14972 SD_TRACE(SD_LOG_IO_CORE, un,
14973 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14974 pktp);
14975 goto got_pkt;
14976
14977 case SD_PKT_ALLOC_FAILURE:
14978 /*
14979 * Temporary (hopefully) resource depletion.
14980 * Since retries and RQS commands always have a
14981 * scsi_pkt allocated, these cases should never
14982 * get here. So the only cases this needs to
14983 * handle is a bp from the waitq (which we put
14984 * back onto the waitq for sdrunout), or a bp
14985 * sent as an immed_bp (which we just fail).
14986 */
14987 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14988 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14989
14990 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
14991
14992 if (bp == immed_bp) {
14993 /*
14994 * If SD_XB_DMA_FREED is clear, then
14995 * this is a failure to allocate a
14996 * scsi_pkt, and we must fail the
14997 * command.
14998 */
14999 if ((xp->xb_pkt_flags &
15000 SD_XB_DMA_FREED) == 0) {
15001 break;
15002 }
15003
15004 /*
15005 * If this immediate command is NOT our
15006 * un_retry_bp, then we must fail it.
15007 */
15008 if (bp != un->un_retry_bp) {
15009 break;
15010 }
15011
15043 * keep kstat counts coherent
15044 * when we do retry the command.
15045 */
15046 un->un_retry_statp =
15047 saved_statp;
15048 }
15049
15050 if ((un->un_startstop_timeid == NULL) &&
15051 (un->un_retry_timeid == NULL) &&
15052 (un->un_direct_priority_timeid ==
15053 NULL)) {
15054
15055 un->un_retry_timeid =
15056 timeout(
15057 sd_start_retry_command,
15058 un, SD_RESTART_TIMEOUT);
15059 }
15060 goto exit;
15061 }
15062
15063 #else
15064 if (bp == immed_bp) {
15065 break; /* Just fail the command */
15066 }
15067 #endif
15068
15069 /* Add the buf back to the head of the waitq */
15070 bp->av_forw = un->un_waitq_headp;
15071 un->un_waitq_headp = bp;
15072 if (un->un_waitq_tailp == NULL) {
15073 un->un_waitq_tailp = bp;
15074 }
15075 goto exit;
15076
15077 case SD_PKT_ALLOC_FAILURE_NO_DMA:
15078 /*
15079 * HBA DMA resource failure. Fail the command
15080 * and continue processing of the queues.
15081 */
15082 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15083 "sd_start_cmds: "
15084 "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15085 break;
15086
15087 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15088 /*
15089 * Note:x86: Partial DMA mapping not supported
15090 * for USCSI commands, and all the needed DMA
15091 * resources were not allocated.
15092 */
15093 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15094 "sd_start_cmds: "
15095 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15096 break;
15097
15098 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15099 /*
15100 * Note:x86: Request cannot fit into CDB based
15101 * on lba and len.
15102 */
15103 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15104 "sd_start_cmds: "
15105 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15106 break;
15107
15108 default:
15109 /* Should NEVER get here! */
15110 panic("scsi_initpkt error");
15111 /*NOTREACHED*/
15112 }
15113
15114 /*
15115 * Fatal error in allocating a scsi_pkt for this buf.
15116 * Update kstats & return the buf with an error code.
15117 * We must use sd_return_failed_command_no_restart() to
15118 * avoid a recursive call back into sd_start_cmds().
15119 * However this also means that we must keep processing
15120 * the waitq here in order to avoid stalling.
15121 */
15122 if (statp == kstat_waitq_to_runq) {
15123 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15124 }
15125 sd_return_failed_command_no_restart(un, bp, EIO);
15126 if (bp == immed_bp) {
15127 /* immed_bp is gone by now, so clear this */
15128 immed_bp = NULL;
15129 }
15130 continue;
15131 }
15132 got_pkt:
15133 if (bp == immed_bp) {
15134 /* goto the head of the class.... */
15135 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15136 }
15137
15138 un->un_ncmds_in_transport++;
15139 SD_UPDATE_KSTATS(un, statp, bp);
15140
15141 /*
15142 * Call scsi_transport() to send the command to the target.
15143 * According to SCSA architecture, we must drop the mutex here
15144 * before calling scsi_transport() in order to avoid deadlock.
15145 * Note that the scsi_pkt's completion routine can be executed
15146 * (from interrupt context) even before the call to
15147 * scsi_transport() returns.
15148 */
15149 SD_TRACE(SD_LOG_IO_CORE, un,
15150 "sd_start_cmds: calling scsi_transport()\n");
15151 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15152
15153 mutex_exit(SD_MUTEX(un));
15154 rval = scsi_transport(xp->xb_pktp);
15155 mutex_enter(SD_MUTEX(un));
15156
15157 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15158 "sd_start_cmds: scsi_transport() returned %d\n", rval);
15159
15160 switch (rval) {
15161 case TRAN_ACCEPT:
15162 /* Clear this with every pkt accepted by the HBA */
15163 un->un_tran_fatal_count = 0;
15164 break; /* Success; try the next cmd (if any) */
15165
15166 case TRAN_BUSY:
15167 un->un_ncmds_in_transport--;
15168 ASSERT(un->un_ncmds_in_transport >= 0);
15169
15170 /*
15171 * Don't retry request sense, the sense data
15172 * is lost when another request is sent.
15173 * Free up the rqs buf and retry
15174 * the original failed cmd. Update kstat.
15175 */
15176 if (bp == un->un_rqs_bp) {
15177 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15178 bp = sd_mark_rqs_idle(un, xp);
15179 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15180 NULL, NULL, EIO, un->un_busy_timeout / 500,
15181 kstat_waitq_enter);
15182 goto exit;
15183 }
15184
15185 #if defined(__i386) || defined(__amd64) /* DMAFREE for x86 only */
15186 /*
15187 * Free the DMA resources for the scsi_pkt. This will
15188 * allow mpxio to select another path the next time
15189 * we call scsi_transport() with this scsi_pkt.
15190 * See sdintr() for the rationalization behind this.
15191 */
15192 if ((un->un_f_is_fibre == TRUE) &&
15193 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15194 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15195 scsi_dmafree(xp->xb_pktp);
15196 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15197 }
15198 #endif
15199
15200 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15201 /*
15202 * Commands that are SD_PATH_DIRECT_PRIORITY
15203 * are for error recovery situations. These do
15204 * not use the normal command waitq, so if they
15205 * get a TRAN_BUSY we cannot put them back onto
15206 * the waitq for later retry. One possible
15207 * problem is that there could already be some
15208 * other command on un_retry_bp that is waiting
15209 * for this one to complete, so we would be
15210 * deadlocked if we put this command back onto
15211 * the waitq for later retry (since un_retry_bp
15212 * must complete before the driver gets back to
15213 * commands on the waitq).
15214 *
15215 * To avoid deadlock we must schedule a callback
15216 * that will restart this command after a set
15217 * interval. This should keep retrying for as
15218 * long as the underlying transport keeps
15225 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15226
15227 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15228 un->un_direct_priority_timeid =
15229 timeout(sd_start_direct_priority_command,
15230 bp, un->un_busy_timeout / 500);
15231
15232 goto exit;
15233 }
15234
15235 /*
15236 * For TRAN_BUSY, we want to reduce the throttle value,
15237 * unless we are retrying a command.
15238 */
15239 if (bp != un->un_retry_bp) {
15240 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15241 }
15242
15243 /*
15244 * Set up the bp to be tried again 10 ms later.
15245 * Note:x86: Is there a timeout value in the sd_lun
15246 * for this condition?
15247 */
15248 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15249 kstat_runq_back_to_waitq);
15250 goto exit;
15251
15252 case TRAN_FATAL_ERROR:
15253 un->un_tran_fatal_count++;
15254 /* FALLTHRU */
15255
15256 case TRAN_BADPKT:
15257 default:
15258 un->un_ncmds_in_transport--;
15259 ASSERT(un->un_ncmds_in_transport >= 0);
15260
15261 /*
15262 * If this is our REQUEST SENSE command with a
15263 * transport error, we must get back the pointers
15264 * to the original buf, and mark the REQUEST
15265 * SENSE command as "available".
15354 ASSERT(sfip != NULL);
15355
15356 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15357
15358 /*
15359 * Note: check for the "sdrestart failed" case.
15360 */
15361 if ((un->un_partial_dma_supported == 1) &&
15362 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15363 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15364 (xp->xb_pktp->pkt_resid == 0)) {
15365
15366 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15367 /*
15368 * Successfully set up next portion of cmd
15369 * transfer, try sending it
15370 */
15371 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15372 NULL, NULL, 0, (clock_t)0, NULL);
15373 sd_start_cmds(un, NULL);
15374 return; /* Note:x86: need a return here? */
15375 }
15376 }
15377
15378 /*
15379 * If this is the failfast bp, clear it from un_failfast_bp. This
15380 * can happen if upon being re-tried the failfast bp either
15381 * succeeded or encountered another error (possibly even a different
15382 * error than the one that precipitated the failfast state, but in
15383 * that case it would have had to exhaust retries as well). Regardless,
15384 * this should not occur whenever the instance is in the active
15385 * failfast state.
15386 */
15387 if (bp == un->un_failfast_bp) {
15388 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15389 un->un_failfast_bp = NULL;
15390 }
15391
15392 /*
15393 * Clear the failfast state upon successful completion of ANY cmd.
15394 */
15570 *
15571 * Arguments: un - Pointer to the sd_lun struct for the target.
15572 *
15573 * bp - Pointer to the buf for the command to be retried.
15574 *
15575 * retry_check_flag - Flag to see which (if any) of the retry
15576 * counts should be decremented/checked. If the indicated
15577 * retry count is exhausted, then the command will not be
15578 * retried; it will be failed instead. This should use a
15579 * value equal to one of the following:
15580 *
15581 * SD_RETRIES_NOCHECK
15582 * SD_RESD_RETRIES_STANDARD
15583 * SD_RETRIES_VICTIM
15584 *
15585 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15586 * if the check should be made to see of FLAG_ISOLATE is set
15587 * in the pkt. If FLAG_ISOLATE is set, then the command is
15588 * not retried, it is simply failed.
15589 *
15590 * user_funcp - Ptr to function to call before dispatching the
15591 * command. May be NULL if no action needs to be performed.
15592 * (Primarily intended for printing messages.)
15593 *
15594 * user_arg - Optional argument to be passed along to
15595 * the user_funcp call.
15596 *
15597 * failure_code - errno return code to set in the bp if the
15598 * command is going to be failed.
15599 *
15600 * retry_delay - Retry delay interval in (clock_t) units. May
15601 * be zero which indicates that the retry should be retried
15602 * immediately (ie, without an intervening delay).
15603 *
15604 * statp - Ptr to kstat function to be updated if the command
15605 * is queued for a delayed retry. May be NULL if no kstat
15606 * update is desired.
15607 *
15608 * Context: May be called from interrupt context.
15609 */
15674 }
15675 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15676 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15677 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15678 return;
15679 default:
15680 break;
15681 }
15682
15683 /*
15684 * If the caller wants us to check FLAG_ISOLATE, then see if that
15685 * is set; if it is then we do not want to retry the command.
15686 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15687 */
15688 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15689 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15690 goto fail_command;
15691 }
15692 }
15693
15694
15695 /*
15696 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15697 * command timeout or a selection timeout has occurred. This means
15698 * that we were unable to establish an kind of communication with
15699 * the target, and subsequent retries and/or commands are likely
15700 * to encounter similar results and take a long time to complete.
15701 *
15702 * If this is a failfast error condition, we need to update the
15703 * failfast state, even if this bp does not have B_FAILFAST set.
15704 */
15705 if (retry_check_flag & SD_RETRIES_FAILFAST) {
15706 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15707 ASSERT(un->un_failfast_bp == NULL);
15708 /*
15709 * If we are already in the active failfast state, and
15710 * another failfast error condition has been detected,
15711 * then fail this command if it has B_FAILFAST set.
15712 * If B_FAILFAST is clear, then maintain the legacy
15713 * behavior of retrying heroically, even tho this will
15725 */
15726 if (un->un_failfast_bp == NULL) {
15727 /*
15728 * This is the first bp to meet a failfast
15729 * condition so save it on un_failfast_bp &
15730 * do normal retry processing. Do not enter
15731 * active failfast state yet. This marks
15732 * entry into the "failfast pending" state.
15733 */
15734 un->un_failfast_bp = bp;
15735
15736 } else if (un->un_failfast_bp == bp) {
15737 /*
15738 * This is the second time *this* bp has
15739 * encountered a failfast error condition,
15740 * so enter active failfast state & flush
15741 * queues as appropriate.
15742 */
15743 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15744 un->un_failfast_bp = NULL;
15745 sd_failfast_flushq(un);
15746
15747 /*
15748 * Fail this bp now if B_FAILFAST set;
15749 * otherwise continue with retries. (It would
15750 * be pretty ironic if this bp succeeded on a
15751 * subsequent retry after we just flushed all
15752 * the queues).
15753 */
15754 if (bp->b_flags & B_FAILFAST) {
15755 goto fail_command;
15756 }
15757
15758 #if !defined(lint) && !defined(__lint)
15759 } else {
15760 /*
15761 * If neither of the preceeding conditionals
15762 * was true, it means that there is some
15763 * *other* bp that has met an inital failfast
15764 * condition and is currently either being
15765 * retried or is waiting to be retried. In
15766 * that case we should perform normal retry
15767 * processing on *this* bp, since there is a
15768 * chance that the current failfast condition
15769 * is transient and recoverable. If that does
15770 * not turn out to be the case, then retries
15771 * will be cleared when the wait queue is
15772 * flushed anyway.
15773 */
15774 #endif
15775 }
15776 }
15777 } else {
15778 /*
15779 * SD_RETRIES_FAILFAST is clear, which indicates that we
15780 * likely were able to at least establish some level of
15781 * communication with the target and subsequent commands
15782 * and/or retries are likely to get through to the target,
15783 * In this case we want to be aggressive about clearing
15784 * the failfast state. Note that this does not affect
15785 * the "failfast pending" condition.
15786 */
15787 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15788 }
15789
15790
15791 /*
15792 * Check the specified retry count to see if we can still do
15793 * any retries with this pkt before we should fail it.
15794 */
15795 switch (retry_check_flag & SD_RETRIES_MASK) {
15796 case SD_RETRIES_VICTIM:
15797 /*
15798 * Check the victim retry count. If exhausted, then fall
15799 * thru & check against the standard retry count.
15800 */
15801 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15802 /* Increment count & proceed with the retry */
15803 xp->xb_victim_retry_count++;
15804 break;
15805 }
15806 /* Victim retries exhausted, fall back to std. retries... */
16208 "sd_start_direct_priority_command: entry\n");
16209
16210 mutex_enter(SD_MUTEX(un));
16211 un->un_direct_priority_timeid = NULL;
16212 sd_start_cmds(un, priority_bp);
16213 mutex_exit(SD_MUTEX(un));
16214
16215 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16216 "sd_start_direct_priority_command: exit\n");
16217 }
16218
16219
16220 /*
16221 * Function: sd_send_request_sense_command
16222 *
16223 * Description: Sends a REQUEST SENSE command to the target
16224 *
16225 * Context: May be called from interrupt context.
16226 */
16227
16228 static void
16229 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16230 struct scsi_pkt *pktp)
16231 {
16232 ASSERT(bp != NULL);
16233 ASSERT(un != NULL);
16234 ASSERT(mutex_owned(SD_MUTEX(un)));
16235
16236 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16237 "entry: buf:0x%p\n", bp);
16238
16239 /*
16240 * If we are syncing or dumping, then fail the command to avoid a
16241 * recursive callback into scsi_transport(). Also fail the command
16242 * if we are suspended (legacy behavior).
16243 */
16244 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16245 (un->un_state == SD_STATE_DUMPING)) {
16246 sd_return_failed_command(un, bp, EIO);
16247 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16248 "sd_send_request_sense_command: syncing/dumping, exit\n");
16249 return;
16250 }
16251
16252 /*
16253 * Retry the failed command and don't issue the request sense if:
16254 * 1) the sense buf is busy
16255 * 2) we have 1 or more outstanding commands on the target
16256 * (the sense data will be cleared or invalidated any way)
16257 *
16258 * Note: There could be an issue with not checking a retry limit here,
16259 * the problem is determining which retry limit to check.
16260 */
16261 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16262 /* Don't retry if the command is flagged as non-retryable */
16263 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16264 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16265 NULL, NULL, 0, un->un_busy_timeout,
16266 kstat_waitq_enter);
16267 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16268 "sd_send_request_sense_command: "
16269 "at full throttle, retrying exit\n");
16270 } else {
16271 sd_return_failed_command(un, bp, EIO);
16272 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16273 "sd_send_request_sense_command: "
16274 "at full throttle, non-retryable exit\n");
16275 }
16276 return;
16277 }
16278
16279 sd_mark_rqs_busy(un, bp);
16280 sd_start_cmds(un, un->un_rqs_bp);
16281
16282 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16283 "sd_send_request_sense_command: exit\n");
16284 }
16416 if (un->un_rqs_bp == NULL) {
16417 return (DDI_FAILURE);
16418 }
16419
16420 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16421 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16422
16423 if (un->un_rqs_pktp == NULL) {
16424 sd_free_rqs(un);
16425 return (DDI_FAILURE);
16426 }
16427
16428 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16429 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16430 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16431
16432 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16433
16434 /* Set up the other needed members in the ARQ scsi_pkt. */
16435 un->un_rqs_pktp->pkt_comp = sdintr;
16436 un->un_rqs_pktp->pkt_time = sd_io_time;
16437 un->un_rqs_pktp->pkt_flags |=
16438 (FLAG_SENSING | FLAG_HEAD); /* (1222170) */
16439
16440 /*
16441 * Allocate & init the sd_xbuf struct for the RQS command. Do not
16442 * provide any intpkt, destroypkt routines as we take care of
16443 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16444 */
16445 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16446 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16447 xp->xb_pktp = un->un_rqs_pktp;
16448 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16449 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n",
16450 un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16451
16452 /*
16453 * Save the pointer to the request sense private bp so it can
16454 * be retrieved in sdintr.
16455 */
16456 un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16457 ASSERT(un->un_rqs_bp->b_private == xp);
16458
16459 /*
16460 * See if the HBA supports auto-request sense for the specified
16461 * target/lun. If it does, then try to enable it (if not already
16462 * enabled).
16463 *
16464 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16465 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16466 * return success. However, in both of these cases ARQ is always
16467 * enabled and scsi_ifgetcap will always return true. The best approach
16468 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16469 *
16470 * The 3rd case is the HBA (adp) always return enabled on
16471 * scsi_ifgetgetcap even when it's not enable, the best approach
16472 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16473 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16474 */
16475
16476 if (un->un_f_is_fibre == TRUE) {
16477 un->un_f_arq_enabled = TRUE;
16478 } else {
16479 #if defined(__i386) || defined(__amd64)
16480 /*
16481 * Circumvent the Adaptec bug, remove this code when
16482 * the bug is fixed
16483 */
16484 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16485 #endif
16486 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16487 case 0:
16488 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16489 "sd_alloc_rqs: HBA supports ARQ\n");
16490 /*
16491 * ARQ is supported by this HBA but currently is not
16492 * enabled. Attempt to enable it and if successful then
16493 * mark this instance as ARQ enabled.
16494 */
16495 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16496 == 1) {
16497 /* Successfully enabled ARQ in the HBA */
16498 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16499 "sd_alloc_rqs: ARQ enabled\n");
16500 un->un_f_arq_enabled = TRUE;
16501 } else {
16502 /* Could not enable ARQ in the HBA */
16503 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16504 "sd_alloc_rqs: failed ARQ enable\n");
16505 un->un_f_arq_enabled = FALSE;
16760 {
16761 struct sd_lun *un = (struct sd_lun *)arg;
16762
16763 ASSERT(un != NULL);
16764 ASSERT(!mutex_owned(SD_MUTEX(un)));
16765
16766 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16767
16768 mutex_enter(SD_MUTEX(un));
16769 sd_start_cmds(un, NULL);
16770 mutex_exit(SD_MUTEX(un));
16771 /*
16772 * This callback routine always returns 1 (i.e. do not reschedule)
16773 * because we always specify sdrunout as the callback handler for
16774 * scsi_init_pkt inside the call to sd_start_cmds.
16775 */
16776 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16777 return (1);
16778 }
16779
16780
16781 /*
16782 * Function: sdintr
16783 *
16784 * Description: Completion callback routine for scsi_pkt(9S) structs
16785 * sent to the HBA driver via scsi_transport(9F).
16786 *
16787 * Context: Interrupt context
16788 */
16789
16790 static void
16791 sdintr(struct scsi_pkt *pktp)
16792 {
16793 struct buf *bp;
16794 struct sd_xbuf *xp;
16795 struct sd_lun *un;
16796 size_t actual_len;
16797 sd_ssc_t *sscp;
16798
16799 ASSERT(pktp != NULL);
16800 bp = (struct buf *)pktp->pkt_private;
16801 ASSERT(bp != NULL);
16802 xp = SD_GET_XBUF(bp);
16803 ASSERT(xp != NULL);
16804 ASSERT(xp->xb_pktp != NULL);
16805 un = SD_GET_UN(bp);
16806 ASSERT(un != NULL);
16807 ASSERT(!mutex_owned(SD_MUTEX(un)));
16808
16809 #ifdef SD_FAULT_INJECTION
16810
16811 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16812 /* SD FaultInjection */
16813 sd_faultinjection(pktp);
16814
16815 #endif /* SD_FAULT_INJECTION */
16816
16817 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16818 " xp:0x%p, un:0x%p\n", bp, xp, un);
16819
16820 mutex_enter(SD_MUTEX(un));
16821
16822 ASSERT(un->un_fm_private != NULL);
16823 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16824 ASSERT(sscp != NULL);
16825
16826 /* Reduce the count of the #commands currently in transport */
16827 un->un_ncmds_in_transport--;
16828 ASSERT(un->un_ncmds_in_transport >= 0);
16829
16830 /* Increment counter to indicate that the callback routine is active */
16831 un->un_in_callback++;
16832
16833 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16834
16835 #ifdef SDDEBUG
16836 if (bp == un->un_retry_bp) {
16837 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16838 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16839 un, un->un_retry_bp, un->un_ncmds_in_transport);
16840 }
16841 #endif
16842
16843 /*
16844 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16845 * state if needed.
16846 */
16847 if (pktp->pkt_reason == CMD_DEV_GONE) {
16848 /* Prevent multiple console messages for the same failure. */
16849 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16850 un->un_last_pkt_reason = CMD_DEV_GONE;
16851 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16852 "Command failed to complete...Device is gone\n");
16853 }
16854 if (un->un_mediastate != DKIO_DEV_GONE) {
16913 actual_len) {
16914 xp->xb_sense_resid =
16915 (((struct uscsi_cmd *)
16916 (xp->xb_pktinfo))->
16917 uscsi_rqlen) - actual_len;
16918 } else {
16919 xp->xb_sense_resid = 0;
16920 }
16921 }
16922 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16923 SENSE_LENGTH);
16924 }
16925
16926 /* fail the command */
16927 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16928 "sdintr: arq done and FLAG_DIAGNOSE set\n");
16929 sd_return_failed_command(un, bp, EIO);
16930 goto exit;
16931 }
16932
16933 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
16934 /*
16935 * We want to either retry or fail this command, so free
16936 * the DMA resources here. If we retry the command then
16937 * the DMA resources will be reallocated in sd_start_cmds().
16938 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16939 * causes the *entire* transfer to start over again from the
16940 * beginning of the request, even for PARTIAL chunks that
16941 * have already transferred successfully.
16942 */
16943 if ((un->un_f_is_fibre == TRUE) &&
16944 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16945 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16946 scsi_dmafree(pktp);
16947 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16948 }
16949 #endif
16950
16951 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16952 "sdintr: arq done, sd_handle_auto_request_sense\n");
16953
16954 sd_handle_auto_request_sense(un, bp, xp, pktp);
16955 goto exit;
16956 }
16957
16958 /* Next see if this is the REQUEST SENSE pkt for the instance */
16959 if (pktp->pkt_flags & FLAG_SENSING) {
16960 /* This pktp is from the unit's REQUEST_SENSE command */
16961 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16962 "sdintr: sd_handle_request_sense\n");
16963 sd_handle_request_sense(un, bp, xp, pktp);
16964 goto exit;
16965 }
16966
16967 /*
16968 * Check to see if the command successfully completed as requested;
16969 * this is the most common case (and also the hot performance path).
17005 SD_UPDATE_B_RESID(bp, pktp);
17006 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17007 "sdintr: returning uscsi command\n");
17008 } else {
17009 goto not_successful;
17010 }
17011 sd_return_command(un, bp);
17012
17013 /*
17014 * Decrement counter to indicate that the callback routine
17015 * is done.
17016 */
17017 un->un_in_callback--;
17018 ASSERT(un->un_in_callback >= 0);
17019 mutex_exit(SD_MUTEX(un));
17020
17021 return;
17022 }
17023
17024 not_successful:
17025
17026 #if (defined(__i386) || defined(__amd64)) /* DMAFREE for x86 only */
17027 /*
17028 * The following is based upon knowledge of the underlying transport
17029 * and its use of DMA resources. This code should be removed when
17030 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17031 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17032 * and sd_start_cmds().
17033 *
17034 * Free any DMA resources associated with this command if there
17035 * is a chance it could be retried or enqueued for later retry.
17036 * If we keep the DMA binding then mpxio cannot reissue the
17037 * command on another path whenever a path failure occurs.
17038 *
17039 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17040 * causes the *entire* transfer to start over again from the
17041 * beginning of the request, even for PARTIAL chunks that
17042 * have already transferred successfully.
17043 *
17044 * This is only done for non-uscsi commands (and also skipped for the
17045 * driver's internal RQS command). Also just do this for Fibre Channel
17046 * devices as these are the only ones that support mpxio.
17047 */
17048 if ((un->un_f_is_fibre == TRUE) &&
17049 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17050 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
17051 scsi_dmafree(pktp);
17052 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17053 }
17054 #endif
17055
17056 /*
17057 * The command did not successfully complete as requested so check
17058 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17059 * driver command that should not be retried so just return. If
17060 * FLAG_DIAGNOSE is not set the error will be processed below.
17061 */
17062 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17063 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17064 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17065 /*
17066 * Issue a request sense if a check condition caused the error
17067 * (we handle the auto request sense case above), otherwise
17068 * just fail the command.
17069 */
17070 if ((pktp->pkt_reason == CMD_CMPLT) &&
17071 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17072 sd_send_request_sense_command(un, bp, pktp);
17073 } else {
17074 sd_return_failed_command(un, bp, EIO);
17075 }
17076 goto exit;
17077 }
17078
17079 /*
17080 * The command did not successfully complete as requested so process
17081 * the error, retry, and/or attempt recovery.
17082 */
17083 switch (pktp->pkt_reason) {
17084 case CMD_CMPLT:
17085 switch (SD_GET_PKT_STATUS(pktp)) {
17086 case STATUS_GOOD:
17087 /*
17088 * The command completed successfully with a non-zero
17089 * residual
17090 */
17091 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17092 "sdintr: STATUS_GOOD \n");
17696 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17697 (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17698 (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17699 (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17700 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17701 /* Mark the ssc_flags for detecting invalid sense data */
17702 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17703 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17704 "sense-data");
17705 }
17706 goto sense_failed;
17707 }
17708
17709 return (SD_SENSE_DATA_IS_VALID);
17710
17711 sense_failed:
17712 /*
17713 * If the request sense failed (for whatever reason), attempt
17714 * to retry the original command.
17715 */
17716 #if defined(__i386) || defined(__amd64)
17717 /*
17718 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17719 * sddef.h for Sparc platform, and x86 uses 1 binary
17720 * for both SCSI/FC.
17721 * The SD_RETRY_DELAY value need to be adjusted here
17722 * when SD_RETRY_DELAY change in sddef.h
17723 */
17724 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17725 sd_print_sense_failed_msg, msgp, EIO,
17726 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17727 #else
17728 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17729 sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17730 #endif
17731
17732 return (SD_SENSE_DATA_IS_INVALID);
17733 }
17734
17735 /*
17736 * Function: sd_decode_sense
17737 *
17738 * Description: Take recovery action(s) when SCSI Sense Data is received.
17739 *
17740 * Context: Interrupt context.
17741 */
17742
17743 static void
17744 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17745 struct scsi_pkt *pktp)
17746 {
17747 uint8_t sense_key;
17748
17749 ASSERT(un != NULL);
17750 ASSERT(mutex_owned(SD_MUTEX(un)));
19156 */
19157
19158 static void
19159 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19160 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19161 {
19162 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19163
19164 ASSERT(un != NULL);
19165 ASSERT(mutex_owned(SD_MUTEX(un)));
19166 ASSERT(bp != NULL);
19167 ASSERT(xp != NULL);
19168 ASSERT(pktp != NULL);
19169
19170 SD_UPDATE_ERRSTATS(un, sd_harderrs);
19171 SD_UPDATE_RESERVATION_STATUS(un, pktp);
19172
19173 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19174 sd_print_retry_msg : NULL;
19175
19176 sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19177 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19178 }
19179
19180
19181 /*
19182 * Function: sd_pkt_reason_cmd_tag_reject
19183 *
19184 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19185 *
19186 * Context: May be called from interrupt context
19187 */
19188
19189 static void
19190 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19191 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19192 {
19193 ASSERT(un != NULL);
19194 ASSERT(mutex_owned(SD_MUTEX(un)));
19195 ASSERT(bp != NULL);
19196 ASSERT(xp != NULL);
19259 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19260 {
19261 ASSERT(un != NULL);
19262 ASSERT(mutex_owned(SD_MUTEX(un)));
19263 ASSERT(bp != NULL);
19264 ASSERT(xp != NULL);
19265 ASSERT(pktp != NULL);
19266
19267 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19268 "entry: buf:0x%p xp:0x%p\n", bp, xp);
19269
19270 /*
19271 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19272 * command will be retried after the request sense). Otherwise, retry
19273 * the command. Note: we are issuing the request sense even though the
19274 * retry limit may have been reached for the failed command.
19275 */
19276 if (un->un_f_arq_enabled == FALSE) {
19277 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19278 "no ARQ, sending request sense command\n");
19279 sd_send_request_sense_command(un, bp, pktp);
19280 } else {
19281 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19282 "ARQ,retrying request sense command\n");
19283 #if defined(__i386) || defined(__amd64)
19284 /*
19285 * The SD_RETRY_DELAY value need to be adjusted here
19286 * when SD_RETRY_DELAY change in sddef.h
19287 */
19288 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19289 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19290 NULL);
19291 #else
19292 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19293 EIO, SD_RETRY_DELAY, NULL);
19294 #endif
19295 }
19296
19297 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19298 }
19299
19300
19301 /*
19302 * Function: sd_pkt_status_busy
19303 *
19304 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19305 *
19306 * Context: May be called from interrupt context
19307 */
19308
19309 static void
19310 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19311 struct scsi_pkt *pktp)
19312 {
19313 ASSERT(un != NULL);
19314 ASSERT(mutex_owned(SD_MUTEX(un)));
20030 * (This command is mandatory under SCSI-2.)
20031 *
20032 * Set up the CDB for the READ_CAPACITY command. The Partial
20033 * Medium Indicator bit is cleared. The address field must be
20034 * zero if the PMI bit is zero.
20035 */
20036 bzero(&cdb, sizeof (cdb));
20037 bzero(&ucmd_buf, sizeof (ucmd_buf));
20038
20039 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20040
20041 cdb.scc_cmd = SCMD_READ_CAPACITY;
20042
20043 ucmd_buf.uscsi_cdb = (char *)&cdb;
20044 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20045 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf;
20046 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE;
20047 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20048 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20049 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20050 ucmd_buf.uscsi_timeout = 60;
20051
20052 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20053 UIO_SYSSPACE, path_flag);
20054
20055 switch (status) {
20056 case 0:
20057 /* Return failure if we did not get valid capacity data. */
20058 if (ucmd_buf.uscsi_resid != 0) {
20059 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20060 "sd_send_scsi_READ_CAPACITY received invalid "
20061 "capacity data");
20062 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20063 return (EIO);
20064 }
20065 /*
20066 * Read capacity and block size from the READ CAPACITY 10 data.
20067 * This data may be adjusted later due to device specific
20068 * issues.
20069 *
20070 * According to the SCSI spec, the READ CAPACITY 10
20246
20247 /*
20248 * First send a READ_CAPACITY_16 command to the target.
20249 *
20250 * Set up the CDB for the READ_CAPACITY_16 command. The Partial
20251 * Medium Indicator bit is cleared. The address field must be
20252 * zero if the PMI bit is zero.
20253 */
20254 bzero(&cdb, sizeof (cdb));
20255 bzero(&ucmd_buf, sizeof (ucmd_buf));
20256
20257 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20258
20259 ucmd_buf.uscsi_cdb = (char *)&cdb;
20260 ucmd_buf.uscsi_cdblen = CDB_GROUP4;
20261 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf;
20262 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE;
20263 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20264 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20265 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20266 ucmd_buf.uscsi_timeout = 60;
20267
20268 /*
20269 * Read Capacity (16) is a Service Action In command. One
20270 * command byte (0x9E) is overloaded for multiple operations,
20271 * with the second CDB byte specifying the desired operation
20272 */
20273 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20274 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20275
20276 /*
20277 * Fill in allocation length field
20278 */
20279 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20280
20281 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20282 UIO_SYSSPACE, path_flag);
20283
20284 switch (status) {
20285 case 0:
20286 /* Return failure if we did not get valid capacity data. */
20290 "capacity data");
20291 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20292 return (EIO);
20293 }
20294
20295 /*
20296 * Read capacity and block size from the READ CAPACITY 16 data.
20297 * This data may be adjusted later due to device specific
20298 * issues.
20299 *
20300 * According to the SCSI spec, the READ CAPACITY 16
20301 * command returns the following:
20302 *
20303 * bytes 0-7: Maximum logical block address available.
20304 * (MSB in byte:0 & LSB in byte:7)
20305 *
20306 * bytes 8-11: Block length in bytes
20307 * (MSB in byte:8 & LSB in byte:11)
20308 *
20309 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20310 */
20311 capacity = BE_64(capacity16_buf[0]);
20312 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20313 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20314
20315 pbsize = lbasize << lbpb_exp;
20316
20317 /*
20318 * Done with capacity16_buf
20319 */
20320 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20321
20322 /*
20323 * if the reported capacity is set to all 0xf's, then
20324 * this disk is too large. This could only happen with
20325 * a device that supports LBAs larger than 64 bits which
20326 * are not defined by any current T10 standards.
20327 */
20328 if (capacity == 0xffffffffffffffff) {
20329 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20330 "disk is too large");
20331 return (EIO);
20332 }
20333 break; /* Success! */
20334 case EIO:
20468 return (EAGAIN);
20469 }
20470 mutex_exit(SD_MUTEX(un));
20471 }
20472
20473 bzero(&cdb, sizeof (cdb));
20474 bzero(&ucmd_buf, sizeof (ucmd_buf));
20475 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20476
20477 cdb.scc_cmd = SCMD_START_STOP;
20478 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20479 (uchar_t)(flag << 4) : (uchar_t)flag;
20480
20481 ucmd_buf.uscsi_cdb = (char *)&cdb;
20482 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20483 ucmd_buf.uscsi_bufaddr = NULL;
20484 ucmd_buf.uscsi_buflen = 0;
20485 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20486 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20487 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20488 ucmd_buf.uscsi_timeout = 200;
20489
20490 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20491 UIO_SYSSPACE, path_flag);
20492
20493 switch (status) {
20494 case 0:
20495 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20496 break; /* Success! */
20497 case EIO:
20498 switch (ucmd_buf.uscsi_status) {
20499 case STATUS_RESERVATION_CONFLICT:
20500 status = EACCES;
20501 break;
20502 case STATUS_CHECK:
20503 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20504 switch (scsi_sense_key(
20505 (uint8_t *)&sense_buf)) {
20506 case KEY_ILLEGAL_REQUEST:
20507 status = ENOTSUP;
20508 break;
20687 ASSERT(bufaddr != NULL);
20688
20689 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20690
20691 bzero(&cdb, sizeof (cdb));
20692 bzero(&ucmd_buf, sizeof (ucmd_buf));
20693 bzero(bufaddr, buflen);
20694
20695 cdb.scc_cmd = SCMD_INQUIRY;
20696 cdb.cdb_opaque[1] = evpd;
20697 cdb.cdb_opaque[2] = page_code;
20698 FORMG0COUNT(&cdb, buflen);
20699
20700 ucmd_buf.uscsi_cdb = (char *)&cdb;
20701 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20702 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
20703 ucmd_buf.uscsi_buflen = buflen;
20704 ucmd_buf.uscsi_rqbuf = NULL;
20705 ucmd_buf.uscsi_rqlen = 0;
20706 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
20707 ucmd_buf.uscsi_timeout = 200; /* Excessive legacy value */
20708
20709 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20710 UIO_SYSSPACE, SD_PATH_DIRECT);
20711
20712 /*
20713 * Only handle status == 0, the upper-level caller
20714 * will put different assessment based on the context.
20715 */
20716 if (status == 0)
20717 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20718
20719 if ((status == 0) && (residp != NULL)) {
20720 *residp = ucmd_buf.uscsi_resid;
20721 }
20722
20723 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20724
20725 return (status);
20726 }
20727
20790 }
20791
20792 bzero(&cdb, sizeof (cdb));
20793 bzero(&ucmd_buf, sizeof (ucmd_buf));
20794 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20795
20796 cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20797
20798 ucmd_buf.uscsi_cdb = (char *)&cdb;
20799 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20800 ucmd_buf.uscsi_bufaddr = NULL;
20801 ucmd_buf.uscsi_buflen = 0;
20802 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20803 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20804 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20805
20806 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20807 if ((flag & SD_DONT_RETRY_TUR) != 0) {
20808 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20809 }
20810 ucmd_buf.uscsi_timeout = 60;
20811
20812 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20813 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20814 SD_PATH_STANDARD));
20815
20816 switch (status) {
20817 case 0:
20818 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20819 break; /* Success! */
20820 case EIO:
20821 switch (ucmd_buf.uscsi_status) {
20822 case STATUS_RESERVATION_CONFLICT:
20823 status = EACCES;
20824 break;
20825 case STATUS_CHECK:
20826 if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20827 break;
20828 }
20829 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20830 (scsi_sense_key((uint8_t *)&sense_buf) ==
20887 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20888 if (data_bufp == NULL) {
20889 /* Allocate a default buf if the caller did not give one */
20890 ASSERT(data_len == 0);
20891 data_len = MHIOC_RESV_KEY_SIZE;
20892 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20893 no_caller_buf = TRUE;
20894 }
20895
20896 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20897 cdb.cdb_opaque[1] = usr_cmd;
20898 FORMG1COUNT(&cdb, data_len);
20899
20900 ucmd_buf.uscsi_cdb = (char *)&cdb;
20901 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20902 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp;
20903 ucmd_buf.uscsi_buflen = data_len;
20904 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20905 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20906 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20907 ucmd_buf.uscsi_timeout = 60;
20908
20909 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20910 UIO_SYSSPACE, SD_PATH_STANDARD);
20911
20912 switch (status) {
20913 case 0:
20914 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20915
20916 break; /* Success! */
20917 case EIO:
20918 switch (ucmd_buf.uscsi_status) {
20919 case STATUS_RESERVATION_CONFLICT:
20920 status = EACCES;
20921 break;
20922 case STATUS_CHECK:
20923 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20924 (scsi_sense_key((uint8_t *)&sense_buf) ==
20925 KEY_ILLEGAL_REQUEST)) {
20926 status = ENOTSUP;
20927 }
20993 if (usr_bufp == NULL) {
20994 return (EINVAL);
20995 }
20996
20997 bzero(&cdb, sizeof (cdb));
20998 bzero(&ucmd_buf, sizeof (ucmd_buf));
20999 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21000 prp = kmem_zalloc(data_len, KM_SLEEP);
21001
21002 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21003 cdb.cdb_opaque[1] = usr_cmd;
21004 FORMG1COUNT(&cdb, data_len);
21005
21006 ucmd_buf.uscsi_cdb = (char *)&cdb;
21007 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21008 ucmd_buf.uscsi_bufaddr = (caddr_t)prp;
21009 ucmd_buf.uscsi_buflen = data_len;
21010 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21011 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21012 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21013 ucmd_buf.uscsi_timeout = 60;
21014
21015 switch (usr_cmd) {
21016 case SD_SCSI3_REGISTER: {
21017 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21018
21019 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21020 bcopy(ptr->newkey.key, prp->service_key,
21021 MHIOC_RESV_KEY_SIZE);
21022 prp->aptpl = ptr->aptpl;
21023 break;
21024 }
21025 case SD_SCSI3_CLEAR: {
21026 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21027
21028 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21029 break;
21030 }
21031 case SD_SCSI3_RESERVE:
21032 case SD_SCSI3_RELEASE: {
21033 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21188 * if the device supports SYNC_NV bit, turn on
21189 * the SYNC_NV bit to only flush volatile cache
21190 */
21191 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21192 }
21193 mutex_exit(SD_MUTEX(un));
21194
21195 /*
21196 * First get some memory for the uscsi_cmd struct and cdb
21197 * and initialize for SYNCHRONIZE_CACHE cmd.
21198 */
21199 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21200 uscmd->uscsi_cdblen = CDB_GROUP1;
21201 uscmd->uscsi_cdb = (caddr_t)cdb;
21202 uscmd->uscsi_bufaddr = NULL;
21203 uscmd->uscsi_buflen = 0;
21204 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21205 uscmd->uscsi_rqlen = SENSE_LENGTH;
21206 uscmd->uscsi_rqresid = SENSE_LENGTH;
21207 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21208 uscmd->uscsi_timeout = sd_io_time;
21209
21210 /*
21211 * Allocate an sd_uscsi_info struct and fill it with the info
21212 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
21213 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
21214 * since we allocate the buf here in this function, we do not
21215 * need to preserve the prior contents of b_private.
21216 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21217 */
21218 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21219 uip->ui_flags = SD_PATH_DIRECT;
21220 uip->ui_cmdp = uscmd;
21221
21222 bp = getrbuf(KM_SLEEP);
21223 bp->b_private = uip;
21224
21225 /*
21226 * Setup buffer to carry uscsi request.
21227 */
21228 bp->b_flags = B_BUSY;
21335 /*
21336 * Turn on the un_f_sync_cache_required flag
21337 * since the SYNC CACHE command failed
21338 */
21339 mutex_enter(SD_MUTEX(un));
21340 un->un_f_sync_cache_required = TRUE;
21341 mutex_exit(SD_MUTEX(un));
21342
21343 /*
21344 * Don't log an error message if this device
21345 * has removable media.
21346 */
21347 if (!un->un_f_has_removable_media) {
21348 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21349 "SYNCHRONIZE CACHE command failed (%d)\n", status);
21350 }
21351 break;
21352 }
21353
21354 done:
21355 if (uip->ui_dkc.dkc_callback != NULL) {
21356 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21357 }
21358
21359 ASSERT((bp->b_flags & B_REMAPPED) == 0);
21360 freerbuf(bp);
21361 kmem_free(uip, sizeof (struct sd_uscsi_info));
21362 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21363 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21364 kmem_free(uscmd, sizeof (struct uscsi_cmd));
21365
21366 return (status);
21367 }
21368
21369
21370 /*
21371 * Function: sd_send_scsi_GET_CONFIGURATION
21372 *
21373 * Description: Issues the get configuration command to the device.
21374 * Called from sd_check_for_writable_cd & sd_get_media_info
21375 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21376 * Arguments: ssc
21377 * ucmdbuf
21378 * rqbuf
21379 * rqbuflen
21380 * bufaddr
21381 * buflen
21382 * path_flag
21383 *
21384 * Return Code: 0 - Success
21385 * errno return code from sd_ssc_send()
21386 *
21387 * Context: Can sleep. Does not return until command is completed.
21388 *
21389 */
21390
21406 ASSERT(rqbuf != NULL);
21407
21408 SD_TRACE(SD_LOG_IO, un,
21409 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21410
21411 bzero(cdb, sizeof (cdb));
21412 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21413 bzero(rqbuf, rqbuflen);
21414 bzero(bufaddr, buflen);
21415
21416 /*
21417 * Set up cdb field for the get configuration command.
21418 */
21419 cdb[0] = SCMD_GET_CONFIGURATION;
21420 cdb[1] = 0x02; /* Requested Type */
21421 cdb[8] = SD_PROFILE_HEADER_LEN;
21422 ucmdbuf->uscsi_cdb = cdb;
21423 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21424 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21425 ucmdbuf->uscsi_buflen = buflen;
21426 ucmdbuf->uscsi_timeout = sd_io_time;
21427 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21428 ucmdbuf->uscsi_rqlen = rqbuflen;
21429 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21430
21431 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21432 UIO_SYSSPACE, path_flag);
21433
21434 switch (status) {
21435 case 0:
21436 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21437 break; /* Success! */
21438 case EIO:
21439 switch (ucmdbuf->uscsi_status) {
21440 case STATUS_RESERVATION_CONFLICT:
21441 status = EACCES;
21442 break;
21443 default:
21444 break;
21445 }
21446 break;
21499
21500 SD_TRACE(SD_LOG_IO, un,
21501 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21502
21503 bzero(cdb, sizeof (cdb));
21504 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21505 bzero(rqbuf, rqbuflen);
21506 bzero(bufaddr, buflen);
21507
21508 /*
21509 * Set up cdb field for the get configuration command.
21510 */
21511 cdb[0] = SCMD_GET_CONFIGURATION;
21512 cdb[1] = 0x02; /* Requested Type */
21513 cdb[3] = feature;
21514 cdb[8] = buflen;
21515 ucmdbuf->uscsi_cdb = cdb;
21516 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21517 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21518 ucmdbuf->uscsi_buflen = buflen;
21519 ucmdbuf->uscsi_timeout = sd_io_time;
21520 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21521 ucmdbuf->uscsi_rqlen = rqbuflen;
21522 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21523
21524 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21525 UIO_SYSSPACE, path_flag);
21526
21527 switch (status) {
21528 case 0:
21529
21530 break; /* Success! */
21531 case EIO:
21532 switch (ucmdbuf->uscsi_status) {
21533 case STATUS_RESERVATION_CONFLICT:
21534 status = EACCES;
21535 break;
21536 default:
21537 break;
21538 }
21539 break;
21612 cdb.cdb_opaque[2] = page_code;
21613 FORMG0COUNT(&cdb, buflen);
21614 headlen = MODE_HEADER_LENGTH;
21615 } else {
21616 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21617 cdb.cdb_opaque[2] = page_code;
21618 FORMG1COUNT(&cdb, buflen);
21619 headlen = MODE_HEADER_LENGTH_GRP2;
21620 }
21621
21622 ASSERT(headlen <= buflen);
21623 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21624
21625 ucmd_buf.uscsi_cdb = (char *)&cdb;
21626 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21627 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21628 ucmd_buf.uscsi_buflen = buflen;
21629 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21630 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21631 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21632 ucmd_buf.uscsi_timeout = 60;
21633
21634 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21635 UIO_SYSSPACE, path_flag);
21636
21637 switch (status) {
21638 case 0:
21639 /*
21640 * sr_check_wp() uses 0x3f page code and check the header of
21641 * mode page to determine if target device is write-protected.
21642 * But some USB devices return 0 bytes for 0x3f page code. For
21643 * this case, make sure that mode page header is returned at
21644 * least.
21645 */
21646 if (buflen - ucmd_buf.uscsi_resid < headlen) {
21647 status = EIO;
21648 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21649 "mode page header is not returned");
21650 }
21651 break; /* Success! */
21652 case EIO:
21731 cdb.cdb_opaque[1] |= 0x01;
21732 }
21733
21734 if (cdbsize == CDB_GROUP0) {
21735 cdb.scc_cmd = SCMD_MODE_SELECT;
21736 FORMG0COUNT(&cdb, buflen);
21737 } else {
21738 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21739 FORMG1COUNT(&cdb, buflen);
21740 }
21741
21742 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21743
21744 ucmd_buf.uscsi_cdb = (char *)&cdb;
21745 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21746 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21747 ucmd_buf.uscsi_buflen = buflen;
21748 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21749 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21750 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21751 ucmd_buf.uscsi_timeout = 60;
21752
21753 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21754 UIO_SYSSPACE, path_flag);
21755
21756 switch (status) {
21757 case 0:
21758 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21759 break; /* Success! */
21760 case EIO:
21761 switch (ucmd_buf.uscsi_status) {
21762 case STATUS_RESERVATION_CONFLICT:
21763 status = EACCES;
21764 break;
21765 default:
21766 break;
21767 }
21768 break;
21769 default:
21770 break;
21771 }
21869 cdb.scc_cmd = cmd | SCMD_GROUP4;
21870 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21871 FORMG4COUNT(&cdb, block_count);
21872 break;
21873 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */
21874 default:
21875 /* All others reserved */
21876 return (EINVAL);
21877 }
21878
21879 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21880 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21881
21882 ucmd_buf.uscsi_cdb = (char *)&cdb;
21883 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21884 ucmd_buf.uscsi_bufaddr = bufaddr;
21885 ucmd_buf.uscsi_buflen = buflen;
21886 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21887 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21888 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT;
21889 ucmd_buf.uscsi_timeout = 60;
21890 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21891 UIO_SYSSPACE, path_flag);
21892
21893 switch (status) {
21894 case 0:
21895 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21896 break; /* Success! */
21897 case EIO:
21898 switch (ucmd_buf.uscsi_status) {
21899 case STATUS_RESERVATION_CONFLICT:
21900 status = EACCES;
21901 break;
21902 default:
21903 break;
21904 }
21905 break;
21906 default:
21907 break;
21908 }
21909
21949
21950 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21951
21952 bzero(&cdb, sizeof (cdb));
21953 bzero(&ucmd_buf, sizeof (ucmd_buf));
21954 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21955
21956 cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21957 cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21958 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21959 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF);
21960 FORMG1COUNT(&cdb, buflen);
21961
21962 ucmd_buf.uscsi_cdb = (char *)&cdb;
21963 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21964 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21965 ucmd_buf.uscsi_buflen = buflen;
21966 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21967 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21968 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21969 ucmd_buf.uscsi_timeout = 60;
21970
21971 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21972 UIO_SYSSPACE, path_flag);
21973
21974 switch (status) {
21975 case 0:
21976 break;
21977 case EIO:
21978 switch (ucmd_buf.uscsi_status) {
21979 case STATUS_RESERVATION_CONFLICT:
21980 status = EACCES;
21981 break;
21982 case STATUS_CHECK:
21983 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21984 (scsi_sense_key((uint8_t *)&sense_buf) ==
21985 KEY_ILLEGAL_REQUEST) &&
21986 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21987 /*
21988 * ASC 0x24: INVALID FIELD IN CDB
21989 */
22081
22082 SD_TRACE(SD_LOG_IO, un,
22083 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22084
22085 bzero(&cdb, sizeof (cdb));
22086 bzero(&ucmd_buf, sizeof (ucmd_buf));
22087 bzero(bufaddr, buflen);
22088
22089 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22090 cdb.cdb_opaque[1] = 1; /* polled */
22091 cdb.cdb_opaque[4] = class_req;
22092 FORMG1COUNT(&cdb, buflen);
22093
22094 ucmd_buf.uscsi_cdb = (char *)&cdb;
22095 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
22096 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
22097 ucmd_buf.uscsi_buflen = buflen;
22098 ucmd_buf.uscsi_rqbuf = NULL;
22099 ucmd_buf.uscsi_rqlen = 0;
22100 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
22101 ucmd_buf.uscsi_timeout = 60;
22102
22103 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22104 UIO_SYSSPACE, SD_PATH_DIRECT);
22105
22106 /*
22107 * Only handle status == 0, the upper-level caller
22108 * will put different assessment based on the context.
22109 */
22110 if (status == 0) {
22111 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22112
22113 if (ucmd_buf.uscsi_resid != 0) {
22114 status = EIO;
22115 }
22116 }
22117
22118 SD_TRACE(SD_LOG_IO, un,
22119 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22120
22121 return (status);
22163 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22164 {
22165 struct sd_lun *un = NULL;
22166 int err = 0;
22167 int i = 0;
22168 cred_t *cr;
22169 int tmprval = EINVAL;
22170 boolean_t is_valid;
22171 sd_ssc_t *ssc;
22172
22173 /*
22174 * All device accesses go thru sdstrategy where we check on suspend
22175 * status
22176 */
22177 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22178 return (ENXIO);
22179 }
22180
22181 ASSERT(!mutex_owned(SD_MUTEX(un)));
22182
22183 /* Initialize sd_ssc_t for internal uscsi commands */
22184 ssc = sd_ssc_init(un);
22185
22186 is_valid = SD_IS_VALID_LABEL(un);
22187
22188 /*
22189 * Moved this wait from sd_uscsi_strategy to here for
22190 * reasons of deadlock prevention. Internal driver commands,
22191 * specifically those to change a devices power level, result
22192 * in a call to sd_uscsi_strategy.
22193 */
22194 mutex_enter(SD_MUTEX(un));
22195 while ((un->un_state == SD_STATE_SUSPENDED) ||
22196 (un->un_state == SD_STATE_PM_CHANGING)) {
22197 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22198 }
22199 /*
22200 * Twiddling the counter here protects commands from now
22201 * through to the top of sd_uscsi_strategy. Without the
22202 * counter inc. a power down, for example, could get in
22203 * after the above check for state is made and before
22204 * execution gets to the top of sd_uscsi_strategy.
22205 * That would cause problems.
22206 */
22207 un->un_ncmds_in_driver++;
22208
22209 if (!is_valid &&
22210 (flag & (FNDELAY | FNONBLOCK))) {
22211 switch (cmd) {
22212 case DKIOCGGEOM: /* SD_PATH_DIRECT */
22213 case DKIOCGVTOC:
22214 case DKIOCGEXTVTOC:
22215 case DKIOCGAPART:
22216 case DKIOCPARTINFO:
22217 case DKIOCEXTPARTINFO:
22218 case DKIOCSGEOM:
22219 case DKIOCSAPART:
22220 case DKIOCGETEFI:
22221 case DKIOCPARTITION:
22222 case DKIOCSVTOC:
22223 case DKIOCSEXTVTOC:
22224 case DKIOCSETEFI:
22225 case DKIOCGMBOOT:
22226 case DKIOCSMBOOT:
22227 case DKIOCG_PHYGEOM:
22228 case DKIOCG_VIRTGEOM:
22229 #if defined(__i386) || defined(__amd64)
22230 case DKIOCSETEXTPART:
22231 #endif
22232 /* let cmlb handle it */
22233 goto skip_ready_valid;
22234
22235 case CDROMPAUSE:
22236 case CDROMRESUME:
22237 case CDROMPLAYMSF:
22238 case CDROMPLAYTRKIND:
22239 case CDROMREADTOCHDR:
22240 case CDROMREADTOCENTRY:
22241 case CDROMSTOP:
22242 case CDROMSTART:
22243 case CDROMVOLCTRL:
22244 case CDROMSUBCHNL:
22245 case CDROMREADMODE2:
22246 case CDROMREADMODE1:
22247 case CDROMREADOFFSET:
22248 case CDROMSBLKMODE:
22249 case CDROMGBLKMODE:
22250 case CDROMGDRVSPEED:
22251 case CDROMSDRVSPEED:
22252 case CDROMCDDA:
22253 case CDROMCDXA:
22254 case CDROMSUBCODE:
22255 if (!ISCD(un)) {
22256 un->un_ncmds_in_driver--;
22257 ASSERT(un->un_ncmds_in_driver >= 0);
22258 mutex_exit(SD_MUTEX(un));
22259 err = ENOTTY;
22260 goto done_without_assess;
22261 }
22262 break;
22263 case FDEJECT:
22264 case DKIOCEJECT:
22265 case CDROMEJECT:
22266 if (!un->un_f_eject_media_supported) {
22267 un->un_ncmds_in_driver--;
22268 ASSERT(un->un_ncmds_in_driver >= 0);
22269 mutex_exit(SD_MUTEX(un));
22270 err = ENOTTY;
22271 goto done_without_assess;
22272 }
22273 break;
22274 case DKIOCFLUSHWRITECACHE:
22275 mutex_exit(SD_MUTEX(un));
22276 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22277 if (err != 0) {
22278 mutex_enter(SD_MUTEX(un));
22279 un->un_ncmds_in_driver--;
22280 ASSERT(un->un_ncmds_in_driver >= 0);
22281 mutex_exit(SD_MUTEX(un));
22282 err = EIO;
22283 goto done_quick_assess;
22284 }
22285 mutex_enter(SD_MUTEX(un));
22286 /* FALLTHROUGH */
22287 case DKIOCREMOVABLE:
22288 case DKIOCHOTPLUGGABLE:
22289 case DKIOCINFO:
22290 case DKIOCGMEDIAINFO:
22291 case DKIOCGMEDIAINFOEXT:
22292 case DKIOCSOLIDSTATE:
22293 case MHIOCENFAILFAST:
22294 case MHIOCSTATUS:
22295 case MHIOCTKOWN:
22296 case MHIOCRELEASE:
22297 case MHIOCGRP_INKEYS:
22298 case MHIOCGRP_INRESV:
22299 case MHIOCGRP_REGISTER:
22300 case MHIOCGRP_CLEAR:
22319 case CDROMSDRVSPEED:
22320 case FDEJECT: /* for eject command */
22321 case DKIOCEJECT:
22322 case CDROMEJECT:
22323 case DKIOCREMOVABLE:
22324 case DKIOCHOTPLUGGABLE:
22325 break;
22326 default:
22327 if (un->un_f_has_removable_media) {
22328 err = ENXIO;
22329 } else {
22330 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22331 if (err == SD_RESERVED_BY_OTHERS) {
22332 err = EACCES;
22333 } else {
22334 err = EIO;
22335 }
22336 }
22337 un->un_ncmds_in_driver--;
22338 ASSERT(un->un_ncmds_in_driver >= 0);
22339 mutex_exit(SD_MUTEX(un));
22340
22341 goto done_without_assess;
22342 }
22343 }
22344 }
22345
22346 skip_ready_valid:
22347 mutex_exit(SD_MUTEX(un));
22348
22349 switch (cmd) {
22350 case DKIOCINFO:
22351 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22352 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22353 break;
22354
22355 case DKIOCGMEDIAINFO:
22356 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22357 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22358 break;
22362 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22363 break;
22364
22365 case DKIOCGGEOM:
22366 case DKIOCGVTOC:
22367 case DKIOCGEXTVTOC:
22368 case DKIOCGAPART:
22369 case DKIOCPARTINFO:
22370 case DKIOCEXTPARTINFO:
22371 case DKIOCSGEOM:
22372 case DKIOCSAPART:
22373 case DKIOCGETEFI:
22374 case DKIOCPARTITION:
22375 case DKIOCSVTOC:
22376 case DKIOCSEXTVTOC:
22377 case DKIOCSETEFI:
22378 case DKIOCGMBOOT:
22379 case DKIOCSMBOOT:
22380 case DKIOCG_PHYGEOM:
22381 case DKIOCG_VIRTGEOM:
22382 #if defined(__i386) || defined(__amd64)
22383 case DKIOCSETEXTPART:
22384 #endif
22385 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22386
22387 /* TUR should spin up */
22388
22389 if (un->un_f_has_removable_media)
22390 err = sd_send_scsi_TEST_UNIT_READY(ssc,
22391 SD_CHECK_FOR_MEDIA);
22392
22393 else
22394 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22395
22396 if (err != 0)
22397 goto done_with_assess;
22398
22399 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22400 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22401
22402 if ((err == 0) &&
22403 ((cmd == DKIOCSETEFI) ||
22404 ((un->un_f_pkstats_enabled) &&
22743 case CDROMRESUME:
22744 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22745 if (!ISCD(un)) {
22746 err = ENOTTY;
22747 } else {
22748 err = sr_pause_resume(dev, cmd);
22749 }
22750 break;
22751
22752 case CDROMPLAYMSF:
22753 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22754 if (!ISCD(un)) {
22755 err = ENOTTY;
22756 } else {
22757 err = sr_play_msf(dev, (caddr_t)arg, flag);
22758 }
22759 break;
22760
22761 case CDROMPLAYTRKIND:
22762 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22763 #if defined(__i386) || defined(__amd64)
22764 /*
22765 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22766 */
22767 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22768 #else
22769 if (!ISCD(un)) {
22770 #endif
22771 err = ENOTTY;
22772 } else {
22773 err = sr_play_trkind(dev, (caddr_t)arg, flag);
22774 }
22775 break;
22776
22777 case CDROMREADTOCHDR:
22778 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22779 if (!ISCD(un)) {
22780 err = ENOTTY;
22781 } else {
22782 err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22783 }
22784 break;
22785
22786 case CDROMREADTOCENTRY:
22787 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22788 if (!ISCD(un)) {
22789 err = ENOTTY;
22790 } else {
23041 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23042 if (scsi_abort(SD_ADDRESS(un), NULL)) {
23043 err = 0;
23044 } else {
23045 err = EIO;
23046 }
23047 break;
23048 #endif
23049
23050 #ifdef SD_FAULT_INJECTION
23051 /* SDIOC FaultInjection testing ioctls */
23052 case SDIOCSTART:
23053 case SDIOCSTOP:
23054 case SDIOCINSERTPKT:
23055 case SDIOCINSERTXB:
23056 case SDIOCINSERTUN:
23057 case SDIOCINSERTARQ:
23058 case SDIOCPUSH:
23059 case SDIOCRETRIEVE:
23060 case SDIOCRUN:
23061 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23062 "SDIOC detected cmd:0x%X:\n", cmd);
23063 /* call error generator */
23064 sd_faultinjection_ioctl(cmd, arg, un);
23065 err = 0;
23066 break;
23067
23068 #endif /* SD_FAULT_INJECTION */
23069
23070 case DKIOCFLUSHWRITECACHE:
23071 {
23072 struct dk_callback *dkc = (struct dk_callback *)arg;
23073
23074 mutex_enter(SD_MUTEX(un));
23075 if (!un->un_f_sync_cache_supported ||
23076 !un->un_f_write_cache_enabled) {
23077 err = un->un_f_sync_cache_supported ?
23078 0 : ENOTSUP;
23079 mutex_exit(SD_MUTEX(un));
23080 if ((flag & FKIOCTL) && dkc != NULL &&
23081 dkc->dkc_callback != NULL) {
23082 (*dkc->dkc_callback)(dkc->dkc_cookie,
23083 err);
23084 /*
23085 * Did callback and reported error.
23086 * Since we did a callback, ioctl
23087 * should return 0.
23088 */
23089 err = 0;
23090 }
23091 break;
23092 }
23093 mutex_exit(SD_MUTEX(un));
23094
23095 if ((flag & FKIOCTL) && dkc != NULL &&
23096 dkc->dkc_callback != NULL) {
23097 /* async SYNC CACHE request */
23098 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23099 } else {
23100 /* synchronous SYNC CACHE request */
23101 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23102 }
23103 }
23104 break;
23105
23106 case DKIOCGETWCE: {
23107
23108 int wce;
23109
23110 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23111 break;
23112 }
23113
23114 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23115 err = EFAULT;
23116 }
23117 break;
23118 }
23119
23120 case DKIOCSETWCE: {
23121
23122 int wce, sync_supported;
23123 int cur_wce = 0;
23124
23125 if (!un->un_f_cache_mode_changeable) {
23231 mutex_enter(SD_MUTEX(un));
23232
23233 if (err) {
23234 un->un_f_write_cache_enabled = 0;
23235 }
23236 }
23237
23238 un->un_f_wcc_inprog = 0;
23239 cv_broadcast(&un->un_wcc_cv);
23240 mutex_exit(SD_MUTEX(un));
23241 break;
23242 }
23243
23244 default:
23245 err = ENOTTY;
23246 break;
23247 }
23248 mutex_enter(SD_MUTEX(un));
23249 un->un_ncmds_in_driver--;
23250 ASSERT(un->un_ncmds_in_driver >= 0);
23251 mutex_exit(SD_MUTEX(un));
23252
23253
23254 done_without_assess:
23255 sd_ssc_fini(ssc);
23256
23257 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23258 return (err);
23259
23260 done_with_assess:
23261 mutex_enter(SD_MUTEX(un));
23262 un->un_ncmds_in_driver--;
23263 ASSERT(un->un_ncmds_in_driver >= 0);
23264 mutex_exit(SD_MUTEX(un));
23265
23266 done_quick_assess:
23267 if (err != 0)
23268 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23269 /* Uninitialize sd_ssc_t pointer */
23270 sd_ssc_fini(ssc);
23271
23272 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23273 return (err);
23274 }
23275
23276
23277 /*
23278 * Function: sd_dkio_ctrl_info
23279 *
23280 * Description: This routine is the driver entry point for handling controller
23281 * information ioctl requests (DKIOCINFO).
23282 *
23283 * Arguments: dev - the device number
23348 return (EFAULT);
23349 } else {
23350 kmem_free(info, sizeof (struct dk_cinfo));
23351 return (0);
23352 }
23353 }
23354
23355 /*
23356 * Function: sd_get_media_info_com
23357 *
23358 * Description: This routine returns the information required to populate
23359 * the fields for the dk_minfo/dk_minfo_ext structures.
23360 *
23361 * Arguments: dev - the device number
23362 * dki_media_type - media_type
23363 * dki_lbsize - logical block size
23364 * dki_capacity - capacity in blocks
23365 * dki_pbsize - physical block size (if requested)
23366 *
23367 * Return Code: 0
23368 * EACCESS
23369 * EFAULT
23370 * ENXIO
23371 * EIO
23372 */
23373 static int
23374 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23375 diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23376 {
23377 struct sd_lun *un = NULL;
23378 struct uscsi_cmd com;
23379 struct scsi_inquiry *sinq;
23380 u_longlong_t media_capacity;
23381 uint64_t capacity;
23382 uint_t lbasize;
23383 uint_t pbsize;
23384 uchar_t *out_data;
23385 uchar_t *rqbuf;
23386 int rval = 0;
23387 int rtn;
23388 sd_ssc_t *ssc;
23466 *dki_media_type = DK_JAZ;
23467 }
23468 }
23469 } else {
23470 /*
23471 * Not a CD, direct access or optical disk so return
23472 * unknown media
23473 */
23474 *dki_media_type = DK_UNKNOWN;
23475 }
23476 }
23477
23478 /*
23479 * Now read the capacity so we can provide the lbasize,
23480 * pbsize and capacity.
23481 */
23482 if (dki_pbsize && un->un_f_descr_format_supported) {
23483 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23484 &pbsize, SD_PATH_DIRECT);
23485
23486 /*
23487 * Override the physical blocksize if the instance already
23488 * has a larger value.
23489 */
23490 pbsize = MAX(pbsize, un->un_phy_blocksize);
23491 }
23492
23493 if (dki_pbsize == NULL || rval != 0 ||
23494 !un->un_f_descr_format_supported) {
23495 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23496 SD_PATH_DIRECT);
23497
23498 switch (rval) {
23499 case 0:
23500 if (un->un_f_enable_rmw &&
23501 un->un_phy_blocksize != 0) {
23502 pbsize = un->un_phy_blocksize;
23503 } else {
23504 pbsize = lbasize;
23505 }
23506 media_capacity = capacity;
23507
23508 /*
23509 * sd_send_scsi_READ_CAPACITY() reports capacity in
23730
23731 sd_pm_exit(un);
23732
23733 mutex_enter(SD_MUTEX(un));
23734 if (token == NULL) {
23735 rval = EAGAIN;
23736 goto done;
23737 }
23738
23739 /*
23740 * This is a special case IOCTL that doesn't return
23741 * until the media state changes. Routine sdpower
23742 * knows about and handles this so don't count it
23743 * as an active cmd in the driver, which would
23744 * keep the device busy to the pm framework.
23745 * If the count isn't decremented the device can't
23746 * be powered down.
23747 */
23748 un->un_ncmds_in_driver--;
23749 ASSERT(un->un_ncmds_in_driver >= 0);
23750
23751 /*
23752 * if a prior request had been made, this will be the same
23753 * token, as scsi_watch was designed that way.
23754 */
23755 un->un_swr_token = token;
23756 un->un_specified_mediastate = state;
23757
23758 /*
23759 * now wait for media change
23760 * we will not be signalled unless mediastate == state but it is
23761 * still better to test for this condition, since there is a
23762 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23763 */
23764 SD_TRACE(SD_LOG_COMMON, un,
23765 "sd_check_media: waiting for media state change\n");
23766 while (un->un_mediastate == state) {
23767 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23768 SD_TRACE(SD_LOG_COMMON, un,
23769 "sd_check_media: waiting for media state "
24223 }
24224
24225 return (rval);
24226 }
24227
24228
24229 /*
24230 * Function: sd_log_page_supported
24231 *
24232 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24233 * supported log pages.
24234 *
24235 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
24236 * structure for this target.
24237 * log_page -
24238 *
24239 * Return Code: -1 - on error (log sense is optional and may not be supported).
24240 * 0 - log page not found.
24241 * 1 - log page found.
24242 */
24243
24244 static int
24245 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24246 {
24247 uchar_t *log_page_data;
24248 int i;
24249 int match = 0;
24250 int log_size;
24251 int status = 0;
24252 struct sd_lun *un;
24253
24254 ASSERT(ssc != NULL);
24255 un = ssc->ssc_un;
24256 ASSERT(un != NULL);
24257
24258 log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24259
24260 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24261 SD_PATH_DIRECT);
24262
24263 if (status != 0) {
24288 SD_ERROR(SD_LOG_COMMON, un,
24289 "sd_log_page_supported: failed log page retrieval\n");
24290 kmem_free(log_page_data, 0xFF);
24291 return (-1);
24292 }
24293
24294 log_size = log_page_data[3];
24295
24296 /*
24297 * The list of supported log pages start from the fourth byte. Check
24298 * until we run out of log pages or a match is found.
24299 */
24300 for (i = 4; (i < (log_size + 4)) && !match; i++) {
24301 if (log_page_data[i] == log_page) {
24302 match++;
24303 }
24304 }
24305 kmem_free(log_page_data, 0xFF);
24306 return (match);
24307 }
24308
24309
24310 /*
24311 * Function: sd_mhdioc_failfast
24312 *
24313 * Description: This routine is the driver entry point for handling ioctl
24314 * requests to enable/disable the multihost failfast option.
24315 * (MHIOCENFAILFAST)
24316 *
24317 * Arguments: dev - the device number
24318 * arg - user specified probing interval.
24319 * flag - this argument is a pass through to ddi_copyxxx()
24320 * directly from the mode argument of ioctl().
24321 *
24322 * Return Code: 0
24323 * EFAULT
24324 * ENXIO
24325 */
24326
24327 static int
24328 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24329 {
25725 int i;
25726 int err;
25727 struct sd_lun *un;
25728 struct scsi_pkt *wr_pktp;
25729 struct buf *wr_bp;
25730 struct buf wr_buf;
25731 daddr_t tgt_byte_offset; /* rmw - byte offset for target */
25732 daddr_t tgt_blkno; /* rmw - blkno for target */
25733 size_t tgt_byte_count; /* rmw - # of bytes to xfer */
25734 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */
25735 size_t io_start_offset;
25736 int doing_rmw = FALSE;
25737 int rval;
25738 ssize_t dma_resid;
25739 daddr_t oblkno;
25740 diskaddr_t nblks = 0;
25741 diskaddr_t start_block;
25742
25743 instance = SDUNIT(dev);
25744 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25745 !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25746 return (ENXIO);
25747 }
25748
25749 _NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25750
25751 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25752
25753 partition = SDPART(dev);
25754 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25755
25756 if (!(NOT_DEVBSIZE(un))) {
25757 int secmask = 0;
25758 int blknomask = 0;
25759
25760 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25761 secmask = un->un_tgt_blocksize - 1;
25762
25763 if (blkno & blknomask) {
25764 SD_TRACE(SD_LOG_DUMP, un,
25765 "sddump: dump start block not modulo %d\n",
25766 un->un_tgt_blocksize);
25767 return (EINVAL);
25768 }
25769
25770 if ((nblk * DEV_BSIZE) & secmask) {
27974 }
27975
27976 buffer = kmem_zalloc(4, KM_SLEEP);
27977 bzero(cdb, CDB_GROUP1);
27978 cdb[0] = SCMD_READ_TOC;
27979 /*
27980 * Specifying a track number of 0x00 in the READ TOC command indicates
27981 * that the TOC header should be returned
27982 */
27983 cdb[6] = 0x00;
27984 /*
27985 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27986 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27987 */
27988 cdb[8] = 0x04;
27989 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27990 com->uscsi_cdb = cdb;
27991 com->uscsi_cdblen = CDB_GROUP1;
27992 com->uscsi_bufaddr = buffer;
27993 com->uscsi_buflen = 0x04;
27994 com->uscsi_timeout = 300;
27995 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27996
27997 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27998 SD_PATH_STANDARD);
27999 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28000 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28001 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28002 } else {
28003 hdr->cdth_trk0 = buffer[2];
28004 hdr->cdth_trk1 = buffer[3];
28005 }
28006 kmem_free(buffer, 4);
28007 kmem_free(com, sizeof (*com));
28008 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28009 return (EFAULT);
28010 }
28011 return (rval);
28012 }
28013
28014
29323 } else {
29324 state = SD_WM_LOCK_RANGE;
29325 }
29326
29327 }
29328 break;
29329
29330 case SD_WM_LOCK_RANGE:
29331 ASSERT(un->un_wm_cache);
29332 /*
29333 * The range need to be locked, try to get a wmap.
29334 * First attempt it with NO_SLEEP, want to avoid a sleep
29335 * if possible as we will have to release the sd mutex
29336 * if we have to sleep.
29337 */
29338 if (wmp == NULL)
29339 wmp = kmem_cache_alloc(un->un_wm_cache,
29340 KM_NOSLEEP);
29341 if (wmp == NULL) {
29342 mutex_exit(SD_MUTEX(un));
29343 _NOTE(DATA_READABLE_WITHOUT_LOCK
29344 (sd_lun::un_wm_cache))
29345 wmp = kmem_cache_alloc(un->un_wm_cache,
29346 KM_SLEEP);
29347 mutex_enter(SD_MUTEX(un));
29348 /*
29349 * we released the mutex so recheck and go to
29350 * check list state.
29351 */
29352 state = SD_WM_CHK_LIST;
29353 } else {
29354 /*
29355 * We exit out of state machine since we
29356 * have the wmap. Do the housekeeping first.
29357 * place the wmap on the wmap list if it is not
29358 * on it already and then set the state to done.
29359 */
29360 wmp->wm_start = startb;
29361 wmp->wm_end = endb;
29362 wmp->wm_flags = typ | SD_WM_BUSY;
29363 if (typ & SD_WTYPE_RMW) {
29364 un->un_rmw_count++;
29723 done:
29724 mutex_enter(SD_MUTEX(un));
29725 return (err);
29726 }
29727
29728
29729 /*
29730 * Function: sd_failfast_flushq
29731 *
29732 * Description: Take all bp's on the wait queue that have B_FAILFAST set
29733 * in b_flags and move them onto the failfast queue, then kick
29734 * off a thread to return all bp's on the failfast queue to
29735 * their owners with an error set.
29736 *
29737 * Arguments: un - pointer to the soft state struct for the instance.
29738 *
29739 * Context: may execute in interrupt context.
29740 */
29741
29742 static void
29743 sd_failfast_flushq(struct sd_lun *un)
29744 {
29745 struct buf *bp;
29746 struct buf *next_waitq_bp;
29747 struct buf *prev_waitq_bp = NULL;
29748
29749 ASSERT(un != NULL);
29750 ASSERT(mutex_owned(SD_MUTEX(un)));
29751 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29752 ASSERT(un->un_failfast_bp == NULL);
29753
29754 SD_TRACE(SD_LOG_IO_FAILFAST, un,
29755 "sd_failfast_flushq: entry: un:0x%p\n", un);
29756
29757 /*
29758 * Check if we should flush all bufs when entering failfast state, or
29759 * just those with B_FAILFAST set.
29760 */
29761 if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29762 /*
29763 * Move *all* bp's on the wait queue to the failfast flush
29764 * queue, including those that do NOT have B_FAILFAST set.
29765 */
29766 if (un->un_failfast_headp == NULL) {
29767 ASSERT(un->un_failfast_tailp == NULL);
29768 un->un_failfast_headp = un->un_waitq_headp;
29769 } else {
29770 ASSERT(un->un_failfast_tailp != NULL);
29771 un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29772 }
29773
29774 un->un_failfast_tailp = un->un_waitq_tailp;
29775
29776 /* update kstat for each bp moved out of the waitq */
29777 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29778 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29779 }
29780
29781 /* empty the waitq */
30001 sd_panic_for_res_conflict(struct sd_lun *un)
30002 {
30003 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30004 char path_str[MAXPATHLEN];
30005
30006 (void) snprintf(panic_str, sizeof (panic_str),
30007 "Reservation Conflict\nDisk: %s",
30008 ddi_pathname(SD_DEVINFO(un), path_str));
30009
30010 panic(panic_str);
30011 }
30012
30013 /*
30014 * Note: The following sd_faultinjection_ioctl( ) routines implement
30015 * driver support for handling fault injection for error analysis
30016 * causing faults in multiple layers of the driver.
30017 *
30018 */
30019
30020 #ifdef SD_FAULT_INJECTION
30021 static uint_t sd_fault_injection_on = 0;
30022
30023 /*
30024 * Function: sd_faultinjection_ioctl()
30025 *
30026 * Description: This routine is the driver entry point for handling
30027 * faultinjection ioctls to inject errors into the
30028 * layer model
30029 *
30030 * Arguments: cmd - the ioctl cmd received
30031 * arg - the arguments from user and returns
30032 */
30033
30034 static void
30035 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un)
30036 {
30037 uint_t i = 0;
30038 uint_t rval;
30039
30040 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30041
30042 mutex_enter(SD_MUTEX(un));
30043
30044 switch (cmd) {
30045 case SDIOCRUN:
30046 /* Allow pushed faults to be injected */
30047 SD_INFO(SD_LOG_SDTEST, un,
30048 "sd_faultinjection_ioctl: Injecting Fault Run\n");
30049
30050 sd_fault_injection_on = 1;
30051
30052 SD_INFO(SD_LOG_IOERR, un,
30053 "sd_faultinjection_ioctl: run finished\n");
30054 break;
30055
30056 case SDIOCSTART:
30057 /* Start Injection Session */
30058 SD_INFO(SD_LOG_SDTEST, un,
30059 "sd_faultinjection_ioctl: Injecting Fault Start\n");
30060
30061 sd_fault_injection_on = 0;
30062 un->sd_injection_mask = 0xFFFFFFFF;
30063 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30064 un->sd_fi_fifo_pkt[i] = NULL;
30065 un->sd_fi_fifo_xb[i] = NULL;
30066 un->sd_fi_fifo_un[i] = NULL;
30067 un->sd_fi_fifo_arq[i] = NULL;
30068 }
30069 un->sd_fi_fifo_start = 0;
30070 un->sd_fi_fifo_end = 0;
30071
30072 mutex_enter(&(un->un_fi_mutex));
30073 un->sd_fi_log[0] = '\0';
30074 un->sd_fi_buf_len = 0;
30075 mutex_exit(&(un->un_fi_mutex));
30076
30077 SD_INFO(SD_LOG_IOERR, un,
30078 "sd_faultinjection_ioctl: start finished\n");
30079 break;
30080
30081 case SDIOCSTOP:
30082 /* Stop Injection Session */
30083 SD_INFO(SD_LOG_SDTEST, un,
30084 "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30085 sd_fault_injection_on = 0;
30086 un->sd_injection_mask = 0x0;
30087
30088 /* Empty stray or unuseds structs from fifo */
30089 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30090 if (un->sd_fi_fifo_pkt[i] != NULL) {
30091 kmem_free(un->sd_fi_fifo_pkt[i],
30092 sizeof (struct sd_fi_pkt));
30093 }
30094 if (un->sd_fi_fifo_xb[i] != NULL) {
30095 kmem_free(un->sd_fi_fifo_xb[i],
30096 sizeof (struct sd_fi_xb));
30097 }
30098 if (un->sd_fi_fifo_un[i] != NULL) {
30099 kmem_free(un->sd_fi_fifo_un[i],
30100 sizeof (struct sd_fi_un));
30101 }
30102 if (un->sd_fi_fifo_arq[i] != NULL) {
30103 kmem_free(un->sd_fi_fifo_arq[i],
30104 sizeof (struct sd_fi_arq));
30105 }
30106 un->sd_fi_fifo_pkt[i] = NULL;
30107 un->sd_fi_fifo_un[i] = NULL;
30108 un->sd_fi_fifo_xb[i] = NULL;
30109 un->sd_fi_fifo_arq[i] = NULL;
30110 }
30111 un->sd_fi_fifo_start = 0;
30112 un->sd_fi_fifo_end = 0;
30113
30114 SD_INFO(SD_LOG_IOERR, un,
30115 "sd_faultinjection_ioctl: stop finished\n");
30116 break;
30117
30118 case SDIOCINSERTPKT:
30119 /* Store a packet struct to be pushed onto fifo */
30120 SD_INFO(SD_LOG_SDTEST, un,
30121 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30122
30123 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30124
30125 sd_fault_injection_on = 0;
30126
30127 /* No more that SD_FI_MAX_ERROR allowed in Queue */
30128 if (un->sd_fi_fifo_pkt[i] != NULL) {
30129 kmem_free(un->sd_fi_fifo_pkt[i],
30130 sizeof (struct sd_fi_pkt));
30131 }
30132 if (arg != NULL) {
30133 un->sd_fi_fifo_pkt[i] =
30134 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30135 if (un->sd_fi_fifo_pkt[i] == NULL) {
30136 /* Alloc failed don't store anything */
30137 break;
30138 }
30139 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30140 sizeof (struct sd_fi_pkt), 0);
30141 if (rval == -1) {
30142 kmem_free(un->sd_fi_fifo_pkt[i],
30143 sizeof (struct sd_fi_pkt));
30144 un->sd_fi_fifo_pkt[i] = NULL;
30145 }
30146 } else {
30147 SD_INFO(SD_LOG_IOERR, un,
30148 "sd_faultinjection_ioctl: pkt null\n");
30149 }
30150 break;
30151
30152 case SDIOCINSERTXB:
30153 /* Store a xb struct to be pushed onto fifo */
30154 SD_INFO(SD_LOG_SDTEST, un,
30155 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30156
30157 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30158
30159 sd_fault_injection_on = 0;
30160
30161 if (un->sd_fi_fifo_xb[i] != NULL) {
30162 kmem_free(un->sd_fi_fifo_xb[i],
30163 sizeof (struct sd_fi_xb));
30164 un->sd_fi_fifo_xb[i] = NULL;
30165 }
30166 if (arg != NULL) {
30167 un->sd_fi_fifo_xb[i] =
30168 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30169 if (un->sd_fi_fifo_xb[i] == NULL) {
30170 /* Alloc failed don't store anything */
30171 break;
30172 }
30173 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30174 sizeof (struct sd_fi_xb), 0);
30175
30176 if (rval == -1) {
30177 kmem_free(un->sd_fi_fifo_xb[i],
30178 sizeof (struct sd_fi_xb));
30179 un->sd_fi_fifo_xb[i] = NULL;
30180 }
30181 } else {
30182 SD_INFO(SD_LOG_IOERR, un,
30183 "sd_faultinjection_ioctl: xb null\n");
30184 }
30185 break;
30186
30187 case SDIOCINSERTUN:
30188 /* Store a un struct to be pushed onto fifo */
30189 SD_INFO(SD_LOG_SDTEST, un,
30190 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30191
30192 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30193
30194 sd_fault_injection_on = 0;
30195
30196 if (un->sd_fi_fifo_un[i] != NULL) {
30197 kmem_free(un->sd_fi_fifo_un[i],
30198 sizeof (struct sd_fi_un));
30199 un->sd_fi_fifo_un[i] = NULL;
30200 }
30201 if (arg != NULL) {
30202 un->sd_fi_fifo_un[i] =
30203 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30204 if (un->sd_fi_fifo_un[i] == NULL) {
30205 /* Alloc failed don't store anything */
30206 break;
30207 }
30208 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30209 sizeof (struct sd_fi_un), 0);
30210 if (rval == -1) {
30211 kmem_free(un->sd_fi_fifo_un[i],
30212 sizeof (struct sd_fi_un));
30213 un->sd_fi_fifo_un[i] = NULL;
30214 }
30215
30216 } else {
30217 SD_INFO(SD_LOG_IOERR, un,
30218 "sd_faultinjection_ioctl: un null\n");
30219 }
30220
30221 break;
30222
30223 case SDIOCINSERTARQ:
30224 /* Store a arq struct to be pushed onto fifo */
30225 SD_INFO(SD_LOG_SDTEST, un,
30226 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30227 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30228
30229 sd_fault_injection_on = 0;
30230
30231 if (un->sd_fi_fifo_arq[i] != NULL) {
30232 kmem_free(un->sd_fi_fifo_arq[i],
30233 sizeof (struct sd_fi_arq));
30234 un->sd_fi_fifo_arq[i] = NULL;
30235 }
30236 if (arg != NULL) {
30237 un->sd_fi_fifo_arq[i] =
30238 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30239 if (un->sd_fi_fifo_arq[i] == NULL) {
30240 /* Alloc failed don't store anything */
30241 break;
30242 }
30243 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30244 sizeof (struct sd_fi_arq), 0);
30245 if (rval == -1) {
30246 kmem_free(un->sd_fi_fifo_arq[i],
30247 sizeof (struct sd_fi_arq));
30248 un->sd_fi_fifo_arq[i] = NULL;
30249 }
30250
30251 } else {
30252 SD_INFO(SD_LOG_IOERR, un,
30253 "sd_faultinjection_ioctl: arq null\n");
30254 }
30255
30256 break;
30257
30258 case SDIOCPUSH:
30259 /* Push stored xb, pkt, un, and arq onto fifo */
30260 sd_fault_injection_on = 0;
30261
30262 if (arg != NULL) {
30263 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30264 if (rval != -1 &&
30265 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30266 un->sd_fi_fifo_end += i;
30267 }
30268 } else {
30269 SD_INFO(SD_LOG_IOERR, un,
30270 "sd_faultinjection_ioctl: push arg null\n");
30271 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30272 un->sd_fi_fifo_end++;
30273 }
30274 }
30275 SD_INFO(SD_LOG_IOERR, un,
30276 "sd_faultinjection_ioctl: push to end=%d\n",
30277 un->sd_fi_fifo_end);
30278 break;
30279
30284
30285 sd_fault_injection_on = 0;
30286
30287 mutex_enter(&(un->un_fi_mutex));
30288 rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30289 un->sd_fi_buf_len+1, 0);
30290 mutex_exit(&(un->un_fi_mutex));
30291
30292 if (rval == -1) {
30293 /*
30294 * arg is possibly invalid setting
30295 * it to NULL for return
30296 */
30297 arg = NULL;
30298 }
30299 break;
30300 }
30301
30302 mutex_exit(SD_MUTEX(un));
30303 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
30304 }
30305
30306
30307 /*
30308 * Function: sd_injection_log()
30309 *
30310 * Description: This routine adds buff to the already existing injection log
30311 * for retrieval via faultinjection_ioctl for use in fault
30312 * detection and recovery
30313 *
30314 * Arguments: buf - the string to add to the log
30315 */
30316
30317 static void
30318 sd_injection_log(char *buf, struct sd_lun *un)
30319 {
30320 uint_t len;
30321
30322 ASSERT(un != NULL);
30323 ASSERT(buf != NULL);
30324
30325 mutex_enter(&(un->un_fi_mutex));
30326
30327 len = min(strlen(buf), 255);
30328 /* Add logged value to Injection log to be returned later */
30329 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30330 uint_t offset = strlen((char *)un->sd_fi_log);
30331 char *destp = (char *)un->sd_fi_log + offset;
30332 int i;
30333 for (i = 0; i < len; i++) {
30334 *destp++ = *buf++;
30335 }
30336 un->sd_fi_buf_len += len;
30337 un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30338 }
30339
30340 mutex_exit(&(un->un_fi_mutex));
30341 }
30342
30343
30344 /*
30345 * Function: sd_faultinjection()
30346 *
30347 * Description: This routine takes the pkt and changes its
30348 * content based on error injection scenerio.
30349 *
30350 * Arguments: pktp - packet to be changed
30351 */
30352
30353 static void
30354 sd_faultinjection(struct scsi_pkt *pktp)
30355 {
30356 uint_t i;
30357 struct sd_fi_pkt *fi_pkt;
30358 struct sd_fi_xb *fi_xb;
30359 struct sd_fi_un *fi_un;
30360 struct sd_fi_arq *fi_arq;
30361 struct buf *bp;
30362 struct sd_xbuf *xb;
30363 struct sd_lun *un;
30548 * If a direct-access disk has no EFI label, sd will check if it has a
30549 * valid VTOC label. Now, sd also does that check for removable media
30550 * and hotpluggable devices.
30551 *
30552 * --------------------------------------------------------------
30553 * Direct-Access removable media hotpluggable | Check Label
30554 * -------------------------------------------------------------
30555 * false false false | No
30556 * false false true | No
30557 * false true false | Yes
30558 * false true true | Yes
30559 * true x x | Yes
30560 * --------------------------------------------------------------
30561 *
30562 *
30563 * 4. Building default VTOC label
30564 *
30565 * As section 3 says, sd checks if some kinds of devices have VTOC label.
30566 * If those devices have no valid VTOC label, sd(7d) will attempt to
30567 * create default VTOC for them. Currently sd creates default VTOC label
30568 * for all devices on x86 platform (VTOC_16), but only for removable
30569 * media devices on SPARC (VTOC_8).
30570 *
30571 * -----------------------------------------------------------
30572 * removable media hotpluggable platform | Default Label
30573 * -----------------------------------------------------------
30574 * false false sparc | No
30575 * false true x86 | Yes
30576 * false true sparc | Yes
30577 * true x x | Yes
30578 * ----------------------------------------------------------
30579 *
30580 *
30581 * 5. Supported blocksizes of target devices
30582 *
30583 * Sd supports non-512-byte blocksize for removable media devices only.
30584 * For other devices, only 512-byte blocksize is supported. This may be
30585 * changed in near future because some RAID devices require non-512-byte
30586 * blocksize
30587 *
30588 * -----------------------------------------------------------
30589 * removable media hotpluggable | non-512-byte blocksize
30590 * -----------------------------------------------------------
30591 * false false | No
30592 * false true | No
30593 * true x | Yes
30594 * -----------------------------------------------------------
30595 *
30596 *
30597 * 6. Automatic mount & unmount
30598 *
30599 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30600 * if a device is removable media device. It return 1 for removable media
30601 * devices, and 0 for others.
30602 *
30603 * The automatic mounting subsystem should distinguish between the types
30604 * of devices and apply automounting policies to each.
30605 *
30606 *
30607 * 7. fdisk partition management
30608 *
30609 * Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30610 * just supports fdisk partitions on x86 platform. On sparc platform, sd
30611 * doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30612 * fdisk partitions on both x86 and SPARC platform.
30613 *
30614 * -----------------------------------------------------------
30615 * platform removable media USB/1394 | fdisk supported
30616 * -----------------------------------------------------------
30617 * x86 X X | true
30618 * ------------------------------------------------------------
30619 * sparc X X | false
30620 * ------------------------------------------------------------
30621 *
30622 *
30623 * 8. MBOOT/MBR
30624 *
30625 * Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30626 * read/write mboot for removable media devices on sparc platform.
30627 *
30628 * -----------------------------------------------------------
30629 * platform removable media USB/1394 | mboot supported
30630 * -----------------------------------------------------------
30631 * x86 X X | true
30632 * ------------------------------------------------------------
30633 * sparc false false | false
30634 * sparc false true | true
30635 * sparc true false | true
30636 * sparc true true | true
30637 * ------------------------------------------------------------
30638 *
30639 *
30640 * 9. error handling during opening device
30641 *
30642 * If failed to open a disk device, an errno is returned. For some kinds
30643 * of errors, different errno is returned depending on if this device is
30644 * a removable media device. This brings USB/1394 hard disks in line with
30645 * expected hard disk behavior. It is not expected that this breaks any
30646 * application.
30647 *
30648 * ------------------------------------------------------
30649 * removable media hotpluggable | errno
30650 * ------------------------------------------------------
30651 * false false | EIO
30652 * false true | EIO
30653 * true x | ENXIO
30654 * ------------------------------------------------------
30655 *
30656 *
30657 * 11. ioctls: DKIOCEJECT, CDROMEJECT
31155 un->un_f_mmc_writable_media;
31156 ((tg_attribute_t *)arg)->media_is_solid_state =
31157 un->un_f_is_solid_state;
31158 ((tg_attribute_t *)arg)->media_is_rotational =
31159 un->un_f_is_rotational;
31160 mutex_exit(SD_MUTEX(un));
31161 return (0);
31162 default:
31163 return (ENOTTY);
31164
31165 }
31166 }
31167
31168 /*
31169 * Function: sd_ssc_ereport_post
31170 *
31171 * Description: Will be called when SD driver need to post an ereport.
31172 *
31173 * Context: Kernel thread or interrupt context.
31174 */
31175
31176 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31177
31178 static void
31179 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31180 {
31181 int uscsi_path_instance = 0;
31182 uchar_t uscsi_pkt_reason;
31183 uint32_t uscsi_pkt_state;
31184 uint32_t uscsi_pkt_statistics;
31185 uint64_t uscsi_ena;
31186 uchar_t op_code;
31187 uint8_t *sensep;
31188 union scsi_cdb *cdbp;
31189 uint_t cdblen = 0;
31190 uint_t senlen = 0;
31191 struct sd_lun *un;
31192 dev_info_t *dip;
31193 char *devid;
31194 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31195 SSC_FLAGS_INVALID_STATUS |
31196 SSC_FLAGS_INVALID_SENSE |
31197 SSC_FLAGS_INVALID_DATA;
31198 char assessment[16];
31199
31200 ASSERT(ssc != NULL);
31201 ASSERT(ssc->ssc_uscsi_cmd != NULL);
31204 un = ssc->ssc_un;
31205 ASSERT(un != NULL);
31206
31207 dip = un->un_sd->sd_dev;
31208
31209 /*
31210 * Get the devid:
31211 * devid will only be passed to non-transport error reports.
31212 */
31213 devid = DEVI(dip)->devi_devid_str;
31214
31215 /*
31216 * If we are syncing or dumping, the command will not be executed
31217 * so we bypass this situation.
31218 */
31219 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31220 (un->un_state == SD_STATE_DUMPING))
31221 return;
31222
31223 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31224 uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31225 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31226 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31227 uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31228
31229 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31230 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31231
31232 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31233 if (cdbp == NULL) {
31234 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31235 "sd_ssc_ereport_post meet empty cdb\n");
31236 return;
31237 }
31238
31239 op_code = cdbp->scc_cmd;
31240
31241 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31242 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31243 ssc->ssc_uscsi_cmd->uscsi_rqresid);
31244
31253 switch (drv_assess) {
31254 case SD_FM_DRV_RECOVERY:
31255 (void) sprintf(assessment, "%s", "recovered");
31256 break;
31257 case SD_FM_DRV_RETRY:
31258 (void) sprintf(assessment, "%s", "retry");
31259 break;
31260 case SD_FM_DRV_NOTICE:
31261 (void) sprintf(assessment, "%s", "info");
31262 break;
31263 case SD_FM_DRV_FATAL:
31264 default:
31265 (void) sprintf(assessment, "%s", "unknown");
31266 }
31267 /*
31268 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31269 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31270 * driver-assessment will always be "recovered" here.
31271 */
31272 if (drv_assess == SD_FM_DRV_RECOVERY) {
31273 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31274 "cmd.disk.recovered", uscsi_ena, devid, NULL,
31275 DDI_NOSLEEP, NULL,
31276 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31277 DEVID_IF_KNOWN(devid),
31278 "driver-assessment", DATA_TYPE_STRING, assessment,
31279 "op-code", DATA_TYPE_UINT8, op_code,
31280 "cdb", DATA_TYPE_UINT8_ARRAY,
31281 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31282 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31283 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31284 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31285 NULL);
31286 return;
31287 }
31288
31289 /*
31290 * If there is un-expected/un-decodable data, we should post
31291 * ereport.io.scsi.cmd.disk.dev.uderr.
31292 * driver-assessment will be set based on parameter drv_assess.
31293 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31294 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31295 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31296 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31297 */
31298 if (ssc->ssc_flags & ssc_invalid_flags) {
31299 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31300 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31301 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31302 NULL, DDI_NOSLEEP, NULL,
31303 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31304 DEVID_IF_KNOWN(devid),
31305 "driver-assessment", DATA_TYPE_STRING,
31306 drv_assess == SD_FM_DRV_FATAL ?
31307 "fail" : assessment,
31308 "op-code", DATA_TYPE_UINT8, op_code,
31309 "cdb", DATA_TYPE_UINT8_ARRAY,
31310 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31311 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31312 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31313 "pkt-stats", DATA_TYPE_UINT32,
31314 uscsi_pkt_statistics,
31315 "stat-code", DATA_TYPE_UINT8,
31316 ssc->ssc_uscsi_cmd->uscsi_status,
31317 "un-decode-info", DATA_TYPE_STRING,
31318 ssc->ssc_info,
31319 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31320 senlen, sensep,
31321 NULL);
31322 } else {
31323 /*
31324 * For other type of invalid data, the
31325 * un-decode-value field would be empty because the
31326 * un-decodable content could be seen from upper
31327 * level payload or inside un-decode-info.
31328 */
31329 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31330 NULL,
31331 "cmd.disk.dev.uderr", uscsi_ena, devid,
31332 NULL, DDI_NOSLEEP, NULL,
31333 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31334 DEVID_IF_KNOWN(devid),
31335 "driver-assessment", DATA_TYPE_STRING,
31336 drv_assess == SD_FM_DRV_FATAL ?
31337 "fail" : assessment,
31338 "op-code", DATA_TYPE_UINT8, op_code,
31339 "cdb", DATA_TYPE_UINT8_ARRAY,
31340 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31341 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31342 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31343 "pkt-stats", DATA_TYPE_UINT32,
31344 uscsi_pkt_statistics,
31345 "stat-code", DATA_TYPE_UINT8,
31346 ssc->ssc_uscsi_cmd->uscsi_status,
31347 "un-decode-info", DATA_TYPE_STRING,
31348 ssc->ssc_info,
31349 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31351 NULL);
31352 }
31353 ssc->ssc_flags &= ~ssc_invalid_flags;
31354 return;
31355 }
31356
31357 if (uscsi_pkt_reason != CMD_CMPLT ||
31358 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31359 /*
31360 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31361 * set inside sd_start_cmds due to errors(bad packet or
31362 * fatal transport error), we should take it as a
31363 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31364 * driver-assessment will be set based on drv_assess.
31365 * We will set devid to NULL because it is a transport
31366 * error.
31367 */
31368 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31369 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31370
31371 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31372 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31373 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31374 DEVID_IF_KNOWN(devid),
31375 "driver-assessment", DATA_TYPE_STRING,
31376 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31377 "op-code", DATA_TYPE_UINT8, op_code,
31378 "cdb", DATA_TYPE_UINT8_ARRAY,
31379 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31380 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31381 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31382 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31383 NULL);
31384 } else {
31385 /*
31386 * If we got here, we have a completed command, and we need
31387 * to further investigate the sense data to see what kind
31388 * of ereport we should post.
31389 * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31390 * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31391 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31395 * drv_assess.
31396 */
31397 if (senlen > 0) {
31398 /*
31399 * Here we have sense data available.
31400 */
31401 uint8_t sense_key = scsi_sense_key(sensep);
31402 uint8_t sense_asc = scsi_sense_asc(sensep);
31403 uint8_t sense_ascq = scsi_sense_ascq(sensep);
31404
31405 if (sense_key == KEY_RECOVERABLE_ERROR &&
31406 sense_asc == 0x00 && sense_ascq == 0x1d)
31407 return;
31408
31409 if (sense_key == KEY_MEDIUM_ERROR) {
31410 /*
31411 * driver-assessment should be "fatal" if
31412 * drv_assess is SD_FM_DRV_FATAL.
31413 */
31414 scsi_fm_ereport_post(un->un_sd,
31415 uscsi_path_instance, NULL,
31416 "cmd.disk.dev.rqs.merr",
31417 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31418 FM_VERSION, DATA_TYPE_UINT8,
31419 FM_EREPORT_VERS0,
31420 DEVID_IF_KNOWN(devid),
31421 "driver-assessment",
31422 DATA_TYPE_STRING,
31423 drv_assess == SD_FM_DRV_FATAL ?
31424 "fatal" : assessment,
31425 "op-code",
31426 DATA_TYPE_UINT8, op_code,
31427 "cdb",
31428 DATA_TYPE_UINT8_ARRAY, cdblen,
31429 ssc->ssc_uscsi_cmd->uscsi_cdb,
31430 "pkt-reason",
31431 DATA_TYPE_UINT8, uscsi_pkt_reason,
31432 "pkt-state",
31433 DATA_TYPE_UINT8, uscsi_pkt_state,
31434 "pkt-stats",
31435 DATA_TYPE_UINT32,
31444 DATA_TYPE_UINT8,
31445 scsi_sense_asc(sensep),
31446 "ascq",
31447 DATA_TYPE_UINT8,
31448 scsi_sense_ascq(sensep),
31449 "sense-data",
31450 DATA_TYPE_UINT8_ARRAY,
31451 senlen, sensep,
31452 "lba",
31453 DATA_TYPE_UINT64,
31454 ssc->ssc_uscsi_info->ui_lba,
31455 NULL);
31456 } else {
31457 /*
31458 * if sense-key == 0x4(hardware
31459 * error), driver-assessment should
31460 * be "fatal" if drv_assess is
31461 * SD_FM_DRV_FATAL.
31462 */
31463 scsi_fm_ereport_post(un->un_sd,
31464 uscsi_path_instance, NULL,
31465 "cmd.disk.dev.rqs.derr",
31466 uscsi_ena, devid,
31467 NULL, DDI_NOSLEEP, NULL,
31468 FM_VERSION,
31469 DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31470 DEVID_IF_KNOWN(devid),
31471 "driver-assessment",
31472 DATA_TYPE_STRING,
31473 drv_assess == SD_FM_DRV_FATAL ?
31474 (sense_key == 0x4 ?
31475 "fatal" : "fail") : assessment,
31476 "op-code",
31477 DATA_TYPE_UINT8, op_code,
31478 "cdb",
31479 DATA_TYPE_UINT8_ARRAY, cdblen,
31480 ssc->ssc_uscsi_cmd->uscsi_cdb,
31481 "pkt-reason",
31482 DATA_TYPE_UINT8, uscsi_pkt_reason,
31483 "pkt-state",
31484 DATA_TYPE_UINT8, uscsi_pkt_state,
31499 scsi_sense_ascq(sensep),
31500 "sense-data",
31501 DATA_TYPE_UINT8_ARRAY,
31502 senlen, sensep,
31503 NULL);
31504 }
31505 } else {
31506 /*
31507 * For stat_code == STATUS_GOOD, this is not a
31508 * hardware error.
31509 */
31510 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31511 return;
31512
31513 /*
31514 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31515 * stat-code but with sense data unavailable.
31516 * driver-assessment will be set based on parameter
31517 * drv_assess.
31518 */
31519 scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31520 NULL,
31521 "cmd.disk.dev.serr", uscsi_ena,
31522 devid, NULL, DDI_NOSLEEP, NULL,
31523 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31524 DEVID_IF_KNOWN(devid),
31525 "driver-assessment", DATA_TYPE_STRING,
31526 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31527 "op-code", DATA_TYPE_UINT8, op_code,
31528 "cdb",
31529 DATA_TYPE_UINT8_ARRAY,
31530 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31531 "pkt-reason",
31532 DATA_TYPE_UINT8, uscsi_pkt_reason,
31533 "pkt-state",
31534 DATA_TYPE_UINT8, uscsi_pkt_state,
31535 "pkt-stats",
31536 DATA_TYPE_UINT32, uscsi_pkt_statistics,
31537 "stat-code",
31538 DATA_TYPE_UINT8,
31539 ssc->ssc_uscsi_cmd->uscsi_status,
31753 ASSERT(un != NULL);
31754 ASSERT(!mutex_owned(SD_MUTEX(un)));
31755
31756 mutex_enter(SD_MUTEX(un));
31757 if (ISCD(un)) {
31758 mutex_exit(SD_MUTEX(un));
31759 return;
31760 }
31761
31762 if (un->un_f_descr_format_supported) {
31763 mutex_exit(SD_MUTEX(un));
31764 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31765 &pbsize, SD_PATH_DIRECT);
31766 mutex_enter(SD_MUTEX(un));
31767
31768 if (rval != 0) {
31769 un->un_phy_blocksize = DEV_BSIZE;
31770 } else {
31771 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31772 un->un_phy_blocksize = DEV_BSIZE;
31773 } else if (pbsize > un->un_phy_blocksize) {
31774 /*
31775 * Don't reset the physical blocksize
31776 * unless we've detected a larger value.
31777 */
31778 un->un_phy_blocksize = pbsize;
31779 }
31780 }
31781 }
31782
31783 for (i = 0; i < sd_flash_dev_table_size; i++) {
31784 devid_len = (int)strlen(sd_flash_dev_table[i]);
31785 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31786 == SD_SUCCESS) {
31787 un->un_phy_blocksize = SSD_SECSIZE;
31788 if (un->un_f_is_solid_state &&
31789 un->un_phy_blocksize != un->un_tgt_blocksize)
31790 un->un_f_enable_rmw = TRUE;
31791 }
31792 }
31793
31794 mutex_exit(SD_MUTEX(un));
31795 }
|
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 /*
27 * Copyright 2011 cyril.galibern@opensvc.com
28 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
29 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
30 * Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
31 * Copyright 2019 Nexenta Systems, Inc.
32 */
33
34 /*
35 * SCSI disk target driver.
36 */
37 #include <sys/aio_req.h>
38 #include <sys/byteorder.h>
39 #include <sys/cdio.h>
40 #include <sys/cmlb.h>
41 #include <sys/debug.h>
42 #include <sys/dkbad.h>
43 #include <sys/dkio.h>
44 #include <sys/dkioc_free_util.h>
45 #include <sys/dklabel.h>
46 #include <sys/dktp/fdisk.h>
47 #include <sys/efi_partition.h>
48 #include <sys/fdio.h>
49 #include <sys/fm/protocol.h>
50 #include <sys/fs/dv_node.h>
51 #include <sys/kstat.h>
52 #include <sys/mhd.h>
53 #include <sys/proc.h>
54 #include <sys/scsi/scsi.h>
55 #include <sys/scsi/targets/sddef.h>
56 #include <sys/sdt.h>
57 #include <sys/sysevent/dev.h>
58 #include <sys/sysevent/eventdefs.h>
59 #include <sys/taskq.h>
60 #include <sys/thread.h>
61 #include <sys/uuid.h>
62 #include <sys/var.h>
63 #include <sys/vtoc.h>
64 #include <sys/vtrace.h>
65
66 #include "sd_xbuf.h"
67
68 #define SD_MODULE_NAME "SCSI Disk Driver"
69 static char *sd_label = "sd";
70
71 /*
72 * Driver global variables
73 */
74
75 #ifdef SDDEBUG
76 int sd_force_pm_supported = 0;
77 #endif /* SDDEBUG */
78
79 void *sd_state = NULL;
80 int sd_io_time = SD_IO_TIME;
81 int sd_ua_retry_count = SD_UA_RETRY_COUNT;
82 int sd_report_pfa = 1;
83 int sd_max_throttle = SD_MAX_THROTTLE;
84 int sd_min_throttle = SD_MIN_THROTTLE;
85 int sd_rot_delay = 4; /* Default 4ms Rotation delay */
86 int sd_qfull_throttle_enable = TRUE;
87
88 int sd_retry_on_reservation_conflict = 1;
89 int sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
90 int sd_enable_lun_reset = FALSE;
91
92 /*
93 * Default safe I/O delay threshold of 30s for all devices.
94 * Can be overriden for vendor/device id in sd.conf
95 */
96 hrtime_t sd_slow_io_threshold = 30LL * NANOSEC;
97
98 /*
99 * Global data for debug logging. To enable debug printing, sd_component_mask
100 * and sd_level_mask should be set to the desired bit patterns as outlined in
101 * sddef.h.
102 */
103 uint_t sd_component_mask = 0x0;
104 uint_t sd_level_mask = 0x0;
105 struct sd_lun *sd_debug_un = NULL;
106 uint_t sd_error_level = SCSI_ERR_RETRYABLE;
107
108 /* Note: these may go away in the future... */
109 static uint32_t sd_xbuf_active_limit = 512;
110 static uint32_t sd_xbuf_reserve_limit = 16;
111
112 static struct sd_resv_reclaim_request sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
113
114 /*
115 * Timer value used to reset the throttle after it has been reduced
116 * (typically in response to TRAN_BUSY or STATUS_QFULL)
117 */
123 */
124 static int sd_check_media_time = 3000000;
125
126 /*
127 * Wait value used for in progress operations during a DDI_SUSPEND
128 */
129 static int sd_wait_cmds_complete = SD_WAIT_CMDS_COMPLETE;
130
131 /*
132 * sd_label_mutex protects a static buffer used in the disk label
133 * component of the driver
134 */
135 static kmutex_t sd_label_mutex;
136
137 /*
138 * sd_detach_mutex protects un_layer_count, un_detach_count, and
139 * un_opens_in_progress in the sd_lun structure.
140 */
141 static kmutex_t sd_detach_mutex;
142
143 /*
144 * Global buffer and mutex for debug logging
145 */
146 static char sd_log_buf[1024];
147 static kmutex_t sd_log_mutex;
148
149 /*
150 * Structs and globals for recording attached lun information.
151 * This maintains a chain. Each node in the chain represents a SCSI controller.
152 * The structure records the number of luns attached to each target connected
153 * with the controller.
154 * For parallel scsi device only.
155 */
156 struct sd_scsi_hba_tgt_lun {
157 struct sd_scsi_hba_tgt_lun *next;
158 dev_info_t *pdip;
159 int nlun[NTARGETS_WIDE];
160 };
161
162 /*
163 * Flag to indicate the lun is attached or detached
164 */
165 #define SD_SCSI_LUN_ATTACH 0
166 #define SD_SCSI_LUN_DETACH 1
167
168 static kmutex_t sd_scsi_target_lun_mutex;
169 static struct sd_scsi_hba_tgt_lun *sd_scsi_target_lun_head = NULL;
170
171 /*
172 * "Smart" Probe Caching structs, globals, #defines, etc.
173 * For parallel scsi and non-self-identify device only.
174 */
175
176 /*
177 * The following resources and routines are implemented to support
178 * "smart" probing, which caches the scsi_probe() results in an array,
179 * in order to help avoid long probe times.
180 */
181 struct sd_scsi_probe_cache {
182 struct sd_scsi_probe_cache *next;
183 dev_info_t *pdip;
184 int cache[NTARGETS_WIDE];
185 };
186
187 static kmutex_t sd_scsi_probe_cache_mutex;
188 static struct sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
189
190 /*
191 * Create taskq for all targets in the system. This is created at
192 * _init(9E) and destroyed at _fini(9E).
193 *
194 * Note: here we set the minalloc to a reasonably high number to ensure that
195 * we will have an adequate supply of task entries available at interrupt time.
196 * This is used in conjunction with the TASKQ_PREPOPULATE flag in
197 * sd_create_taskq(). Since we do not want to sleep for allocations at
198 * interrupt time, set maxalloc equal to minalloc. That way we will just fail
199 * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
200 * requests any one instant in time.
201 */
202 #define SD_TASKQ_NUMTHREADS 8
203 #define SD_TASKQ_MINALLOC 256
204 #define SD_TASKQ_MAXALLOC 256
205
206 static taskq_t *sd_tq = NULL;
207
208 static int sd_taskq_minalloc = SD_TASKQ_MINALLOC;
209 static int sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
210
211 #define SD_BAIL_CHECK(a) if ((a)->un_detach_count != 0) { \
212 mutex_exit(SD_MUTEX((a))); \
213 return (ENXIO); \
214 }
215 /*
216 * The following task queue is being created for the write part of
217 * read-modify-write of non-512 block size devices.
218 * Limit the number of threads to 1 for now. This number has been chosen
219 * considering the fact that it applies only to dvd ram drives/MO drives
220 * currently. Performance for which is not main criteria at this stage.
221 * Note: It needs to be explored if we can use a single taskq in future
222 */
223 #define SD_WMR_TASKQ_NUMTHREADS 1
224 static taskq_t *sd_wmr_tq = NULL;
225
226 /*
227 * Power attribute table
228 */
229 static sd_power_attr_ss sd_pwr_ss = {
230 { "NAME=spindle-motor", "0=off", "1=on", NULL },
231 {0, 100},
232 {30, 0},
233 {20000, 0}
234 };
235
236 static sd_power_attr_pc sd_pwr_pc = {
237 { "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
238 "3=active", NULL },
239 {0, 0, 0, 100},
240 {90, 90, 20, 0},
241 {15000, 15000, 1000, 0}
242 };
243
244 /*
245 * Power level to power condition
246 */
247 static int sd_pl2pc[] = {
248 SD_TARGET_START_VALID,
249 SD_TARGET_STANDBY,
250 SD_TARGET_IDLE,
251 SD_TARGET_ACTIVE
252 };
253
254 /*
255 * Vendor specific data name property declarations
256 */
257
258 static sd_tunables seagate_properties = {
259 SEAGATE_THROTTLE_VALUE,
260 0,
261 0,
262 0,
263 0,
264 0,
265 0,
266 0,
267 0
268 };
269
270 static sd_tunables fujitsu_properties = {
271 FUJITSU_THROTTLE_VALUE,
272 0,
273 0,
274 0,
275 0,
276 0,
277 0,
278 0,
279 0
280 };
281
282 static sd_tunables ibm_properties = {
283 IBM_THROTTLE_VALUE,
284 0,
285 0,
286 0,
287 0,
288 0,
289 0,
322 0,
323 0,
324 0,
325 0,
326 MASERATI_DISKSORT_DISABLED_FLAG,
327 MASERATI_LUN_RESET_ENABLED_FLAG
328 };
329
330 static sd_tunables pirus_properties = {
331 PIRUS_THROTTLE_VALUE,
332 0,
333 PIRUS_NRR_COUNT,
334 PIRUS_BUSY_RETRIES,
335 PIRUS_RESET_RETRY_COUNT,
336 0,
337 PIRUS_MIN_THROTTLE_VALUE,
338 PIRUS_DISKSORT_DISABLED_FLAG,
339 PIRUS_LUN_RESET_ENABLED_FLAG
340 };
341
342 static sd_tunables elite_properties = {
343 ELITE_THROTTLE_VALUE,
344 0,
345 0,
346 0,
347 0,
348 0,
349 0,
350 0,
351 0
352 };
353
354 static sd_tunables st31200n_properties = {
355 ST31200N_THROTTLE_VALUE,
356 0,
357 0,
358 0,
359 0,
360 0,
361 0,
362 0,
363 0
364 };
365
366 static sd_tunables lsi_properties_scsi = {
367 LSI_THROTTLE_VALUE,
368 0,
369 LSI_NOTREADY_RETRIES,
370 0,
371 0,
372 0,
373 0,
374 0,
375 0
376 };
377
378 static sd_tunables symbios_properties = {
379 SYMBIOS_THROTTLE_VALUE,
380 0,
381 SYMBIOS_NOTREADY_RETRIES,
382 0,
383 0,
384 0,
385 0,
395 0,
396 0,
397 0,
398 0,
399 0
400 };
401
402 static sd_tunables lsi_oem_properties = {
403 0,
404 0,
405 LSI_OEM_NOTREADY_RETRIES,
406 0,
407 0,
408 0,
409 0,
410 0,
411 0,
412 1
413 };
414
415 #if (defined(SD_PROP_TST))
416 #define SD_TST_CTYPE_VAL CTYPE_CDROM
417 #define SD_TST_THROTTLE_VAL 16
418 #define SD_TST_NOTREADY_VAL 12
419 #define SD_TST_BUSY_VAL 60
420 #define SD_TST_RST_RETRY_VAL 36
421 #define SD_TST_RSV_REL_TIME 60
422 static sd_tunables tst_properties = {
423 SD_TST_THROTTLE_VAL,
424 SD_TST_CTYPE_VAL,
425 SD_TST_NOTREADY_VAL,
426 SD_TST_BUSY_VAL,
427 SD_TST_RST_RETRY_VAL,
428 SD_TST_RSV_REL_TIME,
429 0,
430 0,
431 0
432 };
433 #endif
434
435 /* This is similar to the ANSI toupper implementation */
436 #define SD_TOUPPER(C) (((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
437
438 /*
439 * Static Driver Configuration Table
440 *
441 * This is the table of disks which need throttle adjustment (or, perhaps
446 * structure. Device type is searched as far as the device_id string is
447 * defined. Flags defines which values are to be set in the driver from the
448 * properties list.
449 *
450 * Entries below which begin and end with a "*" are a special case.
451 * These do not have a specific vendor, and the string which follows
452 * can appear anywhere in the 16 byte PID portion of the inquiry data.
453 *
454 * Entries below which begin and end with a " " (blank) are a special
455 * case. The comparison function will treat multiple consecutive blanks
456 * as equivalent to a single blank. For example, this causes a
457 * sd_disk_table entry of " NEC CDROM " to match a device's id string
458 * of "NEC CDROM".
459 *
460 * Note: The MD21 controller type has been obsoleted.
461 * ST318202F is a Legacy device
462 * MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
463 * made with an FC connection. The entries here are a legacy.
464 */
465 static sd_disk_config_t sd_disk_table[] = {
466 { "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
467 { "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
468 { "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
469 { "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
470 { "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
471 { "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
472 { "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
473 { "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
474 { "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
475 { "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
476 { "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
477 { "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
478 { "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
479 { "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
480 { "FUJITSU MAG3091F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
481 { "FUJITSU MAG3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
482 { "FUJITSU MAA3182F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
483 { "FUJITSU MAF3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
484 { "FUJITSU MAL3364F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
485 { "FUJITSU MAL3738F", SD_CONF_BSET_THROTTLE, &fujitsu_properties },
495 { "IBM 1726-22x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
496 { "IBM 1726-4xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
497 { "IBM 1726-42x", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
498 { "IBM 1726-3xx", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
499 { "IBM 3526", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
500 { "IBM 3542", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
501 { "IBM 3552", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
502 { "IBM 1722", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
503 { "IBM 1742", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
504 { "IBM 1815", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
505 { "IBM FAStT", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
506 { "IBM 1814", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
507 { "IBM 1814-200", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
508 { "IBM 1818", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
509 { "DELL MD3000", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
510 { "DELL MD3000i", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
511 { "LSI INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
512 { "ENGENIO INF", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
513 { "SGI TP", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
514 { "SGI IS", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
515 { "*CSM100_*", SD_CONF_BSET_NRR_COUNT|
516 SD_CONF_BSET_CACHE_IS_NV,
517 &lsi_oem_properties },
518 { "*CSM200_*", SD_CONF_BSET_NRR_COUNT|
519 SD_CONF_BSET_CACHE_IS_NV,
520 &lsi_oem_properties },
521 { "Fujitsu SX300", SD_CONF_BSET_THROTTLE, &lsi_oem_properties },
522 { "LSI", SD_CONF_BSET_NRR_COUNT, &lsi_properties },
523 { "SUN T3", SD_CONF_BSET_THROTTLE|
524 SD_CONF_BSET_BSY_RETRY_COUNT|
525 SD_CONF_BSET_RST_RETRIES|
526 SD_CONF_BSET_RSV_REL_TIME,
527 &purple_properties },
528 { "SUN SESS01", SD_CONF_BSET_THROTTLE|
529 SD_CONF_BSET_BSY_RETRY_COUNT|
530 SD_CONF_BSET_RST_RETRIES|
531 SD_CONF_BSET_RSV_REL_TIME|
532 SD_CONF_BSET_MIN_THROTTLE|
533 SD_CONF_BSET_DISKSORT_DISABLED,
534 &sve_properties },
535 { "SUN T4", SD_CONF_BSET_THROTTLE|
536 SD_CONF_BSET_BSY_RETRY_COUNT|
537 SD_CONF_BSET_RST_RETRIES|
538 SD_CONF_BSET_RSV_REL_TIME,
539 &purple_properties },
540 { "SUN SVE01", SD_CONF_BSET_DISKSORT_DISABLED|
541 SD_CONF_BSET_LUN_RESET_ENABLED,
542 &maserati_properties },
543 { "SUN SE6920", SD_CONF_BSET_THROTTLE|
544 SD_CONF_BSET_NRR_COUNT|
545 SD_CONF_BSET_BSY_RETRY_COUNT|
546 SD_CONF_BSET_RST_RETRIES|
547 SD_CONF_BSET_MIN_THROTTLE|
548 SD_CONF_BSET_DISKSORT_DISABLED|
549 SD_CONF_BSET_LUN_RESET_ENABLED,
550 &pirus_properties },
551 { "SUN SE6940", SD_CONF_BSET_THROTTLE|
552 SD_CONF_BSET_NRR_COUNT|
553 SD_CONF_BSET_BSY_RETRY_COUNT|
554 SD_CONF_BSET_RST_RETRIES|
555 SD_CONF_BSET_MIN_THROTTLE|
556 SD_CONF_BSET_DISKSORT_DISABLED|
557 SD_CONF_BSET_LUN_RESET_ENABLED,
558 &pirus_properties },
559 { "SUN StorageTek 6920", SD_CONF_BSET_THROTTLE|
560 SD_CONF_BSET_NRR_COUNT|
561 SD_CONF_BSET_BSY_RETRY_COUNT|
562 SD_CONF_BSET_RST_RETRIES|
563 SD_CONF_BSET_MIN_THROTTLE|
564 SD_CONF_BSET_DISKSORT_DISABLED|
565 SD_CONF_BSET_LUN_RESET_ENABLED,
566 &pirus_properties },
567 { "SUN StorageTek 6940", SD_CONF_BSET_THROTTLE|
568 SD_CONF_BSET_NRR_COUNT|
569 SD_CONF_BSET_BSY_RETRY_COUNT|
570 SD_CONF_BSET_RST_RETRIES|
571 SD_CONF_BSET_MIN_THROTTLE|
572 SD_CONF_BSET_DISKSORT_DISABLED|
573 SD_CONF_BSET_LUN_RESET_ENABLED,
574 &pirus_properties },
575 { "SUN PSX1000", SD_CONF_BSET_THROTTLE|
576 SD_CONF_BSET_NRR_COUNT|
577 SD_CONF_BSET_BSY_RETRY_COUNT|
578 SD_CONF_BSET_RST_RETRIES|
579 SD_CONF_BSET_MIN_THROTTLE|
580 SD_CONF_BSET_DISKSORT_DISABLED|
581 SD_CONF_BSET_LUN_RESET_ENABLED,
582 &pirus_properties },
583 { "SUN SE6330", SD_CONF_BSET_THROTTLE|
584 SD_CONF_BSET_NRR_COUNT|
585 SD_CONF_BSET_BSY_RETRY_COUNT|
586 SD_CONF_BSET_RST_RETRIES|
587 SD_CONF_BSET_MIN_THROTTLE|
588 SD_CONF_BSET_DISKSORT_DISABLED|
589 SD_CONF_BSET_LUN_RESET_ENABLED,
590 &pirus_properties },
591 { "SUN STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
592 { "SUN SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
593 { "STK OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
594 { "STK OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
595 { "STK BladeCtlr", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
596 { "STK FLEXLINE", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
597 { "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
598 { "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
599 { "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
600 { "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
601 { "CONNER CP30540", SD_CONF_BSET_NOCACHE, NULL },
602 { "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
603 { "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
604 { "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
605 { "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
606 { "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
607 { "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
608 { "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
609 { "SYMBIOS INF-01-00", SD_CONF_BSET_FAB_DEVID, NULL },
610 { "SYMBIOS", SD_CONF_BSET_THROTTLE|
611 SD_CONF_BSET_NRR_COUNT,
612 &symbios_properties },
613 { "LSI", SD_CONF_BSET_THROTTLE|
614 SD_CONF_BSET_NRR_COUNT,
615 &lsi_properties_scsi },
616 { " NEC CD-ROM DRIVE:260 ", SD_CONF_BSET_PLAYMSF_BCD|
617 SD_CONF_BSET_READSUB_BCD|
618 SD_CONF_BSET_READ_TOC_ADDR_BCD|
619 SD_CONF_BSET_NO_READ_HEADER|
620 SD_CONF_BSET_READ_CD_XD4,
621 NULL },
622 { " NEC CD-ROM DRIVE:270 ", SD_CONF_BSET_PLAYMSF_BCD|
623 SD_CONF_BSET_READSUB_BCD|
624 SD_CONF_BSET_READ_TOC_ADDR_BCD|
625 SD_CONF_BSET_NO_READ_HEADER|
626 SD_CONF_BSET_READ_CD_XD4,
627 NULL },
628 #if (defined(SD_PROP_TST))
629 { "VENDOR PRODUCT ", SD_CONF_BSET_THROTTLE|
630 SD_CONF_BSET_CTYPE|
631 SD_CONF_BSET_NRR_COUNT|
632 SD_CONF_BSET_FAB_DEVID|
633 SD_CONF_BSET_NOCACHE|
634 SD_CONF_BSET_BSY_RETRY_COUNT|
635 SD_CONF_BSET_PLAYMSF_BCD|
636 SD_CONF_BSET_READSUB_BCD|
637 SD_CONF_BSET_READ_TOC_TRK_BCD|
638 SD_CONF_BSET_READ_TOC_ADDR_BCD|
639 SD_CONF_BSET_NO_READ_HEADER|
640 SD_CONF_BSET_READ_CD_XD4|
641 SD_CONF_BSET_RST_RETRIES|
642 SD_CONF_BSET_RSV_REL_TIME|
643 SD_CONF_BSET_TUR_CHECK,
644 &tst_properties},
645 #endif
646 };
647
648 static const int sd_disk_table_size =
649 sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
650
651 /*
652 * Emulation mode disk drive VID/PID table
653 */
654 static char sd_flash_dev_table[][25] = {
655 "ATA MARVELL SD88SA02",
656 "MARVELL SD88SA02",
657 "TOSHIBA THNSNV05",
658 };
659
660 static const int sd_flash_dev_table_size =
661 sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
662
663 #define SD_INTERCONNECT_PARALLEL 0
664 #define SD_INTERCONNECT_FABRIC 1
689 (un)->un_last_state = (un)->un_state, (un)->un_state = (s)
690 #define Restore_state(un) \
691 { uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
692
693 static struct sd_cdbinfo sd_cdbtab[] = {
694 { CDB_GROUP0, 0x00, 0x1FFFFF, 0xFF, },
695 { CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF, },
696 { CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF, },
697 { CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
698 };
699
700 /*
701 * Specifies the number of seconds that must have elapsed since the last
702 * cmd. has completed for a device to be declared idle to the PM framework.
703 */
704 static int sd_pm_idletime = 1;
705
706 /*
707 * Internal function prototypes
708 */
709 typedef struct unmap_param_hdr_s {
710 uint16_t uph_data_len;
711 uint16_t uph_descr_data_len;
712 uint32_t uph_reserved;
713 } unmap_param_hdr_t;
714
715 typedef struct unmap_blk_descr_s {
716 uint64_t ubd_lba;
717 uint32_t ubd_lba_cnt;
718 uint32_t ubd_reserved;
719 } unmap_blk_descr_t;
720
721 /* Max number of block descriptors in UNMAP command */
722 #define SD_UNMAP_MAX_DESCR \
723 ((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
724 /* Max size of the UNMAP parameter list in bytes */
725 #define SD_UNMAP_PARAM_LIST_MAXSZ (sizeof (unmap_param_hdr_t) + \
726 SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
727
728 int _init(void);
729 int _fini(void);
730 int _info(struct modinfo *modinfop);
731
732 /*PRINTFLIKE3*/
733 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
734 /*PRINTFLIKE3*/
735 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
736 /*PRINTFLIKE3*/
737 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
738
739 static int sdprobe(dev_info_t *devi);
740 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
741 void **result);
742 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
743 int mod_flags, char *name, caddr_t valuep, int *lengthp);
744
745 /*
746 * Smart probe for parallel scsi
747 */
757 static void sd_scsi_target_lun_fini(void);
758 static int sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
759 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
760
761 static int sd_spin_up_unit(sd_ssc_t *ssc);
762
763 /*
764 * Using sd_ssc_init to establish sd_ssc_t struct
765 * Using sd_ssc_send to send uscsi internal command
766 * Using sd_ssc_fini to free sd_ssc_t struct
767 */
768 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
769 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
770 int flag, enum uio_seg dataspace, int path_flag);
771 static void sd_ssc_fini(sd_ssc_t *ssc);
772
773 /*
774 * Using sd_ssc_assessment to set correct type-of-assessment
775 * Using sd_ssc_post to post ereport & system log
776 * sd_ssc_post will call sd_ssc_print to print system log
777 * sd_ssc_post will call sd_ssc_ereport_post to post ereport
778 */
779 static void sd_ssc_assessment(sd_ssc_t *ssc,
780 enum sd_type_assessment tp_assess);
781
782 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
783 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
784 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
785 enum sd_driver_assessment drv_assess);
786
787 /*
788 * Using sd_ssc_set_info to mark an un-decodable-data error.
789 * Using sd_ssc_extract_info to transfer information from internal
790 * data structures to sd_ssc_t.
791 */
792 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
793 const char *fmt, ...);
794 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
795 struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
796
797 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
809 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
810 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
811 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
812 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
813 int *data_list, sd_tunables *values);
814 static void sd_process_sdconf_table(struct sd_lun *un);
815 static int sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
816 static int sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
817 static int sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
818 int list_len, char *dataname_ptr);
819 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
820 sd_tunables *prop_list);
821
822 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
823 int reservation_flag);
824 static int sd_get_devid(sd_ssc_t *ssc);
825 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
826 static int sd_write_deviceid(sd_ssc_t *ssc);
827 static int sd_check_vpd_page_support(sd_ssc_t *ssc);
828
829 #ifdef notyet
830 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
831 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
832 #endif
833
834 static int sd_ddi_suspend(dev_info_t *devi);
835 static int sd_ddi_resume(dev_info_t *devi);
836 static int sd_pm_state_change(struct sd_lun *un, int level, int flag);
837 static int sdpower(dev_info_t *devi, int component, int level);
838
839 static int sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
840 static int sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
841 static void sd_unit_attach(void *arg);
842 static int sd_unit_detach(dev_info_t *devi);
843
844 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
845 static void sd_create_errstats(struct sd_lun *un, int instance);
846 static void sd_set_errstats(struct sd_lun *un);
847 static void sd_set_pstats(struct sd_lun *un);
848
849 static int sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
850 static int sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
851 static int sd_send_polled_RQS(struct sd_lun *un);
852 static int sd_ddi_scsi_poll(struct scsi_pkt *pkt);
853
854 /*
855 * Defines for sd_cache_control
856 */
857 #define SD_CACHE_ENABLE 1
858 #define SD_CACHE_DISABLE 0
859 #define SD_CACHE_NOCHANGE -1
860
861 static int sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
862 static int sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
863 static void sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
864 static void sd_get_nv_sup(sd_ssc_t *ssc);
865 static dev_t sd_make_device(dev_info_t *devi);
866 static void sd_check_bdc_vpd(sd_ssc_t *ssc);
867 static void sd_check_emulation_mode(sd_ssc_t *ssc);
868 static void sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
869 uint64_t capacity);
870
871 /*
872 * Driver entry point functions.
873 */
874 static int sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
875 static int sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
876 static int sd_ready_and_valid(sd_ssc_t *ssc, int part);
959 static void sd_print_transport_rejected_message(struct sd_lun *un,
960 struct sd_xbuf *xp, int code);
961 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
962 void *arg, int code);
963 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
964 void *arg, int code);
965 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
966 void *arg, int code);
967
968 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
969 int retry_check_flag,
970 void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
971 int c),
972 void *user_arg, int failure_code, clock_t retry_delay,
973 void (*statp)(kstat_io_t *));
974
975 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
976 clock_t retry_delay, void (*statp)(kstat_io_t *));
977
978 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
979 int retry_check_flag, struct scsi_pkt *pktp);
980 static void sd_start_retry_command(void *arg);
981 static void sd_start_direct_priority_command(void *arg);
982 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
983 int errcode);
984 static void sd_return_failed_command_no_restart(struct sd_lun *un,
985 struct buf *bp, int errcode);
986 static void sd_return_command(struct sd_lun *un, struct buf *bp);
987 static void sd_sync_with_callback(struct sd_lun *un);
988 static int sdrunout(caddr_t arg);
989
990 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
991 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
992
993 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
994 static void sd_restore_throttle(void *arg);
995
996 static void sd_init_cdb_limits(struct sd_lun *un);
997
998 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
999 struct sd_xbuf *xp, struct scsi_pkt *pktp);
1082 static void sd_media_change_task(void *arg);
1083
1084 static int sd_handle_mchange(struct sd_lun *un);
1085 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1086 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1087 uint32_t *lbap, int path_flag);
1088 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1089 uint32_t *lbap, uint32_t *psp, int path_flag);
1090 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1091 int flag, int path_flag);
1092 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1093 size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1094 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1095 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1096 uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1097 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1098 uchar_t usr_cmd, uchar_t *usr_bufp);
1099 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1100 struct dk_callback *dkc);
1101 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1102 static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
1103 int flag);
1104 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1105 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1106 uchar_t *bufaddr, uint_t buflen, int path_flag);
1107 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1108 struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1109 uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1110 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1111 uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1112 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1113 uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1114 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1115 size_t buflen, daddr_t start_block, int path_flag);
1116 #define sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag) \
1117 sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1118 path_flag)
1119 #define sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1120 sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1121 path_flag)
1122
1123 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1178 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1179 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1180 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1181 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1182 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1183 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1184 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1185 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1186 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1187 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1188 static int sr_eject(dev_t dev);
1189 static void sr_ejected(register struct sd_lun *un);
1190 static int sr_check_wp(dev_t dev);
1191 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1192 static int sd_check_media(dev_t dev, enum dkio_state state);
1193 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1194 static void sd_delayed_cv_broadcast(void *arg);
1195 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1196 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1197
1198 #ifdef notyet
1199 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1200 #endif
1201
1202 /*
1203 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1204 */
1205 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1206 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1207 static void sd_wm_cache_destructor(void *wm, void *un);
1208 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1209 daddr_t endb, ushort_t typ);
1210 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1211 daddr_t endb);
1212 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1213 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1214 static void sd_read_modify_write_task(void * arg);
1215 static int
1216 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1217 struct buf **bpp);
1218
1219
1220 /*
1221 * Function prototypes for failfast support.
1222 */
1223 static void sd_failfast_flushq(struct sd_lun *un, boolean_t flush_all);
1224 static int sd_failfast_flushq_callback(struct buf *bp);
1225
1226 /*
1227 * Function prototypes to check for lsi devices
1228 */
1229 static void sd_is_lsi(struct sd_lun *un);
1230
1231 /*
1232 * Function prototypes for partial DMA support
1233 */
1234 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1235 struct scsi_pkt *pkt, struct sd_xbuf *xp);
1236
1237
1238 /* Function prototypes for cmlb */
1239 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1240 diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1241
1242 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1243
1244 /*
1245 * For printing RMW warning message timely
1246 */
1247 static void sd_rmw_msg_print_handler(void *arg);
1248
1249 /*
1250 * Constants for failfast support:
1251 *
1252 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1253 * failfast processing being performed.
1254 *
1255 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1256 * failfast processing on all bufs with B_FAILFAST set.
1257 */
1258
1259 #define SD_FAILFAST_INACTIVE 0
1260 #define SD_FAILFAST_ACTIVE 1
1261
1262 /*
1263 * Bitmask to control behaviour in failfast active state:
1264 *
1265 * SD_FAILFAST_ENABLE_FORCE_INACTIVE: When set, allow retries without
1266 * SD_RETRIES_FAILFAST to cause transition to failfast inactive state.
1267 *
1268 * SD_FAILFAST_ENABLE_FAIL_RETRIES: When set, cause retries with the flag
1269 * SD_RETRIES_FAILFAST set (following a timeout) to fail when in failfast
1270 * active state.
1271 *
1272 * SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES: When set, cause ALL retries,
1273 * regardless of reason, to fail when in failfast active state. This takes
1274 * precedence over SD_FAILFAST_FAIL_RETRIES.
1275 *
1276 * SD_FAILFAST_ENABLE_FAIL_USCSI: When set, discard all commands in the USCSI
1277 * chain (sdioctl or driver generated) when in failfast active state.
1278 * To prevent problems with sdopen, this is limited to when there are
1279 * multiple pending commands.
1280 */
1281
1282 #define SD_FAILFAST_ENABLE_FORCE_INACTIVE 0x01
1283 #define SD_FAILFAST_ENABLE_FAIL_RETRIES 0x02
1284 #define SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES 0x04
1285 #define SD_FAILFAST_ENABLE_FAIL_USCSI 0x08
1286
1287 /*
1288 * The default behaviour is to fail all retries due to timeout when in failfast
1289 * active state, and not allow other retries to transition to inactive.
1290 */
1291 static int sd_failfast_enable = SD_FAILFAST_ENABLE_FAIL_RETRIES |
1292 SD_FAILFAST_ENABLE_FAIL_USCSI;
1293
1294 /*
1295 * Bitmask to control behavior of buf(9S) flushes when a transition to
1296 * the failfast state occurs. Optional bits include:
1297 *
1298 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1299 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1300 * be flushed.
1301 *
1302 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1303 * driver, in addition to the regular wait queue. This includes the xbuf
1304 * queues. When clear, only the driver's wait queue will be flushed.
1305 */
1306 #define SD_FAILFAST_FLUSH_ALL_BUFS 0x01
1307 #define SD_FAILFAST_FLUSH_ALL_QUEUES 0x02
1308
1309 /*
1310 * The default behavior is to flush all bufs in all queues within the driver.
1311 */
1312 static int sd_failfast_flushctl =
1313 SD_FAILFAST_FLUSH_ALL_BUFS | SD_FAILFAST_FLUSH_ALL_QUEUES;
1314
1315 #ifdef SD_FAULT_INJECTION
1316 static uint_t sd_fault_injection_on = 0;
1317 #endif
1318
1319 /*
1320 * SD Testing Fault Injection
1321 */
1322 #ifdef SD_FAULT_INJECTION
1323 static int sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1324 static void sd_faultinjection(struct scsi_pkt *pktp);
1325 static void sd_prefaultinjection(struct scsi_pkt *pktp);
1326 static void sd_injection_log(char *buf, struct sd_lun *un);
1327 #endif
1328
1329 /*
1330 * Device driver ops vector
1331 */
1332 static struct cb_ops sd_cb_ops = {
1333 sdopen, /* open */
1334 sdclose, /* close */
1335 sdstrategy, /* strategy */
1336 nodev, /* print */
1337 sddump, /* dump */
1338 sdread, /* read */
1339 sdwrite, /* write */
1340 sdioctl, /* ioctl */
1341 nodev, /* devmap */
1342 nodev, /* mmap */
1343 nodev, /* segmap */
1344 nochpoll, /* poll */
1345 sd_prop_op, /* cb_prop_op */
1961 #define SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM 11
1962
1963 /* un->un_uscsi_chain_type must be set to one of these */
1964 #define SD_CHAIN_INFO_USCSI_CMD 6
1965 /* USCSI with PM disabled is the same as DIRECT */
1966 #define SD_CHAIN_INFO_USCSI_CMD_NO_PM 8
1967 #define SD_CHAIN_INFO_USCSI_CHKSUM 7
1968
1969 /* un->un_direct_chain_type must be set to one of these */
1970 #define SD_CHAIN_INFO_DIRECT_CMD 8
1971
1972 /* un->un_priority_chain_type must be set to one of these */
1973 #define SD_CHAIN_INFO_PRIORITY_CMD 9
1974
1975 /* size for devid inquiries */
1976 #define MAX_INQUIRY_SIZE 0xF0
1977
1978 /*
1979 * Macros used by functions to pass a given buf(9S) struct along to the
1980 * next function in the layering chain for further processing.
1981 */
1982 #define SD_BEGIN_IOSTART(index, un, bp) \
1983 ((*(sd_iostart_chain[index]))(index, un, bp))
1984
1985 #define SD_BEGIN_IODONE(index, un, bp) \
1986 ((*(sd_iodone_chain[index]))(index, un, bp))
1987
1988 #define SD_NEXT_IOSTART(index, un, bp) \
1989 ((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
1990
1991 #define SD_NEXT_IODONE(index, un, bp) \
1992 ((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
1993
1994 /*
1995 * Function: _init
1996 *
1997 * Description: This is the driver _init(9E) entry point.
1998 *
1999 * Return Code: Returns the value from mod_install(9F) or
2000 * ddi_soft_state_init(9F) as appropriate.
2153
2154 ASSERT(un != NULL);
2155 dev = SD_DEVINFO(un);
2156 ASSERT(dev != NULL);
2157
2158 /*
2159 * Filter messages based on the global component and level masks.
2160 * Also print if un matches the value of sd_debug_un, or if
2161 * sd_debug_un is set to NULL.
2162 */
2163 if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2164 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2165 mutex_enter(&sd_log_mutex);
2166 va_start(ap, fmt);
2167 (void) vsprintf(sd_log_buf, fmt, ap);
2168 va_end(ap);
2169 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2170 mutex_exit(&sd_log_mutex);
2171 }
2172 #ifdef SD_FAULT_INJECTION
2173 if (un->sd_injection_mask & comp) {
2174 mutex_enter(&sd_log_mutex);
2175 va_start(ap, fmt);
2176 (void) vsprintf(sd_log_buf, fmt, ap);
2177 va_end(ap);
2178 sd_injection_log(sd_log_buf, un);
2179 mutex_exit(&sd_log_mutex);
2180 }
2181 #endif
2182 }
2183
2184
2185 /*
2186 * Function: sd_log_info
2187 *
2188 * Description: This routine is called by the SD_INFO macro for debug
2189 * logging of general purpose informational conditions.
2190 *
2191 * Arguments: comp - driver component being logged
2192 * dev - pointer to driver info structure
2202 ASSERT(un != NULL);
2203 dev = SD_DEVINFO(un);
2204 ASSERT(dev != NULL);
2205
2206 /*
2207 * Filter messages based on the global component and level masks.
2208 * Also print if un matches the value of sd_debug_un, or if
2209 * sd_debug_un is set to NULL.
2210 */
2211 if ((sd_component_mask & component) &&
2212 (sd_level_mask & SD_LOGMASK_INFO) &&
2213 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2214 mutex_enter(&sd_log_mutex);
2215 va_start(ap, fmt);
2216 (void) vsprintf(sd_log_buf, fmt, ap);
2217 va_end(ap);
2218 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2219 mutex_exit(&sd_log_mutex);
2220 }
2221 #ifdef SD_FAULT_INJECTION
2222 if (un->sd_injection_mask & component) {
2223 mutex_enter(&sd_log_mutex);
2224 va_start(ap, fmt);
2225 (void) vsprintf(sd_log_buf, fmt, ap);
2226 va_end(ap);
2227 sd_injection_log(sd_log_buf, un);
2228 mutex_exit(&sd_log_mutex);
2229 }
2230 #endif
2231 }
2232
2233
2234 /*
2235 * Function: sd_log_trace
2236 *
2237 * Description: This routine is called by the SD_TRACE macro for debug
2238 * logging of trace conditions (i.e. function entry/exit).
2239 *
2240 * Arguments: comp - driver component being logged
2241 * dev - pointer to driver info structure
2251 ASSERT(un != NULL);
2252 dev = SD_DEVINFO(un);
2253 ASSERT(dev != NULL);
2254
2255 /*
2256 * Filter messages based on the global component and level masks.
2257 * Also print if un matches the value of sd_debug_un, or if
2258 * sd_debug_un is set to NULL.
2259 */
2260 if ((sd_component_mask & component) &&
2261 (sd_level_mask & SD_LOGMASK_TRACE) &&
2262 ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2263 mutex_enter(&sd_log_mutex);
2264 va_start(ap, fmt);
2265 (void) vsprintf(sd_log_buf, fmt, ap);
2266 va_end(ap);
2267 scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2268 mutex_exit(&sd_log_mutex);
2269 }
2270 #ifdef SD_FAULT_INJECTION
2271 if (un->sd_injection_mask & component) {
2272 mutex_enter(&sd_log_mutex);
2273 va_start(ap, fmt);
2274 (void) vsprintf(sd_log_buf, fmt, ap);
2275 va_end(ap);
2276 sd_injection_log(sd_log_buf, un);
2277 mutex_exit(&sd_log_mutex);
2278 }
2279 #endif
2280 }
2281
2282
2283 /*
2284 * Function: sdprobe
2285 *
2286 * Description: This is the driver probe(9e) entry point function.
2287 *
2288 * Arguments: devi - opaque device info handle
2289 *
2290 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2291 * DDI_PROBE_FAILURE: If the probe failed.
2292 * DDI_PROBE_PARTIAL: If the instance is not present now,
2293 * but may be present in the future.
2294 */
2295
2296 static int
2297 sdprobe(dev_info_t *devi)
2298 {
2299 struct scsi_device *devp;
2300 int rval;
2301 int instance = ddi_get_instance(devi);
2302
2303 if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2304 return (DDI_PROBE_DONTCARE);
2305 }
2306
2307 devp = ddi_get_driver_private(devi);
2308
2309 if (devp == NULL) {
2310 /* Ooops... nexus driver is mis-configured... */
2311 return (DDI_PROBE_FAILURE);
2312 }
2313
2314 if (ddi_get_soft_state(sd_state, instance) != NULL) {
2315 return (DDI_PROBE_PARTIAL);
2316 }
2317
2318 /*
2319 * Call the SCSA utility probe routine to see if we actually
2320 * have a target at this SCSI nexus.
2321 */
2322 switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2323 case SCSIPROBE_EXISTS:
2324 switch (devp->sd_inq->inq_dtype) {
2325 case DTYPE_DIRECT:
2326 rval = DDI_PROBE_SUCCESS;
2327 break;
2328 case DTYPE_RODIRECT:
2329 /* CDs etc. Can be removable media. */
2330 rval = DDI_PROBE_SUCCESS;
2331 break;
2332 case DTYPE_OPTICAL:
2333 /*
2334 * Rewritable optical driver HP115AA.
2335 * Can also be removable media.
2336 */
2337 rval = DDI_PROBE_SUCCESS;
2338 break;
2339 case DTYPE_NOTPRESENT:
2340 default:
2341 rval = DDI_PROBE_FAILURE;
2342 break;
2343 }
2344 break;
2345 default:
2346 rval = DDI_PROBE_PARTIAL;
2347 break;
2348 }
2349
2350 /*
2351 * This routine checks for resource allocation prior to freeing,
2352 * so it will take care of the "smart probing" case where a
2353 * scsi_probe() may or may not have been issued and will *not*
2354 * free previously-freed resources.
2355 */
2356 scsi_unprobe(devp);
2357 return (rval);
2358 }
2418 * prop_op - property operator
2419 * mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2420 * name - pointer to property name
2421 * valuep - pointer or address of the user buffer
2422 * lengthp - property length
2423 *
2424 * Return Code: DDI_PROP_SUCCESS
2425 * DDI_PROP_NOT_FOUND
2426 * DDI_PROP_UNDEFINED
2427 * DDI_PROP_NO_MEMORY
2428 * DDI_PROP_BUF_TOO_SMALL
2429 */
2430
2431 static int
2432 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2433 char *name, caddr_t valuep, int *lengthp)
2434 {
2435 struct sd_lun *un;
2436
2437 if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2438 goto fallback;
2439
2440 mutex_enter(SD_MUTEX(un));
2441 while ((un->un_state == SD_STATE_ATTACHING))
2442 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
2443
2444 if (un->un_state == SD_STATE_ATTACH_FAILED) {
2445 mutex_exit(SD_MUTEX(un));
2446 goto fallback;
2447 }
2448 mutex_exit(SD_MUTEX(un));
2449
2450 return (cmlb_prop_op(un->un_cmlbhandle,
2451 dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2452 SDPART(dev), (void *)SD_PATH_DIRECT));
2453
2454 fallback:
2455 return (ddi_prop_op(dev, dip, prop_op, mod_flags, name, valuep,
2456 lengthp));
2457 }
2458
2459 /*
2460 * The following functions are for smart probing:
2461 * sd_scsi_probe_cache_init()
2462 * sd_scsi_probe_cache_fini()
2463 * sd_scsi_clear_probe_cache()
2464 * sd_scsi_probe_with_cache()
2465 */
2466
2467 /*
2468 * Function: sd_scsi_probe_cache_init
2469 *
2470 * Description: Initializes the probe response cache mutex and head pointer.
2471 *
2472 * Context: Kernel thread context
2473 */
2474
2475 static void
2476 sd_scsi_probe_cache_init(void)
2784 struct sd_lun *un;
2785
2786 ASSERT(ssc != NULL);
2787 un = ssc->ssc_un;
2788 ASSERT(un != NULL);
2789
2790 /*
2791 * Send a throwaway START UNIT command.
2792 *
2793 * If we fail on this, we don't care presently what precisely
2794 * is wrong. EMC's arrays will also fail this with a check
2795 * condition (0x2/0x4/0x3) if the device is "inactive," but
2796 * we don't want to fail the attach because it may become
2797 * "active" later.
2798 * We don't know if power condition is supported or not at
2799 * this stage, use START STOP bit.
2800 */
2801 status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
2802 SD_TARGET_START, SD_PATH_DIRECT);
2803
2804 switch (status) {
2805 case EIO:
2806 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
2807 return (status);
2808 case EACCES:
2809 has_conflict = TRUE;
2810 default: /*FALLTHROUGH*/
2811 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
2812 }
2813
2814 /*
2815 * Send another INQUIRY command to the target. This is necessary for
2816 * non-removable media direct access devices because their INQUIRY data
2817 * may not be fully qualified until they are spun up (perhaps via the
2818 * START command above). Note: This seems to be needed for some
2819 * legacy devices only.) The INQUIRY command should succeed even if a
2820 * Reservation Conflict is present.
2821 */
2822 bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
2823
2824 if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
2825 != 0) {
2826 kmem_free(bufaddr, SUN_INQSIZE);
2827 sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
2828 return (EIO);
2829 }
2830
3409
3410 static int
3411 sd_process_sdconf_file(struct sd_lun *un)
3412 {
3413 char **config_list = NULL;
3414 uint_t nelements;
3415 char *vidptr;
3416 int vidlen;
3417 char *dnlist_ptr;
3418 char *dataname_ptr;
3419 char *dataname_lasts;
3420 int *data_list = NULL;
3421 uint_t data_list_len;
3422 int rval = SD_FAILURE;
3423 int i;
3424
3425 ASSERT(un != NULL);
3426
3427 /* Obtain the configuration list associated with the .conf file */
3428 if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3429 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "sd-config-list",
3430 &config_list, &nelements) != DDI_PROP_SUCCESS) {
3431 return (SD_FAILURE);
3432 }
3433
3434 /*
3435 * Compare vids in each duplet to the inquiry vid - if a match is
3436 * made, get the data value and update the soft state structure
3437 * accordingly.
3438 *
3439 * Each duplet should show as a pair of strings, return SD_FAILURE
3440 * otherwise.
3441 */
3442 if (nelements & 1) {
3443 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3444 "sd-config-list should show as pairs of strings.\n");
3445 if (config_list)
3446 ddi_prop_free(config_list);
3447 return (SD_FAILURE);
3448 }
3449
3773 }
3774 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3775 "min throttle set to %d\n", un->un_min_throttle);
3776 }
3777
3778 if (strcasecmp(name, "rmw-type") == 0) {
3779 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3780 un->un_f_rmw_type = val;
3781 } else {
3782 goto value_invalid;
3783 }
3784 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3785 "RMW type set to %d\n", un->un_f_rmw_type);
3786 }
3787
3788 if (strcasecmp(name, "physical-block-size") == 0) {
3789 if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
3790 ISP2(val) && val >= un->un_tgt_blocksize &&
3791 val >= un->un_sys_blocksize) {
3792 un->un_phy_blocksize = val;
3793 un->un_f_sdconf_phy_blocksize = TRUE;
3794 } else {
3795 goto value_invalid;
3796 }
3797 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3798 "physical block size set to %d\n", un->un_phy_blocksize);
3799 }
3800
3801 if (strcasecmp(name, "slow-io-threshold") == 0) {
3802 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3803 un->un_slow_io_threshold = (hrtime_t)val * NANOSEC;
3804 } else {
3805 un->un_slow_io_threshold =
3806 (hrtime_t)sd_slow_io_threshold;
3807 goto value_invalid;
3808 }
3809 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3810 "slow IO threshold set to %llu\n",
3811 un->un_slow_io_threshold);
3812 }
3813
3814 if (strcasecmp(name, "io-time") == 0) {
3815 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3816 un->un_io_time = val;
3817 } else {
3818 un->un_io_time = sd_io_time;
3819 goto value_invalid;
3820 }
3821 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3822 "IO time set to %llu\n", un->un_io_time);
3823 }
3824
3825 if (strcasecmp(name, "retries-victim") == 0) {
3826 if (ddi_strtol(value, &endptr, 0, &val) == 0) {
3827 un->un_victim_retry_count = val;
3828 } else {
3829 goto value_invalid;
3830 }
3831 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
3832 "victim retry count set to %d\n",
3833 un->un_victim_retry_count);
3834 return;
3835 }
3836
3837 /*
3838 * Validate the throttle values.
3839 * If any of the numbers are invalid, set everything to defaults.
3840 */
3841 if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
3842 (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
3843 (un->un_min_throttle > un->un_throttle)) {
3844 un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4566 } else {
4567 lbasize = un->un_sys_blocksize;
4568 }
4569 }
4570 pgeom_p->g_secsize = (unsigned short)lbasize;
4571
4572 /*
4573 * If the unit is a cd/dvd drive MODE SENSE page three
4574 * and MODE SENSE page four are reserved (see SBC spec
4575 * and MMC spec). To prevent soft errors just return
4576 * using the default LBA size.
4577 *
4578 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4579 * implement support for mode pages 3 and 4 return here to prevent
4580 * illegal requests on SATA drives.
4581 *
4582 * These pages are also reserved in SBC-2 and later. We assume SBC-2
4583 * or later for a direct-attached block device if the SCSI version is
4584 * at least SPC-3.
4585 */
4586 if (ISCD(un) ||
4587 un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4588 (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4589 return (ret);
4590
4591 cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4592
4593 /*
4594 * Retrieve MODE SENSE page 3 - Format Device Page
4595 */
4596 p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4597 ssc = sd_ssc_init(un);
4598 status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4599 SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4600 if (status != 0) {
4601 SD_ERROR(SD_LOG_COMMON, un,
4602 "sd_get_physical_geometry: mode sense page 3 failed\n");
4603 goto page3_exit;
4604 }
4605
4907 }
4908
4909 if (capacity != 0) {
4910 un->un_blockcount = capacity;
4911 un->un_f_blockcount_is_valid = TRUE;
4912
4913 /*
4914 * The capacity has changed so update the errstats.
4915 */
4916 if (un->un_errstats != NULL) {
4917 struct sd_errstats *stp;
4918
4919 capacity *= un->un_sys_blocksize;
4920 stp = (struct sd_errstats *)un->un_errstats->ks_data;
4921 if (stp->sd_capacity.value.ui64 < capacity)
4922 stp->sd_capacity.value.ui64 = capacity;
4923 }
4924 }
4925 }
4926
4927 /*
4928 * Parses the SCSI Block Limits VPD page (0xB0). It's legal to pass NULL for
4929 * vpd_pg, in which case all the block limits will be reset to the defaults.
4930 */
4931 static void
4932 sd_parse_blk_limits_vpd(struct sd_lun *un, uchar_t *vpd_pg)
4933 {
4934 sd_blk_limits_t *lim = &un->un_blk_lim;
4935 unsigned pg_len;
4936
4937 if (vpd_pg != NULL)
4938 pg_len = BE_IN16(&vpd_pg[2]);
4939 else
4940 pg_len = 0;
4941
4942 /* Block Limits VPD can be 16 bytes or 64 bytes long - support both */
4943 if (pg_len >= 0x10) {
4944 lim->lim_opt_xfer_len_gran = BE_IN16(&vpd_pg[6]);
4945 lim->lim_max_xfer_len = BE_IN32(&vpd_pg[8]);
4946 lim->lim_opt_xfer_len = BE_IN32(&vpd_pg[12]);
4947 } else {
4948 lim->lim_opt_xfer_len_gran = 0;
4949 lim->lim_max_xfer_len = UINT32_MAX;
4950 lim->lim_opt_xfer_len = UINT32_MAX;
4951 }
4952 if (pg_len >= 0x3c) {
4953 lim->lim_max_pfetch_len = BE_IN32(&vpd_pg[16]);
4954 /*
4955 * A zero in either of the following two fields indicates lack
4956 * of UNMAP support.
4957 */
4958 lim->lim_max_unmap_lba_cnt = BE_IN32(&vpd_pg[20]);
4959 lim->lim_max_unmap_descr_cnt = BE_IN32(&vpd_pg[24]);
4960 lim->lim_opt_unmap_gran = BE_IN32(&vpd_pg[28]);
4961 if ((vpd_pg[32] >> 7) == 1) {
4962 /* left-most bit on each byte is a flag */
4963 lim->lim_unmap_gran_align =
4964 ((vpd_pg[32] & 0x7f) << 24) | (vpd_pg[33] << 16) |
4965 (vpd_pg[34] << 8) | vpd_pg[35];
4966 } else {
4967 lim->lim_unmap_gran_align = 0;
4968 }
4969 lim->lim_max_write_same_len = BE_IN64(&vpd_pg[36]);
4970 } else {
4971 lim->lim_max_pfetch_len = UINT32_MAX;
4972 lim->lim_max_unmap_lba_cnt = UINT32_MAX;
4973 lim->lim_max_unmap_descr_cnt = SD_UNMAP_MAX_DESCR;
4974 lim->lim_opt_unmap_gran = 0;
4975 lim->lim_unmap_gran_align = 0;
4976 lim->lim_max_write_same_len = UINT64_MAX;
4977 }
4978 }
4979
4980 /*
4981 * Collects VPD page B0 data if available (block limits). If the data is
4982 * not available or querying the device failed, we revert to the defaults.
4983 */
4984 static void
4985 sd_setup_blk_limits(sd_ssc_t *ssc)
4986 {
4987 struct sd_lun *un = ssc->ssc_un;
4988 uchar_t *inqB0 = NULL;
4989 size_t inqB0_resid = 0;
4990 int rval;
4991
4992 if (un->un_vpd_page_mask & SD_VPD_BLK_LIMITS_PG) {
4993 inqB0 = kmem_zalloc(MAX_INQUIRY_SIZE, KM_SLEEP);
4994 rval = sd_send_scsi_INQUIRY(ssc, inqB0, MAX_INQUIRY_SIZE, 0x01,
4995 0xB0, &inqB0_resid);
4996 if (rval != 0) {
4997 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
4998 kmem_free(inqB0, MAX_INQUIRY_SIZE);
4999 inqB0 = NULL;
5000 }
5001 }
5002 /* passing NULL inqB0 will reset to defaults */
5003 sd_parse_blk_limits_vpd(ssc->ssc_un, inqB0);
5004 if (inqB0)
5005 kmem_free(inqB0, MAX_INQUIRY_SIZE);
5006 }
5007
5008 #define DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
5009
5010 /*
5011 * Function: sd_register_devid
5012 *
5013 * Description: This routine will obtain the device id information from the
5014 * target, obtain the serial number, and register the device
5015 * id with the ddi framework.
5016 *
5017 * Arguments: devi - the system's dev_info_t for the device.
5018 * un - driver soft state (unit) structure
5019 * reservation_flag - indicates if a reservation conflict
5020 * occurred during attach
5021 *
5022 * Context: Kernel Thread
5023 */
5024 static void
5025 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5026 {
5027 int rval = 0;
5028 uchar_t *inq80 = NULL;
5029 size_t inq80_len = MAX_INQUIRY_SIZE;
5030 size_t inq80_resid = 0;
5121 * then that takes precedence over the driver's determination
5122 * of the devid.
5123 *
5124 * NOTE: The reason this check is done here instead of at the beginning
5125 * of the function is to allow the code above to create the
5126 * 'inquiry-serial-no' property.
5127 */
5128 if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5129 ASSERT(un->un_devid);
5130 un->un_f_devid_transport_defined = TRUE;
5131 goto cleanup; /* use devid registered by the transport */
5132 }
5133
5134 /*
5135 * This is the case of antiquated Sun disk drives that have the
5136 * FAB_DEVID property set in the disk_table. These drives
5137 * manage the devid's by storing them in last 2 available sectors
5138 * on the drive and have them fabricated by the ddi layer by calling
5139 * ddi_devid_init and passing the DEVID_FAB flag.
5140 */
5141 if (un->un_f_opt_fab_devid == TRUE &&
5142 reservation_flag != SD_TARGET_IS_RESERVED) {
5143 if (sd_get_devid(ssc) == EINVAL)
5144 /*
5145 * The devid is invalid AND there is no reservation
5146 * conflict. Fabricate a new devid.
5147 */
5148 (void) sd_create_devid(ssc);
5149
5150 /* Register the devid if it exists */
5151 if (un->un_devid != NULL) {
5152 (void) ddi_devid_register(SD_DEVINFO(un),
5153 un->un_devid);
5154 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5155 "sd_register_devid: Devid Fabricated\n");
5156 }
5157 goto cleanup;
5158 }
5159
5160 /* encode best devid possible based on data available */
5161 if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5162 (char *)ddi_driver_name(SD_DEVINFO(un)),
5163 (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5164 inq80, inq80_len - inq80_resid, inq83, inq83_len -
5165 inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5166
5167 /* devid successfully encoded, register devid */
5168 (void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5169
5170 } else if (reservation_flag != SD_TARGET_IS_RESERVED) {
5171 /*
5172 * Unable to encode a devid based on data available.
5173 * This is not a Sun qualified disk. Older Sun disk
5174 * drives that have the SD_FAB_DEVID property
5175 * set in the disk_table and non Sun qualified
5176 * disks are treated in the same manner. These
5177 * drives manage the devid's by storing them in
5178 * last 2 available sectors on the drive and
5179 * have them fabricated by the ddi layer by
5180 * calling ddi_devid_init and passing the
5181 * DEVID_FAB flag.
5182 * Create a fabricate devid only if there's no
5183 * fabricate devid existed.
5184 */
5185 if (sd_get_devid(ssc) == EINVAL) {
5186 (void) sd_create_devid(ssc);
5187 }
5188 un->un_f_opt_fab_devid = TRUE;
5189
5190 /* Register the devid if it exists */
5512
5513 switch (page_list[counter]) {
5514 case 0x00:
5515 un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5516 break;
5517 case 0x80:
5518 un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5519 break;
5520 case 0x81:
5521 un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5522 break;
5523 case 0x82:
5524 un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5525 break;
5526 case 0x83:
5527 un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5528 break;
5529 case 0x86:
5530 un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5531 break;
5532 case 0xB0:
5533 un->un_vpd_page_mask |= SD_VPD_BLK_LIMITS_PG;
5534 break;
5535 case 0xB1:
5536 un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5537 break;
5538 }
5539 counter++;
5540 }
5541
5542 } else {
5543 rval = -1;
5544
5545 SD_INFO(SD_LOG_ATTACH_DETACH, un,
5546 "sd_check_vpd_page_support: This drive does not implement "
5547 "VPD pages.\n");
5548 }
5549
5550 kmem_free(page_list, page_length);
5551
5552 return (rval);
5553 }
5554
5555
5556 /*
5557 * Function: sd_setup_pm
5558 *
5559 * Description: Initialize Power Management on the device
5560 *
5561 * Context: Kernel Thread
5562 */
5563 #ifdef notyet
5564 static void
5565 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5566 {
5567 uint_t log_page_size;
5568 uchar_t *log_page_data;
5569 int rval = 0;
5570 struct sd_lun *un;
5571
5572 ASSERT(ssc != NULL);
5573 un = ssc->ssc_un;
5574 ASSERT(un != NULL);
5575
5576 /*
5577 * Since we are called from attach, holding a mutex for
5578 * un is unnecessary. Because some of the routines called
5579 * from here require SD_MUTEX to not be held, assert this
5580 * right up front.
5581 */
5582 ASSERT(!mutex_owned(SD_MUTEX(un)));
5583 /*
5598 * the value of this property is bigger than 0.
5599 */
5600 if (un->un_f_pm_supported) {
5601 /*
5602 * not all devices have a motor, try it first.
5603 * some devices may return ILLEGAL REQUEST, some
5604 * will hang
5605 * The following START_STOP_UNIT is used to check if target
5606 * device has a motor.
5607 */
5608 un->un_f_start_stop_supported = TRUE;
5609
5610 if (un->un_f_power_condition_supported) {
5611 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5612 SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5613 SD_PATH_DIRECT);
5614 if (rval != 0) {
5615 un->un_f_power_condition_supported = FALSE;
5616 }
5617 }
5618 /* WTF? this fails for optical drives with no media */
5619 if (!un->un_f_power_condition_supported) {
5620 rval = sd_send_scsi_START_STOP_UNIT(ssc,
5621 SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5622 }
5623 if (rval != 0) {
5624 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5625 un->un_f_start_stop_supported = FALSE;
5626 }
5627
5628 /*
5629 * create pm properties anyways otherwise the parent can't
5630 * go to sleep
5631 */
5632 un->un_f_pm_is_enabled = TRUE;
5633 (void) sd_create_pm_components(devi, un);
5634
5635 /*
5636 * If it claims that log sense is supported, check it out.
5637 */
5638 if (un->un_f_log_sense_supported) {
5800 * fails. In the case of removable media, the start/stop
5801 * will fail if the media is not present.
5802 */
5803 if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
5804 SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
5805 mutex_enter(SD_MUTEX(un));
5806 un->un_power_level = SD_PM_STATE_ACTIVE(un);
5807 mutex_enter(&un->un_pm_mutex);
5808 /* Set to on and not busy. */
5809 un->un_pm_count = 0;
5810 } else {
5811 mutex_enter(SD_MUTEX(un));
5812 un->un_power_level = SD_PM_STATE_STOPPED(un);
5813 mutex_enter(&un->un_pm_mutex);
5814 /* Set to off. */
5815 un->un_pm_count = -1;
5816 }
5817 mutex_exit(&un->un_pm_mutex);
5818 mutex_exit(SD_MUTEX(un));
5819 }
5820 #endif
5821
5822 /*
5823 * Function: sd_ddi_suspend
5824 *
5825 * Description: Performs system power-down operations. This includes
5826 * setting the drive state to indicate its suspended so
5827 * that no new commands will be accepted. Also, wait for
5828 * all commands that are in transport or queued to a timer
5829 * for retry to complete. All timeout threads are cancelled.
5830 *
5831 * Return Code: DDI_FAILURE or DDI_SUCCESS
5832 *
5833 * Context: Kernel thread context
5834 */
5835
5836 static int
5837 sd_ddi_suspend(dev_info_t *devi)
5838 {
5839 struct sd_lun *un;
5840 clock_t wait_cmds_complete;
5841
5974
5975 if (un->un_retry_bp != NULL) {
5976 un->un_retry_bp->av_forw = un->un_waitq_headp;
5977 un->un_waitq_headp = un->un_retry_bp;
5978 if (un->un_waitq_tailp == NULL) {
5979 un->un_waitq_tailp = un->un_retry_bp;
5980 }
5981 un->un_retry_bp = NULL;
5982 un->un_retry_statp = NULL;
5983 }
5984 }
5985
5986 if (un->un_direct_priority_timeid != NULL) {
5987 timeout_id_t temp_id = un->un_direct_priority_timeid;
5988 un->un_direct_priority_timeid = NULL;
5989 mutex_exit(SD_MUTEX(un));
5990 (void) untimeout(temp_id);
5991 mutex_enter(SD_MUTEX(un));
5992 }
5993
5994 mutex_exit(SD_MUTEX(un));
5995
5996 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
5997
5998 return (DDI_SUCCESS);
5999 }
6000
6001
6002 /*
6003 * Function: sd_ddi_resume
6004 *
6005 * Description: Performs system power-up operations..
6006 *
6007 * Return Code: DDI_SUCCESS
6008 * DDI_FAILURE
6009 *
6010 * Context: Kernel thread context
6011 */
6012
6013 static int
6014 sd_ddi_resume(dev_info_t *devi)
6015 {
6055 */
6056 if (un->un_f_attach_spinup) {
6057 mutex_exit(SD_MUTEX(un));
6058 (void) pm_raise_power(SD_DEVINFO(un), 0,
6059 SD_PM_STATE_ACTIVE(un));
6060 mutex_enter(SD_MUTEX(un));
6061 }
6062
6063 /*
6064 * Don't broadcast to the suspend cv and therefore possibly
6065 * start I/O until after power has been restored.
6066 */
6067 cv_broadcast(&un->un_suspend_cv);
6068 cv_broadcast(&un->un_state_cv);
6069
6070 /* restart thread */
6071 if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6072 scsi_watch_resume(un->un_swr_token);
6073 }
6074
6075 /*
6076 * Transport any pending commands to the target.
6077 *
6078 * If this is a low-activity device commands in queue will have to wait
6079 * until new commands come in, which may take awhile. Also, we
6080 * specifically don't check un_ncmds_in_transport because we know that
6081 * there really are no commands in progress after the unit was
6082 * suspended and we could have reached the throttle level, been
6083 * suspended, and have no new commands coming in for awhile. Highly
6084 * unlikely, but so is the low-activity disk scenario.
6085 */
6086 ddi_xbuf_dispatch(un->un_xbuf_attr);
6087
6088 sd_start_cmds(un, NULL);
6089 mutex_exit(SD_MUTEX(un));
6090
6091 SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6092
6093 return (DDI_SUCCESS);
6094 }
6095
6741
6742
6743
6744 /*
6745 * Function: sdattach
6746 *
6747 * Description: Driver's attach(9e) entry point function.
6748 *
6749 * Arguments: devi - opaque device info handle
6750 * cmd - attach type
6751 *
6752 * Return Code: DDI_SUCCESS
6753 * DDI_FAILURE
6754 *
6755 * Context: Kernel thread context
6756 */
6757
6758 static int
6759 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
6760 {
6761 struct scsi_device *devp;
6762 struct sd_lun *un;
6763 char *variantp;
6764 int instance;
6765 int tgt;
6766 dev_info_t *pdip = ddi_get_parent(devi);
6767 int max_xfer_size;
6768 sd_ssc_t *ssc;
6769 struct sd_fm_internal *sfip = NULL;
6770
6771 switch (cmd) {
6772 case DDI_ATTACH:
6773 break;
6774 case DDI_RESUME:
6775 return (sd_ddi_resume(devi));
6776 default:
6777 return (DDI_FAILURE);
6778 }
6779
6780 /*
6781 * Retrieve the target driver's private data area. This was set
6782 * up by the HBA.
6783 */
6784 devp = ddi_get_driver_private(devi);
6785
6786 /*
6787 * Retrieve the target ID of the device.
6788 */
6789 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
6790 SCSI_ADDR_PROP_TARGET, -1);
6791
6792 /*
6793 * Since we have no idea what state things were left in by the last
6794 * user of the device, set up some 'default' settings, ie. turn 'em
6795 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
6796 * Do this before the scsi_probe, which sends an inquiry.
6797 * This is a fix for bug (4430280).
6798 * Of special importance is wide-xfer. The drive could have been left
6799 * in wide transfer mode by the last driver to communicate with it,
6854 *
6855 * We rely upon this memory being set to all zeroes by
6856 * ddi_soft_state_zalloc(). We assume that any member of the
6857 * soft state structure that is not explicitly initialized by
6858 * this routine will have a value of zero.
6859 */
6860 instance = ddi_get_instance(devp->sd_dev);
6861 if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
6862 goto probe_failed;
6863 }
6864
6865 /*
6866 * Retrieve a pointer to the newly-allocated soft state.
6867 *
6868 * This should NEVER fail if the ddi_soft_state_zalloc() call above
6869 * was successful, unless something has gone horribly wrong and the
6870 * ddi's soft state internals are corrupt (in which case it is
6871 * probably better to halt here than just fail the attach....)
6872 */
6873 if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
6874 panic("sdattach: NULL soft state on instance:0x%x",
6875 instance);
6876 /*NOTREACHED*/
6877 }
6878
6879 /*
6880 * Link the back ptr of the driver soft state to the scsi_device
6881 * struct for this lun.
6882 * Save a pointer to the softstate in the driver-private area of
6883 * the scsi_device struct.
6884 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
6885 * we first set un->un_sd below.
6886 */
6887 un->un_sd = devp;
6888 devp->sd_private = (opaque_t)un;
6889
6890 /*
6891 * The following must be after devp is stored in the soft state struct.
6892 */
6893 #ifdef SDDEBUG
6894 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
6900 * Set up the device type and node type (for the minor nodes).
6901 * By default we assume that the device can at least support the
6902 * Common Command Set. Call it a CD-ROM if it reports itself
6903 * as a RODIRECT device.
6904 */
6905 switch (devp->sd_inq->inq_dtype) {
6906 case DTYPE_RODIRECT:
6907 un->un_node_type = DDI_NT_CD_CHAN;
6908 un->un_ctype = CTYPE_CDROM;
6909 break;
6910 case DTYPE_OPTICAL:
6911 un->un_node_type = DDI_NT_BLOCK_CHAN;
6912 un->un_ctype = CTYPE_ROD;
6913 break;
6914 default:
6915 un->un_node_type = DDI_NT_BLOCK_CHAN;
6916 un->un_ctype = CTYPE_CCS;
6917 break;
6918 }
6919
6920 /* Try to read the interconnect type from the HBA */
6921 un->un_f_is_fibre = FALSE;
6922 switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
6923 case INTERCONNECT_SSA:
6924 un->un_f_is_fibre = TRUE;
6925 un->un_interconnect_type = SD_INTERCONNECT_SSA;
6926 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6927 "sdattach: un:0x%p SD_INTERCONNECT_SSA\n", un);
6928 break;
6929 case INTERCONNECT_PARALLEL:
6930 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
6931 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6932 "sdattach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
6933 break;
6934 case INTERCONNECT_SAS:
6935 un->un_interconnect_type = SD_INTERCONNECT_SAS;
6936 un->un_node_type = DDI_NT_BLOCK_SAS;
6937 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6938 "sdattach: un:0x%p SD_INTERCONNECT_SAS\n", un);
6939 break;
6940 case INTERCONNECT_SATA:
6941 un->un_interconnect_type = SD_INTERCONNECT_SATA;
6942 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6943 "sdattach: un:0x%p SD_INTERCONNECT_SATA\n", un);
6944 break;
6945 case INTERCONNECT_FIBRE:
6946 un->un_f_is_fibre = TRUE;
6947 un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
6948 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6949 "sdattach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
6950 break;
6951 case INTERCONNECT_FABRIC:
6952 un->un_f_is_fibre = TRUE;
6953 un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
6954 un->un_node_type = DDI_NT_BLOCK_FABRIC;
6955 SD_INFO(SD_LOG_ATTACH_DETACH, un,
6956 "sdattach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
6957 break;
6958 default:
6959 /*
6960 * The default is to assume that if a device does not support
6961 * the "interconnect-type" property it is a parallel SCSI HBA
6962 * and set the interconnect type for parallel SCSI.
6963 */
6964 un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
6965 break;
6966 }
6967
6968 if (un->un_f_is_fibre == TRUE) {
6969 if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
6970 SCSI_VERSION_3) {
6971 switch (un->un_interconnect_type) {
6972 case SD_INTERCONNECT_FIBRE:
6973 case SD_INTERCONNECT_SSA:
6974 un->un_node_type = DDI_NT_BLOCK_WWN;
6975 break;
6976 default:
6977 break;
6978 }
6979 }
6980 }
6981
6982 (void) ddi_prop_update_int(DDI_DEV_T_NONE, devi,
6983 "allow-unconstrained-retire", 1);
6984
6985 /*
6986 * Initialize the Request Sense command for the target
6987 */
6988 if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
6989 goto alloc_rqs_failed;
6990 }
6991
6992 /* The value used is base on interconnect type */
6993 un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
6994
6995 /*
6996 * Set the per disk retry count to the default number of retries
6997 * for disks and CDROMs. This value can be overridden by the
6998 * disk property list or an entry in sd.conf.
6999 */
7000 un->un_notready_retry_count =
7001 ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7002 : DISK_NOT_READY_RETRY_COUNT(un);
7003
7004 /*
7005 * Set the busy retry count to the default value of un_retry_count.
7006 * This can be overridden by entries in sd.conf or the device
7007 * config table.
7008 */
7009 un->un_busy_retry_count = un->un_retry_count;
7010
7011 /*
7012 * Init the reset threshold for retries. This number determines
7013 * how many retries must be performed before a reset can be issued
7014 * (for certain error conditions). This can be overridden by entries
7015 * in sd.conf or the device config table.
7016 */
7017 un->un_reset_retry_count = (un->un_retry_count / 2);
7018
7019 /*
7020 * Set the victim_retry_count to the default un_retry_count.
7021 * This value is used in addition to the standard retry count.
7022 * This can be overridden by entries in sd.conf or the device
7023 * config table.
7024 */
7025 un->un_victim_retry_count = un->un_retry_count;
7026
7027 /*
7028 * Set the reservation release timeout to the default value of
7029 * 5 seconds. This can be overridden by entries in sd.conf or the
7030 * device config table.
7031 */
7032 un->un_reserve_release_time = 5;
7033
7034 un->un_io_time = sd_io_time;
7035
7036 un->un_slow_io_threshold = sd_slow_io_threshold;
7037
7038 un->un_f_lun_reset_enabled = sd_enable_lun_reset;
7039
7040 /*
7041 * Set up the default maximum transfer size. Note that this may
7042 * get updated later in the attach, when setting up default wide
7043 * operations for disks.
7044 */
7045 un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7046 un->un_partial_dma_supported = 1;
7047
7048 /*
7049 * Get "allow bus device reset" property (defaults to "enabled" if
7050 * the property was not defined). This is to disable bus resets for
7051 * certain kinds of error recovery. Note: In the future when a run-time
7052 * fibre check is available the soft state flag should default to
7053 * enabled.
7054 */
7055 if (un->un_f_is_fibre == TRUE) {
7056 un->un_f_allow_bus_device_reset = TRUE;
7057 } else {
7058 if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7059 "allow-bus-device-reset", 1) != 0) {
7060 un->un_f_allow_bus_device_reset = TRUE;
7061 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7062 "sdattach: un:0x%p Bus device reset "
7063 "enabled\n", un);
7064 } else {
7065 un->un_f_allow_bus_device_reset = FALSE;
7066 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7067 "sdattach: un:0x%p Bus device reset "
7068 "disabled\n", un);
7069 }
7070 }
7071
7072 /*
7073 * Check if this is an ATAPI device. ATAPI devices use Group 1
7074 * Read/Write commands and Group 2 Mode Sense/Select commands.
7075 *
7076 * Note: The "obsolete" way of doing this is to check for the "atapi"
7077 * property. The new "variant" property with a value of "atapi" has been
7078 * introduced so that future 'variants' of standard SCSI behavior (like
7079 * atapi) could be specified by the underlying HBA drivers by supplying
7080 * a new value for the "variant" property, instead of having to define a
7081 * new property.
7082 */
7083 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7084 un->un_f_cfg_is_atapi = TRUE;
7085 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7086 "sdattach: un:0x%p Atapi device\n", un);
7087 }
7088 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7089 &variantp) == DDI_PROP_SUCCESS) {
7090 if (strcmp(variantp, "atapi") == 0) {
7091 un->un_f_cfg_is_atapi = TRUE;
7092 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7093 "sdattach: un:0x%p Atapi device\n", un);
7094 }
7095 ddi_prop_free(variantp);
7096 }
7097
7098 un->un_cmd_timeout = ((ISCD(un)) ? 2 : 1) * (ushort_t)un->un_io_time;
7099 un->un_uscsi_timeout = un->un_cmd_timeout;
7100 un->un_busy_timeout = SD_BSY_TIMEOUT;
7101
7102 /*
7103 * Info on current states, statuses, etc. (Updated frequently)
7104 *
7105 * Current state is ATTACHING until we finished sd_unit_attach.
7106 * Last state is NORMAL so that sd_unit_attach can Restore_state()
7107 * when it finishes successfully.
7108 */
7109 un->un_state = SD_STATE_ATTACHING;
7110 un->un_last_state = SD_STATE_NORMAL;
7111
7112 /* Control & status info for command throttling */
7113 un->un_throttle = sd_max_throttle;
7114 un->un_saved_throttle = sd_max_throttle;
7115 un->un_min_throttle = sd_min_throttle;
7116
7117 if (un->un_f_is_fibre == TRUE) {
7118 un->un_f_use_adaptive_throttle = TRUE;
7119 } else {
7120 un->un_f_use_adaptive_throttle = FALSE;
7121 }
7122
7123 /* Unit detach has to pause until outstanding commands abort */
7124 un->un_f_detach_waiting = 0;
7125 cv_init(&un->un_detach_cv, NULL, CV_DRIVER, NULL);
7126
7127 /* Removable media support. */
7128 cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7129 un->un_mediastate = DKIO_NONE;
7130 un->un_specified_mediastate = DKIO_NONE;
7131
7132 /* CVs for suspend/resume (PM or DR) */
7133 cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
7134 cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7135
7136 /* Power management support. */
7137 un->un_power_level = SD_SPINDLE_UNINIT;
7138
7139 cv_init(&un->un_wcc_cv, NULL, CV_DRIVER, NULL);
7140 un->un_f_wcc_inprog = 0;
7141
7142 /*
7143 * The open/close semaphore is used to serialize threads executing
7144 * in the driver's open & close entry point routines for a given
7145 * instance.
7146 */
7149 /*
7150 * The conf file entry and softstate variable is a forceful override,
7151 * meaning a non-zero value must be entered to change the default.
7152 */
7153 un->un_f_disksort_disabled = FALSE;
7154 un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7155 un->un_f_enable_rmw = FALSE;
7156
7157 /*
7158 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7159 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7160 */
7161 un->un_f_mmc_gesn_polling = TRUE;
7162
7163 /*
7164 * physical sector size defaults to DEV_BSIZE currently. We can
7165 * override this value via the driver configuration file so we must
7166 * set it before calling sd_read_unit_properties().
7167 */
7168 un->un_phy_blocksize = DEV_BSIZE;
7169 un->un_f_sdconf_phy_blocksize = FALSE;
7170
7171 /*
7172 * Retrieve the properties from the static driver table or the driver
7173 * configuration file (.conf) for this unit and update the soft state
7174 * for the device as needed for the indicated properties.
7175 * Note: the property configuration needs to occur here as some of the
7176 * following routines may have dependencies on soft state flags set
7177 * as part of the driver property configuration.
7178 */
7179 sd_read_unit_properties(un);
7180 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7181 "sdattach: un:0x%p property configuration complete.\n", un);
7182
7183 /*
7184 * Only if a device has "hotpluggable" property, it is
7185 * treated as hotpluggable device. Otherwise, it is
7186 * regarded as non-hotpluggable one.
7187 */
7188 if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7189 -1) != -1) {
7190 un->un_f_is_hotpluggable = TRUE;
7191 }
7192
7193 /*
7194 * set unit's attributes(flags) according to "hotpluggable" and
7195 * RMB bit in INQUIRY data.
7196 */
7197 sd_set_unit_attributes(un, devi);
7198
7199 /*
7200 * By default, we mark the capacity, lbasize, and geometry
7201 * as invalid. Only if we successfully read a valid capacity
7247 * routines that send commands to the unit (either polled or via
7248 * sd_send_scsi_cmd).
7249 *
7250 * Note: This is a critical sequence that needs to be maintained:
7251 * 1) Instantiate the kstats here, before any routines using the
7252 * iopath (i.e. sd_send_scsi_cmd).
7253 * 2) Instantiate and initialize the partition stats
7254 * (sd_set_pstats).
7255 * 3) Initialize the error stats (sd_set_errstats), following
7256 * sd_validate_geometry(),sd_register_devid(),
7257 * and sd_cache_control().
7258 */
7259
7260 un->un_stats = kstat_create(sd_label, instance,
7261 NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7262 if (un->un_stats != NULL) {
7263 un->un_stats->ks_lock = SD_MUTEX(un);
7264 kstat_install(un->un_stats);
7265 }
7266 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7267 "sdattach: un:0x%p un_stats created\n", un);
7268
7269 un->un_unmapstats_ks = kstat_create(sd_label, instance, "unmapstats",
7270 "misc", KSTAT_TYPE_NAMED, sizeof (*un->un_unmapstats) /
7271 sizeof (kstat_named_t), 0);
7272 if (un->un_unmapstats_ks) {
7273 un->un_unmapstats = un->un_unmapstats_ks->ks_data;
7274
7275 kstat_named_init(&un->un_unmapstats->us_cmds,
7276 "commands", KSTAT_DATA_UINT64);
7277 kstat_named_init(&un->un_unmapstats->us_errs,
7278 "errors", KSTAT_DATA_UINT64);
7279 kstat_named_init(&un->un_unmapstats->us_extents,
7280 "extents", KSTAT_DATA_UINT64);
7281 kstat_named_init(&un->un_unmapstats->us_bytes,
7282 "bytes", KSTAT_DATA_UINT64);
7283
7284 kstat_install(un->un_unmapstats_ks);
7285 } else {
7286 cmn_err(CE_NOTE, "!Cannot create unmap kstats for disk %d",
7287 instance);
7288 }
7289
7290 un->un_lat_ksp = kstat_create(sd_label, instance, "io_latency",
7291 "io_latency", KSTAT_TYPE_RAW, sizeof (un_lat_stat_t),
7292 KSTAT_FLAG_PERSISTENT);
7293
7294 if (un->un_lat_ksp != NULL) {
7295 un->un_lat_ksp->ks_lock = SD_MUTEX(un);
7296 un->un_lat_stats = (un_lat_stat_t *)un->un_lat_ksp->ks_data;
7297 kstat_install(un->un_lat_ksp);
7298 } else {
7299 un->un_lat_stats = NULL;
7300 }
7301
7302 sd_create_errstats(un, instance);
7303 if (un->un_errstats == NULL) {
7304 goto create_errstats_failed;
7305 }
7306 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7307 "sdattach: un:0x%p errstats created\n", un);
7308
7309 /*
7310 * The following if/else code was relocated here from below as part
7311 * of the fix for bug (4430280). However with the default setup added
7312 * on entry to this routine, it's no longer absolutely necessary for
7313 * this to be before the call to sd_spin_up_unit.
7314 */
7315 if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7316 int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7317 (devp->sd_inq->inq_ansi == 5)) &&
7318 devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7319
7320 /*
7321 * If tagged queueing is supported by the target
7322 * and by the host adapter then we will enable it
7323 */
7324 un->un_tagflags = 0;
7325 if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7326 (un->un_f_arq_enabled == TRUE)) {
7327 if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7328 1, 1) == 1) {
7329 un->un_tagflags = FLAG_STAG;
7330 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7331 "sdattach: un:0x%p tag queueing "
7332 "enabled\n", un);
7333 } else if (scsi_ifgetcap(SD_ADDRESS(un),
7334 "untagged-qing", 0) == 1) {
7335 un->un_f_opt_queueing = TRUE;
7336 un->un_saved_throttle = un->un_throttle =
7337 min(un->un_throttle, 3);
7338 } else {
7339 un->un_f_opt_queueing = FALSE;
7340 un->un_saved_throttle = un->un_throttle = 1;
7341 }
7342 } else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7343 == 1) && (un->un_f_arq_enabled == TRUE)) {
7344 /* The Host Adapter supports internal queueing. */
7345 un->un_f_opt_queueing = TRUE;
7346 un->un_saved_throttle = un->un_throttle =
7347 min(un->un_throttle, 3);
7348 } else {
7349 un->un_f_opt_queueing = FALSE;
7350 un->un_saved_throttle = un->un_throttle = 1;
7351 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7352 "sdattach: un:0x%p no tag queueing\n", un);
7353 }
7354
7355 /*
7356 * Enable large transfers for SATA/SAS drives
7357 */
7358 if (SD_IS_SERIAL(un)) {
7359 un->un_max_xfer_size =
7360 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7361 "sd_max_xfer_size", SD_MAX_XFER_SIZE);
7362 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7363 "sdattach: un:0x%p max transfer "
7364 "size=0x%x\n", un, un->un_max_xfer_size);
7365
7366 }
7367
7368 /* Setup or tear down default wide operations for disks */
7369 if (SD_IS_PARALLEL_SCSI(un) &&
7370 (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7371 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7372 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7373 1, 1) == 1) {
7374 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7375 "sdattach: un:0x%p Wide Transfer "
7376 "enabled\n", un);
7377 }
7378
7379 /*
7380 * If tagged queuing has also been enabled, then
7381 * enable large xfers
7382 */
7383 if (un->un_saved_throttle == sd_max_throttle) {
7384 un->un_max_xfer_size =
7385 ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7386 "sd_max_xfer_size", SD_MAX_XFER_SIZE);
7387 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7388 "sdattach: un:0x%p max transfer "
7389 "size=0x%x\n", un, un->un_max_xfer_size);
7390 }
7391 } else {
7392 if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7393 0, 1) == 1) {
7394 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7395 "sdattach: un:0x%p "
7396 "Wide Transfer disabled\n", un);
7397 }
7398 }
7399 } else {
7400 un->un_tagflags = FLAG_STAG;
7401 un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7402 devi, 0, "sd_max_xfer_size", SD_MAX_XFER_SIZE);
7403 }
7404
7405 /*
7406 * If this target supports LUN reset, try to enable it.
7407 */
7408 if (un->un_f_lun_reset_enabled) {
7409 if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7410 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sdattach: "
7411 "un:0x%p lun_reset capability set\n", un);
7412 } else {
7413 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sdattach: "
7414 "un:0x%p lun-reset capability not set\n", un);
7415 }
7416 }
7417
7418 /*
7419 * XXX Adjust the maximum transfer size. This was to fix
7420 * the problem of partial DMA support on SPARC. Some
7421 * HBA driver, like aac, has very small dma_attr_maxxfer
7422 * size, which requires partial DMA support on SPARC.
7423 */
7424 max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7425 if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7426 un->un_max_xfer_size = max_xfer_size;
7427 if (un->un_partial_dma_supported == 0)
7428 un->un_partial_dma_supported = 1;
7429 }
7430 if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7431 DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7432 if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7433 un->un_max_xfer_size) == 1) {
7434 un->un_buf_breakup_supported = 1;
7435 SD_INFO(SD_LOG_ATTACH_DETACH, un, "sdattach: "
7436 "un:0x%p Buf breakup enabled\n", un);
7437 }
7438 }
7439
7440 /*
7441 * Set PKT_DMA_PARTIAL flag.
7442 */
7443 if (un->un_partial_dma_supported == 1) {
7444 un->un_pkt_flags = PKT_DMA_PARTIAL;
7445 } else {
7446 un->un_pkt_flags = 0;
7447 }
7448
7449 scsi_fm_init(devp);
7450
7451 /*
7452 * Allocate memory for SCSI FMA stuff.
7453 */
7454 un->un_fm_private =
7455 kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7456 sfip = (struct sd_fm_internal *)un->un_fm_private;
7457 sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7458 sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7459 sfip->fm_ssc.ssc_un = un;
7460
7461 if (ISCD(un) ||
7462 un->un_f_has_removable_media ||
7463 devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7464 /*
7465 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7466 * Their log are unchanged.
7467 */
7468 sfip->fm_log_level = SD_FM_LOG_NSUP;
7469 } else {
7470 /*
7471 * If enter here, it should be non-CDROM and FM-capable
7472 * device, and it will not keep the old scsi_log as before
7473 * in /var/adm/messages. However, the property
7474 * "fm-scsi-log" will control whether the FM telemetry will
7475 * be logged in /var/adm/messages.
7476 */
7477 int fm_scsi_log;
7478 fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7479 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7480
7481 if (fm_scsi_log)
7482 sfip->fm_log_level = SD_FM_LOG_EREPORT;
7483 else
7484 sfip->fm_log_level = SD_FM_LOG_SILENT;
7485 }
7486
7487 /* Initialize sd_ssc_t for internal uscsi commands */
7488 ssc = sd_ssc_init(un);
7489
7490 mutex_enter(SD_MUTEX(un));
7491 /*
7492 * Initialize the devid for the unit. Indicate target reservation so
7493 * that no real I/O is done for devices that need devid fabrication.
7494 * We will try again in sd_unit_attach() if necessary.
7495 */
7496 if (un->un_f_devid_supported) {
7497 sd_register_devid(ssc, devi, SD_TARGET_IS_RESERVED);
7498 }
7499 mutex_exit(SD_MUTEX(un));
7500
7501 /* Uninitialize sd_ssc_t pointer */
7502 sd_ssc_fini(ssc);
7503
7504 cmlb_alloc_handle(&un->un_cmlbhandle);
7505
7506 if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
7507 VOID2BOOLEAN(un->un_f_has_removable_media != 0),
7508 VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
7509 un->un_node_type, 0, un->un_cmlbhandle,
7510 (void *)SD_PATH_DIRECT) != 0) {
7511 goto cmlb_attach_failed;
7512 }
7513
7514 /*
7515 * At this point in the attach, we have enough info in the
7516 * soft state to be able to issue commands to the target.
7517 *
7518 * Schedule a taskq to finish attach to avoid holding the
7519 * device tree lock for too long. If this fails, rollback
7520 * and fail the attach.
7521 */
7522
7523 if (taskq_dispatch(sd_tq, sd_unit_attach, devi, KM_PUSHPAGE) != NULL)
7524 return (DDI_SUCCESS);
7525
7526 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
7527 cmlb_free_handle(&un->un_cmlbhandle);
7528
7529 cmlb_attach_failed:
7530 mutex_enter(SD_MUTEX(un));
7531
7532 /* Deallocate SCSI FMA memory spaces */
7533 kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
7534
7535 /* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
7536 if (un->un_direct_priority_timeid != NULL) {
7537 timeout_id_t temp_id = un->un_direct_priority_timeid;
7538 un->un_direct_priority_timeid = NULL;
7539 mutex_exit(SD_MUTEX(un));
7540 (void) untimeout(temp_id);
7541 mutex_enter(SD_MUTEX(un));
7542 }
7543
7544 /* Cancel any pending start/stop timeouts */
7545 if (un->un_startstop_timeid != NULL) {
7546 timeout_id_t temp_id = un->un_startstop_timeid;
7547 un->un_startstop_timeid = NULL;
7548 mutex_exit(SD_MUTEX(un));
7549 (void) untimeout(temp_id);
7550 mutex_enter(SD_MUTEX(un));
7551 }
7552
7553 /* Cancel any pending reset-throttle timeouts */
7554 if (un->un_reset_throttle_timeid != NULL) {
7555 timeout_id_t temp_id = un->un_reset_throttle_timeid;
7556 un->un_reset_throttle_timeid = NULL;
7557 mutex_exit(SD_MUTEX(un));
7558 (void) untimeout(temp_id);
7559 mutex_enter(SD_MUTEX(un));
7560 }
7561
7562 /* Cancel rmw warning message timeouts */
7563 if (un->un_rmw_msg_timeid != NULL) {
7564 timeout_id_t temp_id = un->un_rmw_msg_timeid;
7565 un->un_rmw_msg_timeid = NULL;
7566 mutex_exit(SD_MUTEX(un));
7567 (void) untimeout(temp_id);
7568 mutex_enter(SD_MUTEX(un));
7569 }
7570
7571 /* Cancel any pending retry timeouts */
7572 if (un->un_retry_timeid != NULL) {
7573 timeout_id_t temp_id = un->un_retry_timeid;
7574 un->un_retry_timeid = NULL;
7575 mutex_exit(SD_MUTEX(un));
7576 (void) untimeout(temp_id);
7577 mutex_enter(SD_MUTEX(un));
7578 }
7579
7580 /* Cancel any pending delayed cv broadcast timeouts */
7581 if (un->un_dcvb_timeid != NULL) {
7582 timeout_id_t temp_id = un->un_dcvb_timeid;
7583 un->un_dcvb_timeid = NULL;
7584 mutex_exit(SD_MUTEX(un));
7585 (void) untimeout(temp_id);
7586 mutex_enter(SD_MUTEX(un));
7587 }
7588
7589 mutex_exit(SD_MUTEX(un));
7590
7591 /* There should not be any in-progress I/O so ASSERT this check */
7592 ASSERT(un->un_ncmds_in_transport == 0);
7593 ASSERT(un->un_ncmds_in_driver == 0);
7594
7595 /* Do not free the softstate if the callback routine is active */
7596 sd_sync_with_callback(un);
7597
7598 /*
7599 * Partition stats apparently are not used with removables. These would
7600 * not have been created during attach, so no need to clean them up...
7601 */
7602 if (un->un_errstats != NULL) {
7603 kstat_delete(un->un_errstats);
7604 un->un_errstats = NULL;
7605 }
7606
7607 create_errstats_failed:
7608
7609 if (un->un_stats != NULL) {
7610 kstat_delete(un->un_stats);
7611 un->un_stats = NULL;
7612 }
7613
7614 if (un->un_unmapstats != NULL) {
7615 kstat_delete(un->un_unmapstats_ks);
7616 un->un_unmapstats_ks = NULL;
7617 un->un_unmapstats = NULL;
7618 }
7619
7620 if (un->un_lat_ksp != NULL) {
7621 kstat_delete(un->un_lat_ksp);
7622 un->un_lat_ksp = NULL;
7623 un->un_lat_stats = NULL;
7624 }
7625
7626 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
7627 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
7628
7629 ddi_prop_remove_all(devi);
7630 sema_destroy(&un->un_semoclose);
7631 cv_destroy(&un->un_state_cv);
7632 cv_destroy(&un->un_detach_cv);
7633 sd_free_rqs(un);
7634
7635 alloc_rqs_failed:
7636
7637 devp->sd_private = NULL;
7638 bzero(un, sizeof (struct sd_lun)); /* Clear any stale data! */
7639
7640 /*
7641 * Note: the man pages are unclear as to whether or not doing a
7642 * ddi_soft_state_free(sd_state, instance) is the right way to
7643 * clean up after the ddi_soft_state_zalloc() if the subsequent
7644 * ddi_get_soft_state() fails. The implication seems to be
7645 * that the get_soft_state cannot fail if the zalloc succeeds.
7646 */
7647 ddi_soft_state_free(sd_state, instance);
7648
7649 probe_failed:
7650 scsi_unprobe(devp);
7651
7652 return (DDI_FAILURE);
7653 }
7654
7655
7656 /*
7657 * Function: sddetach
7658 *
7659 * Description: Driver's detach(9E) entry point function.
7660 *
7661 * Arguments: devi - opaque device info handle
7662 * cmd - detach type
7663 *
7664 * Return Code: DDI_SUCCESS
7665 * DDI_FAILURE
7666 *
7667 * Context: Kernel thread context
7668 */
7669
7670 static int
7671 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7672 {
7673 switch (cmd) {
7674 case DDI_DETACH:
7675 return (sd_unit_detach(devi));
7676 case DDI_SUSPEND:
7677 return (sd_ddi_suspend(devi));
7678 default:
7679 break;
7680 }
7681 return (DDI_FAILURE);
7682 }
7683
7684
7685 /*
7686 * Function: sd_sync_with_callback
7687 *
7688 * Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7689 * state while the callback routine is active.
7690 *
7691 * Arguments: un: softstate structure for the instance
7692 *
7693 * Context: Kernel thread context
7694 */
7695
7696 static void
7697 sd_sync_with_callback(struct sd_lun *un)
7698 {
7699 ASSERT(un != NULL);
7700
7701 mutex_enter(SD_MUTEX(un));
7702
7703 ASSERT(un->un_in_callback >= 0);
7704
7705 while (un->un_in_callback > 0) {
7706 mutex_exit(SD_MUTEX(un));
7707 delay(2);
7708 mutex_enter(SD_MUTEX(un));
7709 }
7710
7711 mutex_exit(SD_MUTEX(un));
7712 }
7713
7714 /*
7715 * Function: sd_unit_attach
7716 *
7717 * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7718 * the soft state structure for the device and performs
7719 * all necessary structure and device initializations.
7720 *
7721 * Arguments: devi: the system's dev_info_t for the device.
7722 *
7723 * Return Code: DDI_SUCCESS if attach is successful.
7724 * DDI_FAILURE if any part of the attach fails.
7725 *
7726 * Context: Called at attach(9e) time for the DDI_ATTACH flag.
7727 * Kernel thread context only. Can sleep.
7728 */
7729 void
7730 sd_unit_attach(void *arg)
7731 {
7732 dev_info_t *devi = arg;
7733 struct scsi_device *devp = ddi_get_driver_private(devi);
7734 struct sd_lun *un = (struct sd_lun *)devp->sd_private;
7735 char name_str[48];
7736 int reservation_flag = SD_TARGET_IS_UNRESERVED;
7737 int rval;
7738 int wc_enabled;
7739 int wc_changeable;
7740 int tgt;
7741 uint64_t capacity;
7742 uint_t lbasize = 0;
7743 dev_info_t *pdip = ddi_get_parent(devi);
7744 int geom_label_valid = 0;
7745 sd_ssc_t *ssc;
7746 int status;
7747 char *devid;
7748
7749 /*
7750 * Retrieve the target ID of the device.
7751 */
7752 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7753 SCSI_ADDR_PROP_TARGET, -1);
7754
7755 /*
7756 * All command paths used below MUST issue their commands as
7757 * SD_PATH_DIRECT. This is important as intermediate layers
7758 * are not all initialized yet (such as PM).
7759 */
7760
7761 /* Initialize sd_ssc_t for internal uscsi commands */
7762 ssc = sd_ssc_init(un);
7763
7764 /*
7765 * Send a TEST UNIT READY command to the device. This should clear
7766 * any outstanding UNIT ATTENTION that may be present.
7767 *
7768 * Note: Don't check for success, just track if there is a reservation,
7769 * this is a throw away command to clear any unit attentions.
7770 *
7771 * Note: This MUST be the first command issued to the target during
7772 * attach to ensure power on UNIT ATTENTIONS are cleared.
7773 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7774 * with attempts at spinning up a device with no media.
7775 */
7776 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7777 if (status != 0) {
7778 if (status == EACCES)
7779 reservation_flag = SD_TARGET_IS_RESERVED;
7780 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7781 }
7782
7783 /*
7807 if (capacity > DK_MAX_BLOCKS) {
7808 #ifdef _LP64
7809 if ((capacity + 1) >
7810 SD_GROUP1_MAX_ADDRESS) {
7811 /*
7812 * Enable descriptor format
7813 * sense data so that we can
7814 * get 64 bit sense data
7815 * fields.
7816 */
7817 sd_enable_descr_sense(ssc);
7818 }
7819 #else
7820 /* 32-bit kernels can't handle this */
7821 scsi_log(SD_DEVINFO(un),
7822 sd_label, CE_WARN,
7823 "disk has %llu blocks, which "
7824 "is too large for a 32-bit "
7825 "kernel", capacity);
7826
7827 /*
7828 * 1TB disk was treated as (1T - 512)B
7829 * in the past, so that it might have
7830 * valid VTOC and solaris partitions,
7831 * we have to allow it to continue to
7832 * work.
7833 */
7834 if (capacity - 1 > DK_MAX_BLOCKS)
7835 goto spinup_failed;
7836 #endif
7837 }
7838
7839 /*
7840 * Here it's not necessary to check the case:
7841 * the capacity of the device is bigger than
7842 * what the max hba cdb can support. Because
7843 * sd_send_scsi_READ_CAPACITY will retrieve
7844 * the capacity by sending USCSI command, which
7845 * is constrained by the max hba cdb. Actually,
7846 * sd_send_scsi_READ_CAPACITY will return
7847 * EINVAL when using bigger cdb than required
7848 * cdb length. Will handle this case in
7849 * "case EINVAL".
7850 */
7851
7852 /*
7853 * The following relies on
7854 * sd_send_scsi_READ_CAPACITY never
7879 goto spinup_failed;
7880 case EACCES:
7881 /*
7882 * Should never get here if the spin-up
7883 * succeeded, but code it in anyway.
7884 * From here, just continue with the attach...
7885 */
7886 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7887 "sd_unit_attach: un:0x%p "
7888 "sd_send_scsi_READ_CAPACITY "
7889 "returned reservation conflict\n", un);
7890 reservation_flag = SD_TARGET_IS_RESERVED;
7891 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7892 break;
7893 default:
7894 /*
7895 * Likewise, should never get here if the
7896 * spin-up succeeded. Just continue with
7897 * the attach...
7898 */
7899 if (status == EIO) {
7900 sd_ssc_assessment(ssc,
7901 SD_FMT_STATUS_CHECK);
7902 goto spinup_failed;
7903 } else {
7904 sd_ssc_assessment(ssc,
7905 SD_FMT_IGNORE);
7906 }
7907 break;
7908 }
7909 break;
7910 case EACCES:
7911 /*
7912 * Device is reserved by another host. In this case
7913 * we could not spin it up or read the capacity, but
7914 * we continue with the attach anyway.
7915 */
7916 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7917 "sd_unit_attach: un:0x%p spin-up reservation "
7918 "conflict.\n", un);
7919 reservation_flag = SD_TARGET_IS_RESERVED;
7920 break;
7921 default:
7922 /* Fail the attach if the spin-up failed. */
7923 SD_INFO(SD_LOG_ATTACH_DETACH, un,
7924 "sd_unit_attach: un:0x%p spin-up failed.", un);
7925 goto spinup_failed;
7926 }
7936
7937 /*
7938 * Add a zero-length attribute to tell the world we support
7939 * kernel ioctls (for layered drivers)
7940 */
7941 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
7942 DDI_KERNEL_IOCTL, NULL, 0);
7943
7944 /*
7945 * Add a boolean property to tell the world we support
7946 * the B_FAILFAST flag (for layered drivers)
7947 */
7948 (void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
7949 "ddi-failfast-supported", NULL, 0);
7950
7951 /*
7952 * Initialize power management
7953 */
7954 mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
7955 cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
7956 #ifdef notyet
7957 sd_setup_pm(ssc, devi);
7958 #endif
7959 if (un->un_f_pm_is_enabled == FALSE) {
7960 /*
7961 * For performance, point to a jump table that does
7962 * not include pm.
7963 * The direct and priority chains don't change with PM.
7964 *
7965 * Note: this is currently done based on individual device
7966 * capabilities. When an interface for determining system
7967 * power enabled state becomes available, or when additional
7968 * layers are added to the command chain, these values will
7969 * have to be re-evaluated for correctness.
7970 */
7971 if (un->un_f_non_devbsize_supported) {
7972 un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
7973 } else {
7974 un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
7975 }
7976 un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
7977 }
7978
7979 /*
7980 * This property is set to 0 by HA software to avoid retries
7981 * on a reserved disk. (The preferred property name is
7982 * "retry-on-reservation-conflict") (1189689)
7983 *
7984 * Note: The use of a global here can have unintended consequences. A
7985 * per instance variable is preferable to match the capabilities of
7986 * different underlying hba's (4402600)
7987 */
7988 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
7989 DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
7990 sd_retry_on_reservation_conflict);
7991 if (sd_retry_on_reservation_conflict != 0) {
7992 sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
7993 devi, DDI_PROP_DONTPASS, "sd_retry_on_reservation_conflict",
7994 sd_retry_on_reservation_conflict);
7995 }
7996
7997 /* Set up options for QFULL handling. */
7998 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7999 "qfull-retries", -1)) != -1) {
8000 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8001 rval, 1);
8002 }
8003 if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8004 "qfull-retry-interval", -1)) != -1) {
8005 (void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8006 rval, 1);
8007 }
8008
8009 /*
8010 * This just prints a message that announces the existence of the
8011 * device. The message is always printed in the system logfile, but
8012 * only appears on the console if the system is booted with the
8013 * -v (verbose) argument.
8014 */
8015 ddi_report_dev(devi);
8016
8017 un->un_mediastate = DKIO_NONE;
8018
8019 /*
8020 * Check Block Device Characteristics VPD.
8021 */
8022 sd_check_bdc_vpd(ssc);
8023
8024 /*
8025 * Check whether the drive is in emulation mode.
8026 */
8027 sd_check_emulation_mode(ssc);
8028
8029 /* Compensate for off-by-1 legacy error */
8030 if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8031 (lbasize == un->un_sys_blocksize))
8032 cmlb_workaround_off_by_one(un->un_cmlbhandle);
8033
8034 /*
8035 * Read and validate the device's geometry (ie, disk label)
8036 * A new unformatted drive will not have a valid geometry, but
8037 * the driver needs to successfully attach to this device so
8038 * the drive can be formatted via ioctls.
8039 */
8040 geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8041 (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8042
8043 mutex_enter(SD_MUTEX(un));
8044
8045 /*
8046 * Read and initialize the devid for the unit if not done already.
8047 */
8048 if (un->un_f_devid_supported && un->un_devid == NULL) {
8049 sd_register_devid(ssc, devi, reservation_flag);
8050 }
8051 mutex_exit(SD_MUTEX(un));
8052
8053 if (un->un_f_opt_disable_cache == TRUE) {
8054 /*
8055 * Disable both read cache and write cache. This is
8056 * the historic behavior of the keywords in the config file.
8057 */
8058 if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8059 0) {
8060 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8061 "sd_unit_attach: un:0x%p Could not disable "
8062 "caching", un);
8063 goto devid_failed;
8064 }
8065 }
8066
8067 /*
8068 * Check the value of the WCE bit and if it's allowed to be changed,
8069 * set un_f_write_cache_enabled and un_f_cache_mode_changeable
8070 * accordingly.
8071 */
8072 (void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8144 *
8145 * Note: This is a critical sequence that needs to be maintained:
8146 * 1) Instantiate the kstats before any routines using the iopath
8147 * (i.e. sd_send_scsi_cmd).
8148 * 2) Initialize the error stats (sd_set_errstats) and partition
8149 * stats (sd_set_pstats)here, following
8150 * cmlb_validate_geometry(), sd_register_devid(), and
8151 * sd_cache_control().
8152 */
8153
8154 if (un->un_f_pkstats_enabled && geom_label_valid) {
8155 sd_set_pstats(un);
8156 SD_TRACE(SD_LOG_IO_PARTITION, un,
8157 "sd_unit_attach: un:0x%p pstats created and set\n", un);
8158 }
8159
8160 sd_set_errstats(un);
8161 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8162 "sd_unit_attach: un:0x%p errstats set\n", un);
8163
8164 sd_setup_blk_limits(ssc);
8165
8166 /*
8167 * After successfully attaching an instance, we record the information
8168 * of how many luns have been attached on the relative target and
8169 * controller for parallel SCSI. This information is used when sd tries
8170 * to set the tagged queuing capability in HBA.
8171 */
8172 if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8173 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8174 }
8175
8176 SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8177 "sd_unit_attach: un:0x%p exit success\n", un);
8178
8179 /* Uninitialize sd_ssc_t pointer */
8180 sd_ssc_fini(ssc);
8181
8182 /* attach finished, switch to SD_STATE_NORMAL */
8183 mutex_enter(SD_MUTEX(un));
8184 New_state(un, SD_STATE_NORMAL);
8185 cv_broadcast(&un->un_suspend_cv);
8186 mutex_exit(SD_MUTEX(un));
8187
8188 return;
8189
8190 /*
8191 * An error occurred during the attach; clean up & return failure.
8192 */
8193
8194 wm_cache_failed:
8195 devid_failed:
8196 /*
8197 * Cleanup from the scsi_ifsetcap() calls (437868)
8198 */
8199 (void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8200 (void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8201
8202 /*
8203 * Refer to the comments of setting tagged-qing in the beginning of
8204 * sd_unit_attach. We can only disable tagged queuing when there is
8205 * no lun attached on the target.
8206 */
8207 if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8208 (void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8209 }
8210
8211 if (un->un_f_is_fibre == FALSE) {
8212 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8213 }
8214
8215 spinup_failed:
8216 /* attach failed, switch to SD_STATE_ATTACH_FAILED */
8217 mutex_enter(SD_MUTEX(un));
8218 New_state(un, SD_STATE_ATTACH_FAILED);
8219 cv_broadcast(&un->un_suspend_cv);
8220 mutex_exit(SD_MUTEX(un));
8221
8222 devid = DEVI(devi)->devi_devid_str;
8223 scsi_fm_ereport_post(un->un_sd, 0,
8224 NULL, "disk.attach-failure", ssc->ssc_uscsi_info->ui_ena,
8225 devid, NULL, DDI_NOSLEEP, NULL,
8226 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
8227 DEVID_IF_KNOWN(devid));
8228
8229 /* Uninitialize sd_ssc_t pointer */
8230 sd_ssc_fini(ssc);
8231 SD_ERROR(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach failed: un: %p",
8232 (void *)un);
8233 }
8234
8235
8236 /*
8237 * Function: sd_unit_detach
8238 *
8239 * Description: Performs DDI_DETACH processing for sddetach().
8240 *
8241 * Return Code: DDI_SUCCESS
8242 * DDI_FAILURE
8243 *
8244 * Context: Kernel thread context
8245 */
8246
8247 static int
8248 sd_unit_detach(dev_info_t *devi)
8249 {
8250 struct scsi_device *devp;
8251 struct sd_lun *un;
8252 int i;
8253 int tgt;
8254 dev_t dev;
8255 dev_info_t *pdip = ddi_get_parent(devi);
8256 int instance = ddi_get_instance(devi);
8257
8258 mutex_enter(&sd_detach_mutex);
8259
8260 /*
8261 * Fail the detach for any of the following:
8262 * - Unable to get the sd_lun struct for the instance
8263 * - The instance is still attaching
8264 * - Another thread is already detaching this instance
8265 * - Another thread is currently performing an open
8266 *
8267 * Additionaly, if "device gone" flag is not set:
8268 * - There are outstanding commands in driver
8269 * - There are outstanding commands in transport
8270 */
8271 devp = ddi_get_driver_private(devi);
8272 if (devp == NULL || (un = (struct sd_lun *)devp->sd_private) == NULL ||
8273 un->un_detach_count != 0 || un->un_opens_in_progress != 0 ||
8274 (!DEVI_IS_GONE(devi) &&
8275 (un->un_state == SD_STATE_RWAIT ||
8276 un->un_state == SD_STATE_ATTACHING ||
8277 un->un_ncmds_in_driver != 0 ||
8278 un->un_ncmds_in_transport != 0))) {
8279 mutex_exit(&sd_detach_mutex);
8280 return (DDI_FAILURE);
8281 }
8282
8283 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "%s: entry 0x%p\n", __func__, un);
8284
8285 /*
8286 * Mark this instance as currently in a detach, to inhibit any
8287 * opens from a layered driver.
8288 */
8289 un->un_detach_count++;
8290 mutex_exit(&sd_detach_mutex);
8291
8292 tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8293 SCSI_ADDR_PROP_TARGET, -1);
8294
8295 dev = sd_make_device(SD_DEVINFO(un));
8296
8297 mutex_enter(SD_MUTEX(un));
8298
8299 /*
8300 * Fail the detach if there are any outstanding layered
8301 * opens on this device.
8302 */
8303 for (i = 0; i < NDKMAP; i++) {
8304 if (un->un_ocmap.lyropen[i] != 0) {
8305 goto err_notclosed;
8306 }
8307 }
8308
8309 /*
8310 * If the attach wasn't successful, some normal cleanup work must not
8311 * be done.
8312 */
8313 if (un->un_state == SD_STATE_ATTACH_FAILED) {
8314 mutex_exit(SD_MUTEX(un));
8315 goto no_attach_cleanup;
8316 }
8317
8318 /*
8319 * If we have the device reserved, release the reservation.
8320 */
8321 if (!DEVI_IS_GONE(devi) &&
8322 (un->un_resvd_status & SD_RESERVE) &&
8323 !(un->un_resvd_status & SD_LOST_RESERVE)) {
8324 mutex_exit(SD_MUTEX(un));
8325 /*
8326 * Note: sd_reserve_release sends a command to the device
8327 * via the sd_ioctlcmd() path, and can sleep.
8328 */
8329 if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8330 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8331 "%s: cannot release reservation\n", __func__);
8332 }
8333 } else {
8334 mutex_exit(SD_MUTEX(un));
8335 }
8336
8337 /*
8338 * Untimeout any reserve recover, throttle reset, restart unit
8339 * and delayed broadcast timeout threads. Protect the timeout pointer
8340 * from getting nulled by their callback functions.
8341 */
8342 mutex_enter(SD_MUTEX(un));
8343 if (un->un_resvd_timeid != NULL) {
8344 timeout_id_t temp_id = un->un_resvd_timeid;
8345 un->un_resvd_timeid = NULL;
8346 mutex_exit(SD_MUTEX(un));
8347 (void) untimeout(temp_id);
8348 mutex_enter(SD_MUTEX(un));
8349 }
8350
8351 if (un->un_reset_throttle_timeid != NULL) {
8368 timeout_id_t temp_id = un->un_rmw_msg_timeid;
8369 un->un_rmw_msg_timeid = NULL;
8370 mutex_exit(SD_MUTEX(un));
8371 (void) untimeout(temp_id);
8372 mutex_enter(SD_MUTEX(un));
8373 }
8374
8375 if (un->un_dcvb_timeid != NULL) {
8376 timeout_id_t temp_id = un->un_dcvb_timeid;
8377 un->un_dcvb_timeid = NULL;
8378 mutex_exit(SD_MUTEX(un));
8379 (void) untimeout(temp_id);
8380 } else {
8381 mutex_exit(SD_MUTEX(un));
8382 }
8383
8384 /* Remove any pending reservation reclaim requests for this device */
8385 sd_rmv_resv_reclaim_req(dev);
8386
8387 mutex_enter(SD_MUTEX(un));
8388 if (un->un_retry_timeid != NULL) {
8389 timeout_id_t temp_id = un->un_retry_timeid;
8390 un->un_retry_timeid = NULL;
8391 mutex_exit(SD_MUTEX(un));
8392 (void) untimeout(temp_id);
8393 mutex_enter(SD_MUTEX(un));
8394
8395 if (un->un_retry_bp != NULL) {
8396 un->un_retry_bp->av_forw = un->un_waitq_headp;
8397 un->un_waitq_headp = un->un_retry_bp;
8398 if (un->un_waitq_tailp == NULL)
8399 un->un_waitq_tailp = un->un_retry_bp;
8400 un->un_retry_bp = NULL;
8401 un->un_retry_statp = NULL;
8402 }
8403 }
8404
8405 if (DEVI_IS_GONE(SD_DEVINFO(un))) {
8406 /* abort in-flight IO */
8407 (void) scsi_abort(SD_ADDRESS(un), NULL);
8408 /* abort pending IO */
8409 un->un_failfast_state = SD_FAILFAST_ACTIVE;
8410 un->un_failfast_bp = NULL;
8411 sd_failfast_flushq(un, B_TRUE);
8412 }
8413
8414 /* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8415 if (un->un_direct_priority_timeid != NULL) {
8416 timeout_id_t temp_id = un->un_direct_priority_timeid;
8417 un->un_direct_priority_timeid = NULL;
8418 mutex_exit(SD_MUTEX(un));
8419 (void) untimeout(temp_id);
8420 mutex_enter(SD_MUTEX(un));
8421 }
8422
8423 /* Cancel any active multi-host disk watch thread requests */
8424 if (un->un_mhd_token != NULL) {
8425 mutex_exit(SD_MUTEX(un));
8426 if (scsi_watch_request_terminate(un->un_mhd_token,
8427 SCSI_WATCH_TERMINATE_NOWAIT)) {
8428 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8429 "%s: cannot cancel mhd watch request\n", __func__);
8430 /*
8431 * Note: We are returning here after having removed
8432 * some driver timeouts above. This is consistent with
8433 * the legacy implementation but perhaps the watch
8434 * terminate call should be made with the wait flag set.
8435 */
8436 goto err_remove_event;
8437 }
8438 mutex_enter(SD_MUTEX(un));
8439 un->un_mhd_token = NULL;
8440 }
8441
8442 if (un->un_swr_token != NULL) {
8443 mutex_exit(SD_MUTEX(un));
8444 if (scsi_watch_request_terminate(un->un_swr_token,
8445 SCSI_WATCH_TERMINATE_NOWAIT)) {
8446 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8447 "%s: cannot cancel swr watch request\n", __func__);
8448 /*
8449 * Note: We are returning here after having removed
8450 * some driver timeouts above. This is consistent with
8451 * the legacy implementation but perhaps the watch
8452 * terminate call should be made with the wait flag set.
8453 */
8454 goto err_remove_event;
8455 }
8456 mutex_enter(SD_MUTEX(un));
8457 un->un_swr_token = NULL;
8458 }
8459
8460 /*
8461 * Clear any scsi_reset_notifies. We clear the reset notifies
8462 * if we have not registered one.
8463 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8464 */
8465 mutex_exit(SD_MUTEX(un));
8466 (void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8467 sd_mhd_reset_notify_cb, (caddr_t)un);
8468
8469 mutex_enter(&un->un_pm_mutex);
8470 if (un->un_pm_idle_timeid != NULL) {
8471 timeout_id_t temp_id = un->un_pm_idle_timeid;
8472 un->un_pm_idle_timeid = NULL;
8473 mutex_exit(&un->un_pm_mutex);
8474
8475 /*
8476 * Timeout is active; cancel it.
8477 * Note that it'll never be active on a device
8478 * that does not support PM therefore we don't
8479 * have to check before calling pm_idle_component.
8480 */
8481 (void) untimeout(temp_id);
8482 (void) pm_idle_component(SD_DEVINFO(un), 0);
8483 mutex_enter(&un->un_pm_mutex);
8484 }
8485
8486 /*
8487 * Check whether there is already a timeout scheduled for power
8488 * management. If yes then don't lower the power here, that's.
8490 */
8491 if (un->un_pm_timeid != NULL) {
8492 timeout_id_t temp_id = un->un_pm_timeid;
8493 un->un_pm_timeid = NULL;
8494 mutex_exit(&un->un_pm_mutex);
8495 /*
8496 * Timeout is active; cancel it.
8497 * Note that it'll never be active on a device
8498 * that does not support PM therefore we don't
8499 * have to check before calling pm_idle_component.
8500 */
8501 (void) untimeout(temp_id);
8502 (void) pm_idle_component(SD_DEVINFO(un), 0);
8503
8504 } else {
8505 mutex_exit(&un->un_pm_mutex);
8506 if ((un->un_f_pm_is_enabled == TRUE) &&
8507 (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8508 != DDI_SUCCESS)) {
8509 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8510 "%s: lower power request failed, ignoring\n",
8511 __func__);
8512 /*
8513 * The above test now includes a check to see if PM is
8514 * supported by this device before call
8515 * pm_lower_power().
8516 * Note, the following is not dead code. The call to
8517 * pm_lower_power above will generate a call back into
8518 * our sdpower routine which might result in a timeout
8519 * handler getting activated. Therefore the following
8520 * code is valid and necessary.
8521 */
8522 mutex_enter(&un->un_pm_mutex);
8523 if (un->un_pm_timeid != NULL) {
8524 timeout_id_t temp_id = un->un_pm_timeid;
8525 un->un_pm_timeid = NULL;
8526 mutex_exit(&un->un_pm_mutex);
8527 (void) untimeout(temp_id);
8528 (void) pm_idle_component(SD_DEVINFO(un), 0);
8529 } else {
8530 mutex_exit(&un->un_pm_mutex);
8531 }
8532 }
8555 }
8556
8557 if (un->un_f_is_fibre == FALSE) {
8558 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8559 }
8560
8561 /*
8562 * Remove any event callbacks, fibre only
8563 */
8564 if (un->un_f_is_fibre == TRUE) {
8565 if ((un->un_insert_event != NULL) &&
8566 (ddi_remove_event_handler(un->un_insert_cb_id) !=
8567 DDI_SUCCESS)) {
8568 /*
8569 * Note: We are returning here after having done
8570 * substantial cleanup above. This is consistent
8571 * with the legacy implementation but this may not
8572 * be the right thing to do.
8573 */
8574 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8575 "%s: cannot cancel insert event\n", __func__);
8576 goto err_remove_event;
8577 }
8578 un->un_insert_event = NULL;
8579
8580 if ((un->un_remove_event != NULL) &&
8581 (ddi_remove_event_handler(un->un_remove_cb_id) !=
8582 DDI_SUCCESS)) {
8583 /*
8584 * Note: We are returning here after having done
8585 * substantial cleanup above. This is consistent
8586 * with the legacy implementation but this may not
8587 * be the right thing to do.
8588 */
8589 SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8590 "%s: cannot cancel remove event\n", __func__);
8591 goto err_remove_event;
8592 }
8593 un->un_remove_event = NULL;
8594 }
8595
8596 /* Do not free the softstate if the callback routine is active */
8597 sd_sync_with_callback(un);
8598
8599 no_attach_cleanup:
8600 /*
8601 * The driver must wait, at least attempt to wait, for any commands
8602 * still in the driver.
8603 */
8604 mutex_enter(SD_MUTEX(un));
8605
8606 while (un->un_ncmds_in_driver != 0) {
8607 clock_t max_delay = ddi_get_lbolt() + SEC_TO_TICK(30);
8608 un->un_f_detach_waiting = 1;
8609 if (cv_timedwait(&un->un_detach_cv, SD_MUTEX(un),
8610 max_delay) == -1) {
8611 break;
8612 }
8613 }
8614
8615 un->un_f_detach_waiting = 0;
8616 mutex_exit(SD_MUTEX(un));
8617
8618 cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8619 cmlb_free_handle(&un->un_cmlbhandle);
8620
8621 /*
8622 * Hold the detach mutex here, to make sure that no other threads ever
8623 * can access a (partially) freed soft state structure.
8624 */
8625 mutex_enter(&sd_detach_mutex);
8626
8627 /*
8628 * Clean up the soft state struct.
8629 * Cleanup is done in reverse order of allocs/inits.
8630 * At this point there should be no competing threads anymore.
8631 */
8632
8633 scsi_fm_fini(devp);
8634
8635 /*
8636 * Deallocate memory for SCSI FMA.
8637 */
8654 }
8655
8656 /*
8657 * Destroy wmap cache if it exists.
8658 */
8659 if (un->un_wm_cache != NULL) {
8660 kmem_cache_destroy(un->un_wm_cache);
8661 un->un_wm_cache = NULL;
8662 }
8663
8664 /*
8665 * kstat cleanup is done in detach for all device types (4363169).
8666 * We do not want to fail detach if the device kstats are not deleted
8667 * since there is a confusion about the devo_refcnt for the device.
8668 * We just delete the kstats and let detach complete successfully.
8669 */
8670 if (un->un_stats != NULL) {
8671 kstat_delete(un->un_stats);
8672 un->un_stats = NULL;
8673 }
8674 if (un->un_unmapstats != NULL) {
8675 kstat_delete(un->un_unmapstats_ks);
8676 un->un_unmapstats_ks = NULL;
8677 un->un_unmapstats = NULL;
8678 }
8679 if (un->un_lat_ksp != NULL) {
8680 kstat_delete(un->un_lat_ksp);
8681 un->un_lat_stats = NULL;
8682 un->un_lat_ksp = NULL;
8683 }
8684 if (un->un_errstats != NULL) {
8685 kstat_delete(un->un_errstats);
8686 un->un_errstats = NULL;
8687 }
8688
8689 /* Remove partition stats */
8690 if (un->un_f_pkstats_enabled) {
8691 for (i = 0; i < NSDMAP; i++) {
8692 if (un->un_pstats[i] != NULL) {
8693 kstat_delete(un->un_pstats[i]);
8694 un->un_pstats[i] = NULL;
8695 }
8696 }
8697 }
8698
8699 /* Remove xbuf registration */
8700 ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8701 ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8702
8703 /* Remove driver properties */
8704 ddi_prop_remove_all(devi);
8705
8706 mutex_destroy(&un->un_pm_mutex);
8707 cv_destroy(&un->un_pm_busy_cv);
8708
8709 cv_destroy(&un->un_wcc_cv);
8710
8711 /* Open/close semaphore */
8712 sema_destroy(&un->un_semoclose);
8713
8714 /* Used to wait for outstanding commands */
8715 cv_destroy(&un->un_detach_cv);
8716
8717 /* Removable media condvar. */
8718 cv_destroy(&un->un_state_cv);
8719
8720 /* Suspend/resume condvar. */
8721 cv_destroy(&un->un_suspend_cv);
8722 cv_destroy(&un->un_disk_busy_cv);
8723
8724 sd_free_rqs(un);
8725
8726 /* Free up soft state */
8727 devp->sd_private = NULL;
8728
8729 bzero(un, sizeof (struct sd_lun));
8730
8731 ddi_soft_state_free(sd_state, instance);
8732
8733 mutex_exit(&sd_detach_mutex);
8734
8735 /* This frees up the INQUIRY data associated with the device. */
8736 scsi_unprobe(devp);
8737
8738 /*
8739 * After successfully detaching an instance, we update the information
8740 * of how many luns have been attached in the relative target and
8741 * controller for parallel SCSI. This information is used when sd tries
8742 * to set the tagged queuing capability in HBA.
8743 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8744 * check if the device is parallel SCSI. However, we don't need to
8745 * check here because we've already checked during attach. No device
8746 * that is not parallel SCSI is in the chain.
8747 */
8748 if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8749 sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
8750 }
8751
8752 ddi_remove_minor_node(devi, NULL);
8753 (void) devfs_clean(devi, NULL, DV_CLEAN_FORCE);
8754
8755 return (DDI_SUCCESS);
8756
8757 err_notclosed:
8758 mutex_exit(SD_MUTEX(un));
8759
8760 err_remove_event:
8761 mutex_enter(&sd_detach_mutex);
8762 un->un_detach_count--;
8763 mutex_exit(&sd_detach_mutex);
8764
8765 SD_TRACE(SD_LOG_ATTACH_DETACH, un, "%s: exit failure\n", __func__);
8766 return (DDI_FAILURE);
8767 }
8768
8769
8770 /*
8771 * Function: sd_create_errstats
8772 *
8773 * Description: This routine instantiates the device error stats.
8774 *
8775 * Note: During attach the stats are instantiated first so they are
8776 * available for attach-time routines that utilize the driver
8777 * iopath to send commands to the device. The stats are initialized
8778 * separately so data obtained during some attach-time routines is
8779 * available. (4362483)
8780 *
8781 * Arguments: un - driver soft state (unit) structure
8782 * instance - driver instance
8783 *
8784 * Context: Kernel thread context
8785 */
8952 * (4363169)
8953 *
8954 * Arguments: un - driver soft state (unit) structure
8955 *
8956 * Context: Kernel thread context
8957 */
8958
8959 static void
8960 sd_set_pstats(struct sd_lun *un)
8961 {
8962 char kstatname[KSTAT_STRLEN];
8963 int instance;
8964 int i;
8965 diskaddr_t nblks = 0;
8966 char *partname = NULL;
8967
8968 ASSERT(un != NULL);
8969
8970 instance = ddi_get_instance(SD_DEVINFO(un));
8971
8972 /* XXX is this a VTOC8/VTOC16 difference? */
8973 for (i = 0; i < NSDMAP; i++) {
8974 if (cmlb_partinfo(un->un_cmlbhandle, i,
8975 &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
8976 continue;
8977 mutex_enter(SD_MUTEX(un));
8978
8979 if ((un->un_pstats[i] == NULL) &&
8980 (nblks != 0)) {
8981
8982 (void) snprintf(kstatname, sizeof (kstatname),
8983 "%s%d,%s", sd_label, instance,
8984 partname);
8985
8986 un->un_pstats[i] = kstat_create(sd_label,
8987 instance, kstatname, "partition", KSTAT_TYPE_IO,
8988 1, KSTAT_FLAG_PERSISTENT);
8989 if (un->un_pstats[i] != NULL) {
8990 un->un_pstats[i]->ks_lock = SD_MUTEX(un);
8991 kstat_install(un->un_pstats[i]);
8992 }
8993 }
8994 mutex_exit(SD_MUTEX(un));
8995 }
8996 }
8997
8998 /*
8999 * Values related to caching mode page depending on whether the unit is ATAPI.
9000 */
9001 #define SDC_CDB_GROUP(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9002 CDB_GROUP1 : CDB_GROUP0)
9003 #define SDC_HDRLEN(un) ((un->un_f_cfg_is_atapi == TRUE) ? \
9004 MODE_HEADER_LENGTH_GRP2 : MODE_HEADER_LENGTH)
9005 /*
9006 * Use mode_cache_scsi3 to ensure we get all of the mode sense data, otherwise
9007 * the mode select will fail (mode_cache_scsi3 is a superset of mode_caching).
9008 */
9009 #define SDC_BUFLEN(un) (SDC_HDRLEN(un) + MODE_BLK_DESC_LENGTH + \
9010 sizeof (struct mode_cache_scsi3))
9011
9012 static int
9013 sd_get_caching_mode_page(sd_ssc_t *ssc, uchar_t page_control, uchar_t **header,
9014 int *bdlen)
9015 {
9016 struct sd_lun *un = ssc->ssc_un;
9017 struct mode_caching *mode_caching_page;
9018 size_t buflen = SDC_BUFLEN(un);
9381 * If either sd.conf or internal disk table
9382 * specifies cache flush be suppressed, then
9383 * we don't bother checking NV_SUP bit.
9384 */
9385 if (un->un_f_suppress_cache_flush == TRUE) {
9386 mutex_exit(SD_MUTEX(un));
9387 return;
9388 }
9389
9390 if (sd_check_vpd_page_support(ssc) == 0 &&
9391 un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9392 mutex_exit(SD_MUTEX(un));
9393 /* collect page 86 data if available */
9394 inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9395
9396 rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9397 0x01, 0x86, &inq86_resid);
9398
9399 if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9400 SD_TRACE(SD_LOG_COMMON, un,
9401 "sd_get_nv_sup: successfully get VPD page: %x "
9402 "PAGE LENGTH: %x BYTE 6: %x\n",
9403 inq86[1], inq86[3], inq86[6]);
9404
9405 mutex_enter(SD_MUTEX(un));
9406 /*
9407 * check the value of NV_SUP bit: only if the device
9408 * reports NV_SUP bit to be 1, the
9409 * un_f_sync_nv_supported bit will be set to true.
9410 */
9411 if (inq86[6] & SD_VPD_NV_SUP) {
9412 un->un_f_sync_nv_supported = TRUE;
9413 }
9414 mutex_exit(SD_MUTEX(un));
9415 } else if (rval != 0) {
9416 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9417 }
9418
9419 kmem_free(inq86, inq86_len);
9420 } else {
9421 mutex_exit(SD_MUTEX(un));
9422 }
9724 int instance;
9725 dev_t dev;
9726 int rval = EIO;
9727 diskaddr_t nblks = 0;
9728 diskaddr_t label_cap;
9729
9730 /* Validate the open type */
9731 if (otyp >= OTYPCNT) {
9732 return (EINVAL);
9733 }
9734
9735 dev = *dev_p;
9736 instance = SDUNIT(dev);
9737 mutex_enter(&sd_detach_mutex);
9738
9739 /*
9740 * Fail the open if there is no softstate for the instance, or
9741 * if another thread somewhere is trying to detach the instance.
9742 */
9743 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
9744 un->un_detach_count != 0 || DEVI_IS_GONE(SD_DEVINFO(un))) {
9745 mutex_exit(&sd_detach_mutex);
9746 /*
9747 * The probe cache only needs to be cleared when open (9E) fails
9748 * with ENXIO.
9749 */
9750 sd_scsi_clear_probe_cache();
9751 return (ENXIO);
9752 }
9753
9754 /*
9755 * The un_layer_count is to prevent another thread in specfs from
9756 * trying to detach the instance, which can happen when we are
9757 * called from a higher-layer driver instead of thru specfs.
9758 * This will not be needed when DDI provides a layered driver
9759 * interface that allows specfs to know that an instance is in
9760 * use by a layered driver & should not be detached.
9761 *
9762 * Note: the semantics for layered driver opens are exactly one
9763 * close for every open.
9764 */
9765 if (otyp == OTYP_LYR) {
9766 un->un_layer_count++;
9767 }
9768
9769 /*
9780 part = SDPART(dev);
9781 partmask = 1 << part;
9782
9783 /*
9784 * We use a semaphore here in order to serialize
9785 * open and close requests on the device.
9786 */
9787 sema_p(&un->un_semoclose);
9788
9789 mutex_enter(SD_MUTEX(un));
9790
9791 /*
9792 * All device accesses go thru sdstrategy() where we check
9793 * on suspend status but there could be a scsi_poll command,
9794 * which bypasses sdstrategy(), so we need to check pm
9795 * status.
9796 */
9797
9798 if (!nodelay) {
9799 while ((un->un_state == SD_STATE_SUSPENDED) ||
9800 (un->un_state == SD_STATE_PM_CHANGING) ||
9801 (un->un_state == SD_STATE_ATTACHING)) {
9802 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
9803 }
9804 mutex_exit(SD_MUTEX(un));
9805 if (sd_pm_entry(un) != DDI_SUCCESS) {
9806 rval = EIO;
9807 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
9808 "sdopen: sd_pm_entry failed\n");
9809 goto open_failed_with_pm;
9810 }
9811 mutex_enter(SD_MUTEX(un));
9812 } else if (un->un_state == SD_STATE_ATTACH_FAILED) {
9813 mutex_exit(SD_MUTEX(un));
9814 rval = EIO;
9815 SD_ERROR(SD_LOG_OPEN_CLOSE, un,
9816 "sdopen: attach failed, can't open\n");
9817 goto open_failed_not_attached;
9818 }
9819
9820 /* check for previous exclusive open */
9821 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
9822 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
9823 "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
9824 un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
9825
9826 if (un->un_exclopen & (partmask)) {
9827 goto excl_open_fail;
9828 }
9829
9830 if (flag & FEXCL) {
9831 int i;
9832 if (un->un_ocmap.lyropen[part]) {
9833 goto excl_open_fail;
9834 }
9835 for (i = 0; i < (OTYPCNT - 1); i++) {
9836 if (un->un_ocmap.regopen[i] & (partmask)) {
9837 goto excl_open_fail;
9888 * blocks is zero or negative for non CD devices.
9889 */
9890
9891 nblks = 0;
9892
9893 if (rval == SD_READY_VALID && (!ISCD(un))) {
9894 /* if cmlb_partinfo fails, nblks remains 0 */
9895 mutex_exit(SD_MUTEX(un));
9896 (void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
9897 NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
9898 mutex_enter(SD_MUTEX(un));
9899 }
9900
9901 if ((rval != SD_READY_VALID) ||
9902 (!ISCD(un) && nblks <= 0)) {
9903 rval = un->un_f_has_removable_media ? ENXIO : EIO;
9904 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9905 "device not ready or invalid disk block value\n");
9906 goto open_fail;
9907 }
9908 } else {
9909 uchar_t *cp;
9910 /*
9911 * x86 requires special nodelay handling, so that p0 is
9912 * always defined and accessible.
9913 * Invalidate geometry only if device is not already open.
9914 */
9915 cp = &un->un_ocmap.chkd[0];
9916 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
9917 if (*cp != (uchar_t)0) {
9918 break;
9919 }
9920 cp++;
9921 }
9922 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
9923 mutex_exit(SD_MUTEX(un));
9924 cmlb_invalidate(un->un_cmlbhandle,
9925 (void *)SD_PATH_DIRECT);
9926 mutex_enter(SD_MUTEX(un));
9927 }
9928 }
9929
9930 if (otyp == OTYP_LYR) {
9931 un->un_ocmap.lyropen[part]++;
9932 } else {
9933 un->un_ocmap.regopen[otyp] |= partmask;
9934 }
9935
9936 /* Set up open and exclusive open flags */
9937 if (flag & FEXCL) {
9938 un->un_exclopen |= (partmask);
9939 }
9940
9941 /*
9942 * If the lun is EFI labeled and lun capacity is greater than the
9943 * capacity contained in the label, log a sys-event to notify the
9944 * interested module.
9945 * To avoid an infinite loop of logging sys-event, we only log the
9946 * event when the lun is not opened in NDELAY mode. The event handler
9947 * should open the lun in NDELAY mode.
9951 if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
9952 (void*)SD_PATH_DIRECT) == 0) {
9953 mutex_enter(SD_MUTEX(un));
9954 if (un->un_f_blockcount_is_valid &&
9955 un->un_blockcount > label_cap &&
9956 un->un_f_expnevent == B_FALSE) {
9957 un->un_f_expnevent = B_TRUE;
9958 mutex_exit(SD_MUTEX(un));
9959 sd_log_lun_expansion_event(un,
9960 (nodelay ? KM_NOSLEEP : KM_SLEEP));
9961 mutex_enter(SD_MUTEX(un));
9962 }
9963 } else {
9964 mutex_enter(SD_MUTEX(un));
9965 }
9966 }
9967
9968 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9969 "open of part %d type %d\n", part, otyp);
9970
9971 /*
9972 * If we made it here, the disk is alive.
9973 * Make sure it is set to normal state.
9974 */
9975 New_state(un, SD_STATE_NORMAL);
9976
9977 mutex_exit(SD_MUTEX(un));
9978 if (!nodelay) {
9979 sd_pm_exit(un);
9980 }
9981
9982 sema_v(&un->un_semoclose);
9983
9984 mutex_enter(&sd_detach_mutex);
9985 un->un_opens_in_progress--;
9986 mutex_exit(&sd_detach_mutex);
9987
9988 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
9989 return (DDI_SUCCESS);
9990
9991 excl_open_fail:
9992 SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
9993 rval = EBUSY;
9994
9995 open_fail:
9996 mutex_exit(SD_MUTEX(un));
9997
9998 /*
9999 * On a failed open we must exit the pm management.
10000 */
10001 if (!nodelay) {
10002 sd_pm_exit(un);
10003 }
10004 open_failed_with_pm:
10005 open_failed_not_attached:
10006 sema_v(&un->un_semoclose);
10007
10008 mutex_enter(&sd_detach_mutex);
10009 un->un_opens_in_progress--;
10010 if (otyp == OTYP_LYR) {
10011 un->un_layer_count--;
10012 }
10013 mutex_exit(&sd_detach_mutex);
10014
10015 return (rval);
10016 }
10017
10018
10019 /*
10020 * Function: sdclose
10021 *
10022 * Description: Driver's close(9e) entry point function.
10023 *
10024 * Arguments: dev - device number
10025 * flag - file status flag, informational only
10028 *
10029 * Return Code: ENXIO
10030 *
10031 * Context: Kernel thread context
10032 */
10033 /* ARGSUSED */
10034 static int
10035 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10036 {
10037 struct sd_lun *un;
10038 uchar_t *cp;
10039 int part;
10040 int nodelay;
10041 int rval = 0;
10042
10043 /* Validate the open type */
10044 if (otyp >= OTYPCNT) {
10045 return (ENXIO);
10046 }
10047
10048 /* Hold the detach mutex to allow close to complete */
10049 mutex_enter(&sd_detach_mutex);
10050
10051 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10052 mutex_exit(&sd_detach_mutex);
10053 return (ENXIO);
10054 }
10055
10056 part = SDPART(dev);
10057 nodelay = flag & (FNDELAY | FNONBLOCK);
10058
10059 SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10060 "sdclose: close of part %d type %d\n", part, otyp);
10061
10062 /*
10063 * We use a semaphore here in order to serialize
10064 * open and close requests on the device.
10065 */
10066 sema_p(&un->un_semoclose);
10067
10068 mutex_enter(SD_MUTEX(un));
10069
10070 /* Don't proceed if power is being changed or we're still attaching. */
10071 while ((un->un_state == SD_STATE_PM_CHANGING) ||
10072 (un->un_state == SD_STATE_ATTACHING)) {
10073 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10074 }
10075
10076 if (un->un_exclopen & (1 << part)) {
10077 un->un_exclopen &= ~(1 << part);
10078 }
10079
10080 /* Update the open partition map */
10081 if (otyp == OTYP_LYR) {
10082 un->un_ocmap.lyropen[part] -= 1;
10083 } else {
10084 un->un_ocmap.regopen[otyp] &= ~(1 << part);
10085 }
10086
10087 cp = &un->un_ocmap.chkd[0];
10088 while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10089 if (*cp != NULL) {
10090 break;
10091 }
10092 cp++;
10093 }
10094
10095 if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10096 SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10097
10098 /*
10099 * We avoid persistance upon the last close, and set
10100 * the throttle back to the maximum.
10101 */
10102 un->un_throttle = un->un_saved_throttle;
10103
10104 if (un->un_state == SD_STATE_OFFLINE) {
10105 if (un->un_f_is_fibre == FALSE) {
10106 scsi_log(SD_DEVINFO(un), sd_label,
10107 CE_WARN, "offline\n");
10108 }
10109 mutex_exit(SD_MUTEX(un));
10110 cmlb_invalidate(un->un_cmlbhandle,
10111 (void *)SD_PATH_DIRECT);
10112 mutex_enter(SD_MUTEX(un));
10113 } else if (un->un_state != SD_STATE_ATTACH_FAILED) {
10114 /*
10115 * Flush any outstanding writes in NVRAM cache.
10116 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10117 * cmd, it may not work for non-Pluto devices.
10118 * SYNCHRONIZE CACHE is not required for removables,
10119 * except DVD-RAM drives.
10120 *
10121 * Also note: because SYNCHRONIZE CACHE is currently
10122 * the only command issued here that requires the
10123 * drive be powered up, only do the power up before
10124 * sending the Sync Cache command. If additional
10125 * commands are added which require a powered up
10126 * drive, the following sequence may have to change.
10127 */
10128 if (!DEVI_IS_GONE(SD_DEVINFO(un)) &&
10129 ((un->un_f_sync_cache_supported &&
10130 un->un_f_sync_cache_required) ||
10131 un->un_f_dvdram_writable_device == TRUE)) {
10132 mutex_exit(SD_MUTEX(un));
10133 if (sd_pm_entry(un) == DDI_SUCCESS) {
10134 rval =
10135 sd_send_scsi_SYNCHRONIZE_CACHE(un,
10136 NULL);
10137 /* ignore error if not supported */
10138 if (rval == ENOTSUP) {
10139 rval = 0;
10140 } else if (rval != 0) {
10141 rval = EIO;
10142 }
10143 sd_pm_exit(un);
10144 } else {
10145 rval = EIO;
10146 }
10147 mutex_enter(SD_MUTEX(un));
10148 }
10149
10150 /*
10151 * For devices which supports DOOR_LOCK, send an ALLOW
10161 ssc = sd_ssc_init(un);
10162 rval = sd_send_scsi_DOORLOCK(ssc,
10163 SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10164 if (rval != 0)
10165 sd_ssc_assessment(ssc,
10166 SD_FMT_IGNORE);
10167 sd_ssc_fini(ssc);
10168
10169 sd_pm_exit(un);
10170 if (ISCD(un) && (rval != 0) &&
10171 (nodelay != 0)) {
10172 rval = ENXIO;
10173 }
10174 } else {
10175 rval = EIO;
10176 }
10177 mutex_enter(SD_MUTEX(un));
10178 }
10179
10180 /*
10181 * Pardon a device that is currently in failfast
10182 * active state, to not bias a future open.
10183 */
10184 un->un_failfast_state = SD_FAILFAST_INACTIVE;
10185
10186 /*
10187 * If a device has removable media, invalidate all
10188 * parameters related to media, such as geometry,
10189 * blocksize, and blockcount.
10190 */
10191 if (un->un_f_has_removable_media) {
10192 sr_ejected(un);
10193 }
10194
10195 /*
10196 * Destroy the cache (if it exists) which was
10197 * allocated for the write maps since this is
10198 * the last close for this media.
10199 */
10200 if (un->un_wm_cache) {
10201 /*
10202 * Check if there are pending commands.
10203 * and if there are give a warning and
10204 * do not destroy the cache.
10205 */
10206 if (un->un_ncmds_in_driver > 0) {
10207 scsi_log(SD_DEVINFO(un),
10208 sd_label, CE_WARN,
10209 "Unable to clean up memory "
10210 "because of pending I/O\n");
10211 } else {
10212 kmem_cache_destroy(
10213 un->un_wm_cache);
10214 un->un_wm_cache = NULL;
10215 }
10216 }
10217 }
10218 }
10219
10220 mutex_exit(SD_MUTEX(un));
10221 sema_v(&un->un_semoclose);
10222
10223 if (otyp == OTYP_LYR)
10224 un->un_layer_count--;
10225
10226 mutex_exit(&sd_detach_mutex);
10227
10228 return (rval);
10229 }
10230
10231
10232 /*
10233 * Function: sd_ready_and_valid
10234 *
10235 * Description: Test if device is ready and has a valid geometry.
10236 *
10237 * Arguments: ssc - sd_ssc_t will contain un
10238 * un - driver soft state (unit) structure
10239 *
10240 * Return Code: SD_READY_VALID ready and valid label
10241 * SD_NOT_READY_VALID not ready, no label
10242 * SD_RESERVED_BY_OTHERS reservation conflict
10243 *
10244 * Context: Never called at interrupt context.
10245 */
10246
10303 } else {
10304 mutex_enter(SD_MUTEX(un));
10305 sd_update_block_info(un, lbasize, capacity);
10306 }
10307 }
10308
10309 /*
10310 * Check if the media in the device is writable or not.
10311 */
10312 if (!is_valid && ISCD(un)) {
10313 sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10314 }
10315
10316 } else {
10317 /*
10318 * Do a test unit ready to clear any unit attention from non-cd
10319 * devices.
10320 */
10321 mutex_exit(SD_MUTEX(un));
10322
10323 status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
10324 if (status != 0) {
10325 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10326 }
10327
10328 mutex_enter(SD_MUTEX(un));
10329 }
10330
10331
10332 /*
10333 * If this is a non 512 block device, allocate space for
10334 * the wmap cache. This is being done here since every time
10335 * a media is changed this routine will be called and the
10336 * block size is a function of media rather than device.
10337 */
10338 if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10339 un->un_f_non_devbsize_supported) &&
10340 un->un_tgt_blocksize != DEV_BSIZE) ||
10341 un->un_f_enable_rmw) {
10342 if (!(un->un_wm_cache)) {
10343 (void) snprintf(name_str, sizeof (name_str),
10509 * uio - structure pointer describing where data is to be stored
10510 * in user's space
10511 * cred_p - user credential pointer
10512 *
10513 * Return Code: ENXIO
10514 * EIO
10515 * EINVAL
10516 * value returned by physio
10517 *
10518 * Context: Kernel thread context.
10519 */
10520 /* ARGSUSED */
10521 static int
10522 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10523 {
10524 struct sd_lun *un = NULL;
10525 int secmask;
10526 int err = 0;
10527 sd_ssc_t *ssc;
10528
10529 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10530 DEVI_IS_GONE(SD_DEVINFO(un)))
10531 return (ENXIO);
10532
10533 ASSERT(!mutex_owned(SD_MUTEX(un)));
10534
10535 mutex_enter(SD_MUTEX(un));
10536 while (un->un_state == SD_STATE_ATTACHING)
10537 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10538
10539 if (un->un_state == SD_STATE_ATTACH_FAILED) {
10540 mutex_exit(SD_MUTEX(un));
10541 SD_ERROR(SD_LOG_READ_WRITE, un, "sdread: attach failed\n");
10542 return (EIO);
10543 }
10544 mutex_exit(SD_MUTEX(un));
10545
10546 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10547 mutex_enter(SD_MUTEX(un));
10548 /*
10549 * Because the call to sd_ready_and_valid will issue I/O we
10550 * must wait here if either the device is suspended or
10551 * if it's power level is changing.
10552 */
10553 while ((un->un_state == SD_STATE_SUSPENDED) ||
10554 (un->un_state == SD_STATE_PM_CHANGING)) {
10555 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10556 }
10557
10558 SD_BAIL_CHECK(un);
10559 un->un_ncmds_in_driver++;
10560 mutex_exit(SD_MUTEX(un));
10561
10562 /* Initialize sd_ssc_t for internal uscsi commands */
10563 ssc = sd_ssc_init(un);
10564 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10565 err = EIO;
10566 } else {
10567 err = 0;
10568 }
10569 sd_ssc_fini(ssc);
10570
10571 mutex_enter(SD_MUTEX(un));
10572 un->un_ncmds_in_driver--;
10573 if (un->un_f_detach_waiting)
10574 cv_signal(&un->un_detach_cv);
10575 ASSERT(un->un_ncmds_in_driver >= 0);
10576 mutex_exit(SD_MUTEX(un));
10577 if (err != 0)
10578 return (err);
10579 }
10580
10581 /*
10582 * Read requests are restricted to multiples of the system block size.
10583 */
10584 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10585 !un->un_f_enable_rmw)
10586 secmask = un->un_tgt_blocksize - 1;
10587 else
10588 secmask = DEV_BSIZE - 1;
10589
10590 if (uio->uio_loffset & ((offset_t)(secmask))) {
10591 SD_ERROR(SD_LOG_READ_WRITE, un,
10592 "sdread: file offset not modulo %d\n",
10593 secmask + 1);
10594 err = EINVAL;
10614 * uio - structure pointer describing where data is stored in
10615 * user's space
10616 * cred_p - user credential pointer
10617 *
10618 * Return Code: ENXIO
10619 * EIO
10620 * EINVAL
10621 * value returned by physio
10622 *
10623 * Context: Kernel thread context.
10624 */
10625 /* ARGSUSED */
10626 static int
10627 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10628 {
10629 struct sd_lun *un = NULL;
10630 int secmask;
10631 int err = 0;
10632 sd_ssc_t *ssc;
10633
10634 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10635 DEVI_IS_GONE(SD_DEVINFO(un)))
10636 return (ENXIO);
10637
10638 ASSERT(!mutex_owned(SD_MUTEX(un)));
10639
10640 mutex_enter(SD_MUTEX(un));
10641 while (un->un_state == SD_STATE_ATTACHING)
10642 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10643
10644 if (un->un_state == SD_STATE_ATTACH_FAILED) {
10645 mutex_exit(SD_MUTEX(un));
10646 SD_ERROR(SD_LOG_READ_WRITE, un, "sdwrite: attach failed\n");
10647 return (EIO);
10648 }
10649 mutex_exit(SD_MUTEX(un));
10650
10651 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10652 mutex_enter(SD_MUTEX(un));
10653 /*
10654 * Because the call to sd_ready_and_valid will issue I/O we
10655 * must wait here if either the device is suspended or
10656 * if it's power level is changing.
10657 */
10658 while ((un->un_state == SD_STATE_SUSPENDED) ||
10659 (un->un_state == SD_STATE_PM_CHANGING)) {
10660 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10661 }
10662
10663 SD_BAIL_CHECK(un);
10664 un->un_ncmds_in_driver++;
10665 mutex_exit(SD_MUTEX(un));
10666
10667 /* Initialize sd_ssc_t for internal uscsi commands */
10668 ssc = sd_ssc_init(un);
10669 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10670 err = EIO;
10671 } else {
10672 err = 0;
10673 }
10674 sd_ssc_fini(ssc);
10675
10676 mutex_enter(SD_MUTEX(un));
10677 un->un_ncmds_in_driver--;
10678 ASSERT(un->un_ncmds_in_driver >= 0);
10679 if (un->un_f_detach_waiting)
10680 cv_signal(&un->un_detach_cv);
10681 mutex_exit(SD_MUTEX(un));
10682 if (err != 0)
10683 return (err);
10684 }
10685
10686 /*
10687 * Write requests are restricted to multiples of the system block size.
10688 */
10689 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10690 !un->un_f_enable_rmw)
10691 secmask = un->un_tgt_blocksize - 1;
10692 else
10693 secmask = DEV_BSIZE - 1;
10694
10695 if (uio->uio_loffset & ((offset_t)(secmask))) {
10696 SD_ERROR(SD_LOG_READ_WRITE, un,
10697 "sdwrite: file offset not modulo %d\n",
10698 secmask + 1);
10699 err = EINVAL;
10700 } else if (uio->uio_iov->iov_len & (secmask)) {
10719 * aio - structure pointer describing where data is to be stored
10720 * cred_p - user credential pointer
10721 *
10722 * Return Code: ENXIO
10723 * EIO
10724 * EINVAL
10725 * value returned by aphysio
10726 *
10727 * Context: Kernel thread context.
10728 */
10729 /* ARGSUSED */
10730 static int
10731 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10732 {
10733 struct sd_lun *un = NULL;
10734 struct uio *uio = aio->aio_uio;
10735 int secmask;
10736 int err = 0;
10737 sd_ssc_t *ssc;
10738
10739 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10740 DEVI_IS_GONE(SD_DEVINFO(un)))
10741 return (ENXIO);
10742
10743 ASSERT(!mutex_owned(SD_MUTEX(un)));
10744
10745 mutex_enter(SD_MUTEX(un));
10746 while (un->un_state == SD_STATE_ATTACHING)
10747 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10748
10749 if (un->un_state == SD_STATE_ATTACH_FAILED) {
10750 mutex_exit(SD_MUTEX(un));
10751 SD_ERROR(SD_LOG_READ_WRITE, un, "sdaread: attach failed\n");
10752 return (EIO);
10753 }
10754 mutex_exit(SD_MUTEX(un));
10755
10756 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10757 mutex_enter(SD_MUTEX(un));
10758 /*
10759 * Because the call to sd_ready_and_valid will issue I/O we
10760 * must wait here if either the device is suspended or
10761 * if it's power level is changing.
10762 */
10763 while ((un->un_state == SD_STATE_SUSPENDED) ||
10764 (un->un_state == SD_STATE_PM_CHANGING)) {
10765 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10766 }
10767
10768 SD_BAIL_CHECK(un);
10769 un->un_ncmds_in_driver++;
10770 mutex_exit(SD_MUTEX(un));
10771
10772 /* Initialize sd_ssc_t for internal uscsi commands */
10773 ssc = sd_ssc_init(un);
10774 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10775 err = EIO;
10776 } else {
10777 err = 0;
10778 }
10779 sd_ssc_fini(ssc);
10780
10781 mutex_enter(SD_MUTEX(un));
10782 un->un_ncmds_in_driver--;
10783 ASSERT(un->un_ncmds_in_driver >= 0);
10784 if (un->un_f_detach_waiting)
10785 cv_signal(&un->un_detach_cv);
10786 mutex_exit(SD_MUTEX(un));
10787 if (err != 0)
10788 return (err);
10789 }
10790
10791 /*
10792 * Read requests are restricted to multiples of the system block size.
10793 */
10794 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10795 !un->un_f_enable_rmw)
10796 secmask = un->un_tgt_blocksize - 1;
10797 else
10798 secmask = DEV_BSIZE - 1;
10799
10800 if (uio->uio_loffset & ((offset_t)(secmask))) {
10801 SD_ERROR(SD_LOG_READ_WRITE, un,
10802 "sdaread: file offset not modulo %d\n",
10803 secmask + 1);
10804 err = EINVAL;
10805 } else if (uio->uio_iov->iov_len & (secmask)) {
10824 * aio - structure pointer describing where data is stored
10825 * cred_p - user credential pointer
10826 *
10827 * Return Code: ENXIO
10828 * EIO
10829 * EINVAL
10830 * value returned by aphysio
10831 *
10832 * Context: Kernel thread context.
10833 */
10834 /* ARGSUSED */
10835 static int
10836 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10837 {
10838 struct sd_lun *un = NULL;
10839 struct uio *uio = aio->aio_uio;
10840 int secmask;
10841 int err = 0;
10842 sd_ssc_t *ssc;
10843
10844 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
10845 DEVI_IS_GONE(SD_DEVINFO(un)))
10846 return (ENXIO);
10847
10848 ASSERT(!mutex_owned(SD_MUTEX(un)));
10849
10850 mutex_enter(SD_MUTEX(un));
10851 while (un->un_state == SD_STATE_ATTACHING)
10852 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10853
10854 if (un->un_state == SD_STATE_ATTACH_FAILED) {
10855 mutex_exit(SD_MUTEX(un));
10856 SD_ERROR(SD_LOG_READ_WRITE, un,
10857 "sdawrite: attach failed\n");
10858 return (EIO);
10859 }
10860 mutex_exit(SD_MUTEX(un));
10861
10862 if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10863 mutex_enter(SD_MUTEX(un));
10864 /*
10865 * Because the call to sd_ready_and_valid will issue I/O we
10866 * must wait here if either the device is suspended or
10867 * if it's power level is changing.
10868 */
10869 while ((un->un_state == SD_STATE_SUSPENDED) ||
10870 (un->un_state == SD_STATE_PM_CHANGING)) {
10871 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10872 }
10873
10874 SD_BAIL_CHECK(un);
10875 un->un_ncmds_in_driver++;
10876 mutex_exit(SD_MUTEX(un));
10877
10878 /* Initialize sd_ssc_t for internal uscsi commands */
10879 ssc = sd_ssc_init(un);
10880 if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10881 err = EIO;
10882 } else {
10883 err = 0;
10884 }
10885 sd_ssc_fini(ssc);
10886
10887 mutex_enter(SD_MUTEX(un));
10888 un->un_ncmds_in_driver--;
10889 ASSERT(un->un_ncmds_in_driver >= 0);
10890 if (un->un_f_detach_waiting)
10891 cv_signal(&un->un_detach_cv);
10892 mutex_exit(SD_MUTEX(un));
10893 if (err != 0)
10894 return (err);
10895 }
10896
10897 /*
10898 * Write requests are restricted to multiples of the system block size.
10899 */
10900 if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10901 !un->un_f_enable_rmw)
10902 secmask = un->un_tgt_blocksize - 1;
10903 else
10904 secmask = DEV_BSIZE - 1;
10905
10906 if (uio->uio_loffset & ((offset_t)(secmask))) {
10907 SD_ERROR(SD_LOG_READ_WRITE, un,
10908 "sdawrite: file offset not modulo %d\n",
10909 secmask + 1);
10910 err = EINVAL;
10911 } else if (uio->uio_iov->iov_len & (secmask)) {
11001 * - The taskq mechanism may be used by the iodone side functions to dispatch
11002 * requests to the iostart side functions. The iostart side functions in
11003 * this case would be called under the context of a taskq thread, so it's
11004 * OK for them to block/sleep/spin in this case.
11005 *
11006 * - iostart side functions may allocate "shadow" buf(9S) structs and
11007 * pass them along to the next function in the chain. The corresponding
11008 * iodone side functions must coalesce the "shadow" bufs and return
11009 * the "original" buf to the next higher layer.
11010 *
11011 * - The b_private field of the buf(9S) struct holds a pointer to
11012 * an sd_xbuf struct, which contains information needed to
11013 * construct the scsi_pkt for the command.
11014 *
11015 * - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11016 * layer must acquire & release the SD_MUTEX(un) as needed.
11017 */
11018
11019
11020 /*
11021 * Function: sd_taskq_create
11022 *
11023 * Description: Create taskq thread(s) and preallocate task entries
11024 *
11025 * Return Code: Returns a pointer to the allocated taskq_t.
11026 *
11027 * Context: Can sleep. Requires blockable context.
11028 *
11029 * Notes: - The taskq() facility currently is NOT part of the DDI.
11030 * (definitely NOT recommeded for 3rd-party drivers!) :-)
11031 * - taskq_create() will block for memory, also it will panic
11032 * if it cannot create the requested number of threads.
11033 * - Currently taskq_create() creates threads that cannot be
11034 * swapped.
11035 * - We use TASKQ_PREPOPULATE to ensure we have an adequate
11036 * supply of taskq entries at interrupt time (ie, so that we
11037 * do not have to sleep for memory)
11038 */
11039
11040 static void
11078 sd_wmr_tq = NULL;
11079 }
11080
11081
11082 /*
11083 * Function: sdstrategy
11084 *
11085 * Description: Driver's strategy (9E) entry point function.
11086 *
11087 * Arguments: bp - pointer to buf(9S)
11088 *
11089 * Return Code: Always returns zero
11090 *
11091 * Context: Kernel thread context.
11092 */
11093
11094 static int
11095 sdstrategy(struct buf *bp)
11096 {
11097 struct sd_lun *un;
11098 int error = EIO;
11099
11100 if ((un = ddi_get_soft_state(sd_state,
11101 SD_GET_INSTANCE_FROM_BUF(bp))) == NULL)
11102 goto fail;
11103
11104 /* Fail new cmds if state is dumping or device is gone */
11105 if (un->un_state == SD_STATE_DUMPING ||
11106 DEVI_IS_GONE(SD_DEVINFO(un))) {
11107 error = ENXIO;
11108 goto fail;
11109 }
11110
11111 ASSERT(!mutex_owned(SD_MUTEX(un)));
11112
11113 /*
11114 * Commands may sneak in while we released the mutex in
11115 * DDI_SUSPEND, we should block new commands. However, old
11116 * commands that are still in the driver at this point should
11117 * still be allowed to drain.
11118 */
11119 mutex_enter(SD_MUTEX(un));
11120 /*
11121 * Must wait here if either the device is suspended or
11122 * if it's power level is changing.
11123 */
11124 while ((un->un_state == SD_STATE_SUSPENDED) ||
11125 (un->un_state == SD_STATE_PM_CHANGING) ||
11126 (un->un_state == SD_STATE_ATTACHING)) {
11127 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11128 }
11129
11130 if (un->un_state == SD_STATE_ATTACH_FAILED) {
11131 mutex_exit(SD_MUTEX(un));
11132 SD_ERROR(SD_LOG_READ_WRITE, un,
11133 "sdstrategy: attach failed\n");
11134 goto fail;
11135 }
11136 if (un->un_detach_count != 0) {
11137 mutex_exit(SD_MUTEX(un));
11138 goto fail;
11139 }
11140
11141 un->un_ncmds_in_driver++;
11142
11143 /*
11144 * atapi: Since we are running the CD for now in PIO mode we need to
11145 * call bp_mapin here to avoid bp_mapin called interrupt context under
11146 * the HBA's init_pkt routine.
11147 */
11148 if (un->un_f_cfg_is_atapi == TRUE) {
11149 mutex_exit(SD_MUTEX(un));
11150 bp_mapin(bp);
11151 mutex_enter(SD_MUTEX(un));
11152 }
11153 SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11154 un->un_ncmds_in_driver);
11155
11156 if (bp->b_flags & B_WRITE)
11157 un->un_f_sync_cache_required = TRUE;
11158
11159 mutex_exit(SD_MUTEX(un));
11160
11161 /*
11162 * This will (eventually) allocate the sd_xbuf area and
11163 * call sd_xbuf_strategy(). We just want to return the
11164 * result of ddi_xbuf_qstrategy so that we have an opt-
11165 * imized tail call which saves us a stack frame.
11166 */
11167 return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11168
11169 fail:
11170 bioerror(bp, error);
11171 bp->b_resid = bp->b_bcount;
11172 biodone(bp);
11173 return (0);
11174 }
11175
11176 /*
11177 * Function: sd_xbuf_strategy
11178 *
11179 * Description: Function for initiating IO operations via the
11180 * ddi_xbuf_qstrategy() mechanism.
11181 *
11182 * Context: Kernel thread context.
11183 */
11184
11185 static void
11186 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11187 {
11188 struct sd_lun *un = arg;
11189
11190 ASSERT(bp != NULL);
11191 ASSERT(xp != NULL);
11192 ASSERT(un != NULL);
11193 ASSERT(!mutex_owned(SD_MUTEX(un)));
11194
11195 /*
11338 *
11339 * Arguments: bp - buf struct ptr
11340 *
11341 * Return Code: Always returns 0
11342 *
11343 * Context: Kernel thread context
11344 */
11345
11346 static int
11347 sd_uscsi_strategy(struct buf *bp)
11348 {
11349 struct sd_lun *un;
11350 struct sd_uscsi_info *uip;
11351 struct sd_xbuf *xp;
11352 uchar_t chain_type;
11353 uchar_t cmd;
11354
11355 ASSERT(bp != NULL);
11356
11357 un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11358 if (un == NULL || DEVI_IS_GONE(SD_DEVINFO(un))) {
11359 bioerror(bp, EIO);
11360 bp->b_resid = bp->b_bcount;
11361 biodone(bp);
11362 return (0);
11363 }
11364
11365 ASSERT(!mutex_owned(SD_MUTEX(un)));
11366
11367 SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11368
11369 /*
11370 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11371 */
11372 ASSERT(bp->b_private != NULL);
11373 uip = (struct sd_uscsi_info *)bp->b_private;
11374 cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11375
11376 mutex_enter(SD_MUTEX(un));
11377 /*
11378 * atapi: Since we are running the CD for now in PIO mode we need to
11379 * call bp_mapin here to avoid bp_mapin called interrupt context under
11380 * the HBA's init_pkt routine.
11381 */
11382 if (un->un_f_cfg_is_atapi == TRUE) {
11383 mutex_exit(SD_MUTEX(un));
11384 bp_mapin(bp);
11385 mutex_enter(SD_MUTEX(un));
11386 }
11387 un->un_ncmds_in_driver++;
11388 SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11389 un->un_ncmds_in_driver);
11390
11391 if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11392 (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11393 un->un_f_sync_cache_required = TRUE;
11394
11395 if (sd_failfast_enable & SD_FAILFAST_ENABLE_FAIL_USCSI) {
11396 /*
11397 * If there are outstanding commands, treat all
11398 * USCSI commands as if they have B_FAILFAST set.
11399 */
11400 if (un->un_ncmds_in_driver != 1)
11401 bp->b_flags |= B_FAILFAST;
11402 }
11403
11404 mutex_exit(SD_MUTEX(un));
11405
11406 switch (uip->ui_flags) {
11407 case SD_PATH_DIRECT:
11408 chain_type = SD_CHAIN_DIRECT;
11409 break;
11410 case SD_PATH_DIRECT_PRIORITY:
11411 chain_type = SD_CHAIN_DIRECT_PRIORITY;
11412 break;
11413 default:
11414 chain_type = SD_CHAIN_USCSI;
11415 break;
11416 }
11417
11418 /*
11419 * We may allocate extra buf for external USCSI commands. If the
11420 * application asks for bigger than 20-byte sense data via USCSI,
11421 * SCSA layer will allocate 252 bytes sense buf for that command.
11422 */
11423 if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11458 * ENXIO - soft state not found for specified dev
11459 * EINVAL
11460 * EFAULT - copyin/copyout error
11461 * return code of scsi_uscsi_handle_command():
11462 * EIO
11463 * ENXIO
11464 * EACCES
11465 *
11466 * Context: Waits for command to complete. Can sleep.
11467 */
11468
11469 static int
11470 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11471 enum uio_seg dataspace, int path_flag)
11472 {
11473 struct sd_lun *un;
11474 sd_ssc_t *ssc;
11475 int rval;
11476
11477 un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11478 if (un == NULL || DEVI_IS_GONE(SD_DEVINFO(un)))
11479 return (ENXIO);
11480
11481 /*
11482 * Using sd_ssc_send to handle uscsi cmd
11483 */
11484 ssc = sd_ssc_init(un);
11485 rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11486 sd_ssc_fini(ssc);
11487
11488 return (rval);
11489 }
11490
11491 /*
11492 * Function: sd_ssc_init
11493 *
11494 * Description: Uscsi end-user call this function to initialize necessary
11495 * fields, such as uscsi_cmd and sd_uscsi_info struct.
11496 *
11497 * The return value of sd_send_scsi_cmd will be treated as a
11498 * fault in various conditions. Even it is not Zero, some
11499 * callers may ignore the return value. That is to say, we can
11626 * ENXIO - soft state not found for specified dev
11627 * ECANCELED - command cancelled due to low power
11628 * EINVAL
11629 * EFAULT - copyin/copyout error
11630 * return code of scsi_uscsi_handle_command():
11631 * EIO
11632 * ENXIO
11633 * EACCES
11634 *
11635 * Context: Kernel Thread;
11636 * Waits for command to complete. Can sleep.
11637 */
11638 static int
11639 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11640 enum uio_seg dataspace, int path_flag)
11641 {
11642 struct sd_uscsi_info *uip;
11643 struct uscsi_cmd *uscmd;
11644 struct sd_lun *un;
11645 dev_t dev;
11646 dev_info_t *dip = SD_DEVINFO(ssc->ssc_un);
11647
11648 int format = 0;
11649 int rval;
11650
11651 ASSERT(ssc != NULL);
11652 un = ssc->ssc_un;
11653 ASSERT(un != NULL);
11654 uscmd = ssc->ssc_uscsi_cmd;
11655 ASSERT(uscmd != NULL);
11656 ASSERT(!mutex_owned(SD_MUTEX(un)));
11657 if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11658 /*
11659 * If enter here, it indicates that the previous uscsi
11660 * command has not been processed by sd_ssc_assessment.
11661 * This is violating our rules of FMA telemetry processing.
11662 * We should print out this message and the last undisposed
11663 * uscsi command.
11664 */
11665 if (uscmd->uscsi_cdb != NULL) {
11666 SD_INFO(SD_LOG_SDTEST, un,
11670 }
11671 /*
11672 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11673 * the initial status.
11674 */
11675 ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11676 }
11677
11678 /*
11679 * We need to make sure sd_ssc_send will have sd_ssc_assessment
11680 * followed to avoid missing FMA telemetries.
11681 */
11682 ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11683
11684 /*
11685 * if USCSI_PMFAILFAST is set and un is in low power, fail the
11686 * command immediately.
11687 */
11688 mutex_enter(SD_MUTEX(un));
11689 mutex_enter(&un->un_pm_mutex);
11690
11691 if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11692 SD_DEVICE_IS_IN_LOW_POWER(un)) {
11693 SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11694 "un:0x%p is in low power\n", un);
11695 mutex_exit(&un->un_pm_mutex);
11696 mutex_exit(SD_MUTEX(un));
11697 return (ECANCELED);
11698 }
11699 mutex_exit(&un->un_pm_mutex);
11700 mutex_exit(SD_MUTEX(un));
11701
11702 #ifdef SDDEBUG
11703 switch (dataspace) {
11704 case UIO_USERSPACE:
11705 SD_TRACE(SD_LOG_IO, un,
11706 "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
11707 break;
11708 case UIO_SYSSPACE:
11709 SD_TRACE(SD_LOG_IO, un,
11710 "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
11739 * since we allocate the buf here in this function, we do not
11740 * need to preserve the prior contents of b_private.
11741 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11742 */
11743 uip = ssc->ssc_uscsi_info;
11744 uip->ui_flags = path_flag;
11745 uip->ui_cmdp = uscmd;
11746
11747 /*
11748 * Commands sent with priority are intended for error recovery
11749 * situations, and do not have retries performed.
11750 */
11751 if (path_flag == SD_PATH_DIRECT_PRIORITY) {
11752 uscmd->uscsi_flags |= USCSI_DIAGNOSE;
11753 }
11754 uscmd->uscsi_flags &= ~USCSI_NOINTR;
11755
11756 dev = SD_GET_DEV(un);
11757 rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
11758 sd_uscsi_strategy, NULL, uip);
11759 if (DEVI_IS_GONE(dip)) {
11760 cmn_err(CE_WARN, "%s-%d: device is gone!", __func__, __LINE__);
11761 return (ENXIO);
11762 }
11763
11764 /*
11765 * mark ssc_flags right after handle_cmd to make sure
11766 * the uscsi has been sent
11767 */
11768 ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
11769
11770 #ifdef SDDEBUG
11771 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
11772 "uscsi_status: 0x%02x uscsi_resid:0x%x\n",
11773 uscmd->uscsi_status, uscmd->uscsi_resid);
11774 if (uscmd->uscsi_bufaddr != NULL) {
11775 SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
11776 "uscmd->uscsi_bufaddr: 0x%p uscmd->uscsi_buflen:%d\n",
11777 uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
11778 if (dataspace == UIO_SYSSPACE) {
11779 SD_DUMP_MEMORY(un, SD_LOG_IO,
11780 "data", (uchar_t *)uscmd->uscsi_bufaddr,
11781 uscmd->uscsi_buflen, SD_LOG_HEX);
11782 }
12111 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12112
12113 xp = SD_GET_XBUF(bp);
12114 ASSERT(xp != NULL);
12115
12116 /* xbuf is gone after this */
12117 if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12118 mutex_enter(SD_MUTEX(un));
12119
12120 /*
12121 * Grab time when the cmd completed.
12122 * This is used for determining if the system has been
12123 * idle long enough to make it idle to the PM framework.
12124 * This is for lowering the overhead, and therefore improving
12125 * performance per I/O operation.
12126 */
12127 un->un_pm_idle_time = gethrtime();
12128
12129 un->un_ncmds_in_driver--;
12130 ASSERT(un->un_ncmds_in_driver >= 0);
12131 if (un->un_f_detach_waiting)
12132 cv_signal(&un->un_detach_cv);
12133 SD_INFO(SD_LOG_IO, un,
12134 "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12135 un->un_ncmds_in_driver);
12136
12137 mutex_exit(SD_MUTEX(un));
12138 }
12139
12140 biodone(bp); /* bp is gone after this */
12141
12142 SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12143 }
12144
12145
12146 /*
12147 * Function: sd_uscsi_iodone
12148 *
12149 * Description: Frees the sd_xbuf & returns the buf to its originator.
12150 *
12151 * Context: May be called from interrupt context.
12152 */
12163 ASSERT(xp != NULL);
12164 ASSERT(!mutex_owned(SD_MUTEX(un)));
12165
12166 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12167
12168 bp->b_private = xp->xb_private;
12169
12170 mutex_enter(SD_MUTEX(un));
12171
12172 /*
12173 * Grab time when the cmd completed.
12174 * This is used for determining if the system has been
12175 * idle long enough to make it idle to the PM framework.
12176 * This is for lowering the overhead, and therefore improving
12177 * performance per I/O operation.
12178 */
12179 un->un_pm_idle_time = gethrtime();
12180
12181 un->un_ncmds_in_driver--;
12182 ASSERT(un->un_ncmds_in_driver >= 0);
12183 if (un->un_f_detach_waiting)
12184 cv_signal(&un->un_detach_cv);
12185 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12186 un->un_ncmds_in_driver);
12187
12188 mutex_exit(SD_MUTEX(un));
12189
12190 if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12191 SENSE_LENGTH) {
12192 kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12193 MAX_SENSE_LENGTH);
12194 } else {
12195 kmem_free(xp, sizeof (struct sd_xbuf));
12196 }
12197
12198 biodone(bp);
12199
12200 SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12201 }
12202
12203
12204 /*
12562 * For a non-writable CD, a write request is an error
12563 */
12564 if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12565 (un->un_f_mmc_writable_media == FALSE)) {
12566 bioerror(bp, EIO);
12567 bp->b_resid = bp->b_bcount;
12568 SD_BEGIN_IODONE(index, un, bp);
12569 return;
12570 }
12571
12572 /*
12573 * We do not need a shadow buf if the device is using
12574 * un->un_sys_blocksize as its block size or if bcount == 0.
12575 * In this case there is no layer-private data block allocated.
12576 */
12577 if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12578 (bp->b_bcount == 0)) {
12579 goto done;
12580 }
12581
12582 /* We do not support non-block-aligned transfers for ROD devices */
12583 ASSERT(!ISROD(un));
12584
12585 xp = SD_GET_XBUF(bp);
12586 ASSERT(xp != NULL);
12587
12588 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12589 "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12590 un->un_tgt_blocksize, DEV_BSIZE);
12591 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12592 "request start block:0x%x\n", xp->xb_blkno);
12593 SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12594 "request len:0x%x\n", bp->b_bcount);
12595
12596 /*
12597 * Allocate the layer-private data area for the mapblocksize layer.
12598 * Layers are allowed to use the xp_private member of the sd_xbuf
12599 * struct to store the pointer to their layer-private data block, but
12600 * each layer also has the responsibility of restoring the prior
12601 * contents of xb_private before returning the buf/xbuf to the
12602 * higher layer that sent it.
12603 *
13262 * The xfer time difference of 6 vs 10 byte CDBs is
13263 * still significant so this code is still worthwhile.
13264 * 10 byte CDBs are very inefficient with the fas HBA driver
13265 * and older disks. Each CDB byte took 1 usec with some
13266 * popular disks.
13267 *
13268 * Context: Must be called at attach time
13269 */
13270
13271 static void
13272 sd_init_cdb_limits(struct sd_lun *un)
13273 {
13274 int hba_cdb_limit;
13275
13276 /*
13277 * Use CDB_GROUP1 commands for most devices except for
13278 * parallel SCSI fixed drives in which case we get better
13279 * performance using CDB_GROUP0 commands (where applicable).
13280 */
13281 un->un_mincdb = SD_CDB_GROUP1;
13282 if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13283 !un->un_f_has_removable_media) {
13284 un->un_mincdb = SD_CDB_GROUP0;
13285 }
13286
13287 /*
13288 * Try to read the max-cdb-length supported by HBA.
13289 */
13290 un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13291 if (0 >= un->un_max_hba_cdb) {
13292 un->un_max_hba_cdb = CDB_GROUP4;
13293 hba_cdb_limit = SD_CDB_GROUP4;
13294 } else if (0 < un->un_max_hba_cdb &&
13295 un->un_max_hba_cdb < CDB_GROUP1) {
13296 hba_cdb_limit = SD_CDB_GROUP0;
13297 } else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13298 un->un_max_hba_cdb < CDB_GROUP5) {
13299 hba_cdb_limit = SD_CDB_GROUP1;
13300 } else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13301 un->un_max_hba_cdb < CDB_GROUP4) {
13302 hba_cdb_limit = SD_CDB_GROUP5;
13303 } else {
13304 hba_cdb_limit = SD_CDB_GROUP4;
13305 }
13306
13307 /*
13308 * Use CDB_GROUP5 commands for removable devices. Use CDB_GROUP4
13309 * commands for fixed disks unless we are building for a 32 bit
13310 * kernel.
13311 */
13312 #ifdef _LP64
13313 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13314 min(hba_cdb_limit, SD_CDB_GROUP4);
13315 #else
13316 un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13317 min(hba_cdb_limit, SD_CDB_GROUP1);
13318 #endif
13319
13320 un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13321 ? sizeof (struct scsi_arq_status) : 1);
13322 if (!ISCD(un))
13323 un->un_cmd_timeout = (ushort_t)un->un_io_time;
13324 un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13325 }
13326
13327
13328 /*
13329 * Function: sd_initpkt_for_buf
13330 *
13331 * Description: Allocate and initialize for transport a scsi_pkt struct,
13332 * based upon the info specified in the given buf struct.
13333 *
13334 * Assumes the xb_blkno in the request is absolute (ie,
13335 * relative to the start of the device (NOT partition!).
13336 * Also assumes that the request is using the native block
13337 * size of the device (as returned by the READ CAPACITY
13338 * command).
13339 *
13340 * Return Code: SD_PKT_ALLOC_SUCCESS
13341 * SD_PKT_ALLOC_FAILURE
13342 * SD_PKT_ALLOC_FAILURE_NO_DMA
13343 * SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13355 struct sd_lun *un;
13356 size_t blockcount;
13357 daddr_t startblock;
13358 int rval;
13359 int cmd_flags;
13360
13361 ASSERT(bp != NULL);
13362 ASSERT(pktpp != NULL);
13363 xp = SD_GET_XBUF(bp);
13364 ASSERT(xp != NULL);
13365 un = SD_GET_UN(bp);
13366 ASSERT(un != NULL);
13367 ASSERT(mutex_owned(SD_MUTEX(un)));
13368 ASSERT(bp->b_resid == 0);
13369
13370 SD_TRACE(SD_LOG_IO_CORE, un,
13371 "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13372
13373 mutex_exit(SD_MUTEX(un));
13374
13375 if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13376 /*
13377 * Already have a scsi_pkt -- just need DMA resources.
13378 * We must recompute the CDB in case the mapping returns
13379 * a nonzero pkt_resid.
13380 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13381 * that is being retried, the unmap/remap of the DMA resouces
13382 * will result in the entire transfer starting over again
13383 * from the very first block.
13384 */
13385 ASSERT(xp->xb_pktp != NULL);
13386 pktp = xp->xb_pktp;
13387 } else {
13388 pktp = NULL;
13389 }
13390
13391 startblock = xp->xb_blkno; /* Absolute block num. */
13392 blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13393
13394 cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13395
13396 /*
13397 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13398 * call scsi_init_pkt, and build the CDB.
13399 */
13400 rval = sd_setup_rw_pkt(un, &pktp, bp,
13401 cmd_flags, sdrunout, (caddr_t)un,
13402 startblock, blockcount);
13403
13404 if (rval == 0) {
13405 /*
13406 * Success.
13407 *
13408 * If partial DMA is being used and required for this transfer.
13409 * set it up here.
13417 */
13418 xp->xb_dma_resid = pktp->pkt_resid;
13419
13420 /* rezero resid */
13421 pktp->pkt_resid = 0;
13422
13423 } else {
13424 xp->xb_dma_resid = 0;
13425 }
13426
13427 pktp->pkt_flags = un->un_tagflags;
13428 pktp->pkt_time = un->un_cmd_timeout;
13429 pktp->pkt_comp = sdintr;
13430
13431 pktp->pkt_private = bp;
13432 *pktpp = pktp;
13433
13434 SD_TRACE(SD_LOG_IO_CORE, un,
13435 "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13436
13437 xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13438
13439 mutex_enter(SD_MUTEX(un));
13440 return (SD_PKT_ALLOC_SUCCESS);
13441
13442 }
13443
13444 /*
13445 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13446 * from sd_setup_rw_pkt.
13447 */
13448 ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13449
13450 if (rval == SD_PKT_ALLOC_FAILURE) {
13451 *pktpp = NULL;
13452 /*
13453 * Set the driver state to RWAIT to indicate the driver
13454 * is waiting on resource allocations. The driver will not
13455 * suspend, pm_suspend, or detatch while the state is RWAIT.
13456 */
13457 mutex_enter(SD_MUTEX(un));
14081 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14082 daddr_t blkno, int (*func)(struct buf *))
14083 {
14084 struct sd_lun *un;
14085 struct sd_xbuf *xp;
14086 struct sd_xbuf *new_xp;
14087 struct buf *new_bp;
14088
14089 ASSERT(bp != NULL);
14090 xp = SD_GET_XBUF(bp);
14091 ASSERT(xp != NULL);
14092 un = SD_GET_UN(bp);
14093 ASSERT(un != NULL);
14094 ASSERT(!mutex_owned(SD_MUTEX(un)));
14095
14096 if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14097 bp_mapin(bp);
14098 }
14099
14100 bflags &= (B_READ | B_WRITE);
14101 new_bp = getrbuf(KM_SLEEP);
14102 new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14103 new_bp->b_bcount = datalen;
14104 new_bp->b_flags = bflags |
14105 (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14106 new_bp->av_forw = NULL;
14107 new_bp->av_back = NULL;
14108 new_bp->b_dev = bp->b_dev;
14109 new_bp->b_blkno = blkno;
14110 new_bp->b_iodone = func;
14111 new_bp->b_edev = bp->b_edev;
14112 new_bp->b_resid = 0;
14113
14114 /* We need to preserve the B_FAILFAST flag */
14115 if (bp->b_flags & B_FAILFAST) {
14116 new_bp->b_flags |= B_FAILFAST;
14117 }
14118
14119 /*
14120 * Allocate an xbuf for the shadow bp and copy the contents of the
14121 * original xbuf into it.
14122 */
14123 new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14124 bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14125
14174 kmem_free(xp, sizeof (struct sd_xbuf));
14175 }
14176
14177 /*
14178 * Function: sd_shadow_buf_free
14179 *
14180 * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14181 *
14182 * Context: May be called under interrupt context
14183 */
14184
14185 static void
14186 sd_shadow_buf_free(struct buf *bp)
14187 {
14188 struct sd_xbuf *xp;
14189
14190 ASSERT(bp != NULL);
14191 xp = SD_GET_XBUF(bp);
14192 ASSERT(xp != NULL);
14193
14194 /*
14195 * Null out b_iodone before freeing the bp, to ensure that the driver
14196 * never gets confused by a stale value in this field. (Just a little
14197 * extra defensiveness here.)
14198 */
14199 bp->b_iodone = NULL;
14200
14201 kmem_free(bp->b_un.b_addr, bp->b_bcount);
14202 freerbuf(bp);
14203
14204 kmem_free(xp, sizeof (struct sd_xbuf));
14205 }
14206
14207
14208 /*
14209 * Function: sd_print_transport_rejected_message
14210 *
14211 * Description: This implements the ludicrously complex rules for printing
14212 * a "transport rejected" message. This is to address the
14213 * specific problem of having a flood of this error message
14214 * produced when a failover occurs.
14215 *
14216 * Context: Any.
14217 */
14218
14219 static void
14220 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14221 int code)
14222 {
14408 * Description: Remove and transport cmds from the driver queues.
14409 *
14410 * Arguments: un - pointer to the unit (soft state) struct for the target.
14411 *
14412 * immed_bp - ptr to a buf to be transported immediately. Only
14413 * the immed_bp is transported; bufs on the waitq are not
14414 * processed and the un_retry_bp is not checked. If immed_bp is
14415 * NULL, then normal queue processing is performed.
14416 *
14417 * Context: May be called from kernel thread context, interrupt context,
14418 * or runout callback context. This function may not block or
14419 * call routines that block.
14420 */
14421
14422 static void
14423 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14424 {
14425 struct sd_xbuf *xp;
14426 struct buf *bp;
14427 void (*statp)(kstat_io_t *);
14428 void (*saved_statp)(kstat_io_t *);
14429 int rval;
14430 struct sd_fm_internal *sfip = NULL;
14431
14432 ASSERT(un != NULL);
14433 ASSERT(mutex_owned(SD_MUTEX(un)));
14434 ASSERT(un->un_ncmds_in_transport >= 0);
14435 ASSERT(un->un_throttle >= 0);
14436
14437 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14438
14439 /*
14440 * If device is currently retired, we should abort all pending I/O.
14441 */
14442 if (DEVI(un->un_sd->sd_dev)->devi_flags & DEVI_RETIRED) {
14443 if (immed_bp) {
14444 immed_bp->b_resid = immed_bp->b_bcount;
14445 bioerror(immed_bp, ENXIO);
14446 biodone(immed_bp);
14447 }
14448 /* abort in-flight IO */
14449 (void) scsi_abort(SD_ADDRESS(un), NULL);
14450 /* abort pending IO */
14451 un->un_failfast_state = SD_FAILFAST_ACTIVE;
14452 un->un_failfast_bp = NULL;
14453 sd_failfast_flushq(un, B_TRUE);
14454 return;
14455 }
14456
14457 do {
14458 saved_statp = NULL;
14459
14460 /*
14461 * If we are syncing or dumping, fail the command to
14462 * avoid recursively calling back into scsi_transport().
14463 * The dump I/O itself uses a separate code path so this
14464 * only prevents non-dump I/O from being sent while dumping.
14465 * File system sync takes place before dumping begins.
14466 * During panic, filesystem I/O is allowed provided
14467 * un_in_callback is <= 1. This is to prevent recursion
14468 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14469 * sd_start_cmds and so on. See panic.c for more information
14470 * about the states the system can be in during panic.
14471 */
14472 if ((un->un_state == SD_STATE_DUMPING) ||
14473 (ddi_in_panic() && (un->un_in_callback > 1))) {
14474 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14475 "sd_start_cmds: panicking\n");
14476 goto exit;
14477 }
14478
14495 statp = kstat_runq_enter;
14496 if (bp == un->un_retry_bp) {
14497 ASSERT((un->un_retry_statp == NULL) ||
14498 (un->un_retry_statp == kstat_waitq_enter) ||
14499 (un->un_retry_statp ==
14500 kstat_runq_back_to_waitq));
14501 /*
14502 * If the waitq kstat was incremented when
14503 * sd_set_retry_bp() queued this bp for a retry,
14504 * then we must set up statp so that the waitq
14505 * count will get decremented correctly below.
14506 * Also we must clear un->un_retry_statp to
14507 * ensure that we do not act on a stale value
14508 * in this field.
14509 */
14510 if ((un->un_retry_statp == kstat_waitq_enter) ||
14511 (un->un_retry_statp ==
14512 kstat_runq_back_to_waitq)) {
14513 statp = kstat_waitq_to_runq;
14514 }
14515 saved_statp = un->un_retry_statp;
14516 un->un_retry_statp = NULL;
14517
14518 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14519 "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14520 "un_throttle:%d un_ncmds_in_transport:%d\n",
14521 un, un->un_retry_bp, un->un_throttle,
14522 un->un_ncmds_in_transport);
14523 } else {
14524 SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14525 "processing priority bp:0x%p\n", bp);
14526 }
14527
14528 } else if ((bp = un->un_waitq_headp) != NULL) {
14529 /*
14530 * A command on the waitq is ready to go, but do not
14531 * send it if:
14532 *
14533 * (1) the throttle limit has been reached, or
14534 * (2) a retry is pending, or
14535 * (3) a START_STOP_UNIT callback pending, or
14572 statp = kstat_waitq_to_runq;
14573 SD_TRACE(SD_LOG_IO_CORE, un,
14574 "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14575
14576 } else {
14577 /* No work to do so bail out now */
14578 SD_TRACE(SD_LOG_IO_CORE, un,
14579 "sd_start_cmds: no more work, exiting!\n");
14580 goto exit;
14581 }
14582
14583 /*
14584 * Reset the state to normal. This is the mechanism by which
14585 * the state transitions from either SD_STATE_RWAIT or
14586 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14587 * If state is SD_STATE_PM_CHANGING then this command is
14588 * part of the device power control and the state must
14589 * not be put back to normal. Doing so would would
14590 * allow new commands to proceed when they shouldn't,
14591 * the device may be going off.
14592 *
14593 * Similarly, if the state is SD_STATE_ATTACHING we should
14594 * not set it to SD_STATE_NORMAL to avoid corruption.
14595 */
14596 if ((un->un_state != SD_STATE_SUSPENDED) &&
14597 (un->un_state != SD_STATE_PM_CHANGING) &&
14598 (un->un_state != SD_STATE_ATTACHING)) {
14599 New_state(un, SD_STATE_NORMAL);
14600 }
14601
14602 xp = SD_GET_XBUF(bp);
14603 ASSERT(xp != NULL);
14604
14605 /*
14606 * Allocate the scsi_pkt if we need one, or attach DMA
14607 * resources if we have a scsi_pkt that needs them. The
14608 * latter should only occur for commands that are being
14609 * retried.
14610 */
14611 if ((xp->xb_pktp == NULL) ||
14612 ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14613 /*
14614 * There is no scsi_pkt allocated for this buf. Call
14615 * the initpkt function to allocate & init one.
14616 *
14617 * The scsi_init_pkt runout callback functionality is
14618 * implemented as follows:
14619 *
14620 * 1) The initpkt function always calls
14621 * scsi_init_pkt(9F) with sdrunout specified as the
14622 * callback routine.
14623 * 2) A successful packet allocation is initialized and
14624 * the I/O is transported.
14625 * 3) The I/O associated with an allocation resource
14626 * failure is left on its queue to be retried via
14627 * runout or the next I/O.
14628 * 4) The I/O associated with a DMA error is removed
14629 * from the queue and failed with EIO. Processing of
14630 * the transport queues is also halted to be
14631 * restarted via runout or the next I/O.
14632 * 5) The I/O associated with a CDB size or packet
14660 case SD_PKT_ALLOC_SUCCESS:
14661 xp->xb_pktp = pktp;
14662 SD_TRACE(SD_LOG_IO_CORE, un,
14663 "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14664 pktp);
14665 goto got_pkt;
14666
14667 case SD_PKT_ALLOC_FAILURE:
14668 /*
14669 * Temporary (hopefully) resource depletion.
14670 * Since retries and RQS commands always have a
14671 * scsi_pkt allocated, these cases should never
14672 * get here. So the only cases this needs to
14673 * handle is a bp from the waitq (which we put
14674 * back onto the waitq for sdrunout), or a bp
14675 * sent as an immed_bp (which we just fail).
14676 */
14677 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14678 "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14679
14680 if (bp == immed_bp) {
14681 /*
14682 * If SD_XB_DMA_FREED is clear, then
14683 * this is a failure to allocate a
14684 * scsi_pkt, and we must fail the
14685 * command.
14686 */
14687 if ((xp->xb_pkt_flags &
14688 SD_XB_DMA_FREED) == 0) {
14689 break;
14690 }
14691
14692 /*
14693 * If this immediate command is NOT our
14694 * un_retry_bp, then we must fail it.
14695 */
14696 if (bp != un->un_retry_bp) {
14697 break;
14698 }
14699
14731 * keep kstat counts coherent
14732 * when we do retry the command.
14733 */
14734 un->un_retry_statp =
14735 saved_statp;
14736 }
14737
14738 if ((un->un_startstop_timeid == NULL) &&
14739 (un->un_retry_timeid == NULL) &&
14740 (un->un_direct_priority_timeid ==
14741 NULL)) {
14742
14743 un->un_retry_timeid =
14744 timeout(
14745 sd_start_retry_command,
14746 un, SD_RESTART_TIMEOUT);
14747 }
14748 goto exit;
14749 }
14750
14751 /* Add the buf back to the head of the waitq */
14752 bp->av_forw = un->un_waitq_headp;
14753 un->un_waitq_headp = bp;
14754 if (un->un_waitq_tailp == NULL) {
14755 un->un_waitq_tailp = bp;
14756 }
14757 goto exit;
14758
14759 case SD_PKT_ALLOC_FAILURE_NO_DMA:
14760 /*
14761 * HBA DMA resource failure. Fail the command
14762 * and continue processing of the queues.
14763 */
14764 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14765 "sd_start_cmds: "
14766 "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14767 break;
14768
14769 case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14770 /*
14771 * Partial DMA mapping not supported for USCSI
14772 * commands, and all the needed DMA resources
14773 * were not allocated.
14774 */
14775 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14776 "sd_start_cmds: "
14777 "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14778 break;
14779
14780 case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14781 /*
14782 * Request cannot fit into CDB based on lba
14783 * and len.
14784 */
14785 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14786 "sd_start_cmds: "
14787 "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14788 break;
14789
14790 default:
14791 /* Should NEVER get here! */
14792 panic("scsi_initpkt error");
14793 /*NOTREACHED*/
14794 }
14795
14796 /*
14797 * Fatal error in allocating a scsi_pkt for this buf.
14798 * Update kstats & return the buf with an error code.
14799 * We must use sd_return_failed_command_no_restart() to
14800 * avoid a recursive call back into sd_start_cmds().
14801 * However this also means that we must keep processing
14802 * the waitq here in order to avoid stalling.
14803 */
14804 if (statp == kstat_waitq_to_runq) {
14805 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14806 }
14807 sd_return_failed_command_no_restart(un, bp, EIO);
14808 if (bp == immed_bp) {
14809 /* immed_bp is gone by now, so clear this */
14810 immed_bp = NULL;
14811 }
14812 continue;
14813 }
14814 got_pkt:
14815 if (bp == immed_bp) {
14816 /* goto the head of the class.... */
14817 xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14818 }
14819
14820 un->un_ncmds_in_transport++;
14821 SD_UPDATE_KSTATS(un, statp, bp);
14822 /* The start time MAY be overriden by the HBA driver. */
14823 xp->xb_pktp->pkt_start = gethrtime();
14824 xp->xb_pktp->pkt_stop = 0;
14825
14826 /*
14827 * Call scsi_transport() to send the command to the target.
14828 * According to SCSA architecture, we must drop the mutex here
14829 * before calling scsi_transport() in order to avoid deadlock.
14830 * Note that the scsi_pkt's completion routine can be executed
14831 * (from interrupt context) even before the call to
14832 * scsi_transport() returns.
14833 */
14834 SD_TRACE(SD_LOG_IO_CORE, un,
14835 "sd_start_cmds: calling scsi_transport()\n");
14836 DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14837
14838 #ifdef SD_FAULT_INJECTION
14839 /*
14840 * Packet is ready for submission to the HBA. Perform HBA-based
14841 * fault-injection.
14842 */
14843 sd_prefaultinjection(xp->xb_pktp);
14844 #endif /* SD_FAULT_INJECTION */
14845
14846 mutex_exit(SD_MUTEX(un));
14847 rval = scsi_transport(xp->xb_pktp);
14848 mutex_enter(SD_MUTEX(un));
14849
14850 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14851 "sd_start_cmds: scsi_transport() returned %d\n", rval);
14852
14853 switch (rval) {
14854 case TRAN_ACCEPT:
14855 /* Clear this with every pkt accepted by the HBA */
14856 un->un_tran_fatal_count = 0;
14857 break; /* Success; try the next cmd (if any) */
14858
14859 case TRAN_BUSY:
14860 un->un_ncmds_in_transport--;
14861 ASSERT(un->un_ncmds_in_transport >= 0);
14862
14863 #ifdef SD_FAULT_INJECTION
14864 /*
14865 * If the packet was rejected during active fault
14866 * injection session, move to the next fault slot
14867 * and reset packet flag related to rejection.
14868 */
14869 if (sd_fault_injection_on) {
14870 uint_t i = un->sd_fi_fifo_start;
14871
14872 if (un->sd_fi_fifo_tran[i] != NULL) {
14873 kmem_free(un->sd_fi_fifo_tran[i],
14874 sizeof (struct sd_fi_tran));
14875 un->sd_fi_fifo_tran[i] = NULL;
14876 }
14877 un->sd_fi_fifo_start++;
14878 }
14879
14880 if (xp->xb_pktp->pkt_flags & FLAG_PKT_BUSY) {
14881 xp->xb_pktp->pkt_flags &= ~FLAG_PKT_BUSY;
14882 }
14883 #endif /* SD_FAULT_INJECTION */
14884
14885 /*
14886 * Don't retry request sense, the sense data
14887 * is lost when another request is sent.
14888 * Free up the rqs buf and retry
14889 * the original failed cmd. Update kstat.
14890 */
14891 if ((un->un_ncmds_in_transport > 0) &&
14892 (bp == un->un_rqs_bp)) {
14893 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14894 bp = sd_mark_rqs_idle(un, xp);
14895 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14896 NULL, NULL, EIO, un->un_busy_timeout / 500,
14897 kstat_waitq_enter);
14898 goto exit;
14899 }
14900
14901 /*
14902 * Free the DMA resources for the scsi_pkt. This will
14903 * allow mpxio to select another path the next time
14904 * we call scsi_transport() with this scsi_pkt.
14905 * See sdintr() for the rationalization behind this.
14906 */
14907 if ((un->un_f_is_fibre == TRUE) &&
14908 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14909 ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14910 scsi_dmafree(xp->xb_pktp);
14911 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14912 }
14913
14914 if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14915 /*
14916 * Commands that are SD_PATH_DIRECT_PRIORITY
14917 * are for error recovery situations. These do
14918 * not use the normal command waitq, so if they
14919 * get a TRAN_BUSY we cannot put them back onto
14920 * the waitq for later retry. One possible
14921 * problem is that there could already be some
14922 * other command on un_retry_bp that is waiting
14923 * for this one to complete, so we would be
14924 * deadlocked if we put this command back onto
14925 * the waitq for later retry (since un_retry_bp
14926 * must complete before the driver gets back to
14927 * commands on the waitq).
14928 *
14929 * To avoid deadlock we must schedule a callback
14930 * that will restart this command after a set
14931 * interval. This should keep retrying for as
14932 * long as the underlying transport keeps
14939 "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14940
14941 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14942 un->un_direct_priority_timeid =
14943 timeout(sd_start_direct_priority_command,
14944 bp, un->un_busy_timeout / 500);
14945
14946 goto exit;
14947 }
14948
14949 /*
14950 * For TRAN_BUSY, we want to reduce the throttle value,
14951 * unless we are retrying a command.
14952 */
14953 if (bp != un->un_retry_bp) {
14954 sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14955 }
14956
14957 /*
14958 * Set up the bp to be tried again 10 ms later.
14959 * XXX Is there a timeout value in the sd_lun
14960 * for this condition?
14961 */
14962 sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
14963 kstat_runq_back_to_waitq);
14964 goto exit;
14965
14966 case TRAN_FATAL_ERROR:
14967 un->un_tran_fatal_count++;
14968 /* FALLTHRU */
14969
14970 case TRAN_BADPKT:
14971 default:
14972 un->un_ncmds_in_transport--;
14973 ASSERT(un->un_ncmds_in_transport >= 0);
14974
14975 /*
14976 * If this is our REQUEST SENSE command with a
14977 * transport error, we must get back the pointers
14978 * to the original buf, and mark the REQUEST
14979 * SENSE command as "available".
15068 ASSERT(sfip != NULL);
15069
15070 SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15071
15072 /*
15073 * Note: check for the "sdrestart failed" case.
15074 */
15075 if ((un->un_partial_dma_supported == 1) &&
15076 ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15077 (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15078 (xp->xb_pktp->pkt_resid == 0)) {
15079
15080 if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15081 /*
15082 * Successfully set up next portion of cmd
15083 * transfer, try sending it
15084 */
15085 sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15086 NULL, NULL, 0, (clock_t)0, NULL);
15087 sd_start_cmds(un, NULL);
15088 return; /* XXX need a return here? */
15089 }
15090 }
15091
15092 /*
15093 * If this is the failfast bp, clear it from un_failfast_bp. This
15094 * can happen if upon being re-tried the failfast bp either
15095 * succeeded or encountered another error (possibly even a different
15096 * error than the one that precipitated the failfast state, but in
15097 * that case it would have had to exhaust retries as well). Regardless,
15098 * this should not occur whenever the instance is in the active
15099 * failfast state.
15100 */
15101 if (bp == un->un_failfast_bp) {
15102 ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15103 un->un_failfast_bp = NULL;
15104 }
15105
15106 /*
15107 * Clear the failfast state upon successful completion of ANY cmd.
15108 */
15284 *
15285 * Arguments: un - Pointer to the sd_lun struct for the target.
15286 *
15287 * bp - Pointer to the buf for the command to be retried.
15288 *
15289 * retry_check_flag - Flag to see which (if any) of the retry
15290 * counts should be decremented/checked. If the indicated
15291 * retry count is exhausted, then the command will not be
15292 * retried; it will be failed instead. This should use a
15293 * value equal to one of the following:
15294 *
15295 * SD_RETRIES_NOCHECK
15296 * SD_RESD_RETRIES_STANDARD
15297 * SD_RETRIES_VICTIM
15298 *
15299 * Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15300 * if the check should be made to see of FLAG_ISOLATE is set
15301 * in the pkt. If FLAG_ISOLATE is set, then the command is
15302 * not retried, it is simply failed.
15303 *
15304 * Optionally may be bitwise-OR'ed with SD_RETRIES_FAILFAST
15305 * to indicate a retry following a command timeout, and check
15306 * if the target should transition to failfast pending or
15307 * failfast active. If the buf has B_FAILFAST set, the
15308 * command should be failed when failfast is active.
15309 *
15310 * user_funcp - Ptr to function to call before dispatching the
15311 * command. May be NULL if no action needs to be performed.
15312 * (Primarily intended for printing messages.)
15313 *
15314 * user_arg - Optional argument to be passed along to
15315 * the user_funcp call.
15316 *
15317 * failure_code - errno return code to set in the bp if the
15318 * command is going to be failed.
15319 *
15320 * retry_delay - Retry delay interval in (clock_t) units. May
15321 * be zero which indicates that the retry should be retried
15322 * immediately (ie, without an intervening delay).
15323 *
15324 * statp - Ptr to kstat function to be updated if the command
15325 * is queued for a delayed retry. May be NULL if no kstat
15326 * update is desired.
15327 *
15328 * Context: May be called from interrupt context.
15329 */
15394 }
15395 SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15396 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15397 "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15398 return;
15399 default:
15400 break;
15401 }
15402
15403 /*
15404 * If the caller wants us to check FLAG_ISOLATE, then see if that
15405 * is set; if it is then we do not want to retry the command.
15406 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15407 */
15408 if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15409 if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15410 goto fail_command;
15411 }
15412 }
15413
15414 if (sd_failfast_enable & (SD_FAILFAST_ENABLE_FAIL_RETRIES |
15415 SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES)) {
15416 if (sd_failfast_enable & SD_FAILFAST_ENABLE_FAIL_ALL_RETRIES) {
15417 /*
15418 * Fail ALL retries when in active failfast state,
15419 * regardless of reason.
15420 */
15421 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15422 goto fail_command;
15423 }
15424 }
15425 /*
15426 * Treat bufs being retried as if they have the
15427 * B_FAILFAST flag set.
15428 */
15429 bp->b_flags |= B_FAILFAST;
15430 }
15431
15432 /*
15433 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15434 * command timeout or a selection timeout has occurred. This means
15435 * that we were unable to establish an kind of communication with
15436 * the target, and subsequent retries and/or commands are likely
15437 * to encounter similar results and take a long time to complete.
15438 *
15439 * If this is a failfast error condition, we need to update the
15440 * failfast state, even if this bp does not have B_FAILFAST set.
15441 */
15442 if (retry_check_flag & SD_RETRIES_FAILFAST) {
15443 if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15444 ASSERT(un->un_failfast_bp == NULL);
15445 /*
15446 * If we are already in the active failfast state, and
15447 * another failfast error condition has been detected,
15448 * then fail this command if it has B_FAILFAST set.
15449 * If B_FAILFAST is clear, then maintain the legacy
15450 * behavior of retrying heroically, even tho this will
15462 */
15463 if (un->un_failfast_bp == NULL) {
15464 /*
15465 * This is the first bp to meet a failfast
15466 * condition so save it on un_failfast_bp &
15467 * do normal retry processing. Do not enter
15468 * active failfast state yet. This marks
15469 * entry into the "failfast pending" state.
15470 */
15471 un->un_failfast_bp = bp;
15472
15473 } else if (un->un_failfast_bp == bp) {
15474 /*
15475 * This is the second time *this* bp has
15476 * encountered a failfast error condition,
15477 * so enter active failfast state & flush
15478 * queues as appropriate.
15479 */
15480 un->un_failfast_state = SD_FAILFAST_ACTIVE;
15481 un->un_failfast_bp = NULL;
15482 sd_failfast_flushq(un, B_FALSE);
15483
15484 /*
15485 * Fail this bp now if B_FAILFAST set;
15486 * otherwise continue with retries. (It would
15487 * be pretty ironic if this bp succeeded on a
15488 * subsequent retry after we just flushed all
15489 * the queues).
15490 */
15491 if (bp->b_flags & B_FAILFAST) {
15492 goto fail_command;
15493 }
15494
15495 #if !defined(lint) && !defined(__lint)
15496 } else {
15497 /*
15498 * If neither of the preceeding conditionals
15499 * was true, it means that there is some
15500 * *other* bp that has met an inital failfast
15501 * condition and is currently either being
15502 * retried or is waiting to be retried. In
15503 * that case we should perform normal retry
15504 * processing on *this* bp, since there is a
15505 * chance that the current failfast condition
15506 * is transient and recoverable. If that does
15507 * not turn out to be the case, then retries
15508 * will be cleared when the wait queue is
15509 * flushed anyway.
15510 */
15511 #endif
15512 }
15513 }
15514 } else {
15515 /*
15516 * SD_RETRIES_FAILFAST is clear, which indicates that we
15517 * likely were able to at least establish some level of
15518 * communication with the target and subsequent commands
15519 * and/or retries are likely to get through to the target,
15520 * In this case we want to be aggressive about clearing
15521 * the failfast state. Note that this does not affect
15522 * the "failfast pending" condition.
15523 *
15524 * We limit this to retries that are not a side effect of an
15525 * unrelated event, as it would be unwise to clear failfast
15526 * active state when we see retries due to a reset.
15527 */
15528 if ((sd_failfast_enable & SD_FAILFAST_ENABLE_FORCE_INACTIVE) &&
15529 (retry_check_flag & SD_RETRIES_MASK) != SD_RETRIES_VICTIM)
15530 un->un_failfast_state = SD_FAILFAST_INACTIVE;
15531 }
15532
15533
15534 /*
15535 * Check the specified retry count to see if we can still do
15536 * any retries with this pkt before we should fail it.
15537 */
15538 switch (retry_check_flag & SD_RETRIES_MASK) {
15539 case SD_RETRIES_VICTIM:
15540 /*
15541 * Check the victim retry count. If exhausted, then fall
15542 * thru & check against the standard retry count.
15543 */
15544 if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15545 /* Increment count & proceed with the retry */
15546 xp->xb_victim_retry_count++;
15547 break;
15548 }
15549 /* Victim retries exhausted, fall back to std. retries... */
15951 "sd_start_direct_priority_command: entry\n");
15952
15953 mutex_enter(SD_MUTEX(un));
15954 un->un_direct_priority_timeid = NULL;
15955 sd_start_cmds(un, priority_bp);
15956 mutex_exit(SD_MUTEX(un));
15957
15958 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15959 "sd_start_direct_priority_command: exit\n");
15960 }
15961
15962
15963 /*
15964 * Function: sd_send_request_sense_command
15965 *
15966 * Description: Sends a REQUEST SENSE command to the target
15967 *
15968 * Context: May be called from interrupt context.
15969 */
15970
15971 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15972 int retry_check_flag, struct scsi_pkt *pktp)
15973 {
15974 ASSERT(bp != NULL);
15975 ASSERT(un != NULL);
15976 ASSERT(mutex_owned(SD_MUTEX(un)));
15977
15978 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15979 "entry: buf:0x%p\n", bp);
15980
15981 /*
15982 * If we are syncing or dumping, then fail the command to avoid a
15983 * recursive callback into scsi_transport(). Also fail the command
15984 * if we are suspended (legacy behavior).
15985 */
15986 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15987 (un->un_state == SD_STATE_DUMPING)) {
15988 sd_return_failed_command(un, bp, EIO);
15989 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15990 "sd_send_request_sense_command: syncing/dumping, exit\n");
15991 return;
15992 }
15993
15994 /*
15995 * Retry the failed command and don't issue the request sense if:
15996 * 1) the sense buf is busy
15997 * 2) we have 1 or more outstanding commands on the target
15998 * (the sense data will be cleared or invalidated any way)
15999 */
16000 if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16001 /* Don't retry if the command is flagged as non-retryable */
16002 if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16003 sd_retry_command(un, bp, retry_check_flag,
16004 NULL, NULL, 0, un->un_busy_timeout,
16005 kstat_waitq_enter);
16006 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16007 "sd_send_request_sense_command: "
16008 "at full throttle, retrying exit\n");
16009 } else {
16010 sd_return_failed_command(un, bp, EIO);
16011 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16012 "sd_send_request_sense_command: "
16013 "at full throttle, non-retryable exit\n");
16014 }
16015 return;
16016 }
16017
16018 sd_mark_rqs_busy(un, bp);
16019 sd_start_cmds(un, un->un_rqs_bp);
16020
16021 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16022 "sd_send_request_sense_command: exit\n");
16023 }
16155 if (un->un_rqs_bp == NULL) {
16156 return (DDI_FAILURE);
16157 }
16158
16159 un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16160 CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16161
16162 if (un->un_rqs_pktp == NULL) {
16163 sd_free_rqs(un);
16164 return (DDI_FAILURE);
16165 }
16166
16167 /* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16168 (void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16169 SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16170
16171 SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16172
16173 /* Set up the other needed members in the ARQ scsi_pkt. */
16174 un->un_rqs_pktp->pkt_comp = sdintr;
16175 un->un_rqs_pktp->pkt_time = ((ISCD(un)) ? 2 : 1) *
16176 (ushort_t)un->un_io_time;
16177 un->un_rqs_pktp->pkt_flags |= (FLAG_SENSING | FLAG_HEAD);
16178
16179 /*
16180 * Allocate & init the sd_xbuf struct for the RQS command. Do not
16181 * provide any intpkt, destroypkt routines as we take care of
16182 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16183 */
16184 xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16185 sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16186 xp->xb_pktp = un->un_rqs_pktp;
16187 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16188 "sd_alloc_rqs: un 0x%p, rqs xp 0x%p, pkt 0x%p, buf 0x%p\n",
16189 un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16190
16191 /*
16192 * Save the pointer to the request sense private bp so it can
16193 * be retrieved in sdintr.
16194 */
16195 un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16196 ASSERT(un->un_rqs_bp->b_private == xp);
16197
16198 /*
16199 * See if the HBA supports auto-request sense for the specified
16200 * target/lun. If it does, then try to enable it (if not already
16201 * enabled).
16202 *
16203 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16204 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16205 * return success. However, in both of these cases ARQ is always
16206 * enabled and scsi_ifgetcap will always return true. The best approach
16207 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16208 *
16209 * The 3rd case is the HBA (adp) always return enabled on
16210 * scsi_ifgetgetcap even when it's not enable, the best approach
16211 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16212 */
16213
16214 if (un->un_f_is_fibre == TRUE) {
16215 un->un_f_arq_enabled = TRUE;
16216 } else {
16217 /*
16218 * XXX Circumvent the Adaptec bug, remove this code when
16219 * the bug is fixed.
16220 */
16221 (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16222 switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16223 case 0:
16224 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16225 "sd_alloc_rqs: HBA supports ARQ\n");
16226 /*
16227 * ARQ is supported by this HBA but currently is not
16228 * enabled. Attempt to enable it and if successful then
16229 * mark this instance as ARQ enabled.
16230 */
16231 if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16232 == 1) {
16233 /* Successfully enabled ARQ in the HBA */
16234 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16235 "sd_alloc_rqs: ARQ enabled\n");
16236 un->un_f_arq_enabled = TRUE;
16237 } else {
16238 /* Could not enable ARQ in the HBA */
16239 SD_INFO(SD_LOG_ATTACH_DETACH, un,
16240 "sd_alloc_rqs: failed ARQ enable\n");
16241 un->un_f_arq_enabled = FALSE;
16496 {
16497 struct sd_lun *un = (struct sd_lun *)arg;
16498
16499 ASSERT(un != NULL);
16500 ASSERT(!mutex_owned(SD_MUTEX(un)));
16501
16502 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16503
16504 mutex_enter(SD_MUTEX(un));
16505 sd_start_cmds(un, NULL);
16506 mutex_exit(SD_MUTEX(un));
16507 /*
16508 * This callback routine always returns 1 (i.e. do not reschedule)
16509 * because we always specify sdrunout as the callback handler for
16510 * scsi_init_pkt inside the call to sd_start_cmds.
16511 */
16512 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16513 return (1);
16514 }
16515
16516 static void
16517 sd_slow_io_ereport(struct scsi_pkt *pktp)
16518 {
16519 struct buf *bp;
16520 struct sd_lun *un;
16521 char *devid;
16522
16523 ASSERT(pktp != NULL);
16524 bp = (struct buf *)pktp->pkt_private;
16525 ASSERT(bp != NULL);
16526 un = SD_GET_UN(bp);
16527 ASSERT(un != NULL);
16528
16529 SD_ERROR(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16530 "Slow IO detected SD: 0x%p delta in nsec: %llu",
16531 (void *)un, pktp->pkt_stop - pktp->pkt_start);
16532
16533 devid = DEVI(un->un_sd->sd_dev)->devi_devid_str;
16534 scsi_fm_ereport_post(un->un_sd, 0, NULL, "cmd.disk.slow-io",
16535 fm_ena_generate(0, FM_ENA_FMT1), devid, NULL, DDI_NOSLEEP, NULL,
16536 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
16537 "start", DATA_TYPE_UINT64, pktp->pkt_start,
16538 "stop", DATA_TYPE_UINT64, pktp->pkt_stop,
16539 "delta", DATA_TYPE_UINT64, pktp->pkt_stop - pktp->pkt_start,
16540 "threshold", DATA_TYPE_UINT64, un->un_slow_io_threshold,
16541 "pkt-reason", DATA_TYPE_UINT32, pktp->pkt_reason,
16542 NULL);
16543 }
16544
16545 /* Clamp the value between 0..max using min as the offset */
16546 static int
16547 clamp_lat(int bucket, int min, int max)
16548 {
16549
16550 if (max < bucket)
16551 bucket = max;
16552 if (min > bucket)
16553 bucket = min;
16554
16555 return (bucket - min);
16556 }
16557
16558 /*
16559 * Function: sdintr
16560 *
16561 * Description: Completion callback routine for scsi_pkt(9S) structs
16562 * sent to the HBA driver via scsi_transport(9F).
16563 *
16564 * Context: Interrupt context
16565 */
16566
16567 static void
16568 sdintr(struct scsi_pkt *pktp)
16569 {
16570 struct buf *bp;
16571 struct sd_xbuf *xp;
16572 struct sd_lun *un;
16573 size_t actual_len;
16574 sd_ssc_t *sscp;
16575 hrtime_t io_delta = 0LL;
16576 int bucket;
16577
16578 ASSERT(pktp != NULL);
16579 bp = (struct buf *)pktp->pkt_private;
16580 ASSERT(bp != NULL);
16581 xp = SD_GET_XBUF(bp);
16582 ASSERT(xp != NULL);
16583 ASSERT(xp->xb_pktp != NULL);
16584 un = SD_GET_UN(bp);
16585 ASSERT(un != NULL);
16586 ASSERT(!mutex_owned(SD_MUTEX(un)));
16587
16588 #ifdef SD_FAULT_INJECTION
16589
16590 SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16591 /* SD FaultInjection */
16592 sd_faultinjection(pktp);
16593
16594 #endif /* SD_FAULT_INJECTION */
16595
16596 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16597 " xp:0x%p, un:0x%p\n", bp, xp, un);
16598
16599 mutex_enter(SD_MUTEX(un));
16600
16601 ASSERT(un->un_fm_private != NULL);
16602 sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16603 ASSERT(sscp != NULL);
16604
16605 /* Reduce the count of the #commands currently in transport */
16606 un->un_ncmds_in_transport--;
16607 ASSERT(un->un_ncmds_in_transport >= 0);
16608
16609 /* Increment counter to indicate that the callback routine is active */
16610 un->un_in_callback++;
16611
16612 SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16613 /* If the HBA driver did not set the stop time, set it now. */
16614 if (pktp->pkt_stop == 0)
16615 pktp->pkt_stop = gethrtime();
16616 /*
16617 * If there are HBA drivers or layered drivers which do not participate
16618 * in slow-io diagnosis, the start time, set above may be overwritten
16619 * with zero. If pkt_start is zero, the delta should also be zero.
16620 */
16621 if (pktp->pkt_start != 0)
16622 io_delta = pktp->pkt_stop - pktp->pkt_start;
16623 if (un->un_slow_io_threshold > 0 && io_delta > un->un_slow_io_threshold)
16624 sd_slow_io_ereport(pktp);
16625 if (un->un_lat_stats) {
16626 un->un_lat_stats->l_nrequest++;
16627 un->un_lat_stats->l_sum += io_delta;
16628
16629 /* Track the latency in usec and quantize by power of 2 */
16630 bucket = clamp_lat(ddi_fls(io_delta / 1000),
16631 SD_LAT_MIN_USEC_SHIFT, SD_LAT_MAX_USEC_SHIFT - 1);
16632 ASSERT3S(bucket, >=, 0);
16633 ASSERT3S(bucket, <, ARRAY_SIZE(un->un_lat_stats->l_histogram));
16634 un->un_lat_stats->l_histogram[bucket]++;
16635 }
16636
16637 #ifdef SDDEBUG
16638 if (bp == un->un_retry_bp) {
16639 SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16640 "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16641 un, un->un_retry_bp, un->un_ncmds_in_transport);
16642 }
16643 #endif
16644
16645 /*
16646 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16647 * state if needed.
16648 */
16649 if (pktp->pkt_reason == CMD_DEV_GONE) {
16650 /* Prevent multiple console messages for the same failure. */
16651 if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16652 un->un_last_pkt_reason = CMD_DEV_GONE;
16653 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16654 "Command failed to complete...Device is gone\n");
16655 }
16656 if (un->un_mediastate != DKIO_DEV_GONE) {
16715 actual_len) {
16716 xp->xb_sense_resid =
16717 (((struct uscsi_cmd *)
16718 (xp->xb_pktinfo))->
16719 uscsi_rqlen) - actual_len;
16720 } else {
16721 xp->xb_sense_resid = 0;
16722 }
16723 }
16724 bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16725 SENSE_LENGTH);
16726 }
16727
16728 /* fail the command */
16729 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16730 "sdintr: arq done and FLAG_DIAGNOSE set\n");
16731 sd_return_failed_command(un, bp, EIO);
16732 goto exit;
16733 }
16734
16735 /*
16736 * We want to either retry or fail this command, so free
16737 * the DMA resources here. If we retry the command then
16738 * the DMA resources will be reallocated in sd_start_cmds().
16739 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16740 * causes the *entire* transfer to start over again from the
16741 * beginning of the request, even for PARTIAL chunks that
16742 * have already transferred successfully.
16743 */
16744 if ((un->un_f_is_fibre == TRUE) &&
16745 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16746 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16747 scsi_dmafree(pktp);
16748 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16749 }
16750
16751 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16752 "sdintr: arq done, sd_handle_auto_request_sense\n");
16753
16754 sd_handle_auto_request_sense(un, bp, xp, pktp);
16755 goto exit;
16756 }
16757
16758 /* Next see if this is the REQUEST SENSE pkt for the instance */
16759 if (pktp->pkt_flags & FLAG_SENSING) {
16760 /* This pktp is from the unit's REQUEST_SENSE command */
16761 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16762 "sdintr: sd_handle_request_sense\n");
16763 sd_handle_request_sense(un, bp, xp, pktp);
16764 goto exit;
16765 }
16766
16767 /*
16768 * Check to see if the command successfully completed as requested;
16769 * this is the most common case (and also the hot performance path).
16805 SD_UPDATE_B_RESID(bp, pktp);
16806 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16807 "sdintr: returning uscsi command\n");
16808 } else {
16809 goto not_successful;
16810 }
16811 sd_return_command(un, bp);
16812
16813 /*
16814 * Decrement counter to indicate that the callback routine
16815 * is done.
16816 */
16817 un->un_in_callback--;
16818 ASSERT(un->un_in_callback >= 0);
16819 mutex_exit(SD_MUTEX(un));
16820
16821 return;
16822 }
16823
16824 not_successful:
16825 /*
16826 * The following is based upon knowledge of the underlying transport
16827 * and its use of DMA resources. This code should be removed when
16828 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16829 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16830 * and sd_start_cmds().
16831 *
16832 * Free any DMA resources associated with this command if there
16833 * is a chance it could be retried or enqueued for later retry.
16834 * If we keep the DMA binding then mpxio cannot reissue the
16835 * command on another path whenever a path failure occurs.
16836 *
16837 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16838 * causes the *entire* transfer to start over again from the
16839 * beginning of the request, even for PARTIAL chunks that
16840 * have already transferred successfully.
16841 *
16842 * This is only done for non-uscsi commands (and also skipped for the
16843 * driver's internal RQS command). Also just do this for Fibre Channel
16844 * devices as these are the only ones that support mpxio.
16845 */
16846 if ((un->un_f_is_fibre == TRUE) &&
16847 ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16848 ((pktp->pkt_flags & FLAG_SENSING) == 0)) {
16849 scsi_dmafree(pktp);
16850 xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16851 }
16852
16853 /*
16854 * The command did not successfully complete as requested so check
16855 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16856 * driver command that should not be retried so just return. If
16857 * FLAG_DIAGNOSE is not set the error will be processed below.
16858 */
16859 if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16860 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16861 "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16862 /*
16863 * Issue a request sense if a check condition caused the error
16864 * (we handle the auto request sense case above), otherwise
16865 * just fail the command.
16866 */
16867 if ((pktp->pkt_reason == CMD_CMPLT) &&
16868 (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16869 sd_send_request_sense_command(un, bp,
16870 SD_RETRIES_STANDARD, pktp);
16871 } else {
16872 sd_return_failed_command(un, bp, EIO);
16873 }
16874 goto exit;
16875 }
16876
16877 /*
16878 * The command did not successfully complete as requested so process
16879 * the error, retry, and/or attempt recovery.
16880 */
16881 switch (pktp->pkt_reason) {
16882 case CMD_CMPLT:
16883 switch (SD_GET_PKT_STATUS(pktp)) {
16884 case STATUS_GOOD:
16885 /*
16886 * The command completed successfully with a non-zero
16887 * residual
16888 */
16889 SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16890 "sdintr: STATUS_GOOD \n");
17494 if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17495 (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17496 (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17497 (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17498 (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17499 /* Mark the ssc_flags for detecting invalid sense data */
17500 if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17501 sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17502 "sense-data");
17503 }
17504 goto sense_failed;
17505 }
17506
17507 return (SD_SENSE_DATA_IS_VALID);
17508
17509 sense_failed:
17510 /*
17511 * If the request sense failed (for whatever reason), attempt
17512 * to retry the original command.
17513 */
17514 sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17515 sd_print_sense_failed_msg, msgp, EIO,
17516 un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17517
17518 return (SD_SENSE_DATA_IS_INVALID);
17519 }
17520
17521 /*
17522 * Function: sd_decode_sense
17523 *
17524 * Description: Take recovery action(s) when SCSI Sense Data is received.
17525 *
17526 * Context: Interrupt context.
17527 */
17528
17529 static void
17530 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17531 struct scsi_pkt *pktp)
17532 {
17533 uint8_t sense_key;
17534
17535 ASSERT(un != NULL);
17536 ASSERT(mutex_owned(SD_MUTEX(un)));
18942 */
18943
18944 static void
18945 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18946 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18947 {
18948 void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18949
18950 ASSERT(un != NULL);
18951 ASSERT(mutex_owned(SD_MUTEX(un)));
18952 ASSERT(bp != NULL);
18953 ASSERT(xp != NULL);
18954 ASSERT(pktp != NULL);
18955
18956 SD_UPDATE_ERRSTATS(un, sd_harderrs);
18957 SD_UPDATE_RESERVATION_STATUS(un, pktp);
18958
18959 funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18960 sd_print_retry_msg : NULL;
18961
18962 sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18963 funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18964 }
18965
18966
18967 /*
18968 * Function: sd_pkt_reason_cmd_tag_reject
18969 *
18970 * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18971 *
18972 * Context: May be called from interrupt context
18973 */
18974
18975 static void
18976 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18977 struct sd_xbuf *xp, struct scsi_pkt *pktp)
18978 {
18979 ASSERT(un != NULL);
18980 ASSERT(mutex_owned(SD_MUTEX(un)));
18981 ASSERT(bp != NULL);
18982 ASSERT(xp != NULL);
19045 struct sd_xbuf *xp, struct scsi_pkt *pktp)
19046 {
19047 ASSERT(un != NULL);
19048 ASSERT(mutex_owned(SD_MUTEX(un)));
19049 ASSERT(bp != NULL);
19050 ASSERT(xp != NULL);
19051 ASSERT(pktp != NULL);
19052
19053 SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19054 "entry: buf:0x%p xp:0x%p\n", bp, xp);
19055
19056 /*
19057 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19058 * command will be retried after the request sense). Otherwise, retry
19059 * the command. Note: we are issuing the request sense even though the
19060 * retry limit may have been reached for the failed command.
19061 */
19062 if (un->un_f_arq_enabled == FALSE) {
19063 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19064 "no ARQ, sending request sense command\n");
19065 sd_send_request_sense_command(un, bp, SD_RETRIES_STANDARD,
19066 pktp);
19067 } else {
19068 SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19069 "ARQ,retrying request sense command\n");
19070 sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19071 un->un_f_is_fibre ? drv_usectohz(100000) : (clock_t)0,
19072 NULL);
19073 }
19074
19075 SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19076 }
19077
19078
19079 /*
19080 * Function: sd_pkt_status_busy
19081 *
19082 * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19083 *
19084 * Context: May be called from interrupt context
19085 */
19086
19087 static void
19088 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19089 struct scsi_pkt *pktp)
19090 {
19091 ASSERT(un != NULL);
19092 ASSERT(mutex_owned(SD_MUTEX(un)));
19808 * (This command is mandatory under SCSI-2.)
19809 *
19810 * Set up the CDB for the READ_CAPACITY command. The Partial
19811 * Medium Indicator bit is cleared. The address field must be
19812 * zero if the PMI bit is zero.
19813 */
19814 bzero(&cdb, sizeof (cdb));
19815 bzero(&ucmd_buf, sizeof (ucmd_buf));
19816
19817 capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19818
19819 cdb.scc_cmd = SCMD_READ_CAPACITY;
19820
19821 ucmd_buf.uscsi_cdb = (char *)&cdb;
19822 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
19823 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity_buf;
19824 ucmd_buf.uscsi_buflen = SD_CAPACITY_SIZE;
19825 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
19826 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
19827 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19828 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
19829
19830 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19831 UIO_SYSSPACE, path_flag);
19832
19833 switch (status) {
19834 case 0:
19835 /* Return failure if we did not get valid capacity data. */
19836 if (ucmd_buf.uscsi_resid != 0) {
19837 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19838 "sd_send_scsi_READ_CAPACITY received invalid "
19839 "capacity data");
19840 kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19841 return (EIO);
19842 }
19843 /*
19844 * Read capacity and block size from the READ CAPACITY 10 data.
19845 * This data may be adjusted later due to device specific
19846 * issues.
19847 *
19848 * According to the SCSI spec, the READ CAPACITY 10
20024
20025 /*
20026 * First send a READ_CAPACITY_16 command to the target.
20027 *
20028 * Set up the CDB for the READ_CAPACITY_16 command. The Partial
20029 * Medium Indicator bit is cleared. The address field must be
20030 * zero if the PMI bit is zero.
20031 */
20032 bzero(&cdb, sizeof (cdb));
20033 bzero(&ucmd_buf, sizeof (ucmd_buf));
20034
20035 capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20036
20037 ucmd_buf.uscsi_cdb = (char *)&cdb;
20038 ucmd_buf.uscsi_cdblen = CDB_GROUP4;
20039 ucmd_buf.uscsi_bufaddr = (caddr_t)capacity16_buf;
20040 ucmd_buf.uscsi_buflen = SD_CAPACITY_16_SIZE;
20041 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20042 ucmd_buf.uscsi_rqlen = sizeof (sense_buf);
20043 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20044 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
20045
20046 /*
20047 * Read Capacity (16) is a Service Action In command. One
20048 * command byte (0x9E) is overloaded for multiple operations,
20049 * with the second CDB byte specifying the desired operation
20050 */
20051 cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20052 cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20053
20054 /*
20055 * Fill in allocation length field
20056 */
20057 FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20058
20059 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20060 UIO_SYSSPACE, path_flag);
20061
20062 switch (status) {
20063 case 0:
20064 /* Return failure if we did not get valid capacity data. */
20068 "capacity data");
20069 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20070 return (EIO);
20071 }
20072
20073 /*
20074 * Read capacity and block size from the READ CAPACITY 16 data.
20075 * This data may be adjusted later due to device specific
20076 * issues.
20077 *
20078 * According to the SCSI spec, the READ CAPACITY 16
20079 * command returns the following:
20080 *
20081 * bytes 0-7: Maximum logical block address available.
20082 * (MSB in byte:0 & LSB in byte:7)
20083 *
20084 * bytes 8-11: Block length in bytes
20085 * (MSB in byte:8 & LSB in byte:11)
20086 *
20087 * byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20088 *
20089 * byte 14:
20090 * bit 7: Thin-Provisioning Enabled
20091 * bit 6: Thin-Provisioning Read Zeros
20092 */
20093 capacity = BE_64(capacity16_buf[0]);
20094 lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20095 lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20096
20097 un->un_thin_flags = 0;
20098 if (((uint8_t *)capacity16_buf)[14] & (1 << 7))
20099 un->un_thin_flags |= SD_THIN_PROV_ENABLED;
20100 if (((uint8_t *)capacity16_buf)[14] & (1 << 6))
20101 un->un_thin_flags |= SD_THIN_PROV_READ_ZEROS;
20102
20103 pbsize = lbasize << lbpb_exp;
20104
20105 /*
20106 * Done with capacity16_buf
20107 */
20108 kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20109
20110 /*
20111 * if the reported capacity is set to all 0xf's, then
20112 * this disk is too large. This could only happen with
20113 * a device that supports LBAs larger than 64 bits which
20114 * are not defined by any current T10 standards.
20115 */
20116 if (capacity == 0xffffffffffffffff) {
20117 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20118 "disk is too large");
20119 return (EIO);
20120 }
20121 break; /* Success! */
20122 case EIO:
20256 return (EAGAIN);
20257 }
20258 mutex_exit(SD_MUTEX(un));
20259 }
20260
20261 bzero(&cdb, sizeof (cdb));
20262 bzero(&ucmd_buf, sizeof (ucmd_buf));
20263 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20264
20265 cdb.scc_cmd = SCMD_START_STOP;
20266 cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20267 (uchar_t)(flag << 4) : (uchar_t)flag;
20268
20269 ucmd_buf.uscsi_cdb = (char *)&cdb;
20270 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20271 ucmd_buf.uscsi_bufaddr = NULL;
20272 ucmd_buf.uscsi_buflen = 0;
20273 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20274 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20275 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20276 ucmd_buf.uscsi_timeout = 3 * un->un_uscsi_timeout;
20277
20278 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20279 UIO_SYSSPACE, path_flag);
20280
20281 switch (status) {
20282 case 0:
20283 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20284 break; /* Success! */
20285 case EIO:
20286 switch (ucmd_buf.uscsi_status) {
20287 case STATUS_RESERVATION_CONFLICT:
20288 status = EACCES;
20289 break;
20290 case STATUS_CHECK:
20291 if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20292 switch (scsi_sense_key(
20293 (uint8_t *)&sense_buf)) {
20294 case KEY_ILLEGAL_REQUEST:
20295 status = ENOTSUP;
20296 break;
20475 ASSERT(bufaddr != NULL);
20476
20477 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20478
20479 bzero(&cdb, sizeof (cdb));
20480 bzero(&ucmd_buf, sizeof (ucmd_buf));
20481 bzero(bufaddr, buflen);
20482
20483 cdb.scc_cmd = SCMD_INQUIRY;
20484 cdb.cdb_opaque[1] = evpd;
20485 cdb.cdb_opaque[2] = page_code;
20486 FORMG0COUNT(&cdb, buflen);
20487
20488 ucmd_buf.uscsi_cdb = (char *)&cdb;
20489 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20490 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
20491 ucmd_buf.uscsi_buflen = buflen;
20492 ucmd_buf.uscsi_rqbuf = NULL;
20493 ucmd_buf.uscsi_rqlen = 0;
20494 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
20495 ucmd_buf.uscsi_timeout = 2 * un->un_uscsi_timeout;
20496
20497 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20498 UIO_SYSSPACE, SD_PATH_DIRECT);
20499
20500 /*
20501 * Only handle status == 0, the upper-level caller
20502 * will put different assessment based on the context.
20503 */
20504 if (status == 0)
20505 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20506
20507 if ((status == 0) && (residp != NULL)) {
20508 *residp = ucmd_buf.uscsi_resid;
20509 }
20510
20511 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20512
20513 return (status);
20514 }
20515
20578 }
20579
20580 bzero(&cdb, sizeof (cdb));
20581 bzero(&ucmd_buf, sizeof (ucmd_buf));
20582 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20583
20584 cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20585
20586 ucmd_buf.uscsi_cdb = (char *)&cdb;
20587 ucmd_buf.uscsi_cdblen = CDB_GROUP0;
20588 ucmd_buf.uscsi_bufaddr = NULL;
20589 ucmd_buf.uscsi_buflen = 0;
20590 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20591 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20592 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20593
20594 /* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20595 if ((flag & SD_DONT_RETRY_TUR) != 0) {
20596 ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20597 }
20598 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
20599
20600 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20601 UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20602 SD_PATH_STANDARD));
20603
20604 switch (status) {
20605 case 0:
20606 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20607 break; /* Success! */
20608 case EIO:
20609 switch (ucmd_buf.uscsi_status) {
20610 case STATUS_RESERVATION_CONFLICT:
20611 status = EACCES;
20612 break;
20613 case STATUS_CHECK:
20614 if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20615 break;
20616 }
20617 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20618 (scsi_sense_key((uint8_t *)&sense_buf) ==
20675 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20676 if (data_bufp == NULL) {
20677 /* Allocate a default buf if the caller did not give one */
20678 ASSERT(data_len == 0);
20679 data_len = MHIOC_RESV_KEY_SIZE;
20680 data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20681 no_caller_buf = TRUE;
20682 }
20683
20684 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20685 cdb.cdb_opaque[1] = usr_cmd;
20686 FORMG1COUNT(&cdb, data_len);
20687
20688 ucmd_buf.uscsi_cdb = (char *)&cdb;
20689 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20690 ucmd_buf.uscsi_bufaddr = (caddr_t)data_bufp;
20691 ucmd_buf.uscsi_buflen = data_len;
20692 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20693 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20694 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20695 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
20696
20697 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20698 UIO_SYSSPACE, SD_PATH_STANDARD);
20699
20700 switch (status) {
20701 case 0:
20702 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20703
20704 break; /* Success! */
20705 case EIO:
20706 switch (ucmd_buf.uscsi_status) {
20707 case STATUS_RESERVATION_CONFLICT:
20708 status = EACCES;
20709 break;
20710 case STATUS_CHECK:
20711 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20712 (scsi_sense_key((uint8_t *)&sense_buf) ==
20713 KEY_ILLEGAL_REQUEST)) {
20714 status = ENOTSUP;
20715 }
20781 if (usr_bufp == NULL) {
20782 return (EINVAL);
20783 }
20784
20785 bzero(&cdb, sizeof (cdb));
20786 bzero(&ucmd_buf, sizeof (ucmd_buf));
20787 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20788 prp = kmem_zalloc(data_len, KM_SLEEP);
20789
20790 cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20791 cdb.cdb_opaque[1] = usr_cmd;
20792 FORMG1COUNT(&cdb, data_len);
20793
20794 ucmd_buf.uscsi_cdb = (char *)&cdb;
20795 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
20796 ucmd_buf.uscsi_bufaddr = (caddr_t)prp;
20797 ucmd_buf.uscsi_buflen = data_len;
20798 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
20799 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
20800 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20801 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
20802
20803 switch (usr_cmd) {
20804 case SD_SCSI3_REGISTER: {
20805 mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20806
20807 bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20808 bcopy(ptr->newkey.key, prp->service_key,
20809 MHIOC_RESV_KEY_SIZE);
20810 prp->aptpl = ptr->aptpl;
20811 break;
20812 }
20813 case SD_SCSI3_CLEAR: {
20814 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20815
20816 bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20817 break;
20818 }
20819 case SD_SCSI3_RESERVE:
20820 case SD_SCSI3_RELEASE: {
20821 mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20976 * if the device supports SYNC_NV bit, turn on
20977 * the SYNC_NV bit to only flush volatile cache
20978 */
20979 cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
20980 }
20981 mutex_exit(SD_MUTEX(un));
20982
20983 /*
20984 * First get some memory for the uscsi_cmd struct and cdb
20985 * and initialize for SYNCHRONIZE_CACHE cmd.
20986 */
20987 uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20988 uscmd->uscsi_cdblen = CDB_GROUP1;
20989 uscmd->uscsi_cdb = (caddr_t)cdb;
20990 uscmd->uscsi_bufaddr = NULL;
20991 uscmd->uscsi_buflen = 0;
20992 uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20993 uscmd->uscsi_rqlen = SENSE_LENGTH;
20994 uscmd->uscsi_rqresid = SENSE_LENGTH;
20995 uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20996 uscmd->uscsi_timeout = un->un_cmd_timeout;
20997
20998 /*
20999 * Allocate an sd_uscsi_info struct and fill it with the info
21000 * needed by sd_initpkt_for_uscsi(). Then put the pointer into
21001 * b_private in the buf for sd_initpkt_for_uscsi(). Note that
21002 * since we allocate the buf here in this function, we do not
21003 * need to preserve the prior contents of b_private.
21004 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21005 */
21006 uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21007 uip->ui_flags = SD_PATH_DIRECT;
21008 uip->ui_cmdp = uscmd;
21009
21010 bp = getrbuf(KM_SLEEP);
21011 bp->b_private = uip;
21012
21013 /*
21014 * Setup buffer to carry uscsi request.
21015 */
21016 bp->b_flags = B_BUSY;
21123 /*
21124 * Turn on the un_f_sync_cache_required flag
21125 * since the SYNC CACHE command failed
21126 */
21127 mutex_enter(SD_MUTEX(un));
21128 un->un_f_sync_cache_required = TRUE;
21129 mutex_exit(SD_MUTEX(un));
21130
21131 /*
21132 * Don't log an error message if this device
21133 * has removable media.
21134 */
21135 if (!un->un_f_has_removable_media) {
21136 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21137 "SYNCHRONIZE CACHE command failed (%d)\n", status);
21138 }
21139 break;
21140 }
21141
21142 done:
21143 if (uip->ui_dkc.dkc_callback != NULL)
21144 (*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21145
21146 ASSERT((bp->b_flags & B_REMAPPED) == 0);
21147 freerbuf(bp);
21148 kmem_free(uip, sizeof (struct sd_uscsi_info));
21149 kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21150 kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21151 kmem_free(uscmd, sizeof (struct uscsi_cmd));
21152
21153 return (status);
21154 }
21155
21156 /*
21157 * Issues a single SCSI UNMAP command with a prepared UNMAP parameter list.
21158 * Returns zero on success, or the non-zero command error code on failure.
21159 */
21160 static int
21161 sd_send_scsi_UNMAP_issue_one(sd_ssc_t *ssc, unmap_param_hdr_t *uph,
21162 uint64_t num_descr, uint64_t bytes)
21163 {
21164 struct sd_lun *un = ssc->ssc_un;
21165 struct scsi_extended_sense sense_buf;
21166 union scsi_cdb cdb;
21167 struct uscsi_cmd ucmd_buf;
21168 int status;
21169 const uint64_t param_size = sizeof (unmap_param_hdr_t) +
21170 num_descr * sizeof (unmap_blk_descr_t);
21171
21172 uph->uph_data_len = BE_16(param_size - 2);
21173 uph->uph_descr_data_len = BE_16(param_size - 8);
21174
21175 bzero(&cdb, sizeof (cdb));
21176 bzero(&ucmd_buf, sizeof (ucmd_buf));
21177 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21178
21179 cdb.scc_cmd = SCMD_UNMAP;
21180 FORMG1COUNT(&cdb, param_size);
21181
21182 ucmd_buf.uscsi_cdb = (char *)&cdb;
21183 ucmd_buf.uscsi_cdblen = (uchar_t)CDB_GROUP1;
21184 ucmd_buf.uscsi_bufaddr = (caddr_t)uph;
21185 ucmd_buf.uscsi_buflen = param_size;
21186 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21187 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21188 ucmd_buf.uscsi_flags = USCSI_WRITE | USCSI_RQENABLE | USCSI_SILENT;
21189 ucmd_buf.uscsi_timeout = un->un_cmd_timeout;
21190
21191 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL, UIO_SYSSPACE,
21192 SD_PATH_STANDARD);
21193
21194 switch (status) {
21195 case 0:
21196 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21197
21198 if (un->un_unmapstats) {
21199 atomic_inc_64(&un->un_unmapstats->us_cmds.value.ui64);
21200 atomic_add_64(&un->un_unmapstats->us_extents.value.ui64,
21201 num_descr);
21202 atomic_add_64(&un->un_unmapstats->us_bytes.value.ui64,
21203 bytes);
21204 }
21205 break; /* Success! */
21206 case EIO:
21207 if (un->un_unmapstats)
21208 atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21209 switch (ucmd_buf.uscsi_status) {
21210 case STATUS_RESERVATION_CONFLICT:
21211 status = EACCES;
21212 break;
21213 default:
21214 break;
21215 }
21216 break;
21217 default:
21218 if (un->un_unmapstats)
21219 atomic_inc_64(&un->un_unmapstats->us_errs.value.ui64);
21220 break;
21221 }
21222
21223 return (status);
21224 }
21225
21226 /*
21227 * Returns a pointer to the i'th block descriptor inside an UNMAP param list.
21228 */
21229 static inline unmap_blk_descr_t *
21230 UNMAP_blk_descr_i(void *buf, uint64_t i)
21231 {
21232 return ((unmap_blk_descr_t *)((uint8_t *)buf +
21233 sizeof (unmap_param_hdr_t) + (i * sizeof (unmap_blk_descr_t))));
21234 }
21235
21236 /*
21237 * Takes the list of extents from sd_send_scsi_UNMAP, chops it up, prepares
21238 * UNMAP block descriptors and issues individual SCSI UNMAP commands. While
21239 * doing so we consult the block limits to determine at most how many
21240 * extents and LBAs we can UNMAP in one command.
21241 * If a command fails for whatever, reason, extent list processing is aborted
21242 * and the failed command's status is returned. Otherwise returns 0 on
21243 * success.
21244 */
21245 static int
21246 sd_send_scsi_UNMAP_issue(dev_t dev, sd_ssc_t *ssc, const dkioc_free_list_t *dfl)
21247 {
21248 struct sd_lun *un = ssc->ssc_un;
21249 unmap_param_hdr_t *uph;
21250 sd_blk_limits_t *lim = &un->un_blk_lim;
21251 int rval = 0;
21252 int partition;
21253 /* partition offset & length in system blocks */
21254 diskaddr_t part_off_sysblks = 0, part_len_sysblks = 0;
21255 uint64_t part_off, part_len;
21256 uint64_t descr_cnt_lim, byte_cnt_lim;
21257 uint64_t descr_issued = 0, bytes_issued = 0;
21258
21259 uph = kmem_zalloc(SD_UNMAP_PARAM_LIST_MAXSZ, KM_SLEEP);
21260
21261 partition = SDPART(dev);
21262 (void) cmlb_partinfo(un->un_cmlbhandle, partition, &part_len_sysblks,
21263 &part_off_sysblks, NULL, NULL, (void *)SD_PATH_DIRECT);
21264 part_off = SD_SYSBLOCKS2BYTES(part_off_sysblks);
21265 part_len = SD_SYSBLOCKS2BYTES(part_len_sysblks);
21266
21267 ASSERT(un->un_blk_lim.lim_max_unmap_lba_cnt != 0);
21268 ASSERT(un->un_blk_lim.lim_max_unmap_descr_cnt != 0);
21269 /* Spec says 0xffffffff are special values, so compute maximums. */
21270 byte_cnt_lim = lim->lim_max_unmap_lba_cnt < UINT32_MAX ?
21271 (uint64_t)lim->lim_max_unmap_lba_cnt * un->un_tgt_blocksize :
21272 UINT64_MAX;
21273 descr_cnt_lim = MIN(lim->lim_max_unmap_descr_cnt, SD_UNMAP_MAX_DESCR);
21274
21275 for (size_t i = 0; i < dfl->dfl_num_exts; i++) {
21276 const dkioc_free_list_ext_t *ext = &dfl->dfl_exts[i];
21277 uint64_t ext_start = ext->dfle_start;
21278 uint64_t ext_length = ext->dfle_length;
21279
21280 while (ext_length > 0) {
21281 unmap_blk_descr_t *ubd;
21282 /* Respect device limit on LBA count per command */
21283 uint64_t len = MIN(MIN(ext_length, byte_cnt_lim -
21284 bytes_issued), SD_TGTBLOCKS2BYTES(un, UINT32_MAX));
21285
21286 /* check partition limits */
21287 if (ext_start + len > part_len) {
21288 rval = SET_ERROR(EINVAL);
21289 goto out;
21290 }
21291 #ifdef DEBUG
21292 if (dfl->dfl_ck_func)
21293 dfl->dfl_ck_func(dfl->dfl_offset + ext_start,
21294 len, dfl->dfl_ck_arg);
21295 #endif
21296 ASSERT3U(descr_issued, <, descr_cnt_lim);
21297 ASSERT3U(bytes_issued, <, byte_cnt_lim);
21298 ubd = UNMAP_blk_descr_i(uph, descr_issued);
21299
21300 /* adjust in-partition addresses to be device-global */
21301 ubd->ubd_lba = BE_64(SD_BYTES2TGTBLOCKS(un,
21302 dfl->dfl_offset + ext_start + part_off));
21303 ubd->ubd_lba_cnt = BE_32(SD_BYTES2TGTBLOCKS(un, len));
21304
21305 descr_issued++;
21306 bytes_issued += len;
21307
21308 /* Issue command when device limits reached */
21309 if (descr_issued == descr_cnt_lim ||
21310 bytes_issued == byte_cnt_lim) {
21311 rval = sd_send_scsi_UNMAP_issue_one(ssc, uph,
21312 descr_issued, bytes_issued);
21313 if (rval != 0)
21314 goto out;
21315 descr_issued = 0;
21316 bytes_issued = 0;
21317 }
21318
21319 ext_start += len;
21320 ext_length -= len;
21321 }
21322 }
21323
21324 if (descr_issued > 0) {
21325 /* issue last command */
21326 rval = sd_send_scsi_UNMAP_issue_one(ssc, uph, descr_issued,
21327 bytes_issued);
21328 }
21329
21330 out:
21331 kmem_free(uph, SD_UNMAP_PARAM_LIST_MAXSZ);
21332 return (rval);
21333 }
21334
21335 /*
21336 * Issues one or several UNMAP commands based on a list of extents to be
21337 * unmapped. The internal multi-command processing is hidden, as the exact
21338 * number of commands and extents per command is limited by both SCSI
21339 * command syntax and device limits (as expressed in the SCSI Block Limits
21340 * VPD page and un_blk_lim in struct sd_lun).
21341 * Returns zero on success, or the error code of the first failed SCSI UNMAP
21342 * command.
21343 */
21344 static int
21345 sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl, int flag)
21346 {
21347 struct sd_lun *un = ssc->ssc_un;
21348 int rval = 0;
21349
21350 ASSERT(!mutex_owned(SD_MUTEX(un)));
21351 ASSERT(dfl != NULL);
21352
21353 /* Per spec, any of these conditions signals lack of UNMAP support. */
21354 if (!(un->un_thin_flags & SD_THIN_PROV_ENABLED) ||
21355 un->un_blk_lim.lim_max_unmap_descr_cnt == 0 ||
21356 un->un_blk_lim.lim_max_unmap_lba_cnt == 0) {
21357 return (SET_ERROR(ENOTSUP));
21358 }
21359
21360 /* For userspace calls we must copy in. */
21361 if (!(flag & FKIOCTL) && (dfl = dfl_copyin(dfl, flag, KM_SLEEP)) ==
21362 NULL)
21363 return (SET_ERROR(EFAULT));
21364
21365 rval = sd_send_scsi_UNMAP_issue(dev, ssc, dfl);
21366
21367 if (!(flag & FKIOCTL)) {
21368 dfl_free(dfl);
21369 dfl = NULL;
21370 }
21371
21372 return (rval);
21373 }
21374
21375 /*
21376 * Function: sd_send_scsi_GET_CONFIGURATION
21377 *
21378 * Description: Issues the get configuration command to the device.
21379 * Called from sd_check_for_writable_cd & sd_get_media_info
21380 * caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21381 * Arguments: ssc
21382 * ucmdbuf
21383 * rqbuf
21384 * rqbuflen
21385 * bufaddr
21386 * buflen
21387 * path_flag
21388 *
21389 * Return Code: 0 - Success
21390 * errno return code from sd_ssc_send()
21391 *
21392 * Context: Can sleep. Does not return until command is completed.
21393 *
21394 */
21395
21411 ASSERT(rqbuf != NULL);
21412
21413 SD_TRACE(SD_LOG_IO, un,
21414 "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21415
21416 bzero(cdb, sizeof (cdb));
21417 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21418 bzero(rqbuf, rqbuflen);
21419 bzero(bufaddr, buflen);
21420
21421 /*
21422 * Set up cdb field for the get configuration command.
21423 */
21424 cdb[0] = SCMD_GET_CONFIGURATION;
21425 cdb[1] = 0x02; /* Requested Type */
21426 cdb[8] = SD_PROFILE_HEADER_LEN;
21427 ucmdbuf->uscsi_cdb = cdb;
21428 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21429 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21430 ucmdbuf->uscsi_buflen = buflen;
21431 ucmdbuf->uscsi_timeout = un->un_uscsi_timeout;
21432 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21433 ucmdbuf->uscsi_rqlen = rqbuflen;
21434 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21435
21436 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21437 UIO_SYSSPACE, path_flag);
21438
21439 switch (status) {
21440 case 0:
21441 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21442 break; /* Success! */
21443 case EIO:
21444 switch (ucmdbuf->uscsi_status) {
21445 case STATUS_RESERVATION_CONFLICT:
21446 status = EACCES;
21447 break;
21448 default:
21449 break;
21450 }
21451 break;
21504
21505 SD_TRACE(SD_LOG_IO, un,
21506 "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21507
21508 bzero(cdb, sizeof (cdb));
21509 bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21510 bzero(rqbuf, rqbuflen);
21511 bzero(bufaddr, buflen);
21512
21513 /*
21514 * Set up cdb field for the get configuration command.
21515 */
21516 cdb[0] = SCMD_GET_CONFIGURATION;
21517 cdb[1] = 0x02; /* Requested Type */
21518 cdb[3] = feature;
21519 cdb[8] = buflen;
21520 ucmdbuf->uscsi_cdb = cdb;
21521 ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21522 ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21523 ucmdbuf->uscsi_buflen = buflen;
21524 ucmdbuf->uscsi_timeout = un->un_uscsi_timeout;
21525 ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21526 ucmdbuf->uscsi_rqlen = rqbuflen;
21527 ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21528
21529 status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21530 UIO_SYSSPACE, path_flag);
21531
21532 switch (status) {
21533 case 0:
21534
21535 break; /* Success! */
21536 case EIO:
21537 switch (ucmdbuf->uscsi_status) {
21538 case STATUS_RESERVATION_CONFLICT:
21539 status = EACCES;
21540 break;
21541 default:
21542 break;
21543 }
21544 break;
21617 cdb.cdb_opaque[2] = page_code;
21618 FORMG0COUNT(&cdb, buflen);
21619 headlen = MODE_HEADER_LENGTH;
21620 } else {
21621 cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21622 cdb.cdb_opaque[2] = page_code;
21623 FORMG1COUNT(&cdb, buflen);
21624 headlen = MODE_HEADER_LENGTH_GRP2;
21625 }
21626
21627 ASSERT(headlen <= buflen);
21628 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21629
21630 ucmd_buf.uscsi_cdb = (char *)&cdb;
21631 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21632 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21633 ucmd_buf.uscsi_buflen = buflen;
21634 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21635 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21636 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21637 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
21638
21639 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21640 UIO_SYSSPACE, path_flag);
21641
21642 switch (status) {
21643 case 0:
21644 /*
21645 * sr_check_wp() uses 0x3f page code and check the header of
21646 * mode page to determine if target device is write-protected.
21647 * But some USB devices return 0 bytes for 0x3f page code. For
21648 * this case, make sure that mode page header is returned at
21649 * least.
21650 */
21651 if (buflen - ucmd_buf.uscsi_resid < headlen) {
21652 status = EIO;
21653 sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21654 "mode page header is not returned");
21655 }
21656 break; /* Success! */
21657 case EIO:
21736 cdb.cdb_opaque[1] |= 0x01;
21737 }
21738
21739 if (cdbsize == CDB_GROUP0) {
21740 cdb.scc_cmd = SCMD_MODE_SELECT;
21741 FORMG0COUNT(&cdb, buflen);
21742 } else {
21743 cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21744 FORMG1COUNT(&cdb, buflen);
21745 }
21746
21747 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21748
21749 ucmd_buf.uscsi_cdb = (char *)&cdb;
21750 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21751 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21752 ucmd_buf.uscsi_buflen = buflen;
21753 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21754 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21755 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21756 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
21757
21758 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21759 UIO_SYSSPACE, path_flag);
21760
21761 switch (status) {
21762 case 0:
21763 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21764 break; /* Success! */
21765 case EIO:
21766 switch (ucmd_buf.uscsi_status) {
21767 case STATUS_RESERVATION_CONFLICT:
21768 status = EACCES;
21769 break;
21770 default:
21771 break;
21772 }
21773 break;
21774 default:
21775 break;
21776 }
21874 cdb.scc_cmd = cmd | SCMD_GROUP4;
21875 FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21876 FORMG4COUNT(&cdb, block_count);
21877 break;
21878 case CDB_GROUP5: /* 12-byte CDBs (currently unsupported) */
21879 default:
21880 /* All others reserved */
21881 return (EINVAL);
21882 }
21883
21884 /* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21885 SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21886
21887 ucmd_buf.uscsi_cdb = (char *)&cdb;
21888 ucmd_buf.uscsi_cdblen = (uchar_t)cdbsize;
21889 ucmd_buf.uscsi_bufaddr = bufaddr;
21890 ucmd_buf.uscsi_buflen = buflen;
21891 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21892 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21893 ucmd_buf.uscsi_flags = flag | USCSI_RQENABLE | USCSI_SILENT;
21894 ucmd_buf.uscsi_timeout = un->un_cmd_timeout;
21895 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21896 UIO_SYSSPACE, path_flag);
21897
21898 switch (status) {
21899 case 0:
21900 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21901 break; /* Success! */
21902 case EIO:
21903 switch (ucmd_buf.uscsi_status) {
21904 case STATUS_RESERVATION_CONFLICT:
21905 status = EACCES;
21906 break;
21907 default:
21908 break;
21909 }
21910 break;
21911 default:
21912 break;
21913 }
21914
21954
21955 SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21956
21957 bzero(&cdb, sizeof (cdb));
21958 bzero(&ucmd_buf, sizeof (ucmd_buf));
21959 bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21960
21961 cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21962 cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21963 cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21964 cdb.cdb_opaque[6] = (uchar_t)(param_ptr & 0x00FF);
21965 FORMG1COUNT(&cdb, buflen);
21966
21967 ucmd_buf.uscsi_cdb = (char *)&cdb;
21968 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
21969 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
21970 ucmd_buf.uscsi_buflen = buflen;
21971 ucmd_buf.uscsi_rqbuf = (caddr_t)&sense_buf;
21972 ucmd_buf.uscsi_rqlen = sizeof (struct scsi_extended_sense);
21973 ucmd_buf.uscsi_flags = USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21974 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
21975
21976 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21977 UIO_SYSSPACE, path_flag);
21978
21979 switch (status) {
21980 case 0:
21981 break;
21982 case EIO:
21983 switch (ucmd_buf.uscsi_status) {
21984 case STATUS_RESERVATION_CONFLICT:
21985 status = EACCES;
21986 break;
21987 case STATUS_CHECK:
21988 if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21989 (scsi_sense_key((uint8_t *)&sense_buf) ==
21990 KEY_ILLEGAL_REQUEST) &&
21991 (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21992 /*
21993 * ASC 0x24: INVALID FIELD IN CDB
21994 */
22086
22087 SD_TRACE(SD_LOG_IO, un,
22088 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22089
22090 bzero(&cdb, sizeof (cdb));
22091 bzero(&ucmd_buf, sizeof (ucmd_buf));
22092 bzero(bufaddr, buflen);
22093
22094 cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22095 cdb.cdb_opaque[1] = 1; /* polled */
22096 cdb.cdb_opaque[4] = class_req;
22097 FORMG1COUNT(&cdb, buflen);
22098
22099 ucmd_buf.uscsi_cdb = (char *)&cdb;
22100 ucmd_buf.uscsi_cdblen = CDB_GROUP1;
22101 ucmd_buf.uscsi_bufaddr = (caddr_t)bufaddr;
22102 ucmd_buf.uscsi_buflen = buflen;
22103 ucmd_buf.uscsi_rqbuf = NULL;
22104 ucmd_buf.uscsi_rqlen = 0;
22105 ucmd_buf.uscsi_flags = USCSI_READ | USCSI_SILENT;
22106 ucmd_buf.uscsi_timeout = un->un_uscsi_timeout;
22107
22108 status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22109 UIO_SYSSPACE, SD_PATH_DIRECT);
22110
22111 /*
22112 * Only handle status == 0, the upper-level caller
22113 * will put different assessment based on the context.
22114 */
22115 if (status == 0) {
22116 sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22117
22118 if (ucmd_buf.uscsi_resid != 0) {
22119 status = EIO;
22120 }
22121 }
22122
22123 SD_TRACE(SD_LOG_IO, un,
22124 "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22125
22126 return (status);
22168 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22169 {
22170 struct sd_lun *un = NULL;
22171 int err = 0;
22172 int i = 0;
22173 cred_t *cr;
22174 int tmprval = EINVAL;
22175 boolean_t is_valid;
22176 sd_ssc_t *ssc;
22177
22178 /*
22179 * All device accesses go thru sdstrategy where we check on suspend
22180 * status
22181 */
22182 if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22183 return (ENXIO);
22184 }
22185
22186 ASSERT(!mutex_owned(SD_MUTEX(un)));
22187
22188 /*
22189 * Moved this wait from sd_uscsi_strategy to here for
22190 * reasons of deadlock prevention. Internal driver commands,
22191 * specifically those to change a devices power level, result
22192 * in a call to sd_uscsi_strategy.
22193 */
22194 mutex_enter(SD_MUTEX(un));
22195 while ((un->un_state == SD_STATE_SUSPENDED) ||
22196 (un->un_state == SD_STATE_PM_CHANGING) ||
22197 (un->un_state == SD_STATE_ATTACHING)) {
22198 cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22199 }
22200
22201 if (un->un_state == SD_STATE_ATTACH_FAILED) {
22202 mutex_exit(SD_MUTEX(un));
22203 SD_ERROR(SD_LOG_READ_WRITE, un,
22204 "sdioctl: attach failed\n");
22205 return (EIO);
22206 }
22207
22208 /*
22209 * Twiddling the counter here protects commands from now
22210 * through to the top of sd_uscsi_strategy. Without the
22211 * counter inc. a power down, for example, could get in
22212 * after the above check for state is made and before
22213 * execution gets to the top of sd_uscsi_strategy.
22214 * That would cause problems.
22215 */
22216 un->un_ncmds_in_driver++;
22217 mutex_exit(SD_MUTEX(un));
22218
22219 /* Initialize sd_ssc_t for internal uscsi commands */
22220 ssc = sd_ssc_init(un);
22221
22222 is_valid = SD_IS_VALID_LABEL(un);
22223
22224 mutex_enter(SD_MUTEX(un));
22225
22226 if (!is_valid &&
22227 (flag & (FNDELAY | FNONBLOCK))) {
22228 switch (cmd) {
22229 case DKIOCGGEOM: /* SD_PATH_DIRECT */
22230 case DKIOCGVTOC:
22231 case DKIOCGEXTVTOC:
22232 case DKIOCGAPART:
22233 case DKIOCPARTINFO:
22234 case DKIOCEXTPARTINFO:
22235 case DKIOCSGEOM:
22236 case DKIOCSAPART:
22237 case DKIOCGETEFI:
22238 case DKIOCPARTITION:
22239 case DKIOCSVTOC:
22240 case DKIOCSEXTVTOC:
22241 case DKIOCSETEFI:
22242 case DKIOCGMBOOT:
22243 case DKIOCSMBOOT:
22244 case DKIOCG_PHYGEOM:
22245 case DKIOCG_VIRTGEOM:
22246 case DKIOCSETEXTPART:
22247 /* let cmlb handle it */
22248 goto skip_ready_valid;
22249 case CDROMPAUSE:
22250 case CDROMRESUME:
22251 case CDROMPLAYMSF:
22252 case CDROMPLAYTRKIND:
22253 case CDROMREADTOCHDR:
22254 case CDROMREADTOCENTRY:
22255 case CDROMSTOP:
22256 case CDROMSTART:
22257 case CDROMVOLCTRL:
22258 case CDROMSUBCHNL:
22259 case CDROMREADMODE2:
22260 case CDROMREADMODE1:
22261 case CDROMREADOFFSET:
22262 case CDROMSBLKMODE:
22263 case CDROMGBLKMODE:
22264 case CDROMGDRVSPEED:
22265 case CDROMSDRVSPEED:
22266 case CDROMCDDA:
22267 case CDROMCDXA:
22268 case CDROMSUBCODE:
22269 if (!ISCD(un)) {
22270 un->un_ncmds_in_driver--;
22271 ASSERT(un->un_ncmds_in_driver >= 0);
22272 if (un->un_f_detach_waiting)
22273 cv_signal(&un->un_detach_cv);
22274 mutex_exit(SD_MUTEX(un));
22275 err = ENOTTY;
22276 goto done_without_assess;
22277 }
22278 break;
22279 case FDEJECT:
22280 case DKIOCEJECT:
22281 case CDROMEJECT:
22282 if (!un->un_f_eject_media_supported) {
22283 un->un_ncmds_in_driver--;
22284 ASSERT(un->un_ncmds_in_driver >= 0);
22285 if (un->un_f_detach_waiting)
22286 cv_signal(&un->un_detach_cv);
22287 mutex_exit(SD_MUTEX(un));
22288 err = ENOTTY;
22289 goto done_without_assess;
22290 }
22291 break;
22292 case DKIOCFLUSHWRITECACHE:
22293 mutex_exit(SD_MUTEX(un));
22294 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22295 if (err != 0) {
22296 mutex_enter(SD_MUTEX(un));
22297 un->un_ncmds_in_driver--;
22298 ASSERT(un->un_ncmds_in_driver >= 0);
22299 if (un->un_f_detach_waiting)
22300 cv_signal(&un->un_detach_cv);
22301 mutex_exit(SD_MUTEX(un));
22302 err = EIO;
22303 goto done_quick_assess;
22304 }
22305 mutex_enter(SD_MUTEX(un));
22306 /* FALLTHROUGH */
22307 case DKIOCREMOVABLE:
22308 case DKIOCHOTPLUGGABLE:
22309 case DKIOCINFO:
22310 case DKIOCGMEDIAINFO:
22311 case DKIOCGMEDIAINFOEXT:
22312 case DKIOCSOLIDSTATE:
22313 case MHIOCENFAILFAST:
22314 case MHIOCSTATUS:
22315 case MHIOCTKOWN:
22316 case MHIOCRELEASE:
22317 case MHIOCGRP_INKEYS:
22318 case MHIOCGRP_INRESV:
22319 case MHIOCGRP_REGISTER:
22320 case MHIOCGRP_CLEAR:
22339 case CDROMSDRVSPEED:
22340 case FDEJECT: /* for eject command */
22341 case DKIOCEJECT:
22342 case CDROMEJECT:
22343 case DKIOCREMOVABLE:
22344 case DKIOCHOTPLUGGABLE:
22345 break;
22346 default:
22347 if (un->un_f_has_removable_media) {
22348 err = ENXIO;
22349 } else {
22350 /* Do not map SD_RESERVED_BY_OTHERS to EIO */
22351 if (err == SD_RESERVED_BY_OTHERS) {
22352 err = EACCES;
22353 } else {
22354 err = EIO;
22355 }
22356 }
22357 un->un_ncmds_in_driver--;
22358 ASSERT(un->un_ncmds_in_driver >= 0);
22359 if (un->un_f_detach_waiting)
22360 cv_signal(&un->un_detach_cv);
22361 mutex_exit(SD_MUTEX(un));
22362
22363 goto done_without_assess;
22364 }
22365 }
22366 }
22367
22368 skip_ready_valid:
22369 mutex_exit(SD_MUTEX(un));
22370
22371 switch (cmd) {
22372 case DKIOCINFO:
22373 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22374 err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22375 break;
22376
22377 case DKIOCGMEDIAINFO:
22378 SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22379 err = sd_get_media_info(dev, (caddr_t)arg, flag);
22380 break;
22384 err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22385 break;
22386
22387 case DKIOCGGEOM:
22388 case DKIOCGVTOC:
22389 case DKIOCGEXTVTOC:
22390 case DKIOCGAPART:
22391 case DKIOCPARTINFO:
22392 case DKIOCEXTPARTINFO:
22393 case DKIOCSGEOM:
22394 case DKIOCSAPART:
22395 case DKIOCGETEFI:
22396 case DKIOCPARTITION:
22397 case DKIOCSVTOC:
22398 case DKIOCSEXTVTOC:
22399 case DKIOCSETEFI:
22400 case DKIOCGMBOOT:
22401 case DKIOCSMBOOT:
22402 case DKIOCG_PHYGEOM:
22403 case DKIOCG_VIRTGEOM:
22404 case DKIOCSETEXTPART:
22405 SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22406
22407 /* TUR should spin up */
22408
22409 if (un->un_f_has_removable_media)
22410 err = sd_send_scsi_TEST_UNIT_READY(ssc,
22411 SD_CHECK_FOR_MEDIA);
22412
22413 else
22414 err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22415
22416 if (err != 0)
22417 goto done_with_assess;
22418
22419 err = cmlb_ioctl(un->un_cmlbhandle, dev,
22420 cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22421
22422 if ((err == 0) &&
22423 ((cmd == DKIOCSETEFI) ||
22424 ((un->un_f_pkstats_enabled) &&
22763 case CDROMRESUME:
22764 SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22765 if (!ISCD(un)) {
22766 err = ENOTTY;
22767 } else {
22768 err = sr_pause_resume(dev, cmd);
22769 }
22770 break;
22771
22772 case CDROMPLAYMSF:
22773 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22774 if (!ISCD(un)) {
22775 err = ENOTTY;
22776 } else {
22777 err = sr_play_msf(dev, (caddr_t)arg, flag);
22778 }
22779 break;
22780
22781 case CDROMPLAYTRKIND:
22782 SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22783 /*
22784 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22785 */
22786 if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22787 err = ENOTTY;
22788 } else {
22789 err = sr_play_trkind(dev, (caddr_t)arg, flag);
22790 }
22791 break;
22792
22793 case CDROMREADTOCHDR:
22794 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22795 if (!ISCD(un)) {
22796 err = ENOTTY;
22797 } else {
22798 err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22799 }
22800 break;
22801
22802 case CDROMREADTOCENTRY:
22803 SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22804 if (!ISCD(un)) {
22805 err = ENOTTY;
22806 } else {
23057 SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23058 if (scsi_abort(SD_ADDRESS(un), NULL)) {
23059 err = 0;
23060 } else {
23061 err = EIO;
23062 }
23063 break;
23064 #endif
23065
23066 #ifdef SD_FAULT_INJECTION
23067 /* SDIOC FaultInjection testing ioctls */
23068 case SDIOCSTART:
23069 case SDIOCSTOP:
23070 case SDIOCINSERTPKT:
23071 case SDIOCINSERTXB:
23072 case SDIOCINSERTUN:
23073 case SDIOCINSERTARQ:
23074 case SDIOCPUSH:
23075 case SDIOCRETRIEVE:
23076 case SDIOCRUN:
23077 case SDIOCINSERTTRAN:
23078 SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23079 "SDIOC detected cmd:0x%X:\n", cmd);
23080 /* call error generator */
23081 err = sd_faultinjection_ioctl(cmd, arg, un);
23082 break;
23083
23084 #endif /* SD_FAULT_INJECTION */
23085
23086 case DKIOCFLUSHWRITECACHE:
23087 {
23088 struct dk_callback *dkc = (struct dk_callback *)arg;
23089
23090 mutex_enter(SD_MUTEX(un));
23091 if (!un->un_f_sync_cache_supported ||
23092 !un->un_f_write_cache_enabled) {
23093 err = un->un_f_sync_cache_supported ?
23094 0 : ENOTSUP;
23095 mutex_exit(SD_MUTEX(un));
23096 if ((flag & FKIOCTL) && dkc != NULL &&
23097 dkc->dkc_callback != NULL) {
23098 (*dkc->dkc_callback)(dkc->dkc_cookie,
23099 err);
23100 /*
23101 * Did callback and reported error.
23102 * Since we did a callback, ioctl
23103 * should return 0.
23104 */
23105 err = 0;
23106 }
23107 break;
23108 }
23109 mutex_exit(SD_MUTEX(un));
23110
23111 if ((flag & FKIOCTL) && dkc != NULL &&
23112 dkc->dkc_callback != NULL) {
23113 /* async SYNC CACHE request */
23114 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23115 } else {
23116 /* synchronous SYNC CACHE request */
23117 err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23118 }
23119 }
23120 break;
23121
23122 case DKIOCFREE:
23123 {
23124 dkioc_free_list_t *dfl = (dkioc_free_list_t *)arg;
23125
23126 /* bad userspace ioctls shouldn't panic */
23127 if (dfl == NULL && !(flag & FKIOCTL)) {
23128 err = SET_ERROR(EINVAL);
23129 break;
23130 }
23131 /* synchronous UNMAP request */
23132 err = sd_send_scsi_UNMAP(dev, ssc, dfl, flag);
23133 }
23134 break;
23135
23136 case DKIOCGETWCE: {
23137
23138 int wce;
23139
23140 if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23141 break;
23142 }
23143
23144 if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23145 err = EFAULT;
23146 }
23147 break;
23148 }
23149
23150 case DKIOCSETWCE: {
23151
23152 int wce, sync_supported;
23153 int cur_wce = 0;
23154
23155 if (!un->un_f_cache_mode_changeable) {
23261 mutex_enter(SD_MUTEX(un));
23262
23263 if (err) {
23264 un->un_f_write_cache_enabled = 0;
23265 }
23266 }
23267
23268 un->un_f_wcc_inprog = 0;
23269 cv_broadcast(&un->un_wcc_cv);
23270 mutex_exit(SD_MUTEX(un));
23271 break;
23272 }
23273
23274 default:
23275 err = ENOTTY;
23276 break;
23277 }
23278 mutex_enter(SD_MUTEX(un));
23279 un->un_ncmds_in_driver--;
23280 ASSERT(un->un_ncmds_in_driver >= 0);
23281 if (un->un_f_detach_waiting)
23282 cv_signal(&un->un_detach_cv);
23283 mutex_exit(SD_MUTEX(un));
23284
23285
23286 done_without_assess:
23287 sd_ssc_fini(ssc);
23288
23289 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23290 return (err);
23291
23292 done_with_assess:
23293 mutex_enter(SD_MUTEX(un));
23294 un->un_ncmds_in_driver--;
23295 ASSERT(un->un_ncmds_in_driver >= 0);
23296 if (un->un_f_detach_waiting)
23297 cv_signal(&un->un_detach_cv);
23298 mutex_exit(SD_MUTEX(un));
23299
23300 done_quick_assess:
23301 if (err != 0)
23302 sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23303 /* Uninitialize sd_ssc_t pointer */
23304 sd_ssc_fini(ssc);
23305
23306 SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23307 return (err);
23308 }
23309
23310
23311 /*
23312 * Function: sd_dkio_ctrl_info
23313 *
23314 * Description: This routine is the driver entry point for handling controller
23315 * information ioctl requests (DKIOCINFO).
23316 *
23317 * Arguments: dev - the device number
23382 return (EFAULT);
23383 } else {
23384 kmem_free(info, sizeof (struct dk_cinfo));
23385 return (0);
23386 }
23387 }
23388
23389 /*
23390 * Function: sd_get_media_info_com
23391 *
23392 * Description: This routine returns the information required to populate
23393 * the fields for the dk_minfo/dk_minfo_ext structures.
23394 *
23395 * Arguments: dev - the device number
23396 * dki_media_type - media_type
23397 * dki_lbsize - logical block size
23398 * dki_capacity - capacity in blocks
23399 * dki_pbsize - physical block size (if requested)
23400 *
23401 * Return Code: 0
23402 * EACCES
23403 * EFAULT
23404 * ENXIO
23405 * EIO
23406 */
23407 static int
23408 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23409 diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23410 {
23411 struct sd_lun *un = NULL;
23412 struct uscsi_cmd com;
23413 struct scsi_inquiry *sinq;
23414 u_longlong_t media_capacity;
23415 uint64_t capacity;
23416 uint_t lbasize;
23417 uint_t pbsize;
23418 uchar_t *out_data;
23419 uchar_t *rqbuf;
23420 int rval = 0;
23421 int rtn;
23422 sd_ssc_t *ssc;
23500 *dki_media_type = DK_JAZ;
23501 }
23502 }
23503 } else {
23504 /*
23505 * Not a CD, direct access or optical disk so return
23506 * unknown media
23507 */
23508 *dki_media_type = DK_UNKNOWN;
23509 }
23510 }
23511
23512 /*
23513 * Now read the capacity so we can provide the lbasize,
23514 * pbsize and capacity.
23515 */
23516 if (dki_pbsize && un->un_f_descr_format_supported) {
23517 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23518 &pbsize, SD_PATH_DIRECT);
23519
23520 if (un->un_f_sdconf_phy_blocksize) /* keep sd.conf's pbs */
23521 pbsize = un->un_phy_blocksize;
23522 else /* override the pbs if the instance has a larger value */
23523 pbsize = MAX(pbsize, un->un_phy_blocksize);
23524 }
23525
23526 if (dki_pbsize == NULL || rval != 0 ||
23527 !un->un_f_descr_format_supported) {
23528 rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23529 SD_PATH_DIRECT);
23530
23531 switch (rval) {
23532 case 0:
23533 if (un->un_f_enable_rmw &&
23534 un->un_phy_blocksize != 0) {
23535 pbsize = un->un_phy_blocksize;
23536 } else {
23537 pbsize = lbasize;
23538 }
23539 media_capacity = capacity;
23540
23541 /*
23542 * sd_send_scsi_READ_CAPACITY() reports capacity in
23763
23764 sd_pm_exit(un);
23765
23766 mutex_enter(SD_MUTEX(un));
23767 if (token == NULL) {
23768 rval = EAGAIN;
23769 goto done;
23770 }
23771
23772 /*
23773 * This is a special case IOCTL that doesn't return
23774 * until the media state changes. Routine sdpower
23775 * knows about and handles this so don't count it
23776 * as an active cmd in the driver, which would
23777 * keep the device busy to the pm framework.
23778 * If the count isn't decremented the device can't
23779 * be powered down.
23780 */
23781 un->un_ncmds_in_driver--;
23782 ASSERT(un->un_ncmds_in_driver >= 0);
23783 if (un->un_f_detach_waiting)
23784 cv_signal(&un->un_detach_cv);
23785
23786 /*
23787 * if a prior request had been made, this will be the same
23788 * token, as scsi_watch was designed that way.
23789 */
23790 un->un_swr_token = token;
23791 un->un_specified_mediastate = state;
23792
23793 /*
23794 * now wait for media change
23795 * we will not be signalled unless mediastate == state but it is
23796 * still better to test for this condition, since there is a
23797 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23798 */
23799 SD_TRACE(SD_LOG_COMMON, un,
23800 "sd_check_media: waiting for media state change\n");
23801 while (un->un_mediastate == state) {
23802 if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23803 SD_TRACE(SD_LOG_COMMON, un,
23804 "sd_check_media: waiting for media state "
24258 }
24259
24260 return (rval);
24261 }
24262
24263
24264 /*
24265 * Function: sd_log_page_supported
24266 *
24267 * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24268 * supported log pages.
24269 *
24270 * Arguments: ssc - ssc contains pointer to driver soft state (unit)
24271 * structure for this target.
24272 * log_page -
24273 *
24274 * Return Code: -1 - on error (log sense is optional and may not be supported).
24275 * 0 - log page not found.
24276 * 1 - log page found.
24277 */
24278 #ifdef notyet
24279 static int
24280 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24281 {
24282 uchar_t *log_page_data;
24283 int i;
24284 int match = 0;
24285 int log_size;
24286 int status = 0;
24287 struct sd_lun *un;
24288
24289 ASSERT(ssc != NULL);
24290 un = ssc->ssc_un;
24291 ASSERT(un != NULL);
24292
24293 log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24294
24295 status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24296 SD_PATH_DIRECT);
24297
24298 if (status != 0) {
24323 SD_ERROR(SD_LOG_COMMON, un,
24324 "sd_log_page_supported: failed log page retrieval\n");
24325 kmem_free(log_page_data, 0xFF);
24326 return (-1);
24327 }
24328
24329 log_size = log_page_data[3];
24330
24331 /*
24332 * The list of supported log pages start from the fourth byte. Check
24333 * until we run out of log pages or a match is found.
24334 */
24335 for (i = 4; (i < (log_size + 4)) && !match; i++) {
24336 if (log_page_data[i] == log_page) {
24337 match++;
24338 }
24339 }
24340 kmem_free(log_page_data, 0xFF);
24341 return (match);
24342 }
24343 #endif
24344
24345 /*
24346 * Function: sd_mhdioc_failfast
24347 *
24348 * Description: This routine is the driver entry point for handling ioctl
24349 * requests to enable/disable the multihost failfast option.
24350 * (MHIOCENFAILFAST)
24351 *
24352 * Arguments: dev - the device number
24353 * arg - user specified probing interval.
24354 * flag - this argument is a pass through to ddi_copyxxx()
24355 * directly from the mode argument of ioctl().
24356 *
24357 * Return Code: 0
24358 * EFAULT
24359 * ENXIO
24360 */
24361
24362 static int
24363 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24364 {
25760 int i;
25761 int err;
25762 struct sd_lun *un;
25763 struct scsi_pkt *wr_pktp;
25764 struct buf *wr_bp;
25765 struct buf wr_buf;
25766 daddr_t tgt_byte_offset; /* rmw - byte offset for target */
25767 daddr_t tgt_blkno; /* rmw - blkno for target */
25768 size_t tgt_byte_count; /* rmw - # of bytes to xfer */
25769 size_t tgt_nblk; /* rmw - # of tgt blks to xfer */
25770 size_t io_start_offset;
25771 int doing_rmw = FALSE;
25772 int rval;
25773 ssize_t dma_resid;
25774 daddr_t oblkno;
25775 diskaddr_t nblks = 0;
25776 diskaddr_t start_block;
25777
25778 instance = SDUNIT(dev);
25779 if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25780 (un->un_state == SD_STATE_ATTACHING) ||
25781 (un->un_state == SD_STATE_ATTACH_FAILED) ||
25782 !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25783 return (ENXIO);
25784 }
25785
25786 SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25787
25788 partition = SDPART(dev);
25789 SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25790
25791 if (!(NOT_DEVBSIZE(un))) {
25792 int secmask = 0;
25793 int blknomask = 0;
25794
25795 blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25796 secmask = un->un_tgt_blocksize - 1;
25797
25798 if (blkno & blknomask) {
25799 SD_TRACE(SD_LOG_DUMP, un,
25800 "sddump: dump start block not modulo %d\n",
25801 un->un_tgt_blocksize);
25802 return (EINVAL);
25803 }
25804
25805 if ((nblk * DEV_BSIZE) & secmask) {
28009 }
28010
28011 buffer = kmem_zalloc(4, KM_SLEEP);
28012 bzero(cdb, CDB_GROUP1);
28013 cdb[0] = SCMD_READ_TOC;
28014 /*
28015 * Specifying a track number of 0x00 in the READ TOC command indicates
28016 * that the TOC header should be returned
28017 */
28018 cdb[6] = 0x00;
28019 /*
28020 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28021 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28022 */
28023 cdb[8] = 0x04;
28024 com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28025 com->uscsi_cdb = cdb;
28026 com->uscsi_cdblen = CDB_GROUP1;
28027 com->uscsi_bufaddr = buffer;
28028 com->uscsi_buflen = 0x04;
28029 com->uscsi_timeout = 3 * un->un_cmd_timeout;
28030 com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28031
28032 rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28033 SD_PATH_STANDARD);
28034 if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28035 hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28036 hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28037 } else {
28038 hdr->cdth_trk0 = buffer[2];
28039 hdr->cdth_trk1 = buffer[3];
28040 }
28041 kmem_free(buffer, 4);
28042 kmem_free(com, sizeof (*com));
28043 if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28044 return (EFAULT);
28045 }
28046 return (rval);
28047 }
28048
28049
29358 } else {
29359 state = SD_WM_LOCK_RANGE;
29360 }
29361
29362 }
29363 break;
29364
29365 case SD_WM_LOCK_RANGE:
29366 ASSERT(un->un_wm_cache);
29367 /*
29368 * The range need to be locked, try to get a wmap.
29369 * First attempt it with NO_SLEEP, want to avoid a sleep
29370 * if possible as we will have to release the sd mutex
29371 * if we have to sleep.
29372 */
29373 if (wmp == NULL)
29374 wmp = kmem_cache_alloc(un->un_wm_cache,
29375 KM_NOSLEEP);
29376 if (wmp == NULL) {
29377 mutex_exit(SD_MUTEX(un));
29378 wmp = kmem_cache_alloc(un->un_wm_cache,
29379 KM_SLEEP);
29380 mutex_enter(SD_MUTEX(un));
29381 /*
29382 * we released the mutex so recheck and go to
29383 * check list state.
29384 */
29385 state = SD_WM_CHK_LIST;
29386 } else {
29387 /*
29388 * We exit out of state machine since we
29389 * have the wmap. Do the housekeeping first.
29390 * place the wmap on the wmap list if it is not
29391 * on it already and then set the state to done.
29392 */
29393 wmp->wm_start = startb;
29394 wmp->wm_end = endb;
29395 wmp->wm_flags = typ | SD_WM_BUSY;
29396 if (typ & SD_WTYPE_RMW) {
29397 un->un_rmw_count++;
29756 done:
29757 mutex_enter(SD_MUTEX(un));
29758 return (err);
29759 }
29760
29761
29762 /*
29763 * Function: sd_failfast_flushq
29764 *
29765 * Description: Take all bp's on the wait queue that have B_FAILFAST set
29766 * in b_flags and move them onto the failfast queue, then kick
29767 * off a thread to return all bp's on the failfast queue to
29768 * their owners with an error set.
29769 *
29770 * Arguments: un - pointer to the soft state struct for the instance.
29771 *
29772 * Context: may execute in interrupt context.
29773 */
29774
29775 static void
29776 sd_failfast_flushq(struct sd_lun *un, boolean_t flush_all)
29777 {
29778 struct buf *bp;
29779 struct buf *next_waitq_bp;
29780 struct buf *prev_waitq_bp = NULL;
29781
29782 ASSERT(un != NULL);
29783 ASSERT(mutex_owned(SD_MUTEX(un)));
29784 ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29785 ASSERT(un->un_failfast_bp == NULL);
29786
29787 SD_TRACE(SD_LOG_IO_FAILFAST, un,
29788 "sd_failfast_flushq: entry: un:0x%p\n", un);
29789
29790 /*
29791 * Check if we should flush all bufs when entering failfast state, or
29792 * just those with B_FAILFAST set.
29793 */
29794 if ((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29795 flush_all) {
29796 /*
29797 * Move *all* bp's on the wait queue to the failfast flush
29798 * queue, including those that do NOT have B_FAILFAST set.
29799 */
29800 if (un->un_failfast_headp == NULL) {
29801 ASSERT(un->un_failfast_tailp == NULL);
29802 un->un_failfast_headp = un->un_waitq_headp;
29803 } else {
29804 ASSERT(un->un_failfast_tailp != NULL);
29805 un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29806 }
29807
29808 un->un_failfast_tailp = un->un_waitq_tailp;
29809
29810 /* update kstat for each bp moved out of the waitq */
29811 for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29812 SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29813 }
29814
29815 /* empty the waitq */
30035 sd_panic_for_res_conflict(struct sd_lun *un)
30036 {
30037 char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30038 char path_str[MAXPATHLEN];
30039
30040 (void) snprintf(panic_str, sizeof (panic_str),
30041 "Reservation Conflict\nDisk: %s",
30042 ddi_pathname(SD_DEVINFO(un), path_str));
30043
30044 panic(panic_str);
30045 }
30046
30047 /*
30048 * Note: The following sd_faultinjection_ioctl( ) routines implement
30049 * driver support for handling fault injection for error analysis
30050 * causing faults in multiple layers of the driver.
30051 *
30052 */
30053
30054 #ifdef SD_FAULT_INJECTION
30055
30056 /*
30057 * Function: sd_faultinjection_ioctl()
30058 *
30059 * Description: This routine is the driver entry point for handling
30060 * faultinjection ioctls to inject errors into the
30061 * layer model
30062 *
30063 * Arguments: cmd - the ioctl cmd received
30064 * arg - the arguments from user and returns
30065 */
30066
30067 static int
30068 sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un)
30069 {
30070 uint_t i = 0;
30071 uint_t rval;
30072 int ret = 0;
30073
30074 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30075
30076 mutex_enter(SD_MUTEX(un));
30077
30078 switch (cmd) {
30079 case SDIOCRUN:
30080 /* Allow pushed faults to be injected */
30081 SD_INFO(SD_LOG_SDTEST, un,
30082 "sd_faultinjection_ioctl: Injecting Fault Run\n");
30083
30084 sd_fault_injection_on = 1;
30085
30086 SD_INFO(SD_LOG_IOERR, un,
30087 "sd_faultinjection_ioctl: run finished\n");
30088 break;
30089
30090 case SDIOCSTART:
30091 /* Start Injection Session */
30092 SD_INFO(SD_LOG_SDTEST, un,
30093 "sd_faultinjection_ioctl: Injecting Fault Start\n");
30094
30095 sd_fault_injection_on = 0;
30096 un->sd_injection_mask = 0xFFFFFFFF;
30097 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30098 un->sd_fi_fifo_pkt[i] = NULL;
30099 un->sd_fi_fifo_xb[i] = NULL;
30100 un->sd_fi_fifo_un[i] = NULL;
30101 un->sd_fi_fifo_arq[i] = NULL;
30102 un->sd_fi_fifo_tran[i] = NULL;
30103 }
30104 un->sd_fi_fifo_start = 0;
30105 un->sd_fi_fifo_end = 0;
30106
30107 mutex_enter(&(un->un_fi_mutex));
30108 un->sd_fi_log[0] = '\0';
30109 un->sd_fi_buf_len = 0;
30110 mutex_exit(&(un->un_fi_mutex));
30111
30112 SD_INFO(SD_LOG_IOERR, un,
30113 "sd_faultinjection_ioctl: start finished\n");
30114 break;
30115
30116 case SDIOCSTOP:
30117 /* Stop Injection Session */
30118 SD_INFO(SD_LOG_SDTEST, un,
30119 "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30120 sd_fault_injection_on = 0;
30121 un->sd_injection_mask = 0x0;
30122
30123 /* Empty stray or unuseds structs from fifo */
30124 for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30125 if (un->sd_fi_fifo_pkt[i] != NULL) {
30126 kmem_free(un->sd_fi_fifo_pkt[i],
30127 sizeof (struct sd_fi_pkt));
30128 }
30129 if (un->sd_fi_fifo_xb[i] != NULL) {
30130 kmem_free(un->sd_fi_fifo_xb[i],
30131 sizeof (struct sd_fi_xb));
30132 }
30133 if (un->sd_fi_fifo_un[i] != NULL) {
30134 kmem_free(un->sd_fi_fifo_un[i],
30135 sizeof (struct sd_fi_un));
30136 }
30137 if (un->sd_fi_fifo_arq[i] != NULL) {
30138 kmem_free(un->sd_fi_fifo_arq[i],
30139 sizeof (struct sd_fi_arq));
30140 }
30141 if (un->sd_fi_fifo_tran[i] != NULL) {
30142 kmem_free(un->sd_fi_fifo_tran[i],
30143 sizeof (struct sd_fi_tran));
30144 }
30145 un->sd_fi_fifo_pkt[i] = NULL;
30146 un->sd_fi_fifo_un[i] = NULL;
30147 un->sd_fi_fifo_xb[i] = NULL;
30148 un->sd_fi_fifo_arq[i] = NULL;
30149 un->sd_fi_fifo_tran[i] = NULL;
30150 }
30151 un->sd_fi_fifo_start = 0;
30152 un->sd_fi_fifo_end = 0;
30153
30154 SD_INFO(SD_LOG_IOERR, un,
30155 "sd_faultinjection_ioctl: stop finished\n");
30156 break;
30157
30158 case SDIOCINSERTPKT:
30159 /* Store a packet struct to be pushed onto fifo */
30160 SD_INFO(SD_LOG_SDTEST, un,
30161 "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30162
30163 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30164
30165 if (un->sd_fi_fifo_tran[i] != NULL) {
30166 ret = EBUSY;
30167 break;
30168 }
30169
30170 sd_fault_injection_on = 0;
30171
30172 /* No more that SD_FI_MAX_ERROR allowed in Queue */
30173 if (un->sd_fi_fifo_pkt[i] != NULL) {
30174 kmem_free(un->sd_fi_fifo_pkt[i],
30175 sizeof (struct sd_fi_pkt));
30176 }
30177 if (arg != NULL) {
30178 un->sd_fi_fifo_pkt[i] =
30179 kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30180 if (un->sd_fi_fifo_pkt[i] == NULL) {
30181 /* Alloc failed don't store anything */
30182 ret = ENOMEM;
30183 break;
30184 }
30185 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30186 sizeof (struct sd_fi_pkt), 0);
30187 if (rval == -1) {
30188 kmem_free(un->sd_fi_fifo_pkt[i],
30189 sizeof (struct sd_fi_pkt));
30190 un->sd_fi_fifo_pkt[i] = NULL;
30191 ret = EFAULT;
30192 break;
30193 }
30194 } else {
30195 SD_INFO(SD_LOG_IOERR, un,
30196 "sd_faultinjection_ioctl: pkt null\n");
30197 }
30198 break;
30199
30200 case SDIOCINSERTTRAN:
30201 /* Store a tran packet struct to be pushed onto fifo. */
30202 SD_INFO(SD_LOG_SDTEST, un,
30203 "sd_faultinjection_ioctl: Injecting Fault Insert TRAN\n");
30204 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30205
30206 /*
30207 * HBA-related fault injections can't be mixed with target-level
30208 * fault injections.
30209 */
30210 if (un->sd_fi_fifo_pkt[i] != NULL ||
30211 un->sd_fi_fifo_xb[i] != NULL ||
30212 un->sd_fi_fifo_un[i] != NULL ||
30213 un->sd_fi_fifo_arq[i] != NULL) {
30214 ret = EBUSY;
30215 break;
30216 }
30217
30218 sd_fault_injection_on = 0;
30219
30220 if (un->sd_fi_fifo_tran[i] != NULL) {
30221 kmem_free(un->sd_fi_fifo_tran[i],
30222 sizeof (struct sd_fi_tran));
30223 un->sd_fi_fifo_tran[i] = NULL;
30224 }
30225 if (arg != NULL) {
30226 un->sd_fi_fifo_tran[i] =
30227 kmem_alloc(sizeof (struct sd_fi_tran), KM_NOSLEEP);
30228 if (un->sd_fi_fifo_tran[i] == NULL) {
30229 /* Alloc failed don't store anything */
30230 ret = ENOMEM;
30231 break;
30232 }
30233 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_tran[i],
30234 sizeof (struct sd_fi_tran), 0);
30235
30236 if (rval == 0) {
30237 switch (un->sd_fi_fifo_tran[i]->tran_cmd) {
30238 case SD_FLTINJ_CMD_BUSY:
30239 case SD_FLTINJ_CMD_TIMEOUT:
30240 break;
30241 default:
30242 ret = EINVAL;
30243 break;
30244 }
30245 } else {
30246 ret = EFAULT;
30247 }
30248
30249 if (ret != 0) {
30250 kmem_free(un->sd_fi_fifo_tran[i],
30251 sizeof (struct sd_fi_tran));
30252 un->sd_fi_fifo_tran[i] = NULL;
30253 break;
30254 }
30255 } else {
30256 SD_INFO(SD_LOG_IOERR, un,
30257 "sd_faultinjection_ioctl: tran null\n");
30258 }
30259 break;
30260
30261 case SDIOCINSERTXB:
30262 /* Store a xb struct to be pushed onto fifo */
30263 SD_INFO(SD_LOG_SDTEST, un,
30264 "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30265
30266 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30267
30268 if (un->sd_fi_fifo_tran[i] != NULL) {
30269 ret = EBUSY;
30270 break;
30271 }
30272
30273 sd_fault_injection_on = 0;
30274
30275 if (un->sd_fi_fifo_xb[i] != NULL) {
30276 kmem_free(un->sd_fi_fifo_xb[i],
30277 sizeof (struct sd_fi_xb));
30278 un->sd_fi_fifo_xb[i] = NULL;
30279 }
30280 if (arg != NULL) {
30281 un->sd_fi_fifo_xb[i] =
30282 kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30283 if (un->sd_fi_fifo_xb[i] == NULL) {
30284 /* Alloc failed don't store anything */
30285 ret = ENOMEM;
30286 break;
30287 }
30288 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30289 sizeof (struct sd_fi_xb), 0);
30290
30291 if (rval == -1) {
30292 kmem_free(un->sd_fi_fifo_xb[i],
30293 sizeof (struct sd_fi_xb));
30294 un->sd_fi_fifo_xb[i] = NULL;
30295 ret = EFAULT;
30296 break;
30297 }
30298 } else {
30299 SD_INFO(SD_LOG_IOERR, un,
30300 "sd_faultinjection_ioctl: xb null\n");
30301 }
30302 break;
30303
30304 case SDIOCINSERTUN:
30305 /* Store a un struct to be pushed onto fifo */
30306 SD_INFO(SD_LOG_SDTEST, un,
30307 "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30308
30309 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30310 if (un->sd_fi_fifo_tran[i] != NULL) {
30311 ret = EBUSY;
30312 break;
30313 }
30314
30315 sd_fault_injection_on = 0;
30316
30317 if (un->sd_fi_fifo_un[i] != NULL) {
30318 kmem_free(un->sd_fi_fifo_un[i],
30319 sizeof (struct sd_fi_un));
30320 un->sd_fi_fifo_un[i] = NULL;
30321 }
30322 if (arg != NULL) {
30323 un->sd_fi_fifo_un[i] =
30324 kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30325 if (un->sd_fi_fifo_un[i] == NULL) {
30326 /* Alloc failed don't store anything */
30327 ret = ENOMEM;
30328 break;
30329 }
30330 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30331 sizeof (struct sd_fi_un), 0);
30332 if (rval == -1) {
30333 kmem_free(un->sd_fi_fifo_un[i],
30334 sizeof (struct sd_fi_un));
30335 un->sd_fi_fifo_un[i] = NULL;
30336 ret = EFAULT;
30337 break;
30338 }
30339
30340 } else {
30341 SD_INFO(SD_LOG_IOERR, un,
30342 "sd_faultinjection_ioctl: un null\n");
30343 }
30344
30345 break;
30346
30347 case SDIOCINSERTARQ:
30348 /* Store a arq struct to be pushed onto fifo */
30349 SD_INFO(SD_LOG_SDTEST, un,
30350 "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30351 i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30352 if (un->sd_fi_fifo_tran[i] != NULL) {
30353 ret = EBUSY;
30354 break;
30355 }
30356
30357 sd_fault_injection_on = 0;
30358
30359 if (un->sd_fi_fifo_arq[i] != NULL) {
30360 kmem_free(un->sd_fi_fifo_arq[i],
30361 sizeof (struct sd_fi_arq));
30362 un->sd_fi_fifo_arq[i] = NULL;
30363 }
30364 if (arg != NULL) {
30365 un->sd_fi_fifo_arq[i] =
30366 kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30367 if (un->sd_fi_fifo_arq[i] == NULL) {
30368 /* Alloc failed don't store anything */
30369 ret = ENOMEM;
30370 break;
30371 }
30372 rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30373 sizeof (struct sd_fi_arq), 0);
30374 if (rval == -1) {
30375 kmem_free(un->sd_fi_fifo_arq[i],
30376 sizeof (struct sd_fi_arq));
30377 un->sd_fi_fifo_arq[i] = NULL;
30378 ret = EFAULT;
30379 break;
30380 }
30381
30382 } else {
30383 SD_INFO(SD_LOG_IOERR, un,
30384 "sd_faultinjection_ioctl: arq null\n");
30385 }
30386
30387 break;
30388
30389 case SDIOCPUSH:
30390 /* Push stored xb, pkt, un, arq and tran onto fifo */
30391 sd_fault_injection_on = 0;
30392
30393 if (arg != NULL) {
30394 rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30395 if (rval != -1 &&
30396 un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30397 un->sd_fi_fifo_end += i;
30398 }
30399 } else {
30400 SD_INFO(SD_LOG_IOERR, un,
30401 "sd_faultinjection_ioctl: push arg null\n");
30402 if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30403 un->sd_fi_fifo_end++;
30404 }
30405 }
30406 SD_INFO(SD_LOG_IOERR, un,
30407 "sd_faultinjection_ioctl: push to end=%d\n",
30408 un->sd_fi_fifo_end);
30409 break;
30410
30415
30416 sd_fault_injection_on = 0;
30417
30418 mutex_enter(&(un->un_fi_mutex));
30419 rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30420 un->sd_fi_buf_len+1, 0);
30421 mutex_exit(&(un->un_fi_mutex));
30422
30423 if (rval == -1) {
30424 /*
30425 * arg is possibly invalid setting
30426 * it to NULL for return
30427 */
30428 arg = NULL;
30429 }
30430 break;
30431 }
30432
30433 mutex_exit(SD_MUTEX(un));
30434 SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: exit\n");
30435 return (ret);
30436 }
30437
30438
30439 /*
30440 * Function: sd_injection_log()
30441 *
30442 * Description: This routine adds buff to the already existing injection log
30443 * for retrieval via faultinjection_ioctl for use in fault
30444 * detection and recovery
30445 *
30446 * Arguments: buf - the string to add to the log
30447 */
30448
30449 static void
30450 sd_injection_log(char *buf, struct sd_lun *un)
30451 {
30452 uint_t len;
30453
30454 ASSERT(un != NULL);
30455 ASSERT(buf != NULL);
30456
30457 mutex_enter(&(un->un_fi_mutex));
30458
30459 len = min(strlen(buf), 255);
30460 /* Add logged value to Injection log to be returned later */
30461 if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30462 uint_t offset = strlen((char *)un->sd_fi_log);
30463 char *destp = (char *)un->sd_fi_log + offset;
30464 int i;
30465 for (i = 0; i < len; i++) {
30466 *destp++ = *buf++;
30467 }
30468 un->sd_fi_buf_len += len;
30469 un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30470 }
30471
30472 mutex_exit(&(un->un_fi_mutex));
30473 }
30474
30475 /*
30476 * This function is called just before sending the packet to the HBA.
30477 * Caller must hold per-LUN mutex. Mutex is held locked upon return.
30478 */
30479 static void
30480 sd_prefaultinjection(struct scsi_pkt *pktp)
30481 {
30482 uint_t i;
30483 struct buf *bp;
30484 struct sd_lun *un;
30485 struct sd_fi_tran *fi_tran;
30486
30487 ASSERT(pktp != NULL);
30488
30489 /* pull bp and un from pktp */
30490 bp = (struct buf *)pktp->pkt_private;
30491 un = SD_GET_UN(bp);
30492
30493 /* if injection is off return */
30494 if (sd_fault_injection_on == 0 ||
30495 un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30496 return;
30497 }
30498
30499 ASSERT(un != NULL);
30500 ASSERT(mutex_owned(SD_MUTEX(un)));
30501
30502 /* take next set off fifo */
30503 i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30504
30505 fi_tran = un->sd_fi_fifo_tran[i];
30506 if (fi_tran != NULL) {
30507 switch (fi_tran->tran_cmd) {
30508 case SD_FLTINJ_CMD_BUSY:
30509 pktp->pkt_flags |= FLAG_PKT_BUSY;
30510 break;
30511 case SD_FLTINJ_CMD_TIMEOUT:
30512 pktp->pkt_flags |= FLAG_PKT_TIMEOUT;
30513 break;
30514 default:
30515 return;
30516 }
30517 }
30518 /*
30519 * We don't deallocate any data here - it will be deallocated after
30520 * the packet has been processed by the HBA.
30521 */
30522 }
30523
30524
30525 /*
30526 * Function: sd_faultinjection()
30527 *
30528 * Description: This routine takes the pkt and changes its
30529 * content based on error injection scenerio.
30530 *
30531 * Arguments: pktp - packet to be changed
30532 */
30533
30534 static void
30535 sd_faultinjection(struct scsi_pkt *pktp)
30536 {
30537 uint_t i;
30538 struct sd_fi_pkt *fi_pkt;
30539 struct sd_fi_xb *fi_xb;
30540 struct sd_fi_un *fi_un;
30541 struct sd_fi_arq *fi_arq;
30542 struct buf *bp;
30543 struct sd_xbuf *xb;
30544 struct sd_lun *un;
30729 * If a direct-access disk has no EFI label, sd will check if it has a
30730 * valid VTOC label. Now, sd also does that check for removable media
30731 * and hotpluggable devices.
30732 *
30733 * --------------------------------------------------------------
30734 * Direct-Access removable media hotpluggable | Check Label
30735 * -------------------------------------------------------------
30736 * false false false | No
30737 * false false true | No
30738 * false true false | Yes
30739 * false true true | Yes
30740 * true x x | Yes
30741 * --------------------------------------------------------------
30742 *
30743 *
30744 * 4. Building default VTOC label
30745 *
30746 * As section 3 says, sd checks if some kinds of devices have VTOC label.
30747 * If those devices have no valid VTOC label, sd(7d) will attempt to
30748 * create default VTOC for them. Currently sd creates default VTOC label
30749 * for all devices on x86 platform (VTOC_16).
30750 *
30751 * -----------------------------------------------------------
30752 * removable media hotpluggable platform | Default Label
30753 * -----------------------------------------------------------
30754 * false true x86 | Yes
30755 * true x x | Yes
30756 * ----------------------------------------------------------
30757 *
30758 *
30759 * 5. Supported blocksizes of target devices
30760 *
30761 * Sd supports non-512-byte blocksize for removable media devices only.
30762 * For other devices, only 512-byte blocksize is supported. This may be
30763 * changed in near future because some RAID devices require non-512-byte
30764 * blocksize
30765 *
30766 * -----------------------------------------------------------
30767 * removable media hotpluggable | non-512-byte blocksize
30768 * -----------------------------------------------------------
30769 * false false | No
30770 * false true | No
30771 * true x | Yes
30772 * -----------------------------------------------------------
30773 *
30774 *
30775 * 6. Automatic mount & unmount
30776 *
30777 * Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30778 * if a device is removable media device. It return 1 for removable media
30779 * devices, and 0 for others.
30780 *
30781 * The automatic mounting subsystem should distinguish between the types
30782 * of devices and apply automounting policies to each.
30783 *
30784 *
30785 * 7. fdisk partition management
30786 *
30787 * Fdisk is traditional partition method on x86 platform. sd(7D) driver
30788 * just supports fdisk partitions on x86 platform.
30789 *
30790 * -----------------------------------------------------------
30791 * platform removable media USB/1394 | fdisk supported
30792 * -----------------------------------------------------------
30793 * x86 X X | true
30794 * -----------------------------------------------------------
30795 *
30796 *
30797 * 8. MBOOT/MBR
30798 *
30799 * -----------------------------------------------------------
30800 * platform removable media USB/1394 | mboot supported
30801 * -----------------------------------------------------------
30802 * x86 X X | true
30803 * -----------------------------------------------------------
30804 *
30805 *
30806 * 9. error handling during opening device
30807 *
30808 * If failed to open a disk device, an errno is returned. For some kinds
30809 * of errors, different errno is returned depending on if this device is
30810 * a removable media device. This brings USB/1394 hard disks in line with
30811 * expected hard disk behavior. It is not expected that this breaks any
30812 * application.
30813 *
30814 * ------------------------------------------------------
30815 * removable media hotpluggable | errno
30816 * ------------------------------------------------------
30817 * false false | EIO
30818 * false true | EIO
30819 * true x | ENXIO
30820 * ------------------------------------------------------
30821 *
30822 *
30823 * 11. ioctls: DKIOCEJECT, CDROMEJECT
31321 un->un_f_mmc_writable_media;
31322 ((tg_attribute_t *)arg)->media_is_solid_state =
31323 un->un_f_is_solid_state;
31324 ((tg_attribute_t *)arg)->media_is_rotational =
31325 un->un_f_is_rotational;
31326 mutex_exit(SD_MUTEX(un));
31327 return (0);
31328 default:
31329 return (ENOTTY);
31330
31331 }
31332 }
31333
31334 /*
31335 * Function: sd_ssc_ereport_post
31336 *
31337 * Description: Will be called when SD driver need to post an ereport.
31338 *
31339 * Context: Kernel thread or interrupt context.
31340 */
31341 static void
31342 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31343 {
31344 uchar_t uscsi_pkt_reason;
31345 uint32_t uscsi_pkt_state;
31346 uint32_t uscsi_pkt_statistics;
31347 uint64_t uscsi_ena;
31348 uchar_t op_code;
31349 uint8_t *sensep;
31350 union scsi_cdb *cdbp;
31351 uint_t cdblen = 0;
31352 uint_t senlen = 0;
31353 struct sd_lun *un;
31354 dev_info_t *dip;
31355 char *devid;
31356 int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31357 SSC_FLAGS_INVALID_STATUS |
31358 SSC_FLAGS_INVALID_SENSE |
31359 SSC_FLAGS_INVALID_DATA;
31360 char assessment[16];
31361
31362 ASSERT(ssc != NULL);
31363 ASSERT(ssc->ssc_uscsi_cmd != NULL);
31366 un = ssc->ssc_un;
31367 ASSERT(un != NULL);
31368
31369 dip = un->un_sd->sd_dev;
31370
31371 /*
31372 * Get the devid:
31373 * devid will only be passed to non-transport error reports.
31374 */
31375 devid = DEVI(dip)->devi_devid_str;
31376
31377 /*
31378 * If we are syncing or dumping, the command will not be executed
31379 * so we bypass this situation.
31380 */
31381 if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31382 (un->un_state == SD_STATE_DUMPING))
31383 return;
31384
31385 uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31386 uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31387 uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31388 uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31389
31390 sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31391 cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31392
31393 /* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31394 if (cdbp == NULL) {
31395 scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31396 "sd_ssc_ereport_post meet empty cdb\n");
31397 return;
31398 }
31399
31400 op_code = cdbp->scc_cmd;
31401
31402 cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31403 senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31404 ssc->ssc_uscsi_cmd->uscsi_rqresid);
31405
31414 switch (drv_assess) {
31415 case SD_FM_DRV_RECOVERY:
31416 (void) sprintf(assessment, "%s", "recovered");
31417 break;
31418 case SD_FM_DRV_RETRY:
31419 (void) sprintf(assessment, "%s", "retry");
31420 break;
31421 case SD_FM_DRV_NOTICE:
31422 (void) sprintf(assessment, "%s", "info");
31423 break;
31424 case SD_FM_DRV_FATAL:
31425 default:
31426 (void) sprintf(assessment, "%s", "unknown");
31427 }
31428 /*
31429 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31430 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31431 * driver-assessment will always be "recovered" here.
31432 */
31433 if (drv_assess == SD_FM_DRV_RECOVERY) {
31434 scsi_fm_ereport_post(un->un_sd, 0, NULL,
31435 "cmd.disk.recovered", uscsi_ena, devid, NULL,
31436 DDI_NOSLEEP, NULL,
31437 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31438 DEVID_IF_KNOWN(devid),
31439 "driver-assessment", DATA_TYPE_STRING, assessment,
31440 "op-code", DATA_TYPE_UINT8, op_code,
31441 "cdb", DATA_TYPE_UINT8_ARRAY,
31442 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31443 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31444 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31445 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31446 NULL);
31447 return;
31448 }
31449
31450 /*
31451 * If there is un-expected/un-decodable data, we should post
31452 * ereport.io.scsi.cmd.disk.dev.uderr.
31453 * driver-assessment will be set based on parameter drv_assess.
31454 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31455 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31456 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31457 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31458 */
31459 if (ssc->ssc_flags & ssc_invalid_flags) {
31460 if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31461 scsi_fm_ereport_post(un->un_sd, 0,
31462 NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31463 NULL, DDI_NOSLEEP, NULL,
31464 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31465 DEVID_IF_KNOWN(devid),
31466 "driver-assessment", DATA_TYPE_STRING,
31467 drv_assess == SD_FM_DRV_FATAL ?
31468 "fail" : assessment,
31469 "op-code", DATA_TYPE_UINT8, op_code,
31470 "cdb", DATA_TYPE_UINT8_ARRAY,
31471 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31472 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31473 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31474 "pkt-stats", DATA_TYPE_UINT32,
31475 uscsi_pkt_statistics,
31476 "stat-code", DATA_TYPE_UINT8,
31477 ssc->ssc_uscsi_cmd->uscsi_status,
31478 "un-decode-info", DATA_TYPE_STRING,
31479 ssc->ssc_info,
31480 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31481 senlen, sensep,
31482 NULL);
31483 } else {
31484 /*
31485 * For other type of invalid data, the
31486 * un-decode-value field would be empty because the
31487 * un-decodable content could be seen from upper
31488 * level payload or inside un-decode-info.
31489 */
31490 scsi_fm_ereport_post(un->un_sd, 0,
31491 NULL,
31492 "cmd.disk.dev.uderr", uscsi_ena, devid,
31493 NULL, DDI_NOSLEEP, NULL,
31494 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31495 DEVID_IF_KNOWN(devid),
31496 "driver-assessment", DATA_TYPE_STRING,
31497 drv_assess == SD_FM_DRV_FATAL ?
31498 "fail" : assessment,
31499 "op-code", DATA_TYPE_UINT8, op_code,
31500 "cdb", DATA_TYPE_UINT8_ARRAY,
31501 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31502 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31503 "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31504 "pkt-stats", DATA_TYPE_UINT32,
31505 uscsi_pkt_statistics,
31506 "stat-code", DATA_TYPE_UINT8,
31507 ssc->ssc_uscsi_cmd->uscsi_status,
31508 "un-decode-info", DATA_TYPE_STRING,
31509 ssc->ssc_info,
31510 "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31512 NULL);
31513 }
31514 ssc->ssc_flags &= ~ssc_invalid_flags;
31515 return;
31516 }
31517
31518 if (uscsi_pkt_reason != CMD_CMPLT ||
31519 (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31520 /*
31521 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31522 * set inside sd_start_cmds due to errors(bad packet or
31523 * fatal transport error), we should take it as a
31524 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31525 * driver-assessment will be set based on drv_assess.
31526 * We will set devid to NULL because it is a transport
31527 * error.
31528 */
31529 if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31530 ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31531
31532 scsi_fm_ereport_post(un->un_sd, 0, NULL,
31533 "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31534 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31535 DEVID_IF_KNOWN(devid),
31536 "driver-assessment", DATA_TYPE_STRING,
31537 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31538 "op-code", DATA_TYPE_UINT8, op_code,
31539 "cdb", DATA_TYPE_UINT8_ARRAY,
31540 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31541 "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31542 "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31543 "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31544 NULL);
31545 } else {
31546 /*
31547 * If we got here, we have a completed command, and we need
31548 * to further investigate the sense data to see what kind
31549 * of ereport we should post.
31550 * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31551 * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31552 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31556 * drv_assess.
31557 */
31558 if (senlen > 0) {
31559 /*
31560 * Here we have sense data available.
31561 */
31562 uint8_t sense_key = scsi_sense_key(sensep);
31563 uint8_t sense_asc = scsi_sense_asc(sensep);
31564 uint8_t sense_ascq = scsi_sense_ascq(sensep);
31565
31566 if (sense_key == KEY_RECOVERABLE_ERROR &&
31567 sense_asc == 0x00 && sense_ascq == 0x1d)
31568 return;
31569
31570 if (sense_key == KEY_MEDIUM_ERROR) {
31571 /*
31572 * driver-assessment should be "fatal" if
31573 * drv_assess is SD_FM_DRV_FATAL.
31574 */
31575 scsi_fm_ereport_post(un->un_sd,
31576 0, NULL,
31577 "cmd.disk.dev.rqs.merr",
31578 uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31579 FM_VERSION, DATA_TYPE_UINT8,
31580 FM_EREPORT_VERS0,
31581 DEVID_IF_KNOWN(devid),
31582 "driver-assessment",
31583 DATA_TYPE_STRING,
31584 drv_assess == SD_FM_DRV_FATAL ?
31585 "fatal" : assessment,
31586 "op-code",
31587 DATA_TYPE_UINT8, op_code,
31588 "cdb",
31589 DATA_TYPE_UINT8_ARRAY, cdblen,
31590 ssc->ssc_uscsi_cmd->uscsi_cdb,
31591 "pkt-reason",
31592 DATA_TYPE_UINT8, uscsi_pkt_reason,
31593 "pkt-state",
31594 DATA_TYPE_UINT8, uscsi_pkt_state,
31595 "pkt-stats",
31596 DATA_TYPE_UINT32,
31605 DATA_TYPE_UINT8,
31606 scsi_sense_asc(sensep),
31607 "ascq",
31608 DATA_TYPE_UINT8,
31609 scsi_sense_ascq(sensep),
31610 "sense-data",
31611 DATA_TYPE_UINT8_ARRAY,
31612 senlen, sensep,
31613 "lba",
31614 DATA_TYPE_UINT64,
31615 ssc->ssc_uscsi_info->ui_lba,
31616 NULL);
31617 } else {
31618 /*
31619 * if sense-key == 0x4(hardware
31620 * error), driver-assessment should
31621 * be "fatal" if drv_assess is
31622 * SD_FM_DRV_FATAL.
31623 */
31624 scsi_fm_ereport_post(un->un_sd,
31625 0, NULL,
31626 "cmd.disk.dev.rqs.derr",
31627 uscsi_ena, devid,
31628 NULL, DDI_NOSLEEP, NULL,
31629 FM_VERSION,
31630 DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31631 DEVID_IF_KNOWN(devid),
31632 "driver-assessment",
31633 DATA_TYPE_STRING,
31634 drv_assess == SD_FM_DRV_FATAL ?
31635 (sense_key == 0x4 ?
31636 "fatal" : "fail") : assessment,
31637 "op-code",
31638 DATA_TYPE_UINT8, op_code,
31639 "cdb",
31640 DATA_TYPE_UINT8_ARRAY, cdblen,
31641 ssc->ssc_uscsi_cmd->uscsi_cdb,
31642 "pkt-reason",
31643 DATA_TYPE_UINT8, uscsi_pkt_reason,
31644 "pkt-state",
31645 DATA_TYPE_UINT8, uscsi_pkt_state,
31660 scsi_sense_ascq(sensep),
31661 "sense-data",
31662 DATA_TYPE_UINT8_ARRAY,
31663 senlen, sensep,
31664 NULL);
31665 }
31666 } else {
31667 /*
31668 * For stat_code == STATUS_GOOD, this is not a
31669 * hardware error.
31670 */
31671 if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31672 return;
31673
31674 /*
31675 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31676 * stat-code but with sense data unavailable.
31677 * driver-assessment will be set based on parameter
31678 * drv_assess.
31679 */
31680 scsi_fm_ereport_post(un->un_sd, 0,
31681 NULL,
31682 "cmd.disk.dev.serr", uscsi_ena,
31683 devid, NULL, DDI_NOSLEEP, NULL,
31684 FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31685 DEVID_IF_KNOWN(devid),
31686 "driver-assessment", DATA_TYPE_STRING,
31687 drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31688 "op-code", DATA_TYPE_UINT8, op_code,
31689 "cdb",
31690 DATA_TYPE_UINT8_ARRAY,
31691 cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31692 "pkt-reason",
31693 DATA_TYPE_UINT8, uscsi_pkt_reason,
31694 "pkt-state",
31695 DATA_TYPE_UINT8, uscsi_pkt_state,
31696 "pkt-stats",
31697 DATA_TYPE_UINT32, uscsi_pkt_statistics,
31698 "stat-code",
31699 DATA_TYPE_UINT8,
31700 ssc->ssc_uscsi_cmd->uscsi_status,
31914 ASSERT(un != NULL);
31915 ASSERT(!mutex_owned(SD_MUTEX(un)));
31916
31917 mutex_enter(SD_MUTEX(un));
31918 if (ISCD(un)) {
31919 mutex_exit(SD_MUTEX(un));
31920 return;
31921 }
31922
31923 if (un->un_f_descr_format_supported) {
31924 mutex_exit(SD_MUTEX(un));
31925 rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31926 &pbsize, SD_PATH_DIRECT);
31927 mutex_enter(SD_MUTEX(un));
31928
31929 if (rval != 0) {
31930 un->un_phy_blocksize = DEV_BSIZE;
31931 } else {
31932 if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31933 un->un_phy_blocksize = DEV_BSIZE;
31934 } else if (pbsize > un->un_phy_blocksize &&
31935 !un->un_f_sdconf_phy_blocksize) {
31936 /*
31937 * Reset the physical block size
31938 * if we've detected a larger value and
31939 * we didn't already set the physical
31940 * block size in sd.conf
31941 */
31942 un->un_phy_blocksize = pbsize;
31943 }
31944 }
31945 }
31946
31947 for (i = 0; i < sd_flash_dev_table_size; i++) {
31948 devid_len = (int)strlen(sd_flash_dev_table[i]);
31949 if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31950 == SD_SUCCESS) {
31951 un->un_phy_blocksize = SSD_SECSIZE;
31952 if (un->un_f_is_solid_state &&
31953 un->un_phy_blocksize != un->un_tgt_blocksize)
31954 un->un_f_enable_rmw = TRUE;
31955 }
31956 }
31957
31958 mutex_exit(SD_MUTEX(un));
31959 }
|