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