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