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