xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 29219719)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
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 2019 Joyent, Inc.
30  * Copyright 2019 Racktop Systems
31  * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
32  * Copyright 2022 Tintri by DDN, Inc. All rights reserved.
33  */
34 /*
35  * Copyright 2011 cyril.galibern@opensvc.com
36  */
37 
38 /*
39  * SCSI disk target driver.
40  */
41 #include <sys/scsi/scsi.h>
42 #include <sys/dkbad.h>
43 #include <sys/dklabel.h>
44 #include <sys/dkio.h>
45 #include <sys/fdio.h>
46 #include <sys/cdio.h>
47 #include <sys/mhd.h>
48 #include <sys/vtoc.h>
49 #include <sys/dktp/fdisk.h>
50 #include <sys/kstat.h>
51 #include <sys/vtrace.h>
52 #include <sys/note.h>
53 #include <sys/thread.h>
54 #include <sys/proc.h>
55 #include <sys/efi_partition.h>
56 #include <sys/var.h>
57 #include <sys/aio_req.h>
58 #include <sys/dkioc_free_util.h>
59 
60 #ifdef __lock_lint
61 #define	_LP64
62 #define	__amd64
63 #endif
64 
65 #if (defined(__fibre))
66 /* Note: is there a leadville version of the following? */
67 #include <sys/fc4/fcal_linkapp.h>
68 #endif
69 #include <sys/taskq.h>
70 #include <sys/uuid.h>
71 #include <sys/byteorder.h>
72 #include <sys/sdt.h>
73 
74 #include "sd_xbuf.h"
75 
76 #include <sys/scsi/targets/sddef.h>
77 #include <sys/cmlb.h>
78 #include <sys/sysevent/eventdefs.h>
79 #include <sys/sysevent/dev.h>
80 
81 #include <sys/fm/protocol.h>
82 
83 /*
84  * Loadable module info.
85  */
86 #if (defined(__fibre))
87 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
88 #else /* !__fibre */
89 #define	SD_MODULE_NAME	"SCSI Disk Driver"
90 #endif /* !__fibre */
91 
92 /*
93  * Define the interconnect type, to allow the driver to distinguish
94  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
95  *
96  * This is really for backward compatibility. In the future, the driver
97  * should actually check the "interconnect-type" property as reported by
98  * the HBA; however at present this property is not defined by all HBAs,
99  * so we will use this #define (1) to permit the driver to run in
100  * backward-compatibility mode; and (2) to print a notification message
101  * if an FC HBA does not support the "interconnect-type" property.  The
102  * behavior of the driver will be to assume parallel SCSI behaviors unless
103  * the "interconnect-type" property is defined by the HBA **AND** has a
104  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
105  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
106  * Channel behaviors (as per the old ssd).  (Note that the
107  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
108  * will result in the driver assuming parallel SCSI behaviors.)
109  *
110  * (see common/sys/scsi/impl/services.h)
111  *
112  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
113  * since some FC HBAs may already support that, and there is some code in
114  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
115  * default would confuse that code, and besides things should work fine
116  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
117  * "interconnect_type" property.
118  *
119  */
120 #if (defined(__fibre))
121 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
122 #else
123 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
124 #endif
125 
126 /*
127  * The name of the driver, established from the module name in _init.
128  */
129 static	char *sd_label			= NULL;
130 
131 /*
132  * Driver name is unfortunately prefixed on some driver.conf properties.
133  */
134 #if (defined(__fibre))
135 #define	sd_max_xfer_size		ssd_max_xfer_size
136 #define	sd_config_list			ssd_config_list
137 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
138 static	char *sd_config_list		= "ssd-config-list";
139 #else
140 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
141 static	char *sd_config_list		= "sd-config-list";
142 #endif
143 
144 /*
145  * Driver global variables
146  */
147 
148 #if (defined(__fibre))
149 /*
150  * These #defines are to avoid namespace collisions that occur because this
151  * code is currently used to compile two separate driver modules: sd and ssd.
152  * All global variables need to be treated this way (even if declared static)
153  * in order to allow the debugger to resolve the names properly.
154  * It is anticipated that in the near future the ssd module will be obsoleted,
155  * at which time this namespace issue should go away.
156  */
157 #define	sd_state			ssd_state
158 #define	sd_io_time			ssd_io_time
159 #define	sd_failfast_enable		ssd_failfast_enable
160 #define	sd_ua_retry_count		ssd_ua_retry_count
161 #define	sd_report_pfa			ssd_report_pfa
162 #define	sd_max_throttle			ssd_max_throttle
163 #define	sd_min_throttle			ssd_min_throttle
164 #define	sd_rot_delay			ssd_rot_delay
165 
166 #define	sd_retry_on_reservation_conflict	\
167 					ssd_retry_on_reservation_conflict
168 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
169 #define	sd_resv_conflict_name		ssd_resv_conflict_name
170 
171 #define	sd_component_mask		ssd_component_mask
172 #define	sd_level_mask			ssd_level_mask
173 #define	sd_debug_un			ssd_debug_un
174 #define	sd_error_level			ssd_error_level
175 
176 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
177 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
178 
179 #define	sd_tr				ssd_tr
180 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
181 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
182 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
183 #define	sd_check_media_time		ssd_check_media_time
184 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
185 #define	sd_label_mutex			ssd_label_mutex
186 #define	sd_detach_mutex			ssd_detach_mutex
187 #define	sd_log_buf			ssd_log_buf
188 #define	sd_log_mutex			ssd_log_mutex
189 
190 #define	sd_disk_table			ssd_disk_table
191 #define	sd_disk_table_size		ssd_disk_table_size
192 #define	sd_sense_mutex			ssd_sense_mutex
193 #define	sd_cdbtab			ssd_cdbtab
194 
195 #define	sd_cb_ops			ssd_cb_ops
196 #define	sd_ops				ssd_ops
197 #define	sd_additional_codes		ssd_additional_codes
198 #define	sd_tgops			ssd_tgops
199 
200 #define	sd_minor_data			ssd_minor_data
201 #define	sd_minor_data_efi		ssd_minor_data_efi
202 
203 #define	sd_tq				ssd_tq
204 #define	sd_wmr_tq			ssd_wmr_tq
205 #define	sd_taskq_name			ssd_taskq_name
206 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
207 #define	sd_taskq_minalloc		ssd_taskq_minalloc
208 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
209 
210 #define	sd_dump_format_string		ssd_dump_format_string
211 
212 #define	sd_iostart_chain		ssd_iostart_chain
213 #define	sd_iodone_chain			ssd_iodone_chain
214 
215 #define	sd_pm_idletime			ssd_pm_idletime
216 
217 #define	sd_force_pm_supported		ssd_force_pm_supported
218 
219 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
220 
221 #define	sd_ssc_init			ssd_ssc_init
222 #define	sd_ssc_send			ssd_ssc_send
223 #define	sd_ssc_fini			ssd_ssc_fini
224 #define	sd_ssc_assessment		ssd_ssc_assessment
225 #define	sd_ssc_post			ssd_ssc_post
226 #define	sd_ssc_print			ssd_ssc_print
227 #define	sd_ssc_ereport_post		ssd_ssc_ereport_post
228 #define	sd_ssc_set_info			ssd_ssc_set_info
229 #define	sd_ssc_extract_info		ssd_ssc_extract_info
230 
231 #endif
232 
233 #ifdef	SDDEBUG
234 int	sd_force_pm_supported		= 0;
235 #endif	/* SDDEBUG */
236 
237 void *sd_state				= NULL;
238 int sd_io_time				= SD_IO_TIME;
239 int sd_failfast_enable			= 1;
240 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
241 int sd_report_pfa			= 1;
242 int sd_max_throttle			= SD_MAX_THROTTLE;
243 int sd_min_throttle			= SD_MIN_THROTTLE;
244 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
245 int sd_qfull_throttle_enable		= TRUE;
246 
247 int sd_retry_on_reservation_conflict	= 1;
248 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
249 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
250 
251 static int sd_dtype_optical_bind	= -1;
252 
253 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
254 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
255 
256 /*
257  * Global data for debug logging. To enable debug printing, sd_component_mask
258  * and sd_level_mask should be set to the desired bit patterns as outlined in
259  * sddef.h.
260  */
261 uint_t	sd_component_mask		= 0x0;
262 uint_t	sd_level_mask			= 0x0;
263 struct	sd_lun *sd_debug_un		= NULL;
264 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
265 
266 /* Note: these may go away in the future... */
267 static uint32_t	sd_xbuf_active_limit	= 512;
268 static uint32_t sd_xbuf_reserve_limit	= 16;
269 
270 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
271 
272 /*
273  * Timer value used to reset the throttle after it has been reduced
274  * (typically in response to TRAN_BUSY or STATUS_QFULL)
275  */
276 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
277 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
278 
279 /*
280  * Interval value associated with the media change scsi watch.
281  */
282 static int sd_check_media_time		= 3000000;
283 
284 /*
285  * Wait value used for in progress operations during a DDI_SUSPEND
286  */
287 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
288 
289 /*
290  * sd_label_mutex protects a static buffer used in the disk label
291  * component of the driver
292  */
293 static kmutex_t sd_label_mutex;
294 
295 /*
296  * sd_detach_mutex protects un_layer_count, un_detach_count, and
297  * un_opens_in_progress in the sd_lun structure.
298  */
299 static kmutex_t sd_detach_mutex;
300 
301 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
302 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
303 
304 /*
305  * Global buffer and mutex for debug logging
306  */
307 static char	sd_log_buf[1024];
308 static kmutex_t	sd_log_mutex;
309 
310 /*
311  * Structs and globals for recording attached lun information.
312  * This maintains a chain. Each node in the chain represents a SCSI controller.
313  * The structure records the number of luns attached to each target connected
314  * with the controller.
315  * For parallel scsi device only.
316  */
317 struct sd_scsi_hba_tgt_lun {
318 	struct sd_scsi_hba_tgt_lun	*next;
319 	dev_info_t			*pdip;
320 	int				nlun[NTARGETS_WIDE];
321 };
322 
323 /*
324  * Flag to indicate the lun is attached or detached
325  */
326 #define	SD_SCSI_LUN_ATTACH	0
327 #define	SD_SCSI_LUN_DETACH	1
328 
329 static kmutex_t	sd_scsi_target_lun_mutex;
330 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
331 
332 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
333     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
334 
335 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
336     sd_scsi_target_lun_head))
337 
338 /*
339  * "Smart" Probe Caching structs, globals, #defines, etc.
340  * For parallel scsi and non-self-identify device only.
341  */
342 
343 /*
344  * The following resources and routines are implemented to support
345  * "smart" probing, which caches the scsi_probe() results in an array,
346  * in order to help avoid long probe times.
347  */
348 struct sd_scsi_probe_cache {
349 	struct	sd_scsi_probe_cache	*next;
350 	dev_info_t	*pdip;
351 	int		cache[NTARGETS_WIDE];
352 };
353 
354 static kmutex_t	sd_scsi_probe_cache_mutex;
355 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
356 
357 /*
358  * Really we only need protection on the head of the linked list, but
359  * better safe than sorry.
360  */
361 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
362     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
363 
364 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
365     sd_scsi_probe_cache_head))
366 
367 /*
368  * Power attribute table
369  */
370 static sd_power_attr_ss sd_pwr_ss = {
371 	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
372 	{0, 100},
373 	{30, 0},
374 	{20000, 0}
375 };
376 
377 static sd_power_attr_pc sd_pwr_pc = {
378 	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
379 		"3=active", NULL },
380 	{0, 0, 0, 100},
381 	{90, 90, 20, 0},
382 	{15000, 15000, 1000, 0}
383 };
384 
385 /*
386  * Power level to power condition
387  */
388 static int sd_pl2pc[] = {
389 	SD_TARGET_START_VALID,
390 	SD_TARGET_STANDBY,
391 	SD_TARGET_IDLE,
392 	SD_TARGET_ACTIVE
393 };
394 
395 /*
396  * Vendor specific data name property declarations
397  */
398 
399 #if defined(__fibre) || defined(__x86)
400 
401 static sd_tunables seagate_properties = {
402 	SEAGATE_THROTTLE_VALUE,
403 	0,
404 	0,
405 	0,
406 	0,
407 	0,
408 	0,
409 	0,
410 	0
411 };
412 
413 
414 static sd_tunables fujitsu_properties = {
415 	FUJITSU_THROTTLE_VALUE,
416 	0,
417 	0,
418 	0,
419 	0,
420 	0,
421 	0,
422 	0,
423 	0
424 };
425 
426 static sd_tunables ibm_properties = {
427 	IBM_THROTTLE_VALUE,
428 	0,
429 	0,
430 	0,
431 	0,
432 	0,
433 	0,
434 	0,
435 	0
436 };
437 
438 static sd_tunables sve_properties = {
439 	SVE_THROTTLE_VALUE,
440 	0,
441 	0,
442 	SVE_BUSY_RETRIES,
443 	SVE_RESET_RETRY_COUNT,
444 	SVE_RESERVE_RELEASE_TIME,
445 	SVE_MIN_THROTTLE_VALUE,
446 	SVE_DISKSORT_DISABLED_FLAG,
447 	0
448 };
449 
450 static sd_tunables maserati_properties = {
451 	0,
452 	0,
453 	0,
454 	0,
455 	0,
456 	0,
457 	0,
458 	MASERATI_DISKSORT_DISABLED_FLAG,
459 	MASERATI_LUN_RESET_ENABLED_FLAG
460 };
461 
462 static sd_tunables pirus_properties = {
463 	PIRUS_THROTTLE_VALUE,
464 	0,
465 	PIRUS_NRR_COUNT,
466 	PIRUS_BUSY_RETRIES,
467 	PIRUS_RESET_RETRY_COUNT,
468 	0,
469 	PIRUS_MIN_THROTTLE_VALUE,
470 	PIRUS_DISKSORT_DISABLED_FLAG,
471 	PIRUS_LUN_RESET_ENABLED_FLAG
472 };
473 
474 #endif
475 
476 #if (defined(__sparc) && !defined(__fibre)) || \
477 	(defined(__x86))
478 
479 
480 static sd_tunables elite_properties = {
481 	ELITE_THROTTLE_VALUE,
482 	0,
483 	0,
484 	0,
485 	0,
486 	0,
487 	0,
488 	0,
489 	0
490 };
491 
492 static sd_tunables st31200n_properties = {
493 	ST31200N_THROTTLE_VALUE,
494 	0,
495 	0,
496 	0,
497 	0,
498 	0,
499 	0,
500 	0,
501 	0
502 };
503 
504 #endif /* Fibre or not */
505 
506 static sd_tunables lsi_properties_scsi = {
507 	LSI_THROTTLE_VALUE,
508 	0,
509 	LSI_NOTREADY_RETRIES,
510 	0,
511 	0,
512 	0,
513 	0,
514 	0,
515 	0
516 };
517 
518 static sd_tunables symbios_properties = {
519 	SYMBIOS_THROTTLE_VALUE,
520 	0,
521 	SYMBIOS_NOTREADY_RETRIES,
522 	0,
523 	0,
524 	0,
525 	0,
526 	0,
527 	0
528 };
529 
530 static sd_tunables lsi_properties = {
531 	0,
532 	0,
533 	LSI_NOTREADY_RETRIES,
534 	0,
535 	0,
536 	0,
537 	0,
538 	0,
539 	0
540 };
541 
542 static sd_tunables lsi_oem_properties = {
543 	0,
544 	0,
545 	LSI_OEM_NOTREADY_RETRIES,
546 	0,
547 	0,
548 	0,
549 	0,
550 	0,
551 	0,
552 	1
553 };
554 
555 
556 
557 #if (defined(SD_PROP_TST))
558 
559 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
560 #define	SD_TST_THROTTLE_VAL	16
561 #define	SD_TST_NOTREADY_VAL	12
562 #define	SD_TST_BUSY_VAL		60
563 #define	SD_TST_RST_RETRY_VAL	36
564 #define	SD_TST_RSV_REL_TIME	60
565 
566 static sd_tunables tst_properties = {
567 	SD_TST_THROTTLE_VAL,
568 	SD_TST_CTYPE_VAL,
569 	SD_TST_NOTREADY_VAL,
570 	SD_TST_BUSY_VAL,
571 	SD_TST_RST_RETRY_VAL,
572 	SD_TST_RSV_REL_TIME,
573 	0,
574 	0,
575 	0
576 };
577 #endif
578 
579 /* This is similar to the ANSI toupper implementation */
580 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
581 
582 /*
583  * Static Driver Configuration Table
584  *
585  * This is the table of disks which need throttle adjustment (or, perhaps
586  * something else as defined by the flags at a future time.)  device_id
587  * is a string consisting of concatenated vid (vendor), pid (product/model)
588  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
589  * the parts of the string are as defined by the sizes in the scsi_inquiry
590  * structure.  Device type is searched as far as the device_id string is
591  * defined.  Flags defines which values are to be set in the driver from the
592  * properties list.
593  *
594  * Entries below which begin and end with a "*" are a special case.
595  * These do not have a specific vendor, and the string which follows
596  * can appear anywhere in the 16 byte PID portion of the inquiry data.
597  *
598  * Entries below which begin and end with a " " (blank) are a special
599  * case. The comparison function will treat multiple consecutive blanks
600  * as equivalent to a single blank. For example, this causes a
601  * sd_disk_table entry of " NEC CDROM " to match a device's id string
602  * of  "NEC       CDROM".
603  *
604  * Note: The MD21 controller type has been obsoleted.
605  *	 ST318202F is a Legacy device
606  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
607  *	 made with an FC connection. The entries here are a legacy.
608  */
609 static sd_disk_config_t sd_disk_table[] = {
610 #if defined(__fibre) || defined(__x86)
611 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
612 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
613 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
614 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
615 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
616 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
617 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
618 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
626 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
627 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
628 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
629 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
630 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
631 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
632 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
633 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
635 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
636 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
637 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
638 	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
639 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
640 	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
641 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
642 	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
643 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
644 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
645 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
661 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
662 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
663 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
664 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
665 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
666 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
667 		SD_CONF_BSET_BSY_RETRY_COUNT|
668 		SD_CONF_BSET_RST_RETRIES|
669 		SD_CONF_BSET_RSV_REL_TIME|
670 		SD_CONF_BSET_MIN_THROTTLE|
671 		SD_CONF_BSET_DISKSORT_DISABLED,
672 		&sve_properties },
673 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
674 		SD_CONF_BSET_LUN_RESET_ENABLED,
675 		&maserati_properties },
676 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
677 		SD_CONF_BSET_NRR_COUNT|
678 		SD_CONF_BSET_BSY_RETRY_COUNT|
679 		SD_CONF_BSET_RST_RETRIES|
680 		SD_CONF_BSET_MIN_THROTTLE|
681 		SD_CONF_BSET_DISKSORT_DISABLED|
682 		SD_CONF_BSET_LUN_RESET_ENABLED,
683 		&pirus_properties },
684 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
685 		SD_CONF_BSET_NRR_COUNT|
686 		SD_CONF_BSET_BSY_RETRY_COUNT|
687 		SD_CONF_BSET_RST_RETRIES|
688 		SD_CONF_BSET_MIN_THROTTLE|
689 		SD_CONF_BSET_DISKSORT_DISABLED|
690 		SD_CONF_BSET_LUN_RESET_ENABLED,
691 		&pirus_properties },
692 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
693 		SD_CONF_BSET_NRR_COUNT|
694 		SD_CONF_BSET_BSY_RETRY_COUNT|
695 		SD_CONF_BSET_RST_RETRIES|
696 		SD_CONF_BSET_MIN_THROTTLE|
697 		SD_CONF_BSET_DISKSORT_DISABLED|
698 		SD_CONF_BSET_LUN_RESET_ENABLED,
699 		&pirus_properties },
700 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
701 		SD_CONF_BSET_NRR_COUNT|
702 		SD_CONF_BSET_BSY_RETRY_COUNT|
703 		SD_CONF_BSET_RST_RETRIES|
704 		SD_CONF_BSET_MIN_THROTTLE|
705 		SD_CONF_BSET_DISKSORT_DISABLED|
706 		SD_CONF_BSET_LUN_RESET_ENABLED,
707 		&pirus_properties },
708 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
709 		SD_CONF_BSET_NRR_COUNT|
710 		SD_CONF_BSET_BSY_RETRY_COUNT|
711 		SD_CONF_BSET_RST_RETRIES|
712 		SD_CONF_BSET_MIN_THROTTLE|
713 		SD_CONF_BSET_DISKSORT_DISABLED|
714 		SD_CONF_BSET_LUN_RESET_ENABLED,
715 		&pirus_properties },
716 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
717 		SD_CONF_BSET_NRR_COUNT|
718 		SD_CONF_BSET_BSY_RETRY_COUNT|
719 		SD_CONF_BSET_RST_RETRIES|
720 		SD_CONF_BSET_MIN_THROTTLE|
721 		SD_CONF_BSET_DISKSORT_DISABLED|
722 		SD_CONF_BSET_LUN_RESET_ENABLED,
723 		&pirus_properties },
724 	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
725 	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
726 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
727 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
728 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
729 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
730 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
731 #endif /* fibre or NON-sparc platforms */
732 #if ((defined(__sparc) && !defined(__fibre)) ||\
733 	(defined(__x86)))
734 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
735 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
736 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
737 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
738 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
739 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
740 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
741 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
742 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
743 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
744 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
745 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
746 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
747 	    &symbios_properties },
748 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
749 	    &lsi_properties_scsi },
750 #if defined(__x86)
751 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
752 				    | SD_CONF_BSET_READSUB_BCD
753 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
754 				    | SD_CONF_BSET_NO_READ_HEADER
755 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
756 
757 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
758 				    | SD_CONF_BSET_READSUB_BCD
759 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
760 				    | SD_CONF_BSET_NO_READ_HEADER
761 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
762 #endif /* __x86 */
763 #endif /* sparc NON-fibre or NON-sparc platforms */
764 
765 #if (defined(SD_PROP_TST))
766 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
767 				| SD_CONF_BSET_CTYPE
768 				| SD_CONF_BSET_NRR_COUNT
769 				| SD_CONF_BSET_FAB_DEVID
770 				| SD_CONF_BSET_NOCACHE
771 				| SD_CONF_BSET_BSY_RETRY_COUNT
772 				| SD_CONF_BSET_PLAYMSF_BCD
773 				| SD_CONF_BSET_READSUB_BCD
774 				| SD_CONF_BSET_READ_TOC_TRK_BCD
775 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
776 				| SD_CONF_BSET_NO_READ_HEADER
777 				| SD_CONF_BSET_READ_CD_XD4
778 				| SD_CONF_BSET_RST_RETRIES
779 				| SD_CONF_BSET_RSV_REL_TIME
780 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
781 #endif
782 };
783 
784 static const int sd_disk_table_size =
785 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
786 
787 /*
788  * Emulation mode disk drive VID/PID table
789  */
790 static char sd_flash_dev_table[][25] = {
791 	"ATA     MARVELL SD88SA02",
792 	"MARVELL SD88SA02",
793 	"TOSHIBA THNSNV05",
794 };
795 
796 static const int sd_flash_dev_table_size =
797 	sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
798 
799 #define	SD_INTERCONNECT_PARALLEL	0
800 #define	SD_INTERCONNECT_FABRIC		1
801 #define	SD_INTERCONNECT_FIBRE		2
802 #define	SD_INTERCONNECT_SSA		3
803 #define	SD_INTERCONNECT_SATA		4
804 #define	SD_INTERCONNECT_SAS		5
805 
806 #define	SD_IS_PARALLEL_SCSI(un)		\
807 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
808 #define	SD_IS_SERIAL(un)		\
809 	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
810 	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
811 
812 /*
813  * Definitions used by device id registration routines
814  */
815 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
816 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
817 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
818 
819 static kmutex_t sd_sense_mutex = {0};
820 
821 /*
822  * Macros for updates of the driver state
823  */
824 #define	New_state(un, s)        \
825 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
826 #define	Restore_state(un)	\
827 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
828 
829 static struct sd_cdbinfo sd_cdbtab[] = {
830 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
831 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
832 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
833 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
834 };
835 
836 /*
837  * Specifies the number of seconds that must have elapsed since the last
838  * cmd. has completed for a device to be declared idle to the PM framework.
839  */
840 static int sd_pm_idletime = 1;
841 
842 /*
843  * Internal function prototypes
844  */
845 
846 #if (defined(__fibre))
847 /*
848  * These #defines are to avoid namespace collisions that occur because this
849  * code is currently used to compile two separate driver modules: sd and ssd.
850  * All function names need to be treated this way (even if declared static)
851  * in order to allow the debugger to resolve the names properly.
852  * It is anticipated that in the near future the ssd module will be obsoleted,
853  * at which time this ugliness should go away.
854  */
855 #define	sd_log_trace			ssd_log_trace
856 #define	sd_log_info			ssd_log_info
857 #define	sd_log_err			ssd_log_err
858 #define	sdprobe				ssdprobe
859 #define	sdinfo				ssdinfo
860 #define	sd_prop_op			ssd_prop_op
861 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
862 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
863 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
864 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
865 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
866 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
867 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
868 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
869 #define	sd_spin_up_unit			ssd_spin_up_unit
870 #define	sd_enable_descr_sense		ssd_enable_descr_sense
871 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
872 #define	sd_set_mmc_caps			ssd_set_mmc_caps
873 #define	sd_read_unit_properties		ssd_read_unit_properties
874 #define	sd_process_sdconf_file		ssd_process_sdconf_file
875 #define	sd_process_sdconf_table		ssd_process_sdconf_table
876 #define	sd_sdconf_id_match		ssd_sdconf_id_match
877 #define	sd_blank_cmp			ssd_blank_cmp
878 #define	sd_chk_vers1_data		ssd_chk_vers1_data
879 #define	sd_set_vers1_properties		ssd_set_vers1_properties
880 #define	sd_check_bdc_vpd		ssd_check_bdc_vpd
881 #define	sd_check_emulation_mode		ssd_check_emulation_mode
882 
883 #define	sd_get_physical_geometry	ssd_get_physical_geometry
884 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
885 #define	sd_update_block_info		ssd_update_block_info
886 #define	sd_register_devid		ssd_register_devid
887 #define	sd_get_devid			ssd_get_devid
888 #define	sd_create_devid			ssd_create_devid
889 #define	sd_write_deviceid		ssd_write_deviceid
890 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
891 #define	sd_setup_pm			ssd_setup_pm
892 #define	sd_create_pm_components		ssd_create_pm_components
893 #define	sd_ddi_suspend			ssd_ddi_suspend
894 #define	sd_ddi_resume			ssd_ddi_resume
895 #define	sd_pm_state_change		ssd_pm_state_change
896 #define	sdpower				ssdpower
897 #define	sdattach			ssdattach
898 #define	sddetach			ssddetach
899 #define	sd_unit_attach			ssd_unit_attach
900 #define	sd_unit_detach			ssd_unit_detach
901 #define	sd_set_unit_attributes		ssd_set_unit_attributes
902 #define	sd_create_errstats		ssd_create_errstats
903 #define	sd_set_errstats			ssd_set_errstats
904 #define	sd_set_pstats			ssd_set_pstats
905 #define	sddump				ssddump
906 #define	sd_scsi_poll			ssd_scsi_poll
907 #define	sd_send_polled_RQS		ssd_send_polled_RQS
908 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
909 #define	sd_init_event_callbacks		ssd_init_event_callbacks
910 #define	sd_event_callback		ssd_event_callback
911 #define	sd_cache_control		ssd_cache_control
912 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
913 #define	sd_get_write_cache_changeable	ssd_get_write_cache_changeable
914 #define	sd_get_nv_sup			ssd_get_nv_sup
915 #define	sd_make_device			ssd_make_device
916 #define	sdopen				ssdopen
917 #define	sdclose				ssdclose
918 #define	sd_ready_and_valid		ssd_ready_and_valid
919 #define	sdmin				ssdmin
920 #define	sdread				ssdread
921 #define	sdwrite				ssdwrite
922 #define	sdaread				ssdaread
923 #define	sdawrite			ssdawrite
924 #define	sdstrategy			ssdstrategy
925 #define	sdioctl				ssdioctl
926 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
927 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
928 #define	sd_checksum_iostart		ssd_checksum_iostart
929 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
930 #define	sd_pm_iostart			ssd_pm_iostart
931 #define	sd_core_iostart			ssd_core_iostart
932 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
933 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
934 #define	sd_checksum_iodone		ssd_checksum_iodone
935 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
936 #define	sd_pm_iodone			ssd_pm_iodone
937 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
938 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
939 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
940 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
941 #define	sd_buf_iodone			ssd_buf_iodone
942 #define	sd_uscsi_strategy		ssd_uscsi_strategy
943 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
944 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
945 #define	sd_uscsi_iodone			ssd_uscsi_iodone
946 #define	sd_xbuf_strategy		ssd_xbuf_strategy
947 #define	sd_xbuf_init			ssd_xbuf_init
948 #define	sd_pm_entry			ssd_pm_entry
949 #define	sd_pm_exit			ssd_pm_exit
950 
951 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
952 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
953 
954 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
955 #define	sdintr				ssdintr
956 #define	sd_start_cmds			ssd_start_cmds
957 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
958 #define	sd_bioclone_alloc		ssd_bioclone_alloc
959 #define	sd_bioclone_free		ssd_bioclone_free
960 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
961 #define	sd_shadow_buf_free		ssd_shadow_buf_free
962 #define	sd_print_transport_rejected_message	\
963 					ssd_print_transport_rejected_message
964 #define	sd_retry_command		ssd_retry_command
965 #define	sd_set_retry_bp			ssd_set_retry_bp
966 #define	sd_send_request_sense_command	ssd_send_request_sense_command
967 #define	sd_start_retry_command		ssd_start_retry_command
968 #define	sd_start_direct_priority_command	\
969 					ssd_start_direct_priority_command
970 #define	sd_return_failed_command	ssd_return_failed_command
971 #define	sd_return_failed_command_no_restart	\
972 					ssd_return_failed_command_no_restart
973 #define	sd_return_command		ssd_return_command
974 #define	sd_sync_with_callback		ssd_sync_with_callback
975 #define	sdrunout			ssdrunout
976 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
977 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
978 #define	sd_reduce_throttle		ssd_reduce_throttle
979 #define	sd_restore_throttle		ssd_restore_throttle
980 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
981 #define	sd_init_cdb_limits		ssd_init_cdb_limits
982 #define	sd_pkt_status_good		ssd_pkt_status_good
983 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
984 #define	sd_pkt_status_busy		ssd_pkt_status_busy
985 #define	sd_pkt_status_reservation_conflict	\
986 					ssd_pkt_status_reservation_conflict
987 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
988 #define	sd_handle_request_sense		ssd_handle_request_sense
989 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
990 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
991 #define	sd_validate_sense_data		ssd_validate_sense_data
992 #define	sd_decode_sense			ssd_decode_sense
993 #define	sd_print_sense_msg		ssd_print_sense_msg
994 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
995 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
996 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
997 #define	sd_sense_key_medium_or_hardware_error	\
998 					ssd_sense_key_medium_or_hardware_error
999 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1000 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1001 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1002 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1003 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1004 #define	sd_sense_key_default		ssd_sense_key_default
1005 #define	sd_print_retry_msg		ssd_print_retry_msg
1006 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1007 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1008 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1009 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1010 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1011 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1012 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1013 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1014 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1015 #define	sd_reset_target			ssd_reset_target
1016 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1017 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1018 #define	sd_taskq_create			ssd_taskq_create
1019 #define	sd_taskq_delete			ssd_taskq_delete
1020 #define	sd_target_change_task		ssd_target_change_task
1021 #define	sd_log_dev_status_event		ssd_log_dev_status_event
1022 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1023 #define	sd_log_eject_request_event	ssd_log_eject_request_event
1024 #define	sd_media_change_task		ssd_media_change_task
1025 #define	sd_handle_mchange		ssd_handle_mchange
1026 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1027 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1028 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1029 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1030 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1031 					sd_send_scsi_feature_GET_CONFIGURATION
1032 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1033 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1034 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1035 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1036 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1037 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1038 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1039 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1040 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1041 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1042 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1043 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1044 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1045 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1046 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1047 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1048 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1049 #define	sd_alloc_rqs			ssd_alloc_rqs
1050 #define	sd_free_rqs			ssd_free_rqs
1051 #define	sd_dump_memory			ssd_dump_memory
1052 #define	sd_get_media_info_com		ssd_get_media_info_com
1053 #define	sd_get_media_info		ssd_get_media_info
1054 #define	sd_get_media_info_ext		ssd_get_media_info_ext
1055 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1056 #define	sd_nvpair_str_decode		ssd_nvpair_str_decode
1057 #define	sd_set_properties		ssd_set_properties
1058 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1059 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1060 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1061 #define	sd_check_mhd			ssd_check_mhd
1062 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1063 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1064 #define	sd_sname			ssd_sname
1065 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1066 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1067 #define	sd_take_ownership		ssd_take_ownership
1068 #define	sd_reserve_release		ssd_reserve_release
1069 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1070 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1071 #define	sd_persistent_reservation_in_read_keys	\
1072 					ssd_persistent_reservation_in_read_keys
1073 #define	sd_persistent_reservation_in_read_resv	\
1074 					ssd_persistent_reservation_in_read_resv
1075 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1076 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1077 #define	sd_mhdioc_release		ssd_mhdioc_release
1078 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1079 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1080 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1081 #define	sr_change_blkmode		ssr_change_blkmode
1082 #define	sr_change_speed			ssr_change_speed
1083 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1084 #define	sr_pause_resume			ssr_pause_resume
1085 #define	sr_play_msf			ssr_play_msf
1086 #define	sr_play_trkind			ssr_play_trkind
1087 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1088 #define	sr_read_subchannel		ssr_read_subchannel
1089 #define	sr_read_tocentry		ssr_read_tocentry
1090 #define	sr_read_tochdr			ssr_read_tochdr
1091 #define	sr_read_cdda			ssr_read_cdda
1092 #define	sr_read_cdxa			ssr_read_cdxa
1093 #define	sr_read_mode1			ssr_read_mode1
1094 #define	sr_read_mode2			ssr_read_mode2
1095 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1096 #define	sr_sector_mode			ssr_sector_mode
1097 #define	sr_eject			ssr_eject
1098 #define	sr_ejected			ssr_ejected
1099 #define	sr_check_wp			ssr_check_wp
1100 #define	sd_watch_request_submit		ssd_watch_request_submit
1101 #define	sd_check_media			ssd_check_media
1102 #define	sd_media_watch_cb		ssd_media_watch_cb
1103 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1104 #define	sr_volume_ctrl			ssr_volume_ctrl
1105 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1106 #define	sd_log_page_supported		ssd_log_page_supported
1107 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1108 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1109 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1110 #define	sd_range_lock			ssd_range_lock
1111 #define	sd_get_range			ssd_get_range
1112 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1113 #define	sd_range_unlock			ssd_range_unlock
1114 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1115 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1116 
1117 #define	sd_iostart_chain		ssd_iostart_chain
1118 #define	sd_iodone_chain			ssd_iodone_chain
1119 #define	sd_initpkt_map			ssd_initpkt_map
1120 #define	sd_destroypkt_map		ssd_destroypkt_map
1121 #define	sd_chain_type_map		ssd_chain_type_map
1122 #define	sd_chain_index_map		ssd_chain_index_map
1123 
1124 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1125 #define	sd_failfast_flushq		ssd_failfast_flushq
1126 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1127 
1128 #define	sd_is_lsi			ssd_is_lsi
1129 #define	sd_tg_rdwr			ssd_tg_rdwr
1130 #define	sd_tg_getinfo			ssd_tg_getinfo
1131 #define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
1132 
1133 #endif	/* #if (defined(__fibre)) */
1134 
1135 typedef struct unmap_param_hdr_s {
1136 	uint16_t	uph_data_len;
1137 	uint16_t	uph_descr_data_len;
1138 	uint32_t	uph_reserved;
1139 } unmap_param_hdr_t;
1140 
1141 typedef struct unmap_blk_descr_s {
1142 	uint64_t	ubd_lba;
1143 	uint32_t	ubd_lba_cnt;
1144 	uint32_t	ubd_reserved;
1145 } unmap_blk_descr_t;
1146 
1147 /* Max number of block descriptors in UNMAP command */
1148 #define	SD_UNMAP_MAX_DESCR \
1149 	((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
1150 /* Max size of the UNMAP parameter list in bytes */
1151 #define	SD_UNMAP_PARAM_LIST_MAXSZ	(sizeof (unmap_param_hdr_t) + \
1152 	SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
1153 
1154 int _init(void);
1155 int _fini(void);
1156 int _info(struct modinfo *modinfop);
1157 
1158 /*PRINTFLIKE3*/
1159 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1160 /*PRINTFLIKE3*/
1161 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1162 /*PRINTFLIKE3*/
1163 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1164 
1165 static int sdprobe(dev_info_t *devi);
1166 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1167     void **result);
1168 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1169     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1170 
1171 /*
1172  * Smart probe for parallel scsi
1173  */
1174 static void sd_scsi_probe_cache_init(void);
1175 static void sd_scsi_probe_cache_fini(void);
1176 static void sd_scsi_clear_probe_cache(void);
1177 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1178 
1179 /*
1180  * Attached luns on target for parallel scsi
1181  */
1182 static void sd_scsi_target_lun_init(void);
1183 static void sd_scsi_target_lun_fini(void);
1184 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1185 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1186 
1187 static int sd_spin_up_unit(sd_ssc_t *ssc);
1188 
1189 /*
1190  * Using sd_ssc_init to establish sd_ssc_t struct
1191  * Using sd_ssc_send to send uscsi internal command
1192  * Using sd_ssc_fini to free sd_ssc_t struct
1193  */
1194 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1195 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1196     int flag, enum uio_seg dataspace, int path_flag);
1197 static void sd_ssc_fini(sd_ssc_t *ssc);
1198 
1199 /*
1200  * Using sd_ssc_assessment to set correct type-of-assessment
1201  * Using sd_ssc_post to post ereport & system log
1202  *       sd_ssc_post will call sd_ssc_print to print system log
1203  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1204  */
1205 static void sd_ssc_assessment(sd_ssc_t *ssc,
1206     enum sd_type_assessment tp_assess);
1207 
1208 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1209 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1210 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1211     enum sd_driver_assessment drv_assess);
1212 
1213 /*
1214  * Using sd_ssc_set_info to mark an un-decodable-data error.
1215  * Using sd_ssc_extract_info to transfer information from internal
1216  *       data structures to sd_ssc_t.
1217  */
1218 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1219     const char *fmt, ...);
1220 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1221     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1222 
1223 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1224     enum uio_seg dataspace, int path_flag);
1225 
1226 #ifdef _LP64
1227 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1228 static void	sd_reenable_dsense_task(void *arg);
1229 #endif /* _LP64 */
1230 
1231 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1232 
1233 static void sd_read_unit_properties(struct sd_lun *un);
1234 static int  sd_process_sdconf_file(struct sd_lun *un);
1235 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1236 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1237 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1238     int *data_list, sd_tunables *values);
1239 static void sd_process_sdconf_table(struct sd_lun *un);
1240 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1241 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1242 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1243     int list_len, char *dataname_ptr);
1244 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1245     sd_tunables *prop_list);
1246 
1247 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1248     int reservation_flag);
1249 static int  sd_get_devid(sd_ssc_t *ssc);
1250 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1251 static int  sd_write_deviceid(sd_ssc_t *ssc);
1252 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1253 
1254 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1255 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1256 
1257 static int  sd_ddi_suspend(dev_info_t *devi);
1258 static int  sd_ddi_resume(dev_info_t *devi);
1259 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1260 static int  sdpower(dev_info_t *devi, int component, int level);
1261 
1262 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1263 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1264 static int  sd_unit_attach(dev_info_t *devi);
1265 static int  sd_unit_detach(dev_info_t *devi);
1266 
1267 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1268 static void sd_create_errstats(struct sd_lun *un, int instance);
1269 static void sd_set_errstats(struct sd_lun *un);
1270 static void sd_set_pstats(struct sd_lun *un);
1271 
1272 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1273 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1274 static int  sd_send_polled_RQS(struct sd_lun *un);
1275 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1276 
1277 #if (defined(__fibre))
1278 /*
1279  * Event callbacks (photon)
1280  */
1281 static void sd_init_event_callbacks(struct sd_lun *un);
1282 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1283 #endif
1284 
1285 /*
1286  * Defines for sd_cache_control
1287  */
1288 
1289 #define	SD_CACHE_ENABLE		1
1290 #define	SD_CACHE_DISABLE	0
1291 #define	SD_CACHE_NOCHANGE	-1
1292 
1293 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1294 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1295 static void  sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
1296 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1297 static dev_t sd_make_device(dev_info_t *devi);
1298 static void  sd_check_bdc_vpd(sd_ssc_t *ssc);
1299 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1300 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1301     uint64_t capacity);
1302 
1303 /*
1304  * Driver entry point functions.
1305  */
1306 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1307 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1308 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1309 
1310 static void sdmin(struct buf *bp);
1311 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1312 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1313 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1314 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1315 
1316 static int sdstrategy(struct buf *bp);
1317 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1318 
1319 /*
1320  * Function prototypes for layering functions in the iostart chain.
1321  */
1322 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1323     struct buf *bp);
1324 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1325     struct buf *bp);
1326 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1327 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1328     struct buf *bp);
1329 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1330 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1331 
1332 /*
1333  * Function prototypes for layering functions in the iodone chain.
1334  */
1335 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1336 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1337 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1338     struct buf *bp);
1339 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1340     struct buf *bp);
1341 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1342 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1343     struct buf *bp);
1344 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1345 
1346 /*
1347  * Prototypes for functions to support buf(9S) based IO.
1348  */
1349 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1350 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1351 static void sd_destroypkt_for_buf(struct buf *);
1352 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1353     struct buf *bp, int flags,
1354     int (*callback)(caddr_t), caddr_t callback_arg,
1355     diskaddr_t lba, uint32_t blockcount);
1356 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1357     struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1358 
1359 /*
1360  * Prototypes for functions to support USCSI IO.
1361  */
1362 static int sd_uscsi_strategy(struct buf *bp);
1363 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1364 static void sd_destroypkt_for_uscsi(struct buf *);
1365 
1366 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1367     uchar_t chain_type, void *pktinfop);
1368 
1369 static int  sd_pm_entry(struct sd_lun *un);
1370 static void sd_pm_exit(struct sd_lun *un);
1371 
1372 static void sd_pm_idletimeout_handler(void *arg);
1373 
1374 /*
1375  * sd_core internal functions (used at the sd_core_io layer).
1376  */
1377 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1378 static void sdintr(struct scsi_pkt *pktp);
1379 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1380 
1381 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1382     enum uio_seg dataspace, int path_flag);
1383 
1384 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1385     daddr_t blkno, int (*func)(struct buf *));
1386 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1387     uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1388 static void sd_bioclone_free(struct buf *bp);
1389 static void sd_shadow_buf_free(struct buf *bp);
1390 
1391 static void sd_print_transport_rejected_message(struct sd_lun *un,
1392     struct sd_xbuf *xp, int code);
1393 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1394     void *arg, int code);
1395 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1396     void *arg, int code);
1397 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1398     void *arg, int code);
1399 
1400 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1401     int retry_check_flag,
1402     void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int c),
1403     void *user_arg, int failure_code,  clock_t retry_delay,
1404     void (*statp)(kstat_io_t *));
1405 
1406 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1407     clock_t retry_delay, void (*statp)(kstat_io_t *));
1408 
1409 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1410     struct scsi_pkt *pktp);
1411 static void sd_start_retry_command(void *arg);
1412 static void sd_start_direct_priority_command(void *arg);
1413 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1414     int errcode);
1415 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1416     struct buf *bp, int errcode);
1417 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1418 static void sd_sync_with_callback(struct sd_lun *un);
1419 static int sdrunout(caddr_t arg);
1420 
1421 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1422 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1423 
1424 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1425 static void sd_restore_throttle(void *arg);
1426 
1427 static void sd_init_cdb_limits(struct sd_lun *un);
1428 
1429 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1430     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1431 
1432 /*
1433  * Error handling functions
1434  */
1435 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1436     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1437 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1438     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1439 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1440     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1441 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1442     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1443 
1444 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1445     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1446 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1447     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1448 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1449     struct sd_xbuf *xp, size_t actual_len);
1450 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1451     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1452 
1453 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1454     void *arg, int code);
1455 
1456 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1457     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1458 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1459     uint8_t *sense_datap,
1460     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1461 static void sd_sense_key_not_ready(struct sd_lun *un,
1462     uint8_t *sense_datap,
1463     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1464 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1465     uint8_t *sense_datap,
1466     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1467 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1468     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1469 static void sd_sense_key_unit_attention(struct sd_lun *un,
1470     uint8_t *sense_datap,
1471     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1472 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1473     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1474 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1475     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1476 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1477     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1478 static void sd_sense_key_default(struct sd_lun *un,
1479     uint8_t *sense_datap,
1480     struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1481 
1482 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1483     void *arg, int flag);
1484 
1485 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1486     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1487 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1488     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1489 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1490     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1491 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1492     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1493 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1494     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1495 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1496     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1497 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1498     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1499 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1500     struct sd_xbuf *xp, struct scsi_pkt *pktp);
1501 
1502 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1503 
1504 static void sd_start_stop_unit_callback(void *arg);
1505 static void sd_start_stop_unit_task(void *arg);
1506 
1507 static void sd_taskq_create(void);
1508 static void sd_taskq_delete(void);
1509 static void sd_target_change_task(void *arg);
1510 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1511 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1512 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1513 static void sd_media_change_task(void *arg);
1514 
1515 static int sd_handle_mchange(struct sd_lun *un);
1516 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1517 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1518     uint32_t *lbap, int path_flag);
1519 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1520     uint32_t *lbap, uint32_t *psp, int path_flag);
1521 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1522     int flag, int path_flag);
1523 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1524     size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1525 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1526 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1527     uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1528 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1529     uchar_t usr_cmd, uchar_t *usr_bufp);
1530 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1531     struct dk_callback *dkc);
1532 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1533 static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
1534     int flag);
1535 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1536     struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1537     uchar_t *bufaddr, uint_t buflen, int path_flag);
1538 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1539     struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1540     uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1541 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1542     uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1543 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1544     uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1545 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1546     size_t buflen, daddr_t start_block, int path_flag);
1547 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1548     sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1549     path_flag)
1550 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1551     sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1552     path_flag)
1553 
1554 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1555     uint16_t buflen, uchar_t page_code, uchar_t page_control,
1556     uint16_t param_ptr, int path_flag);
1557 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1558     uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1559 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1560 
1561 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1562 static void sd_free_rqs(struct sd_lun *un);
1563 
1564 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1565     uchar_t *data, int len, int fmt);
1566 static void sd_panic_for_res_conflict(struct sd_lun *un);
1567 
1568 /*
1569  * Disk Ioctl Function Prototypes
1570  */
1571 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1572 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1573 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1574 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1575 
1576 /*
1577  * Multi-host Ioctl Prototypes
1578  */
1579 static int sd_check_mhd(dev_t dev, int interval);
1580 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1581 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1582 static char *sd_sname(uchar_t status);
1583 static void sd_mhd_resvd_recover(void *arg);
1584 static void sd_resv_reclaim_thread();
1585 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1586 static int sd_reserve_release(dev_t dev, int cmd);
1587 static void sd_rmv_resv_reclaim_req(dev_t dev);
1588 static void sd_mhd_reset_notify_cb(caddr_t arg);
1589 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1590     mhioc_inkeys_t *usrp, int flag);
1591 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1592     mhioc_inresvs_t *usrp, int flag);
1593 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1594 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1595 static int sd_mhdioc_release(dev_t dev);
1596 static int sd_mhdioc_register_devid(dev_t dev);
1597 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1598 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1599 
1600 /*
1601  * SCSI removable prototypes
1602  */
1603 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1604 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1605 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1606 static int sr_pause_resume(dev_t dev, int mode);
1607 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1608 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1609 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1610 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1616 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1617 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1618 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1619 static int sr_eject(dev_t dev);
1620 static void sr_ejected(register struct sd_lun *un);
1621 static int sr_check_wp(dev_t dev);
1622 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1623 static int sd_check_media(dev_t dev, enum dkio_state state);
1624 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1625 static void sd_delayed_cv_broadcast(void *arg);
1626 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1627 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1628 
1629 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1630 
1631 /*
1632  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1633  */
1634 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1635 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1636 static void sd_wm_cache_destructor(void *wm, void *un);
1637 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1638     daddr_t endb, ushort_t typ);
1639 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1640     daddr_t endb);
1641 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1642 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1643 static void sd_read_modify_write_task(void * arg);
1644 static int
1645 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1646     struct buf **bpp);
1647 
1648 
1649 /*
1650  * Function prototypes for failfast support.
1651  */
1652 static void sd_failfast_flushq(struct sd_lun *un);
1653 static int sd_failfast_flushq_callback(struct buf *bp);
1654 
1655 /*
1656  * Function prototypes to check for lsi devices
1657  */
1658 static void sd_is_lsi(struct sd_lun *un);
1659 
1660 /*
1661  * Function prototypes for partial DMA support
1662  */
1663 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1664 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1665 
1666 
1667 /* Function prototypes for cmlb */
1668 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1669     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1670 
1671 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1672 
1673 /*
1674  * For printing RMW warning message timely
1675  */
1676 static void sd_rmw_msg_print_handler(void *arg);
1677 
1678 /*
1679  * Constants for failfast support:
1680  *
1681  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1682  * failfast processing being performed.
1683  *
1684  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1685  * failfast processing on all bufs with B_FAILFAST set.
1686  */
1687 
1688 #define	SD_FAILFAST_INACTIVE		0
1689 #define	SD_FAILFAST_ACTIVE		1
1690 
1691 /*
1692  * Bitmask to control behavior of buf(9S) flushes when a transition to
1693  * the failfast state occurs. Optional bits include:
1694  *
1695  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1696  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1697  * be flushed.
1698  *
1699  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1700  * driver, in addition to the regular wait queue. This includes the xbuf
1701  * queues. When clear, only the driver's wait queue will be flushed.
1702  */
1703 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1704 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1705 
1706 /*
1707  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1708  * to flush all queues within the driver.
1709  */
1710 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1711 
1712 
1713 /*
1714  * SD Testing Fault Injection
1715  */
1716 #ifdef SD_FAULT_INJECTION
1717 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1718 static void sd_faultinjection(struct scsi_pkt *pktp);
1719 static void sd_injection_log(char *buf, struct sd_lun *un);
1720 #endif
1721 
1722 /*
1723  * Device driver ops vector
1724  */
1725 static struct cb_ops sd_cb_ops = {
1726 	sdopen,			/* open */
1727 	sdclose,		/* close */
1728 	sdstrategy,		/* strategy */
1729 	nodev,			/* print */
1730 	sddump,			/* dump */
1731 	sdread,			/* read */
1732 	sdwrite,		/* write */
1733 	sdioctl,		/* ioctl */
1734 	nodev,			/* devmap */
1735 	nodev,			/* mmap */
1736 	nodev,			/* segmap */
1737 	nochpoll,		/* poll */
1738 	sd_prop_op,		/* cb_prop_op */
1739 	0,			/* streamtab  */
1740 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1741 	CB_REV,			/* cb_rev */
1742 	sdaread,		/* async I/O read entry point */
1743 	sdawrite		/* async I/O write entry point */
1744 };
1745 
1746 struct dev_ops sd_ops = {
1747 	DEVO_REV,		/* devo_rev, */
1748 	0,			/* refcnt  */
1749 	sdinfo,			/* info */
1750 	nulldev,		/* identify */
1751 	sdprobe,		/* probe */
1752 	sdattach,		/* attach */
1753 	sddetach,		/* detach */
1754 	nodev,			/* reset */
1755 	&sd_cb_ops,		/* driver operations */
1756 	NULL,			/* bus operations */
1757 	sdpower,		/* power */
1758 	ddi_quiesce_not_needed,		/* quiesce */
1759 };
1760 
1761 /*
1762  * This is the loadable module wrapper.
1763  */
1764 #include <sys/modctl.h>
1765 
1766 static struct modldrv modldrv = {
1767 	&mod_driverops,		/* Type of module. This one is a driver */
1768 	SD_MODULE_NAME,		/* Module name. */
1769 	&sd_ops			/* driver ops */
1770 };
1771 
1772 static struct modlinkage modlinkage = {
1773 	MODREV_1, &modldrv, NULL
1774 };
1775 
1776 static cmlb_tg_ops_t sd_tgops = {
1777 	TG_DK_OPS_VERSION_1,
1778 	sd_tg_rdwr,
1779 	sd_tg_getinfo
1780 };
1781 
1782 static struct scsi_asq_key_strings sd_additional_codes[] = {
1783 	0x81, 0, "Logical Unit is Reserved",
1784 	0x85, 0, "Audio Address Not Valid",
1785 	0xb6, 0, "Media Load Mechanism Failed",
1786 	0xB9, 0, "Audio Play Operation Aborted",
1787 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1788 	0x53, 2, "Medium removal prevented",
1789 	0x6f, 0, "Authentication failed during key exchange",
1790 	0x6f, 1, "Key not present",
1791 	0x6f, 2, "Key not established",
1792 	0x6f, 3, "Read without proper authentication",
1793 	0x6f, 4, "Mismatched region to this logical unit",
1794 	0x6f, 5, "Region reset count error",
1795 	0xffff, 0x0, NULL
1796 };
1797 
1798 
1799 /*
1800  * Struct for passing printing information for sense data messages
1801  */
1802 struct sd_sense_info {
1803 	int	ssi_severity;
1804 	int	ssi_pfa_flag;
1805 };
1806 
1807 /*
1808  * Table of function pointers for iostart-side routines. Separate "chains"
1809  * of layered function calls are formed by placing the function pointers
1810  * sequentially in the desired order. Functions are called according to an
1811  * incrementing table index ordering. The last function in each chain must
1812  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1813  * in the sd_iodone_chain[] array.
1814  *
1815  * Note: It may seem more natural to organize both the iostart and iodone
1816  * functions together, into an array of structures (or some similar
1817  * organization) with a common index, rather than two separate arrays which
1818  * must be maintained in synchronization. The purpose of this division is
1819  * to achieve improved performance: individual arrays allows for more
1820  * effective cache line utilization on certain platforms.
1821  */
1822 
1823 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1824 
1825 
1826 static sd_chain_t sd_iostart_chain[] = {
1827 
1828 	/* Chain for buf IO for disk drive targets (PM enabled) */
1829 	sd_mapblockaddr_iostart,	/* Index: 0 */
1830 	sd_pm_iostart,			/* Index: 1 */
1831 	sd_core_iostart,		/* Index: 2 */
1832 
1833 	/* Chain for buf IO for disk drive targets (PM disabled) */
1834 	sd_mapblockaddr_iostart,	/* Index: 3 */
1835 	sd_core_iostart,		/* Index: 4 */
1836 
1837 	/*
1838 	 * Chain for buf IO for removable-media or large sector size
1839 	 * disk drive targets with RMW needed (PM enabled)
1840 	 */
1841 	sd_mapblockaddr_iostart,	/* Index: 5 */
1842 	sd_mapblocksize_iostart,	/* Index: 6 */
1843 	sd_pm_iostart,			/* Index: 7 */
1844 	sd_core_iostart,		/* Index: 8 */
1845 
1846 	/*
1847 	 * Chain for buf IO for removable-media or large sector size
1848 	 * disk drive targets with RMW needed (PM disabled)
1849 	 */
1850 	sd_mapblockaddr_iostart,	/* Index: 9 */
1851 	sd_mapblocksize_iostart,	/* Index: 10 */
1852 	sd_core_iostart,		/* Index: 11 */
1853 
1854 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1855 	sd_mapblockaddr_iostart,	/* Index: 12 */
1856 	sd_checksum_iostart,		/* Index: 13 */
1857 	sd_pm_iostart,			/* Index: 14 */
1858 	sd_core_iostart,		/* Index: 15 */
1859 
1860 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1861 	sd_mapblockaddr_iostart,	/* Index: 16 */
1862 	sd_checksum_iostart,		/* Index: 17 */
1863 	sd_core_iostart,		/* Index: 18 */
1864 
1865 	/* Chain for USCSI commands (all targets) */
1866 	sd_pm_iostart,			/* Index: 19 */
1867 	sd_core_iostart,		/* Index: 20 */
1868 
1869 	/* Chain for checksumming USCSI commands (all targets) */
1870 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1871 	sd_pm_iostart,			/* Index: 22 */
1872 	sd_core_iostart,		/* Index: 23 */
1873 
1874 	/* Chain for "direct" USCSI commands (all targets) */
1875 	sd_core_iostart,		/* Index: 24 */
1876 
1877 	/* Chain for "direct priority" USCSI commands (all targets) */
1878 	sd_core_iostart,		/* Index: 25 */
1879 
1880 	/*
1881 	 * Chain for buf IO for large sector size disk drive targets
1882 	 * with RMW needed with checksumming (PM enabled)
1883 	 */
1884 	sd_mapblockaddr_iostart,	/* Index: 26 */
1885 	sd_mapblocksize_iostart,	/* Index: 27 */
1886 	sd_checksum_iostart,		/* Index: 28 */
1887 	sd_pm_iostart,			/* Index: 29 */
1888 	sd_core_iostart,		/* Index: 30 */
1889 
1890 	/*
1891 	 * Chain for buf IO for large sector size disk drive targets
1892 	 * with RMW needed with checksumming (PM disabled)
1893 	 */
1894 	sd_mapblockaddr_iostart,	/* Index: 31 */
1895 	sd_mapblocksize_iostart,	/* Index: 32 */
1896 	sd_checksum_iostart,		/* Index: 33 */
1897 	sd_core_iostart,		/* Index: 34 */
1898 
1899 };
1900 
1901 /*
1902  * Macros to locate the first function of each iostart chain in the
1903  * sd_iostart_chain[] array. These are located by the index in the array.
1904  */
1905 #define	SD_CHAIN_DISK_IOSTART			0
1906 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1907 #define	SD_CHAIN_MSS_DISK_IOSTART		5
1908 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1909 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1910 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1911 #define	SD_CHAIN_CHKSUM_IOSTART			12
1912 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1913 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1914 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1915 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1916 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1917 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1918 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1919 
1920 
1921 /*
1922  * Table of function pointers for the iodone-side routines for the driver-
1923  * internal layering mechanism.  The calling sequence for iodone routines
1924  * uses a decrementing table index, so the last routine called in a chain
1925  * must be at the lowest array index location for that chain.  The last
1926  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1927  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1928  * of the functions in an iodone side chain must correspond to the ordering
1929  * of the iostart routines for that chain.  Note that there is no iodone
1930  * side routine that corresponds to sd_core_iostart(), so there is no
1931  * entry in the table for this.
1932  */
1933 
1934 static sd_chain_t sd_iodone_chain[] = {
1935 
1936 	/* Chain for buf IO for disk drive targets (PM enabled) */
1937 	sd_buf_iodone,			/* Index: 0 */
1938 	sd_mapblockaddr_iodone,		/* Index: 1 */
1939 	sd_pm_iodone,			/* Index: 2 */
1940 
1941 	/* Chain for buf IO for disk drive targets (PM disabled) */
1942 	sd_buf_iodone,			/* Index: 3 */
1943 	sd_mapblockaddr_iodone,		/* Index: 4 */
1944 
1945 	/*
1946 	 * Chain for buf IO for removable-media or large sector size
1947 	 * disk drive targets with RMW needed (PM enabled)
1948 	 */
1949 	sd_buf_iodone,			/* Index: 5 */
1950 	sd_mapblockaddr_iodone,		/* Index: 6 */
1951 	sd_mapblocksize_iodone,		/* Index: 7 */
1952 	sd_pm_iodone,			/* Index: 8 */
1953 
1954 	/*
1955 	 * Chain for buf IO for removable-media or large sector size
1956 	 * disk drive targets with RMW needed (PM disabled)
1957 	 */
1958 	sd_buf_iodone,			/* Index: 9 */
1959 	sd_mapblockaddr_iodone,		/* Index: 10 */
1960 	sd_mapblocksize_iodone,		/* Index: 11 */
1961 
1962 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1963 	sd_buf_iodone,			/* Index: 12 */
1964 	sd_mapblockaddr_iodone,		/* Index: 13 */
1965 	sd_checksum_iodone,		/* Index: 14 */
1966 	sd_pm_iodone,			/* Index: 15 */
1967 
1968 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1969 	sd_buf_iodone,			/* Index: 16 */
1970 	sd_mapblockaddr_iodone,		/* Index: 17 */
1971 	sd_checksum_iodone,		/* Index: 18 */
1972 
1973 	/* Chain for USCSI commands (non-checksum targets) */
1974 	sd_uscsi_iodone,		/* Index: 19 */
1975 	sd_pm_iodone,			/* Index: 20 */
1976 
1977 	/* Chain for USCSI commands (checksum targets) */
1978 	sd_uscsi_iodone,		/* Index: 21 */
1979 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1980 	sd_pm_iodone,			/* Index: 22 */
1981 
1982 	/* Chain for "direct" USCSI commands (all targets) */
1983 	sd_uscsi_iodone,		/* Index: 24 */
1984 
1985 	/* Chain for "direct priority" USCSI commands (all targets) */
1986 	sd_uscsi_iodone,		/* Index: 25 */
1987 
1988 	/*
1989 	 * Chain for buf IO for large sector size disk drive targets
1990 	 * with checksumming (PM enabled)
1991 	 */
1992 	sd_buf_iodone,			/* Index: 26 */
1993 	sd_mapblockaddr_iodone,		/* Index: 27 */
1994 	sd_mapblocksize_iodone,		/* Index: 28 */
1995 	sd_checksum_iodone,		/* Index: 29 */
1996 	sd_pm_iodone,			/* Index: 30 */
1997 
1998 	/*
1999 	 * Chain for buf IO for large sector size disk drive targets
2000 	 * with checksumming (PM disabled)
2001 	 */
2002 	sd_buf_iodone,			/* Index: 31 */
2003 	sd_mapblockaddr_iodone,		/* Index: 32 */
2004 	sd_mapblocksize_iodone,		/* Index: 33 */
2005 	sd_checksum_iodone,		/* Index: 34 */
2006 };
2007 
2008 
2009 /*
2010  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2011  * each iodone-side chain. These are located by the array index, but as the
2012  * iodone side functions are called in a decrementing-index order, the
2013  * highest index number in each chain must be specified (as these correspond
2014  * to the first function in the iodone chain that will be called by the core
2015  * at IO completion time).
2016  */
2017 
2018 #define	SD_CHAIN_DISK_IODONE			2
2019 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
2020 #define	SD_CHAIN_RMMEDIA_IODONE			8
2021 #define	SD_CHAIN_MSS_DISK_IODONE		8
2022 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2023 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2024 #define	SD_CHAIN_CHKSUM_IODONE			15
2025 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2026 #define	SD_CHAIN_USCSI_CMD_IODONE		20
2027 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2028 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
2029 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2030 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2031 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2032 
2033 
2034 
2035 /*
2036  * Array to map a layering chain index to the appropriate initpkt routine.
2037  * The redundant entries are present so that the index used for accessing
2038  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2039  * with this table as well.
2040  */
2041 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2042 
2043 static sd_initpkt_t	sd_initpkt_map[] = {
2044 
2045 	/* Chain for buf IO for disk drive targets (PM enabled) */
2046 	sd_initpkt_for_buf,		/* Index: 0 */
2047 	sd_initpkt_for_buf,		/* Index: 1 */
2048 	sd_initpkt_for_buf,		/* Index: 2 */
2049 
2050 	/* Chain for buf IO for disk drive targets (PM disabled) */
2051 	sd_initpkt_for_buf,		/* Index: 3 */
2052 	sd_initpkt_for_buf,		/* Index: 4 */
2053 
2054 	/*
2055 	 * Chain for buf IO for removable-media or large sector size
2056 	 * disk drive targets (PM enabled)
2057 	 */
2058 	sd_initpkt_for_buf,		/* Index: 5 */
2059 	sd_initpkt_for_buf,		/* Index: 6 */
2060 	sd_initpkt_for_buf,		/* Index: 7 */
2061 	sd_initpkt_for_buf,		/* Index: 8 */
2062 
2063 	/*
2064 	 * Chain for buf IO for removable-media or large sector size
2065 	 * disk drive targets (PM disabled)
2066 	 */
2067 	sd_initpkt_for_buf,		/* Index: 9 */
2068 	sd_initpkt_for_buf,		/* Index: 10 */
2069 	sd_initpkt_for_buf,		/* Index: 11 */
2070 
2071 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2072 	sd_initpkt_for_buf,		/* Index: 12 */
2073 	sd_initpkt_for_buf,		/* Index: 13 */
2074 	sd_initpkt_for_buf,		/* Index: 14 */
2075 	sd_initpkt_for_buf,		/* Index: 15 */
2076 
2077 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2078 	sd_initpkt_for_buf,		/* Index: 16 */
2079 	sd_initpkt_for_buf,		/* Index: 17 */
2080 	sd_initpkt_for_buf,		/* Index: 18 */
2081 
2082 	/* Chain for USCSI commands (non-checksum targets) */
2083 	sd_initpkt_for_uscsi,		/* Index: 19 */
2084 	sd_initpkt_for_uscsi,		/* Index: 20 */
2085 
2086 	/* Chain for USCSI commands (checksum targets) */
2087 	sd_initpkt_for_uscsi,		/* Index: 21 */
2088 	sd_initpkt_for_uscsi,		/* Index: 22 */
2089 	sd_initpkt_for_uscsi,		/* Index: 22 */
2090 
2091 	/* Chain for "direct" USCSI commands (all targets) */
2092 	sd_initpkt_for_uscsi,		/* Index: 24 */
2093 
2094 	/* Chain for "direct priority" USCSI commands (all targets) */
2095 	sd_initpkt_for_uscsi,		/* Index: 25 */
2096 
2097 	/*
2098 	 * Chain for buf IO for large sector size disk drive targets
2099 	 * with checksumming (PM enabled)
2100 	 */
2101 	sd_initpkt_for_buf,		/* Index: 26 */
2102 	sd_initpkt_for_buf,		/* Index: 27 */
2103 	sd_initpkt_for_buf,		/* Index: 28 */
2104 	sd_initpkt_for_buf,		/* Index: 29 */
2105 	sd_initpkt_for_buf,		/* Index: 30 */
2106 
2107 	/*
2108 	 * Chain for buf IO for large sector size disk drive targets
2109 	 * with checksumming (PM disabled)
2110 	 */
2111 	sd_initpkt_for_buf,		/* Index: 31 */
2112 	sd_initpkt_for_buf,		/* Index: 32 */
2113 	sd_initpkt_for_buf,		/* Index: 33 */
2114 	sd_initpkt_for_buf,		/* Index: 34 */
2115 };
2116 
2117 
2118 /*
2119  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2120  * The redundant entries are present so that the index used for accessing
2121  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2122  * with this table as well.
2123  */
2124 typedef void (*sd_destroypkt_t)(struct buf *);
2125 
2126 static sd_destroypkt_t	sd_destroypkt_map[] = {
2127 
2128 	/* Chain for buf IO for disk drive targets (PM enabled) */
2129 	sd_destroypkt_for_buf,		/* Index: 0 */
2130 	sd_destroypkt_for_buf,		/* Index: 1 */
2131 	sd_destroypkt_for_buf,		/* Index: 2 */
2132 
2133 	/* Chain for buf IO for disk drive targets (PM disabled) */
2134 	sd_destroypkt_for_buf,		/* Index: 3 */
2135 	sd_destroypkt_for_buf,		/* Index: 4 */
2136 
2137 	/*
2138 	 * Chain for buf IO for removable-media or large sector size
2139 	 * disk drive targets (PM enabled)
2140 	 */
2141 	sd_destroypkt_for_buf,		/* Index: 5 */
2142 	sd_destroypkt_for_buf,		/* Index: 6 */
2143 	sd_destroypkt_for_buf,		/* Index: 7 */
2144 	sd_destroypkt_for_buf,		/* Index: 8 */
2145 
2146 	/*
2147 	 * Chain for buf IO for removable-media or large sector size
2148 	 * disk drive targets (PM disabled)
2149 	 */
2150 	sd_destroypkt_for_buf,		/* Index: 9 */
2151 	sd_destroypkt_for_buf,		/* Index: 10 */
2152 	sd_destroypkt_for_buf,		/* Index: 11 */
2153 
2154 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2155 	sd_destroypkt_for_buf,		/* Index: 12 */
2156 	sd_destroypkt_for_buf,		/* Index: 13 */
2157 	sd_destroypkt_for_buf,		/* Index: 14 */
2158 	sd_destroypkt_for_buf,		/* Index: 15 */
2159 
2160 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2161 	sd_destroypkt_for_buf,		/* Index: 16 */
2162 	sd_destroypkt_for_buf,		/* Index: 17 */
2163 	sd_destroypkt_for_buf,		/* Index: 18 */
2164 
2165 	/* Chain for USCSI commands (non-checksum targets) */
2166 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2167 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2168 
2169 	/* Chain for USCSI commands (checksum targets) */
2170 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2171 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2172 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2173 
2174 	/* Chain for "direct" USCSI commands (all targets) */
2175 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2176 
2177 	/* Chain for "direct priority" USCSI commands (all targets) */
2178 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2179 
2180 	/*
2181 	 * Chain for buf IO for large sector size disk drive targets
2182 	 * with checksumming (PM disabled)
2183 	 */
2184 	sd_destroypkt_for_buf,		/* Index: 26 */
2185 	sd_destroypkt_for_buf,		/* Index: 27 */
2186 	sd_destroypkt_for_buf,		/* Index: 28 */
2187 	sd_destroypkt_for_buf,		/* Index: 29 */
2188 	sd_destroypkt_for_buf,		/* Index: 30 */
2189 
2190 	/*
2191 	 * Chain for buf IO for large sector size disk drive targets
2192 	 * with checksumming (PM enabled)
2193 	 */
2194 	sd_destroypkt_for_buf,		/* Index: 31 */
2195 	sd_destroypkt_for_buf,		/* Index: 32 */
2196 	sd_destroypkt_for_buf,		/* Index: 33 */
2197 	sd_destroypkt_for_buf,		/* Index: 34 */
2198 };
2199 
2200 
2201 
2202 /*
2203  * Array to map a layering chain index to the appropriate chain "type".
2204  * The chain type indicates a specific property/usage of the chain.
2205  * The redundant entries are present so that the index used for accessing
2206  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2207  * with this table as well.
2208  */
2209 
2210 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2211 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2212 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2213 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2214 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2215 						/* (for error recovery) */
2216 
2217 static int sd_chain_type_map[] = {
2218 
2219 	/* Chain for buf IO for disk drive targets (PM enabled) */
2220 	SD_CHAIN_BUFIO,			/* Index: 0 */
2221 	SD_CHAIN_BUFIO,			/* Index: 1 */
2222 	SD_CHAIN_BUFIO,			/* Index: 2 */
2223 
2224 	/* Chain for buf IO for disk drive targets (PM disabled) */
2225 	SD_CHAIN_BUFIO,			/* Index: 3 */
2226 	SD_CHAIN_BUFIO,			/* Index: 4 */
2227 
2228 	/*
2229 	 * Chain for buf IO for removable-media or large sector size
2230 	 * disk drive targets (PM enabled)
2231 	 */
2232 	SD_CHAIN_BUFIO,			/* Index: 5 */
2233 	SD_CHAIN_BUFIO,			/* Index: 6 */
2234 	SD_CHAIN_BUFIO,			/* Index: 7 */
2235 	SD_CHAIN_BUFIO,			/* Index: 8 */
2236 
2237 	/*
2238 	 * Chain for buf IO for removable-media or large sector size
2239 	 * disk drive targets (PM disabled)
2240 	 */
2241 	SD_CHAIN_BUFIO,			/* Index: 9 */
2242 	SD_CHAIN_BUFIO,			/* Index: 10 */
2243 	SD_CHAIN_BUFIO,			/* Index: 11 */
2244 
2245 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2246 	SD_CHAIN_BUFIO,			/* Index: 12 */
2247 	SD_CHAIN_BUFIO,			/* Index: 13 */
2248 	SD_CHAIN_BUFIO,			/* Index: 14 */
2249 	SD_CHAIN_BUFIO,			/* Index: 15 */
2250 
2251 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2252 	SD_CHAIN_BUFIO,			/* Index: 16 */
2253 	SD_CHAIN_BUFIO,			/* Index: 17 */
2254 	SD_CHAIN_BUFIO,			/* Index: 18 */
2255 
2256 	/* Chain for USCSI commands (non-checksum targets) */
2257 	SD_CHAIN_USCSI,			/* Index: 19 */
2258 	SD_CHAIN_USCSI,			/* Index: 20 */
2259 
2260 	/* Chain for USCSI commands (checksum targets) */
2261 	SD_CHAIN_USCSI,			/* Index: 21 */
2262 	SD_CHAIN_USCSI,			/* Index: 22 */
2263 	SD_CHAIN_USCSI,			/* Index: 23 */
2264 
2265 	/* Chain for "direct" USCSI commands (all targets) */
2266 	SD_CHAIN_DIRECT,		/* Index: 24 */
2267 
2268 	/* Chain for "direct priority" USCSI commands (all targets) */
2269 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2270 
2271 	/*
2272 	 * Chain for buf IO for large sector size disk drive targets
2273 	 * with checksumming (PM enabled)
2274 	 */
2275 	SD_CHAIN_BUFIO,			/* Index: 26 */
2276 	SD_CHAIN_BUFIO,			/* Index: 27 */
2277 	SD_CHAIN_BUFIO,			/* Index: 28 */
2278 	SD_CHAIN_BUFIO,			/* Index: 29 */
2279 	SD_CHAIN_BUFIO,			/* Index: 30 */
2280 
2281 	/*
2282 	 * Chain for buf IO for large sector size disk drive targets
2283 	 * with checksumming (PM disabled)
2284 	 */
2285 	SD_CHAIN_BUFIO,			/* Index: 31 */
2286 	SD_CHAIN_BUFIO,			/* Index: 32 */
2287 	SD_CHAIN_BUFIO,			/* Index: 33 */
2288 	SD_CHAIN_BUFIO,			/* Index: 34 */
2289 };
2290 
2291 
2292 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2293 #define	SD_IS_BUFIO(xp)			\
2294 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2295 
2296 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2297 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2298 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2299 
2300 
2301 
2302 /*
2303  * Struct, array, and macros to map a specific chain to the appropriate
2304  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2305  *
2306  * The sd_chain_index_map[] array is used at attach time to set the various
2307  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2308  * chain to be used with the instance. This allows different instances to use
2309  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2310  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2311  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2312  * dynamically & without the use of locking; and (2) a layer may update the
2313  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2314  * to allow for deferred processing of an IO within the same chain from a
2315  * different execution context.
2316  */
2317 
2318 struct sd_chain_index {
2319 	int	sci_iostart_index;
2320 	int	sci_iodone_index;
2321 };
2322 
2323 static struct sd_chain_index	sd_chain_index_map[] = {
2324 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2325 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2326 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2327 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2328 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2329 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2330 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2331 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2332 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2333 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2334 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2335 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2336 
2337 };
2338 
2339 
2340 /*
2341  * The following are indexes into the sd_chain_index_map[] array.
2342  */
2343 
2344 /* un->un_buf_chain_type must be set to one of these */
2345 #define	SD_CHAIN_INFO_DISK		0
2346 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2347 #define	SD_CHAIN_INFO_RMMEDIA		2
2348 #define	SD_CHAIN_INFO_MSS_DISK		2
2349 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2350 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2351 #define	SD_CHAIN_INFO_CHKSUM		4
2352 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2353 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2354 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2355 
2356 /* un->un_uscsi_chain_type must be set to one of these */
2357 #define	SD_CHAIN_INFO_USCSI_CMD		6
2358 /* USCSI with PM disabled is the same as DIRECT */
2359 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2360 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2361 
2362 /* un->un_direct_chain_type must be set to one of these */
2363 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2364 
2365 /* un->un_priority_chain_type must be set to one of these */
2366 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2367 
2368 /* size for devid inquiries */
2369 #define	MAX_INQUIRY_SIZE		0xF0
2370 
2371 /*
2372  * Macros used by functions to pass a given buf(9S) struct along to the
2373  * next function in the layering chain for further processing.
2374  *
2375  * In the following macros, passing more than three arguments to the called
2376  * routines causes the optimizer for the SPARC compiler to stop doing tail
2377  * call elimination which results in significant performance degradation.
2378  */
2379 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2380 	((*(sd_iostart_chain[index]))(index, un, bp))
2381 
2382 #define	SD_BEGIN_IODONE(index, un, bp)	\
2383 	((*(sd_iodone_chain[index]))(index, un, bp))
2384 
2385 #define	SD_NEXT_IOSTART(index, un, bp)				\
2386 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2387 
2388 #define	SD_NEXT_IODONE(index, un, bp)				\
2389 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2390 
2391 /*
2392  *    Function: _init
2393  *
2394  * Description: This is the driver _init(9E) entry point.
2395  *
2396  * Return Code: Returns the value from mod_install(9F) or
2397  *		ddi_soft_state_init(9F) as appropriate.
2398  *
2399  *     Context: Called when driver module loaded.
2400  */
2401 
2402 int
2403 _init(void)
2404 {
2405 	int	err;
2406 
2407 	/* establish driver name from module name */
2408 	sd_label = (char *)mod_modname(&modlinkage);
2409 
2410 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2411 	    SD_MAXUNIT);
2412 	if (err != 0) {
2413 		return (err);
2414 	}
2415 
2416 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2417 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2418 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2419 
2420 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2421 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2422 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2423 
2424 	/*
2425 	 * it's ok to init here even for fibre device
2426 	 */
2427 	sd_scsi_probe_cache_init();
2428 
2429 	sd_scsi_target_lun_init();
2430 
2431 	/*
2432 	 * Creating taskq before mod_install ensures that all callers (threads)
2433 	 * that enter the module after a successful mod_install encounter
2434 	 * a valid taskq.
2435 	 */
2436 	sd_taskq_create();
2437 
2438 	err = mod_install(&modlinkage);
2439 	if (err != 0) {
2440 		/* delete taskq if install fails */
2441 		sd_taskq_delete();
2442 
2443 		mutex_destroy(&sd_detach_mutex);
2444 		mutex_destroy(&sd_log_mutex);
2445 		mutex_destroy(&sd_label_mutex);
2446 
2447 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2448 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2449 		cv_destroy(&sd_tr.srq_inprocess_cv);
2450 
2451 		sd_scsi_probe_cache_fini();
2452 
2453 		sd_scsi_target_lun_fini();
2454 
2455 		ddi_soft_state_fini(&sd_state);
2456 
2457 		return (err);
2458 	}
2459 
2460 	return (err);
2461 }
2462 
2463 
2464 /*
2465  *    Function: _fini
2466  *
2467  * Description: This is the driver _fini(9E) entry point.
2468  *
2469  * Return Code: Returns the value from mod_remove(9F)
2470  *
2471  *     Context: Called when driver module is unloaded.
2472  */
2473 
2474 int
2475 _fini(void)
2476 {
2477 	int err;
2478 
2479 	if ((err = mod_remove(&modlinkage)) != 0) {
2480 		return (err);
2481 	}
2482 
2483 	sd_taskq_delete();
2484 
2485 	mutex_destroy(&sd_detach_mutex);
2486 	mutex_destroy(&sd_log_mutex);
2487 	mutex_destroy(&sd_label_mutex);
2488 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2489 
2490 	sd_scsi_probe_cache_fini();
2491 
2492 	sd_scsi_target_lun_fini();
2493 
2494 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2495 	cv_destroy(&sd_tr.srq_inprocess_cv);
2496 
2497 	ddi_soft_state_fini(&sd_state);
2498 
2499 	return (err);
2500 }
2501 
2502 
2503 /*
2504  *    Function: _info
2505  *
2506  * Description: This is the driver _info(9E) entry point.
2507  *
2508  *   Arguments: modinfop - pointer to the driver modinfo structure
2509  *
2510  * Return Code: Returns the value from mod_info(9F).
2511  *
2512  *     Context: Kernel thread context
2513  */
2514 
2515 int
2516 _info(struct modinfo *modinfop)
2517 {
2518 	return (mod_info(&modlinkage, modinfop));
2519 }
2520 
2521 
2522 /*
2523  * The following routines implement the driver message logging facility.
2524  * They provide component- and level- based debug output filtering.
2525  * Output may also be restricted to messages for a single instance by
2526  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2527  * to NULL, then messages for all instances are printed.
2528  *
2529  * These routines have been cloned from each other due to the language
2530  * constraints of macros and variable argument list processing.
2531  */
2532 
2533 
2534 /*
2535  *    Function: sd_log_err
2536  *
2537  * Description: This routine is called by the SD_ERROR macro for debug
2538  *		logging of error conditions.
2539  *
2540  *   Arguments: comp - driver component being logged
2541  *		dev  - pointer to driver info structure
2542  *		fmt  - error string and format to be logged
2543  */
2544 
2545 static void
2546 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2547 {
2548 	va_list		ap;
2549 	dev_info_t	*dev;
2550 
2551 	ASSERT(un != NULL);
2552 	dev = SD_DEVINFO(un);
2553 	ASSERT(dev != NULL);
2554 
2555 	/*
2556 	 * Filter messages based on the global component and level masks.
2557 	 * Also print if un matches the value of sd_debug_un, or if
2558 	 * sd_debug_un is set to NULL.
2559 	 */
2560 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2561 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2562 		mutex_enter(&sd_log_mutex);
2563 		va_start(ap, fmt);
2564 		(void) vsprintf(sd_log_buf, fmt, ap);
2565 		va_end(ap);
2566 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2567 		mutex_exit(&sd_log_mutex);
2568 	}
2569 #ifdef SD_FAULT_INJECTION
2570 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2571 	if (un->sd_injection_mask & comp) {
2572 		mutex_enter(&sd_log_mutex);
2573 		va_start(ap, fmt);
2574 		(void) vsprintf(sd_log_buf, fmt, ap);
2575 		va_end(ap);
2576 		sd_injection_log(sd_log_buf, un);
2577 		mutex_exit(&sd_log_mutex);
2578 	}
2579 #endif
2580 }
2581 
2582 
2583 /*
2584  *    Function: sd_log_info
2585  *
2586  * Description: This routine is called by the SD_INFO macro for debug
2587  *		logging of general purpose informational conditions.
2588  *
2589  *   Arguments: comp - driver component being logged
2590  *		dev  - pointer to driver info structure
2591  *		fmt  - info string and format to be logged
2592  */
2593 
2594 static void
2595 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2596 {
2597 	va_list		ap;
2598 	dev_info_t	*dev;
2599 
2600 	ASSERT(un != NULL);
2601 	dev = SD_DEVINFO(un);
2602 	ASSERT(dev != NULL);
2603 
2604 	/*
2605 	 * Filter messages based on the global component and level masks.
2606 	 * Also print if un matches the value of sd_debug_un, or if
2607 	 * sd_debug_un is set to NULL.
2608 	 */
2609 	if ((sd_component_mask & component) &&
2610 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2611 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2612 		mutex_enter(&sd_log_mutex);
2613 		va_start(ap, fmt);
2614 		(void) vsprintf(sd_log_buf, fmt, ap);
2615 		va_end(ap);
2616 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2617 		mutex_exit(&sd_log_mutex);
2618 	}
2619 #ifdef SD_FAULT_INJECTION
2620 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2621 	if (un->sd_injection_mask & component) {
2622 		mutex_enter(&sd_log_mutex);
2623 		va_start(ap, fmt);
2624 		(void) vsprintf(sd_log_buf, fmt, ap);
2625 		va_end(ap);
2626 		sd_injection_log(sd_log_buf, un);
2627 		mutex_exit(&sd_log_mutex);
2628 	}
2629 #endif
2630 }
2631 
2632 
2633 /*
2634  *    Function: sd_log_trace
2635  *
2636  * Description: This routine is called by the SD_TRACE macro for debug
2637  *		logging of trace conditions (i.e. function entry/exit).
2638  *
2639  *   Arguments: comp - driver component being logged
2640  *		dev  - pointer to driver info structure
2641  *		fmt  - trace string and format to be logged
2642  */
2643 
2644 static void
2645 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2646 {
2647 	va_list		ap;
2648 	dev_info_t	*dev;
2649 
2650 	ASSERT(un != NULL);
2651 	dev = SD_DEVINFO(un);
2652 	ASSERT(dev != NULL);
2653 
2654 	/*
2655 	 * Filter messages based on the global component and level masks.
2656 	 * Also print if un matches the value of sd_debug_un, or if
2657 	 * sd_debug_un is set to NULL.
2658 	 */
2659 	if ((sd_component_mask & component) &&
2660 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2661 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2662 		mutex_enter(&sd_log_mutex);
2663 		va_start(ap, fmt);
2664 		(void) vsprintf(sd_log_buf, fmt, ap);
2665 		va_end(ap);
2666 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2667 		mutex_exit(&sd_log_mutex);
2668 	}
2669 #ifdef SD_FAULT_INJECTION
2670 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2671 	if (un->sd_injection_mask & component) {
2672 		mutex_enter(&sd_log_mutex);
2673 		va_start(ap, fmt);
2674 		(void) vsprintf(sd_log_buf, fmt, ap);
2675 		va_end(ap);
2676 		sd_injection_log(sd_log_buf, un);
2677 		mutex_exit(&sd_log_mutex);
2678 	}
2679 #endif
2680 }
2681 
2682 
2683 /*
2684  *    Function: sdprobe
2685  *
2686  * Description: This is the driver probe(9e) entry point function.
2687  *
2688  *   Arguments: devi - opaque device info handle
2689  *
2690  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2691  *              DDI_PROBE_FAILURE: If the probe failed.
2692  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2693  *				   but may be present in the future.
2694  */
2695 
2696 static int
2697 sdprobe(dev_info_t *devi)
2698 {
2699 	struct scsi_device	*devp;
2700 	int			rval;
2701 	int			instance = ddi_get_instance(devi);
2702 
2703 	/*
2704 	 * if it wasn't for pln, sdprobe could actually be nulldev
2705 	 * in the "__fibre" case.
2706 	 */
2707 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2708 		return (DDI_PROBE_DONTCARE);
2709 	}
2710 
2711 	devp = ddi_get_driver_private(devi);
2712 
2713 	if (devp == NULL) {
2714 		/* Ooops... nexus driver is mis-configured... */
2715 		return (DDI_PROBE_FAILURE);
2716 	}
2717 
2718 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2719 		return (DDI_PROBE_PARTIAL);
2720 	}
2721 
2722 	/*
2723 	 * Call the SCSA utility probe routine to see if we actually
2724 	 * have a target at this SCSI nexus.
2725 	 */
2726 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2727 	case SCSIPROBE_EXISTS:
2728 		switch (devp->sd_inq->inq_dtype) {
2729 		case DTYPE_DIRECT:
2730 			rval = DDI_PROBE_SUCCESS;
2731 			break;
2732 		case DTYPE_RODIRECT:
2733 			/* CDs etc. Can be removable media */
2734 			rval = DDI_PROBE_SUCCESS;
2735 			break;
2736 		case DTYPE_OPTICAL:
2737 			/*
2738 			 * Rewritable optical driver HP115AA
2739 			 * Can also be removable media
2740 			 */
2741 
2742 			/*
2743 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2744 			 * pre solaris 9 sparc sd behavior is required
2745 			 *
2746 			 * If first time through and sd_dtype_optical_bind
2747 			 * has not been set in /etc/system check properties
2748 			 */
2749 
2750 			if (sd_dtype_optical_bind  < 0) {
2751 				sd_dtype_optical_bind = ddi_prop_get_int
2752 				    (DDI_DEV_T_ANY, devi, 0,
2753 				    "optical-device-bind", 1);
2754 			}
2755 
2756 			if (sd_dtype_optical_bind == 0) {
2757 				rval = DDI_PROBE_FAILURE;
2758 			} else {
2759 				rval = DDI_PROBE_SUCCESS;
2760 			}
2761 			break;
2762 
2763 		case DTYPE_NOTPRESENT:
2764 		default:
2765 			rval = DDI_PROBE_FAILURE;
2766 			break;
2767 		}
2768 		break;
2769 	default:
2770 		rval = DDI_PROBE_PARTIAL;
2771 		break;
2772 	}
2773 
2774 	/*
2775 	 * This routine checks for resource allocation prior to freeing,
2776 	 * so it will take care of the "smart probing" case where a
2777 	 * scsi_probe() may or may not have been issued and will *not*
2778 	 * free previously-freed resources.
2779 	 */
2780 	scsi_unprobe(devp);
2781 	return (rval);
2782 }
2783 
2784 
2785 /*
2786  *    Function: sdinfo
2787  *
2788  * Description: This is the driver getinfo(9e) entry point function.
2789  *		Given the device number, return the devinfo pointer from
2790  *		the scsi_device structure or the instance number
2791  *		associated with the dev_t.
2792  *
2793  *   Arguments: dip     - pointer to device info structure
2794  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2795  *			  DDI_INFO_DEVT2INSTANCE)
2796  *		arg     - driver dev_t
2797  *		resultp - user buffer for request response
2798  *
2799  * Return Code: DDI_SUCCESS
2800  *              DDI_FAILURE
2801  */
2802 /* ARGSUSED */
2803 static int
2804 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2805 {
2806 	struct sd_lun	*un;
2807 	dev_t		dev;
2808 	int		instance;
2809 	int		error;
2810 
2811 	switch (infocmd) {
2812 	case DDI_INFO_DEVT2DEVINFO:
2813 		dev = (dev_t)arg;
2814 		instance = SDUNIT(dev);
2815 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2816 			return (DDI_FAILURE);
2817 		}
2818 		*result = (void *) SD_DEVINFO(un);
2819 		error = DDI_SUCCESS;
2820 		break;
2821 	case DDI_INFO_DEVT2INSTANCE:
2822 		dev = (dev_t)arg;
2823 		instance = SDUNIT(dev);
2824 		*result = (void *)(uintptr_t)instance;
2825 		error = DDI_SUCCESS;
2826 		break;
2827 	default:
2828 		error = DDI_FAILURE;
2829 	}
2830 	return (error);
2831 }
2832 
2833 /*
2834  *    Function: sd_prop_op
2835  *
2836  * Description: This is the driver prop_op(9e) entry point function.
2837  *		Return the number of blocks for the partition in question
2838  *		or forward the request to the property facilities.
2839  *
2840  *   Arguments: dev       - device number
2841  *		dip       - pointer to device info structure
2842  *		prop_op   - property operator
2843  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2844  *		name      - pointer to property name
2845  *		valuep    - pointer or address of the user buffer
2846  *		lengthp   - property length
2847  *
2848  * Return Code: DDI_PROP_SUCCESS
2849  *              DDI_PROP_NOT_FOUND
2850  *              DDI_PROP_UNDEFINED
2851  *              DDI_PROP_NO_MEMORY
2852  *              DDI_PROP_BUF_TOO_SMALL
2853  */
2854 
2855 static int
2856 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2857     char *name, caddr_t valuep, int *lengthp)
2858 {
2859 	struct sd_lun	*un;
2860 
2861 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2862 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2863 		    name, valuep, lengthp));
2864 
2865 	return (cmlb_prop_op(un->un_cmlbhandle,
2866 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2867 	    SDPART(dev), (void *)SD_PATH_DIRECT));
2868 }
2869 
2870 /*
2871  * The following functions are for smart probing:
2872  * sd_scsi_probe_cache_init()
2873  * sd_scsi_probe_cache_fini()
2874  * sd_scsi_clear_probe_cache()
2875  * sd_scsi_probe_with_cache()
2876  */
2877 
2878 /*
2879  *    Function: sd_scsi_probe_cache_init
2880  *
2881  * Description: Initializes the probe response cache mutex and head pointer.
2882  *
2883  *     Context: Kernel thread context
2884  */
2885 
2886 static void
2887 sd_scsi_probe_cache_init(void)
2888 {
2889 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2890 	sd_scsi_probe_cache_head = NULL;
2891 }
2892 
2893 
2894 /*
2895  *    Function: sd_scsi_probe_cache_fini
2896  *
2897  * Description: Frees all resources associated with the probe response cache.
2898  *
2899  *     Context: Kernel thread context
2900  */
2901 
2902 static void
2903 sd_scsi_probe_cache_fini(void)
2904 {
2905 	struct sd_scsi_probe_cache *cp;
2906 	struct sd_scsi_probe_cache *ncp;
2907 
2908 	/* Clean up our smart probing linked list */
2909 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2910 		ncp = cp->next;
2911 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2912 	}
2913 	sd_scsi_probe_cache_head = NULL;
2914 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2915 }
2916 
2917 
2918 /*
2919  *    Function: sd_scsi_clear_probe_cache
2920  *
2921  * Description: This routine clears the probe response cache. This is
2922  *		done when open() returns ENXIO so that when deferred
2923  *		attach is attempted (possibly after a device has been
2924  *		turned on) we will retry the probe. Since we don't know
2925  *		which target we failed to open, we just clear the
2926  *		entire cache.
2927  *
2928  *     Context: Kernel thread context
2929  */
2930 
2931 static void
2932 sd_scsi_clear_probe_cache(void)
2933 {
2934 	struct sd_scsi_probe_cache	*cp;
2935 	int				i;
2936 
2937 	mutex_enter(&sd_scsi_probe_cache_mutex);
2938 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2939 		/*
2940 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2941 		 * force probing to be performed the next time
2942 		 * sd_scsi_probe_with_cache is called.
2943 		 */
2944 		for (i = 0; i < NTARGETS_WIDE; i++) {
2945 			cp->cache[i] = SCSIPROBE_EXISTS;
2946 		}
2947 	}
2948 	mutex_exit(&sd_scsi_probe_cache_mutex);
2949 }
2950 
2951 
2952 /*
2953  *    Function: sd_scsi_probe_with_cache
2954  *
2955  * Description: This routine implements support for a scsi device probe
2956  *		with cache. The driver maintains a cache of the target
2957  *		responses to scsi probes. If we get no response from a
2958  *		target during a probe inquiry, we remember that, and we
2959  *		avoid additional calls to scsi_probe on non-zero LUNs
2960  *		on the same target until the cache is cleared. By doing
2961  *		so we avoid the 1/4 sec selection timeout for nonzero
2962  *		LUNs. lun0 of a target is always probed.
2963  *
2964  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2965  *              waitfunc - indicates what the allocator routines should
2966  *			   do when resources are not available. This value
2967  *			   is passed on to scsi_probe() when that routine
2968  *			   is called.
2969  *
2970  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2971  *		otherwise the value returned by scsi_probe(9F).
2972  *
2973  *     Context: Kernel thread context
2974  */
2975 
2976 static int
2977 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2978 {
2979 	struct sd_scsi_probe_cache	*cp;
2980 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2981 	int		lun, tgt;
2982 
2983 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2984 	    SCSI_ADDR_PROP_LUN, 0);
2985 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2986 	    SCSI_ADDR_PROP_TARGET, -1);
2987 
2988 	/* Make sure caching enabled and target in range */
2989 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2990 		/* do it the old way (no cache) */
2991 		return (scsi_probe(devp, waitfn));
2992 	}
2993 
2994 	mutex_enter(&sd_scsi_probe_cache_mutex);
2995 
2996 	/* Find the cache for this scsi bus instance */
2997 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2998 		if (cp->pdip == pdip) {
2999 			break;
3000 		}
3001 	}
3002 
3003 	/* If we can't find a cache for this pdip, create one */
3004 	if (cp == NULL) {
3005 		int i;
3006 
3007 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3008 		    KM_SLEEP);
3009 		cp->pdip = pdip;
3010 		cp->next = sd_scsi_probe_cache_head;
3011 		sd_scsi_probe_cache_head = cp;
3012 		for (i = 0; i < NTARGETS_WIDE; i++) {
3013 			cp->cache[i] = SCSIPROBE_EXISTS;
3014 		}
3015 	}
3016 
3017 	mutex_exit(&sd_scsi_probe_cache_mutex);
3018 
3019 	/* Recompute the cache for this target if LUN zero */
3020 	if (lun == 0) {
3021 		cp->cache[tgt] = SCSIPROBE_EXISTS;
3022 	}
3023 
3024 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3025 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3026 		return (SCSIPROBE_NORESP);
3027 	}
3028 
3029 	/* Do the actual probe; save & return the result */
3030 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3031 }
3032 
3033 
3034 /*
3035  *    Function: sd_scsi_target_lun_init
3036  *
3037  * Description: Initializes the attached lun chain mutex and head pointer.
3038  *
3039  *     Context: Kernel thread context
3040  */
3041 
3042 static void
3043 sd_scsi_target_lun_init(void)
3044 {
3045 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3046 	sd_scsi_target_lun_head = NULL;
3047 }
3048 
3049 
3050 /*
3051  *    Function: sd_scsi_target_lun_fini
3052  *
3053  * Description: Frees all resources associated with the attached lun
3054  *              chain
3055  *
3056  *     Context: Kernel thread context
3057  */
3058 
3059 static void
3060 sd_scsi_target_lun_fini(void)
3061 {
3062 	struct sd_scsi_hba_tgt_lun	*cp;
3063 	struct sd_scsi_hba_tgt_lun	*ncp;
3064 
3065 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3066 		ncp = cp->next;
3067 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3068 	}
3069 	sd_scsi_target_lun_head = NULL;
3070 	mutex_destroy(&sd_scsi_target_lun_mutex);
3071 }
3072 
3073 
3074 /*
3075  *    Function: sd_scsi_get_target_lun_count
3076  *
3077  * Description: This routine will check in the attached lun chain to see
3078  *		how many luns are attached on the required SCSI controller
3079  *		and target. Currently, some capabilities like tagged queue
3080  *		are supported per target based by HBA. So all luns in a
3081  *		target have the same capabilities. Based on this assumption,
3082  *		sd should only set these capabilities once per target. This
3083  *		function is called when sd needs to decide how many luns
3084  *		already attached on a target.
3085  *
3086  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3087  *			  controller device.
3088  *              target	- The target ID on the controller's SCSI bus.
3089  *
3090  * Return Code: The number of luns attached on the required target and
3091  *		controller.
3092  *		-1 if target ID is not in parallel SCSI scope or the given
3093  *		dip is not in the chain.
3094  *
3095  *     Context: Kernel thread context
3096  */
3097 
3098 static int
3099 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3100 {
3101 	struct sd_scsi_hba_tgt_lun	*cp;
3102 
3103 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3104 		return (-1);
3105 	}
3106 
3107 	mutex_enter(&sd_scsi_target_lun_mutex);
3108 
3109 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3110 		if (cp->pdip == dip) {
3111 			break;
3112 		}
3113 	}
3114 
3115 	mutex_exit(&sd_scsi_target_lun_mutex);
3116 
3117 	if (cp == NULL) {
3118 		return (-1);
3119 	}
3120 
3121 	return (cp->nlun[target]);
3122 }
3123 
3124 
3125 /*
3126  *    Function: sd_scsi_update_lun_on_target
3127  *
3128  * Description: This routine is used to update the attached lun chain when a
3129  *		lun is attached or detached on a target.
3130  *
3131  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3132  *                        controller device.
3133  *              target  - The target ID on the controller's SCSI bus.
3134  *		flag	- Indicate the lun is attached or detached.
3135  *
3136  *     Context: Kernel thread context
3137  */
3138 
3139 static void
3140 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3141 {
3142 	struct sd_scsi_hba_tgt_lun	*cp;
3143 
3144 	mutex_enter(&sd_scsi_target_lun_mutex);
3145 
3146 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3147 		if (cp->pdip == dip) {
3148 			break;
3149 		}
3150 	}
3151 
3152 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3153 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3154 		    KM_SLEEP);
3155 		cp->pdip = dip;
3156 		cp->next = sd_scsi_target_lun_head;
3157 		sd_scsi_target_lun_head = cp;
3158 	}
3159 
3160 	mutex_exit(&sd_scsi_target_lun_mutex);
3161 
3162 	if (cp != NULL) {
3163 		if (flag == SD_SCSI_LUN_ATTACH) {
3164 			cp->nlun[target] ++;
3165 		} else {
3166 			cp->nlun[target] --;
3167 		}
3168 	}
3169 }
3170 
3171 
3172 /*
3173  *    Function: sd_spin_up_unit
3174  *
3175  * Description: Issues the following commands to spin-up the device:
3176  *		START STOP UNIT, and INQUIRY.
3177  *
3178  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3179  *                      structure for this target.
3180  *
3181  * Return Code: 0 - success
3182  *		EIO - failure
3183  *		EACCES - reservation conflict
3184  *
3185  *     Context: Kernel thread context
3186  */
3187 
3188 static int
3189 sd_spin_up_unit(sd_ssc_t *ssc)
3190 {
3191 	size_t	resid		= 0;
3192 	int	has_conflict	= FALSE;
3193 	uchar_t *bufaddr;
3194 	int	status;
3195 	struct sd_lun	*un;
3196 
3197 	ASSERT(ssc != NULL);
3198 	un = ssc->ssc_un;
3199 	ASSERT(un != NULL);
3200 
3201 	/*
3202 	 * Send a throwaway START UNIT command.
3203 	 *
3204 	 * If we fail on this, we don't care presently what precisely
3205 	 * is wrong.  EMC's arrays will also fail this with a check
3206 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3207 	 * we don't want to fail the attach because it may become
3208 	 * "active" later.
3209 	 * We don't know if power condition is supported or not at
3210 	 * this stage, use START STOP bit.
3211 	 */
3212 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3213 	    SD_TARGET_START, SD_PATH_DIRECT);
3214 
3215 	if (status != 0) {
3216 		if (status == EACCES)
3217 			has_conflict = TRUE;
3218 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3219 	}
3220 
3221 	/*
3222 	 * Send another INQUIRY command to the target. This is necessary for
3223 	 * non-removable media direct access devices because their INQUIRY data
3224 	 * may not be fully qualified until they are spun up (perhaps via the
3225 	 * START command above).  Note: This seems to be needed for some
3226 	 * legacy devices only.) The INQUIRY command should succeed even if a
3227 	 * Reservation Conflict is present.
3228 	 */
3229 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3230 
3231 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3232 	    != 0) {
3233 		kmem_free(bufaddr, SUN_INQSIZE);
3234 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3235 		return (EIO);
3236 	}
3237 
3238 	/*
3239 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3240 	 * Note that this routine does not return a failure here even if the
3241 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3242 	 */
3243 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3244 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3245 	}
3246 
3247 	kmem_free(bufaddr, SUN_INQSIZE);
3248 
3249 	/* If we hit a reservation conflict above, tell the caller. */
3250 	if (has_conflict == TRUE) {
3251 		return (EACCES);
3252 	}
3253 
3254 	return (0);
3255 }
3256 
3257 #ifdef _LP64
3258 /*
3259  *    Function: sd_enable_descr_sense
3260  *
3261  * Description: This routine attempts to select descriptor sense format
3262  *		using the Control mode page.  Devices that support 64 bit
3263  *		LBAs (for >2TB luns) should also implement descriptor
3264  *		sense data so we will call this function whenever we see
3265  *		a lun larger than 2TB.  If for some reason the device
3266  *		supports 64 bit LBAs but doesn't support descriptor sense
3267  *		presumably the mode select will fail.  Everything will
3268  *		continue to work normally except that we will not get
3269  *		complete sense data for commands that fail with an LBA
3270  *		larger than 32 bits.
3271  *
3272  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3273  *                      structure for this target.
3274  *
3275  *     Context: Kernel thread context only
3276  */
3277 
3278 static void
3279 sd_enable_descr_sense(sd_ssc_t *ssc)
3280 {
3281 	uchar_t			*header;
3282 	struct mode_control_scsi3 *ctrl_bufp;
3283 	size_t			buflen;
3284 	size_t			bd_len;
3285 	int			status;
3286 	struct sd_lun		*un;
3287 
3288 	ASSERT(ssc != NULL);
3289 	un = ssc->ssc_un;
3290 	ASSERT(un != NULL);
3291 
3292 	/*
3293 	 * Read MODE SENSE page 0xA, Control Mode Page
3294 	 */
3295 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3296 	    sizeof (struct mode_control_scsi3);
3297 	header = kmem_zalloc(buflen, KM_SLEEP);
3298 
3299 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3300 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3301 
3302 	if (status != 0) {
3303 		SD_ERROR(SD_LOG_COMMON, un,
3304 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3305 		goto eds_exit;
3306 	}
3307 
3308 	/*
3309 	 * Determine size of Block Descriptors in order to locate
3310 	 * the mode page data. ATAPI devices return 0, SCSI devices
3311 	 * should return MODE_BLK_DESC_LENGTH.
3312 	 */
3313 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3314 
3315 	/* Clear the mode data length field for MODE SELECT */
3316 	((struct mode_header *)header)->length = 0;
3317 
3318 	ctrl_bufp = (struct mode_control_scsi3 *)
3319 	    (header + MODE_HEADER_LENGTH + bd_len);
3320 
3321 	/*
3322 	 * If the page length is smaller than the expected value,
3323 	 * the target device doesn't support D_SENSE. Bail out here.
3324 	 */
3325 	if (ctrl_bufp->mode_page.length <
3326 	    sizeof (struct mode_control_scsi3) - 2) {
3327 		SD_ERROR(SD_LOG_COMMON, un,
3328 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3329 		goto eds_exit;
3330 	}
3331 
3332 	/*
3333 	 * Clear PS bit for MODE SELECT
3334 	 */
3335 	ctrl_bufp->mode_page.ps = 0;
3336 
3337 	/*
3338 	 * Set D_SENSE to enable descriptor sense format.
3339 	 */
3340 	ctrl_bufp->d_sense = 1;
3341 
3342 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3343 
3344 	/*
3345 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3346 	 */
3347 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3348 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3349 
3350 	if (status != 0) {
3351 		SD_INFO(SD_LOG_COMMON, un,
3352 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3353 	} else {
3354 		kmem_free(header, buflen);
3355 		return;
3356 	}
3357 
3358 eds_exit:
3359 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3360 	kmem_free(header, buflen);
3361 }
3362 
3363 /*
3364  *    Function: sd_reenable_dsense_task
3365  *
3366  * Description: Re-enable descriptor sense after device or bus reset
3367  *
3368  *     Context: Executes in a taskq() thread context
3369  */
3370 static void
3371 sd_reenable_dsense_task(void *arg)
3372 {
3373 	struct	sd_lun	*un = arg;
3374 	sd_ssc_t	*ssc;
3375 
3376 	ASSERT(un != NULL);
3377 
3378 	ssc = sd_ssc_init(un);
3379 	sd_enable_descr_sense(ssc);
3380 	sd_ssc_fini(ssc);
3381 }
3382 #endif /* _LP64 */
3383 
3384 /*
3385  *    Function: sd_set_mmc_caps
3386  *
3387  * Description: This routine determines if the device is MMC compliant and if
3388  *		the device supports CDDA via a mode sense of the CDVD
3389  *		capabilities mode page. Also checks if the device is a
3390  *		dvdram writable device.
3391  *
3392  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3393  *                      structure for this target.
3394  *
3395  *     Context: Kernel thread context only
3396  */
3397 
3398 static void
3399 sd_set_mmc_caps(sd_ssc_t *ssc)
3400 {
3401 	struct mode_header_grp2		*sense_mhp;
3402 	uchar_t				*sense_page;
3403 	caddr_t				buf;
3404 	int				bd_len;
3405 	int				status;
3406 	struct uscsi_cmd		com;
3407 	int				rtn;
3408 	uchar_t				*out_data_rw, *out_data_hd;
3409 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3410 	uchar_t				*out_data_gesn;
3411 	int				gesn_len;
3412 	struct sd_lun			*un;
3413 
3414 	ASSERT(ssc != NULL);
3415 	un = ssc->ssc_un;
3416 	ASSERT(un != NULL);
3417 
3418 	/*
3419 	 * The flags which will be set in this function are - mmc compliant,
3420 	 * dvdram writable device, cdda support. Initialize them to FALSE
3421 	 * and if a capability is detected - it will be set to TRUE.
3422 	 */
3423 	un->un_f_mmc_cap = FALSE;
3424 	un->un_f_dvdram_writable_device = FALSE;
3425 	un->un_f_cfg_cdda = FALSE;
3426 
3427 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3428 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3429 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3430 
3431 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3432 
3433 	if (status != 0) {
3434 		/* command failed; just return */
3435 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3436 		return;
3437 	}
3438 	/*
3439 	 * If the mode sense request for the CDROM CAPABILITIES
3440 	 * page (0x2A) succeeds the device is assumed to be MMC.
3441 	 */
3442 	un->un_f_mmc_cap = TRUE;
3443 
3444 	/* See if GET STATUS EVENT NOTIFICATION is supported */
3445 	if (un->un_f_mmc_gesn_polling) {
3446 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3447 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3448 
3449 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3450 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3451 
3452 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3453 
3454 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3455 			un->un_f_mmc_gesn_polling = FALSE;
3456 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3457 			    "sd_set_mmc_caps: gesn not supported "
3458 			    "%d %x %x %x %x\n", rtn,
3459 			    out_data_gesn[0], out_data_gesn[1],
3460 			    out_data_gesn[2], out_data_gesn[3]);
3461 		}
3462 
3463 		kmem_free(out_data_gesn, gesn_len);
3464 	}
3465 
3466 	/* Get to the page data */
3467 	sense_mhp = (struct mode_header_grp2 *)buf;
3468 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3469 	    sense_mhp->bdesc_length_lo;
3470 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3471 		/*
3472 		 * We did not get back the expected block descriptor
3473 		 * length so we cannot determine if the device supports
3474 		 * CDDA. However, we still indicate the device is MMC
3475 		 * according to the successful response to the page
3476 		 * 0x2A mode sense request.
3477 		 */
3478 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3479 		    "sd_set_mmc_caps: Mode Sense returned "
3480 		    "invalid block descriptor length\n");
3481 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3482 		return;
3483 	}
3484 
3485 	/* See if read CDDA is supported */
3486 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3487 	    bd_len);
3488 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3489 
3490 	/* See if writing DVD RAM is supported. */
3491 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3492 	if (un->un_f_dvdram_writable_device == TRUE) {
3493 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3494 		return;
3495 	}
3496 
3497 	/*
3498 	 * If the device presents DVD or CD capabilities in the mode
3499 	 * page, we can return here since a RRD will not have
3500 	 * these capabilities.
3501 	 */
3502 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3503 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3504 		return;
3505 	}
3506 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3507 
3508 	/*
3509 	 * If un->un_f_dvdram_writable_device is still FALSE,
3510 	 * check for a Removable Rigid Disk (RRD).  A RRD
3511 	 * device is identified by the features RANDOM_WRITABLE and
3512 	 * HARDWARE_DEFECT_MANAGEMENT.
3513 	 */
3514 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3515 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3516 
3517 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3518 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3519 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3520 
3521 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3522 
3523 	if (rtn != 0) {
3524 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3525 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3526 		return;
3527 	}
3528 
3529 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3530 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3531 
3532 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3533 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3534 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3535 
3536 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3537 
3538 	if (rtn == 0) {
3539 		/*
3540 		 * We have good information, check for random writable
3541 		 * and hardware defect features.
3542 		 */
3543 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3544 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3545 			un->un_f_dvdram_writable_device = TRUE;
3546 		}
3547 	}
3548 
3549 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3550 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3551 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3552 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3553 }
3554 
3555 /*
3556  *    Function: sd_check_for_writable_cd
3557  *
3558  * Description: This routine determines if the media in the device is
3559  *		writable or not. It uses the get configuration command (0x46)
3560  *		to determine if the media is writable
3561  *
3562  *   Arguments: un - driver soft state (unit) structure
3563  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3564  *                           chain and the normal command waitq, or
3565  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3566  *                           "direct" chain and bypass the normal command
3567  *                           waitq.
3568  *
3569  *     Context: Never called at interrupt context.
3570  */
3571 
3572 static void
3573 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3574 {
3575 	struct uscsi_cmd		com;
3576 	uchar_t				*out_data;
3577 	uchar_t				*rqbuf;
3578 	int				rtn;
3579 	uchar_t				*out_data_rw, *out_data_hd;
3580 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3581 	struct mode_header_grp2		*sense_mhp;
3582 	uchar_t				*sense_page;
3583 	caddr_t				buf;
3584 	int				bd_len;
3585 	int				status;
3586 	struct sd_lun			*un;
3587 
3588 	ASSERT(ssc != NULL);
3589 	un = ssc->ssc_un;
3590 	ASSERT(un != NULL);
3591 	ASSERT(mutex_owned(SD_MUTEX(un)));
3592 
3593 	/*
3594 	 * Initialize the writable med