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