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