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