xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 02cdfdd6)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * SCSI disk target driver.
29  */
30 #include <sys/scsi/scsi.h>
31 #include <sys/dkbad.h>
32 #include <sys/dklabel.h>
33 #include <sys/dkio.h>
34 #include <sys/fdio.h>
35 #include <sys/cdio.h>
36 #include <sys/mhd.h>
37 #include <sys/vtoc.h>
38 #include <sys/dktp/fdisk.h>
39 #include <sys/kstat.h>
40 #include <sys/vtrace.h>
41 #include <sys/note.h>
42 #include <sys/thread.h>
43 #include <sys/proc.h>
44 #include <sys/efi_partition.h>
45 #include <sys/var.h>
46 #include <sys/aio_req.h>
47 
48 #ifdef __lock_lint
49 #define	_LP64
50 #define	__amd64
51 #endif
52 
53 #if (defined(__fibre))
54 /* Note: is there a leadville version of the following? */
55 #include <sys/fc4/fcal_linkapp.h>
56 #endif
57 #include <sys/taskq.h>
58 #include <sys/uuid.h>
59 #include <sys/byteorder.h>
60 #include <sys/sdt.h>
61 
62 #include "sd_xbuf.h"
63 
64 #include <sys/scsi/targets/sddef.h>
65 #include <sys/cmlb.h>
66 #include <sys/sysevent/eventdefs.h>
67 #include <sys/sysevent/dev.h>
68 
69 #include <sys/fm/protocol.h>
70 
71 /*
72  * Loadable module info.
73  */
74 #if (defined(__fibre))
75 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
76 char _depends_on[]	= "misc/scsi misc/cmlb drv/fcp";
77 #else /* !__fibre */
78 #define	SD_MODULE_NAME	"SCSI Disk Driver"
79 char _depends_on[]	= "misc/scsi misc/cmlb";
80 #endif /* !__fibre */
81 
82 /*
83  * Define the interconnect type, to allow the driver to distinguish
84  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
85  *
86  * This is really for backward compatibility. In the future, the driver
87  * should actually check the "interconnect-type" property as reported by
88  * the HBA; however at present this property is not defined by all HBAs,
89  * so we will use this #define (1) to permit the driver to run in
90  * backward-compatibility mode; and (2) to print a notification message
91  * if an FC HBA does not support the "interconnect-type" property.  The
92  * behavior of the driver will be to assume parallel SCSI behaviors unless
93  * the "interconnect-type" property is defined by the HBA **AND** has a
94  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
95  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
96  * Channel behaviors (as per the old ssd).  (Note that the
97  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
98  * will result in the driver assuming parallel SCSI behaviors.)
99  *
100  * (see common/sys/scsi/impl/services.h)
101  *
102  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
103  * since some FC HBAs may already support that, and there is some code in
104  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
105  * default would confuse that code, and besides things should work fine
106  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
107  * "interconnect_type" property.
108  *
109  */
110 #if (defined(__fibre))
111 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
112 #else
113 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
114 #endif
115 
116 /*
117  * The name of the driver, established from the module name in _init.
118  */
119 static	char *sd_label			= NULL;
120 
121 /*
122  * Driver name is unfortunately prefixed on some driver.conf properties.
123  */
124 #if (defined(__fibre))
125 #define	sd_max_xfer_size		ssd_max_xfer_size
126 #define	sd_config_list			ssd_config_list
127 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
128 static	char *sd_config_list		= "ssd-config-list";
129 #else
130 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
131 static	char *sd_config_list		= "sd-config-list";
132 #endif
133 
134 /*
135  * Driver global variables
136  */
137 
138 #if (defined(__fibre))
139 /*
140  * These #defines are to avoid namespace collisions that occur because this
141  * code is currently used to compile two separate driver modules: sd and ssd.
142  * All global variables need to be treated this way (even if declared static)
143  * in order to allow the debugger to resolve the names properly.
144  * It is anticipated that in the near future the ssd module will be obsoleted,
145  * at which time this namespace issue should go away.
146  */
147 #define	sd_state			ssd_state
148 #define	sd_io_time			ssd_io_time
149 #define	sd_failfast_enable		ssd_failfast_enable
150 #define	sd_ua_retry_count		ssd_ua_retry_count
151 #define	sd_report_pfa			ssd_report_pfa
152 #define	sd_max_throttle			ssd_max_throttle
153 #define	sd_min_throttle			ssd_min_throttle
154 #define	sd_rot_delay			ssd_rot_delay
155 
156 #define	sd_retry_on_reservation_conflict	\
157 					ssd_retry_on_reservation_conflict
158 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
159 #define	sd_resv_conflict_name		ssd_resv_conflict_name
160 
161 #define	sd_component_mask		ssd_component_mask
162 #define	sd_level_mask			ssd_level_mask
163 #define	sd_debug_un			ssd_debug_un
164 #define	sd_error_level			ssd_error_level
165 
166 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
167 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
168 
169 #define	sd_tr				ssd_tr
170 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
171 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
172 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
173 #define	sd_check_media_time		ssd_check_media_time
174 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
175 #define	sd_label_mutex			ssd_label_mutex
176 #define	sd_detach_mutex			ssd_detach_mutex
177 #define	sd_log_buf			ssd_log_buf
178 #define	sd_log_mutex			ssd_log_mutex
179 
180 #define	sd_disk_table			ssd_disk_table
181 #define	sd_disk_table_size		ssd_disk_table_size
182 #define	sd_sense_mutex			ssd_sense_mutex
183 #define	sd_cdbtab			ssd_cdbtab
184 
185 #define	sd_cb_ops			ssd_cb_ops
186 #define	sd_ops				ssd_ops
187 #define	sd_additional_codes		ssd_additional_codes
188 #define	sd_tgops			ssd_tgops
189 
190 #define	sd_minor_data			ssd_minor_data
191 #define	sd_minor_data_efi		ssd_minor_data_efi
192 
193 #define	sd_tq				ssd_tq
194 #define	sd_wmr_tq			ssd_wmr_tq
195 #define	sd_taskq_name			ssd_taskq_name
196 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
197 #define	sd_taskq_minalloc		ssd_taskq_minalloc
198 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
199 
200 #define	sd_dump_format_string		ssd_dump_format_string
201 
202 #define	sd_iostart_chain		ssd_iostart_chain
203 #define	sd_iodone_chain			ssd_iodone_chain
204 
205 #define	sd_pm_idletime			ssd_pm_idletime
206 
207 #define	sd_force_pm_supported		ssd_force_pm_supported
208 
209 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
210 
211 #define	sd_ssc_init			ssd_ssc_init
212 #define	sd_ssc_send			ssd_ssc_send
213 #define	sd_ssc_fini			ssd_ssc_fini
214 #define	sd_ssc_assessment		ssd_ssc_assessment
215 #define	sd_ssc_post			ssd_ssc_post
216 #define	sd_ssc_print			ssd_ssc_print
217 #define	sd_ssc_ereport_post		ssd_ssc_ereport_post
218 #define	sd_ssc_set_info			ssd_ssc_set_info
219 #define	sd_ssc_extract_info		ssd_ssc_extract_info
220 
221 #endif
222 
223 #ifdef	SDDEBUG
224 int	sd_force_pm_supported		= 0;
225 #endif	/* SDDEBUG */
226 
227 void *sd_state				= NULL;
228 int sd_io_time				= SD_IO_TIME;
229 int sd_failfast_enable			= 1;
230 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
231 int sd_report_pfa			= 1;
232 int sd_max_throttle			= SD_MAX_THROTTLE;
233 int sd_min_throttle			= SD_MIN_THROTTLE;
234 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
235 int sd_qfull_throttle_enable		= TRUE;
236 
237 int sd_retry_on_reservation_conflict	= 1;
238 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
239 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
240 
241 static int sd_dtype_optical_bind	= -1;
242 
243 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
244 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
245 
246 /*
247  * Global data for debug logging. To enable debug printing, sd_component_mask
248  * and sd_level_mask should be set to the desired bit patterns as outlined in
249  * sddef.h.
250  */
251 uint_t	sd_component_mask		= 0x0;
252 uint_t	sd_level_mask			= 0x0;
253 struct	sd_lun *sd_debug_un		= NULL;
254 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
255 
256 /* Note: these may go away in the future... */
257 static uint32_t	sd_xbuf_active_limit	= 512;
258 static uint32_t sd_xbuf_reserve_limit	= 16;
259 
260 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
261 
262 /*
263  * Timer value used to reset the throttle after it has been reduced
264  * (typically in response to TRAN_BUSY or STATUS_QFULL)
265  */
266 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
267 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
268 
269 /*
270  * Interval value associated with the media change scsi watch.
271  */
272 static int sd_check_media_time		= 3000000;
273 
274 /*
275  * Wait value used for in progress operations during a DDI_SUSPEND
276  */
277 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
278 
279 /*
280  * sd_label_mutex protects a static buffer used in the disk label
281  * component of the driver
282  */
283 static kmutex_t sd_label_mutex;
284 
285 /*
286  * sd_detach_mutex protects un_layer_count, un_detach_count, and
287  * un_opens_in_progress in the sd_lun structure.
288  */
289 static kmutex_t sd_detach_mutex;
290 
291 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
292 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
293 
294 /*
295  * Global buffer and mutex for debug logging
296  */
297 static char	sd_log_buf[1024];
298 static kmutex_t	sd_log_mutex;
299 
300 /*
301  * Structs and globals for recording attached lun information.
302  * This maintains a chain. Each node in the chain represents a SCSI controller.
303  * The structure records the number of luns attached to each target connected
304  * with the controller.
305  * For parallel scsi device only.
306  */
307 struct sd_scsi_hba_tgt_lun {
308 	struct sd_scsi_hba_tgt_lun	*next;
309 	dev_info_t			*pdip;
310 	int				nlun[NTARGETS_WIDE];
311 };
312 
313 /*
314  * Flag to indicate the lun is attached or detached
315  */
316 #define	SD_SCSI_LUN_ATTACH	0
317 #define	SD_SCSI_LUN_DETACH	1
318 
319 static kmutex_t	sd_scsi_target_lun_mutex;
320 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
321 
322 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
323     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
324 
325 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
326     sd_scsi_target_lun_head))
327 
328 /*
329  * "Smart" Probe Caching structs, globals, #defines, etc.
330  * For parallel scsi and non-self-identify device only.
331  */
332 
333 /*
334  * The following resources and routines are implemented to support
335  * "smart" probing, which caches the scsi_probe() results in an array,
336  * in order to help avoid long probe times.
337  */
338 struct sd_scsi_probe_cache {
339 	struct	sd_scsi_probe_cache	*next;
340 	dev_info_t	*pdip;
341 	int		cache[NTARGETS_WIDE];
342 };
343 
344 static kmutex_t	sd_scsi_probe_cache_mutex;
345 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
346 
347 /*
348  * Really we only need protection on the head of the linked list, but
349  * better safe than sorry.
350  */
351 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
352     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
353 
354 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
355     sd_scsi_probe_cache_head))
356 
357 /*
358  * Power attribute table
359  */
360 static sd_power_attr_ss sd_pwr_ss = {
361 	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
362 	{0, 100},
363 	{30, 0},
364 	{20000, 0}
365 };
366 
367 static sd_power_attr_pc sd_pwr_pc = {
368 	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
369 		"3=active", NULL },
370 	{0, 0, 0, 100},
371 	{90, 90, 20, 0},
372 	{15000, 15000, 1000, 0}
373 };
374 
375 /*
376  * Power level to power condition
377  */
378 static int sd_pl2pc[] = {
379 	SD_TARGET_START_VALID,
380 	SD_TARGET_STANDBY,
381 	SD_TARGET_IDLE,
382 	SD_TARGET_ACTIVE
383 };
384 
385 /*
386  * Vendor specific data name property declarations
387  */
388 
389 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
390 
391 static sd_tunables seagate_properties = {
392 	SEAGATE_THROTTLE_VALUE,
393 	0,
394 	0,
395 	0,
396 	0,
397 	0,
398 	0,
399 	0,
400 	0
401 };
402 
403 
404 static sd_tunables fujitsu_properties = {
405 	FUJITSU_THROTTLE_VALUE,
406 	0,
407 	0,
408 	0,
409 	0,
410 	0,
411 	0,
412 	0,
413 	0
414 };
415 
416 static sd_tunables ibm_properties = {
417 	IBM_THROTTLE_VALUE,
418 	0,
419 	0,
420 	0,
421 	0,
422 	0,
423 	0,
424 	0,
425 	0
426 };
427 
428 static sd_tunables purple_properties = {
429 	PURPLE_THROTTLE_VALUE,
430 	0,
431 	0,
432 	PURPLE_BUSY_RETRIES,
433 	PURPLE_RESET_RETRY_COUNT,
434 	PURPLE_RESERVE_RELEASE_TIME,
435 	0,
436 	0,
437 	0
438 };
439 
440 static sd_tunables sve_properties = {
441 	SVE_THROTTLE_VALUE,
442 	0,
443 	0,
444 	SVE_BUSY_RETRIES,
445 	SVE_RESET_RETRY_COUNT,
446 	SVE_RESERVE_RELEASE_TIME,
447 	SVE_MIN_THROTTLE_VALUE,
448 	SVE_DISKSORT_DISABLED_FLAG,
449 	0
450 };
451 
452 static sd_tunables maserati_properties = {
453 	0,
454 	0,
455 	0,
456 	0,
457 	0,
458 	0,
459 	0,
460 	MASERATI_DISKSORT_DISABLED_FLAG,
461 	MASERATI_LUN_RESET_ENABLED_FLAG
462 };
463 
464 static sd_tunables pirus_properties = {
465 	PIRUS_THROTTLE_VALUE,
466 	0,
467 	PIRUS_NRR_COUNT,
468 	PIRUS_BUSY_RETRIES,
469 	PIRUS_RESET_RETRY_COUNT,
470 	0,
471 	PIRUS_MIN_THROTTLE_VALUE,
472 	PIRUS_DISKSORT_DISABLED_FLAG,
473 	PIRUS_LUN_RESET_ENABLED_FLAG
474 };
475 
476 #endif
477 
478 #if (defined(__sparc) && !defined(__fibre)) || \
479 	(defined(__i386) || defined(__amd64))
480 
481 
482 static sd_tunables elite_properties = {
483 	ELITE_THROTTLE_VALUE,
484 	0,
485 	0,
486 	0,
487 	0,
488 	0,
489 	0,
490 	0,
491 	0
492 };
493 
494 static sd_tunables st31200n_properties = {
495 	ST31200N_THROTTLE_VALUE,
496 	0,
497 	0,
498 	0,
499 	0,
500 	0,
501 	0,
502 	0,
503 	0
504 };
505 
506 #endif /* Fibre or not */
507 
508 static sd_tunables lsi_properties_scsi = {
509 	LSI_THROTTLE_VALUE,
510 	0,
511 	LSI_NOTREADY_RETRIES,
512 	0,
513 	0,
514 	0,
515 	0,
516 	0,
517 	0
518 };
519 
520 static sd_tunables symbios_properties = {
521 	SYMBIOS_THROTTLE_VALUE,
522 	0,
523 	SYMBIOS_NOTREADY_RETRIES,
524 	0,
525 	0,
526 	0,
527 	0,
528 	0,
529 	0
530 };
531 
532 static sd_tunables lsi_properties = {
533 	0,
534 	0,
535 	LSI_NOTREADY_RETRIES,
536 	0,
537 	0,
538 	0,
539 	0,
540 	0,
541 	0
542 };
543 
544 static sd_tunables lsi_oem_properties = {
545 	0,
546 	0,
547 	LSI_OEM_NOTREADY_RETRIES,
548 	0,
549 	0,
550 	0,
551 	0,
552 	0,
553 	0,
554 	1
555 };
556 
557 
558 
559 #if (defined(SD_PROP_TST))
560 
561 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
562 #define	SD_TST_THROTTLE_VAL	16
563 #define	SD_TST_NOTREADY_VAL	12
564 #define	SD_TST_BUSY_VAL		60
565 #define	SD_TST_RST_RETRY_VAL	36
566 #define	SD_TST_RSV_REL_TIME	60
567 
568 static sd_tunables tst_properties = {
569 	SD_TST_THROTTLE_VAL,
570 	SD_TST_CTYPE_VAL,
571 	SD_TST_NOTREADY_VAL,
572 	SD_TST_BUSY_VAL,
573 	SD_TST_RST_RETRY_VAL,
574 	SD_TST_RSV_REL_TIME,
575 	0,
576 	0,
577 	0
578 };
579 #endif
580 
581 /* This is similar to the ANSI toupper implementation */
582 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
583 
584 /*
585  * Static Driver Configuration Table
586  *
587  * This is the table of disks which need throttle adjustment (or, perhaps
588  * something else as defined by the flags at a future time.)  device_id
589  * is a string consisting of concatenated vid (vendor), pid (product/model)
590  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
591  * the parts of the string are as defined by the sizes in the scsi_inquiry
592  * structure.  Device type is searched as far as the device_id string is
593  * defined.  Flags defines which values are to be set in the driver from the
594  * properties list.
595  *
596  * Entries below which begin and end with a "*" are a special case.
597  * These do not have a specific vendor, and the string which follows
598  * can appear anywhere in the 16 byte PID portion of the inquiry data.
599  *
600  * Entries below which begin and end with a " " (blank) are a special
601  * case. The comparison function will treat multiple consecutive blanks
602  * as equivalent to a single blank. For example, this causes a
603  * sd_disk_table entry of " NEC CDROM " to match a device's id string
604  * of  "NEC       CDROM".
605  *
606  * Note: The MD21 controller type has been obsoleted.
607  *	 ST318202F is a Legacy device
608  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
609  *	 made with an FC connection. The entries here are a legacy.
610  */
611 static sd_disk_config_t sd_disk_table[] = {
612 #if defined(__fibre) || defined(__i386) || defined(__amd64)
613 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
614 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
615 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
616 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
617 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
618 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
628 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
629 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
630 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
631 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
632 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
633 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
637 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
638 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
639 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
640 	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
641 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
642 	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
643 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
644 	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
645 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
663 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
664 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
665 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
666 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
667 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
668 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
669 			SD_CONF_BSET_BSY_RETRY_COUNT|
670 			SD_CONF_BSET_RST_RETRIES|
671 			SD_CONF_BSET_RSV_REL_TIME,
672 		&purple_properties },
673 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
674 		SD_CONF_BSET_BSY_RETRY_COUNT|
675 		SD_CONF_BSET_RST_RETRIES|
676 		SD_CONF_BSET_RSV_REL_TIME|
677 		SD_CONF_BSET_MIN_THROTTLE|
678 		SD_CONF_BSET_DISKSORT_DISABLED,
679 		&sve_properties },
680 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
681 			SD_CONF_BSET_BSY_RETRY_COUNT|
682 			SD_CONF_BSET_RST_RETRIES|
683 			SD_CONF_BSET_RSV_REL_TIME,
684 		&purple_properties },
685 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
686 		SD_CONF_BSET_LUN_RESET_ENABLED,
687 		&maserati_properties },
688 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
689 		SD_CONF_BSET_NRR_COUNT|
690 		SD_CONF_BSET_BSY_RETRY_COUNT|
691 		SD_CONF_BSET_RST_RETRIES|
692 		SD_CONF_BSET_MIN_THROTTLE|
693 		SD_CONF_BSET_DISKSORT_DISABLED|
694 		SD_CONF_BSET_LUN_RESET_ENABLED,
695 		&pirus_properties },
696 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
697 		SD_CONF_BSET_NRR_COUNT|
698 		SD_CONF_BSET_BSY_RETRY_COUNT|
699 		SD_CONF_BSET_RST_RETRIES|
700 		SD_CONF_BSET_MIN_THROTTLE|
701 		SD_CONF_BSET_DISKSORT_DISABLED|
702 		SD_CONF_BSET_LUN_RESET_ENABLED,
703 		&pirus_properties },
704 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
705 		SD_CONF_BSET_NRR_COUNT|
706 		SD_CONF_BSET_BSY_RETRY_COUNT|
707 		SD_CONF_BSET_RST_RETRIES|
708 		SD_CONF_BSET_MIN_THROTTLE|
709 		SD_CONF_BSET_DISKSORT_DISABLED|
710 		SD_CONF_BSET_LUN_RESET_ENABLED,
711 		&pirus_properties },
712 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
713 		SD_CONF_BSET_NRR_COUNT|
714 		SD_CONF_BSET_BSY_RETRY_COUNT|
715 		SD_CONF_BSET_RST_RETRIES|
716 		SD_CONF_BSET_MIN_THROTTLE|
717 		SD_CONF_BSET_DISKSORT_DISABLED|
718 		SD_CONF_BSET_LUN_RESET_ENABLED,
719 		&pirus_properties },
720 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
721 		SD_CONF_BSET_NRR_COUNT|
722 		SD_CONF_BSET_BSY_RETRY_COUNT|
723 		SD_CONF_BSET_RST_RETRIES|
724 		SD_CONF_BSET_MIN_THROTTLE|
725 		SD_CONF_BSET_DISKSORT_DISABLED|
726 		SD_CONF_BSET_LUN_RESET_ENABLED,
727 		&pirus_properties },
728 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
729 		SD_CONF_BSET_NRR_COUNT|
730 		SD_CONF_BSET_BSY_RETRY_COUNT|
731 		SD_CONF_BSET_RST_RETRIES|
732 		SD_CONF_BSET_MIN_THROTTLE|
733 		SD_CONF_BSET_DISKSORT_DISABLED|
734 		SD_CONF_BSET_LUN_RESET_ENABLED,
735 		&pirus_properties },
736 	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
737 	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
738 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
739 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
740 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
741 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
742 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
743 #endif /* fibre or NON-sparc platforms */
744 #if ((defined(__sparc) && !defined(__fibre)) ||\
745 	(defined(__i386) || defined(__amd64)))
746 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
747 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
748 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
749 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
750 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
751 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
752 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
753 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
754 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
755 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
756 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
757 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
758 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
759 	    &symbios_properties },
760 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
761 	    &lsi_properties_scsi },
762 #if defined(__i386) || defined(__amd64)
763 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
764 				    | SD_CONF_BSET_READSUB_BCD
765 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
766 				    | SD_CONF_BSET_NO_READ_HEADER
767 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
768 
769 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
770 				    | SD_CONF_BSET_READSUB_BCD
771 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
772 				    | SD_CONF_BSET_NO_READ_HEADER
773 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
774 #endif /* __i386 || __amd64 */
775 #endif /* sparc NON-fibre or NON-sparc platforms */
776 
777 #if (defined(SD_PROP_TST))
778 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
779 				| SD_CONF_BSET_CTYPE
780 				| SD_CONF_BSET_NRR_COUNT
781 				| SD_CONF_BSET_FAB_DEVID
782 				| SD_CONF_BSET_NOCACHE
783 				| SD_CONF_BSET_BSY_RETRY_COUNT
784 				| SD_CONF_BSET_PLAYMSF_BCD
785 				| SD_CONF_BSET_READSUB_BCD
786 				| SD_CONF_BSET_READ_TOC_TRK_BCD
787 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
788 				| SD_CONF_BSET_NO_READ_HEADER
789 				| SD_CONF_BSET_READ_CD_XD4
790 				| SD_CONF_BSET_RST_RETRIES
791 				| SD_CONF_BSET_RSV_REL_TIME
792 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
793 #endif
794 };
795 
796 static const int sd_disk_table_size =
797 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
798 
799 
800 
801 #define	SD_INTERCONNECT_PARALLEL	0
802 #define	SD_INTERCONNECT_FABRIC		1
803 #define	SD_INTERCONNECT_FIBRE		2
804 #define	SD_INTERCONNECT_SSA		3
805 #define	SD_INTERCONNECT_SATA		4
806 #define	SD_INTERCONNECT_SAS		5
807 
808 #define	SD_IS_PARALLEL_SCSI(un)		\
809 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
810 #define	SD_IS_SERIAL(un)		\
811 	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
812 	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
813 
814 /*
815  * Definitions used by device id registration routines
816  */
817 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
818 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
819 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
820 
821 static kmutex_t sd_sense_mutex = {0};
822 
823 /*
824  * Macros for updates of the driver state
825  */
826 #define	New_state(un, s)        \
827 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
828 #define	Restore_state(un)	\
829 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
830 
831 static struct sd_cdbinfo sd_cdbtab[] = {
832 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
833 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
834 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
835 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
836 };
837 
838 /*
839  * Specifies the number of seconds that must have elapsed since the last
840  * cmd. has completed for a device to be declared idle to the PM framework.
841  */
842 static int sd_pm_idletime = 1;
843 
844 /*
845  * Internal function prototypes
846  */
847 
848 #if (defined(__fibre))
849 /*
850  * These #defines are to avoid namespace collisions that occur because this
851  * code is currently used to compile two separate driver modules: sd and ssd.
852  * All function names need to be treated this way (even if declared static)
853  * in order to allow the debugger to resolve the names properly.
854  * It is anticipated that in the near future the ssd module will be obsoleted,
855  * at which time this ugliness should go away.
856  */
857 #define	sd_log_trace			ssd_log_trace
858 #define	sd_log_info			ssd_log_info
859 #define	sd_log_err			ssd_log_err
860 #define	sdprobe				ssdprobe
861 #define	sdinfo				ssdinfo
862 #define	sd_prop_op			ssd_prop_op
863 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
864 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
865 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
866 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
867 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
868 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
869 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
870 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
871 #define	sd_spin_up_unit			ssd_spin_up_unit
872 #define	sd_enable_descr_sense		ssd_enable_descr_sense
873 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
874 #define	sd_set_mmc_caps			ssd_set_mmc_caps
875 #define	sd_read_unit_properties		ssd_read_unit_properties
876 #define	sd_process_sdconf_file		ssd_process_sdconf_file
877 #define	sd_process_sdconf_table		ssd_process_sdconf_table
878 #define	sd_sdconf_id_match		ssd_sdconf_id_match
879 #define	sd_blank_cmp			ssd_blank_cmp
880 #define	sd_chk_vers1_data		ssd_chk_vers1_data
881 #define	sd_set_vers1_properties		ssd_set_vers1_properties
882 #define	sd_check_solid_state		ssd_check_solid_state
883 
884 #define	sd_get_physical_geometry	ssd_get_physical_geometry
885 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
886 #define	sd_update_block_info		ssd_update_block_info
887 #define	sd_register_devid		ssd_register_devid
888 #define	sd_get_devid			ssd_get_devid
889 #define	sd_create_devid			ssd_create_devid
890 #define	sd_write_deviceid		ssd_write_deviceid
891 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
892 #define	sd_setup_pm			ssd_setup_pm
893 #define	sd_create_pm_components		ssd_create_pm_components
894 #define	sd_ddi_suspend			ssd_ddi_suspend
895 #define	sd_ddi_resume			ssd_ddi_resume
896 #define	sd_pm_state_change		ssd_pm_state_change
897 #define	sdpower				ssdpower
898 #define	sdattach			ssdattach
899 #define	sddetach			ssddetach
900 #define	sd_unit_attach			ssd_unit_attach
901 #define	sd_unit_detach			ssd_unit_detach
902 #define	sd_set_unit_attributes		ssd_set_unit_attributes
903 #define	sd_create_errstats		ssd_create_errstats
904 #define	sd_set_errstats			ssd_set_errstats
905 #define	sd_set_pstats			ssd_set_pstats
906 #define	sddump				ssddump
907 #define	sd_scsi_poll			ssd_scsi_poll
908 #define	sd_send_polled_RQS		ssd_send_polled_RQS
909 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
910 #define	sd_init_event_callbacks		ssd_init_event_callbacks
911 #define	sd_event_callback		ssd_event_callback
912 #define	sd_cache_control		ssd_cache_control
913 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
914 #define	sd_get_nv_sup			ssd_get_nv_sup
915 #define	sd_make_device			ssd_make_device
916 #define	sdopen				ssdopen
917 #define	sdclose				ssdclose
918 #define	sd_ready_and_valid		ssd_ready_and_valid
919 #define	sdmin				ssdmin
920 #define	sdread				ssdread
921 #define	sdwrite				ssdwrite
922 #define	sdaread				ssdaread
923 #define	sdawrite			ssdawrite
924 #define	sdstrategy			ssdstrategy
925 #define	sdioctl				ssdioctl
926 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
927 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
928 #define	sd_checksum_iostart		ssd_checksum_iostart
929 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
930 #define	sd_pm_iostart			ssd_pm_iostart
931 #define	sd_core_iostart			ssd_core_iostart
932 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
933 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
934 #define	sd_checksum_iodone		ssd_checksum_iodone
935 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
936 #define	sd_pm_iodone			ssd_pm_iodone
937 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
938 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
939 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
940 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
941 #define	sd_buf_iodone			ssd_buf_iodone
942 #define	sd_uscsi_strategy		ssd_uscsi_strategy
943 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
944 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
945 #define	sd_uscsi_iodone			ssd_uscsi_iodone
946 #define	sd_xbuf_strategy		ssd_xbuf_strategy
947 #define	sd_xbuf_init			ssd_xbuf_init
948 #define	sd_pm_entry			ssd_pm_entry
949 #define	sd_pm_exit			ssd_pm_exit
950 
951 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
952 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
953 
954 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
955 #define	sdintr				ssdintr
956 #define	sd_start_cmds			ssd_start_cmds
957 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
958 #define	sd_bioclone_alloc		ssd_bioclone_alloc
959 #define	sd_bioclone_free		ssd_bioclone_free
960 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
961 #define	sd_shadow_buf_free		ssd_shadow_buf_free
962 #define	sd_print_transport_rejected_message	\
963 					ssd_print_transport_rejected_message
964 #define	sd_retry_command		ssd_retry_command
965 #define	sd_set_retry_bp			ssd_set_retry_bp
966 #define	sd_send_request_sense_command	ssd_send_request_sense_command
967 #define	sd_start_retry_command		ssd_start_retry_command
968 #define	sd_start_direct_priority_command	\
969 					ssd_start_direct_priority_command
970 #define	sd_return_failed_command	ssd_return_failed_command
971 #define	sd_return_failed_command_no_restart	\
972 					ssd_return_failed_command_no_restart
973 #define	sd_return_command		ssd_return_command
974 #define	sd_sync_with_callback		ssd_sync_with_callback
975 #define	sdrunout			ssdrunout
976 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
977 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
978 #define	sd_reduce_throttle		ssd_reduce_throttle
979 #define	sd_restore_throttle		ssd_restore_throttle
980 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
981 #define	sd_init_cdb_limits		ssd_init_cdb_limits
982 #define	sd_pkt_status_good		ssd_pkt_status_good
983 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
984 #define	sd_pkt_status_busy		ssd_pkt_status_busy
985 #define	sd_pkt_status_reservation_conflict	\
986 					ssd_pkt_status_reservation_conflict
987 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
988 #define	sd_handle_request_sense		ssd_handle_request_sense
989 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
990 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
991 #define	sd_validate_sense_data		ssd_validate_sense_data
992 #define	sd_decode_sense			ssd_decode_sense
993 #define	sd_print_sense_msg		ssd_print_sense_msg
994 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
995 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
996 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
997 #define	sd_sense_key_medium_or_hardware_error	\
998 					ssd_sense_key_medium_or_hardware_error
999 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1000 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1001 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1002 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1003 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1004 #define	sd_sense_key_default		ssd_sense_key_default
1005 #define	sd_print_retry_msg		ssd_print_retry_msg
1006 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1007 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1008 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1009 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1010 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1011 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1012 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1013 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1014 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1015 #define	sd_reset_target			ssd_reset_target
1016 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1017 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1018 #define	sd_taskq_create			ssd_taskq_create
1019 #define	sd_taskq_delete			ssd_taskq_delete
1020 #define	sd_target_change_task		ssd_target_change_task
1021 #define	sd_log_dev_status_event		ssd_log_dev_status_event
1022 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1023 #define	sd_log_eject_request_event	ssd_log_eject_request_event
1024 #define	sd_media_change_task		ssd_media_change_task
1025 #define	sd_handle_mchange		ssd_handle_mchange
1026 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1027 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1028 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1029 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1030 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1031 					sd_send_scsi_feature_GET_CONFIGURATION
1032 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1033 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1034 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1035 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1036 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1037 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1038 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1039 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1040 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1041 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1042 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1043 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1044 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1045 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1046 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1047 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1048 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1049 #define	sd_alloc_rqs			ssd_alloc_rqs
1050 #define	sd_free_rqs			ssd_free_rqs
1051 #define	sd_dump_memory			ssd_dump_memory
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 
1136 int _init(void);
1137 int _fini(void);
1138 int _info(struct modinfo *modinfop);
1139 
1140 /*PRINTFLIKE3*/
1141 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1142 /*PRINTFLIKE3*/
1143 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1144 /*PRINTFLIKE3*/
1145 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1146 
1147 static int sdprobe(dev_info_t *devi);
1148 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1149     void **result);
1150 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1151     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1152 
1153 /*
1154  * Smart probe for parallel scsi
1155  */
1156 static void sd_scsi_probe_cache_init(void);
1157 static void sd_scsi_probe_cache_fini(void);
1158 static void sd_scsi_clear_probe_cache(void);
1159 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1160 
1161 /*
1162  * Attached luns on target for parallel scsi
1163  */
1164 static void sd_scsi_target_lun_init(void);
1165 static void sd_scsi_target_lun_fini(void);
1166 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1167 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1168 
1169 static int	sd_spin_up_unit(sd_ssc_t *ssc);
1170 
1171 /*
1172  * Using sd_ssc_init to establish sd_ssc_t struct
1173  * Using sd_ssc_send to send uscsi internal command
1174  * Using sd_ssc_fini to free sd_ssc_t struct
1175  */
1176 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1177 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1178     int flag, enum uio_seg dataspace, int path_flag);
1179 static void sd_ssc_fini(sd_ssc_t *ssc);
1180 
1181 /*
1182  * Using sd_ssc_assessment to set correct type-of-assessment
1183  * Using sd_ssc_post to post ereport & system log
1184  *       sd_ssc_post will call sd_ssc_print to print system log
1185  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1186  */
1187 static void sd_ssc_assessment(sd_ssc_t *ssc,
1188     enum sd_type_assessment tp_assess);
1189 
1190 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1191 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1192 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1193     enum sd_driver_assessment drv_assess);
1194 
1195 /*
1196  * Using sd_ssc_set_info to mark an un-decodable-data error.
1197  * Using sd_ssc_extract_info to transfer information from internal
1198  *       data structures to sd_ssc_t.
1199  */
1200 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1201     const char *fmt, ...);
1202 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1203     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1204 
1205 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1206     enum uio_seg dataspace, int path_flag);
1207 
1208 #ifdef _LP64
1209 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1210 static void	sd_reenable_dsense_task(void *arg);
1211 #endif /* _LP64 */
1212 
1213 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1214 
1215 static void sd_read_unit_properties(struct sd_lun *un);
1216 static int  sd_process_sdconf_file(struct sd_lun *un);
1217 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1218 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1219 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1220 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1221     int *data_list, sd_tunables *values);
1222 static void sd_process_sdconf_table(struct sd_lun *un);
1223 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1224 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1225 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1226 	int list_len, char *dataname_ptr);
1227 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1228     sd_tunables *prop_list);
1229 
1230 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1231     int reservation_flag);
1232 static int  sd_get_devid(sd_ssc_t *ssc);
1233 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1234 static int  sd_write_deviceid(sd_ssc_t *ssc);
1235 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1236 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1237 
1238 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1239 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1240 
1241 static int  sd_ddi_suspend(dev_info_t *devi);
1242 static int  sd_ddi_resume(dev_info_t *devi);
1243 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1244 static int  sdpower(dev_info_t *devi, int component, int level);
1245 
1246 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1247 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1248 static int  sd_unit_attach(dev_info_t *devi);
1249 static int  sd_unit_detach(dev_info_t *devi);
1250 
1251 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1252 static void sd_create_errstats(struct sd_lun *un, int instance);
1253 static void sd_set_errstats(struct sd_lun *un);
1254 static void sd_set_pstats(struct sd_lun *un);
1255 
1256 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1257 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1258 static int  sd_send_polled_RQS(struct sd_lun *un);
1259 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1260 
1261 #if (defined(__fibre))
1262 /*
1263  * Event callbacks (photon)
1264  */
1265 static void sd_init_event_callbacks(struct sd_lun *un);
1266 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1267 #endif
1268 
1269 /*
1270  * Defines for sd_cache_control
1271  */
1272 
1273 #define	SD_CACHE_ENABLE		1
1274 #define	SD_CACHE_DISABLE	0
1275 #define	SD_CACHE_NOCHANGE	-1
1276 
1277 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1278 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1279 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1280 static dev_t sd_make_device(dev_info_t *devi);
1281 static void  sd_check_solid_state(sd_ssc_t *ssc);
1282 
1283 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1284 	uint64_t capacity);
1285 
1286 /*
1287  * Driver entry point functions.
1288  */
1289 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1290 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1291 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1292 
1293 static void sdmin(struct buf *bp);
1294 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1295 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1296 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1297 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1298 
1299 static int sdstrategy(struct buf *bp);
1300 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1301 
1302 /*
1303  * Function prototypes for layering functions in the iostart chain.
1304  */
1305 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1306 	struct buf *bp);
1307 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1308 	struct buf *bp);
1309 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1310 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1311 	struct buf *bp);
1312 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1313 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1314 
1315 /*
1316  * Function prototypes for layering functions in the iodone chain.
1317  */
1318 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1319 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1320 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1321 	struct buf *bp);
1322 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1323 	struct buf *bp);
1324 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1325 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1326 	struct buf *bp);
1327 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1328 
1329 /*
1330  * Prototypes for functions to support buf(9S) based IO.
1331  */
1332 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1333 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1334 static void sd_destroypkt_for_buf(struct buf *);
1335 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1336 	struct buf *bp, int flags,
1337 	int (*callback)(caddr_t), caddr_t callback_arg,
1338 	diskaddr_t lba, uint32_t blockcount);
1339 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1340 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1341 
1342 /*
1343  * Prototypes for functions to support USCSI IO.
1344  */
1345 static int sd_uscsi_strategy(struct buf *bp);
1346 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1347 static void sd_destroypkt_for_uscsi(struct buf *);
1348 
1349 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1350 	uchar_t chain_type, void *pktinfop);
1351 
1352 static int  sd_pm_entry(struct sd_lun *un);
1353 static void sd_pm_exit(struct sd_lun *un);
1354 
1355 static void sd_pm_idletimeout_handler(void *arg);
1356 
1357 /*
1358  * sd_core internal functions (used at the sd_core_io layer).
1359  */
1360 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1361 static void sdintr(struct scsi_pkt *pktp);
1362 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1363 
1364 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1365 	enum uio_seg dataspace, int path_flag);
1366 
1367 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1368 	daddr_t blkno, int (*func)(struct buf *));
1369 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1370 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1371 static void sd_bioclone_free(struct buf *bp);
1372 static void sd_shadow_buf_free(struct buf *bp);
1373 
1374 static void sd_print_transport_rejected_message(struct sd_lun *un,
1375 	struct sd_xbuf *xp, int code);
1376 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1377     void *arg, int code);
1378 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1379     void *arg, int code);
1380 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1381     void *arg, int code);
1382 
1383 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1384 	int retry_check_flag,
1385 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1386 		int c),
1387 	void *user_arg, int failure_code,  clock_t retry_delay,
1388 	void (*statp)(kstat_io_t *));
1389 
1390 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1391 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1392 
1393 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1394 	struct scsi_pkt *pktp);
1395 static void sd_start_retry_command(void *arg);
1396 static void sd_start_direct_priority_command(void *arg);
1397 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1398 	int errcode);
1399 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1400 	struct buf *bp, int errcode);
1401 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1402 static void sd_sync_with_callback(struct sd_lun *un);
1403 static int sdrunout(caddr_t arg);
1404 
1405 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1406 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1407 
1408 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1409 static void sd_restore_throttle(void *arg);
1410 
1411 static void sd_init_cdb_limits(struct sd_lun *un);
1412 
1413 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1414 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1415 
1416 /*
1417  * Error handling functions
1418  */
1419 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1420 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1421 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1422 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1423 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1424 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1425 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1426 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1427 
1428 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1429 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1430 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1431 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1432 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1433 	struct sd_xbuf *xp, size_t actual_len);
1434 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1435 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1436 
1437 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1438 	void *arg, int code);
1439 
1440 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1441 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1442 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1443 	uint8_t *sense_datap,
1444 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1445 static void sd_sense_key_not_ready(struct sd_lun *un,
1446 	uint8_t *sense_datap,
1447 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1448 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1449 	uint8_t *sense_datap,
1450 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1451 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1452 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1453 static void sd_sense_key_unit_attention(struct sd_lun *un,
1454 	uint8_t *sense_datap,
1455 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1456 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1457 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1458 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1459 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1460 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1461 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1462 static void sd_sense_key_default(struct sd_lun *un,
1463 	uint8_t *sense_datap,
1464 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465 
1466 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1467 	void *arg, int flag);
1468 
1469 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1470 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1471 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1472 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1473 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1474 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1475 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1476 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1477 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1478 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1479 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1480 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1481 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1482 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1483 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1484 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1485 
1486 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1487 
1488 static void sd_start_stop_unit_callback(void *arg);
1489 static void sd_start_stop_unit_task(void *arg);
1490 
1491 static void sd_taskq_create(void);
1492 static void sd_taskq_delete(void);
1493 static void sd_target_change_task(void *arg);
1494 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1495 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1496 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1497 static void sd_media_change_task(void *arg);
1498 
1499 static int sd_handle_mchange(struct sd_lun *un);
1500 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1501 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1502 	uint32_t *lbap, int path_flag);
1503 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1504 	uint32_t *lbap, uint32_t *psp, int path_flag);
1505 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1506 	int flag, int path_flag);
1507 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1508 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1509 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1510 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1511 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1512 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1513 	uchar_t usr_cmd, uchar_t *usr_bufp);
1514 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1515 	struct dk_callback *dkc);
1516 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1517 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1518 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1519 	uchar_t *bufaddr, uint_t buflen, int path_flag);
1520 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1521 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1522 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1523 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1524 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1525 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1526 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1527 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1528 	size_t buflen, daddr_t start_block, int path_flag);
1529 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1530 	sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1531 	path_flag)
1532 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1533 	sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1534 	path_flag)
1535 
1536 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1537 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1538 	uint16_t param_ptr, int path_flag);
1539 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1540 	uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1541 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1542 
1543 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1544 static void sd_free_rqs(struct sd_lun *un);
1545 
1546 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1547 	uchar_t *data, int len, int fmt);
1548 static void sd_panic_for_res_conflict(struct sd_lun *un);
1549 
1550 /*
1551  * Disk Ioctl Function Prototypes
1552  */
1553 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1554 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1555 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1556 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1557 
1558 /*
1559  * Multi-host Ioctl Prototypes
1560  */
1561 static int sd_check_mhd(dev_t dev, int interval);
1562 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1563 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1564 static char *sd_sname(uchar_t status);
1565 static void sd_mhd_resvd_recover(void *arg);
1566 static void sd_resv_reclaim_thread();
1567 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1568 static int sd_reserve_release(dev_t dev, int cmd);
1569 static void sd_rmv_resv_reclaim_req(dev_t dev);
1570 static void sd_mhd_reset_notify_cb(caddr_t arg);
1571 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1572 	mhioc_inkeys_t *usrp, int flag);
1573 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1574 	mhioc_inresvs_t *usrp, int flag);
1575 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1576 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1577 static int sd_mhdioc_release(dev_t dev);
1578 static int sd_mhdioc_register_devid(dev_t dev);
1579 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1580 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1581 
1582 /*
1583  * SCSI removable prototypes
1584  */
1585 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1586 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1587 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1588 static int sr_pause_resume(dev_t dev, int mode);
1589 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1590 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1591 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1592 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1593 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1594 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1595 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1596 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1597 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1598 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1599 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1600 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1601 static int sr_eject(dev_t dev);
1602 static void sr_ejected(register struct sd_lun *un);
1603 static int sr_check_wp(dev_t dev);
1604 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1605 static int sd_check_media(dev_t dev, enum dkio_state state);
1606 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1607 static void sd_delayed_cv_broadcast(void *arg);
1608 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1609 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1610 
1611 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1612 
1613 /*
1614  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1615  */
1616 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1617 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1618 static void sd_wm_cache_destructor(void *wm, void *un);
1619 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1620 	daddr_t endb, ushort_t typ);
1621 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1622 	daddr_t endb);
1623 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1624 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1625 static void sd_read_modify_write_task(void * arg);
1626 static int
1627 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1628 	struct buf **bpp);
1629 
1630 
1631 /*
1632  * Function prototypes for failfast support.
1633  */
1634 static void sd_failfast_flushq(struct sd_lun *un);
1635 static int sd_failfast_flushq_callback(struct buf *bp);
1636 
1637 /*
1638  * Function prototypes to check for lsi devices
1639  */
1640 static void sd_is_lsi(struct sd_lun *un);
1641 
1642 /*
1643  * Function prototypes for partial DMA support
1644  */
1645 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1646 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1647 
1648 
1649 /* Function prototypes for cmlb */
1650 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1651     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1652 
1653 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1654 
1655 /*
1656  * For printing RMW warning message timely
1657  */
1658 static void sd_rmw_msg_print_handler(void *arg);
1659 
1660 /*
1661  * Constants for failfast support:
1662  *
1663  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1664  * failfast processing being performed.
1665  *
1666  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1667  * failfast processing on all bufs with B_FAILFAST set.
1668  */
1669 
1670 #define	SD_FAILFAST_INACTIVE		0
1671 #define	SD_FAILFAST_ACTIVE		1
1672 
1673 /*
1674  * Bitmask to control behavior of buf(9S) flushes when a transition to
1675  * the failfast state occurs. Optional bits include:
1676  *
1677  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1678  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1679  * be flushed.
1680  *
1681  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1682  * driver, in addition to the regular wait queue. This includes the xbuf
1683  * queues. When clear, only the driver's wait queue will be flushed.
1684  */
1685 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1686 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1687 
1688 /*
1689  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1690  * to flush all queues within the driver.
1691  */
1692 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1693 
1694 
1695 /*
1696  * SD Testing Fault Injection
1697  */
1698 #ifdef SD_FAULT_INJECTION
1699 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1700 static void sd_faultinjection(struct scsi_pkt *pktp);
1701 static void sd_injection_log(char *buf, struct sd_lun *un);
1702 #endif
1703 
1704 /*
1705  * Device driver ops vector
1706  */
1707 static struct cb_ops sd_cb_ops = {
1708 	sdopen,			/* open */
1709 	sdclose,		/* close */
1710 	sdstrategy,		/* strategy */
1711 	nodev,			/* print */
1712 	sddump,			/* dump */
1713 	sdread,			/* read */
1714 	sdwrite,		/* write */
1715 	sdioctl,		/* ioctl */
1716 	nodev,			/* devmap */
1717 	nodev,			/* mmap */
1718 	nodev,			/* segmap */
1719 	nochpoll,		/* poll */
1720 	sd_prop_op,		/* cb_prop_op */
1721 	0,			/* streamtab  */
1722 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1723 	CB_REV,			/* cb_rev */
1724 	sdaread, 		/* async I/O read entry point */
1725 	sdawrite		/* async I/O write entry point */
1726 };
1727 
1728 struct dev_ops sd_ops = {
1729 	DEVO_REV,		/* devo_rev, */
1730 	0,			/* refcnt  */
1731 	sdinfo,			/* info */
1732 	nulldev,		/* identify */
1733 	sdprobe,		/* probe */
1734 	sdattach,		/* attach */
1735 	sddetach,		/* detach */
1736 	nodev,			/* reset */
1737 	&sd_cb_ops,		/* driver operations */
1738 	NULL,			/* bus operations */
1739 	sdpower,		/* power */
1740 	ddi_quiesce_not_needed,		/* quiesce */
1741 };
1742 
1743 /*
1744  * This is the loadable module wrapper.
1745  */
1746 #include <sys/modctl.h>
1747 
1748 #ifndef XPV_HVM_DRIVER
1749 static struct modldrv modldrv = {
1750 	&mod_driverops,		/* Type of module. This one is a driver */
1751 	SD_MODULE_NAME,		/* Module name. */
1752 	&sd_ops			/* driver ops */
1753 };
1754 
1755 static struct modlinkage modlinkage = {
1756 	MODREV_1, &modldrv, NULL
1757 };
1758 
1759 #else /* XPV_HVM_DRIVER */
1760 static struct modlmisc modlmisc = {
1761 	&mod_miscops,		/* Type of module. This one is a misc */
1762 	"HVM " SD_MODULE_NAME,		/* Module name. */
1763 };
1764 
1765 static struct modlinkage modlinkage = {
1766 	MODREV_1, &modlmisc, NULL
1767 };
1768 
1769 #endif /* XPV_HVM_DRIVER */
1770 
1771 static cmlb_tg_ops_t sd_tgops = {
1772 	TG_DK_OPS_VERSION_1,
1773 	sd_tg_rdwr,
1774 	sd_tg_getinfo
1775 };
1776 
1777 static struct scsi_asq_key_strings sd_additional_codes[] = {
1778 	0x81, 0, "Logical Unit is Reserved",
1779 	0x85, 0, "Audio Address Not Valid",
1780 	0xb6, 0, "Media Load Mechanism Failed",
1781 	0xB9, 0, "Audio Play Operation Aborted",
1782 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1783 	0x53, 2, "Medium removal prevented",
1784 	0x6f, 0, "Authentication failed during key exchange",
1785 	0x6f, 1, "Key not present",
1786 	0x6f, 2, "Key not established",
1787 	0x6f, 3, "Read without proper authentication",
1788 	0x6f, 4, "Mismatched region to this logical unit",
1789 	0x6f, 5, "Region reset count error",
1790 	0xffff, 0x0, NULL
1791 };
1792 
1793 
1794 /*
1795  * Struct for passing printing information for sense data messages
1796  */
1797 struct sd_sense_info {
1798 	int	ssi_severity;
1799 	int	ssi_pfa_flag;
1800 };
1801 
1802 /*
1803  * Table of function pointers for iostart-side routines. Separate "chains"
1804  * of layered function calls are formed by placing the function pointers
1805  * sequentially in the desired order. Functions are called according to an
1806  * incrementing table index ordering. The last function in each chain must
1807  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1808  * in the sd_iodone_chain[] array.
1809  *
1810  * Note: It may seem more natural to organize both the iostart and iodone
1811  * functions together, into an array of structures (or some similar
1812  * organization) with a common index, rather than two separate arrays which
1813  * must be maintained in synchronization. The purpose of this division is
1814  * to achieve improved performance: individual arrays allows for more
1815  * effective cache line utilization on certain platforms.
1816  */
1817 
1818 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1819 
1820 
1821 static sd_chain_t sd_iostart_chain[] = {
1822 
1823 	/* Chain for buf IO for disk drive targets (PM enabled) */
1824 	sd_mapblockaddr_iostart,	/* Index: 0 */
1825 	sd_pm_iostart,			/* Index: 1 */
1826 	sd_core_iostart,		/* Index: 2 */
1827 
1828 	/* Chain for buf IO for disk drive targets (PM disabled) */
1829 	sd_mapblockaddr_iostart,	/* Index: 3 */
1830 	sd_core_iostart,		/* Index: 4 */
1831 
1832 	/*
1833 	 * Chain for buf IO for removable-media or large sector size
1834 	 * disk drive targets with RMW needed (PM enabled)
1835 	 */
1836 	sd_mapblockaddr_iostart,	/* Index: 5 */
1837 	sd_mapblocksize_iostart,	/* Index: 6 */
1838 	sd_pm_iostart,			/* Index: 7 */
1839 	sd_core_iostart,		/* Index: 8 */
1840 
1841 	/*
1842 	 * Chain for buf IO for removable-media or large sector size
1843 	 * disk drive targets with RMW needed (PM disabled)
1844 	 */
1845 	sd_mapblockaddr_iostart,	/* Index: 9 */
1846 	sd_mapblocksize_iostart,	/* Index: 10 */
1847 	sd_core_iostart,		/* Index: 11 */
1848 
1849 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1850 	sd_mapblockaddr_iostart,	/* Index: 12 */
1851 	sd_checksum_iostart,		/* Index: 13 */
1852 	sd_pm_iostart,			/* Index: 14 */
1853 	sd_core_iostart,		/* Index: 15 */
1854 
1855 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1856 	sd_mapblockaddr_iostart,	/* Index: 16 */
1857 	sd_checksum_iostart,		/* Index: 17 */
1858 	sd_core_iostart,		/* Index: 18 */
1859 
1860 	/* Chain for USCSI commands (all targets) */
1861 	sd_pm_iostart,			/* Index: 19 */
1862 	sd_core_iostart,		/* Index: 20 */
1863 
1864 	/* Chain for checksumming USCSI commands (all targets) */
1865 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1866 	sd_pm_iostart,			/* Index: 22 */
1867 	sd_core_iostart,		/* Index: 23 */
1868 
1869 	/* Chain for "direct" USCSI commands (all targets) */
1870 	sd_core_iostart,		/* Index: 24 */
1871 
1872 	/* Chain for "direct priority" USCSI commands (all targets) */
1873 	sd_core_iostart,		/* Index: 25 */
1874 
1875 	/*
1876 	 * Chain for buf IO for large sector size disk drive targets
1877 	 * with RMW needed with checksumming (PM enabled)
1878 	 */
1879 	sd_mapblockaddr_iostart,	/* Index: 26 */
1880 	sd_mapblocksize_iostart,	/* Index: 27 */
1881 	sd_checksum_iostart,		/* Index: 28 */
1882 	sd_pm_iostart,			/* Index: 29 */
1883 	sd_core_iostart,		/* Index: 30 */
1884 
1885 	/*
1886 	 * Chain for buf IO for large sector size disk drive targets
1887 	 * with RMW needed with checksumming (PM disabled)
1888 	 */
1889 	sd_mapblockaddr_iostart,	/* Index: 31 */
1890 	sd_mapblocksize_iostart,	/* Index: 32 */
1891 	sd_checksum_iostart,		/* Index: 33 */
1892 	sd_core_iostart,		/* Index: 34 */
1893 
1894 };
1895 
1896 /*
1897  * Macros to locate the first function of each iostart chain in the
1898  * sd_iostart_chain[] array. These are located by the index in the array.
1899  */
1900 #define	SD_CHAIN_DISK_IOSTART			0
1901 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1902 #define	SD_CHAIN_MSS_DISK_IOSTART		5
1903 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1904 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1905 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1906 #define	SD_CHAIN_CHKSUM_IOSTART			12
1907 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1908 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1909 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1910 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1911 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1912 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1913 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1914 
1915 
1916 /*
1917  * Table of function pointers for the iodone-side routines for the driver-
1918  * internal layering mechanism.  The calling sequence for iodone routines
1919  * uses a decrementing table index, so the last routine called in a chain
1920  * must be at the lowest array index location for that chain.  The last
1921  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1922  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1923  * of the functions in an iodone side chain must correspond to the ordering
1924  * of the iostart routines for that chain.  Note that there is no iodone
1925  * side routine that corresponds to sd_core_iostart(), so there is no
1926  * entry in the table for this.
1927  */
1928 
1929 static sd_chain_t sd_iodone_chain[] = {
1930 
1931 	/* Chain for buf IO for disk drive targets (PM enabled) */
1932 	sd_buf_iodone,			/* Index: 0 */
1933 	sd_mapblockaddr_iodone,		/* Index: 1 */
1934 	sd_pm_iodone,			/* Index: 2 */
1935 
1936 	/* Chain for buf IO for disk drive targets (PM disabled) */
1937 	sd_buf_iodone,			/* Index: 3 */
1938 	sd_mapblockaddr_iodone,		/* Index: 4 */
1939 
1940 	/*
1941 	 * Chain for buf IO for removable-media or large sector size
1942 	 * disk drive targets with RMW needed (PM enabled)
1943 	 */
1944 	sd_buf_iodone,			/* Index: 5 */
1945 	sd_mapblockaddr_iodone,		/* Index: 6 */
1946 	sd_mapblocksize_iodone,		/* Index: 7 */
1947 	sd_pm_iodone,			/* Index: 8 */
1948 
1949 	/*
1950 	 * Chain for buf IO for removable-media or large sector size
1951 	 * disk drive targets with RMW needed (PM disabled)
1952 	 */
1953 	sd_buf_iodone,			/* Index: 9 */
1954 	sd_mapblockaddr_iodone,		/* Index: 10 */
1955 	sd_mapblocksize_iodone,		/* Index: 11 */
1956 
1957 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1958 	sd_buf_iodone,			/* Index: 12 */
1959 	sd_mapblockaddr_iodone,		/* Index: 13 */
1960 	sd_checksum_iodone,		/* Index: 14 */
1961 	sd_pm_iodone,			/* Index: 15 */
1962 
1963 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1964 	sd_buf_iodone,			/* Index: 16 */
1965 	sd_mapblockaddr_iodone,		/* Index: 17 */
1966 	sd_checksum_iodone,		/* Index: 18 */
1967 
1968 	/* Chain for USCSI commands (non-checksum targets) */
1969 	sd_uscsi_iodone,		/* Index: 19 */
1970 	sd_pm_iodone,			/* Index: 20 */
1971 
1972 	/* Chain for USCSI commands (checksum targets) */
1973 	sd_uscsi_iodone,		/* Index: 21 */
1974 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1975 	sd_pm_iodone,			/* Index: 22 */
1976 
1977 	/* Chain for "direct" USCSI commands (all targets) */
1978 	sd_uscsi_iodone,		/* Index: 24 */
1979 
1980 	/* Chain for "direct priority" USCSI commands (all targets) */
1981 	sd_uscsi_iodone,		/* Index: 25 */
1982 
1983 	/*
1984 	 * Chain for buf IO for large sector size disk drive targets
1985 	 * with checksumming (PM enabled)
1986 	 */
1987 	sd_buf_iodone,			/* Index: 26 */
1988 	sd_mapblockaddr_iodone,		/* Index: 27 */
1989 	sd_mapblocksize_iodone,		/* Index: 28 */
1990 	sd_checksum_iodone,		/* Index: 29 */
1991 	sd_pm_iodone,			/* Index: 30 */
1992 
1993 	/*
1994 	 * Chain for buf IO for large sector size disk drive targets
1995 	 * with checksumming (PM disabled)
1996 	 */
1997 	sd_buf_iodone,			/* Index: 31 */
1998 	sd_mapblockaddr_iodone,		/* Index: 32 */
1999 	sd_mapblocksize_iodone,		/* Index: 33 */
2000 	sd_checksum_iodone,		/* Index: 34 */
2001 };
2002 
2003 
2004 /*
2005  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2006  * each iodone-side chain. These are located by the array index, but as the
2007  * iodone side functions are called in a decrementing-index order, the
2008  * highest index number in each chain must be specified (as these correspond
2009  * to the first function in the iodone chain that will be called by the core
2010  * at IO completion time).
2011  */
2012 
2013 #define	SD_CHAIN_DISK_IODONE			2
2014 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
2015 #define	SD_CHAIN_RMMEDIA_IODONE			8
2016 #define	SD_CHAIN_MSS_DISK_IODONE		8
2017 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2018 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2019 #define	SD_CHAIN_CHKSUM_IODONE			15
2020 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2021 #define	SD_CHAIN_USCSI_CMD_IODONE		20
2022 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2023 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
2024 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2025 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2026 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2027 
2028 
2029 
2030 /*
2031  * Array to map a layering chain index to the appropriate initpkt routine.
2032  * The redundant entries are present so that the index used for accessing
2033  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2034  * with this table as well.
2035  */
2036 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2037 
2038 static sd_initpkt_t	sd_initpkt_map[] = {
2039 
2040 	/* Chain for buf IO for disk drive targets (PM enabled) */
2041 	sd_initpkt_for_buf,		/* Index: 0 */
2042 	sd_initpkt_for_buf,		/* Index: 1 */
2043 	sd_initpkt_for_buf,		/* Index: 2 */
2044 
2045 	/* Chain for buf IO for disk drive targets (PM disabled) */
2046 	sd_initpkt_for_buf,		/* Index: 3 */
2047 	sd_initpkt_for_buf,		/* Index: 4 */
2048 
2049 	/*
2050 	 * Chain for buf IO for removable-media or large sector size
2051 	 * disk drive targets (PM enabled)
2052 	 */
2053 	sd_initpkt_for_buf,		/* Index: 5 */
2054 	sd_initpkt_for_buf,		/* Index: 6 */
2055 	sd_initpkt_for_buf,		/* Index: 7 */
2056 	sd_initpkt_for_buf,		/* Index: 8 */
2057 
2058 	/*
2059 	 * Chain for buf IO for removable-media or large sector size
2060 	 * disk drive targets (PM disabled)
2061 	 */
2062 	sd_initpkt_for_buf,		/* Index: 9 */
2063 	sd_initpkt_for_buf,		/* Index: 10 */
2064 	sd_initpkt_for_buf,		/* Index: 11 */
2065 
2066 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2067 	sd_initpkt_for_buf,		/* Index: 12 */
2068 	sd_initpkt_for_buf,		/* Index: 13 */
2069 	sd_initpkt_for_buf,		/* Index: 14 */
2070 	sd_initpkt_for_buf,		/* Index: 15 */
2071 
2072 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2073 	sd_initpkt_for_buf,		/* Index: 16 */
2074 	sd_initpkt_for_buf,		/* Index: 17 */
2075 	sd_initpkt_for_buf,		/* Index: 18 */
2076 
2077 	/* Chain for USCSI commands (non-checksum targets) */
2078 	sd_initpkt_for_uscsi,		/* Index: 19 */
2079 	sd_initpkt_for_uscsi,		/* Index: 20 */
2080 
2081 	/* Chain for USCSI commands (checksum targets) */
2082 	sd_initpkt_for_uscsi,		/* Index: 21 */
2083 	sd_initpkt_for_uscsi,		/* Index: 22 */
2084 	sd_initpkt_for_uscsi,		/* Index: 22 */
2085 
2086 	/* Chain for "direct" USCSI commands (all targets) */
2087 	sd_initpkt_for_uscsi,		/* Index: 24 */
2088 
2089 	/* Chain for "direct priority" USCSI commands (all targets) */
2090 	sd_initpkt_for_uscsi,		/* Index: 25 */
2091 
2092 	/*
2093 	 * Chain for buf IO for large sector size disk drive targets
2094 	 * with checksumming (PM enabled)
2095 	 */
2096 	sd_initpkt_for_buf,		/* Index: 26 */
2097 	sd_initpkt_for_buf,		/* Index: 27 */
2098 	sd_initpkt_for_buf,		/* Index: 28 */
2099 	sd_initpkt_for_buf,		/* Index: 29 */
2100 	sd_initpkt_for_buf,		/* Index: 30 */
2101 
2102 	/*
2103 	 * Chain for buf IO for large sector size disk drive targets
2104 	 * with checksumming (PM disabled)
2105 	 */
2106 	sd_initpkt_for_buf,		/* Index: 31 */
2107 	sd_initpkt_for_buf,		/* Index: 32 */
2108 	sd_initpkt_for_buf,		/* Index: 33 */
2109 	sd_initpkt_for_buf,		/* Index: 34 */
2110 };
2111 
2112 
2113 /*
2114  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2115  * The redundant entries are present so that the index used for accessing
2116  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2117  * with this table as well.
2118  */
2119 typedef void (*sd_destroypkt_t)(struct buf *);
2120 
2121 static sd_destroypkt_t	sd_destroypkt_map[] = {
2122 
2123 	/* Chain for buf IO for disk drive targets (PM enabled) */
2124 	sd_destroypkt_for_buf,		/* Index: 0 */
2125 	sd_destroypkt_for_buf,		/* Index: 1 */
2126 	sd_destroypkt_for_buf,		/* Index: 2 */
2127 
2128 	/* Chain for buf IO for disk drive targets (PM disabled) */
2129 	sd_destroypkt_for_buf,		/* Index: 3 */
2130 	sd_destroypkt_for_buf,		/* Index: 4 */
2131 
2132 	/*
2133 	 * Chain for buf IO for removable-media or large sector size
2134 	 * disk drive targets (PM enabled)
2135 	 */
2136 	sd_destroypkt_for_buf,		/* Index: 5 */
2137 	sd_destroypkt_for_buf,		/* Index: 6 */
2138 	sd_destroypkt_for_buf,		/* Index: 7 */
2139 	sd_destroypkt_for_buf,		/* Index: 8 */
2140 
2141 	/*
2142 	 * Chain for buf IO for removable-media or large sector size
2143 	 * disk drive targets (PM disabled)
2144 	 */
2145 	sd_destroypkt_for_buf,		/* Index: 9 */
2146 	sd_destroypkt_for_buf,		/* Index: 10 */
2147 	sd_destroypkt_for_buf,		/* Index: 11 */
2148 
2149 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2150 	sd_destroypkt_for_buf,		/* Index: 12 */
2151 	sd_destroypkt_for_buf,		/* Index: 13 */
2152 	sd_destroypkt_for_buf,		/* Index: 14 */
2153 	sd_destroypkt_for_buf,		/* Index: 15 */
2154 
2155 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2156 	sd_destroypkt_for_buf,		/* Index: 16 */
2157 	sd_destroypkt_for_buf,		/* Index: 17 */
2158 	sd_destroypkt_for_buf,		/* Index: 18 */
2159 
2160 	/* Chain for USCSI commands (non-checksum targets) */
2161 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2162 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2163 
2164 	/* Chain for USCSI commands (checksum targets) */
2165 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2166 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2167 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2168 
2169 	/* Chain for "direct" USCSI commands (all targets) */
2170 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2171 
2172 	/* Chain for "direct priority" USCSI commands (all targets) */
2173 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2174 
2175 	/*
2176 	 * Chain for buf IO for large sector size disk drive targets
2177 	 * with checksumming (PM disabled)
2178 	 */
2179 	sd_destroypkt_for_buf,		/* Index: 26 */
2180 	sd_destroypkt_for_buf,		/* Index: 27 */
2181 	sd_destroypkt_for_buf,		/* Index: 28 */
2182 	sd_destroypkt_for_buf,		/* Index: 29 */
2183 	sd_destroypkt_for_buf,		/* Index: 30 */
2184 
2185 	/*
2186 	 * Chain for buf IO for large sector size disk drive targets
2187 	 * with checksumming (PM enabled)
2188 	 */
2189 	sd_destroypkt_for_buf,		/* Index: 31 */
2190 	sd_destroypkt_for_buf,		/* Index: 32 */
2191 	sd_destroypkt_for_buf,		/* Index: 33 */
2192 	sd_destroypkt_for_buf,		/* Index: 34 */
2193 };
2194 
2195 
2196 
2197 /*
2198  * Array to map a layering chain index to the appropriate chain "type".
2199  * The chain type indicates a specific property/usage of the chain.
2200  * The redundant entries are present so that the index used for accessing
2201  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2202  * with this table as well.
2203  */
2204 
2205 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2206 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2207 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2208 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2209 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2210 						/* (for error recovery) */
2211 
2212 static int sd_chain_type_map[] = {
2213 
2214 	/* Chain for buf IO for disk drive targets (PM enabled) */
2215 	SD_CHAIN_BUFIO,			/* Index: 0 */
2216 	SD_CHAIN_BUFIO,			/* Index: 1 */
2217 	SD_CHAIN_BUFIO,			/* Index: 2 */
2218 
2219 	/* Chain for buf IO for disk drive targets (PM disabled) */
2220 	SD_CHAIN_BUFIO,			/* Index: 3 */
2221 	SD_CHAIN_BUFIO,			/* Index: 4 */
2222 
2223 	/*
2224 	 * Chain for buf IO for removable-media or large sector size
2225 	 * disk drive targets (PM enabled)
2226 	 */
2227 	SD_CHAIN_BUFIO,			/* Index: 5 */
2228 	SD_CHAIN_BUFIO,			/* Index: 6 */
2229 	SD_CHAIN_BUFIO,			/* Index: 7 */
2230 	SD_CHAIN_BUFIO,			/* Index: 8 */
2231 
2232 	/*
2233 	 * Chain for buf IO for removable-media or large sector size
2234 	 * disk drive targets (PM disabled)
2235 	 */
2236 	SD_CHAIN_BUFIO,			/* Index: 9 */
2237 	SD_CHAIN_BUFIO,			/* Index: 10 */
2238 	SD_CHAIN_BUFIO,			/* Index: 11 */
2239 
2240 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2241 	SD_CHAIN_BUFIO,			/* Index: 12 */
2242 	SD_CHAIN_BUFIO,			/* Index: 13 */
2243 	SD_CHAIN_BUFIO,			/* Index: 14 */
2244 	SD_CHAIN_BUFIO,			/* Index: 15 */
2245 
2246 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2247 	SD_CHAIN_BUFIO,			/* Index: 16 */
2248 	SD_CHAIN_BUFIO,			/* Index: 17 */
2249 	SD_CHAIN_BUFIO,			/* Index: 18 */
2250 
2251 	/* Chain for USCSI commands (non-checksum targets) */
2252 	SD_CHAIN_USCSI,			/* Index: 19 */
2253 	SD_CHAIN_USCSI,			/* Index: 20 */
2254 
2255 	/* Chain for USCSI commands (checksum targets) */
2256 	SD_CHAIN_USCSI,			/* Index: 21 */
2257 	SD_CHAIN_USCSI,			/* Index: 22 */
2258 	SD_CHAIN_USCSI,			/* Index: 23 */
2259 
2260 	/* Chain for "direct" USCSI commands (all targets) */
2261 	SD_CHAIN_DIRECT,		/* Index: 24 */
2262 
2263 	/* Chain for "direct priority" USCSI commands (all targets) */
2264 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2265 
2266 	/*
2267 	 * Chain for buf IO for large sector size disk drive targets
2268 	 * with checksumming (PM enabled)
2269 	 */
2270 	SD_CHAIN_BUFIO,			/* Index: 26 */
2271 	SD_CHAIN_BUFIO,			/* Index: 27 */
2272 	SD_CHAIN_BUFIO,			/* Index: 28 */
2273 	SD_CHAIN_BUFIO,			/* Index: 29 */
2274 	SD_CHAIN_BUFIO,			/* Index: 30 */
2275 
2276 	/*
2277 	 * Chain for buf IO for large sector size disk drive targets
2278 	 * with checksumming (PM disabled)
2279 	 */
2280 	SD_CHAIN_BUFIO,			/* Index: 31 */
2281 	SD_CHAIN_BUFIO,			/* Index: 32 */
2282 	SD_CHAIN_BUFIO,			/* Index: 33 */
2283 	SD_CHAIN_BUFIO,			/* Index: 34 */
2284 };
2285 
2286 
2287 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2288 #define	SD_IS_BUFIO(xp)			\
2289 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2290 
2291 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2292 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2293 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2294 
2295 
2296 
2297 /*
2298  * Struct, array, and macros to map a specific chain to the appropriate
2299  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2300  *
2301  * The sd_chain_index_map[] array is used at attach time to set the various
2302  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2303  * chain to be used with the instance. This allows different instances to use
2304  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2305  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2306  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2307  * dynamically & without the use of locking; and (2) a layer may update the
2308  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2309  * to allow for deferred processing of an IO within the same chain from a
2310  * different execution context.
2311  */
2312 
2313 struct sd_chain_index {
2314 	int	sci_iostart_index;
2315 	int	sci_iodone_index;
2316 };
2317 
2318 static struct sd_chain_index	sd_chain_index_map[] = {
2319 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2320 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2321 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2322 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2323 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2324 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2325 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2326 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2327 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2328 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2329 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2330 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2331 
2332 };
2333 
2334 
2335 /*
2336  * The following are indexes into the sd_chain_index_map[] array.
2337  */
2338 
2339 /* un->un_buf_chain_type must be set to one of these */
2340 #define	SD_CHAIN_INFO_DISK		0
2341 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2342 #define	SD_CHAIN_INFO_RMMEDIA		2
2343 #define	SD_CHAIN_INFO_MSS_DISK		2
2344 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2345 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2346 #define	SD_CHAIN_INFO_CHKSUM		4
2347 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2348 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2349 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2350 
2351 /* un->un_uscsi_chain_type must be set to one of these */
2352 #define	SD_CHAIN_INFO_USCSI_CMD		6
2353 /* USCSI with PM disabled is the same as DIRECT */
2354 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2355 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2356 
2357 /* un->un_direct_chain_type must be set to one of these */
2358 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2359 
2360 /* un->un_priority_chain_type must be set to one of these */
2361 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2362 
2363 /* size for devid inquiries */
2364 #define	MAX_INQUIRY_SIZE		0xF0
2365 
2366 /*
2367  * Macros used by functions to pass a given buf(9S) struct along to the
2368  * next function in the layering chain for further processing.
2369  *
2370  * In the following macros, passing more than three arguments to the called
2371  * routines causes the optimizer for the SPARC compiler to stop doing tail
2372  * call elimination which results in significant performance degradation.
2373  */
2374 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2375 	((*(sd_iostart_chain[index]))(index, un, bp))
2376 
2377 #define	SD_BEGIN_IODONE(index, un, bp)	\
2378 	((*(sd_iodone_chain[index]))(index, un, bp))
2379 
2380 #define	SD_NEXT_IOSTART(index, un, bp)				\
2381 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2382 
2383 #define	SD_NEXT_IODONE(index, un, bp)				\
2384 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2385 
2386 /*
2387  *    Function: _init
2388  *
2389  * Description: This is the driver _init(9E) entry point.
2390  *
2391  * Return Code: Returns the value from mod_install(9F) or
2392  *		ddi_soft_state_init(9F) as appropriate.
2393  *
2394  *     Context: Called when driver module loaded.
2395  */
2396 
2397 int
2398 _init(void)
2399 {
2400 	int	err;
2401 
2402 	/* establish driver name from module name */
2403 	sd_label = (char *)mod_modname(&modlinkage);
2404 
2405 #ifndef XPV_HVM_DRIVER
2406 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2407 	    SD_MAXUNIT);
2408 	if (err != 0) {
2409 		return (err);
2410 	}
2411 
2412 #else /* XPV_HVM_DRIVER */
2413 	/* Remove the leading "hvm_" from the module name */
2414 	ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
2415 	sd_label += strlen("hvm_");
2416 
2417 #endif /* XPV_HVM_DRIVER */
2418 
2419 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2420 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2421 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2422 
2423 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2424 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2425 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2426 
2427 	/*
2428 	 * it's ok to init here even for fibre device
2429 	 */
2430 	sd_scsi_probe_cache_init();
2431 
2432 	sd_scsi_target_lun_init();
2433 
2434 	/*
2435 	 * Creating taskq before mod_install ensures that all callers (threads)
2436 	 * that enter the module after a successful mod_install encounter
2437 	 * a valid taskq.
2438 	 */
2439 	sd_taskq_create();
2440 
2441 	err = mod_install(&modlinkage);
2442 	if (err != 0) {
2443 		/* delete taskq if install fails */
2444 		sd_taskq_delete();
2445 
2446 		mutex_destroy(&sd_detach_mutex);
2447 		mutex_destroy(&sd_log_mutex);
2448 		mutex_destroy(&sd_label_mutex);
2449 
2450 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2451 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2452 		cv_destroy(&sd_tr.srq_inprocess_cv);
2453 
2454 		sd_scsi_probe_cache_fini();
2455 
2456 		sd_scsi_target_lun_fini();
2457 
2458 #ifndef XPV_HVM_DRIVER
2459 		ddi_soft_state_fini(&sd_state);
2460 #endif /* !XPV_HVM_DRIVER */
2461 		return (err);
2462 	}
2463 
2464 	return (err);
2465 }
2466 
2467 
2468 /*
2469  *    Function: _fini
2470  *
2471  * Description: This is the driver _fini(9E) entry point.
2472  *
2473  * Return Code: Returns the value from mod_remove(9F)
2474  *
2475  *     Context: Called when driver module is unloaded.
2476  */
2477 
2478 int
2479 _fini(void)
2480 {
2481 	int err;
2482 
2483 	if ((err = mod_remove(&modlinkage)) != 0) {
2484 		return (err);
2485 	}
2486 
2487 	sd_taskq_delete();
2488 
2489 	mutex_destroy(&sd_detach_mutex);
2490 	mutex_destroy(&sd_log_mutex);
2491 	mutex_destroy(&sd_label_mutex);
2492 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2493 
2494 	sd_scsi_probe_cache_fini();
2495 
2496 	sd_scsi_target_lun_fini();
2497 
2498 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2499 	cv_destroy(&sd_tr.srq_inprocess_cv);
2500 
2501 #ifndef XPV_HVM_DRIVER
2502 	ddi_soft_state_fini(&sd_state);
2503 #endif /* !XPV_HVM_DRIVER */
2504 
2505 	return (err);
2506 }
2507 
2508 
2509 /*
2510  *    Function: _info
2511  *
2512  * Description: This is the driver _info(9E) entry point.
2513  *
2514  *   Arguments: modinfop - pointer to the driver modinfo structure
2515  *
2516  * Return Code: Returns the value from mod_info(9F).
2517  *
2518  *     Context: Kernel thread context
2519  */
2520 
2521 int
2522 _info(struct modinfo *modinfop)
2523 {
2524 	return (mod_info(&modlinkage, modinfop));
2525 }
2526 
2527 
2528 /*
2529  * The following routines implement the driver message logging facility.
2530  * They provide component- and level- based debug output filtering.
2531  * Output may also be restricted to messages for a single instance by
2532  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2533  * to NULL, then messages for all instances are printed.
2534  *
2535  * These routines have been cloned from each other due to the language
2536  * constraints of macros and variable argument list processing.
2537  */
2538 
2539 
2540 /*
2541  *    Function: sd_log_err
2542  *
2543  * Description: This routine is called by the SD_ERROR macro for debug
2544  *		logging of error conditions.
2545  *
2546  *   Arguments: comp - driver component being logged
2547  *		dev  - pointer to driver info structure
2548  *		fmt  - error string and format to be logged
2549  */
2550 
2551 static void
2552 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2553 {
2554 	va_list		ap;
2555 	dev_info_t	*dev;
2556 
2557 	ASSERT(un != NULL);
2558 	dev = SD_DEVINFO(un);
2559 	ASSERT(dev != NULL);
2560 
2561 	/*
2562 	 * Filter messages based on the global component and level masks.
2563 	 * Also print if un matches the value of sd_debug_un, or if
2564 	 * sd_debug_un is set to NULL.
2565 	 */
2566 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2567 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2568 		mutex_enter(&sd_log_mutex);
2569 		va_start(ap, fmt);
2570 		(void) vsprintf(sd_log_buf, fmt, ap);
2571 		va_end(ap);
2572 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2573 		mutex_exit(&sd_log_mutex);
2574 	}
2575 #ifdef SD_FAULT_INJECTION
2576 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2577 	if (un->sd_injection_mask & comp) {
2578 		mutex_enter(&sd_log_mutex);
2579 		va_start(ap, fmt);
2580 		(void) vsprintf(sd_log_buf, fmt, ap);
2581 		va_end(ap);
2582 		sd_injection_log(sd_log_buf, un);
2583 		mutex_exit(&sd_log_mutex);
2584 	}
2585 #endif
2586 }
2587 
2588 
2589 /*
2590  *    Function: sd_log_info
2591  *
2592  * Description: This routine is called by the SD_INFO macro for debug
2593  *		logging of general purpose informational conditions.
2594  *
2595  *   Arguments: comp - driver component being logged
2596  *		dev  - pointer to driver info structure
2597  *		fmt  - info string and format to be logged
2598  */
2599 
2600 static void
2601 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2602 {
2603 	va_list		ap;
2604 	dev_info_t	*dev;
2605 
2606 	ASSERT(un != NULL);
2607 	dev = SD_DEVINFO(un);
2608 	ASSERT(dev != NULL);
2609 
2610 	/*
2611 	 * Filter messages based on the global component and level masks.
2612 	 * Also print if un matches the value of sd_debug_un, or if
2613 	 * sd_debug_un is set to NULL.
2614 	 */
2615 	if ((sd_component_mask & component) &&
2616 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2617 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2618 		mutex_enter(&sd_log_mutex);
2619 		va_start(ap, fmt);
2620 		(void) vsprintf(sd_log_buf, fmt, ap);
2621 		va_end(ap);
2622 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2623 		mutex_exit(&sd_log_mutex);
2624 	}
2625 #ifdef SD_FAULT_INJECTION
2626 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2627 	if (un->sd_injection_mask & component) {
2628 		mutex_enter(&sd_log_mutex);
2629 		va_start(ap, fmt);
2630 		(void) vsprintf(sd_log_buf, fmt, ap);
2631 		va_end(ap);
2632 		sd_injection_log(sd_log_buf, un);
2633 		mutex_exit(&sd_log_mutex);
2634 	}
2635 #endif
2636 }
2637 
2638 
2639 /*
2640  *    Function: sd_log_trace
2641  *
2642  * Description: This routine is called by the SD_TRACE macro for debug
2643  *		logging of trace conditions (i.e. function entry/exit).
2644  *
2645  *   Arguments: comp - driver component being logged
2646  *		dev  - pointer to driver info structure
2647  *		fmt  - trace string and format to be logged
2648  */
2649 
2650 static void
2651 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2652 {
2653 	va_list		ap;
2654 	dev_info_t	*dev;
2655 
2656 	ASSERT(un != NULL);
2657 	dev = SD_DEVINFO(un);
2658 	ASSERT(dev != NULL);
2659 
2660 	/*
2661 	 * Filter messages based on the global component and level masks.
2662 	 * Also print if un matches the value of sd_debug_un, or if
2663 	 * sd_debug_un is set to NULL.
2664 	 */
2665 	if ((sd_component_mask & component) &&
2666 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2667 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2668 		mutex_enter(&sd_log_mutex);
2669 		va_start(ap, fmt);
2670 		(void) vsprintf(sd_log_buf, fmt, ap);
2671 		va_end(ap);
2672 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2673 		mutex_exit(&sd_log_mutex);
2674 	}
2675 #ifdef SD_FAULT_INJECTION
2676 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2677 	if (un->sd_injection_mask & component) {
2678 		mutex_enter(&sd_log_mutex);
2679 		va_start(ap, fmt);
2680 		(void) vsprintf(sd_log_buf, fmt, ap);
2681 		va_end(ap);
2682 		sd_injection_log(sd_log_buf, un);
2683 		mutex_exit(&sd_log_mutex);
2684 	}
2685 #endif
2686 }
2687 
2688 
2689 /*
2690  *    Function: sdprobe
2691  *
2692  * Description: This is the driver probe(9e) entry point function.
2693  *
2694  *   Arguments: devi - opaque device info handle
2695  *
2696  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2697  *              DDI_PROBE_FAILURE: If the probe failed.
2698  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2699  *				   but may be present in the future.
2700  */
2701 
2702 static int
2703 sdprobe(dev_info_t *devi)
2704 {
2705 	struct scsi_device	*devp;
2706 	int			rval;
2707 #ifndef XPV_HVM_DRIVER
2708 	int			instance = ddi_get_instance(devi);
2709 #endif /* !XPV_HVM_DRIVER */
2710 
2711 	/*
2712 	 * if it wasn't for pln, sdprobe could actually be nulldev
2713 	 * in the "__fibre" case.
2714 	 */
2715 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2716 		return (DDI_PROBE_DONTCARE);
2717 	}
2718 
2719 	devp = ddi_get_driver_private(devi);
2720 
2721 	if (devp == NULL) {
2722 		/* Ooops... nexus driver is mis-configured... */
2723 		return (DDI_PROBE_FAILURE);
2724 	}
2725 
2726 #ifndef XPV_HVM_DRIVER
2727 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2728 		return (DDI_PROBE_PARTIAL);
2729 	}
2730 #endif /* !XPV_HVM_DRIVER */
2731 
2732 	/*
2733 	 * Call the SCSA utility probe routine to see if we actually
2734 	 * have a target at this SCSI nexus.
2735 	 */
2736 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2737 	case SCSIPROBE_EXISTS:
2738 		switch (devp->sd_inq->inq_dtype) {
2739 		case DTYPE_DIRECT:
2740 			rval = DDI_PROBE_SUCCESS;
2741 			break;
2742 		case DTYPE_RODIRECT:
2743 			/* CDs etc. Can be removable media */
2744 			rval = DDI_PROBE_SUCCESS;
2745 			break;
2746 		case DTYPE_OPTICAL:
2747 			/*
2748 			 * Rewritable optical driver HP115AA
2749 			 * Can also be removable media
2750 			 */
2751 
2752 			/*
2753 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2754 			 * pre solaris 9 sparc sd behavior is required
2755 			 *
2756 			 * If first time through and sd_dtype_optical_bind
2757 			 * has not been set in /etc/system check properties
2758 			 */
2759 
2760 			if (sd_dtype_optical_bind  < 0) {
2761 				sd_dtype_optical_bind = ddi_prop_get_int
2762 				    (DDI_DEV_T_ANY, devi, 0,
2763 				    "optical-device-bind", 1);
2764 			}
2765 
2766 			if (sd_dtype_optical_bind == 0) {
2767 				rval = DDI_PROBE_FAILURE;
2768 			} else {
2769 				rval = DDI_PROBE_SUCCESS;
2770 			}
2771 			break;
2772 
2773 		case DTYPE_NOTPRESENT:
2774 		default:
2775 			rval = DDI_PROBE_FAILURE;
2776 			break;
2777 		}
2778 		break;
2779 	default:
2780 		rval = DDI_PROBE_PARTIAL;
2781 		break;
2782 	}
2783 
2784 	/*
2785 	 * This routine checks for resource allocation prior to freeing,
2786 	 * so it will take care of the "smart probing" case where a
2787 	 * scsi_probe() may or may not have been issued and will *not*
2788 	 * free previously-freed resources.
2789 	 */
2790 	scsi_unprobe(devp);
2791 	return (rval);
2792 }
2793 
2794 
2795 /*
2796  *    Function: sdinfo
2797  *
2798  * Description: This is the driver getinfo(9e) entry point function.
2799  * 		Given the device number, return the devinfo pointer from
2800  *		the scsi_device structure or the instance number
2801  *		associated with the dev_t.
2802  *
2803  *   Arguments: dip     - pointer to device info structure
2804  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2805  *			  DDI_INFO_DEVT2INSTANCE)
2806  *		arg     - driver dev_t
2807  *		resultp - user buffer for request response
2808  *
2809  * Return Code: DDI_SUCCESS
2810  *              DDI_FAILURE
2811  */
2812 /* ARGSUSED */
2813 static int
2814 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2815 {
2816 	struct sd_lun	*un;
2817 	dev_t		dev;
2818 	int		instance;
2819 	int		error;
2820 
2821 	switch (infocmd) {
2822 	case DDI_INFO_DEVT2DEVINFO:
2823 		dev = (dev_t)arg;
2824 		instance = SDUNIT(dev);
2825 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2826 			return (DDI_FAILURE);
2827 		}
2828 		*result = (void *) SD_DEVINFO(un);
2829 		error = DDI_SUCCESS;
2830 		break;
2831 	case DDI_INFO_DEVT2INSTANCE:
2832 		dev = (dev_t)arg;
2833 		instance = SDUNIT(dev);
2834 		*result = (void *)(uintptr_t)instance;
2835 		error = DDI_SUCCESS;
2836 		break;
2837 	default:
2838 		error = DDI_FAILURE;
2839 	}
2840 	return (error);
2841 }
2842 
2843 /*
2844  *    Function: sd_prop_op
2845  *
2846  * Description: This is the driver prop_op(9e) entry point function.
2847  *		Return the number of blocks for the partition in question
2848  *		or forward the request to the property facilities.
2849  *
2850  *   Arguments: dev       - device number
2851  *		dip       - pointer to device info structure
2852  *		prop_op   - property operator
2853  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2854  *		name      - pointer to property name
2855  *		valuep    - pointer or address of the user buffer
2856  *		lengthp   - property length
2857  *
2858  * Return Code: DDI_PROP_SUCCESS
2859  *              DDI_PROP_NOT_FOUND
2860  *              DDI_PROP_UNDEFINED
2861  *              DDI_PROP_NO_MEMORY
2862  *              DDI_PROP_BUF_TOO_SMALL
2863  */
2864 
2865 static int
2866 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2867 	char *name, caddr_t valuep, int *lengthp)
2868 {
2869 	struct sd_lun	*un;
2870 
2871 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2872 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2873 		    name, valuep, lengthp));
2874 
2875 	return (cmlb_prop_op(un->un_cmlbhandle,
2876 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2877 	    SDPART(dev), (void *)SD_PATH_DIRECT));
2878 }
2879 
2880 /*
2881  * The following functions are for smart probing:
2882  * sd_scsi_probe_cache_init()
2883  * sd_scsi_probe_cache_fini()
2884  * sd_scsi_clear_probe_cache()
2885  * sd_scsi_probe_with_cache()
2886  */
2887 
2888 /*
2889  *    Function: sd_scsi_probe_cache_init
2890  *
2891  * Description: Initializes the probe response cache mutex and head pointer.
2892  *
2893  *     Context: Kernel thread context
2894  */
2895 
2896 static void
2897 sd_scsi_probe_cache_init(void)
2898 {
2899 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2900 	sd_scsi_probe_cache_head = NULL;
2901 }
2902 
2903 
2904 /*
2905  *    Function: sd_scsi_probe_cache_fini
2906  *
2907  * Description: Frees all resources associated with the probe response cache.
2908  *
2909  *     Context: Kernel thread context
2910  */
2911 
2912 static void
2913 sd_scsi_probe_cache_fini(void)
2914 {
2915 	struct sd_scsi_probe_cache *cp;
2916 	struct sd_scsi_probe_cache *ncp;
2917 
2918 	/* Clean up our smart probing linked list */
2919 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2920 		ncp = cp->next;
2921 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2922 	}
2923 	sd_scsi_probe_cache_head = NULL;
2924 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2925 }
2926 
2927 
2928 /*
2929  *    Function: sd_scsi_clear_probe_cache
2930  *
2931  * Description: This routine clears the probe response cache. This is
2932  *		done when open() returns ENXIO so that when deferred
2933  *		attach is attempted (possibly after a device has been
2934  *		turned on) we will retry the probe. Since we don't know
2935  *		which target we failed to open, we just clear the
2936  *		entire cache.
2937  *
2938  *     Context: Kernel thread context
2939  */
2940 
2941 static void
2942 sd_scsi_clear_probe_cache(void)
2943 {
2944 	struct sd_scsi_probe_cache	*cp;
2945 	int				i;
2946 
2947 	mutex_enter(&sd_scsi_probe_cache_mutex);
2948 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2949 		/*
2950 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2951 		 * force probing to be performed the next time
2952 		 * sd_scsi_probe_with_cache is called.
2953 		 */
2954 		for (i = 0; i < NTARGETS_WIDE; i++) {
2955 			cp->cache[i] = SCSIPROBE_EXISTS;
2956 		}
2957 	}
2958 	mutex_exit(&sd_scsi_probe_cache_mutex);
2959 }
2960 
2961 
2962 /*
2963  *    Function: sd_scsi_probe_with_cache
2964  *
2965  * Description: This routine implements support for a scsi device probe
2966  *		with cache. The driver maintains a cache of the target
2967  *		responses to scsi probes. If we get no response from a
2968  *		target during a probe inquiry, we remember that, and we
2969  *		avoid additional calls to scsi_probe on non-zero LUNs
2970  *		on the same target until the cache is cleared. By doing
2971  *		so we avoid the 1/4 sec selection timeout for nonzero
2972  *		LUNs. lun0 of a target is always probed.
2973  *
2974  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2975  *              waitfunc - indicates what the allocator routines should
2976  *			   do when resources are not available. This value
2977  *			   is passed on to scsi_probe() when that routine
2978  *			   is called.
2979  *
2980  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2981  *		otherwise the value returned by scsi_probe(9F).
2982  *
2983  *     Context: Kernel thread context
2984  */
2985 
2986 static int
2987 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2988 {
2989 	struct sd_scsi_probe_cache	*cp;
2990 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2991 	int		lun, tgt;
2992 
2993 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2994 	    SCSI_ADDR_PROP_LUN, 0);
2995 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2996 	    SCSI_ADDR_PROP_TARGET, -1);
2997 
2998 	/* Make sure caching enabled and target in range */
2999 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3000 		/* do it the old way (no cache) */
3001 		return (scsi_probe(devp, waitfn));
3002 	}
3003 
3004 	mutex_enter(&sd_scsi_probe_cache_mutex);
3005 
3006 	/* Find the cache for this scsi bus instance */
3007 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3008 		if (cp->pdip == pdip) {
3009 			break;
3010 		}
3011 	}
3012 
3013 	/* If we can't find a cache for this pdip, create one */
3014 	if (cp == NULL) {
3015 		int i;
3016 
3017 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3018 		    KM_SLEEP);
3019 		cp->pdip = pdip;
3020 		cp->next = sd_scsi_probe_cache_head;
3021 		sd_scsi_probe_cache_head = cp;
3022 		for (i = 0; i < NTARGETS_WIDE; i++) {
3023 			cp->cache[i] = SCSIPROBE_EXISTS;
3024 		}
3025 	}
3026 
3027 	mutex_exit(&sd_scsi_probe_cache_mutex);
3028 
3029 	/* Recompute the cache for this target if LUN zero */
3030 	if (lun == 0) {
3031 		cp->cache[tgt] = SCSIPROBE_EXISTS;
3032 	}
3033 
3034 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3035 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3036 		return (SCSIPROBE_NORESP);
3037 	}
3038 
3039 	/* Do the actual probe; save & return the result */
3040 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3041 }
3042 
3043 
3044 /*
3045  *    Function: sd_scsi_target_lun_init
3046  *
3047  * Description: Initializes the attached lun chain mutex and head pointer.
3048  *
3049  *     Context: Kernel thread context
3050  */
3051 
3052 static void
3053 sd_scsi_target_lun_init(void)
3054 {
3055 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3056 	sd_scsi_target_lun_head = NULL;
3057 }
3058 
3059 
3060 /*
3061  *    Function: sd_scsi_target_lun_fini
3062  *
3063  * Description: Frees all resources associated with the attached lun
3064  *              chain
3065  *
3066  *     Context: Kernel thread context
3067  */
3068 
3069 static void
3070 sd_scsi_target_lun_fini(void)
3071 {
3072 	struct sd_scsi_hba_tgt_lun	*cp;
3073 	struct sd_scsi_hba_tgt_lun	*ncp;
3074 
3075 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3076 		ncp = cp->next;
3077 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3078 	}
3079 	sd_scsi_target_lun_head = NULL;
3080 	mutex_destroy(&sd_scsi_target_lun_mutex);
3081 }
3082 
3083 
3084 /*
3085  *    Function: sd_scsi_get_target_lun_count
3086  *
3087  * Description: This routine will check in the attached lun chain to see
3088  * 		how many luns are attached on the required SCSI controller
3089  * 		and target. Currently, some capabilities like tagged queue
3090  *		are supported per target based by HBA. So all luns in a
3091  *		target have the same capabilities. Based on this assumption,
3092  * 		sd should only set these capabilities once per target. This
3093  *		function is called when sd needs to decide how many luns
3094  *		already attached on a target.
3095  *
3096  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3097  *			  controller device.
3098  *              target	- The target ID on the controller's SCSI bus.
3099  *
3100  * Return Code: The number of luns attached on the required target and
3101  *		controller.
3102  *		-1 if target ID is not in parallel SCSI scope or the given
3103  * 		dip is not in the chain.
3104  *
3105  *     Context: Kernel thread context
3106  */
3107 
3108 static int
3109 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3110 {
3111 	struct sd_scsi_hba_tgt_lun	*cp;
3112 
3113 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3114 		return (-1);
3115 	}
3116 
3117 	mutex_enter(&sd_scsi_target_lun_mutex);
3118 
3119 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3120 		if (cp->pdip == dip) {
3121 			break;
3122 		}
3123 	}
3124 
3125 	mutex_exit(&sd_scsi_target_lun_mutex);
3126 
3127 	if (cp == NULL) {
3128 		return (-1);
3129 	}
3130 
3131 	return (cp->nlun[target]);
3132 }
3133 
3134 
3135 /*
3136  *    Function: sd_scsi_update_lun_on_target
3137  *
3138  * Description: This routine is used to update the attached lun chain when a
3139  *		lun is attached or detached on a target.
3140  *
3141  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3142  *                        controller device.
3143  *              target  - The target ID on the controller's SCSI bus.
3144  *		flag	- Indicate the lun is attached or detached.
3145  *
3146  *     Context: Kernel thread context
3147  */
3148 
3149 static void
3150 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3151 {
3152 	struct sd_scsi_hba_tgt_lun	*cp;
3153 
3154 	mutex_enter(&sd_scsi_target_lun_mutex);
3155 
3156 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3157 		if (cp->pdip == dip) {
3158 			break;
3159 		}
3160 	}
3161 
3162 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3163 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3164 		    KM_SLEEP);
3165 		cp->pdip = dip;
3166 		cp->next = sd_scsi_target_lun_head;
3167 		sd_scsi_target_lun_head = cp;
3168 	}
3169 
3170 	mutex_exit(&sd_scsi_target_lun_mutex);
3171 
3172 	if (cp != NULL) {
3173 		if (flag == SD_SCSI_LUN_ATTACH) {
3174 			cp->nlun[target] ++;
3175 		} else {
3176 			cp->nlun[target] --;
3177 		}
3178 	}
3179 }
3180 
3181 
3182 /*
3183  *    Function: sd_spin_up_unit
3184  *
3185  * Description: Issues the following commands to spin-up the device:
3186  *		START STOP UNIT, and INQUIRY.
3187  *
3188  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3189  *                      structure for this target.
3190  *
3191  * Return Code: 0 - success
3192  *		EIO - failure
3193  *		EACCES - reservation conflict
3194  *
3195  *     Context: Kernel thread context
3196  */
3197 
3198 static int
3199 sd_spin_up_unit(sd_ssc_t *ssc)
3200 {
3201 	size_t	resid		= 0;
3202 	int	has_conflict	= FALSE;
3203 	uchar_t *bufaddr;
3204 	int 	status;
3205 	struct sd_lun	*un;
3206 
3207 	ASSERT(ssc != NULL);
3208 	un = ssc->ssc_un;
3209 	ASSERT(un != NULL);
3210 
3211 	/*
3212 	 * Send a throwaway START UNIT command.
3213 	 *
3214 	 * If we fail on this, we don't care presently what precisely
3215 	 * is wrong.  EMC's arrays will also fail this with a check
3216 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3217 	 * we don't want to fail the attach because it may become
3218 	 * "active" later.
3219 	 * We don't know if power condition is supported or not at
3220 	 * this stage, use START STOP bit.
3221 	 */
3222 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3223 	    SD_TARGET_START, SD_PATH_DIRECT);
3224 
3225 	if (status != 0) {
3226 		if (status == EACCES)
3227 			has_conflict = TRUE;
3228 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3229 	}
3230 
3231 	/*
3232 	 * Send another INQUIRY command to the target. This is necessary for
3233 	 * non-removable media direct access devices because their INQUIRY data
3234 	 * may not be fully qualified until they are spun up (perhaps via the
3235 	 * START command above).  Note: This seems to be needed for some
3236 	 * legacy devices only.) The INQUIRY command should succeed even if a
3237 	 * Reservation Conflict is present.
3238 	 */
3239 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3240 
3241 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3242 	    != 0) {
3243 		kmem_free(bufaddr, SUN_INQSIZE);
3244 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3245 		return (EIO);
3246 	}
3247 
3248 	/*
3249 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3250 	 * Note that this routine does not return a failure here even if the
3251 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3252 	 */
3253 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3254 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3255 	}
3256 
3257 	kmem_free(bufaddr, SUN_INQSIZE);
3258 
3259 	/* If we hit a reservation conflict above, tell the caller. */
3260 	if (has_conflict == TRUE) {
3261 		return (EACCES);
3262 	}
3263 
3264 	return (0);
3265 }
3266 
3267 #ifdef _LP64
3268 /*
3269  *    Function: sd_enable_descr_sense
3270  *
3271  * Description: This routine attempts to select descriptor sense format
3272  *		using the Control mode page.  Devices that support 64 bit
3273  *		LBAs (for >2TB luns) should also implement descriptor
3274  *		sense data so we will call this function whenever we see
3275  *		a lun larger than 2TB.  If for some reason the device
3276  *		supports 64 bit LBAs but doesn't support descriptor sense
3277  *		presumably the mode select will fail.  Everything will
3278  *		continue to work normally except that we will not get
3279  *		complete sense data for commands that fail with an LBA
3280  *		larger than 32 bits.
3281  *
3282  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3283  *                      structure for this target.
3284  *
3285  *     Context: Kernel thread context only
3286  */
3287 
3288 static void
3289 sd_enable_descr_sense(sd_ssc_t *ssc)
3290 {
3291 	uchar_t			*header;
3292 	struct mode_control_scsi3 *ctrl_bufp;
3293 	size_t			buflen;
3294 	size_t			bd_len;
3295 	int			status;
3296 	struct sd_lun		*un;
3297 
3298 	ASSERT(ssc != NULL);
3299 	un = ssc->ssc_un;
3300 	ASSERT(un != NULL);
3301 
3302 	/*
3303 	 * Read MODE SENSE page 0xA, Control Mode Page
3304 	 */
3305 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3306 	    sizeof (struct mode_control_scsi3);
3307 	header = kmem_zalloc(buflen, KM_SLEEP);
3308 
3309 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3310 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3311 
3312 	if (status != 0) {
3313 		SD_ERROR(SD_LOG_COMMON, un,
3314 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3315 		goto eds_exit;
3316 	}
3317 
3318 	/*
3319 	 * Determine size of Block Descriptors in order to locate
3320 	 * the mode page data. ATAPI devices return 0, SCSI devices
3321 	 * should return MODE_BLK_DESC_LENGTH.
3322 	 */
3323 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3324 
3325 	/* Clear the mode data length field for MODE SELECT */
3326 	((struct mode_header *)header)->length = 0;
3327 
3328 	ctrl_bufp = (struct mode_control_scsi3 *)
3329 	    (header + MODE_HEADER_LENGTH + bd_len);
3330 
3331 	/*
3332 	 * If the page length is smaller than the expected value,
3333 	 * the target device doesn't support D_SENSE. Bail out here.
3334 	 */
3335 	if (ctrl_bufp->mode_page.length <
3336 	    sizeof (struct mode_control_scsi3) - 2) {
3337 		SD_ERROR(SD_LOG_COMMON, un,
3338 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3339 		goto eds_exit;
3340 	}
3341 
3342 	/*
3343 	 * Clear PS bit for MODE SELECT
3344 	 */
3345 	ctrl_bufp->mode_page.ps = 0;
3346 
3347 	/*
3348 	 * Set D_SENSE to enable descriptor sense format.
3349 	 */
3350 	ctrl_bufp->d_sense = 1;
3351 
3352 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3353 
3354 	/*
3355 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3356 	 */
3357 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3358 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3359 
3360 	if (status != 0) {
3361 		SD_INFO(SD_LOG_COMMON, un,
3362 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3363 	} else {
3364 		kmem_free(header, buflen);
3365 		return;
3366 	}
3367 
3368 eds_exit:
3369 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3370 	kmem_free(header, buflen);
3371 }
3372 
3373 /*
3374  *    Function: sd_reenable_dsense_task
3375  *
3376  * Description: Re-enable descriptor sense after device or bus reset
3377  *
3378  *     Context: Executes in a taskq() thread context
3379  */
3380 static void
3381 sd_reenable_dsense_task(void *arg)
3382 {
3383 	struct	sd_lun	*un = arg;
3384 	sd_ssc_t	*ssc;
3385 
3386 	ASSERT(un != NULL);
3387 
3388 	ssc = sd_ssc_init(un);
3389 	sd_enable_descr_sense(ssc);
3390 	sd_ssc_fini(ssc);
3391 }
3392 #endif /* _LP64 */
3393 
3394 /*
3395  *    Function: sd_set_mmc_caps
3396  *
3397  * Description: This routine determines if the device is MMC compliant and if
3398  *		the device supports CDDA via a mode sense of the CDVD
3399  *		capabilities mode page. Also checks if the device is a
3400  *		dvdram writable device.
3401  *
3402  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3403  *                      structure for this target.
3404  *
3405  *     Context: Kernel thread context only
3406  */
3407 
3408 static void
3409 sd_set_mmc_caps(sd_ssc_t *ssc)
3410 {
3411 	struct mode_header_grp2		*sense_mhp;
3412 	uchar_t				*sense_page;
3413 	caddr_t				buf;
3414 	int				bd_len;
3415 	int				status;
3416 	struct uscsi_cmd		com;
3417 	int				rtn;
3418 	uchar_t				*out_data_rw, *out_data_hd;
3419 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3420 	uchar_t				*out_data_gesn;
3421 	int				gesn_len;
3422 	struct sd_lun			*un;
3423 
3424 	ASSERT(ssc != NULL);
3425 	un = ssc->ssc_un;
3426 	ASSERT(un != NULL);
3427 
3428 	/*
3429 	 * The flags which will be set in this function are - mmc compliant,
3430 	 * dvdram writable device, cdda support. Initialize them to FALSE
3431 	 * and if a capability is detected - it will be set to TRUE.
3432 	 */
3433 	un->un_f_mmc_cap = FALSE;
3434 	un->un_f_dvdram_writable_device = FALSE;
3435 	un->un_f_cfg_cdda = FALSE;
3436 
3437 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3438 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3439 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3440 
3441 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3442 
3443 	if (status != 0) {
3444 		/* command failed; just return */
3445 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3446 		return;
3447 	}
3448 	/*
3449 	 * If the mode sense request for the CDROM CAPABILITIES
3450 	 * page (0x2A) succeeds the device is assumed to be MMC.
3451 	 */
3452 	un->un_f_mmc_cap = TRUE;
3453 
3454 	/* See if GET STATUS EVENT NOTIFICATION is supported */
3455 	if (un->un_f_mmc_gesn_polling) {
3456 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3457 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3458 
3459 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3460 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3461 
3462 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3463 
3464 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3465 			un->un_f_mmc_gesn_polling = FALSE;
3466 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3467 			    "sd_set_mmc_caps: gesn not supported "
3468 			    "%d %x %x %x %x\n", rtn,
3469 			    out_data_gesn[0], out_data_gesn[1],
3470 			    out_data_gesn[2], out_data_gesn[3]);
3471 		}
3472 
3473 		kmem_free(out_data_gesn, gesn_len);
3474 	}
3475 
3476 	/* Get to the page data */
3477 	sense_mhp = (struct mode_header_grp2 *)buf;
3478 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3479 	    sense_mhp->bdesc_length_lo;
3480 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3481 		/*
3482 		 * We did not get back the expected block descriptor
3483 		 * length so we cannot determine if the device supports
3484 		 * CDDA. However, we still indicate the device is MMC
3485 		 * according to the successful response to the page
3486 		 * 0x2A mode sense request.
3487 		 */
3488 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3489 		    "sd_set_mmc_caps: Mode Sense returned "
3490 		    "invalid block descriptor length\n");
3491 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3492 		return;
3493 	}
3494 
3495 	/* See if read CDDA is supported */
3496 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3497 	    bd_len);
3498 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3499 
3500 	/* See if writing DVD RAM is supported. */
3501 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3502 	if (un->un_f_dvdram_writable_device == TRUE) {
3503 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3504 		return;
3505 	}
3506 
3507 	/*
3508 	 * If the device presents DVD or CD capabilities in the mode
3509 	 * page, we can return here since a RRD will not have
3510 	 * these capabilities.
3511 	 */
3512 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3513 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3514 		return;
3515 	}
3516 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3517 
3518 	/*
3519 	 * If un->un_f_dvdram_writable_device is still FALSE,
3520 	 * check for a Removable Rigid Disk (RRD).  A RRD
3521 	 * device is identified by the features RANDOM_WRITABLE and
3522 	 * HARDWARE_DEFECT_MANAGEMENT.
3523 	 */
3524 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3525 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3526 
3527 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3528 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3529 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3530 
3531 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3532 
3533 	if (rtn != 0) {
3534 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3535 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3536 		return;
3537 	}
3538 
3539 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3540 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3541 
3542 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3543 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3544 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3545 
3546 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3547 
3548 	if (rtn == 0) {
3549 		/*
3550 		 * We have good information, check for random writable
3551 		 * and hardware defect features.
3552 		 */
3553 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3554 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3555 			un->un_f_dvdram_writable_device = TRUE;
3556 		}
3557 	}
3558 
3559 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3560 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3561 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3562 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3563 }
3564 
3565 /*
3566  *    Function: sd_check_for_writable_cd
3567  *
3568  * Description: This routine determines if the media in the device is
3569  *		writable or not. It uses the get configuration command (0x46)
3570  *		to determine if the media is writable
3571  *
3572  *   Arguments: un - driver soft state (unit) structure
3573  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3574  *                           chain and the normal command waitq, or
3575  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3576  *                           "direct" chain and bypass the normal command
3577  *                           waitq.
3578  *
3579  *     Context: Never called at interrupt context.
3580  */
3581 
3582 static void
3583 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3584 {
3585 	struct uscsi_cmd		com;
3586 	uchar_t				*out_data;
3587 	uchar_t				*rqbuf;
3588 	int				rtn;
3589 	uchar_t				*out_data_rw, *out_data_hd;
3590 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3591 	struct mode_header_grp2		*sense_mhp;
3592 	uchar_t				*sense_page;
3593 	caddr_t				buf;
3594 	int				bd_len;
3595 	int				status;
3596 	struct sd_lun			*un;
3597 
3598 	ASSERT(ssc != NULL);
3599 	un = ssc->ssc_un;
3600 	ASSERT(un != NULL);
3601 	ASSERT(mutex_owned(SD_MUTEX(un)));
3602 
3603 	/*
3604 	 * Initialize the writable media to false, if configuration info.
3605 	 * tells us otherwise then only we will set it.
3606 	 */
3607 	un->un_f_mmc_writable_media = FALSE;
3608 	mutex_exit(SD_MUTEX(un));
3609 
3610 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3611 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3612 
3613 	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3614 	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
3615 
3616 	if (rtn != 0)
3617 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3618 
3619 	mutex_enter(SD_MUTEX(un));
3620 	if (rtn == 0) {
3621 		/*
3622 		 * We have good information, check for writable DVD.
3623 		 */
3624 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3625 			un->un_f_mmc_writable_media = TRUE;
3626 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3627 			kmem_free(rqbuf, SENSE_LENGTH);
3628 			return;
3629 		}
3630 	}
3631 
3632 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3633 	kmem_free(rqbuf, SENSE_LENGTH);
3634 
3635 	/*
3636 	 * Determine if this is a RRD type device.
3637 	 */
3638 	mutex_exit(SD_MUTEX(un));
3639 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3640 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3641 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3642 
3643 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3644 
3645 	mutex_enter(SD_MUTEX(un));
3646 	if (status != 0) {
3647 		/* command failed; just return */
3648 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3649 		return;
3650 	}
3651 
3652 	/* Get to the page data */
3653 	sense_mhp = (struct mode_header_grp2 *)buf;
3654 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3655 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3656 		/*
3657 		 * We did not get back the expected block descriptor length so
3658 		 * we cannot check the mode page.
3659 		 */
3660 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3661 		    "sd_check_for_writable_cd: Mode Sense returned "
3662 		    "invalid block descriptor length\n");
3663 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3664 		return;
3665 	}
3666 
3667 	/*
3668 	 * If the device presents DVD or CD capabilities in the mode
3669 	 * page, we can return here since a RRD device will not have
3670 	 * these capabilities.
3671 	 */
3672 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3673 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3674 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3675 		return;
3676 	}
3677 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3678 
3679 	/*
3680 	 * If un->un_f_mmc_writable_media is still FALSE,
3681 	 * check for RRD type media.  A RRD device is identified
3682 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3683 	 */
3684 	mutex_exit(SD_MUTEX(un));
3685 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3686 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3687 
3688 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3689 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3690 	    RANDOM_WRITABLE, path_flag);
3691 
3692 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3693 	if (rtn != 0) {
3694 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3695 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3696 		mutex_enter(SD_MUTEX(un));
3697 		return;
3698 	}
3699 
3700 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3701 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3702 
3703 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3704 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3705 	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
3706 
3707 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3708 	mutex_enter(SD_MUTEX(un));
3709 	if (rtn == 0) {
3710 		/*
3711 		 * We have good information, check for random writable
3712 		 * and hardware defect features as current.
3713 		 */
3714 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3715 		    (out_data_rw[10] & 0x1) &&
3716 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3717 		    (out_data_hd[10] & 0x1)) {
3718 			un->un_f_mmc_writable_media = TRUE;
3719 		}
3720 	}
3721 
3722 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3723 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3724 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3725 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3726 }
3727 
3728 /*
3729  *    Function: sd_read_unit_properties
3730  *
3731  * Description: The following implements a property lookup mechanism.
3732  *		Properties for particular disks (keyed on vendor, model
3733  *		and rev numbers) are sought in the sd.conf file via
3734  *		sd_process_sdconf_file(), and if not found there, are
3735  *		looked for in a list hardcoded in this driver via
3736  *		sd_process_sdconf_table() Once located the properties
3737  *		are used to update the driver unit structure.
3738  *
3739  *   Arguments: un - driver soft state (unit) structure
3740  */
3741 
3742 static void
3743 sd_read_unit_properties(struct sd_lun *un)
3744 {
3745 	/*
3746 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3747 	 * the "sd-config-list" property (from the sd.conf file) or if
3748 	 * there was not a match for the inquiry vid/pid. If this event
3749 	 * occurs the static driver configuration table is searched for
3750 	 * a match.
3751 	 */
3752 	ASSERT(un != NULL);
3753 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3754 		sd_process_sdconf_table(un);
3755 	}
3756 
3757 	/* check for LSI device */
3758 	sd_is_lsi(un);
3759 
3760 
3761 }
3762 
3763 
3764 /*
3765  *    Function: sd_process_sdconf_file
3766  *
3767  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3768  *		driver's config file (ie, sd.conf) and update the driver
3769  *		soft state structure accordingly.
3770  *
3771  *   Arguments: un - driver soft state (unit) structure
3772  *
3773  * Return Code: SD_SUCCESS - The properties were successfully set according
3774  *			     to the driver configuration file.
3775  *		SD_FAILURE - The driver config list was not obtained or
3776  *			     there was no vid/pid match. This indicates that
3777  *			     the static config table should be used.
3778  *
3779  * The config file has a property, "sd-config-list". Currently we support
3780  * two kinds of formats. For both formats, the value of this property
3781  * is a list of duplets:
3782  *
3783  *  sd-config-list=
3784  *	<duplet>,
3785  *	[,<duplet>]*;
3786  *
3787  * For the improved format, where
3788  *
3789  *     <duplet>:= "<vid+pid>","<tunable-list>"
3790  *
3791  * and
3792  *
3793  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3794  *     <tunable> =        <name> : <value>
3795  *
3796  * The <vid+pid> is the string that is returned by the target device on a
3797  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3798  * to apply to all target devices with the specified <vid+pid>.
3799  *
3800  * Each <tunable> is a "<name> : <value>" pair.
3801  *
3802  * For the old format, the structure of each duplet is as follows:
3803  *
3804  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3805  *
3806  * The first entry of the duplet is the device ID string (the concatenated
3807  * vid & pid; not to be confused with a device_id).  This is defined in
3808  * the same way as in the sd_disk_table.
3809  *
3810  * The second part of the duplet is a string that identifies a
3811  * data-property-name-list. The data-property-name-list is defined as
3812  * follows:
3813  *
3814  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3815  *
3816  * The syntax of <data-property-name> depends on the <version> field.
3817  *
3818  * If version = SD_CONF_VERSION_1 we have the following syntax:
3819  *
3820  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3821  *
3822  * where the prop0 value will be used to set prop0 if bit0 set in the
3823  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3824  *
3825  */
3826 
3827 static int
3828 sd_process_sdconf_file(struct sd_lun *un)
3829 {
3830 	char	**config_list = NULL;
3831 	uint_t	nelements;
3832 	char	*vidptr;
3833 	int	vidlen;
3834 	char	*dnlist_ptr;
3835 	char	*dataname_ptr;
3836 	char	*dataname_lasts;
3837 	int	*data_list = NULL;
3838 	uint_t	data_list_len;
3839 	int	rval = SD_FAILURE;
3840 	int	i;
3841 
3842 	ASSERT(un != NULL);
3843 
3844 	/* Obtain the configuration list associated with the .conf file */
3845 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3846 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3847 	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
3848 		return (SD_FAILURE);
3849 	}
3850 
3851 	/*
3852 	 * Compare vids in each duplet to the inquiry vid - if a match is
3853 	 * made, get the data value and update the soft state structure
3854 	 * accordingly.
3855 	 *
3856 	 * Each duplet should show as a pair of strings, return SD_FAILURE
3857 	 * otherwise.
3858 	 */
3859 	if (nelements & 1) {
3860 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3861 		    "sd-config-list should show as pairs of strings.\n");
3862 		if (config_list)
3863 			ddi_prop_free(config_list);
3864 		return (SD_FAILURE);
3865 	}
3866 
3867 	for (i = 0; i < nelements; i += 2) {
3868 		/*
3869 		 * Note: The assumption here is that each vid entry is on
3870 		 * a unique line from its associated duplet.
3871 		 */
3872 		vidptr = config_list[i];
3873 		vidlen = (int)strlen(vidptr);
3874 		if ((vidlen == 0) ||
3875 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3876 			continue;
3877 		}
3878 
3879 		/*
3880 		 * dnlist contains 1 or more blank separated
3881 		 * data-property-name entries
3882 		 */
3883 		dnlist_ptr = config_list[i + 1];
3884 
3885 		if (strchr(dnlist_ptr, ':') != NULL) {
3886 			/*
3887 			 * Decode the improved format sd-config-list.
3888 			 */
3889 			sd_nvpair_str_decode(un, dnlist_ptr);
3890 		} else {
3891 			/*
3892 			 * The old format sd-config-list, loop through all
3893 			 * data-property-name entries in the
3894 			 * data-property-name-list
3895 			 * setting the properties for each.
3896 			 */
3897 			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3898 			    &dataname_lasts); dataname_ptr != NULL;
3899 			    dataname_ptr = sd_strtok_r(NULL, " \t",
3900 			    &dataname_lasts)) {
3901 				int version;
3902 
3903 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3904 				    "sd_process_sdconf_file: disk:%s, "
3905 				    "data:%s\n", vidptr, dataname_ptr);
3906 
3907 				/* Get the data list */
3908 				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3909 				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3910 				    &data_list_len) != DDI_PROP_SUCCESS) {
3911 					SD_INFO(SD_LOG_ATTACH_DETACH, un,
3912 					    "sd_process_sdconf_file: data "
3913 					    "property (%s) has no value\n",
3914 					    dataname_ptr);
3915 					continue;
3916 				}
3917 
3918 				version = data_list[0];
3919 
3920 				if (version == SD_CONF_VERSION_1) {
3921 					sd_tunables values;
3922 
3923 					/* Set the properties */
3924 					if (sd_chk_vers1_data(un, data_list[1],
3925 					    &data_list[2], data_list_len,
3926 					    dataname_ptr) == SD_SUCCESS) {
3927 						sd_get_tunables_from_conf(un,
3928 						    data_list[1], &data_list[2],
3929 						    &values);
3930 						sd_set_vers1_properties(un,
3931 						    data_list[1], &values);
3932 						rval = SD_SUCCESS;
3933 					} else {
3934 						rval = SD_FAILURE;
3935 					}
3936 				} else {
3937 					scsi_log(SD_DEVINFO(un), sd_label,
3938 					    CE_WARN, "data property %s version "
3939 					    "0x%x is invalid.",
3940 					    dataname_ptr, version);
3941 					rval = SD_FAILURE;
3942 				}
3943 				if (data_list)
3944 					ddi_prop_free(data_list);
3945 			}
3946 		}
3947 	}
3948 
3949 	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
3950 	if (config_list) {
3951 		ddi_prop_free(config_list);
3952 	}
3953 
3954 	return (rval);
3955 }
3956 
3957 /*
3958  *    Function: sd_nvpair_str_decode()
3959  *
3960  * Description: Parse the improved format sd-config-list to get
3961  *    each entry of tunable, which includes a name-value pair.
3962  *    Then call sd_set_properties() to set the property.
3963  *
3964  *   Arguments: un - driver soft state (unit) structure
3965  *    nvpair_str - the tunable list
3966  */
3967 static void
3968 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3969 {
3970 	char	*nv, *name, *value, *token;
3971 	char	*nv_lasts, *v_lasts, *x_lasts;
3972 
3973 	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3974 	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3975 		token = sd_strtok_r(nv, ":", &v_lasts);
3976 		name  = sd_strtok_r(token, " \t", &x_lasts);
3977 		token = sd_strtok_r(NULL, ":", &v_lasts);
3978 		value = sd_strtok_r(token, " \t", &x_lasts);
3979 		if (name == NULL || value == NULL) {
3980 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3981 			    "sd_nvpair_str_decode: "
3982 			    "name or value is not valid!\n");
3983 		} else {
3984 			sd_set_properties(un, name, value);
3985 		}
3986 	}
3987 }
3988 
3989 /*
3990  *    Function: sd_strtok_r()
3991  *
3992  * Description: This function uses strpbrk and strspn to break
3993  *    string into tokens on sequentially subsequent calls. Return
3994  *    NULL when no non-separator characters remain. The first
3995  *    argument is NULL for subsequent calls.
3996  */
3997 static char *
3998 sd_strtok_r(char *string, const char *sepset, char **lasts)
3999 {
4000 	char	*q, *r;
4001 
4002 	/* First or subsequent call */
4003 	if (string == NULL)
4004 		string = *lasts;
4005 
4006 	if (string == NULL)
4007 		return (NULL);
4008 
4009 	/* Skip leading separators */
4010 	q = string + strspn(string, sepset);
4011 
4012 	if (*q == '\0')
4013 		return (NULL);
4014 
4015 	if ((r = strpbrk(q, sepset)) == NULL)
4016 		*lasts = NULL;
4017 	else {
4018 		*r = '\0';
4019 		*lasts = r + 1;
4020 	}
4021 	return (q);
4022 }
4023 
4024 /*
4025  *    Function: sd_set_properties()
4026  *
4027  * Description: Set device properties based on the improved
4028  *    format sd-config-list.
4029  *
4030  *   Arguments: un - driver soft state (unit) structure
4031  *    name  - supported tunable name
4032  *    value - tunable value
4033  */
4034 static void
4035 sd_set_properties(struct sd_lun *un, char *name, char *value)
4036 {
4037 	char	*endptr = NULL;
4038 	long	val = 0;
4039 
4040 	if (strcasecmp(name, "cache-nonvolatile") == 0) {
4041 		if (strcasecmp(value, "true") == 0) {
4042 			un->un_f_suppress_cache_flush = TRUE;
4043 		} else if (strcasecmp(value, "false") == 0) {
4044 			un->un_f_suppress_cache_flush = FALSE;
4045 		} else {
4046 			goto value_invalid;
4047 		}
4048 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4049 		    "suppress_cache_flush flag set to %d\n",
4050 		    un->un_f_suppress_cache_flush);
4051 		return;
4052 	}
4053 
4054 	if (strcasecmp(name, "controller-type") == 0) {
4055 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4056 			un->un_ctype = val;
4057 		} else {
4058 			goto value_invalid;
4059 		}
4060 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4061 		    "ctype set to %d\n", un->un_ctype);
4062 		return;
4063 	}
4064 
4065 	if (strcasecmp(name, "delay-busy") == 0) {
4066 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4067 			un->un_busy_timeout = drv_usectohz(val / 1000);
4068 		} else {
4069 			goto value_invalid;
4070 		}
4071 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4072 		    "busy_timeout set to %d\n", un->un_busy_timeout);
4073 		return;
4074 	}
4075 
4076 	if (strcasecmp(name, "disksort") == 0) {
4077 		if (strcasecmp(value, "true") == 0) {
4078 			un->un_f_disksort_disabled = FALSE;
4079 		} else if (strcasecmp(value, "false") == 0) {
4080 			un->un_f_disksort_disabled = TRUE;
4081 		} else {
4082 			goto value_invalid;
4083 		}
4084 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4085 		    "disksort disabled flag set to %d\n",
4086 		    un->un_f_disksort_disabled);
4087 		return;
4088 	}
4089 
4090 	if (strcasecmp(name, "power-condition") == 0) {
4091 		if (strcasecmp(value, "true") == 0) {
4092 			un->un_f_power_condition_disabled = FALSE;
4093 		} else if (strcasecmp(value, "false") == 0) {
4094 			un->un_f_power_condition_disabled = TRUE;
4095 		} else {
4096 			goto value_invalid;
4097 		}
4098 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4099 		    "power condition disabled flag set to %d\n",
4100 		    un->un_f_power_condition_disabled);
4101 		return;
4102 	}
4103 
4104 	if (strcasecmp(name, "timeout-releasereservation") == 0) {
4105 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4106 			un->un_reserve_release_time = val;
4107 		} else {
4108 			goto value_invalid;
4109 		}
4110 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4111 		    "reservation release timeout set to %d\n",
4112 		    un->un_reserve_release_time);
4113 		return;
4114 	}
4115 
4116 	if (strcasecmp(name, "reset-lun") == 0) {
4117 		if (strcasecmp(value, "true") == 0) {
4118 			un->un_f_lun_reset_enabled = TRUE;
4119 		} else if (strcasecmp(value, "false") == 0) {
4120 			un->un_f_lun_reset_enabled = FALSE;
4121 		} else {
4122 			goto value_invalid;
4123 		}
4124 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4125 		    "lun reset enabled flag set to %d\n",
4126 		    un->un_f_lun_reset_enabled);
4127 		return;
4128 	}
4129 
4130 	if (strcasecmp(name, "retries-busy") == 0) {
4131 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4132 			un->un_busy_retry_count = val;
4133 		} else {
4134 			goto value_invalid;
4135 		}
4136 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4137 		    "busy retry count set to %d\n", un->un_busy_retry_count);
4138 		return;
4139 	}
4140 
4141 	if (strcasecmp(name, "retries-timeout") == 0) {
4142 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4143 			un->un_retry_count = val;
4144 		} else {
4145 			goto value_invalid;
4146 		}
4147 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4148 		    "timeout retry count set to %d\n", un->un_retry_count);
4149 		return;
4150 	}
4151 
4152 	if (strcasecmp(name, "retries-notready") == 0) {
4153 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4154 			un->un_notready_retry_count = val;
4155 		} else {
4156 			goto value_invalid;
4157 		}
4158 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4159 		    "notready retry count set to %d\n",
4160 		    un->un_notready_retry_count);
4161 		return;
4162 	}
4163 
4164 	if (strcasecmp(name, "retries-reset") == 0) {
4165 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4166 			un->un_reset_retry_count = val;
4167 		} else {
4168 			goto value_invalid;
4169 		}
4170 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4171 		    "reset retry count set to %d\n",
4172 		    un->un_reset_retry_count);
4173 		return;
4174 	}
4175 
4176 	if (strcasecmp(name, "throttle-max") == 0) {
4177 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4178 			un->un_saved_throttle = un->un_throttle = val;
4179 		} else {
4180 			goto value_invalid;
4181 		}
4182 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4183 		    "throttle set to %d\n", un->un_throttle);
4184 	}
4185 
4186 	if (strcasecmp(name, "throttle-min") == 0) {
4187 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4188 			un->un_min_throttle = val;
4189 		} else {
4190 			goto value_invalid;
4191 		}
4192 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4193 		    "min throttle set to %d\n", un->un_min_throttle);
4194 	}
4195 
4196 	if (strcasecmp(name, "rmw-type") == 0) {
4197 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4198 			un->un_f_rmw_type = val;
4199 		} else {
4200 			goto value_invalid;
4201 		}
4202 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4203 		    "RMW type set to %d\n", un->un_f_rmw_type);
4204 	}
4205 
4206 	/*
4207 	 * Validate the throttle values.
4208 	 * If any of the numbers are invalid, set everything to defaults.
4209 	 */
4210 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4211 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4212 	    (un->un_min_throttle > un->un_throttle)) {
4213 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4214 		un->un_min_throttle = sd_min_throttle;
4215 	}
4216 
4217 	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4218 		if (strcasecmp(value, "true") == 0) {
4219 			un->un_f_mmc_gesn_polling = TRUE;
4220 		} else if (strcasecmp(value, "false") == 0) {
4221 			un->un_f_mmc_gesn_polling = FALSE;
4222 		} else {
4223 			goto value_invalid;
4224 		}
4225 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4226 		    "mmc-gesn-polling set to %d\n",
4227 		    un->un_f_mmc_gesn_polling);
4228 	}
4229 
4230 	return;
4231 
4232 value_invalid:
4233 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4234 	    "value of prop %s is invalid\n", name);
4235 }
4236 
4237 /*
4238  *    Function: sd_get_tunables_from_conf()
4239  *
4240  *
4241  *    This function reads the data list from the sd.conf file and pulls
4242  *    the values that can have numeric values as arguments and places
4243  *    the values in the appropriate sd_tunables member.
4244  *    Since the order of the data list members varies across platforms
4245  *    This function reads them from the data list in a platform specific
4246  *    order and places them into the correct sd_tunable member that is
4247  *    consistent across all platforms.
4248  */
4249 static void
4250 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4251     sd_tunables *values)
4252 {
4253 	int i;
4254 	int mask;
4255 
4256 	bzero(values, sizeof (sd_tunables));
4257 
4258 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4259 
4260 		mask = 1 << i;
4261 		if (mask > flags) {
4262 			break;
4263 		}
4264 
4265 		switch (mask & flags) {
4266 		case 0:	/* This mask bit not set in flags */
4267 			continue;
4268 		case SD_CONF_BSET_THROTTLE:
4269 			values->sdt_throttle = data_list[i];
4270 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4271 			    "sd_get_tunables_from_conf: throttle = %d\n",
4272 			    values->sdt_throttle);
4273 			break;
4274 		case SD_CONF_BSET_CTYPE:
4275 			values->sdt_ctype = data_list[i];
4276 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4277 			    "sd_get_tunables_from_conf: ctype = %d\n",
4278 			    values->sdt_ctype);
4279 			break;
4280 		case SD_CONF_BSET_NRR_COUNT:
4281 			values->sdt_not_rdy_retries = data_list[i];
4282 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4283 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4284 			    values->sdt_not_rdy_retries);
4285 			break;
4286 		case SD_CONF_BSET_BSY_RETRY_COUNT:
4287 			values->sdt_busy_retries = data_list[i];
4288 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4289 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
4290 			    values->sdt_busy_retries);
4291 			break;
4292 		case SD_CONF_BSET_RST_RETRIES:
4293 			values->sdt_reset_retries = data_list[i];
4294 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4295 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
4296 			    values->sdt_reset_retries);
4297 			break;
4298 		case SD_CONF_BSET_RSV_REL_TIME:
4299 			values->sdt_reserv_rel_time = data_list[i];
4300 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4301 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4302 			    values->sdt_reserv_rel_time);
4303 			break;
4304 		case SD_CONF_BSET_MIN_THROTTLE:
4305 			values->sdt_min_throttle = data_list[i];
4306 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4307 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
4308 			    values->sdt_min_throttle);
4309 			break;
4310 		case SD_CONF_BSET_DISKSORT_DISABLED:
4311 			values->sdt_disk_sort_dis = data_list[i];
4312 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4313 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4314 			    values->sdt_disk_sort_dis);
4315 			break;
4316 		case SD_CONF_BSET_LUN_RESET_ENABLED:
4317 			values->sdt_lun_reset_enable = data_list[i];
4318 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4319 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
4320 			    "\n", values->sdt_lun_reset_enable);
4321 			break;
4322 		case SD_CONF_BSET_CACHE_IS_NV:
4323 			values->sdt_suppress_cache_flush = data_list[i];
4324 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4325 			    "sd_get_tunables_from_conf: \
4326 			    suppress_cache_flush = %d"
4327 			    "\n", values->sdt_suppress_cache_flush);
4328 			break;
4329 		case SD_CONF_BSET_PC_DISABLED:
4330 			values->sdt_disk_sort_dis = data_list[i];
4331 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4332 			    "sd_get_tunables_from_conf: power_condition_dis = "
4333 			    "%d\n", values->sdt_power_condition_dis);
4334 			break;
4335 		}
4336 	}
4337 }
4338 
4339 /*
4340  *    Function: sd_process_sdconf_table
4341  *
4342  * Description: Search the static configuration table for a match on the
4343  *		inquiry vid/pid and update the driver soft state structure
4344  *		according to the table property values for the device.
4345  *
4346  *		The form of a configuration table entry is:
4347  *		  <vid+pid>,<flags>,<property-data>
4348  *		  "SEAGATE ST42400N",1,0x40000,
4349  *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4350  *
4351  *   Arguments: un - driver soft state (unit) structure
4352  */
4353 
4354 static void
4355 sd_process_sdconf_table(struct sd_lun *un)
4356 {
4357 	char	*id = NULL;
4358 	int	table_index;
4359 	int	idlen;
4360 
4361 	ASSERT(un != NULL);
4362 	for (table_index = 0; table_index < sd_disk_table_size;
4363 	    table_index++) {
4364 		id = sd_disk_table[table_index].device_id;
4365 		idlen = strlen(id);
4366 		if (idlen == 0) {
4367 			continue;
4368 		}
4369 
4370 		/*
4371 		 * The static configuration table currently does not
4372 		 * implement version 10 properties. Additionally,
4373 		 * multiple data-property-name entries are not
4374 		 * implemented in the static configuration table.
4375 		 */
4376 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4377 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4378 			    "sd_process_sdconf_table: disk %s\n", id);
4379 			sd_set_vers1_properties(un,
4380 			    sd_disk_table[table_index].flags,
4381 			    sd_disk_table[table_index].properties);
4382 			break;
4383 		}
4384 	}
4385 }
4386 
4387 
4388 /*
4389  *    Function: sd_sdconf_id_match
4390  *
4391  * Description: This local function implements a case sensitive vid/pid
4392  *		comparison as well as the boundary cases of wild card and
4393  *		multiple blanks.
4394  *
4395  *		Note: An implicit assumption made here is that the scsi
4396  *		inquiry structure will always keep the vid, pid and
4397  *		revision strings in consecutive sequence, so they can be
4398  *		read as a single string. If this assumption is not the
4399  *		case, a separate string, to be used for the check, needs
4400  *		to be built with these strings concatenated.
4401  *
4402  *   Arguments: un - driver soft state (unit) structure
4403  *		id - table or config file vid/pid
4404  *		idlen  - length of the vid/pid (bytes)
4405  *
4406  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4407  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4408  */
4409 
4410 static int
4411 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4412 {
4413 	struct scsi_inquiry	*sd_inq;
4414 	int 			rval = SD_SUCCESS;
4415 
4416 	ASSERT(un != NULL);
4417 	sd_inq = un->un_sd->sd_inq;
4418 	ASSERT(id != NULL);
4419 
4420 	/*
4421 	 * We use the inq_vid as a pointer to a buffer containing the
4422 	 * vid and pid and use the entire vid/pid length of the table
4423 	 * entry for the comparison. This works because the inq_pid
4424 	 * data member follows inq_vid in the scsi_inquiry structure.
4425 	 */
4426 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4427 		/*
4428 		 * The user id string is compared to the inquiry vid/pid
4429 		 * using a case insensitive comparison and ignoring
4430 		 * multiple spaces.
4431 		 */
4432 		rval = sd_blank_cmp(un, id, idlen);
4433 		if (rval != SD_SUCCESS) {
4434 			/*
4435 			 * User id strings that start and end with a "*"
4436 			 * are a special case. These do not have a
4437 			 * specific vendor, and the product string can
4438 			 * appear anywhere in the 16 byte PID portion of
4439 			 * the inquiry data. This is a simple strstr()
4440 			 * type search for the user id in the inquiry data.
4441 			 */
4442 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4443 				char	*pidptr = &id[1];
4444 				int	i;
4445 				int	j;
4446 				int	pidstrlen = idlen - 2;
4447 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
4448 				    pidstrlen;
4449 
4450 				if (j < 0) {
4451 					return (SD_FAILURE);
4452 				}
4453 				for (i = 0; i < j; i++) {
4454 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4455 					    pidptr, pidstrlen) == 0) {
4456 						rval = SD_SUCCESS;
4457 						break;
4458 					}
4459 				}
4460 			}
4461 		}
4462 	}
4463 	return (rval);
4464 }
4465 
4466 
4467 /*
4468  *    Function: sd_blank_cmp
4469  *
4470  * Description: If the id string starts and ends with a space, treat
4471  *		multiple consecutive spaces as equivalent to a single
4472  *		space. For example, this causes a sd_disk_table entry
4473  *		of " NEC CDROM " to match a device's id string of
4474  *		"NEC       CDROM".
4475  *
4476  *		Note: The success exit condition for this routine is if
4477  *		the pointer to the table entry is '\0' and the cnt of
4478  *		the inquiry length is zero. This will happen if the inquiry
4479  *		string returned by the device is padded with spaces to be
4480  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4481  *		SCSI spec states that the inquiry string is to be padded with
4482  *		spaces.
4483  *
4484  *   Arguments: un - driver soft state (unit) structure
4485  *		id - table or config file vid/pid
4486  *		idlen  - length of the vid/pid (bytes)
4487  *
4488  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4489  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4490  */
4491 
4492 static int
4493 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4494 {
4495 	char		*p1;
4496 	char		*p2;
4497 	int		cnt;
4498 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4499 	    sizeof (SD_INQUIRY(un)->inq_pid);
4500 
4501 	ASSERT(un != NULL);
4502 	p2 = un->un_sd->sd_inq->inq_vid;
4503 	ASSERT(id != NULL);
4504 	p1 = id;
4505 
4506 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4507 		/*
4508 		 * Note: string p1 is terminated by a NUL but string p2
4509 		 * isn't.  The end of p2 is determined by cnt.
4510 		 */
4511 		for (;;) {
4512 			/* skip over any extra blanks in both strings */
4513 			while ((*p1 != '\0') && (*p1 == ' ')) {
4514 				p1++;
4515 			}
4516 			while ((cnt != 0) && (*p2 == ' ')) {
4517 				p2++;
4518 				cnt--;
4519 			}
4520 
4521 			/* compare the two strings */
4522 			if ((cnt == 0) ||
4523 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4524 				break;
4525 			}
4526 			while ((cnt > 0) &&
4527 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4528 				p1++;
4529 				p2++;
4530 				cnt--;
4531 			}
4532 		}
4533 	}
4534 
4535 	/* return SD_SUCCESS if both strings match */
4536 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4537 }
4538 
4539 
4540 /*
4541  *    Function: sd_chk_vers1_data
4542  *
4543  * Description: Verify the version 1 device properties provided by the
4544  *		user via the configuration file
4545  *
4546  *   Arguments: un	     - driver soft state (unit) structure
4547  *		flags	     - integer mask indicating properties to be set
4548  *		prop_list    - integer list of property values
4549  *		list_len     - number of the elements
4550  *
4551  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4552  *		SD_FAILURE - Indicates the user provided data is invalid
4553  */
4554 
4555 static int
4556 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4557     int list_len, char *dataname_ptr)
4558 {
4559 	int i;
4560 	int mask = 1;
4561 	int index = 0;
4562 
4563 	ASSERT(un != NULL);
4564 
4565 	/* Check for a NULL property name and list */
4566 	if (dataname_ptr == NULL) {
4567 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4568 		    "sd_chk_vers1_data: NULL data property name.");
4569 		return (SD_FAILURE);
4570 	}
4571 	if (prop_list == NULL) {
4572 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4573 		    "sd_chk_vers1_data: %s NULL data property list.",
4574 		    dataname_ptr);
4575 		return (SD_FAILURE);
4576 	}
4577 
4578 	/* Display a warning if undefined bits are set in the flags */
4579 	if (flags & ~SD_CONF_BIT_MASK) {
4580 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4581 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4582 		    "Properties not set.",
4583 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4584 		return (SD_FAILURE);
4585 	}
4586 
4587 	/*
4588 	 * Verify the length of the list by identifying the highest bit set
4589 	 * in the flags and validating that the property list has a length
4590 	 * up to the index of this bit.
4591 	 */
4592 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4593 		if (flags & mask) {
4594 			index++;
4595 		}
4596 		mask = 1 << i;
4597 	}
4598 	if (list_len < (index + 2)) {
4599 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4600 		    "sd_chk_vers1_data: "
4601 		    "Data property list %s size is incorrect. "
4602 		    "Properties not set.", dataname_ptr);
4603 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4604 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4605 		return (SD_FAILURE);
4606 	}
4607 	return (SD_SUCCESS);
4608 }
4609 
4610 
4611 /*
4612  *    Function: sd_set_vers1_properties
4613  *
4614  * Description: Set version 1 device properties based on a property list
4615  *		retrieved from the driver configuration file or static
4616  *		configuration table. Version 1 properties have the format:
4617  *
4618  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4619  *
4620  *		where the prop0 value will be used to set prop0 if bit0
4621  *		is set in the flags
4622  *
4623  *   Arguments: un	     - driver soft state (unit) structure
4624  *		flags	     - integer mask indicating properties to be set
4625  *		prop_list    - integer list of property values
4626  */
4627 
4628 static void
4629 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4630 {
4631 	ASSERT(un != NULL);
4632 
4633 	/*
4634 	 * Set the flag to indicate cache is to be disabled. An attempt
4635 	 * to disable the cache via sd_cache_control() will be made
4636 	 * later during attach once the basic initialization is complete.
4637 	 */
4638 	if (flags & SD_CONF_BSET_NOCACHE) {
4639 		un->un_f_opt_disable_cache = TRUE;
4640 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4641 		    "sd_set_vers1_properties: caching disabled flag set\n");
4642 	}
4643 
4644 	/* CD-specific configuration parameters */
4645 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4646 		un->un_f_cfg_playmsf_bcd = TRUE;
4647 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4648 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4649 	}
4650 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4651 		un->un_f_cfg_readsub_bcd = TRUE;
4652 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4653 		    "sd_set_vers1_properties: readsub_bcd set\n");
4654 	}
4655 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4656 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4657 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4658 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4659 	}
4660 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4661 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4662 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4663 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4664 	}
4665 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4666 		un->un_f_cfg_no_read_header = TRUE;
4667 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4668 		    "sd_set_vers1_properties: no_read_header set\n");
4669 	}
4670 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4671 		un->un_f_cfg_read_cd_xd4 = TRUE;
4672 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4673 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4674 	}
4675 
4676 	/* Support for devices which do not have valid/unique serial numbers */
4677 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4678 		un->un_f_opt_fab_devid = TRUE;
4679 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4680 		    "sd_set_vers1_properties: fab_devid bit set\n");
4681 	}
4682 
4683 	/* Support for user throttle configuration */
4684 	if (flags & SD_CONF_BSET_THROTTLE) {
4685 		ASSERT(prop_list != NULL);
4686 		un->un_saved_throttle = un->un_throttle =
4687 		    prop_list->sdt_throttle;
4688 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4689 		    "sd_set_vers1_properties: throttle set to %d\n",
4690 		    prop_list->sdt_throttle);
4691 	}
4692 
4693 	/* Set the per disk retry count according to the conf file or table. */
4694 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4695 		ASSERT(prop_list != NULL);
4696 		if (prop_list->sdt_not_rdy_retries) {
4697 			un->un_notready_retry_count =
4698 			    prop_list->sdt_not_rdy_retries;
4699 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4700 			    "sd_set_vers1_properties: not ready retry count"
4701 			    " set to %d\n", un->un_notready_retry_count);
4702 		}
4703 	}
4704 
4705 	/* The controller type is reported for generic disk driver ioctls */
4706 	if (flags & SD_CONF_BSET_CTYPE) {
4707 		ASSERT(prop_list != NULL);
4708 		switch (prop_list->sdt_ctype) {
4709 		case CTYPE_CDROM:
4710 			un->un_ctype = prop_list->sdt_ctype;
4711 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4712 			    "sd_set_vers1_properties: ctype set to "
4713 			    "CTYPE_CDROM\n");
4714 			break;
4715 		case CTYPE_CCS:
4716 			un->un_ctype = prop_list->sdt_ctype;
4717 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4718 			    "sd_set_vers1_properties: ctype set to "
4719 			    "CTYPE_CCS\n");
4720 			break;
4721 		case CTYPE_ROD:		/* RW optical */
4722 			un->un_ctype = prop_list->sdt_ctype;
4723 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4724 			    "sd_set_vers1_properties: ctype set to "
4725 			    "CTYPE_ROD\n");
4726 			break;
4727 		default:
4728 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4729 			    "sd_set_vers1_properties: Could not set "
4730 			    "invalid ctype value (%d)",
4731 			    prop_list->sdt_ctype);
4732 		}
4733 	}
4734 
4735 	/* Purple failover timeout */
4736 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4737 		ASSERT(prop_list != NULL);
4738 		un->un_busy_retry_count =
4739 		    prop_list->sdt_busy_retries;
4740 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4741 		    "sd_set_vers1_properties: "
4742 		    "busy retry count set to %d\n",
4743 		    un->un_busy_retry_count);
4744 	}
4745 
4746 	/* Purple reset retry count */
4747 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4748 		ASSERT(prop_list != NULL);
4749 		un->un_reset_retry_count =
4750 		    prop_list->sdt_reset_retries;
4751 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4752 		    "sd_set_vers1_properties: "
4753 		    "reset retry count set to %d\n",
4754 		    un->un_reset_retry_count);
4755 	}
4756 
4757 	/* Purple reservation release timeout */
4758 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4759 		ASSERT(prop_list != NULL);
4760 		un->un_reserve_release_time =
4761 		    prop_list->sdt_reserv_rel_time;
4762 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4763 		    "sd_set_vers1_properties: "
4764 		    "reservation release timeout set to %d\n",
4765 		    un->un_reserve_release_time);
4766 	}
4767 
4768 	/*
4769 	 * Driver flag telling the driver to verify that no commands are pending
4770 	 * for a device before issuing a Test Unit Ready. This is a workaround
4771 	 * for a firmware bug in some Seagate eliteI drives.
4772 	 */
4773 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4774 		un->un_f_cfg_tur_check = TRUE;
4775 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4776 		    "sd_set_vers1_properties: tur queue check set\n");
4777 	}
4778 
4779 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4780 		un->un_min_throttle = prop_list->sdt_min_throttle;
4781 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4782 		    "sd_set_vers1_properties: min throttle set to %d\n",
4783 		    un->un_min_throttle);
4784 	}
4785 
4786 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4787 		un->un_f_disksort_disabled =
4788 		    (prop_list->sdt_disk_sort_dis != 0) ?
4789 		    TRUE : FALSE;
4790 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4791 		    "sd_set_vers1_properties: disksort disabled "
4792 		    "flag set to %d\n",
4793 		    prop_list->sdt_disk_sort_dis);
4794 	}
4795 
4796 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4797 		un->un_f_lun_reset_enabled =
4798 		    (prop_list->sdt_lun_reset_enable != 0) ?
4799 		    TRUE : FALSE;
4800 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4801 		    "sd_set_vers1_properties: lun reset enabled "
4802 		    "flag set to %d\n",
4803 		    prop_list->sdt_lun_reset_enable);
4804 	}
4805 
4806 	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4807 		un->un_f_suppress_cache_flush =
4808 		    (prop_list->sdt_suppress_cache_flush != 0) ?
4809 		    TRUE : FALSE;
4810 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4811 		    "sd_set_vers1_properties: suppress_cache_flush "
4812 		    "flag set to %d\n",
4813 		    prop_list->sdt_suppress_cache_flush);
4814 	}
4815 
4816 	if (flags & SD_CONF_BSET_PC_DISABLED) {
4817 		un->un_f_power_condition_disabled =
4818 		    (prop_list->sdt_power_condition_dis != 0) ?
4819 		    TRUE : FALSE;
4820 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4821 		    "sd_set_vers1_properties: power_condition_disabled "
4822 		    "flag set to %d\n",
4823 		    prop_list->sdt_power_condition_dis);
4824 	}
4825 
4826 	/*
4827 	 * Validate the throttle values.
4828 	 * If any of the numbers are invalid, set everything to defaults.
4829 	 */
4830 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4831 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4832 	    (un->un_min_throttle > un->un_throttle)) {
4833 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4834 		un->un_min_throttle = sd_min_throttle;
4835 	}
4836 }
4837 
4838 /*
4839  *   Function: sd_is_lsi()
4840  *
4841  *   Description: Check for lsi devices, step through the static device
4842  *	table to match vid/pid.
4843  *
4844  *   Args: un - ptr to sd_lun
4845  *
4846  *   Notes:  When creating new LSI property, need to add the new LSI property
4847  *		to this function.
4848  */
4849 static void
4850 sd_is_lsi(struct sd_lun *un)
4851 {
4852 	char	*id = NULL;
4853 	int	table_index;
4854 	int	idlen;
4855 	void	*prop;
4856 
4857 	ASSERT(un != NULL);
4858 	for (table_index = 0; table_index < sd_disk_table_size;
4859 	    table_index++) {
4860 		id = sd_disk_table[table_index].device_id;
4861 		idlen = strlen(id);
4862 		if (idlen == 0) {
4863 			continue;
4864 		}
4865 
4866 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4867 			prop = sd_disk_table[table_index].properties;
4868 			if (prop == &lsi_properties ||
4869 			    prop == &lsi_oem_properties ||
4870 			    prop == &lsi_properties_scsi ||
4871 			    prop == &symbios_properties) {
4872 				un->un_f_cfg_is_lsi = TRUE;
4873 			}
4874 			break;
4875 		}
4876 	}
4877 }
4878 
4879 /*
4880  *    Function: sd_get_physical_geometry
4881  *
4882  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4883  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4884  *		target, and use this information to initialize the physical
4885  *		geometry cache specified by pgeom_p.
4886  *
4887  *		MODE SENSE is an optional command, so failure in this case
4888  *		does not necessarily denote an error. We want to use the
4889  *		MODE SENSE commands to derive the physical geometry of the
4890  *		device, but if either command fails, the logical geometry is
4891  *		used as the fallback for disk label geometry in cmlb.
4892  *
4893  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4894  *		have already been initialized for the current target and
4895  *		that the current values be passed as args so that we don't
4896  *		end up ever trying to use -1 as a valid value. This could
4897  *		happen if either value is reset while we're not holding
4898  *		the mutex.
4899  *
4900  *   Arguments: un - driver soft state (unit) structure
4901  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4902  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4903  *			to use the USCSI "direct" chain and bypass the normal
4904  *			command waitq.
4905  *
4906  *     Context: Kernel thread only (can sleep).
4907  */
4908 
4909 static int
4910 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4911 	diskaddr_t capacity, int lbasize, int path_flag)
4912 {
4913 	struct	mode_format	*page3p;
4914 	struct	mode_geometry	*page4p;
4915 	struct	mode_header	*headerp;
4916 	int	sector_size;
4917 	int	nsect;
4918 	int	nhead;
4919 	int	ncyl;
4920 	int	intrlv;
4921 	int	spc;
4922 	diskaddr_t	modesense_capacity;
4923 	int	rpm;
4924 	int	bd_len;
4925 	int	mode_header_length;
4926 	uchar_t	*p3bufp;
4927 	uchar_t	*p4bufp;
4928 	int	cdbsize;
4929 	int 	ret = EIO;
4930 	sd_ssc_t *ssc;
4931 	int	status;
4932 
4933 	ASSERT(un != NULL);
4934 
4935 	if (lbasize == 0) {
4936 		if (ISCD(un)) {
4937 			lbasize = 2048;
4938 		} else {
4939 			lbasize = un->un_sys_blocksize;
4940 		}
4941 	}
4942 	pgeom_p->g_secsize = (unsigned short)lbasize;
4943 
4944 	/*
4945 	 * If the unit is a cd/dvd drive MODE SENSE page three
4946 	 * and MODE SENSE page four are reserved (see SBC spec
4947 	 * and MMC spec). To prevent soft errors just return
4948 	 * using the default LBA size.
4949 	 */
4950 	if (ISCD(un))
4951 		return (ret);
4952 
4953 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4954 
4955 	/*
4956 	 * Retrieve MODE SENSE page 3 - Format Device Page
4957 	 */
4958 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4959 	ssc = sd_ssc_init(un);
4960 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4961 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4962 	if (status != 0) {
4963 		SD_ERROR(SD_LOG_COMMON, un,
4964 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
4965 		goto page3_exit;
4966 	}
4967 
4968 	/*
4969 	 * Determine size of Block Descriptors in order to locate the mode
4970 	 * page data.  ATAPI devices return 0, SCSI devices should return
4971 	 * MODE_BLK_DESC_LENGTH.
4972 	 */
4973 	headerp = (struct mode_header *)p3bufp;
4974 	if (un->un_f_cfg_is_atapi == TRUE) {
4975 		struct mode_header_grp2 *mhp =
4976 		    (struct mode_header_grp2 *)headerp;
4977 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
4978 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4979 	} else {
4980 		mode_header_length = MODE_HEADER_LENGTH;
4981 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4982 	}
4983 
4984 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4985 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4986 		    "sd_get_physical_geometry: received unexpected bd_len "
4987 		    "of %d, page3\n", bd_len);
4988 		status = EIO;
4989 		goto page3_exit;
4990 	}
4991 
4992 	page3p = (struct mode_format *)
4993 	    ((caddr_t)headerp + mode_header_length + bd_len);
4994 
4995 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
4996 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
4997 		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
4998 		    "%d\n", page3p->mode_page.code);
4999 		status = EIO;
5000 		goto page3_exit;
5001 	}
5002 
5003 	/*
5004 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5005 	 * complete successfully; otherwise, revert to the logical geometry.
5006 	 * So, we need to save everything in temporary variables.
5007 	 */
5008 	sector_size = BE_16(page3p->data_bytes_sect);
5009 
5010 	/*
5011 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5012 	 */
5013 	if (sector_size == 0) {
5014 		sector_size = un->un_sys_blocksize;
5015 	} else {
5016 		sector_size &= ~(un->un_sys_blocksize - 1);
5017 	}
5018 
5019 	nsect  = BE_16(page3p->sect_track);
5020 	intrlv = BE_16(page3p->interleave);
5021 
5022 	SD_INFO(SD_LOG_COMMON, un,
5023 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5024 	SD_INFO(SD_LOG_COMMON, un,
5025 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5026 	    page3p->mode_page.code, nsect, sector_size);
5027 	SD_INFO(SD_LOG_COMMON, un,
5028 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5029 	    BE_16(page3p->track_skew),
5030 	    BE_16(page3p->cylinder_skew));
5031 
5032 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5033 
5034 	/*
5035 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5036 	 */
5037 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5038 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5039 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5040 	if (status != 0) {
5041 		SD_ERROR(SD_LOG_COMMON, un,
5042 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5043 		goto page4_exit;
5044 	}
5045 
5046 	/*
5047 	 * Determine size of Block Descriptors in order to locate the mode
5048 	 * page data.  ATAPI devices return 0, SCSI devices should return
5049 	 * MODE_BLK_DESC_LENGTH.
5050 	 */
5051 	headerp = (struct mode_header *)p4bufp;
5052 	if (un->un_f_cfg_is_atapi == TRUE) {
5053 		struct mode_header_grp2 *mhp =
5054 		    (struct mode_header_grp2 *)headerp;
5055 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5056 	} else {
5057 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5058 	}
5059 
5060 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5061 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5062 		    "sd_get_physical_geometry: received unexpected bd_len of "
5063 		    "%d, page4\n", bd_len);
5064 		status = EIO;
5065 		goto page4_exit;
5066 	}
5067 
5068 	page4p = (struct mode_geometry *)
5069 	    ((caddr_t)headerp + mode_header_length + bd_len);
5070 
5071 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5072 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5073 		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
5074 		    "%d\n", page4p->mode_page.code);
5075 		status = EIO;
5076 		goto page4_exit;
5077 	}
5078 
5079 	/*
5080 	 * Stash the data now, after we know that both commands completed.
5081 	 */
5082 
5083 
5084 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5085 	spc   = nhead * nsect;
5086 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5087 	rpm   = BE_16(page4p->rpm);
5088 
5089 	modesense_capacity = spc * ncyl;
5090 
5091 	SD_INFO(SD_LOG_COMMON, un,
5092 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5093 	SD_INFO(SD_LOG_COMMON, un,
5094 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5095 	SD_INFO(SD_LOG_COMMON, un,
5096 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5097 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5098 	    (void *)pgeom_p, capacity);
5099 
5100 	/*
5101 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5102 	 * the product of C * H * S returned by MODE SENSE >= that returned
5103 	 * by read capacity. This is an idiosyncrasy of the original x86
5104 	 * disk subsystem.
5105 	 */
5106 	if (modesense_capacity >= capacity) {
5107 		SD_INFO(SD_LOG_COMMON, un,
5108 		    "sd_get_physical_geometry: adjusting acyl; "
5109 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5110 		    (modesense_capacity - capacity + spc - 1) / spc);
5111 		if (sector_size != 0) {
5112 			/* 1243403: NEC D38x7 drives don't support sec size */
5113 			pgeom_p->g_secsize = (unsigned short)sector_size;
5114 		}
5115 		pgeom_p->g_nsect    = (unsigned short)nsect;
5116 		pgeom_p->g_nhead    = (unsigned short)nhead;
5117 		pgeom_p->g_capacity = capacity;
5118 		pgeom_p->g_acyl	    =
5119 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5120 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5121 	}
5122 
5123 	pgeom_p->g_rpm    = (unsigned short)rpm;
5124 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5125 	ret = 0;
5126 
5127 	SD_INFO(SD_LOG_COMMON, un,
5128 	    "sd_get_physical_geometry: mode sense geometry:\n");
5129 	SD_INFO(SD_LOG_COMMON, un,
5130 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5131 	    nsect, sector_size, intrlv);
5132 	SD_INFO(SD_LOG_COMMON, un,
5133 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5134 	    nhead, ncyl, rpm, modesense_capacity);
5135 	SD_INFO(SD_LOG_COMMON, un,
5136 	    "sd_get_physical_geometry: (cached)\n");
5137 	SD_INFO(SD_LOG_COMMON, un,
5138 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5139 	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5140 	    pgeom_p->g_nhead, pgeom_p->g_nsect);
5141 	SD_INFO(SD_LOG_COMMON, un,
5142 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5143 	    pgeom_p->g_secsize, pgeom_p->g_capacity,
5144 	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
5145 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5146 
5147 page4_exit:
5148 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5149 
5150 page3_exit:
5151 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5152 
5153 	if (status != 0) {
5154 		if (status == EIO) {
5155 			/*
5156 			 * Some disks do not support mode sense(6), we
5157 			 * should ignore this kind of error(sense key is
5158 			 * 0x5 - illegal request).
5159 			 */
5160 			uint8_t *sensep;
5161 			int senlen;
5162 
5163 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5164 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5165 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
5166 
5167 			if (senlen > 0 &&
5168 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5169 				sd_ssc_assessment(ssc,
5170 				    SD_FMT_IGNORE_COMPROMISE);
5171 			} else {
5172 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5173 			}
5174 		} else {
5175 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5176 		}
5177 	}
5178 	sd_ssc_fini(ssc);
5179 	return (ret);
5180 }
5181 
5182 /*
5183  *    Function: sd_get_virtual_geometry
5184  *
5185  * Description: Ask the controller to tell us about the target device.
5186  *
5187  *   Arguments: un - pointer to softstate
5188  *		capacity - disk capacity in #blocks
5189  *		lbasize - disk block size in bytes
5190  *
5191  *     Context: Kernel thread only
5192  */
5193 
5194 static int
5195 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5196     diskaddr_t capacity, int lbasize)
5197 {
5198 	uint_t	geombuf;
5199 	int	spc;
5200 
5201 	ASSERT(un != NULL);
5202 
5203 	/* Set sector size, and total number of sectors */
5204 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5205 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5206 
5207 	/* Let the HBA tell us its geometry */
5208 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5209 
5210 	/* A value of -1 indicates an undefined "geometry" property */
5211 	if (geombuf == (-1)) {
5212 		return (EINVAL);
5213 	}
5214 
5215 	/* Initialize the logical geometry cache. */
5216 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5217 	lgeom_p->g_nsect   = geombuf & 0xffff;
5218 	lgeom_p->g_secsize = un->un_sys_blocksize;
5219 
5220 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5221 
5222 	/*
5223 	 * Note: The driver originally converted the capacity value from
5224 	 * target blocks to system blocks. However, the capacity value passed
5225 	 * to this routine is already in terms of system blocks (this scaling
5226 	 * is done when the READ CAPACITY command is issued and processed).
5227 	 * This 'error' may have gone undetected because the usage of g_ncyl
5228 	 * (which is based upon g_capacity) is very limited within the driver
5229 	 */
5230 	lgeom_p->g_capacity = capacity;
5231 
5232 	/*
5233 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5234 	 * hba may return zero values if the device has been removed.
5235 	 */
5236 	if (spc == 0) {
5237 		lgeom_p->g_ncyl = 0;
5238 	} else {
5239 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5240 	}
5241 	lgeom_p->g_acyl = 0;
5242 
5243 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5244 	return (0);
5245 
5246 }
5247 /*
5248  *    Function: sd_update_block_info
5249  *
5250  * Description: Calculate a byte count to sector count bitshift value
5251  *		from sector size.
5252  *
5253  *   Arguments: un: unit struct.
5254  *		lbasize: new target sector size
5255  *		capacity: new target capacity, ie. block count
5256  *
5257  *     Context: Kernel thread context
5258  */
5259 
5260 static void
5261 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5262 {
5263 	if (lbasize != 0) {
5264 		un->un_tgt_blocksize = lbasize;
5265 		un->un_f_tgt_blocksize_is_valid = TRUE;
5266 		if (!un->un_f_has_removable_media) {
5267 			un->un_sys_blocksize = lbasize;
5268 		}
5269 	}
5270 
5271 	if (capacity != 0) {
5272 		un->un_blockcount		= capacity;
5273 		un->un_f_blockcount_is_valid	= TRUE;
5274 	}
5275 }
5276 
5277 
5278 /*
5279  *    Function: sd_register_devid
5280  *
5281  * Description: This routine will obtain the device id information from the
5282  *		target, obtain the serial number, and register the device
5283  *		id with the ddi framework.
5284  *
5285  *   Arguments: devi - the system's dev_info_t for the device.
5286  *		un - driver soft state (unit) structure
5287  *		reservation_flag - indicates if a reservation conflict
5288  *		occurred during attach
5289  *
5290  *     Context: Kernel Thread
5291  */
5292 static void
5293 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5294 {
5295 	int		rval		= 0;
5296 	uchar_t		*inq80		= NULL;
5297 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5298 	size_t		inq80_resid	= 0;
5299 	uchar_t		*inq83		= NULL;
5300 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5301 	size_t		inq83_resid	= 0;
5302 	int		dlen, len;
5303 	char		*sn;
5304 	struct sd_lun	*un;
5305 
5306 	ASSERT(ssc != NULL);
5307 	un = ssc->ssc_un;
5308 	ASSERT(un != NULL);
5309 	ASSERT(mutex_owned(SD_MUTEX(un)));
5310 	ASSERT((SD_DEVINFO(un)) == devi);
5311 
5312 
5313 	/*
5314 	 * We check the availability of the World Wide Name (0x83) and Unit
5315 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5316 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5317 	 * 0x83 is available, that is the best choice.  Our next choice is
5318 	 * 0x80.  If neither are available, we munge the devid from the device
5319 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5320 	 * to fabricate a devid for non-Sun qualified disks.
5321 	 */
5322 	if (sd_check_vpd_page_support(ssc) == 0) {
5323 		/* collect page 80 data if available */
5324 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5325 
5326 			mutex_exit(SD_MUTEX(un));
5327 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5328 
5329 			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5330 			    0x01, 0x80, &inq80_resid);
5331 
5332 			if (rval != 0) {
5333 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5334 				kmem_free(inq80, inq80_len);
5335 				inq80 = NULL;
5336 				inq80_len = 0;
5337 			} else if (ddi_prop_exists(
5338 			    DDI_DEV_T_NONE, SD_DEVINFO(un),
5339 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5340 			    INQUIRY_SERIAL_NO) == 0) {
5341 				/*
5342 				 * If we don't already have a serial number
5343 				 * property, do quick verify of data returned
5344 				 * and define property.
5345 				 */
5346 				dlen = inq80_len - inq80_resid;
5347 				len = (size_t)inq80[3];
5348 				if ((dlen >= 4) && ((len + 4) <= dlen)) {
5349 					/*
5350 					 * Ensure sn termination, skip leading
5351 					 * blanks, and create property
5352 					 * 'inquiry-serial-no'.
5353 					 */
5354 					sn = (char *)&inq80[4];
5355 					sn[len] = 0;
5356 					while (*sn && (*sn == ' '))
5357 						sn++;
5358 					if (*sn) {
5359 						(void) ddi_prop_update_string(
5360 						    DDI_DEV_T_NONE,
5361 						    SD_DEVINFO(un),
5362 						    INQUIRY_SERIAL_NO, sn);
5363 					}
5364 				}
5365 			}
5366 			mutex_enter(SD_MUTEX(un));
5367 		}
5368 
5369 		/* collect page 83 data if available */
5370 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5371 			mutex_exit(SD_MUTEX(un));
5372 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5373 
5374 			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5375 			    0x01, 0x83, &inq83_resid);
5376 
5377 			if (rval != 0) {
5378 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5379 				kmem_free(inq83, inq83_len);
5380 				inq83 = NULL;
5381 				inq83_len = 0;
5382 			}
5383 			mutex_enter(SD_MUTEX(un));
5384 		}
5385 	}
5386 
5387 	/*
5388 	 * If transport has already registered a devid for this target
5389 	 * then that takes precedence over the driver's determination
5390 	 * of the devid.
5391 	 *
5392 	 * NOTE: The reason this check is done here instead of at the beginning
5393 	 * of the function is to allow the code above to create the
5394 	 * 'inquiry-serial-no' property.
5395 	 */
5396 	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5397 		ASSERT(un->un_devid);
5398 		un->un_f_devid_transport_defined = TRUE;
5399 		goto cleanup; /* use devid registered by the transport */
5400 	}
5401 
5402 	/*
5403 	 * This is the case of antiquated Sun disk drives that have the
5404 	 * FAB_DEVID property set in the disk_table.  These drives
5405 	 * manage the devid's by storing them in last 2 available sectors
5406 	 * on the drive and have them fabricated by the ddi layer by calling
5407 	 * ddi_devid_init and passing the DEVID_FAB flag.
5408 	 */
5409 	if (un->un_f_opt_fab_devid == TRUE) {
5410 		/*
5411 		 * Depending on EINVAL isn't reliable, since a reserved disk
5412 		 * may result in invalid geometry, so check to make sure a
5413 		 * reservation conflict did not occur during attach.
5414 		 */
5415 		if ((sd_get_devid(ssc) == EINVAL) &&
5416 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5417 			/*
5418 			 * The devid is invalid AND there is no reservation
5419 			 * conflict.  Fabricate a new devid.
5420 			 */
5421 			(void) sd_create_devid(ssc);
5422 		}
5423 
5424 		/* Register the devid if it exists */
5425 		if (un->un_devid != NULL) {
5426 			(void) ddi_devid_register(SD_DEVINFO(un),
5427 			    un->un_devid);
5428 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5429 			    "sd_register_devid: Devid Fabricated\n");
5430 		}
5431 		goto cleanup;
5432 	}
5433 
5434 	/* encode best devid possible based on data available */
5435 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5436 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5437 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5438 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5439 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5440 
5441 		/* devid successfully encoded, register devid */
5442 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5443 
5444 	} else {
5445 		/*
5446 		 * Unable to encode a devid based on data available.
5447 		 * This is not a Sun qualified disk.  Older Sun disk
5448 		 * drives that have the SD_FAB_DEVID property
5449 		 * set in the disk_table and non Sun qualified
5450 		 * disks are treated in the same manner.  These
5451 		 * drives manage the devid's by storing them in
5452 		 * last 2 available sectors on the drive and
5453 		 * have them fabricated by the ddi layer by
5454 		 * calling ddi_devid_init and passing the
5455 		 * DEVID_FAB flag.
5456 		 * Create a fabricate devid only if there's no
5457 		 * fabricate devid existed.
5458 		 */
5459 		if (sd_get_devid(ssc) == EINVAL) {
5460 			(void) sd_create_devid(ssc);
5461 		}
5462 		un->un_f_opt_fab_devid = TRUE;
5463 
5464 		/* Register the devid if it exists */
5465 		if (un->un_devid != NULL) {
5466 			(void) ddi_devid_register(SD_DEVINFO(un),
5467 			    un->un_devid);
5468 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5469 			    "sd_register_devid: devid fabricated using "
5470 			    "ddi framework\n");
5471 		}
5472 	}
5473 
5474 cleanup:
5475 	/* clean up resources */
5476 	if (inq80 != NULL) {
5477 		kmem_free(inq80, inq80_len);
5478 	}
5479 	if (inq83 != NULL) {
5480 		kmem_free(inq83, inq83_len);
5481 	}
5482 }
5483 
5484 
5485 
5486 /*
5487  *    Function: sd_get_devid
5488  *
5489  * Description: This routine will return 0 if a valid device id has been
5490  *		obtained from the target and stored in the soft state. If a
5491  *		valid device id has not been previously read and stored, a
5492  *		read attempt will be made.
5493  *
5494  *   Arguments: un - driver soft state (unit) structure
5495  *
5496  * Return Code: 0 if we successfully get the device id
5497  *
5498  *     Context: Kernel Thread
5499  */
5500 
5501 static int
5502 sd_get_devid(sd_ssc_t *ssc)
5503 {
5504 	struct dk_devid		*dkdevid;
5505 	ddi_devid_t		tmpid;
5506 	uint_t			*ip;
5507 	size_t			sz;
5508 	diskaddr_t		blk;
5509 	int			status;
5510 	int			chksum;
5511 	int			i;
5512 	size_t			buffer_size;
5513 	struct sd_lun		*un;
5514 
5515 	ASSERT(ssc != NULL);
5516 	un = ssc->ssc_un;
5517 	ASSERT(un != NULL);
5518 	ASSERT(mutex_owned(SD_MUTEX(un)));
5519 
5520 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5521 	    un);
5522 
5523 	if (un->un_devid != NULL) {
5524 		return (0);
5525 	}
5526 
5527 	mutex_exit(SD_MUTEX(un));
5528 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5529 	    (void *)SD_PATH_DIRECT) != 0) {
5530 		mutex_enter(SD_MUTEX(un));
5531 		return (EINVAL);
5532 	}
5533 
5534 	/*
5535 	 * Read and verify device id, stored in the reserved cylinders at the
5536 	 * end of the disk. Backup label is on the odd sectors of the last
5537 	 * track of the last cylinder. Device id will be on track of the next
5538 	 * to last cylinder.
5539 	 */
5540 	mutex_enter(SD_MUTEX(un));
5541 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5542 	mutex_exit(SD_MUTEX(un));
5543 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5544 	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5545 	    SD_PATH_DIRECT);
5546 
5547 	if (status != 0) {
5548 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5549 		goto error;
5550 	}
5551 
5552 	/* Validate the revision */
5553 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5554 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5555 		status = EINVAL;
5556 		goto error;
5557 	}
5558 
5559 	/* Calculate the checksum */
5560 	chksum = 0;
5561 	ip = (uint_t *)dkdevid;
5562 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5563 	    i++) {
5564 		chksum ^= ip[i];
5565 	}
5566 
5567 	/* Compare the checksums */
5568 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5569 		status = EINVAL;
5570 		goto error;
5571 	}
5572 
5573 	/* Validate the device id */
5574 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5575 		status = EINVAL;
5576 		goto error;
5577 	}
5578 
5579 	/*
5580 	 * Store the device id in the driver soft state
5581 	 */
5582 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5583 	tmpid = kmem_alloc(sz, KM_SLEEP);
5584 
5585 	mutex_enter(SD_MUTEX(un));
5586 
5587 	un->un_devid = tmpid;
5588 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5589 
5590 	kmem_free(dkdevid, buffer_size);
5591 
5592 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5593 
5594 	return (status);
5595 error:
5596 	mutex_enter(SD_MUTEX(un));
5597 	kmem_free(dkdevid, buffer_size);
5598 	return (status);
5599 }
5600 
5601 
5602 /*
5603  *    Function: sd_create_devid
5604  *
5605  * Description: This routine will fabricate the device id and write it
5606  *		to the disk.
5607  *
5608  *   Arguments: un - driver soft state (unit) structure
5609  *
5610  * Return Code: value of the fabricated device id
5611  *
5612  *     Context: Kernel Thread
5613  */
5614 
5615 static ddi_devid_t
5616 sd_create_devid(sd_ssc_t *ssc)
5617 {
5618 	struct sd_lun	*un;
5619 
5620 	ASSERT(ssc != NULL);
5621 	un = ssc->ssc_un;
5622 	ASSERT(un != NULL);
5623 
5624 	/* Fabricate the devid */
5625 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5626 	    == DDI_FAILURE) {
5627 		return (NULL);
5628 	}
5629 
5630 	/* Write the devid to disk */
5631 	if (sd_write_deviceid(ssc) != 0) {
5632 		ddi_devid_free(un->un_devid);
5633 		un->un_devid = NULL;
5634 	}
5635 
5636 	return (un->un_devid);
5637 }
5638 
5639 
5640 /*
5641  *    Function: sd_write_deviceid
5642  *
5643  * Description: This routine will write the device id to the disk
5644  *		reserved sector.
5645  *
5646  *   Arguments: un - driver soft state (unit) structure
5647  *
5648  * Return Code: EINVAL
5649  *		value returned by sd_send_scsi_cmd
5650  *
5651  *     Context: Kernel Thread
5652  */
5653 
5654 static int
5655 sd_write_deviceid(sd_ssc_t *ssc)
5656 {
5657 	struct dk_devid		*dkdevid;
5658 	uchar_t			*buf;
5659 	diskaddr_t		blk;
5660 	uint_t			*ip, chksum;
5661 	int			status;
5662 	int			i;
5663 	struct sd_lun		*un;
5664 
5665 	ASSERT(ssc != NULL);
5666 	un = ssc->ssc_un;
5667 	ASSERT(un != NULL);
5668 	ASSERT(mutex_owned(SD_MUTEX(un)));
5669 
5670 	mutex_exit(SD_MUTEX(un));
5671 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5672 	    (void *)SD_PATH_DIRECT) != 0) {
5673 		mutex_enter(SD_MUTEX(un));
5674 		return (-1);
5675 	}
5676 
5677 
5678 	/* Allocate the buffer */
5679 	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5680 	dkdevid = (struct dk_devid *)buf;
5681 
5682 	/* Fill in the revision */
5683 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5684 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5685 
5686 	/* Copy in the device id */
5687 	mutex_enter(SD_MUTEX(un));
5688 	bcopy(un->un_devid, &dkdevid->dkd_devid,
5689 	    ddi_devid_sizeof(un->un_devid));
5690 	mutex_exit(SD_MUTEX(un));
5691 
5692 	/* Calculate the checksum */
5693 	chksum = 0;
5694 	ip = (uint_t *)dkdevid;
5695 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5696 	    i++) {
5697 		chksum ^= ip[i];
5698 	}
5699 
5700 	/* Fill-in checksum */
5701 	DKD_FORMCHKSUM(chksum, dkdevid);
5702 
5703 	/* Write the reserved sector */
5704 	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5705 	    SD_PATH_DIRECT);
5706 	if (status != 0)
5707 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5708 
5709 	kmem_free(buf, un->un_sys_blocksize);
5710 
5711 	mutex_enter(SD_MUTEX(un));
5712 	return (status);
5713 }
5714 
5715 
5716 /*
5717  *    Function: sd_check_vpd_page_support
5718  *
5719  * Description: This routine sends an inquiry command with the EVPD bit set and
5720  *		a page code of 0x00 to the device. It is used to determine which
5721  *		vital product pages are available to find the devid. We are
5722  *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5723  *		the device does not support that command.
5724  *
5725  *   Arguments: un  - driver soft state (unit) structure
5726  *
5727  * Return Code: 0 - success
5728  *		1 - check condition
5729  *
5730  *     Context: This routine can sleep.
5731  */
5732 
5733 static int
5734 sd_check_vpd_page_support(sd_ssc_t *ssc)
5735 {
5736 	uchar_t	*page_list	= NULL;
5737 	uchar_t	page_length	= 0xff;	/* Use max possible length */
5738 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
5739 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
5740 	int    	rval		= 0;
5741 	int	counter;
5742 	struct sd_lun		*un;
5743 
5744 	ASSERT(ssc != NULL);
5745 	un = ssc->ssc_un;
5746 	ASSERT(un != NULL);
5747 	ASSERT(mutex_owned(SD_MUTEX(un)));
5748 
5749 	mutex_exit(SD_MUTEX(un));
5750 
5751 	/*
5752 	 * We'll set the page length to the maximum to save figuring it out
5753 	 * with an additional call.
5754 	 */
5755 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
5756 
5757 	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5758 	    page_code, NULL);
5759 
5760 	if (rval != 0)
5761 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5762 
5763 	mutex_enter(SD_MUTEX(un));
5764 
5765 	/*
5766 	 * Now we must validate that the device accepted the command, as some
5767 	 * drives do not support it.  If the drive does support it, we will
5768 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
5769 	 * not, we return -1.
5770 	 */
5771 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5772 		/* Loop to find one of the 2 pages we need */
5773 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5774 
5775 		/*
5776 		 * Pages are returned in ascending order, and 0x83 is what we
5777 		 * are hoping for.
5778 		 */
5779 		while ((page_list[counter] <= 0xB1) &&
5780 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
5781 		    VPD_HEAD_OFFSET))) {
5782 			/*
5783 			 * Add 3 because page_list[3] is the number of
5784 			 * pages minus 3
5785 			 */
5786 
5787 			switch (page_list[counter]) {
5788 			case 0x00:
5789 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5790 				break;
5791 			case 0x80:
5792 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5793 				break;
5794 			case 0x81:
5795 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5796 				break;
5797 			case 0x82:
5798 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5799 				break;
5800 			case 0x83:
5801 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5802 				break;
5803 			case 0x86:
5804 				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5805 				break;
5806 			case 0xB1:
5807 				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5808 				break;
5809 			}
5810 			counter++;
5811 		}
5812 
5813 	} else {
5814 		rval = -1;
5815 
5816 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
5817 		    "sd_check_vpd_page_support: This drive does not implement "
5818 		    "VPD pages.\n");
5819 	}
5820 
5821 	kmem_free(page_list, page_length);
5822 
5823 	return (rval);
5824 }
5825 
5826 
5827 /*
5828  *    Function: sd_setup_pm
5829  *
5830  * Description: Initialize Power Management on the device
5831  *
5832  *     Context: Kernel Thread
5833  */
5834 
5835 static void
5836 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5837 {
5838 	uint_t		log_page_size;
5839 	uchar_t		*log_page_data;
5840 	int		rval = 0;
5841 	struct sd_lun	*un;
5842 
5843 	ASSERT(ssc != NULL);
5844 	un = ssc->ssc_un;
5845 	ASSERT(un != NULL);
5846 
5847 	/*
5848 	 * Since we are called from attach, holding a mutex for
5849 	 * un is unnecessary. Because some of the routines called
5850 	 * from here require SD_MUTEX to not be held, assert this
5851 	 * right up front.
5852 	 */
5853 	ASSERT(!mutex_owned(SD_MUTEX(un)));
5854 	/*
5855 	 * Since the sd device does not have the 'reg' property,
5856 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5857 	 * The following code is to tell cpr that this device
5858 	 * DOES need to be suspended and resumed.
5859 	 */
5860 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5861 	    "pm-hardware-state", "needs-suspend-resume");
5862 
5863 	/*
5864 	 * This complies with the new power management framework
5865 	 * for certain desktop machines. Create the pm_components
5866 	 * property as a string array property.
5867 	 * If un_f_pm_supported is TRUE, that means the disk
5868 	 * attached HBA has set the "pm-capable" property and
5869 	 * the value of this property is bigger than 0.
5870 	 */
5871 	if (un->un_f_pm_supported) {
5872 		/*
5873 		 * not all devices have a motor, try it first.
5874 		 * some devices may return ILLEGAL REQUEST, some
5875 		 * will hang
5876 		 * The following START_STOP_UNIT is used to check if target
5877 		 * device has a motor.
5878 		 */
5879 		un->un_f_start_stop_supported = TRUE;
5880 
5881 		if (un->un_f_power_condition_supported) {
5882 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5883 			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5884 			    SD_PATH_DIRECT);
5885 			if (rval != 0) {
5886 				un->un_f_power_condition_supported = FALSE;
5887 			}
5888 		}
5889 		if (!un->un_f_power_condition_supported) {
5890 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5891 			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5892 		}
5893 		if (rval != 0) {
5894 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5895 			un->un_f_start_stop_supported = FALSE;
5896 		}
5897 
5898 		/*
5899 		 * create pm properties anyways otherwise the parent can't
5900 		 * go to sleep
5901 		 */
5902 		un->un_f_pm_is_enabled = TRUE;
5903 		(void) sd_create_pm_components(devi, un);
5904 
5905 		/*
5906 		 * If it claims that log sense is supported, check it out.
5907 		 */
5908 		if (un->un_f_log_sense_supported) {
5909 			rval = sd_log_page_supported(ssc,
5910 			    START_STOP_CYCLE_PAGE);
5911 			if (rval == 1) {
5912 				/* Page found, use it. */
5913 				un->un_start_stop_cycle_page =
5914 				    START_STOP_CYCLE_PAGE;
5915 			} else {
5916 				/*
5917 				 * Page not found or log sense is not
5918 				 * supported.
5919 				 * Notice we do not check the old style
5920 				 * START_STOP_CYCLE_VU_PAGE because this
5921 				 * code path does not apply to old disks.
5922 				 */
5923 				un->un_f_log_sense_supported = FALSE;
5924 				un->un_f_pm_log_sense_smart = FALSE;
5925 			}
5926 		}
5927 
5928 		return;
5929 	}
5930 
5931 	/*
5932 	 * For the disk whose attached HBA has not set the "pm-capable"
5933 	 * property, check if it supports the power management.
5934 	 */
5935 	if (!un->un_f_log_sense_supported) {
5936 		un->un_power_level = SD_SPINDLE_ON;
5937 		un->un_f_pm_is_enabled = FALSE;
5938 		return;
5939 	}
5940 
5941 	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5942 
5943 #ifdef	SDDEBUG
5944 	if (sd_force_pm_supported) {
5945 		/* Force a successful result */
5946 		rval = 1;
5947 	}
5948 #endif
5949 
5950 	/*
5951 	 * If the start-stop cycle counter log page is not supported
5952 	 * or if the pm-capable property is set to be false (0),
5953 	 * then we should not create the pm_components property.
5954 	 */
5955 	if (rval == -1) {
5956 		/*
5957 		 * Error.
5958 		 * Reading log sense failed, most likely this is
5959 		 * an older drive that does not support log sense.
5960 		 * If this fails auto-pm is not supported.
5961 		 */
5962 		un->un_power_level = SD_SPINDLE_ON;
5963 		un->un_f_pm_is_enabled = FALSE;
5964 
5965 	} else if (rval == 0) {
5966 		/*
5967 		 * Page not found.
5968 		 * The start stop cycle counter is implemented as page
5969 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
5970 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
5971 		 */
5972 		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
5973 			/*
5974 			 * Page found, use this one.
5975 			 */
5976 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
5977 			un->un_f_pm_is_enabled = TRUE;
5978 		} else {
5979 			/*
5980 			 * Error or page not found.
5981 			 * auto-pm is not supported for this device.
5982 			 */
5983 			un->un_power_level = SD_SPINDLE_ON;
5984 			un->un_f_pm_is_enabled = FALSE;
5985 		}
5986 	} else {
5987 		/*
5988 		 * Page found, use it.
5989 		 */
5990 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
5991 		un->un_f_pm_is_enabled = TRUE;
5992 	}
5993 
5994 
5995 	if (un->un_f_pm_is_enabled == TRUE) {
5996 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
5997 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
5998 
5999 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6000 		    log_page_size, un->un_start_stop_cycle_page,
6001 		    0x01, 0, SD_PATH_DIRECT);
6002 
6003 		if (rval != 0) {
6004 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6005 		}
6006 
6007 #ifdef	SDDEBUG
6008 		if (sd_force_pm_supported) {
6009 			/* Force a successful result */
6010 			rval = 0;
6011 		}
6012 #endif
6013 
6014 		/*
6015 		 * If the Log sense for Page( Start/stop cycle counter page)
6016 		 * succeeds, then power management is supported and we can
6017 		 * enable auto-pm.
6018 		 */
6019 		if (rval == 0)  {
6020 			(void) sd_create_pm_components(devi, un);
6021 		} else {
6022 			un->un_power_level = SD_SPINDLE_ON;
6023 			un->un_f_pm_is_enabled = FALSE;
6024 		}
6025 
6026 		kmem_free(log_page_data, log_page_size);
6027 	}
6028 }
6029 
6030 
6031 /*
6032  *    Function: sd_create_pm_components
6033  *
6034  * Description: Initialize PM property.
6035  *
6036  *     Context: Kernel thread context
6037  */
6038 
6039 static void
6040 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6041 {
6042 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6043 
6044 	if (un->un_f_power_condition_supported) {
6045 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6046 		    "pm-components", sd_pwr_pc.pm_comp, 5)
6047 		    != DDI_PROP_SUCCESS) {
6048 			un->un_power_level = SD_SPINDLE_ACTIVE;
6049 			un->un_f_pm_is_enabled = FALSE;
6050 			return;
6051 		}
6052 	} else {
6053 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6054 		    "pm-components", sd_pwr_ss.pm_comp, 3)
6055 		    != DDI_PROP_SUCCESS) {
6056 			un->un_power_level = SD_SPINDLE_ON;
6057 			un->un_f_pm_is_enabled = FALSE;
6058 			return;
6059 		}
6060 	}
6061 	/*
6062 	 * When components are initially created they are idle,
6063 	 * power up any non-removables.
6064 	 * Note: the return value of pm_raise_power can't be used
6065 	 * for determining if PM should be enabled for this device.
6066 	 * Even if you check the return values and remove this
6067 	 * property created above, the PM framework will not honor the
6068 	 * change after the first call to pm_raise_power. Hence,
6069 	 * removal of that property does not help if pm_raise_power
6070 	 * fails. In the case of removable media, the start/stop
6071 	 * will fail if the media is not present.
6072 	 */
6073 	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6074 	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6075 		mutex_enter(SD_MUTEX(un));
6076 		un->un_power_level = SD_PM_STATE_ACTIVE(un);
6077 		mutex_enter(&un->un_pm_mutex);
6078 		/* Set to on and not busy. */
6079 		un->un_pm_count = 0;
6080 	} else {
6081 		mutex_enter(SD_MUTEX(un));
6082 		un->un_power_level = SD_PM_STATE_STOPPED(un);
6083 		mutex_enter(&un->un_pm_mutex);
6084 		/* Set to off. */
6085 		un->un_pm_count = -1;
6086 	}
6087 	mutex_exit(&un->un_pm_mutex);
6088 	mutex_exit(SD_MUTEX(un));
6089 }
6090 
6091 
6092 /*
6093  *    Function: sd_ddi_suspend
6094  *
6095  * Description: Performs system power-down operations. This includes
6096  *		setting the drive state to indicate its suspended so
6097  *		that no new commands will be accepted. Also, wait for
6098  *		all commands that are in transport or queued to a timer
6099  *		for retry to complete. All timeout threads are cancelled.
6100  *
6101  * Return Code: DDI_FAILURE or DDI_SUCCESS
6102  *
6103  *     Context: Kernel thread context
6104  */
6105 
6106 static int
6107 sd_ddi_suspend(dev_info_t *devi)
6108 {
6109 	struct	sd_lun	*un;
6110 	clock_t		wait_cmds_complete;
6111 
6112 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6113 	if (un == NULL) {
6114 		return (DDI_FAILURE);
6115 	}
6116 
6117 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6118 
6119 	mutex_enter(SD_MUTEX(un));
6120 
6121 	/* Return success if the device is already suspended. */
6122 	if (un->un_state == SD_STATE_SUSPENDED) {
6123 		mutex_exit(SD_MUTEX(un));
6124 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6125 		    "device already suspended, exiting\n");
6126 		return (DDI_SUCCESS);
6127 	}
6128 
6129 	/* Return failure if the device is being used by HA */
6130 	if (un->un_resvd_status &
6131 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6132 		mutex_exit(SD_MUTEX(un));
6133 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6134 		    "device in use by HA, exiting\n");
6135 		return (DDI_FAILURE);
6136 	}
6137 
6138 	/*
6139 	 * Return failure if the device is in a resource wait
6140 	 * or power changing state.
6141 	 */
6142 	if ((un->un_state == SD_STATE_RWAIT) ||
6143 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6144 		mutex_exit(SD_MUTEX(un));
6145 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6146 		    "device in resource wait state, exiting\n");
6147 		return (DDI_FAILURE);
6148 	}
6149 
6150 
6151 	un->un_save_state = un->un_last_state;
6152 	New_state(un, SD_STATE_SUSPENDED);
6153 
6154 	/*
6155 	 * Wait for all commands that are in transport or queued to a timer
6156 	 * for retry to complete.
6157 	 *
6158 	 * While waiting, no new commands will be accepted or sent because of
6159 	 * the new state we set above.
6160 	 *
6161 	 * Wait till current operation has completed. If we are in the resource
6162 	 * wait state (with an intr outstanding) then we need to wait till the
6163 	 * intr completes and starts the next cmd. We want to wait for
6164 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6165 	 */
6166 	wait_cmds_complete = ddi_get_lbolt() +
6167 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6168 
6169 	while (un->un_ncmds_in_transport != 0) {
6170 		/*
6171 		 * Fail if commands do not finish in the specified time.
6172 		 */
6173 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6174 		    wait_cmds_complete) == -1) {
6175 			/*
6176 			 * Undo the state changes made above. Everything
6177 			 * must go back to it's original value.
6178 			 */
6179 			Restore_state(un);
6180 			un->un_last_state = un->un_save_state;
6181 			/* Wake up any threads that might be waiting. */
6182 			cv_broadcast(&un->un_suspend_cv);
6183 			mutex_exit(SD_MUTEX(un));
6184 			SD_ERROR(SD_LOG_IO_PM, un,
6185 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6186 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6187 			return (DDI_FAILURE);
6188 		}
6189 	}
6190 
6191 	/*
6192 	 * Cancel SCSI watch thread and timeouts, if any are active
6193 	 */
6194 
6195 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6196 		opaque_t temp_token = un->un_swr_token;
6197 		mutex_exit(SD_MUTEX(un));
6198 		scsi_watch_suspend(temp_token);
6199 		mutex_enter(SD_MUTEX(un));
6200 	}
6201 
6202 	if (un->un_reset_throttle_timeid != NULL) {
6203 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6204 		un->un_reset_throttle_timeid = NULL;
6205 		mutex_exit(SD_MUTEX(un));
6206 		(void) untimeout(temp_id);
6207 		mutex_enter(SD_MUTEX(un));
6208 	}
6209 
6210 	if (un->un_dcvb_timeid != NULL) {
6211 		timeout_id_t temp_id = un->un_dcvb_timeid;
6212 		un->un_dcvb_timeid = NULL;
6213 		mutex_exit(SD_MUTEX(un));
6214 		(void) untimeout(temp_id);
6215 		mutex_enter(SD_MUTEX(un));
6216 	}
6217 
6218 	mutex_enter(&un->un_pm_mutex);
6219 	if (un->un_pm_timeid != NULL) {
6220 		timeout_id_t temp_id = un->un_pm_timeid;
6221 		un->un_pm_timeid = NULL;
6222 		mutex_exit(&un->un_pm_mutex);
6223 		mutex_exit(SD_MUTEX(un));
6224 		(void) untimeout(temp_id);
6225 		mutex_enter(SD_MUTEX(un));
6226 	} else {
6227 		mutex_exit(&un->un_pm_mutex);
6228 	}
6229 
6230 	if (un->un_rmw_msg_timeid != NULL) {
6231 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
6232 		un->un_rmw_msg_timeid = NULL;
6233 		mutex_exit(SD_MUTEX(un));
6234 		(void) untimeout(temp_id);
6235 		mutex_enter(SD_MUTEX(un));
6236 	}
6237 
6238 	if (un->un_retry_timeid != NULL) {
6239 		timeout_id_t temp_id = un->un_retry_timeid;
6240 		un->un_retry_timeid = NULL;
6241 		mutex_exit(SD_MUTEX(un));
6242 		(void) untimeout(temp_id);
6243 		mutex_enter(SD_MUTEX(un));
6244 
6245 		if (un->un_retry_bp != NULL) {
6246 			un->un_retry_bp->av_forw = un->un_waitq_headp;
6247 			un->un_waitq_headp = un->un_retry_bp;
6248 			if (un->un_waitq_tailp == NULL) {
6249 				un->un_waitq_tailp = un->un_retry_bp;
6250 			}
6251 			un->un_retry_bp = NULL;
6252 			un->un_retry_statp = NULL;
6253 		}
6254 	}
6255 
6256 	if (un->un_direct_priority_timeid != NULL) {
6257 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6258 		un->un_direct_priority_timeid = NULL;
6259 		mutex_exit(SD_MUTEX(un));
6260 		(void) untimeout(temp_id);
6261 		mutex_enter(SD_MUTEX(un));
6262 	}
6263 
6264 	if (un->un_f_is_fibre == TRUE) {
6265 		/*
6266 		 * Remove callbacks for insert and remove events
6267 		 */
6268 		if (un->un_insert_event != NULL) {
6269 			mutex_exit(SD_MUTEX(un));
6270 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6271 			mutex_enter(SD_MUTEX(un));
6272 			un->un_insert_event = NULL;
6273 		}
6274 
6275 		if (un->un_remove_event != NULL) {
6276 			mutex_exit(SD_MUTEX(un));
6277 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6278 			mutex_enter(SD_MUTEX(un));
6279 			un->un_remove_event = NULL;
6280 		}
6281 	}
6282 
6283 	mutex_exit(SD_MUTEX(un));
6284 
6285 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6286 
6287 	return (DDI_SUCCESS);
6288 }
6289 
6290 
6291 /*
6292  *    Function: sd_ddi_resume
6293  *
6294  * Description: Performs system power-up operations..
6295  *
6296  * Return Code: DDI_SUCCESS
6297  *		DDI_FAILURE
6298  *
6299  *     Context: Kernel thread context
6300  */
6301 
6302 static int
6303 sd_ddi_resume(dev_info_t *devi)
6304 {
6305 	struct	sd_lun	*un;
6306 
6307 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6308 	if (un == NULL) {
6309 		return (DDI_FAILURE);
6310 	}
6311 
6312 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6313 
6314 	mutex_enter(SD_MUTEX(un));
6315 	Restore_state(un);
6316 
6317 	/*
6318 	 * Restore the state which was saved to give the
6319 	 * the right state in un_last_state
6320 	 */
6321 	un->un_last_state = un->un_save_state;
6322 	/*
6323 	 * Note: throttle comes back at full.
6324 	 * Also note: this MUST be done before calling pm_raise_power
6325 	 * otherwise the system can get hung in biowait. The scenario where
6326 	 * this'll happen is under cpr suspend. Writing of the system
6327 	 * state goes through sddump, which writes 0 to un_throttle. If
6328 	 * writing the system state then fails, example if the partition is
6329 	 * too small, then cpr attempts a resume. If throttle isn't restored
6330 	 * from the saved value until after calling pm_raise_power then
6331 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6332 	 * in biowait.
6333 	 */
6334 	un->un_throttle = un->un_saved_throttle;
6335 
6336 	/*
6337 	 * The chance of failure is very rare as the only command done in power
6338 	 * entry point is START command when you transition from 0->1 or
6339 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6340 	 * which suspend was done. Ignore the return value as the resume should
6341 	 * not be failed. In the case of removable media the media need not be
6342 	 * inserted and hence there is a chance that raise power will fail with
6343 	 * media not present.
6344 	 */
6345 	if (un->un_f_attach_spinup) {
6346 		mutex_exit(SD_MUTEX(un));
6347 		(void) pm_raise_power(SD_DEVINFO(un), 0,
6348 		    SD_PM_STATE_ACTIVE(un));
6349 		mutex_enter(SD_MUTEX(un));
6350 	}
6351 
6352 	/*
6353 	 * Don't broadcast to the suspend cv and therefore possibly
6354 	 * start I/O until after power has been restored.
6355 	 */
6356 	cv_broadcast(&un->un_suspend_cv);
6357 	cv_broadcast(&un->un_state_cv);
6358 
6359 	/* restart thread */
6360 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6361 		scsi_watch_resume(un->un_swr_token);
6362 	}
6363 
6364 #if (defined(__fibre))
6365 	if (un->un_f_is_fibre == TRUE) {
6366 		/*
6367 		 * Add callbacks for insert and remove events
6368 		 */
6369 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6370 			sd_init_event_callbacks(un);
6371 		}
6372 	}
6373 #endif
6374 
6375 	/*
6376 	 * Transport any pending commands to the target.
6377 	 *
6378 	 * If this is a low-activity device commands in queue will have to wait
6379 	 * until new commands come in, which may take awhile. Also, we
6380 	 * specifically don't check un_ncmds_in_transport because we know that
6381 	 * there really are no commands in progress after the unit was
6382 	 * suspended and we could have reached the throttle level, been
6383 	 * suspended, and have no new commands coming in for awhile. Highly
6384 	 * unlikely, but so is the low-activity disk scenario.
6385 	 */
6386 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6387 
6388 	sd_start_cmds(un, NULL);
6389 	mutex_exit(SD_MUTEX(un));
6390 
6391 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6392 
6393 	return (DDI_SUCCESS);
6394 }
6395 
6396 
6397 /*
6398  *    Function: sd_pm_state_change
6399  *
6400  * Description: Change the driver power state.
6401  * 		Someone else is required to actually change the driver
6402  * 		power level.
6403  *
6404  *   Arguments: un - driver soft state (unit) structure
6405  *              level - the power level that is changed to
6406  *              flag - to decide how to change the power state
6407  *
6408  * Return Code: DDI_SUCCESS
6409  *
6410  *     Context: Kernel thread context
6411  */
6412 static int
6413 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6414 {
6415 	ASSERT(un != NULL);
6416 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6417 
6418 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6419 	mutex_enter(SD_MUTEX(un));
6420 
6421 	if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6422 		un->un_power_level = level;
6423 		ASSERT(!mutex_owned(&un->un_pm_mutex));
6424 		mutex_enter(&un->un_pm_mutex);
6425 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6426 			un->un_pm_count++;
6427 			ASSERT(un->un_pm_count == 0);
6428 		}
6429 		mutex_exit(&un->un_pm_mutex);
6430 	} else {
6431 		/*
6432 		 * Exit if power management is not enabled for this device,
6433 		 * or if the device is being used by HA.
6434 		 */
6435 		if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6436 		    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6437 			mutex_exit(SD_MUTEX(un));
6438 			SD_TRACE(SD_LOG_POWER, un,
6439 			    "sd_pm_state_change: exiting\n");
6440 			return (DDI_FAILURE);
6441 		}
6442 
6443 		SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6444 		    "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6445 
6446 		/*
6447 		 * See if the device is not busy, ie.:
6448 		 *    - we have no commands in the driver for this device
6449 		 *    - not waiting for resources
6450 		 */
6451 		if ((un->un_ncmds_in_driver == 0) &&
6452 		    (un->un_state != SD_STATE_RWAIT)) {
6453 			/*
6454 			 * The device is not busy, so it is OK to go to low
6455 			 * power state. Indicate low power, but rely on someone
6456 			 * else to actually change it.
6457 			 */
6458 			mutex_enter(&un->un_pm_mutex);
6459 			un->un_pm_count = -1;
6460 			mutex_exit(&un->un_pm_mutex);
6461 			un->un_power_level = level;
6462 		}
6463 	}
6464 
6465 	mutex_exit(SD_MUTEX(un));
6466 
6467 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6468 
6469 	return (DDI_SUCCESS);
6470 }
6471 
6472 
6473 /*
6474  *    Function: sd_pm_idletimeout_handler
6475  *
6476  * Description: A timer routine that's active only while a device is busy.
6477  *		The purpose is to extend slightly the pm framework's busy
6478  *		view of the device to prevent busy/idle thrashing for
6479  *		back-to-back commands. Do this by comparing the current time
6480  *		to the time at which the last command completed and when the
6481  *		difference is greater than sd_pm_idletime, call
6482  *		pm_idle_component. In addition to indicating idle to the pm
6483  *		framework, update the chain type to again use the internal pm
6484  *		layers of the driver.
6485  *
6486  *   Arguments: arg - driver soft state (unit) structure
6487  *
6488  *     Context: Executes in a timeout(9F) thread context
6489  */
6490 
6491 static void
6492 sd_pm_idletimeout_handler(void *arg)
6493 {
6494 	struct sd_lun *un = arg;
6495 
6496 	time_t	now;
6497 
6498 	mutex_enter(&sd_detach_mutex);
6499 	if (un->un_detach_count != 0) {
6500 		/* Abort if the instance is detaching */
6501 		mutex_exit(&sd_detach_mutex);
6502 		return;
6503 	}
6504 	mutex_exit(&sd_detach_mutex);
6505 
6506 	now = ddi_get_time();
6507 	/*
6508 	 * Grab both mutexes, in the proper order, since we're accessing
6509 	 * both PM and softstate variables.
6510 	 */
6511 	mutex_enter(SD_MUTEX(un));
6512 	mutex_enter(&un->un_pm_mutex);
6513 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
6514 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6515 		/*
6516 		 * Update the chain types.
6517 		 * This takes affect on the next new command received.
6518 		 */
6519 		if (un->un_f_non_devbsize_supported) {
6520 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6521 		} else {
6522 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6523 		}
6524 		un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6525 
6526 		SD_TRACE(SD_LOG_IO_PM, un,
6527 		    "sd_pm_idletimeout_handler: idling device\n");
6528 		(void) pm_idle_component(SD_DEVINFO(un), 0);
6529 		un->un_pm_idle_timeid = NULL;
6530 	} else {
6531 		un->un_pm_idle_timeid =
6532 		    timeout(sd_pm_idletimeout_handler, un,
6533 		    (drv_usectohz((clock_t)300000))); /* 300 ms. */
6534 	}
6535 	mutex_exit(&un->un_pm_mutex);
6536 	mutex_exit(SD_MUTEX(un));
6537 }
6538 
6539 
6540 /*
6541  *    Function: sd_pm_timeout_handler
6542  *
6543  * Description: Callback to tell framework we are idle.
6544  *
6545  *     Context: timeout(9f) thread context.
6546  */
6547 
6548 static void
6549 sd_pm_timeout_handler(void *arg)
6550 {
6551 	struct sd_lun *un = arg;
6552 
6553 	(void) pm_idle_component(SD_DEVINFO(un), 0);
6554 	mutex_enter(&un->un_pm_mutex);
6555 	un->un_pm_timeid = NULL;
6556 	mutex_exit(&un->un_pm_mutex);
6557 }
6558 
6559 
6560 /*
6561  *    Function: sdpower
6562  *
6563  * Description: PM entry point.
6564  *
6565  * Return Code: DDI_SUCCESS
6566  *		DDI_FAILURE
6567  *
6568  *     Context: Kernel thread context
6569  */
6570 
6571 static int
6572 sdpower(dev_info_t *devi, int component, int level)
6573 {
6574 	struct sd_lun	*un;
6575 	int		instance;
6576 	int		rval = DDI_SUCCESS;
6577 	uint_t		i, log_page_size, maxcycles, ncycles;
6578 	uchar_t		*log_page_data;
6579 	int		log_sense_page;
6580 	int		medium_present;
6581 	time_t		intvlp;
6582 	struct pm_trans_data	sd_pm_tran_data;
6583 	uchar_t		save_state;
6584 	int		sval;
6585 	uchar_t		state_before_pm;
6586 	int		got_semaphore_here;
6587 	sd_ssc_t	*ssc;
6588 	int	last_power_level;
6589 
6590 	instance = ddi_get_instance(devi);
6591 
6592 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6593 	    !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6594 		return (DDI_FAILURE);
6595 	}
6596 
6597 	ssc = sd_ssc_init(un);
6598 
6599 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6600 
6601 	/*
6602 	 * Must synchronize power down with close.
6603 	 * Attempt to decrement/acquire the open/close semaphore,
6604 	 * but do NOT wait on it. If it's not greater than zero,
6605 	 * ie. it can't be decremented without waiting, then
6606 	 * someone else, either open or close, already has it
6607 	 * and the try returns 0. Use that knowledge here to determine
6608 	 * if it's OK to change the device power level.
6609 	 * Also, only increment it on exit if it was decremented, ie. gotten,
6610 	 * here.
6611 	 */
6612 	got_semaphore_here = sema_tryp(&un->un_semoclose);
6613 
6614 	mutex_enter(SD_MUTEX(un));
6615 
6616 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6617 	    un->un_ncmds_in_driver);
6618 
6619 	/*
6620 	 * If un_ncmds_in_driver is non-zero it indicates commands are
6621 	 * already being processed in the driver, or if the semaphore was
6622 	 * not gotten here it indicates an open or close is being processed.
6623 	 * At the same time somebody is requesting to go to a lower power
6624 	 * that can't perform I/O, which can't happen, therefore we need to
6625 	 * return failure.
6626 	 */
6627 	if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6628 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6629 		mutex_exit(SD_MUTEX(un));
6630 
6631 		if (got_semaphore_here != 0) {
6632 			sema_v(&un->un_semoclose);
6633 		}
6634 		SD_TRACE(SD_LOG_IO_PM, un,
6635 		    "sdpower: exit, device has queued cmds.\n");
6636 
6637 		goto sdpower_failed;
6638 	}
6639 
6640 	/*
6641 	 * if it is OFFLINE that means the disk is completely dead
6642 	 * in our case we have to put the disk in on or off by sending commands
6643 	 * Of course that will fail anyway so return back here.
6644 	 *
6645 	 * Power changes to a device that's OFFLINE or SUSPENDED
6646 	 * are not allowed.
6647 	 */
6648 	if ((un->un_state == SD_STATE_OFFLINE) ||
6649 	    (un->un_state == SD_STATE_SUSPENDED)) {
6650 		mutex_exit(SD_MUTEX(un));
6651 
6652 		if (got_semaphore_here != 0) {
6653 			sema_v(&un->un_semoclose);
6654 		}
6655 		SD_TRACE(SD_LOG_IO_PM, un,
6656 		    "sdpower: exit, device is off-line.\n");
6657 
6658 		goto sdpower_failed;
6659 	}
6660 
6661 	/*
6662 	 * Change the device's state to indicate it's power level
6663 	 * is being changed. Do this to prevent a power off in the
6664 	 * middle of commands, which is especially bad on devices
6665 	 * that are really powered off instead of just spun down.
6666 	 */
6667 	state_before_pm = un->un_state;
6668 	un->un_state = SD_STATE_PM_CHANGING;
6669 
6670 	mutex_exit(SD_MUTEX(un));
6671 
6672 	/*
6673 	 * If log sense command is not supported, bypass the
6674 	 * following checking, otherwise, check the log sense
6675 	 * information for this device.
6676 	 */
6677 	if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6678 	    un->un_f_log_sense_supported) {
6679 		/*
6680 		 * Get the log sense information to understand whether the
6681 		 * the powercycle counts have gone beyond the threshhold.
6682 		 */
6683 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6684 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6685 
6686 		mutex_enter(SD_MUTEX(un));
6687 		log_sense_page = un->un_start_stop_cycle_page;
6688 		mutex_exit(SD_MUTEX(un));
6689 
6690 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6691 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6692 
6693 		if (rval != 0) {
6694 			if (rval == EIO)
6695 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6696 			else
6697 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6698 		}
6699 
6700 #ifdef	SDDEBUG
6701 		if (sd_force_pm_supported) {
6702 			/* Force a successful result */
6703 			rval = 0;
6704 		}
6705 #endif
6706 		if (rval != 0) {
6707 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6708 			    "Log Sense Failed\n");
6709 
6710 			kmem_free(log_page_data, log_page_size);
6711 			/* Cannot support power management on those drives */
6712 
6713 			if (got_semaphore_here != 0) {
6714 				sema_v(&un->un_semoclose);
6715 			}
6716 			/*
6717 			 * On exit put the state back to it's original value
6718 			 * and broadcast to anyone waiting for the power
6719 			 * change completion.
6720 			 */
6721 			mutex_enter(SD_MUTEX(un));
6722 			un->un_state = state_before_pm;
6723 			cv_broadcast(&un->un_suspend_cv);
6724 			mutex_exit(SD_MUTEX(un));
6725 			SD_TRACE(SD_LOG_IO_PM, un,
6726 			    "sdpower: exit, Log Sense Failed.\n");
6727 
6728 			goto sdpower_failed;
6729 		}
6730 
6731 		/*
6732 		 * From the page data - Convert the essential information to
6733 		 * pm_trans_data
6734 		 */
6735 		maxcycles =
6736 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6737 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6738 
6739 		ncycles =
6740 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6741 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
6742 
6743 		if (un->un_f_pm_log_sense_smart) {
6744 			sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6745 			sd_pm_tran_data.un.smart_count.consumed = ncycles;
6746 			sd_pm_tran_data.un.smart_count.flag = 0;
6747 			sd_pm_tran_data.format = DC_SMART_FORMAT;
6748 		} else {
6749 			sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6750 			sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6751 			for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6752 				sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6753 				    log_page_data[8+i];
6754 			}
6755 			sd_pm_tran_data.un.scsi_cycles.flag = 0;
6756 			sd_pm_tran_data.format = DC_SCSI_FORMAT;
6757 		}
6758 
6759 		kmem_free(log_page_data, log_page_size);
6760 
6761 		/*
6762 		 * Call pm_trans_check routine to get the Ok from
6763 		 * the global policy
6764 		 */
6765 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6766 #ifdef	SDDEBUG
6767 		if (sd_force_pm_supported) {
6768 			/* Force a successful result */
6769 			rval = 1;
6770 		}
6771 #endif
6772 		switch (rval) {
6773 		case 0:
6774 			/*
6775 			 * Not Ok to Power cycle or error in parameters passed
6776 			 * Would have given the advised time to consider power
6777 			 * cycle. Based on the new intvlp parameter we are
6778 			 * supposed to pretend we are busy so that pm framework
6779 			 * will never call our power entry point. Because of
6780 			 * that install a timeout handler and wait for the
6781 			 * recommended time to elapse so that power management
6782 			 * can be effective again.
6783 			 *
6784 			 * To effect this behavior, call pm_busy_component to
6785 			 * indicate to the framework this device is busy.
6786 			 * By not adjusting un_pm_count the rest of PM in
6787 			 * the driver will function normally, and independent
6788 			 * of this but because the framework is told the device
6789 			 * is busy it won't attempt powering down until it gets
6790 			 * a matching idle. The timeout handler sends this.
6791 			 * Note: sd_pm_entry can't be called here to do this
6792 			 * because sdpower may have been called as a result
6793 			 * of a call to pm_raise_power from within sd_pm_entry.
6794 			 *
6795 			 * If a timeout handler is already active then
6796 			 * don't install another.
6797 			 */
6798 			mutex_enter(&un->un_pm_mutex);
6799 			if (un->un_pm_timeid == NULL) {
6800 				un->un_pm_timeid =
6801 				    timeout(sd_pm_timeout_handler,
6802 				    un, intvlp * drv_usectohz(1000000));
6803 				mutex_exit(&un->un_pm_mutex);
6804 				(void) pm_busy_component(SD_DEVINFO(un), 0);
6805 			} else {
6806 				mutex_exit(&un->un_pm_mutex);
6807 			}
6808 			if (got_semaphore_here != 0) {
6809 				sema_v(&un->un_semoclose);
6810 			}
6811 			/*
6812 			 * On exit put the state back to it's original value
6813 			 * and broadcast to anyone waiting for the power
6814 			 * change completion.
6815 			 */
6816 			mutex_enter(SD_MUTEX(un));
6817 			un->un_state = state_before_pm;
6818 			cv_broadcast(&un->un_suspend_cv);
6819 			mutex_exit(SD_MUTEX(un));
6820 
6821 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6822 			    "trans check Failed, not ok to power cycle.\n");
6823 
6824 			goto sdpower_failed;
6825 		case -1:
6826 			if (got_semaphore_here != 0) {
6827 				sema_v(&un->un_semoclose);
6828 			}
6829 			/*
6830 			 * On exit put the state back to it's original value
6831 			 * and broadcast to anyone waiting for the power
6832 			 * change completion.
6833 			 */
6834 			mutex_enter(SD_MUTEX(un));
6835 			un->un_state = state_before_pm;
6836 			cv_broadcast(&un->un_suspend_cv);
6837 			mutex_exit(SD_MUTEX(un));
6838 			SD_TRACE(SD_LOG_IO_PM, un,
6839 			    "sdpower: exit, trans check command Failed.\n");
6840 
6841 			goto sdpower_failed;
6842 		}
6843 	}
6844 
6845 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6846 		/*
6847 		 * Save the last state... if the STOP FAILS we need it
6848 		 * for restoring
6849 		 */
6850 		mutex_enter(SD_MUTEX(un));
6851 		save_state = un->un_last_state;
6852 		last_power_level = un->un_power_level;
6853 		/*
6854 		 * There must not be any cmds. getting processed
6855 		 * in the driver when we get here. Power to the
6856 		 * device is potentially going off.
6857 		 */
6858 		ASSERT(un->un_ncmds_in_driver == 0);
6859 		mutex_exit(SD_MUTEX(un));
6860 
6861 		/*
6862 		 * For now PM suspend the device completely before spindle is
6863 		 * turned off
6864 		 */
6865 		if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6866 		    == DDI_FAILURE) {
6867 			if (got_semaphore_here != 0) {
6868 				sema_v(&un->un_semoclose);
6869 			}
6870 			/*
6871 			 * On exit put the state back to it's original value
6872 			 * and broadcast to anyone waiting for the power
6873 			 * change completion.
6874 			 */
6875 			mutex_enter(SD_MUTEX(un));
6876 			un->un_state = state_before_pm;
6877 			un->un_power_level = last_power_level;
6878 			cv_broadcast(&un->un_suspend_cv);
6879 			mutex_exit(SD_MUTEX(un));
6880 			SD_TRACE(SD_LOG_IO_PM, un,
6881 			    "sdpower: exit, PM suspend Failed.\n");
6882 
6883 			goto sdpower_failed;
6884 		}
6885 	}
6886 
6887 	/*
6888 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6889 	 * close, or strategy. Dump no long uses this routine, it uses it's
6890 	 * own code so it can be done in polled mode.
6891 	 */
6892 
6893 	medium_present = TRUE;
6894 
6895 	/*
6896 	 * When powering up, issue a TUR in case the device is at unit
6897 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
6898 	 * a deadlock on un_pm_busy_cv will occur.
6899 	 */
6900 	if (SD_PM_IS_IO_CAPABLE(un, level)) {
6901 		sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6902 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6903 		if (sval != 0)
6904 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6905 	}
6906 
6907 	if (un->un_f_power_condition_supported) {
6908 		char *pm_condition_name[] = {"STOPPED", "STANDBY",
6909 		    "IDLE", "ACTIVE"};
6910 		SD_TRACE(SD_LOG_IO_PM, un,
6911 		    "sdpower: sending \'%s\' power condition",
6912 		    pm_condition_name[level]);
6913 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6914 		    sd_pl2pc[level], SD_PATH_DIRECT);
6915 	} else {
6916 		SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6917 		    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6918 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6919 		    ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6920 		    SD_TARGET_STOP), SD_PATH_DIRECT);
6921 	}
6922 	if (sval != 0) {
6923 		if (sval == EIO)
6924 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6925 		else
6926 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6927 	}
6928 
6929 	/* Command failed, check for media present. */
6930 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
6931 		medium_present = FALSE;
6932 	}
6933 
6934 	/*
6935 	 * The conditions of interest here are:
6936 	 *   if a spindle off with media present fails,
6937 	 *	then restore the state and return an error.
6938 	 *   else if a spindle on fails,
6939 	 *	then return an error (there's no state to restore).
6940 	 * In all other cases we setup for the new state
6941 	 * and return success.
6942 	 */
6943 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6944 		if ((medium_present == TRUE) && (sval != 0)) {
6945 			/* The stop command from above failed */
6946 			rval = DDI_FAILURE;
6947 			/*
6948 			 * The stop command failed, and we have media
6949 			 * present. Put the level back by calling the
6950 			 * sd_pm_resume() and set the state back to
6951 			 * it's previous value.
6952 			 */
6953 			(void) sd_pm_state_change(un, last_power_level,
6954 			    SD_PM_STATE_ROLLBACK);
6955 			mutex_enter(SD_MUTEX(un));
6956 			un->un_last_state = save_state;
6957 			mutex_exit(SD_MUTEX(un));
6958 		} else if (un->un_f_monitor_media_state) {
6959 			/*
6960 			 * The stop command from above succeeded.
6961 			 * Terminate watch thread in case of removable media
6962 			 * devices going into low power state. This is as per
6963 			 * the requirements of pm framework, otherwise commands
6964 			 * will be generated for the device (through watch
6965 			 * thread), even when the device is in low power state.
6966 			 */
6967 			mutex_enter(SD_MUTEX(un));
6968 			un->un_f_watcht_stopped = FALSE;
6969 			if (un->un_swr_token != NULL) {
6970 				opaque_t temp_token = un->un_swr_token;
6971 				un->un_f_watcht_stopped = TRUE;
6972 				un->un_swr_token = NULL;
6973 				mutex_exit(SD_MUTEX(un));
6974 				(void) scsi_watch_request_terminate(temp_token,
6975 				    SCSI_WATCH_TERMINATE_ALL_WAIT);
6976 			} else {
6977 				mutex_exit(SD_MUTEX(un));
6978 			}
6979 		}
6980 	} else {
6981 		/*
6982 		 * The level requested is I/O capable.
6983 		 * Legacy behavior: return success on a failed spinup
6984 		 * if there is no media in the drive.
6985 		 * Do this by looking at medium_present here.
6986 		 */
6987 		if ((sval != 0) && medium_present) {
6988 			/* The start command from above failed */
6989 			rval = DDI_FAILURE;
6990 		} else {
6991 			/*
6992 			 * The start command from above succeeded
6993 			 * PM resume the devices now that we have
6994 			 * started the disks
6995 			 */
6996 			(void) sd_pm_state_change(un, level,
6997 			    SD_PM_STATE_CHANGE);
6998 
6999 			/*
7000 			 * Resume the watch thread since it was suspended
7001 			 * when the device went into low power mode.
7002 			 */
7003 			if (un->un_f_monitor_media_state) {
7004 				mutex_enter(SD_MUTEX(un));
7005 				if (un->un_f_watcht_stopped == TRUE) {
7006 					opaque_t temp_token;
7007 
7008 					un->un_f_watcht_stopped = FALSE;
7009 					mutex_exit(SD_MUTEX(un));
7010 					temp_token =
7011 					    sd_watch_request_submit(un);
7012 					mutex_enter(SD_MUTEX(un));
7013 					un->un_swr_token = temp_token;
7014 				}
7015 				mutex_exit(SD_MUTEX(un));
7016 			}
7017 		}
7018 	}
7019 
7020 	if (got_semaphore_here != 0) {
7021 		sema_v(&un->un_semoclose);
7022 	}
7023 	/*
7024 	 * On exit put the state back to it's original value
7025 	 * and broadcast to anyone waiting for the power
7026 	 * change completion.
7027 	 */
7028 	mutex_enter(SD_MUTEX(un));
7029 	un->un_state = state_before_pm;
7030 	cv_broadcast(&un->un_suspend_cv);
7031 	mutex_exit(SD_MUTEX(un));
7032 
7033 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7034 
7035 	sd_ssc_fini(ssc);
7036 	return (rval);
7037 
7038 sdpower_failed:
7039 
7040 	sd_ssc_fini(ssc);
7041 	return (DDI_FAILURE);
7042 }
7043 
7044 
7045 
7046 /*
7047  *    Function: sdattach
7048  *
7049  * Description: Driver's attach(9e) entry point function.
7050  *
7051  *   Arguments: devi - opaque device info handle
7052  *		cmd  - attach  type
7053  *
7054  * Return Code: DDI_SUCCESS
7055  *		DDI_FAILURE
7056  *
7057  *     Context: Kernel thread context
7058  */
7059 
7060 static int
7061 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7062 {
7063 	switch (cmd) {
7064 	case DDI_ATTACH:
7065 		return (sd_unit_attach(devi));
7066 	case DDI_RESUME:
7067 		return (sd_ddi_resume(devi));
7068 	default:
7069 		break;
7070 	}
7071 	return (DDI_FAILURE);
7072 }
7073 
7074 
7075 /*
7076  *    Function: sddetach
7077  *
7078  * Description: Driver's detach(9E) entry point function.
7079  *
7080  *   Arguments: devi - opaque device info handle
7081  *		cmd  - detach  type
7082  *
7083  * Return Code: DDI_SUCCESS
7084  *		DDI_FAILURE
7085  *
7086  *     Context: Kernel thread context
7087  */
7088 
7089 static int
7090 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7091 {
7092 	switch (cmd) {
7093 	case DDI_DETACH:
7094 		return (sd_unit_detach(devi));
7095 	case DDI_SUSPEND:
7096 		return (sd_ddi_suspend(devi));
7097 	default:
7098 		break;
7099 	}
7100 	return (DDI_FAILURE);
7101 }
7102 
7103 
7104 /*
7105  *     Function: sd_sync_with_callback
7106  *
7107  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7108  *		 state while the callback routine is active.
7109  *
7110  *    Arguments: un: softstate structure for the instance
7111  *
7112  *	Context: Kernel thread context
7113  */
7114 
7115 static void
7116 sd_sync_with_callback(struct sd_lun *un)
7117 {
7118 	ASSERT(un != NULL);
7119 
7120 	mutex_enter(SD_MUTEX(un));
7121 
7122 	ASSERT(un->un_in_callback >= 0);
7123 
7124 	while (un->un_in_callback > 0) {
7125 		mutex_exit(SD_MUTEX(un));
7126 		delay(2);
7127 		mutex_enter(SD_MUTEX(un));
7128 	}
7129 
7130 	mutex_exit(SD_MUTEX(un));
7131 }
7132 
7133 /*
7134  *    Function: sd_unit_attach
7135  *
7136  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7137  *		the soft state structure for the device and performs
7138  *		all necessary structure and device initializations.
7139  *
7140  *   Arguments: devi: the system's dev_info_t for the device.
7141  *
7142  * Return Code: DDI_SUCCESS if attach is successful.
7143  *		DDI_FAILURE if any part of the attach fails.
7144  *
7145  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7146  *		Kernel thread context only.  Can sleep.
7147  */
7148 
7149 static int
7150 sd_unit_attach(dev_info_t *devi)
7151 {
7152 	struct	scsi_device	*devp;
7153 	struct	sd_lun		*un;
7154 	char			*variantp;
7155 	char			name_str[48];
7156 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7157 	int	instance;
7158 	int	rval;
7159 	int	wc_enabled;
7160 	int	tgt;
7161 	uint64_t	capacity;
7162 	uint_t		lbasize = 0;
7163 	dev_info_t	*pdip = ddi_get_parent(devi);
7164 	int		offbyone = 0;
7165 	int		geom_label_valid = 0;
7166 	sd_ssc_t	*ssc;
7167 	int		status;
7168 	struct sd_fm_internal	*sfip = NULL;
7169 	int		max_xfer_size;
7170 
7171 	/*
7172 	 * Retrieve the target driver's private data area. This was set
7173 	 * up by the HBA.
7174 	 */
7175 	devp = ddi_get_driver_private(devi);
7176 
7177 	/*
7178 	 * Retrieve the target ID of the device.
7179 	 */
7180 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7181 	    SCSI_ADDR_PROP_TARGET, -1);
7182 
7183 	/*
7184 	 * Since we have no idea what state things were left in by the last
7185 	 * user of the device, set up some 'default' settings, ie. turn 'em
7186 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7187 	 * Do this before the scsi_probe, which sends an inquiry.
7188 	 * This is a fix for bug (4430280).
7189 	 * Of special importance is wide-xfer. The drive could have been left
7190 	 * in wide transfer mode by the last driver to communicate with it,
7191 	 * this includes us. If that's the case, and if the following is not
7192 	 * setup properly or we don't re-negotiate with the drive prior to
7193 	 * transferring data to/from the drive, it causes bus parity errors,
7194 	 * data overruns, and unexpected interrupts. This first occurred when
7195 	 * the fix for bug (4378686) was made.
7196 	 */
7197 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7198 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7199 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7200 
7201 	/*
7202 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7203 	 * on a target. Setting it per lun instance actually sets the
7204 	 * capability of this target, which affects those luns already
7205 	 * attached on the same target. So during attach, we can only disable
7206 	 * this capability only when no other lun has been attached on this
7207 	 * target. By doing this, we assume a target has the same tagged-qing
7208 	 * capability for every lun. The condition can be removed when HBA
7209 	 * is changed to support per lun based tagged-qing capability.
7210 	 */
7211 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7212 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7213 	}
7214 
7215 	/*
7216 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7217 	 * This call will allocate and fill in the scsi_inquiry structure
7218 	 * and point the sd_inq member of the scsi_device structure to it.
7219 	 * If the attach succeeds, then this memory will not be de-allocated
7220 	 * (via scsi_unprobe()) until the instance is detached.
7221 	 */
7222 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7223 		goto probe_failed;
7224 	}
7225 
7226 	/*
7227 	 * Check the device type as specified in the inquiry data and
7228 	 * claim it if it is of a type that we support.
7229 	 */
7230 	switch (devp->sd_inq->inq_dtype) {
7231 	case DTYPE_DIRECT:
7232 		break;
7233 	case DTYPE_RODIRECT:
7234 		break;
7235 	case DTYPE_OPTICAL:
7236 		break;
7237 	case DTYPE_NOTPRESENT:
7238 	default:
7239 		/* Unsupported device type; fail the attach. */
7240 		goto probe_failed;
7241 	}
7242 
7243 	/*
7244 	 * Allocate the soft state structure for this unit.
7245 	 *
7246 	 * We rely upon this memory being set to all zeroes by
7247 	 * ddi_soft_state_zalloc().  We assume that any member of the
7248 	 * soft state structure that is not explicitly initialized by
7249 	 * this routine will have a value of zero.
7250 	 */
7251 	instance = ddi_get_instance(devp->sd_dev);
7252 #ifndef XPV_HVM_DRIVER
7253 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7254 		goto probe_failed;
7255 	}
7256 #endif /* !XPV_HVM_DRIVER */
7257 
7258 	/*
7259 	 * Retrieve a pointer to the newly-allocated soft state.
7260 	 *
7261 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7262 	 * was successful, unless something has gone horribly wrong and the
7263 	 * ddi's soft state internals are corrupt (in which case it is
7264 	 * probably better to halt here than just fail the attach....)
7265 	 */
7266 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7267 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7268 		    instance);
7269 		/*NOTREACHED*/
7270 	}
7271 
7272 	/*
7273 	 * Link the back ptr of the driver soft state to the scsi_device
7274 	 * struct for this lun.
7275 	 * Save a pointer to the softstate in the driver-private area of
7276 	 * the scsi_device struct.
7277 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7278 	 * we first set un->un_sd below.
7279 	 */
7280 	un->un_sd = devp;
7281 	devp->sd_private = (opaque_t)un;
7282 
7283 	/*
7284 	 * The following must be after devp is stored in the soft state struct.
7285 	 */
7286 #ifdef SDDEBUG
7287 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7288 	    "%s_unit_attach: un:0x%p instance:%d\n",
7289 	    ddi_driver_name(devi), un, instance);
7290 #endif
7291 
7292 	/*
7293 	 * Set up the device type and node type (for the minor nodes).
7294 	 * By default we assume that the device can at least support the
7295 	 * Common Command Set. Call it a CD-ROM if it reports itself
7296 	 * as a RODIRECT device.
7297 	 */
7298 	switch (devp->sd_inq->inq_dtype) {
7299 	case DTYPE_RODIRECT:
7300 		un->un_node_type = DDI_NT_CD_CHAN;
7301 		un->un_ctype	 = CTYPE_CDROM;
7302 		break;
7303 	case DTYPE_OPTICAL:
7304 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7305 		un->un_ctype	 = CTYPE_ROD;
7306 		break;
7307 	default:
7308 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7309 		un->un_ctype	 = CTYPE_CCS;
7310 		break;
7311 	}
7312 
7313 	/*
7314 	 * Try to read the interconnect type from the HBA.
7315 	 *
7316 	 * Note: This driver is currently compiled as two binaries, a parallel
7317 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7318 	 * differences are determined at compile time. In the future a single
7319 	 * binary will be provided and the interconnect type will be used to
7320 	 * differentiate between fibre and parallel scsi behaviors. At that time
7321 	 * it will be necessary for all fibre channel HBAs to support this
7322 	 * property.
7323 	 *
7324 	 * set un_f_is_fiber to TRUE ( default fiber )
7325 	 */
7326 	un->un_f_is_fibre = TRUE;
7327 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7328 	case INTERCONNECT_SSA:
7329 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7330 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7331 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7332 		break;
7333 	case INTERCONNECT_PARALLEL:
7334 		un->un_f_is_fibre = FALSE;
7335 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7336 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7337 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7338 		break;
7339 	case INTERCONNECT_SAS:
7340 		un->un_f_is_fibre = FALSE;
7341 		un->un_interconnect_type = SD_INTERCONNECT_SAS;
7342 		un->un_node_type = DDI_NT_BLOCK_SAS;
7343 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7344 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7345 		break;
7346 	case INTERCONNECT_SATA:
7347 		un->un_f_is_fibre = FALSE;
7348 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
7349 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7350 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7351 		break;
7352 	case INTERCONNECT_FIBRE:
7353 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7354 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7355 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7356 		break;
7357 	case INTERCONNECT_FABRIC:
7358 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7359 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7360 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7361 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7362 		break;
7363 	default:
7364 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7365 		/*
7366 		 * The HBA does not support the "interconnect-type" property
7367 		 * (or did not provide a recognized type).
7368 		 *
7369 		 * Note: This will be obsoleted when a single fibre channel
7370 		 * and parallel scsi driver is delivered. In the meantime the
7371 		 * interconnect type will be set to the platform default.If that
7372 		 * type is not parallel SCSI, it means that we should be
7373 		 * assuming "ssd" semantics. However, here this also means that
7374 		 * the FC HBA is not supporting the "interconnect-type" property
7375 		 * like we expect it to, so log this occurrence.
7376 		 */
7377 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7378 		if (!SD_IS_PARALLEL_SCSI(un)) {
7379 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7380 			    "sd_unit_attach: un:0x%p Assuming "
7381 			    "INTERCONNECT_FIBRE\n", un);
7382 		} else {
7383 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7384 			    "sd_unit_attach: un:0x%p Assuming "
7385 			    "INTERCONNECT_PARALLEL\n", un);
7386 			un->un_f_is_fibre = FALSE;
7387 		}
7388 #else
7389 		/*
7390 		 * Note: This source will be implemented when a single fibre
7391 		 * channel and parallel scsi driver is delivered. The default
7392 		 * will be to assume that if a device does not support the
7393 		 * "interconnect-type" property it is a parallel SCSI HBA and
7394 		 * we will set the interconnect type for parallel scsi.
7395 		 */
7396 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7397 		un->un_f_is_fibre = FALSE;
7398 #endif
7399 		break;
7400 	}
7401 
7402 	if (un->un_f_is_fibre == TRUE) {
7403 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7404 		    SCSI_VERSION_3) {
7405 			switch (un->un_interconnect_type) {
7406 			case SD_INTERCONNECT_FIBRE:
7407 			case SD_INTERCONNECT_SSA:
7408 				un->un_node_type = DDI_NT_BLOCK_WWN;
7409 				break;
7410 			default:
7411 				break;
7412 			}
7413 		}
7414 	}
7415 
7416 	/*
7417 	 * Initialize the Request Sense command for the target
7418 	 */
7419 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7420 		goto alloc_rqs_failed;
7421 	}
7422 
7423 	/*
7424 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7425 	 * with separate binary for sd and ssd.
7426 	 *
7427 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7428 	 * The hardcoded values will go away when Sparc uses 1 binary
7429 	 * for sd and ssd.  This hardcoded values need to match
7430 	 * SD_RETRY_COUNT in sddef.h
7431 	 * The value used is base on interconnect type.
7432 	 * fibre = 3, parallel = 5
7433 	 */
7434 #if defined(__i386) || defined(__amd64)
7435 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7436 #else
7437 	un->un_retry_count = SD_RETRY_COUNT;
7438 #endif
7439 
7440 	/*
7441 	 * Set the per disk retry count to the default number of retries
7442 	 * for disks and CDROMs. This value can be overridden by the
7443 	 * disk property list or an entry in sd.conf.
7444 	 */
7445 	un->un_notready_retry_count =
7446 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7447 	    : DISK_NOT_READY_RETRY_COUNT(un);
7448 
7449 	/*
7450 	 * Set the busy retry count to the default value of un_retry_count.
7451 	 * This can be overridden by entries in sd.conf or the device
7452 	 * config table.
7453 	 */
7454 	un->un_busy_retry_count = un->un_retry_count;
7455 
7456 	/*
7457 	 * Init the reset threshold for retries.  This number determines
7458 	 * how many retries must be performed before a reset can be issued
7459 	 * (for certain error conditions). This can be overridden by entries
7460 	 * in sd.conf or the device config table.
7461 	 */
7462 	un->un_reset_retry_count = (un->un_retry_count / 2);
7463 
7464 	/*
7465 	 * Set the victim_retry_count to the default un_retry_count
7466 	 */
7467 	un->un_victim_retry_count = (2 * un->un_retry_count);
7468 
7469 	/*
7470 	 * Set the reservation release timeout to the default value of
7471 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7472 	 * device config table.
7473 	 */
7474 	un->un_reserve_release_time = 5;
7475 
7476 	/*
7477 	 * Set up the default maximum transfer size. Note that this may
7478 	 * get updated later in the attach, when setting up default wide
7479 	 * operations for disks.
7480 	 */
7481 #if defined(__i386) || defined(__amd64)
7482 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7483 	un->un_partial_dma_supported = 1;
7484 #else
7485 	un->un_max_xfer_size = (uint_t)maxphys;
7486 #endif
7487 
7488 	/*
7489 	 * Get "allow bus device reset" property (defaults to "enabled" if
7490 	 * the property was not defined). This is to disable bus resets for
7491 	 * certain kinds of error recovery. Note: In the future when a run-time
7492 	 * fibre check is available the soft state flag should default to
7493 	 * enabled.
7494 	 */
7495 	if (un->un_f_is_fibre == TRUE) {
7496 		un->un_f_allow_bus_device_reset = TRUE;
7497 	} else {
7498 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7499 		    "allow-bus-device-reset", 1) != 0) {
7500 			un->un_f_allow_bus_device_reset = TRUE;
7501 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7502 			    "sd_unit_attach: un:0x%p Bus device reset "
7503 			    "enabled\n", un);
7504 		} else {
7505 			un->un_f_allow_bus_device_reset = FALSE;
7506 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7507 			    "sd_unit_attach: un:0x%p Bus device reset "
7508 			    "disabled\n", un);
7509 		}
7510 	}
7511 
7512 	/*
7513 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7514 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7515 	 *
7516 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7517 	 * property. The new "variant" property with a value of "atapi" has been
7518 	 * introduced so that future 'variants' of standard SCSI behavior (like
7519 	 * atapi) could be specified by the underlying HBA drivers by supplying
7520 	 * a new value for the "variant" property, instead of having to define a
7521 	 * new property.
7522 	 */
7523 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7524 		un->un_f_cfg_is_atapi = TRUE;
7525 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7526 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7527 	}
7528 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7529 	    &variantp) == DDI_PROP_SUCCESS) {
7530 		if (strcmp(variantp, "atapi") == 0) {
7531 			un->un_f_cfg_is_atapi = TRUE;
7532 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7533 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7534 		}
7535 		ddi_prop_free(variantp);
7536 	}
7537 
7538 	un->un_cmd_timeout	= SD_IO_TIME;
7539 
7540 	un->un_busy_timeout  = SD_BSY_TIMEOUT;
7541 
7542 	/* Info on current states, statuses, etc. (Updated frequently) */
7543 	un->un_state		= SD_STATE_NORMAL;
7544 	un->un_last_state	= SD_STATE_NORMAL;
7545 
7546 	/* Control & status info for command throttling */
7547 	un->un_throttle		= sd_max_throttle;
7548 	un->un_saved_throttle	= sd_max_throttle;
7549 	un->un_min_throttle	= sd_min_throttle;
7550 
7551 	if (un->un_f_is_fibre == TRUE) {
7552 		un->un_f_use_adaptive_throttle = TRUE;
7553 	} else {
7554 		un->un_f_use_adaptive_throttle = FALSE;
7555 	}
7556 
7557 	/* Removable media support. */
7558 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7559 	un->un_mediastate		= DKIO_NONE;
7560 	un->un_specified_mediastate	= DKIO_NONE;
7561 
7562 	/* CVs for suspend/resume (PM or DR) */
7563 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7564 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7565 
7566 	/* Power management support. */
7567 	un->un_power_level = SD_SPINDLE_UNINIT;
7568 
7569 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7570 	un->un_f_wcc_inprog = 0;
7571 
7572 	/*
7573 	 * The open/close semaphore is used to serialize threads executing
7574 	 * in the driver's open & close entry point routines for a given
7575 	 * instance.
7576 	 */
7577 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7578 
7579 	/*
7580 	 * The conf file entry and softstate variable is a forceful override,
7581 	 * meaning a non-zero value must be entered to change the default.
7582 	 */
7583 	un->un_f_disksort_disabled = FALSE;
7584 	un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7585 
7586 	/*
7587 	 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7588 	 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7589 	 */
7590 	un->un_f_mmc_gesn_polling = TRUE;
7591 
7592 	/*
7593 	 * Retrieve the properties from the static driver table or the driver
7594 	 * configuration file (.conf) for this unit and update the soft state
7595 	 * for the device as needed for the indicated properties.
7596 	 * Note: the property configuration needs to occur here as some of the
7597 	 * following routines may have dependencies on soft state flags set
7598 	 * as part of the driver property configuration.
7599 	 */
7600 	sd_read_unit_properties(un);
7601 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7602 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7603 
7604 	/*
7605 	 * Only if a device has "hotpluggable" property, it is
7606 	 * treated as hotpluggable device. Otherwise, it is
7607 	 * regarded as non-hotpluggable one.
7608 	 */
7609 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7610 	    -1) != -1) {
7611 		un->un_f_is_hotpluggable = TRUE;
7612 	}
7613 
7614 	/*
7615 	 * set unit's attributes(flags) according to "hotpluggable" and
7616 	 * RMB bit in INQUIRY data.
7617 	 */
7618 	sd_set_unit_attributes(un, devi);
7619 
7620 	/*
7621 	 * By default, we mark the capacity, lbasize, and geometry
7622 	 * as invalid. Only if we successfully read a valid capacity
7623 	 * will we update the un_blockcount and un_tgt_blocksize with the
7624 	 * valid values (the geometry will be validated later).
7625 	 */
7626 	un->un_f_blockcount_is_valid	= FALSE;
7627 	un->un_f_tgt_blocksize_is_valid	= FALSE;
7628 
7629 	/*
7630 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7631 	 * otherwise.
7632 	 */
7633 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7634 	un->un_blockcount = 0;
7635 
7636 	/*
7637 	 * Set up the per-instance info needed to determine the correct
7638 	 * CDBs and other info for issuing commands to the target.
7639 	 */
7640 	sd_init_cdb_limits(un);
7641 
7642 	/*
7643 	 * Set up the IO chains to use, based upon the target type.
7644 	 */
7645 	if (un->un_f_non_devbsize_supported) {
7646 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7647 	} else {
7648 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7649 	}
7650 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7651 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7652 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7653 
7654 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7655 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7656 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7657 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7658 
7659 
7660 	if (ISCD(un)) {
7661 		un->un_additional_codes = sd_additional_codes;
7662 	} else {
7663 		un->un_additional_codes = NULL;
7664 	}
7665 
7666 	/*
7667 	 * Create the kstats here so they can be available for attach-time
7668 	 * routines that send commands to the unit (either polled or via
7669 	 * sd_send_scsi_cmd).
7670 	 *
7671 	 * Note: This is a critical sequence that needs to be maintained:
7672 	 *	1) Instantiate the kstats here, before any routines using the
7673 	 *	   iopath (i.e. sd_send_scsi_cmd).
7674 	 *	2) Instantiate and initialize the partition stats
7675 	 *	   (sd_set_pstats).
7676 	 *	3) Initialize the error stats (sd_set_errstats), following
7677 	 *	   sd_validate_geometry(),sd_register_devid(),
7678 	 *	   and sd_cache_control().
7679 	 */
7680 
7681 	un->un_stats = kstat_create(sd_label, instance,
7682 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7683 	if (un->un_stats != NULL) {
7684 		un->un_stats->ks_lock = SD_MUTEX(un);
7685 		kstat_install(un->un_stats);
7686 	}
7687 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7688 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
7689 
7690 	sd_create_errstats(un, instance);
7691 	if (un->un_errstats == NULL) {
7692 		goto create_errstats_failed;
7693 	}
7694 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7695 	    "sd_unit_attach: un:0x%p errstats created\n", un);
7696 
7697 	/*
7698 	 * The following if/else code was relocated here from below as part
7699 	 * of the fix for bug (4430280). However with the default setup added
7700 	 * on entry to this routine, it's no longer absolutely necessary for
7701 	 * this to be before the call to sd_spin_up_unit.
7702 	 */
7703 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7704 		int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7705 		    (devp->sd_inq->inq_ansi == 5)) &&
7706 		    devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7707 
7708 		/*
7709 		 * If tagged queueing is supported by the target
7710 		 * and by the host adapter then we will enable it
7711 		 */
7712 		un->un_tagflags = 0;
7713 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7714 		    (un->un_f_arq_enabled == TRUE)) {
7715 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7716 			    1, 1) == 1) {
7717 				un->un_tagflags = FLAG_STAG;
7718 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7719 				    "sd_unit_attach: un:0x%p tag queueing "
7720 				    "enabled\n", un);
7721 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
7722 			    "untagged-qing", 0) == 1) {
7723 				un->un_f_opt_queueing = TRUE;
7724 				un->un_saved_throttle = un->un_throttle =
7725 				    min(un->un_throttle, 3);
7726 			} else {
7727 				un->un_f_opt_queueing = FALSE;
7728 				un->un_saved_throttle = un->un_throttle = 1;
7729 			}
7730 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7731 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
7732 			/* The Host Adapter supports internal queueing. */
7733 			un->un_f_opt_queueing = TRUE;
7734 			un->un_saved_throttle = un->un_throttle =
7735 			    min(un->un_throttle, 3);
7736 		} else {
7737 			un->un_f_opt_queueing = FALSE;
7738 			un->un_saved_throttle = un->un_throttle = 1;
7739 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7740 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
7741 		}
7742 
7743 		/*
7744 		 * Enable large transfers for SATA/SAS drives
7745 		 */
7746 		if (SD_IS_SERIAL(un)) {
7747 			un->un_max_xfer_size =
7748 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7749 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7750 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7751 			    "sd_unit_attach: un:0x%p max transfer "
7752 			    "size=0x%x\n", un, un->un_max_xfer_size);
7753 
7754 		}
7755 
7756 		/* Setup or tear down default wide operations for disks */
7757 
7758 		/*
7759 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7760 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
7761 		 * system and be set to different values. In the future this
7762 		 * code may need to be updated when the ssd module is
7763 		 * obsoleted and removed from the system. (4299588)
7764 		 */
7765 		if (SD_IS_PARALLEL_SCSI(un) &&
7766 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7767 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7768 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7769 			    1, 1) == 1) {
7770 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7771 				    "sd_unit_attach: un:0x%p Wide Transfer "
7772 				    "enabled\n", un);
7773 			}
7774 
7775 			/*
7776 			 * If tagged queuing has also been enabled, then
7777 			 * enable large xfers
7778 			 */
7779 			if (un->un_saved_throttle == sd_max_throttle) {
7780 				un->un_max_xfer_size =
7781 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7782 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7783 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7784 				    "sd_unit_attach: un:0x%p max transfer "
7785 				    "size=0x%x\n", un, un->un_max_xfer_size);
7786 			}
7787 		} else {
7788 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7789 			    0, 1) == 1) {
7790 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7791 				    "sd_unit_attach: un:0x%p "
7792 				    "Wide Transfer disabled\n", un);
7793 			}
7794 		}
7795 	} else {
7796 		un->un_tagflags = FLAG_STAG;
7797 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7798 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7799 	}
7800 
7801 	/*
7802 	 * If this target supports LUN reset, try to enable it.
7803 	 */
7804 	if (un->un_f_lun_reset_enabled) {
7805 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7806 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7807 			    "un:0x%p lun_reset capability set\n", un);
7808 		} else {
7809 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7810 			    "un:0x%p lun-reset capability not set\n", un);
7811 		}
7812 	}
7813 
7814 	/*
7815 	 * Adjust the maximum transfer size. This is to fix
7816 	 * the problem of partial DMA support on SPARC. Some
7817 	 * HBA driver, like aac, has very small dma_attr_maxxfer
7818 	 * size, which requires partial DMA support on SPARC.
7819 	 * In the future the SPARC pci nexus driver may solve
7820 	 * the problem instead of this fix.
7821 	 */
7822 	max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7823 	if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7824 		/* We need DMA partial even on sparc to ensure sddump() works */
7825 		un->un_max_xfer_size = max_xfer_size;
7826 		if (un->un_partial_dma_supported == 0)
7827 			un->un_partial_dma_supported = 1;
7828 	}
7829 	if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7830 	    DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7831 		if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7832 		    un->un_max_xfer_size) == 1) {
7833 			un->un_buf_breakup_supported = 1;
7834 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7835 			    "un:0x%p Buf breakup enabled\n", un);
7836 		}
7837 	}
7838 
7839 	/*
7840 	 * Set PKT_DMA_PARTIAL flag.
7841 	 */
7842 	if (un->un_partial_dma_supported == 1) {
7843 		un->un_pkt_flags = PKT_DMA_PARTIAL;
7844 	} else {
7845 		un->un_pkt_flags = 0;
7846 	}
7847 
7848 	/* Initialize sd_ssc_t for internal uscsi commands */
7849 	ssc = sd_ssc_init(un);
7850 	scsi_fm_init(devp);
7851 
7852 	/*
7853 	 * Allocate memory for SCSI FMA stuffs.
7854 	 */
7855 	un->un_fm_private =
7856 	    kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7857 	sfip = (struct sd_fm_internal *)un->un_fm_private;
7858 	sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7859 	sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7860 	sfip->fm_ssc.ssc_un = un;
7861 
7862 	if (ISCD(un) ||
7863 	    un->un_f_has_removable_media ||
7864 	    devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7865 		/*
7866 		 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7867 		 * Their log are unchanged.
7868 		 */
7869 		sfip->fm_log_level = SD_FM_LOG_NSUP;
7870 	} else {
7871 		/*
7872 		 * If enter here, it should be non-CDROM and FM-capable
7873 		 * device, and it will not keep the old scsi_log as before
7874 		 * in /var/adm/messages. However, the property
7875 		 * "fm-scsi-log" will control whether the FM telemetry will
7876 		 * be logged in /var/adm/messages.
7877 		 */
7878 		int fm_scsi_log;
7879 		fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7880 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7881 
7882 		if (fm_scsi_log)
7883 			sfip->fm_log_level = SD_FM_LOG_EREPORT;
7884 		else
7885 			sfip->fm_log_level = SD_FM_LOG_SILENT;
7886 	}
7887 
7888 	/*
7889 	 * At this point in the attach, we have enough info in the
7890 	 * soft state to be able to issue commands to the target.
7891 	 *
7892 	 * All command paths used below MUST issue their commands as
7893 	 * SD_PATH_DIRECT. This is important as intermediate layers
7894 	 * are not all initialized yet (such as PM).
7895 	 */
7896 
7897 	/*
7898 	 * Send a TEST UNIT READY command to the device. This should clear
7899 	 * any outstanding UNIT ATTENTION that may be present.
7900 	 *
7901 	 * Note: Don't check for success, just track if there is a reservation,
7902 	 * this is a throw away command to clear any unit attentions.
7903 	 *
7904 	 * Note: This MUST be the first command issued to the target during
7905 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
7906 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7907 	 * with attempts at spinning up a device with no media.
7908 	 */
7909 	status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7910 	if (status != 0) {
7911 		if (status == EACCES)
7912 			reservation_flag = SD_TARGET_IS_RESERVED;
7913 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7914 	}
7915 
7916 	/*
7917 	 * If the device is NOT a removable media device, attempt to spin
7918 	 * it up (using the START_STOP_UNIT command) and read its capacity
7919 	 * (using the READ CAPACITY command).  Note, however, that either
7920 	 * of these could fail and in some cases we would continue with
7921 	 * the attach despite the failure (see below).
7922 	 */
7923 	if (un->un_f_descr_format_supported) {
7924 
7925 		switch (sd_spin_up_unit(ssc)) {
7926 		case 0:
7927 			/*
7928 			 * Spin-up was successful; now try to read the
7929 			 * capacity.  If successful then save the results
7930 			 * and mark the capacity & lbasize as valid.
7931 			 */
7932 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7933 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
7934 
7935 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7936 			    &lbasize, SD_PATH_DIRECT);
7937 
7938 			switch (status) {
7939 			case 0: {
7940 				if (capacity > DK_MAX_BLOCKS) {
7941 #ifdef _LP64
7942 					if ((capacity + 1) >
7943 					    SD_GROUP1_MAX_ADDRESS) {
7944 						/*
7945 						 * Enable descriptor format
7946 						 * sense data so that we can
7947 						 * get 64 bit sense data
7948 						 * fields.
7949 						 */
7950 						sd_enable_descr_sense(ssc);
7951 					}
7952 #else
7953 					/* 32-bit kernels can't handle this */
7954 					scsi_log(SD_DEVINFO(un),
7955 					    sd_label, CE_WARN,
7956 					    "disk has %llu blocks, which "
7957 					    "is too large for a 32-bit "
7958 					    "kernel", capacity);
7959 
7960 #if defined(__i386) || defined(__amd64)
7961 					/*
7962 					 * 1TB disk was treated as (1T - 512)B
7963 					 * in the past, so that it might have
7964 					 * valid VTOC and solaris partitions,
7965 					 * we have to allow it to continue to
7966 					 * work.
7967 					 */
7968 					if (capacity -1 > DK_MAX_BLOCKS)
7969 #endif
7970 					goto spinup_failed;
7971 #endif
7972 				}
7973 
7974 				/*
7975 				 * Here it's not necessary to check the case:
7976 				 * the capacity of the device is bigger than
7977 				 * what the max hba cdb can support. Because
7978 				 * sd_send_scsi_READ_CAPACITY will retrieve
7979 				 * the capacity by sending USCSI command, which
7980 				 * is constrained by the max hba cdb. Actually,
7981 				 * sd_send_scsi_READ_CAPACITY will return
7982 				 * EINVAL when using bigger cdb than required
7983 				 * cdb length. Will handle this case in
7984 				 * "case EINVAL".
7985 				 */
7986 
7987 				/*
7988 				 * The following relies on
7989 				 * sd_send_scsi_READ_CAPACITY never
7990 				 * returning 0 for capacity and/or lbasize.
7991 				 */
7992 				sd_update_block_info(un, lbasize, capacity);
7993 
7994 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7995 				    "sd_unit_attach: un:0x%p capacity = %ld "
7996 				    "blocks; lbasize= %ld.\n", un,
7997 				    un->un_blockcount, un->un_tgt_blocksize);
7998 
7999 				break;
8000 			}
8001 			case EINVAL:
8002 				/*
8003 				 * In the case where the max-cdb-length property
8004 				 * is smaller than the required CDB length for
8005 				 * a SCSI device, a target driver can fail to
8006 				 * attach to that device.
8007 				 */
8008 				scsi_log(SD_DEVINFO(un),
8009 				    sd_label, CE_WARN,
8010 				    "disk capacity is too large "
8011 				    "for current cdb length");
8012 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8013 
8014 				goto spinup_failed;
8015 			case EACCES:
8016 				/*
8017 				 * Should never get here if the spin-up
8018 				 * succeeded, but code it in anyway.
8019 				 * From here, just continue with the attach...
8020 				 */
8021 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8022 				    "sd_unit_attach: un:0x%p "
8023 				    "sd_send_scsi_READ_CAPACITY "
8024 				    "returned reservation conflict\n", un);
8025 				reservation_flag = SD_TARGET_IS_RESERVED;
8026 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8027 				break;
8028 			default:
8029 				/*
8030 				 * Likewise, should never get here if the
8031 				 * spin-up succeeded. Just continue with
8032 				 * the attach...
8033 				 */
8034 				if (status == EIO)
8035 					sd_ssc_assessment(ssc,
8036 					    SD_FMT_STATUS_CHECK);
8037 				else
8038 					sd_ssc_assessment(ssc,
8039 					    SD_FMT_IGNORE);
8040 				break;
8041 			}
8042 			break;
8043 		case EACCES:
8044 			/*
8045 			 * Device is reserved by another host.  In this case
8046 			 * we could not spin it up or read the capacity, but
8047 			 * we continue with the attach anyway.
8048 			 */
8049 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8050 			    "sd_unit_attach: un:0x%p spin-up reservation "
8051 			    "conflict.\n", un);
8052 			reservation_flag = SD_TARGET_IS_RESERVED;
8053 			break;
8054 		default:
8055 			/* Fail the attach if the spin-up failed. */
8056 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8057 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8058 			goto spinup_failed;
8059 		}
8060 
8061 	}
8062 
8063 	/*
8064 	 * Check to see if this is a MMC drive
8065 	 */
8066 	if (ISCD(un)) {
8067 		sd_set_mmc_caps(ssc);
8068 	}
8069 
8070 	/*
8071 	 * Add a zero-length attribute to tell the world we support
8072 	 * kernel ioctls (for layered drivers)
8073 	 */
8074 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8075 	    DDI_KERNEL_IOCTL, NULL, 0);
8076 
8077 	/*
8078 	 * Add a boolean property to tell the world we support
8079 	 * the B_FAILFAST flag (for layered drivers)
8080 	 */
8081 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8082 	    "ddi-failfast-supported", NULL, 0);
8083 
8084 	/*
8085 	 * Initialize power management
8086 	 */
8087 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8088 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8089 	sd_setup_pm(ssc, devi);
8090 	if (un->un_f_pm_is_enabled == FALSE) {
8091 		/*
8092 		 * For performance, point to a jump table that does
8093 		 * not include pm.
8094 		 * The direct and priority chains don't change with PM.
8095 		 *
8096 		 * Note: this is currently done based on individual device
8097 		 * capabilities. When an interface for determining system
8098 		 * power enabled state becomes available, or when additional
8099 		 * layers are added to the command chain, these values will
8100 		 * have to be re-evaluated for correctness.
8101 		 */
8102 		if (un->un_f_non_devbsize_supported) {
8103 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8104 		} else {
8105 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8106 		}
8107 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8108 	}
8109 
8110 	/*
8111 	 * This property is set to 0 by HA software to avoid retries
8112 	 * on a reserved disk. (The preferred property name is
8113 	 * "retry-on-reservation-conflict") (1189689)
8114 	 *
8115 	 * Note: The use of a global here can have unintended consequences. A
8116 	 * per instance variable is preferable to match the capabilities of
8117 	 * different underlying hba's (4402600)
8118 	 */
8119 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8120 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8121 	    sd_retry_on_reservation_conflict);
8122 	if (sd_retry_on_reservation_conflict != 0) {
8123 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8124 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8125 		    sd_retry_on_reservation_conflict);
8126 	}
8127 
8128 	/* Set up options for QFULL handling. */
8129 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8130 	    "qfull-retries", -1)) != -1) {
8131 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8132 		    rval, 1);
8133 	}
8134 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8135 	    "qfull-retry-interval", -1)) != -1) {
8136 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8137 		    rval, 1);
8138 	}
8139 
8140 	/*
8141 	 * This just prints a message that announces the existence of the
8142 	 * device. The message is always printed in the system logfile, but
8143 	 * only appears on the console if the system is booted with the
8144 	 * -v (verbose) argument.
8145 	 */
8146 	ddi_report_dev(devi);
8147 
8148 	un->un_mediastate = DKIO_NONE;
8149 
8150 	/*
8151 	 * Check if this is a SSD(Solid State Drive).
8152 	 */
8153 	sd_check_solid_state(ssc);
8154 
8155 	cmlb_alloc_handle(&un->un_cmlbhandle);
8156 
8157 #if defined(__i386) || defined(__amd64)
8158 	/*
8159 	 * On x86, compensate for off-by-1 legacy error
8160 	 */
8161 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8162 	    (lbasize == un->un_sys_blocksize))
8163 		offbyone = CMLB_OFF_BY_ONE;
8164 #endif
8165 
8166 	if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8167 	    VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8168 	    VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8169 	    un->un_node_type, offbyone, un->un_cmlbhandle,
8170 	    (void *)SD_PATH_DIRECT) != 0) {
8171 		goto cmlb_attach_failed;
8172 	}
8173 
8174 
8175 	/*
8176 	 * Read and validate the device's geometry (ie, disk label)
8177 	 * A new unformatted drive will not have a valid geometry, but
8178 	 * the driver needs to successfully attach to this device so
8179 	 * the drive can be formatted via ioctls.
8180 	 */
8181 	geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8182 	    (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8183 
8184 	mutex_enter(SD_MUTEX(un));
8185 
8186 	/*
8187 	 * Read and initialize the devid for the unit.
8188 	 */
8189 	if (un->un_f_devid_supported) {
8190 		sd_register_devid(ssc, devi, reservation_flag);
8191 	}
8192 	mutex_exit(SD_MUTEX(un));
8193 
8194 #if (defined(__fibre))
8195 	/*
8196 	 * Register callbacks for fibre only.  You can't do this solely
8197 	 * on the basis of the devid_type because this is hba specific.
8198 	 * We need to query our hba capabilities to find out whether to
8199 	 * register or not.
8200 	 */
8201 	if (un->un_f_is_fibre) {
8202 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8203 			sd_init_event_callbacks(un);
8204 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8205 			    "sd_unit_attach: un:0x%p event callbacks inserted",
8206 			    un);
8207 		}
8208 	}
8209 #endif
8210 
8211 	if (un->un_f_opt_disable_cache == TRUE) {
8212 		/*
8213 		 * Disable both read cache and write cache.  This is
8214 		 * the historic behavior of the keywords in the config file.
8215 		 */
8216 		if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8217 		    0) {
8218 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8219 			    "sd_unit_attach: un:0x%p Could not disable "
8220 			    "caching", un);
8221 			goto devid_failed;
8222 		}
8223 	}
8224 
8225 	/*
8226 	 * Check the value of the WCE bit now and
8227 	 * set un_f_write_cache_enabled accordingly.
8228 	 */
8229 	(void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8230 	mutex_enter(SD_MUTEX(un));
8231 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8232 	mutex_exit(SD_MUTEX(un));
8233 
8234 	if (un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8235 	    un->un_tgt_blocksize != DEV_BSIZE) {
8236 		if (!(un->un_wm_cache)) {
8237 			(void) snprintf(name_str, sizeof (name_str),
8238 			    "%s%d_cache",
8239 			    ddi_driver_name(SD_DEVINFO(un)),
8240 			    ddi_get_instance(SD_DEVINFO(un)));
8241 			un->un_wm_cache = kmem_cache_create(
8242 			    name_str, sizeof (struct sd_w_map),
8243 			    8, sd_wm_cache_constructor,
8244 			    sd_wm_cache_destructor, NULL,
8245 			    (void *)un, NULL, 0);
8246 			if (!(un->un_wm_cache)) {
8247 				goto wm_cache_failed;
8248 			}
8249 		}
8250 	}
8251 
8252 	/*
8253 	 * Check the value of the NV_SUP bit and set
8254 	 * un_f_suppress_cache_flush accordingly.
8255 	 */
8256 	sd_get_nv_sup(ssc);
8257 
8258 	/*
8259 	 * Find out what type of reservation this disk supports.
8260 	 */
8261 	status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8262 
8263 	switch (status) {
8264 	case 0:
8265 		/*
8266 		 * SCSI-3 reservations are supported.
8267 		 */
8268 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8269 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8270 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8271 		break;
8272 	case ENOTSUP:
8273 		/*
8274 		 * The PERSISTENT RESERVE IN command would not be recognized by
8275 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8276 		 */
8277 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8278 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8279 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8280 
8281 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8282 		break;
8283 	default:
8284 		/*
8285 		 * default to SCSI-3 reservations
8286 		 */
8287 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8288 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8289 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8290 
8291 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8292 		break;
8293 	}
8294 
8295 	/*
8296 	 * Set the pstat and error stat values here, so data obtained during the
8297 	 * previous attach-time routines is available.
8298 	 *
8299 	 * Note: This is a critical sequence that needs to be maintained:
8300 	 *	1) Instantiate the kstats before any routines using the iopath
8301 	 *	   (i.e. sd_send_scsi_cmd).
8302 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8303 	 *	   stats (sd_set_pstats)here, following
8304 	 *	   cmlb_validate_geometry(), sd_register_devid(), and
8305 	 *	   sd_cache_control().
8306 	 */
8307 
8308 	if (un->un_f_pkstats_enabled && geom_label_valid) {
8309 		sd_set_pstats(un);
8310 		SD_TRACE(SD_LOG_IO_PARTITION, un,
8311 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8312 	}
8313 
8314 	sd_set_errstats(un);
8315 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8316 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8317 
8318 
8319 	/*
8320 	 * After successfully attaching an instance, we record the information
8321 	 * of how many luns have been attached on the relative target and
8322 	 * controller for parallel SCSI. This information is used when sd tries
8323 	 * to set the tagged queuing capability in HBA.
8324 	 */
8325 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8326 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8327 	}
8328 
8329 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8330 	    "sd_unit_attach: un:0x%p exit success\n", un);
8331 
8332 	/* Uninitialize sd_ssc_t pointer */
8333 	sd_ssc_fini(ssc);
8334 
8335 	return (DDI_SUCCESS);
8336 
8337 	/*
8338 	 * An error occurred during the attach; clean up & return failure.
8339 	 */
8340 wm_cache_failed:
8341 devid_failed:
8342 
8343 setup_pm_failed:
8344 	ddi_remove_minor_node(devi, NULL);
8345 
8346 cmlb_attach_failed:
8347 	/*
8348 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8349 	 */
8350 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8351 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8352 
8353 	/*
8354 	 * Refer to the comments of setting tagged-qing in the beginning of
8355 	 * sd_unit_attach. We can only disable tagged queuing when there is
8356 	 * no lun attached on the target.
8357 	 */
8358 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8359 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8360 	}
8361 
8362 	if (un->un_f_is_fibre == FALSE) {
8363 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8364 	}
8365 
8366 spinup_failed:
8367 
8368 	/* Uninitialize sd_ssc_t pointer */
8369 	sd_ssc_fini(ssc);
8370 
8371 	mutex_enter(SD_MUTEX(un));
8372 
8373 	/* Deallocate SCSI FMA memory spaces */
8374 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8375 
8376 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8377 	if (un->un_direct_priority_timeid != NULL) {
8378 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8379 		un->un_direct_priority_timeid = NULL;
8380 		mutex_exit(SD_MUTEX(un));
8381 		(void) untimeout(temp_id);
8382 		mutex_enter(SD_MUTEX(un));
8383 	}
8384 
8385 	/* Cancel any pending start/stop timeouts */
8386 	if (un->un_startstop_timeid != NULL) {
8387 		timeout_id_t temp_id = un->un_startstop_timeid;
8388 		un->un_startstop_timeid = NULL;
8389 		mutex_exit(SD_MUTEX(un));
8390 		(void) untimeout(temp_id);
8391 		mutex_enter(SD_MUTEX(un));
8392 	}
8393 
8394 	/* Cancel any pending reset-throttle timeouts */
8395 	if (un->un_reset_throttle_timeid != NULL) {
8396 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8397 		un->un_reset_throttle_timeid = NULL;
8398 		mutex_exit(SD_MUTEX(un));
8399 		(void) untimeout(temp_id);
8400 		mutex_enter(SD_MUTEX(un));
8401 	}
8402 
8403 	/* Cancel rmw warning message timeouts */
8404 	if (un->un_rmw_msg_timeid != NULL) {
8405 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8406 		un->un_rmw_msg_timeid = NULL;
8407 		mutex_exit(SD_MUTEX(un));
8408 		(void) untimeout(temp_id);
8409 		mutex_enter(SD_MUTEX(un));
8410 	}
8411 
8412 	/* Cancel any pending retry timeouts */
8413 	if (un->un_retry_timeid != NULL) {
8414 		timeout_id_t temp_id = un->un_retry_timeid;
8415 		un->un_retry_timeid = NULL;
8416 		mutex_exit(SD_MUTEX(un));
8417 		(void) untimeout(temp_id);
8418 		mutex_enter(SD_MUTEX(un));
8419 	}
8420 
8421 	/* Cancel any pending delayed cv broadcast timeouts */
8422 	if (un->un_dcvb_timeid != NULL) {
8423 		timeout_id_t temp_id = un->un_dcvb_timeid;
8424 		un->un_dcvb_timeid = NULL;
8425 		mutex_exit(SD_MUTEX(un));
8426 		(void) untimeout(temp_id);
8427 		mutex_enter(SD_MUTEX(un));
8428 	}
8429 
8430 	mutex_exit(SD_MUTEX(un));
8431 
8432 	/* There should not be any in-progress I/O so ASSERT this check */
8433 	ASSERT(un->un_ncmds_in_transport == 0);
8434 	ASSERT(un->un_ncmds_in_driver == 0);
8435 
8436 	/* Do not free the softstate if the callback routine is active */
8437 	sd_sync_with_callback(un);
8438 
8439 	/*
8440 	 * Partition stats apparently are not used with removables. These would
8441 	 * not have been created during attach, so no need to clean them up...
8442 	 */
8443 	if (un->un_errstats != NULL) {
8444 		kstat_delete(un->un_errstats);
8445 		un->un_errstats = NULL;
8446 	}
8447 
8448 create_errstats_failed:
8449 
8450 	if (un->un_stats != NULL) {
8451 		kstat_delete(un->un_stats);
8452 		un->un_stats = NULL;
8453 	}
8454 
8455 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8456 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8457 
8458 	ddi_prop_remove_all(devi);
8459 	sema_destroy(&un->un_semoclose);
8460 	cv_destroy(&un->un_state_cv);
8461 
8462 getrbuf_failed:
8463 
8464 	sd_free_rqs(un);
8465 
8466 alloc_rqs_failed:
8467 
8468 	devp->sd_private = NULL;
8469 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8470 
8471 get_softstate_failed:
8472 	/*
8473 	 * Note: the man pages are unclear as to whether or not doing a
8474 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8475 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8476 	 * ddi_get_soft_state() fails.  The implication seems to be
8477 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8478 	 */
8479 #ifndef XPV_HVM_DRIVER
8480 	ddi_soft_state_free(sd_state, instance);
8481 #endif /* !XPV_HVM_DRIVER */
8482 
8483 probe_failed:
8484 	scsi_unprobe(devp);
8485 
8486 	return (DDI_FAILURE);
8487 }
8488 
8489 
8490 /*
8491  *    Function: sd_unit_detach
8492  *
8493  * Description: Performs DDI_DETACH processing for sddetach().
8494  *
8495  * Return Code: DDI_SUCCESS
8496  *		DDI_FAILURE
8497  *
8498  *     Context: Kernel thread context
8499  */
8500 
8501 static int
8502 sd_unit_detach(dev_info_t *devi)
8503 {
8504 	struct scsi_device	*devp;
8505 	struct sd_lun		*un;
8506 	int			i;
8507 	int			tgt;
8508 	dev_t			dev;
8509 	dev_info_t		*pdip = ddi_get_parent(devi);
8510 #ifndef XPV_HVM_DRIVER
8511 	int			instance = ddi_get_instance(devi);
8512 #endif /* !XPV_HVM_DRIVER */
8513 
8514 	mutex_enter(&sd_detach_mutex);
8515 
8516 	/*
8517 	 * Fail the detach for any of the following:
8518 	 *  - Unable to get the sd_lun struct for the instance
8519 	 *  - A layered driver has an outstanding open on the instance
8520 	 *  - Another thread is already detaching this instance
8521 	 *  - Another thread is currently performing an open
8522 	 */
8523 	devp = ddi_get_driver_private(devi);
8524 	if ((devp == NULL) ||
8525 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8526 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8527 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8528 		mutex_exit(&sd_detach_mutex);
8529 		return (DDI_FAILURE);
8530 	}
8531 
8532 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8533 
8534 	/*
8535 	 * Mark this instance as currently in a detach, to inhibit any
8536 	 * opens from a layered driver.
8537 	 */
8538 	un->un_detach_count++;
8539 	mutex_exit(&sd_detach_mutex);
8540 
8541 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8542 	    SCSI_ADDR_PROP_TARGET, -1);
8543 
8544 	dev = sd_make_device(SD_DEVINFO(un));
8545 
8546 #ifndef lint
8547 	_NOTE(COMPETING_THREADS_NOW);
8548 #endif
8549 
8550 	mutex_enter(SD_MUTEX(un));
8551 
8552 	/*
8553 	 * Fail the detach if there are any outstanding layered
8554 	 * opens on this device.
8555 	 */
8556 	for (i = 0; i < NDKMAP; i++) {
8557 		if (un->un_ocmap.lyropen[i] != 0) {
8558 			goto err_notclosed;
8559 		}
8560 	}
8561 
8562 	/*
8563 	 * Verify there are NO outstanding commands issued to this device.
8564 	 * ie, un_ncmds_in_transport == 0.
8565 	 * It's possible to have outstanding commands through the physio
8566 	 * code path, even though everything's closed.
8567 	 */
8568 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8569 	    (un->un_direct_priority_timeid != NULL) ||
8570 	    (un->un_state == SD_STATE_RWAIT)) {
8571 		mutex_exit(SD_MUTEX(un));
8572 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8573 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8574 		goto err_stillbusy;
8575 	}
8576 
8577 	/*
8578 	 * If we have the device reserved, release the reservation.
8579 	 */
8580 	if ((un->un_resvd_status & SD_RESERVE) &&
8581 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8582 		mutex_exit(SD_MUTEX(un));
8583 		/*
8584 		 * Note: sd_reserve_release sends a command to the device
8585 		 * via the sd_ioctlcmd() path, and can sleep.
8586 		 */
8587 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8588 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8589 			    "sd_dr_detach: Cannot release reservation \n");
8590 		}
8591 	} else {
8592 		mutex_exit(SD_MUTEX(un));
8593 	}
8594 
8595 	/*
8596 	 * Untimeout any reserve recover, throttle reset, restart unit
8597 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8598 	 * from getting nulled by their callback functions.
8599 	 */
8600 	mutex_enter(SD_MUTEX(un));
8601 	if (un->un_resvd_timeid != NULL) {
8602 		timeout_id_t temp_id = un->un_resvd_timeid;
8603 		un->un_resvd_timeid = NULL;
8604 		mutex_exit(SD_MUTEX(un));
8605 		(void) untimeout(temp_id);
8606 		mutex_enter(SD_MUTEX(un));
8607 	}
8608 
8609 	if (un->un_reset_throttle_timeid != NULL) {
8610 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8611 		un->un_reset_throttle_timeid = NULL;
8612 		mutex_exit(SD_MUTEX(un));
8613 		(void) untimeout(temp_id);
8614 		mutex_enter(SD_MUTEX(un));
8615 	}
8616 
8617 	if (un->un_startstop_timeid != NULL) {
8618 		timeout_id_t temp_id = un->un_startstop_timeid;
8619 		un->un_startstop_timeid = NULL;
8620 		mutex_exit(SD_MUTEX(un));
8621 		(void) untimeout(temp_id);
8622 		mutex_enter(SD_MUTEX(un));
8623 	}
8624 
8625 	if (un->un_rmw_msg_timeid != NULL) {
8626 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8627 		un->un_rmw_msg_timeid = NULL;
8628 		mutex_exit(SD_MUTEX(un));
8629 		(void) untimeout(temp_id);
8630 		mutex_enter(SD_MUTEX(un));
8631 	}
8632 
8633 	if (un->un_dcvb_timeid != NULL) {
8634 		timeout_id_t temp_id = un->un_dcvb_timeid;
8635 		un->un_dcvb_timeid = NULL;
8636 		mutex_exit(SD_MUTEX(un));
8637 		(void) untimeout(temp_id);
8638 	} else {
8639 		mutex_exit(SD_MUTEX(un));
8640 	}
8641 
8642 	/* Remove any pending reservation reclaim requests for this device */
8643 	sd_rmv_resv_reclaim_req(dev);
8644 
8645 	mutex_enter(SD_MUTEX(un));
8646 
8647 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8648 	if (un->un_direct_priority_timeid != NULL) {
8649 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8650 		un->un_direct_priority_timeid = NULL;
8651 		mutex_exit(SD_MUTEX(un));
8652 		(void) untimeout(temp_id);
8653 		mutex_enter(SD_MUTEX(un));
8654 	}
8655 
8656 	/* Cancel any active multi-host disk watch thread requests */
8657 	if (un->un_mhd_token != NULL) {
8658 		mutex_exit(SD_MUTEX(un));
8659 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8660 		if (scsi_watch_request_terminate(un->un_mhd_token,
8661 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8662 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8663 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8664 			/*
8665 			 * Note: We are returning here after having removed
8666 			 * some driver timeouts above. This is consistent with
8667 			 * the legacy implementation but perhaps the watch
8668 			 * terminate call should be made with the wait flag set.
8669 			 */
8670 			goto err_stillbusy;
8671 		}
8672 		mutex_enter(SD_MUTEX(un));
8673 		un->un_mhd_token = NULL;
8674 	}
8675 
8676 	if (un->un_swr_token != NULL) {
8677 		mutex_exit(SD_MUTEX(un));
8678 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8679 		if (scsi_watch_request_terminate(un->un_swr_token,
8680 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8681 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8682 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8683 			/*
8684 			 * Note: We are returning here after having removed
8685 			 * some driver timeouts above. This is consistent with
8686 			 * the legacy implementation but perhaps the watch
8687 			 * terminate call should be made with the wait flag set.
8688 			 */
8689 			goto err_stillbusy;
8690 		}
8691 		mutex_enter(SD_MUTEX(un));
8692 		un->un_swr_token = NULL;
8693 	}
8694 
8695 	mutex_exit(SD_MUTEX(un));
8696 
8697 	/*
8698 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8699 	 * if we have not registered one.
8700 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8701 	 */
8702 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8703 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8704 
8705 	/*
8706 	 * protect the timeout pointers from getting nulled by
8707 	 * their callback functions during the cancellation process.
8708 	 * In such a scenario untimeout can be invoked with a null value.
8709 	 */
8710 	_NOTE(NO_COMPETING_THREADS_NOW);
8711 
8712 	mutex_enter(&un->un_pm_mutex);
8713 	if (un->un_pm_idle_timeid != NULL) {
8714 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8715 		un->un_pm_idle_timeid = NULL;
8716 		mutex_exit(&un->un_pm_mutex);
8717 
8718 		/*
8719 		 * Timeout is active; cancel it.
8720 		 * Note that it'll never be active on a device
8721 		 * that does not support PM therefore we don't
8722 		 * have to check before calling pm_idle_component.
8723 		 */
8724 		(void) untimeout(temp_id);
8725 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8726 		mutex_enter(&un->un_pm_mutex);
8727 	}
8728 
8729 	/*
8730 	 * Check whether there is already a timeout scheduled for power
8731 	 * management. If yes then don't lower the power here, that's.
8732 	 * the timeout handler's job.
8733 	 */
8734 	if (un->un_pm_timeid != NULL) {
8735 		timeout_id_t temp_id = un->un_pm_timeid;
8736 		un->un_pm_timeid = NULL;
8737 		mutex_exit(&un->un_pm_mutex);
8738 		/*
8739 		 * Timeout is active; cancel it.
8740 		 * Note that it'll never be active on a device
8741 		 * that does not support PM therefore we don't
8742 		 * have to check before calling pm_idle_component.
8743 		 */
8744 		(void) untimeout(temp_id);
8745 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8746 
8747 	} else {
8748 		mutex_exit(&un->un_pm_mutex);
8749 		if ((un->un_f_pm_is_enabled == TRUE) &&
8750 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8751 		    != DDI_SUCCESS)) {
8752 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8753 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8754 			/*
8755 			 * Fix for bug: 4297749, item # 13
8756 			 * The above test now includes a check to see if PM is
8757 			 * supported by this device before call
8758 			 * pm_lower_power().
8759 			 * Note, the following is not dead code. The call to
8760 			 * pm_lower_power above will generate a call back into
8761 			 * our sdpower routine which might result in a timeout
8762 			 * handler getting activated. Therefore the following
8763 			 * code is valid and necessary.
8764 			 */
8765 			mutex_enter(&un->un_pm_mutex);
8766 			if (un->un_pm_timeid != NULL) {
8767 				timeout_id_t temp_id = un->un_pm_timeid;
8768 				un->un_pm_timeid = NULL;
8769 				mutex_exit(&un->un_pm_mutex);
8770 				(void) untimeout(temp_id);
8771 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8772 			} else {
8773 				mutex_exit(&un->un_pm_mutex);
8774 			}
8775 		}
8776 	}
8777 
8778 	/*
8779 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8780 	 * Relocated here from above to be after the call to
8781 	 * pm_lower_power, which was getting errors.
8782 	 */
8783 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8784 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8785 
8786 	/*
8787 	 * Currently, tagged queuing is supported per target based by HBA.
8788 	 * Setting this per lun instance actually sets the capability of this
8789 	 * target in HBA, which affects those luns already attached on the
8790 	 * same target. So during detach, we can only disable this capability
8791 	 * only when this is the only lun left on this target. By doing
8792 	 * this, we assume a target has the same tagged queuing capability
8793 	 * for every lun. The condition can be removed when HBA is changed to
8794 	 * support per lun based tagged queuing capability.
8795 	 */
8796 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8797 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8798 	}
8799 
8800 	if (un->un_f_is_fibre == FALSE) {
8801 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8802 	}
8803 
8804 	/*
8805 	 * Remove any event callbacks, fibre only
8806 	 */
8807 	if (un->un_f_is_fibre == TRUE) {
8808 		if ((un->un_insert_event != NULL) &&
8809 		    (ddi_remove_event_handler(un->un_insert_cb_id) !=
8810 		    DDI_SUCCESS)) {
8811 			/*
8812 			 * Note: We are returning here after having done
8813 			 * substantial cleanup above. This is consistent
8814 			 * with the legacy implementation but this may not
8815 			 * be the right thing to do.
8816 			 */
8817 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8818 			    "sd_dr_detach: Cannot cancel insert event\n");
8819 			goto err_remove_event;
8820 		}
8821 		un->un_insert_event = NULL;
8822 
8823 		if ((un->un_remove_event != NULL) &&
8824 		    (ddi_remove_event_handler(un->un_remove_cb_id) !=
8825 		    DDI_SUCCESS)) {
8826 			/*
8827 			 * Note: We are returning here after having done
8828 			 * substantial cleanup above. This is consistent
8829 			 * with the legacy implementation but this may not
8830 			 * be the right thing to do.
8831 			 */
8832 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8833 			    "sd_dr_detach: Cannot cancel remove event\n");
8834 			goto err_remove_event;
8835 		}
8836 		un->un_remove_event = NULL;
8837 	}
8838 
8839 	/* Do not free the softstate if the callback routine is active */
8840 	sd_sync_with_callback(un);
8841 
8842 	cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8843 	cmlb_free_handle(&un->un_cmlbhandle);
8844 
8845 	/*
8846 	 * Hold the detach mutex here, to make sure that no other threads ever
8847 	 * can access a (partially) freed soft state structure.
8848 	 */
8849 	mutex_enter(&sd_detach_mutex);
8850 
8851 	/*
8852 	 * Clean up the soft state struct.
8853 	 * Cleanup is done in reverse order of allocs/inits.
8854 	 * At this point there should be no competing threads anymore.
8855 	 */
8856 
8857 	scsi_fm_fini(devp);
8858 
8859 	/*
8860 	 * Deallocate memory for SCSI FMA.
8861 	 */
8862 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8863 
8864 	/*
8865 	 * Unregister and free device id if it was not registered
8866 	 * by the transport.
8867 	 */
8868 	if (un->un_f_devid_transport_defined == FALSE)
8869 		ddi_devid_unregister(devi);
8870 
8871 	/*
8872 	 * free the devid structure if allocated before (by ddi_devid_init()
8873 	 * or ddi_devid_get()).
8874 	 */
8875 	if (un->un_devid) {
8876 		ddi_devid_free(un->un_devid);
8877 		un->un_devid = NULL;
8878 	}
8879 
8880 	/*
8881 	 * Destroy wmap cache if it exists.
8882 	 */
8883 	if (un->un_wm_cache != NULL) {
8884 		kmem_cache_destroy(un->un_wm_cache);
8885 		un->un_wm_cache = NULL;
8886 	}
8887 
8888 	/*
8889 	 * kstat cleanup is done in detach for all device types (4363169).
8890 	 * We do not want to fail detach if the device kstats are not deleted
8891 	 * since there is a confusion about the devo_refcnt for the device.
8892 	 * We just delete the kstats and let detach complete successfully.
8893 	 */
8894 	if (un->un_stats != NULL) {
8895 		kstat_delete(un->un_stats);
8896 		un->un_stats = NULL;
8897 	}
8898 	if (un->un_errstats != NULL) {
8899 		kstat_delete(un->un_errstats);
8900 		un->un_errstats = NULL;
8901 	}
8902 
8903 	/* Remove partition stats */
8904 	if (un->un_f_pkstats_enabled) {
8905 		for (i = 0; i < NSDMAP; i++) {
8906 			if (un->un_pstats[i] != NULL) {
8907 				kstat_delete(un->un_pstats[i]);
8908 				un->un_pstats[i] = NULL;
8909 			}
8910 		}
8911 	}
8912 
8913 	/* Remove xbuf registration */
8914 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8915 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8916 
8917 	/* Remove driver properties */
8918 	ddi_prop_remove_all(devi);
8919 
8920 	mutex_destroy(&un->un_pm_mutex);
8921 	cv_destroy(&un->un_pm_busy_cv);
8922 
8923 	cv_destroy(&un->un_wcc_cv);
8924 
8925 	/* Open/close semaphore */
8926 	sema_destroy(&un->un_semoclose);
8927 
8928 	/* Removable media condvar. */
8929 	cv_destroy(&un->un_state_cv);
8930 
8931 	/* Suspend/resume condvar. */
8932 	cv_destroy(&un->un_suspend_cv);
8933 	cv_destroy(&un->un_disk_busy_cv);
8934 
8935 	sd_free_rqs(un);
8936 
8937 	/* Free up soft state */
8938 	devp->sd_private = NULL;
8939 
8940 	bzero(un, sizeof (struct sd_lun));
8941 #ifndef XPV_HVM_DRIVER
8942 	ddi_soft_state_free(sd_state, instance);
8943 #endif /* !XPV_HVM_DRIVER */
8944 
8945 	mutex_exit(&sd_detach_mutex);
8946 
8947 	/* This frees up the INQUIRY data associated with the device. */
8948 	scsi_unprobe(devp);
8949 
8950 	/*
8951 	 * After successfully detaching an instance, we update the information
8952 	 * of how many luns have been attached in the relative target and
8953 	 * controller for parallel SCSI. This information is used when sd tries
8954 	 * to set the tagged queuing capability in HBA.
8955 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8956 	 * check if the device is parallel SCSI. However, we don't need to
8957 	 * check here because we've already checked during attach. No device
8958 	 * that is not parallel SCSI is in the chain.
8959 	 */
8960 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8961 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
8962 	}
8963 
8964 	return (DDI_SUCCESS);
8965 
8966 err_notclosed:
8967 	mutex_exit(SD_MUTEX(un));
8968 
8969 err_stillbusy:
8970 	_NOTE(NO_COMPETING_THREADS_NOW);
8971 
8972 err_remove_event:
8973 	mutex_enter(&sd_detach_mutex);
8974 	un->un_detach_count--;
8975 	mutex_exit(&sd_detach_mutex);
8976 
8977 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
8978 	return (DDI_FAILURE);
8979 }
8980 
8981 
8982 /*
8983  *    Function: sd_create_errstats
8984  *
8985  * Description: This routine instantiates the device error stats.
8986  *
8987  *		Note: During attach the stats are instantiated first so they are
8988  *		available for attach-time routines that utilize the driver
8989  *		iopath to send commands to the device. The stats are initialized
8990  *		separately so data obtained during some attach-time routines is
8991  *		available. (4362483)
8992  *
8993  *   Arguments: un - driver soft state (unit) structure
8994  *		instance - driver instance
8995  *
8996  *     Context: Kernel thread context
8997  */
8998 
8999 static void
9000 sd_create_errstats(struct sd_lun *un, int instance)
9001 {
9002 	struct	sd_errstats	*stp;
9003 	char	kstatmodule_err[KSTAT_STRLEN];
9004 	char	kstatname[KSTAT_STRLEN];
9005 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9006 
9007 	ASSERT(un != NULL);
9008 
9009 	if (un->un_errstats != NULL) {
9010 		return;
9011 	}
9012 
9013 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9014 	    "%serr", sd_label);
9015 	(void) snprintf(kstatname, sizeof (kstatname),
9016 	    "%s%d,err", sd_label, instance);
9017 
9018 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9019 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9020 
9021 	if (un->un_errstats == NULL) {
9022 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9023 		    "sd_create_errstats: Failed kstat_create\n");
9024 		return;
9025 	}
9026 
9027 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9028 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9029 	    KSTAT_DATA_UINT32);
9030 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9031 	    KSTAT_DATA_UINT32);
9032 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9033 	    KSTAT_DATA_UINT32);
9034 	kstat_named_init(&stp->sd_vid,		"Vendor",
9035 	    KSTAT_DATA_CHAR);
9036 	kstat_named_init(&stp->sd_pid,		"Product",
9037 	    KSTAT_DATA_CHAR);
9038 	kstat_named_init(&stp->sd_revision,	"Revision",
9039 	    KSTAT_DATA_CHAR);
9040 	kstat_named_init(&stp->sd_serial,	"Serial No",
9041 	    KSTAT_DATA_CHAR);
9042 	kstat_named_init(&stp->sd_capacity,	"Size",
9043 	    KSTAT_DATA_ULONGLONG);
9044 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9045 	    KSTAT_DATA_UINT32);
9046 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9047 	    KSTAT_DATA_UINT32);
9048 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9049 	    KSTAT_DATA_UINT32);
9050 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9051 	    KSTAT_DATA_UINT32);
9052 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9053 	    KSTAT_DATA_UINT32);
9054 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9055 	    KSTAT_DATA_UINT32);
9056 
9057 	un->un_errstats->ks_private = un;
9058 	un->un_errstats->ks_update  = nulldev;
9059 
9060 	kstat_install(un->un_errstats);
9061 }
9062 
9063 
9064 /*
9065  *    Function: sd_set_errstats
9066  *
9067  * Description: This routine sets the value of the vendor id, product id,
9068  *		revision, serial number, and capacity device error stats.
9069  *
9070  *		Note: During attach the stats are instantiated first so they are
9071  *		available for attach-time routines that utilize the driver
9072  *		iopath to send commands to the device. The stats are initialized
9073  *		separately so data obtained during some attach-time routines is
9074  *		available. (4362483)
9075  *
9076  *   Arguments: un - driver soft state (unit) structure
9077  *
9078  *     Context: Kernel thread context
9079  */
9080 
9081 static void
9082 sd_set_errstats(struct sd_lun *un)
9083 {
9084 	struct	sd_errstats	*stp;
9085 
9086 	ASSERT(un != NULL);
9087 	ASSERT(un->un_errstats != NULL);
9088 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9089 	ASSERT(stp != NULL);
9090 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9091 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9092 	(void) strncpy(stp->sd_revision.value.c,
9093 	    un->un_sd->sd_inq->inq_revision, 4);
9094 
9095 	/*
9096 	 * All the errstats are persistent across detach/attach,
9097 	 * so reset all the errstats here in case of the hot
9098 	 * replacement of disk drives, except for not changed
9099 	 * Sun qualified drives.
9100 	 */
9101 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9102 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9103 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9104 		stp->sd_softerrs.value.ui32 = 0;
9105 		stp->sd_harderrs.value.ui32 = 0;
9106 		stp->sd_transerrs.value.ui32 = 0;
9107 		stp->sd_rq_media_err.value.ui32 = 0;
9108 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9109 		stp->sd_rq_nodev_err.value.ui32 = 0;
9110 		stp->sd_rq_recov_err.value.ui32 = 0;
9111 		stp->sd_rq_illrq_err.value.ui32 = 0;
9112 		stp->sd_rq_pfa_err.value.ui32 = 0;
9113 	}
9114 
9115 	/*
9116 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9117 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9118 	 * (4376302))
9119 	 */
9120 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9121 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9122 		    sizeof (SD_INQUIRY(un)->inq_serial));
9123 	}
9124 
9125 	if (un->un_f_blockcount_is_valid != TRUE) {
9126 		/*
9127 		 * Set capacity error stat to 0 for no media. This ensures
9128 		 * a valid capacity is displayed in response to 'iostat -E'
9129 		 * when no media is present in the device.
9130 		 */
9131 		stp->sd_capacity.value.ui64 = 0;
9132 	} else {
9133 		/*
9134 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9135 		 * capacity.
9136 		 *
9137 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9138 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9139 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9140 		 */
9141 		stp->sd_capacity.value.ui64 = (uint64_t)
9142 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9143 	}
9144 }
9145 
9146 
9147 /*
9148  *    Function: sd_set_pstats
9149  *
9150  * Description: This routine instantiates and initializes the partition
9151  *              stats for each partition with more than zero blocks.
9152  *		(4363169)
9153  *
9154  *   Arguments: un - driver soft state (unit) structure
9155  *
9156  *     Context: Kernel thread context
9157  */
9158 
9159 static void
9160 sd_set_pstats(struct sd_lun *un)
9161 {
9162 	char	kstatname[KSTAT_STRLEN];
9163 	int	instance;
9164 	int	i;
9165 	diskaddr_t	nblks = 0;
9166 	char	*partname = NULL;
9167 
9168 	ASSERT(un != NULL);
9169 
9170 	instance = ddi_get_instance(SD_DEVINFO(un));
9171 
9172 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9173 	for (i = 0; i < NSDMAP; i++) {
9174 
9175 		if (cmlb_partinfo(un->un_cmlbhandle, i,
9176 		    &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9177 			continue;
9178 		mutex_enter(SD_MUTEX(un));
9179 
9180 		if ((un->un_pstats[i] == NULL) &&
9181 		    (nblks != 0)) {
9182 
9183 			(void) snprintf(kstatname, sizeof (kstatname),
9184 			    "%s%d,%s", sd_label, instance,
9185 			    partname);
9186 
9187 			un->un_pstats[i] = kstat_create(sd_label,
9188 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9189 			    1, KSTAT_FLAG_PERSISTENT);
9190 			if (un->un_pstats[i] != NULL) {
9191 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9192 				kstat_install(un->un_pstats[i]);
9193 			}
9194 		}
9195 		mutex_exit(SD_MUTEX(un));
9196 	}
9197 }
9198 
9199 
9200 #if (defined(__fibre))
9201 /*
9202  *    Function: sd_init_event_callbacks
9203  *
9204  * Description: This routine initializes the insertion and removal event
9205  *		callbacks. (fibre only)
9206  *
9207  *   Arguments: un - driver soft state (unit) structure
9208  *
9209  *     Context: Kernel thread context
9210  */
9211 
9212 static void
9213 sd_init_event_callbacks(struct sd_lun *un)
9214 {
9215 	ASSERT(un != NULL);
9216 
9217 	if ((un->un_insert_event == NULL) &&
9218 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9219 	    &un->un_insert_event) == DDI_SUCCESS)) {
9220 		/*
9221 		 * Add the callback for an insertion event
9222 		 */
9223 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9224 		    un->un_insert_event, sd_event_callback, (void *)un,
9225 		    &(un->un_insert_cb_id));
9226 	}
9227 
9228 	if ((un->un_remove_event == NULL) &&
9229 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9230 	    &un->un_remove_event) == DDI_SUCCESS)) {
9231 		/*
9232 		 * Add the callback for a removal event
9233 		 */
9234 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9235 		    un->un_remove_event, sd_event_callback, (void *)un,
9236 		    &(un->un_remove_cb_id));
9237 	}
9238 }
9239 
9240 
9241 /*
9242  *    Function: sd_event_callback
9243  *
9244  * Description: This routine handles insert/remove events (photon). The
9245  *		state is changed to OFFLINE which can be used to supress
9246  *		error msgs. (fibre only)
9247  *
9248  *   Arguments: un - driver soft state (unit) structure
9249  *
9250  *     Context: Callout thread context
9251  */
9252 /* ARGSUSED */
9253 static void
9254 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9255     void *bus_impldata)
9256 {
9257 	struct sd_lun *un = (struct sd_lun *)arg;
9258 
9259 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9260 	if (event == un->un_insert_event) {
9261 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9262 		mutex_enter(SD_MUTEX(un));
9263 		if (un->un_state == SD_STATE_OFFLINE) {
9264 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9265 				un->un_state = un->un_last_state;
9266 			} else {
9267 				/*
9268 				 * We have gone through SUSPEND/RESUME while
9269 				 * we were offline. Restore the last state
9270 				 */
9271 				un->un_state = un->un_save_state;
9272 			}
9273 		}
9274 		mutex_exit(SD_MUTEX(un));
9275 
9276 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9277 	} else if (event == un->un_remove_event) {
9278 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9279 		mutex_enter(SD_MUTEX(un));
9280 		/*
9281 		 * We need to handle an event callback that occurs during
9282 		 * the suspend operation, since we don't prevent it.
9283 		 */
9284 		if (un->un_state != SD_STATE_OFFLINE) {
9285 			if (un->un_state != SD_STATE_SUSPENDED) {
9286 				New_state(un, SD_STATE_OFFLINE);
9287 			} else {
9288 				un->un_last_state = SD_STATE_OFFLINE;
9289 			}
9290 		}
9291 		mutex_exit(SD_MUTEX(un));
9292 	} else {
9293 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9294 		    "!Unknown event\n");
9295 	}
9296 
9297 }
9298 #endif
9299 
9300 /*
9301  *    Function: sd_cache_control()
9302  *
9303  * Description: This routine is the driver entry point for setting
9304  *		read and write caching by modifying the WCE (write cache
9305  *		enable) and RCD (read cache disable) bits of mode
9306  *		page 8 (MODEPAGE_CACHING).
9307  *
9308  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
9309  *                      structure for this target.
9310  *		rcd_flag - flag for controlling the read cache
9311  *		wce_flag - flag for controlling the write cache
9312  *
9313  * Return Code: EIO
9314  *		code returned by sd_send_scsi_MODE_SENSE and
9315  *		sd_send_scsi_MODE_SELECT
9316  *
9317  *     Context: Kernel Thread
9318  */
9319 
9320 static int
9321 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9322 {
9323 	struct mode_caching	*mode_caching_page;
9324 	uchar_t			*header;
9325 	size_t			buflen;
9326 	int			hdrlen;
9327 	int			bd_len;
9328 	int			rval = 0;
9329 	struct mode_header_grp2	*mhp;
9330 	struct sd_lun		*un;
9331 	int			status;
9332 
9333 	ASSERT(ssc != NULL);
9334 	un = ssc->ssc_un;
9335 	ASSERT(un != NULL);
9336 
9337 	/*
9338 	 * Do a test unit ready, otherwise a mode sense may not work if this
9339 	 * is the first command sent to the device after boot.
9340 	 */
9341 	status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9342 	if (status != 0)
9343 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9344 
9345 	if (un->un_f_cfg_is_atapi == TRUE) {
9346 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9347 	} else {
9348 		hdrlen = MODE_HEADER_LENGTH;
9349 	}
9350 
9351 	/*
9352 	 * Allocate memory for the retrieved mode page and its headers.  Set
9353 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
9354 	 * we get all of the mode sense data otherwise, the mode select
9355 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
9356 	 */
9357 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9358 	    sizeof (struct mode_cache_scsi3);
9359 
9360 	header = kmem_zalloc(buflen, KM_SLEEP);
9361 
9362 	/* Get the information from the device. */
9363 	if (un->un_f_cfg_is_atapi == TRUE) {
9364 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9365 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9366 	} else {
9367 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9368 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9369 	}
9370 
9371 	if (rval != 0) {
9372 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9373 		    "sd_cache_control: Mode Sense Failed\n");
9374 		goto mode_sense_failed;
9375 	}
9376 
9377 	/*
9378 	 * Determine size of Block Descriptors in order to locate
9379 	 * the mode page data. ATAPI devices return 0, SCSI devices
9380 	 * should return MODE_BLK_DESC_LENGTH.
9381 	 */
9382 	if (un->un_f_cfg_is_atapi == TRUE) {
9383 		mhp	= (struct mode_header_grp2 *)header;
9384 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9385 	} else {
9386 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9387 	}
9388 
9389 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9390 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9391 		    "sd_cache_control: Mode Sense returned invalid block "
9392 		    "descriptor length\n");
9393 		rval = EIO;
9394 		goto mode_sense_failed;
9395 	}
9396 
9397 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9398 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9399 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9400 		    "sd_cache_control: Mode Sense caching page code mismatch "
9401 		    "%d\n", mode_caching_page->mode_page.code);
9402 		rval = EIO;
9403 		goto mode_sense_failed;
9404 	}
9405 
9406 	/* Check the relevant bits on successful mode sense. */
9407 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9408 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9409 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9410 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9411 
9412 		size_t sbuflen;
9413 		uchar_t save_pg;
9414 
9415 		/*
9416 		 * Construct select buffer length based on the
9417 		 * length of the sense data returned.
9418 		 */
9419 		sbuflen =  hdrlen + bd_len +
9420 		    sizeof (struct mode_page) +
9421 		    (int)mode_caching_page->mode_page.length;
9422 
9423 		/*
9424 		 * Set the caching bits as requested.
9425 		 */
9426 		if (rcd_flag == SD_CACHE_ENABLE)
9427 			mode_caching_page->rcd = 0;
9428 		else if (rcd_flag == SD_CACHE_DISABLE)
9429 			mode_caching_page->rcd = 1;
9430 
9431 		if (wce_flag == SD_CACHE_ENABLE)
9432 			mode_caching_page->wce = 1;
9433 		else if (wce_flag == SD_CACHE_DISABLE)
9434 			mode_caching_page->wce = 0;
9435 
9436 		/*
9437 		 * Save the page if the mode sense says the
9438 		 * drive supports it.
9439 		 */
9440 		save_pg = mode_caching_page->mode_page.ps ?
9441 		    SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9442 
9443 		/* Clear reserved bits before mode select. */
9444 		mode_caching_page->mode_page.ps = 0;
9445 
9446 		/*
9447 		 * Clear out mode header for mode select.
9448 		 * The rest of the retrieved page will be reused.
9449 		 */
9450 		bzero(header, hdrlen);
9451 
9452 		if (un->un_f_cfg_is_atapi == TRUE) {
9453 			mhp = (struct mode_header_grp2 *)header;
9454 			mhp->bdesc_length_hi = bd_len >> 8;
9455 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9456 		} else {
9457 			((struct mode_header *)header)->bdesc_length = bd_len;
9458 		}
9459 
9460 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9461 
9462 		/* Issue mode select to change the cache settings */
9463 		if (un->un_f_cfg_is_atapi == TRUE) {
9464 			rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header,
9465 			    sbuflen, save_pg, SD_PATH_DIRECT);
9466 		} else {
9467 			rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
9468 			    sbuflen, save_pg, SD_PATH_DIRECT);
9469 		}
9470 
9471 	}
9472 
9473 
9474 mode_sense_failed:
9475 
9476 	kmem_free(header, buflen);
9477 
9478 	if (rval != 0) {
9479 		if (rval == EIO)
9480 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9481 		else
9482 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9483 	}
9484 	return (rval);
9485 }
9486 
9487 
9488 /*
9489  *    Function: sd_get_write_cache_enabled()
9490  *
9491  * Description: This routine is the driver entry point for determining if
9492  *		write caching is enabled.  It examines the WCE (write cache
9493  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
9494  *
9495  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
9496  *                      structure for this target.
9497  *		is_enabled - pointer to int where write cache enabled state
9498  *		is returned (non-zero -> write cache enabled)
9499  *
9500  *
9501  * Return Code: EIO
9502  *		code returned by sd_send_scsi_MODE_SENSE
9503  *
9504  *     Context: Kernel Thread
9505  *
9506  * NOTE: If ioctl is added to disable write cache, this sequence should
9507  * be followed so that no locking is required for accesses to
9508  * un->un_f_write_cache_enabled:
9509  * 	do mode select to clear wce
9510  * 	do synchronize cache to flush cache
9511  * 	set un->un_f_write_cache_enabled = FALSE
9512  *
9513  * Conversely, an ioctl to enable the write cache should be done
9514  * in this order:
9515  * 	set un->un_f_write_cache_enabled = TRUE
9516  * 	do mode select to set wce
9517  */
9518 
9519 static int
9520 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9521 {
9522 	struct mode_caching	*mode_caching_page;
9523 	uchar_t			*header;
9524 	size_t			buflen;
9525 	int			hdrlen;
9526 	int			bd_len;
9527 	int			rval = 0;
9528 	struct sd_lun		*un;
9529 	int			status;
9530 
9531 	ASSERT(ssc != NULL);
9532 	un = ssc->ssc_un;
9533 	ASSERT(un != NULL);
9534 	ASSERT(is_enabled != NULL);
9535 
9536 	/* in case of error, flag as enabled */
9537 	*is_enabled = TRUE;
9538 
9539 	/*
9540 	 * Do a test unit ready, otherwise a mode sense may not work if this
9541 	 * is the first command sent to the device after boot.
9542 	 */
9543 	status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9544 
9545 	if (status != 0)
9546 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9547 
9548 	if (un->un_f_cfg_is_atapi == TRUE) {
9549 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9550 	} else {
9551 		hdrlen = MODE_HEADER_LENGTH;
9552 	}
9553 
9554 	/*
9555 	 * Allocate memory for the retrieved mode page and its headers.  Set
9556 	 * a pointer to the page itself.
9557 	 */
9558 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9559 	header = kmem_zalloc(buflen, KM_SLEEP);
9560 
9561 	/* Get the information from the device. */
9562 	if (un->un_f_cfg_is_atapi == TRUE) {
9563 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9564 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9565 	} else {
9566 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9567 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9568 	}
9569 
9570 	if (rval != 0) {
9571 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9572 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
9573 		goto mode_sense_failed;
9574 	}
9575 
9576 	/*
9577 	 * Determine size of Block Descriptors in order to locate
9578 	 * the mode page data. ATAPI devices return 0, SCSI devices
9579 	 * should return MODE_BLK_DESC_LENGTH.
9580 	 */
9581 	if (un->un_f_cfg_is_atapi == TRUE) {
9582 		struct mode_header_grp2	*mhp;
9583 		mhp	= (struct mode_header_grp2 *)header;
9584 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9585 	} else {
9586 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9587 	}
9588 
9589 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9590 		/* FMA should make upset complain here */
9591 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9592 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
9593 		    "block descriptor length\n");
9594 		rval = EIO;
9595 		goto mode_sense_failed;
9596 	}
9597 
9598 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9599 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9600 		/* FMA could make upset complain here */
9601 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9602 		    "sd_get_write_cache_enabled: Mode Sense caching page "
9603 		    "code mismatch %d\n", mode_caching_page->mode_page.code);
9604 		rval = EIO;
9605 		goto mode_sense_failed;
9606 	}
9607 	*is_enabled = mode_caching_page->wce;
9608 
9609 mode_sense_failed:
9610 	if (rval == 0) {
9611 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9612 	} else if (rval == EIO) {
9613 		/*
9614 		 * Some disks do not support mode sense(6), we
9615 		 * should ignore this kind of error(sense key is
9616 		 * 0x5 - illegal request).
9617 		 */
9618 		uint8_t *sensep;
9619 		int senlen;
9620 
9621 		sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9622 		senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9623 		    ssc->ssc_uscsi_cmd->uscsi_rqresid);
9624 
9625 		if (senlen > 0 &&
9626 		    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9627 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9628 		} else {
9629 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9630 		}
9631 	} else {
9632 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9633 	}
9634 	kmem_free(header, buflen);
9635 	return (rval);
9636 }
9637 
9638 /*
9639  *    Function: sd_get_nv_sup()
9640  *
9641  * Description: This routine is the driver entry point for
9642  * determining whether non-volatile cache is supported. This
9643  * determination process works as follows:
9644  *
9645  * 1. sd first queries sd.conf on whether
9646  * suppress_cache_flush bit is set for this device.
9647  *
9648  * 2. if not there, then queries the internal disk table.
9649  *
9650  * 3. if either sd.conf or internal disk table specifies
9651  * cache flush be suppressed, we don't bother checking
9652  * NV_SUP bit.
9653  *
9654  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9655  * the optional INQUIRY VPD page 0x86. If the device
9656  * supports VPD page 0x86, sd examines the NV_SUP
9657  * (non-volatile cache support) bit in the INQUIRY VPD page
9658  * 0x86:
9659  *   o If NV_SUP bit is set, sd assumes the device has a
9660  *   non-volatile cache and set the
9661  *   un_f_sync_nv_supported to TRUE.
9662  *   o Otherwise cache is not non-volatile,
9663  *   un_f_sync_nv_supported is set to FALSE.
9664  *
9665  * Arguments: un - driver soft state (unit) structure
9666  *
9667  * Return Code:
9668  *
9669  *     Context: Kernel Thread
9670  */
9671 
9672 static void
9673 sd_get_nv_sup(sd_ssc_t *ssc)
9674 {
9675 	int		rval		= 0;
9676 	uchar_t		*inq86		= NULL;
9677 	size_t		inq86_len	= MAX_INQUIRY_SIZE;
9678 	size_t		inq86_resid	= 0;
9679 	struct		dk_callback *dkc;
9680 	struct sd_lun	*un;
9681 
9682 	ASSERT(ssc != NULL);
9683 	un = ssc->ssc_un;
9684 	ASSERT(un != NULL);
9685 
9686 	mutex_enter(SD_MUTEX(un));
9687 
9688 	/*
9689 	 * Be conservative on the device's support of
9690 	 * SYNC_NV bit: un_f_sync_nv_supported is
9691 	 * initialized to be false.
9692 	 */
9693 	un->un_f_sync_nv_supported = FALSE;
9694 
9695 	/*
9696 	 * If either sd.conf or internal disk table
9697 	 * specifies cache flush be suppressed, then
9698 	 * we don't bother checking NV_SUP bit.
9699 	 */
9700 	if (un->un_f_suppress_cache_flush == TRUE) {
9701 		mutex_exit(SD_MUTEX(un));
9702 		return;
9703 	}
9704 
9705 	if (sd_check_vpd_page_support(ssc) == 0 &&
9706 	    un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9707 		mutex_exit(SD_MUTEX(un));
9708 		/* collect page 86 data if available */
9709 		inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9710 
9711 		rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9712 		    0x01, 0x86, &inq86_resid);
9713 
9714 		if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9715 			SD_TRACE(SD_LOG_COMMON, un,
9716 			    "sd_get_nv_sup: \
9717 			    successfully get VPD page: %x \
9718 			    PAGE LENGTH: %x BYTE 6: %x\n",
9719 			    inq86[1], inq86[3], inq86[6]);
9720 
9721 			mutex_enter(SD_MUTEX(un));
9722 			/*
9723 			 * check the value of NV_SUP bit: only if the device
9724 			 * reports NV_SUP bit to be 1, the
9725 			 * un_f_sync_nv_supported bit will be set to true.
9726 			 */
9727 			if (inq86[6] & SD_VPD_NV_SUP) {
9728 				un->un_f_sync_nv_supported = TRUE;
9729 			}
9730 			mutex_exit(SD_MUTEX(un));
9731 		} else if (rval != 0) {
9732 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9733 		}
9734 
9735 		kmem_free(inq86, inq86_len);
9736 	} else {
9737 		mutex_exit(SD_MUTEX(un));
9738 	}
9739 
9740 	/*
9741 	 * Send a SYNC CACHE command to check whether
9742 	 * SYNC_NV bit is supported. This command should have
9743 	 * un_f_sync_nv_supported set to correct value.
9744 	 */
9745 	mutex_enter(SD_MUTEX(un));
9746 	if (un->un_f_sync_nv_supported) {
9747 		mutex_exit(SD_MUTEX(un));
9748 		dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9749 		dkc->dkc_flag = FLUSH_VOLATILE;
9750 		(void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9751 
9752 		/*
9753 		 * Send a TEST UNIT READY command to the device. This should
9754 		 * clear any outstanding UNIT ATTENTION that may be present.
9755 		 */
9756 		rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9757 		if (rval != 0)
9758 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9759 
9760 		kmem_free(dkc, sizeof (struct dk_callback));
9761 	} else {
9762 		mutex_exit(SD_MUTEX(un));
9763 	}
9764 
9765 	SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9766 	    un_f_suppress_cache_flush is set to %d\n",
9767 	    un->un_f_suppress_cache_flush);
9768 }
9769 
9770 /*
9771  *    Function: sd_make_device
9772  *
9773  * Description: Utility routine to return the Solaris device number from
9774  *		the data in the device's dev_info structure.
9775  *
9776  * Return Code: The Solaris device number
9777  *
9778  *     Context: Any
9779  */
9780 
9781 static dev_t
9782 sd_make_device(dev_info_t *devi)
9783 {
9784 	return (makedevice(ddi_driver_major(devi),
9785 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9786 }
9787 
9788 
9789 /*
9790  *    Function: sd_pm_entry
9791  *
9792  * Description: Called at the start of a new command to manage power
9793  *		and busy status of a device. This includes determining whether
9794  *		the current power state of the device is sufficient for
9795  *		performing the command or whether it must be changed.
9796  *		The PM framework is notified appropriately.
9797  *		Only with a return status of DDI_SUCCESS will the
9798  *		component be busy to the framework.
9799  *
9800  *		All callers of sd_pm_entry must check the return status
9801  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9802  *		of DDI_FAILURE indicates the device failed to power up.
9803  *		In this case un_pm_count has been adjusted so the result
9804  *		on exit is still powered down, ie. count is less than 0.
9805  *		Calling sd_pm_exit with this count value hits an ASSERT.
9806  *
9807  * Return Code: DDI_SUCCESS or DDI_FAILURE
9808  *
9809  *     Context: Kernel thread context.
9810  */
9811 
9812 static int
9813 sd_pm_entry(struct sd_lun *un)
9814 {
9815 	int return_status = DDI_SUCCESS;
9816 
9817 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9818 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9819 
9820 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9821 
9822 	if (un->un_f_pm_is_enabled == FALSE) {
9823 		SD_TRACE(SD_LOG_IO_PM, un,
9824 		    "sd_pm_entry: exiting, PM not enabled\n");
9825 		return (return_status);
9826 	}
9827 
9828 	/*
9829 	 * Just increment a counter if PM is enabled. On the transition from
9830 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9831 	 * the count with each IO and mark the device as idle when the count
9832 	 * hits 0.
9833 	 *
9834 	 * If the count is less than 0 the device is powered down. If a powered
9835 	 * down device is successfully powered up then the count must be
9836 	 * incremented to reflect the power up. Note that it'll get incremented
9837 	 * a second time to become busy.
9838 	 *
9839 	 * Because the following has the potential to change the device state
9840 	 * and must release the un_pm_mutex to do so, only one thread can be
9841 	 * allowed through at a time.
9842 	 */
9843 
9844 	mutex_enter(&un->un_pm_mutex);
9845 	while (un->un_pm_busy == TRUE) {
9846 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9847 	}
9848 	un->un_pm_busy = TRUE;
9849 
9850 	if (un->un_pm_count < 1) {
9851 
9852 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9853 
9854 		/*
9855 		 * Indicate we are now busy so the framework won't attempt to
9856 		 * power down the device. This call will only fail if either
9857 		 * we passed a bad component number or the device has no
9858 		 * components. Neither of these should ever happen.
9859 		 */
9860 		mutex_exit(&un->un_pm_mutex);
9861 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9862 		ASSERT(return_status == DDI_SUCCESS);
9863 
9864 		mutex_enter(&un->un_pm_mutex);
9865 
9866 		if (un->un_pm_count < 0) {
9867 			mutex_exit(&un->un_pm_mutex);
9868 
9869 			SD_TRACE(SD_LOG_IO_PM, un,
9870 			    "sd_pm_entry: power up component\n");
9871 
9872 			/*
9873 			 * pm_raise_power will cause sdpower to be called
9874 			 * which brings the device power level to the
9875 			 * desired state, If successful, un_pm_count and
9876 			 * un_power_level will be updated appropriately.
9877 			 */
9878 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9879 			    SD_PM_STATE_ACTIVE(un));
9880 
9881 			mutex_enter(&un->un_pm_mutex);
9882 
9883 			if (return_status != DDI_SUCCESS) {
9884 				/*
9885 				 * Power up failed.
9886 				 * Idle the device and adjust the count
9887 				 * so the result on exit is that we're
9888 				 * still powered down, ie. count is less than 0.
9889 				 */
9890 				SD_TRACE(SD_LOG_IO_PM, un,
9891 				    "sd_pm_entry: power up failed,"
9892 				    " idle the component\n");
9893 
9894 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9895 				un->un_pm_count--;
9896 			} else {
9897 				/*
9898 				 * Device is powered up, verify the
9899 				 * count is non-negative.
9900 				 * This is debug only.
9901 				 */
9902 				ASSERT(un->un_pm_count == 0);
9903 			}
9904 		}
9905 
9906 		if (return_status == DDI_SUCCESS) {
9907 			/*
9908 			 * For performance, now that the device has been tagged
9909 			 * as busy, and it's known to be powered up, update the
9910 			 * chain types to use jump tables that do not include
9911 			 * pm. This significantly lowers the overhead and
9912 			 * therefore improves performance.
9913 			 */
9914 
9915 			mutex_exit(&un->un_pm_mutex);
9916 			mutex_enter(SD_MUTEX(un));
9917 			SD_TRACE(SD_LOG_IO_PM, un,
9918 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
9919 			    un->un_uscsi_chain_type);
9920 
9921 			if (un->un_f_non_devbsize_supported) {
9922 				un->un_buf_chain_type =
9923 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
9924 			} else {
9925 				un->un_buf_chain_type =
9926 				    SD_CHAIN_INFO_DISK_NO_PM;
9927 			}
9928 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9929 
9930 			SD_TRACE(SD_LOG_IO_PM, un,
9931 			    "             changed  uscsi_chain_type to   %d\n",
9932 			    un->un_uscsi_chain_type);
9933 			mutex_exit(SD_MUTEX(un));
9934 			mutex_enter(&un->un_pm_mutex);
9935 
9936 			if (un->un_pm_idle_timeid == NULL) {
9937 				/* 300 ms. */
9938 				un->un_pm_idle_timeid =
9939 				    timeout(sd_pm_idletimeout_handler, un,
9940 				    (drv_usectohz((clock_t)300000)));
9941 				/*
9942 				 * Include an extra call to busy which keeps the
9943 				 * device busy with-respect-to the PM layer
9944 				 * until the timer fires, at which time it'll
9945 				 * get the extra idle call.
9946 				 */
9947 				(void) pm_busy_component(SD_DEVINFO(un), 0);
9948 			}
9949 		}
9950 	}
9951 	un->un_pm_busy = FALSE;
9952 	/* Next... */
9953 	cv_signal(&un->un_pm_busy_cv);
9954 
9955 	un->un_pm_count++;
9956 
9957 	SD_TRACE(SD_LOG_IO_PM, un,
9958 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9959 
9960 	mutex_exit(&un->un_pm_mutex);
9961 
9962 	return (return_status);
9963 }
9964 
9965 
9966 /*
9967  *    Function: sd_pm_exit
9968  *
9969  * Description: Called at the completion of a command to manage busy
9970  *		status for the device. If the device becomes idle the
9971  *		PM framework is notified.
9972  *
9973  *     Context: Kernel thread context
9974  */
9975 
9976 static void
9977 sd_pm_exit(struct sd_lun *un)
9978 {
9979 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9980 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9981 
9982 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
9983 
9984 	/*
9985 	 * After attach the following flag is only read, so don't
9986 	 * take the penalty of acquiring a mutex for it.
9987 	 */
9988 	if (un->un_f_pm_is_enabled == TRUE) {
9989 
9990 		mutex_enter(&un->un_pm_mutex);
9991 		un->un_pm_count--;
9992 
9993 		SD_TRACE(SD_LOG_IO_PM, un,
9994 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
9995 
9996 		ASSERT(un->un_pm_count >= 0);
9997 		if (un->un_pm_count == 0) {
9998 			mutex_exit(&un->un_pm_mutex);
9999 
10000 			SD_TRACE(SD_LOG_IO_PM, un,
10001 			    "sd_pm_exit: idle component\n");
10002 
10003 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10004 
10005 		} else {
10006 			mutex_exit(&un->un_pm_mutex);
10007 		}
10008 	}
10009 
10010 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10011 }
10012 
10013 
10014 /*
10015  *    Function: sdopen
10016  *
10017  * Description: Driver's open(9e) entry point function.
10018  *
10019  *   Arguments: dev_i   - pointer to device number
10020  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10021  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10022  *		cred_p  - user credential pointer
10023  *
10024  * Return Code: EINVAL
10025  *		ENXIO
10026  *		EIO
10027  *		EROFS
10028  *		EBUSY
10029  *
10030  *     Context: Kernel thread context
10031  */
10032 /* ARGSUSED */
10033 static int
10034 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10035 {
10036 	struct sd_lun	*un;
10037 	int		nodelay;
10038 	int		part;
10039 	uint64_t	partmask;
10040 	int		instance;
10041 	dev_t		dev;
10042 	int		rval = EIO;
10043 	diskaddr_t	nblks = 0;
10044 	diskaddr_t	label_cap;
10045 
10046 	/* Validate the open type */
10047 	if (otyp >= OTYPCNT) {
10048 		return (EINVAL);
10049 	}
10050 
10051 	dev = *dev_p;
10052 	instance = SDUNIT(dev);
10053 	mutex_enter(&sd_detach_mutex);
10054 
10055 	/*
10056 	 * Fail the open if there is no softstate for the instance, or
10057 	 * if another thread somewhere is trying to detach the instance.
10058 	 */
10059 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10060 	    (un->un_detach_count != 0)) {
10061 		mutex_exit(&sd_detach_mutex);
10062 		/*
10063 		 * The probe cache only needs to be cleared when open (9e) fails
10064 		 * with ENXIO (4238046).
10065 		 */
10066 		/*
10067 		 * un-conditionally clearing probe cache is ok with
10068 		 * separate sd/ssd binaries
10069 		 * x86 platform can be an issue with both parallel
10070 		 * and fibre in 1 binary
10071 		 */
10072 		sd_scsi_clear_probe_cache();
10073 		return (ENXIO);
10074 	}
10075 
10076 	/*
10077 	 * The un_layer_count is to prevent another thread in specfs from
10078 	 * trying to detach the instance, which can happen when we are
10079 	 * called from a higher-layer driver instead of thru specfs.
10080 	 * This will not be needed when DDI provides a layered driver
10081 	 * interface that allows specfs to know that an instance is in
10082 	 * use by a layered driver & should not be detached.
10083 	 *
10084 	 * Note: the semantics for layered driver opens are exactly one
10085 	 * close for every open.
10086 	 */
10087 	if (otyp == OTYP_LYR) {
10088 		un->un_layer_count++;
10089 	}
10090 
10091 	/*
10092 	 * Keep a count of the current # of opens in progress. This is because
10093 	 * some layered drivers try to call us as a regular open. This can
10094 	 * cause problems that we cannot prevent, however by keeping this count
10095 	 * we can at least keep our open and detach routines from racing against
10096 	 * each other under such conditions.
10097 	 */
10098 	un->un_opens_in_progress++;
10099 	mutex_exit(&sd_detach_mutex);
10100 
10101 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10102 	part	 = SDPART(dev);
10103 	partmask = 1 << part;
10104 
10105 	/*
10106 	 * We use a semaphore here in order to serialize
10107 	 * open and close requests on the device.
10108 	 */
10109 	sema_p(&un->un_semoclose);
10110 
10111 	mutex_enter(SD_MUTEX(un));
10112 
10113 	/*
10114 	 * All device accesses go thru sdstrategy() where we check
10115 	 * on suspend status but there could be a scsi_poll command,
10116 	 * which bypasses sdstrategy(), so we need to check pm
10117 	 * status.
10118 	 */
10119 
10120 	if (!nodelay) {
10121 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10122 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10123 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10124 		}
10125 
10126 		mutex_exit(SD_MUTEX(un));
10127 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10128 			rval = EIO;
10129 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10130 			    "sdopen: sd_pm_entry failed\n");
10131 			goto open_failed_with_pm;
10132 		}
10133 		mutex_enter(SD_MUTEX(un));
10134 	}
10135 
10136 	/* check for previous exclusive open */
10137 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10138 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10139 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10140 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10141 
10142 	if (un->un_exclopen & (partmask)) {
10143 		goto excl_open_fail;
10144 	}
10145 
10146 	if (flag & FEXCL) {
10147 		int i;
10148 		if (un->un_ocmap.lyropen[part]) {
10149 			goto excl_open_fail;
10150 		}
10151 		for (i = 0; i < (OTYPCNT - 1); i++) {
10152 			if (un->un_ocmap.regopen[i] & (partmask)) {
10153 				goto excl_open_fail;
10154 			}
10155 		}
10156 	}
10157 
10158 	/*
10159 	 * Check the write permission if this is a removable media device,
10160 	 * NDELAY has not been set, and writable permission is requested.
10161 	 *
10162 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10163 	 * attempt will fail with EIO as part of the I/O processing. This is a
10164 	 * more permissive implementation that allows the open to succeed and
10165 	 * WRITE attempts to fail when appropriate.
10166 	 */
10167 	if (un->un_f_chk_wp_open) {
10168 		if ((flag & FWRITE) && (!nodelay)) {
10169 			mutex_exit(SD_MUTEX(un));
10170 			/*
10171 			 * Defer the check for write permission on writable
10172 			 * DVD drive till sdstrategy and will not fail open even
10173 			 * if FWRITE is set as the device can be writable
10174 			 * depending upon the media and the media can change
10175 			 * after the call to open().
10176 			 */
10177 			if (un->un_f_dvdram_writable_device == FALSE) {
10178 				if (ISCD(un) || sr_check_wp(dev)) {
10179 				rval = EROFS;
10180 				mutex_enter(SD_MUTEX(un));
10181 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10182 				    "write to cd or write protected media\n");
10183 				goto open_fail;
10184 				}
10185 			}
10186 			mutex_enter(SD_MUTEX(un));
10187 		}
10188 	}
10189 
10190 	/*
10191 	 * If opening in NDELAY/NONBLOCK mode, just return.
10192 	 * Check if disk is ready and has a valid geometry later.
10193 	 */
10194 	if (!nodelay) {
10195 		sd_ssc_t	*ssc;
10196 
10197 		mutex_exit(SD_MUTEX(un));
10198 		ssc = sd_ssc_init(un);
10199 		rval = sd_ready_and_valid(ssc, part);
10200 		sd_ssc_fini(ssc);
10201 		mutex_enter(SD_MUTEX(un));
10202 		/*
10203 		 * Fail if device is not ready or if the number of disk
10204 		 * blocks is zero or negative for non CD devices.
10205 		 */
10206 
10207 		nblks = 0;
10208 
10209 		if (rval == SD_READY_VALID && (!ISCD(un))) {
10210 			/* if cmlb_partinfo fails, nblks remains 0 */
10211 			mutex_exit(SD_MUTEX(un));
10212 			(void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10213 			    NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10214 			mutex_enter(SD_MUTEX(un));
10215 		}
10216 
10217 		if ((rval != SD_READY_VALID) ||
10218 		    (!ISCD(un) && nblks <= 0)) {
10219 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10220 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10221 			    "device not ready or invalid disk block value\n");
10222 			goto open_fail;
10223 		}
10224 #if defined(__i386) || defined(__amd64)
10225 	} else {
10226 		uchar_t *cp;
10227 		/*
10228 		 * x86 requires special nodelay handling, so that p0 is
10229 		 * always defined and accessible.
10230 		 * Invalidate geometry only if device is not already open.
10231 		 */
10232 		cp = &un->un_ocmap.chkd[0];
10233 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10234 			if (*cp != (uchar_t)0) {
10235 				break;
10236 			}
10237 			cp++;
10238 		}
10239 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10240 			mutex_exit(SD_MUTEX(un));
10241 			cmlb_invalidate(un->un_cmlbhandle,
10242 			    (void *)SD_PATH_DIRECT);
10243 			mutex_enter(SD_MUTEX(un));
10244 		}
10245 
10246 #endif
10247 	}
10248 
10249 	if (otyp == OTYP_LYR) {
10250 		un->un_ocmap.lyropen[part]++;
10251 	} else {
10252 		un->un_ocmap.regopen[otyp] |= partmask;
10253 	}
10254 
10255 	/* Set up open and exclusive open flags */
10256 	if (flag & FEXCL) {
10257 		un->un_exclopen |= (partmask);
10258 	}
10259 
10260 	/*
10261 	 * If the lun is EFI labeled and lun capacity is greater than the
10262 	 * capacity contained in the label, log a sys-event to notify the
10263 	 * interested module.
10264 	 * To avoid an infinite loop of logging sys-event, we only log the
10265 	 * event when the lun is not opened in NDELAY mode. The event handler
10266 	 * should open the lun in NDELAY mode.
10267 	 */
10268 	if (!(flag & FNDELAY)) {
10269 		mutex_exit(SD_MUTEX(un));
10270 		if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10271 		    (void*)SD_PATH_DIRECT) == 0) {
10272 			mutex_enter(SD_MUTEX(un));
10273 			if (un->un_f_blockcount_is_valid &&
10274 			    un->un_blockcount > label_cap) {
10275 				mutex_exit(SD_MUTEX(un));
10276 				sd_log_lun_expansion_event(un,
10277 				    (nodelay ? KM_NOSLEEP : KM_SLEEP));
10278 				mutex_enter(SD_MUTEX(un));
10279 			}
10280 		} else {
10281 			mutex_enter(SD_MUTEX(un));
10282 		}
10283 	}
10284 
10285 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10286 	    "open of part %d type %d\n", part, otyp);
10287 
10288 	mutex_exit(SD_MUTEX(un));
10289 	if (!nodelay) {
10290 		sd_pm_exit(un);
10291 	}
10292 
10293 	sema_v(&un->un_semoclose);
10294 
10295 	mutex_enter(&sd_detach_mutex);
10296 	un->un_opens_in_progress--;
10297 	mutex_exit(&sd_detach_mutex);
10298 
10299 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10300 	return (DDI_SUCCESS);
10301 
10302 excl_open_fail:
10303 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10304 	rval = EBUSY;
10305 
10306 open_fail:
10307 	mutex_exit(SD_MUTEX(un));
10308 
10309 	/*
10310 	 * On a failed open we must exit the pm management.
10311 	 */
10312 	if (!nodelay) {
10313 		sd_pm_exit(un);
10314 	}
10315 open_failed_with_pm:
10316 	sema_v(&un->un_semoclose);
10317 
10318 	mutex_enter(&sd_detach_mutex);
10319 	un->un_opens_in_progress--;
10320 	if (otyp == OTYP_LYR) {
10321 		un->un_layer_count--;
10322 	}
10323 	mutex_exit(&sd_detach_mutex);
10324 
10325 	return (rval);
10326 }
10327 
10328 
10329 /*
10330  *    Function: sdclose
10331  *
10332  * Description: Driver's close(9e) entry point function.
10333  *
10334  *   Arguments: dev    - device number
10335  *		flag   - file status flag, informational only
10336  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10337  *		cred_p - user credential pointer
10338  *
10339  * Return Code: ENXIO
10340  *
10341  *     Context: Kernel thread context
10342  */
10343 /* ARGSUSED */
10344 static int
10345 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10346 {
10347 	struct sd_lun	*un;
10348 	uchar_t		*cp;
10349 	int		part;
10350 	int		nodelay;
10351 	int		rval = 0;
10352 
10353 	/* Validate the open type */
10354 	if (otyp >= OTYPCNT) {
10355 		return (ENXIO);
10356 	}
10357 
10358 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10359 		return (ENXIO);
10360 	}
10361 
10362 	part = SDPART(dev);
10363 	nodelay = flag & (FNDELAY | FNONBLOCK);
10364 
10365 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10366 	    "sdclose: close of part %d type %d\n", part, otyp);
10367 
10368 	/*
10369 	 * We use a semaphore here in order to serialize
10370 	 * open and close requests on the device.
10371 	 */
10372 	sema_p(&un->un_semoclose);
10373 
10374 	mutex_enter(SD_MUTEX(un));
10375 
10376 	/* Don't proceed if power is being changed. */
10377 	while (un->un_state == SD_STATE_PM_CHANGING) {
10378 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10379 	}
10380 
10381 	if (un->un_exclopen & (1 << part)) {
10382 		un->un_exclopen &= ~(1 << part);
10383 	}
10384 
10385 	/* Update the open partition map */
10386 	if (otyp == OTYP_LYR) {
10387 		un->un_ocmap.lyropen[part] -= 1;
10388 	} else {
10389 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10390 	}
10391 
10392 	cp = &un->un_ocmap.chkd[0];
10393 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10394 		if (*cp != NULL) {
10395 			break;
10396 		}
10397 		cp++;
10398 	}
10399 
10400 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10401 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10402 
10403 		/*
10404 		 * We avoid persistance upon the last close, and set
10405 		 * the throttle back to the maximum.
10406 		 */
10407 		un->un_throttle = un->un_saved_throttle;
10408 
10409 		if (un->un_state == SD_STATE_OFFLINE) {
10410 			if (un->un_f_is_fibre == FALSE) {
10411 				scsi_log(SD_DEVINFO(un), sd_label,
10412 				    CE_WARN, "offline\n");
10413 			}
10414 			mutex_exit(SD_MUTEX(un));
10415 			cmlb_invalidate(un->un_cmlbhandle,
10416 			    (void *)SD_PATH_DIRECT);
10417 			mutex_enter(SD_MUTEX(un));
10418 
10419 		} else {
10420 			/*
10421 			 * Flush any outstanding writes in NVRAM cache.
10422 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10423 			 * cmd, it may not work for non-Pluto devices.
10424 			 * SYNCHRONIZE CACHE is not required for removables,
10425 			 * except DVD-RAM drives.
10426 			 *
10427 			 * Also note: because SYNCHRONIZE CACHE is currently
10428 			 * the only command issued here that requires the
10429 			 * drive be powered up, only do the power up before
10430 			 * sending the Sync Cache command. If additional
10431 			 * commands are added which require a powered up
10432 			 * drive, the following sequence may have to change.
10433 			 *
10434 			 * And finally, note that parallel SCSI on SPARC
10435 			 * only issues a Sync Cache to DVD-RAM, a newly
10436 			 * supported device.
10437 			 */
10438 #if defined(__i386) || defined(__amd64)
10439 			if ((un->un_f_sync_cache_supported &&
10440 			    un->un_f_sync_cache_required) ||
10441 			    un->un_f_dvdram_writable_device == TRUE) {
10442 #else
10443 			if (un->un_f_dvdram_writable_device == TRUE) {
10444 #endif
10445 				mutex_exit(SD_MUTEX(un));
10446 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10447 					rval =
10448 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10449 					    NULL);
10450 					/* ignore error if not supported */
10451 					if (rval == ENOTSUP) {
10452 						rval = 0;
10453 					} else if (rval != 0) {
10454 						rval = EIO;
10455 					}
10456 					sd_pm_exit(un);
10457 				} else {
10458 					rval = EIO;
10459 				}
10460 				mutex_enter(SD_MUTEX(un));
10461 			}
10462 
10463 			/*
10464 			 * For devices which supports DOOR_LOCK, send an ALLOW
10465 			 * MEDIA REMOVAL command, but don't get upset if it
10466 			 * fails. We need to raise the power of the drive before
10467 			 * we can call sd_send_scsi_DOORLOCK()
10468 			 */
10469 			if (un->un_f_doorlock_supported) {
10470 				mutex_exit(SD_MUTEX(un));
10471 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10472 					sd_ssc_t	*ssc;
10473 
10474 					ssc = sd_ssc_init(un);
10475 					rval = sd_send_scsi_DOORLOCK(ssc,
10476 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10477 					if (rval != 0)
10478 						sd_ssc_assessment(ssc,
10479 						    SD_FMT_IGNORE);
10480 					sd_ssc_fini(ssc);
10481 
10482 					sd_pm_exit(un);
10483 					if (ISCD(un) && (rval != 0) &&
10484 					    (nodelay != 0)) {
10485 						rval = ENXIO;
10486 					}
10487 				} else {
10488 					rval = EIO;
10489 				}
10490 				mutex_enter(SD_MUTEX(un));
10491 			}
10492 
10493 			/*
10494 			 * If a device has removable media, invalidate all
10495 			 * parameters related to media, such as geometry,
10496 			 * blocksize, and blockcount.
10497 			 */
10498 			if (un->un_f_has_removable_media) {
10499 				sr_ejected(un);
10500 			}
10501 
10502 			/*
10503 			 * Destroy the cache (if it exists) which was
10504 			 * allocated for the write maps since this is
10505 			 * the last close for this media.
10506 			 */
10507 			if (un->un_wm_cache) {
10508 				/*
10509 				 * Check if there are pending commands.
10510 				 * and if there are give a warning and
10511 				 * do not destroy the cache.
10512 				 */
10513 				if (un->un_ncmds_in_driver > 0) {
10514 					scsi_log(SD_DEVINFO(un),
10515 					    sd_label, CE_WARN,
10516 					    "Unable to clean up memory "
10517 					    "because of pending I/O\n");
10518 				} else {
10519 					kmem_cache_destroy(
10520 					    un->un_wm_cache);
10521 					un->un_wm_cache = NULL;
10522 				}
10523 			}
10524 		}
10525 	}
10526 
10527 	mutex_exit(SD_MUTEX(un));
10528 	sema_v(&un->un_semoclose);
10529 
10530 	if (otyp == OTYP_LYR) {
10531 		mutex_enter(&sd_detach_mutex);
10532 		/*
10533 		 * The detach routine may run when the layer count
10534 		 * drops to zero.
10535 		 */
10536 		un->un_layer_count--;
10537 		mutex_exit(&sd_detach_mutex);
10538 	}
10539 
10540 	return (rval);
10541 }
10542 
10543 
10544 /*
10545  *    Function: sd_ready_and_valid
10546  *
10547  * Description: Test if device is ready and has a valid geometry.
10548  *
10549  *   Arguments: ssc - sd_ssc_t will contain un
10550  *		un  - driver soft state (unit) structure
10551  *
10552  * Return Code: SD_READY_VALID		ready and valid label
10553  *		SD_NOT_READY_VALID	not ready, no label
10554  *		SD_RESERVED_BY_OTHERS	reservation conflict
10555  *
10556  *     Context: Never called at interrupt context.
10557  */
10558 
10559 static int
10560 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10561 {
10562 	struct sd_errstats	*stp;
10563 	uint64_t		capacity;
10564 	uint_t			lbasize;
10565 	int			rval = SD_READY_VALID;
10566 	char			name_str[48];
10567 	boolean_t		is_valid;
10568 	struct sd_lun		*un;
10569 	int			status;
10570 
10571 	ASSERT(ssc != NULL);
10572 	un = ssc->ssc_un;
10573 	ASSERT(un != NULL);
10574 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10575 
10576 	mutex_enter(SD_MUTEX(un));
10577 	/*
10578 	 * If a device has removable media, we must check if media is
10579 	 * ready when checking if this device is ready and valid.
10580 	 */
10581 	if (un->un_f_has_removable_media) {
10582 		mutex_exit(SD_MUTEX(un));
10583 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10584 
10585 		if (status != 0) {
10586 			rval = SD_NOT_READY_VALID;
10587 			mutex_enter(SD_MUTEX(un));
10588 
10589 			/* Ignore all failed status for removalbe media */
10590 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10591 
10592 			goto done;
10593 		}
10594 
10595 		is_valid = SD_IS_VALID_LABEL(un);
10596 		mutex_enter(SD_MUTEX(un));
10597 		if (!is_valid ||
10598 		    (un->un_f_blockcount_is_valid == FALSE) ||
10599 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10600 
10601 			/* capacity has to be read every open. */
10602 			mutex_exit(SD_MUTEX(un));
10603 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10604 			    &lbasize, SD_PATH_DIRECT);
10605 
10606 			if (status != 0) {
10607 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10608 
10609 				cmlb_invalidate(un->un_cmlbhandle,
10610 				    (void *)SD_PATH_DIRECT);
10611 				mutex_enter(SD_MUTEX(un));
10612 				rval = SD_NOT_READY_VALID;
10613 
10614 				goto done;
10615 			} else {
10616 				mutex_enter(SD_MUTEX(un));
10617 				sd_update_block_info(un, lbasize, capacity);
10618 			}
10619 		}
10620 
10621 		/*
10622 		 * Check if the media in the device is writable or not.
10623 		 */
10624 		if (!is_valid && ISCD(un)) {
10625 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10626 		}
10627 
10628 	} else {
10629 		/*
10630 		 * Do a test unit ready to clear any unit attention from non-cd
10631 		 * devices.
10632 		 */
10633 		mutex_exit(SD_MUTEX(un));
10634 
10635 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10636 		if (status != 0) {
10637 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10638 		}
10639 
10640 		mutex_enter(SD_MUTEX(un));
10641 	}
10642 
10643 
10644 	/*
10645 	 * If this is a non 512 block device, allocate space for
10646 	 * the wmap cache. This is being done here since every time
10647 	 * a media is changed this routine will be called and the
10648 	 * block size is a function of media rather than device.
10649 	 */
10650 	if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10651 	    un->un_f_non_devbsize_supported) &&
10652 	    un->un_tgt_blocksize != DEV_BSIZE) {
10653 		if (!(un->un_wm_cache)) {
10654 			(void) snprintf(name_str, sizeof (name_str),
10655 			    "%s%d_cache",
10656 			    ddi_driver_name(SD_DEVINFO(un)),
10657 			    ddi_get_instance(SD_DEVINFO(un)));
10658 			un->un_wm_cache = kmem_cache_create(
10659 			    name_str, sizeof (struct sd_w_map),
10660 			    8, sd_wm_cache_constructor,
10661 			    sd_wm_cache_destructor, NULL,
10662 			    (void *)un, NULL, 0);
10663 			if (!(un->un_wm_cache)) {
10664 				rval = ENOMEM;
10665 				goto done;
10666 			}
10667 		}
10668 	}
10669 
10670 	if (un->un_state == SD_STATE_NORMAL) {
10671 		/*
10672 		 * If the target is not yet ready here (defined by a TUR
10673 		 * failure), invalidate the geometry and print an 'offline'
10674 		 * message. This is a legacy message, as the state of the
10675 		 * target is not actually changed to SD_STATE_OFFLINE.
10676 		 *
10677 		 * If the TUR fails for EACCES (Reservation Conflict),
10678 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
10679 		 * reservation conflict. If the TUR fails for other
10680 		 * reasons, SD_NOT_READY_VALID will be returned.
10681 		 */
10682 		int err;
10683 
10684 		mutex_exit(SD_MUTEX(un));
10685 		err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10686 		mutex_enter(SD_MUTEX(un));
10687 
10688 		if (err != 0) {
10689 			mutex_exit(SD_MUTEX(un));
10690 			cmlb_invalidate(un->un_cmlbhandle,
10691 			    (void *)SD_PATH_DIRECT);
10692 			mutex_enter(SD_MUTEX(un));
10693 			if (err == EACCES) {
10694 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10695 				    "reservation conflict\n");
10696 				rval = SD_RESERVED_BY_OTHERS;
10697 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10698 			} else {
10699 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10700 				    "drive offline\n");
10701 				rval = SD_NOT_READY_VALID;
10702 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10703 			}
10704 			goto done;
10705 		}
10706 	}
10707 
10708 	if (un->un_f_format_in_progress == FALSE) {
10709 		mutex_exit(SD_MUTEX(un));
10710 
10711 		(void) cmlb_validate(un->un_cmlbhandle, 0,
10712 		    (void *)SD_PATH_DIRECT);
10713 		if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10714 		    NULL, (void *) SD_PATH_DIRECT) != 0) {
10715 			rval = SD_NOT_READY_VALID;
10716 			mutex_enter(SD_MUTEX(un));
10717 
10718 			goto done;
10719 		}
10720 		if (un->un_f_pkstats_enabled) {
10721 			sd_set_pstats(un);
10722 			SD_TRACE(SD_LOG_IO_PARTITION, un,
10723 			    "sd_ready_and_valid: un:0x%p pstats created and "
10724 			    "set\n", un);
10725 		}
10726 		mutex_enter(SD_MUTEX(un));
10727 	}
10728 
10729 	/*
10730 	 * If this device supports DOOR_LOCK command, try and send
10731 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10732 	 * if it fails. For a CD, however, it is an error
10733 	 */
10734 	if (un->un_f_doorlock_supported) {
10735 		mutex_exit(SD_MUTEX(un));
10736 		status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10737 		    SD_PATH_DIRECT);
10738 
10739 		if ((status != 0) && ISCD(un)) {
10740 			rval = SD_NOT_READY_VALID;
10741 			mutex_enter(SD_MUTEX(un));
10742 
10743 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10744 
10745 			goto done;
10746 		} else if (status != 0)
10747 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10748 		mutex_enter(SD_MUTEX(un));
10749 	}
10750 
10751 	/* The state has changed, inform the media watch routines */
10752 	un->un_mediastate = DKIO_INSERTED;
10753 	cv_broadcast(&un->un_state_cv);
10754 	rval = SD_READY_VALID;
10755 
10756 done:
10757 
10758 	/*
10759 	 * Initialize the capacity kstat value, if no media previously
10760 	 * (capacity kstat is 0) and a media has been inserted
10761 	 * (un_blockcount > 0).
10762 	 */
10763 	if (un->un_errstats != NULL) {
10764 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10765 		if ((stp->sd_capacity.value.ui64 == 0) &&
10766 		    (un->un_f_blockcount_is_valid == TRUE)) {
10767 			stp->sd_capacity.value.ui64 =
10768 			    (uint64_t)((uint64_t)un->un_blockcount *
10769 			    un->un_sys_blocksize);
10770 		}
10771 	}
10772 
10773 	mutex_exit(SD_MUTEX(un));
10774 	return (rval);
10775 }
10776 
10777 
10778 /*
10779  *    Function: sdmin
10780  *
10781  * Description: Routine to limit the size of a data transfer. Used in
10782  *		conjunction with physio(9F).
10783  *
10784  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10785  *
10786  *     Context: Kernel thread context.
10787  */
10788 
10789 static void
10790 sdmin(struct buf *bp)
10791 {
10792 	struct sd_lun	*un;
10793 	int		instance;
10794 
10795 	instance = SDUNIT(bp->b_edev);
10796 
10797 	un = ddi_get_soft_state(sd_state, instance);
10798 	ASSERT(un != NULL);
10799 
10800 	/*
10801 	 * We depend on DMA partial or buf breakup to restrict
10802 	 * IO size if any of them enabled.
10803 	 */
10804 	if (un->un_partial_dma_supported ||
10805 	    un->un_buf_breakup_supported) {
10806 		return;
10807 	}
10808 
10809 	if (bp->b_bcount > un->un_max_xfer_size) {
10810 		bp->b_bcount = un->un_max_xfer_size;
10811 	}
10812 }
10813 
10814 
10815 /*
10816  *    Function: sdread
10817  *
10818  * Description: Driver's read(9e) entry point function.
10819  *
10820  *   Arguments: dev   - device number
10821  *		uio   - structure pointer describing where data is to be stored
10822  *			in user's space
10823  *		cred_p  - user credential pointer
10824  *
10825  * Return Code: ENXIO
10826  *		EIO
10827  *		EINVAL
10828  *		value returned by physio
10829  *
10830  *     Context: Kernel thread context.
10831  */
10832 /* ARGSUSED */
10833 static int
10834 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10835 {
10836 	struct sd_lun	*un = NULL;
10837 	int		secmask;
10838 	int		err = 0;
10839 	sd_ssc_t	*ssc;
10840 
10841 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10842 		return (ENXIO);
10843 	}
10844 
10845 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10846 
10847 
10848 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10849 		mutex_enter(SD_MUTEX(un));
10850 		/*
10851 		 * Because the call to sd_ready_and_valid will issue I/O we
10852 		 * must wait here if either the device is suspended or
10853 		 * if it's power level is changing.
10854 		 */
10855 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10856 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10857 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10858 		}
10859 		un->un_ncmds_in_driver++;
10860 		mutex_exit(SD_MUTEX(un));
10861 
10862 		/* Initialize sd_ssc_t for internal uscsi commands */
10863 		ssc = sd_ssc_init(un);
10864 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10865 			err = EIO;
10866 		} else {
10867 			err = 0;
10868 		}
10869 		sd_ssc_fini(ssc);
10870 
10871 		mutex_enter(SD_MUTEX(un));
10872 		un->un_ncmds_in_driver--;
10873 		ASSERT(un->un_ncmds_in_driver >= 0);
10874 		mutex_exit(SD_MUTEX(un));
10875 		if (err != 0)
10876 			return (err);
10877 	}
10878 
10879 	/*
10880 	 * Read requests are restricted to multiples of the system block size.
10881 	 */
10882 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR)
10883 		secmask = un->un_tgt_blocksize - 1;
10884 	else
10885 		secmask = DEV_BSIZE - 1;
10886 
10887 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10888 		SD_ERROR(SD_LOG_READ_WRITE, un,
10889 		    "sdread: file offset not modulo %d\n",
10890 		    secmask + 1);
10891 		err = EINVAL;
10892 	} else if (uio->uio_iov->iov_len & (secmask)) {
10893 		SD_ERROR(SD_LOG_READ_WRITE, un,
10894 		    "sdread: transfer length not modulo %d\n",
10895 		    secmask + 1);
10896 		err = EINVAL;
10897 	} else {
10898 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10899 	}
10900 
10901 	return (err);
10902 }
10903 
10904 
10905 /*
10906  *    Function: sdwrite
10907  *
10908  * Description: Driver's write(9e) entry point function.
10909  *
10910  *   Arguments: dev   - device number
10911  *		uio   - structure pointer describing where data is stored in
10912  *			user's space
10913  *		cred_p  - user credential pointer
10914  *
10915  * Return Code: ENXIO
10916  *		EIO
10917  *		EINVAL
10918  *		value returned by physio
10919  *
10920  *     Context: Kernel thread context.
10921  */
10922 /* ARGSUSED */
10923 static int
10924 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10925 {
10926 	struct sd_lun	*un = NULL;
10927 	int		secmask;
10928 	int		err = 0;
10929 	sd_ssc_t	*ssc;
10930 
10931 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10932 		return (ENXIO);
10933 	}
10934 
10935 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10936 
10937 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10938 		mutex_enter(SD_MUTEX(un));
10939 		/*
10940 		 * Because the call to sd_ready_and_valid will issue I/O we
10941 		 * must wait here if either the device is suspended or
10942 		 * if it's power level is changing.
10943 		 */
10944 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10945 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10946 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10947 		}
10948 		un->un_ncmds_in_driver++;
10949 		mutex_exit(SD_MUTEX(un));
10950 
10951 		/* Initialize sd_ssc_t for internal uscsi commands */
10952 		ssc = sd_ssc_init(un);
10953 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10954 			err = EIO;
10955 		} else {
10956 			err = 0;
10957 		}
10958 		sd_ssc_fini(ssc);
10959 
10960 		mutex_enter(SD_MUTEX(un));
10961 		un->un_ncmds_in_driver--;
10962 		ASSERT(un->un_ncmds_in_driver >= 0);
10963 		mutex_exit(SD_MUTEX(un));
10964 		if (err != 0)
10965 			return (err);
10966 	}
10967 
10968 	/*
10969 	 * Write requests are restricted to multiples of the system block size.
10970 	 */
10971 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR)
10972 		secmask = un->un_tgt_blocksize - 1;
10973 	else
10974 		secmask = DEV_BSIZE - 1;
10975 
10976 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10977 		SD_ERROR(SD_LOG_READ_WRITE, un,
10978 		    "sdwrite: file offset not modulo %d\n",
10979 		    secmask + 1);
10980 		err = EINVAL;
10981 	} else if (uio->uio_iov->iov_len & (secmask)) {
10982 		SD_ERROR(SD_LOG_READ_WRITE, un,
10983 		    "sdwrite: transfer length not modulo %d\n",
10984 		    secmask + 1);
10985 		err = EINVAL;
10986 	} else {
10987 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
10988 	}
10989 
10990 	return (err);
10991 }
10992 
10993 
10994 /*
10995  *    Function: sdaread
10996  *
10997  * Description: Driver's aread(9e) entry point function.
10998  *
10999  *   Arguments: dev   - device number
11000  *		aio   - structure pointer describing where data is to be stored
11001  *		cred_p  - user credential pointer
11002  *
11003  * Return Code: ENXIO
11004  *		EIO
11005  *		EINVAL
11006  *		value returned by aphysio
11007  *
11008  *     Context: Kernel thread context.
11009  */
11010 /* ARGSUSED */
11011 static int
11012 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11013 {
11014 	struct sd_lun	*un = NULL;
11015 	struct uio	*uio = aio->aio_uio;
11016 	int		secmask;
11017 	int		err = 0;
11018 	sd_ssc_t	*ssc;
11019 
11020 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11021 		return (ENXIO);
11022 	}
11023 
11024 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11025 
11026 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11027 		mutex_enter(SD_MUTEX(un));
11028 		/*
11029 		 * Because the call to sd_ready_and_valid will issue I/O we
11030 		 * must wait here if either the device is suspended or
11031 		 * if it's power level is changing.
11032 		 */
11033 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11034 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11035 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11036 		}
11037 		un->un_ncmds_in_driver++;
11038 		mutex_exit(SD_MUTEX(un));
11039 
11040 		/* Initialize sd_ssc_t for internal uscsi commands */
11041 		ssc = sd_ssc_init(un);
11042 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11043 			err = EIO;
11044 		} else {
11045 			err = 0;
11046 		}
11047 		sd_ssc_fini(ssc);
11048 
11049 		mutex_enter(SD_MUTEX(un));
11050 		un->un_ncmds_in_driver--;
11051 		ASSERT(un->un_ncmds_in_driver >= 0);
11052 		mutex_exit(SD_MUTEX(un));
11053 		if (err != 0)
11054 			return (err);
11055 	}
11056 
11057 	/*
11058 	 * Read requests are restricted to multiples of the system block size.
11059 	 */
11060 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR)
11061 		secmask = un->un_tgt_blocksize - 1;
11062 	else
11063 		secmask = DEV_BSIZE - 1;
11064 
11065 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11066 		SD_ERROR(SD_LOG_READ_WRITE, un,
11067 		    "sdaread: file offset not modulo %d\n",
11068 		    secmask + 1);
11069 		err = EINVAL;
11070 	} else if (uio->uio_iov->iov_len & (secmask)) {
11071 		SD_ERROR(SD_LOG_READ_WRITE, un,
11072 		    "sdaread: transfer length not modulo %d\n",
11073 		    secmask + 1);
11074 		err = EINVAL;
11075 	} else {
11076 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11077 	}
11078 
11079 	return (err);
11080 }
11081 
11082 
11083 /*
11084  *    Function: sdawrite
11085  *
11086  * Description: Driver's awrite(9e) entry point function.
11087  *
11088  *   Arguments: dev   - device number
11089  *		aio   - structure pointer describing where data is stored
11090  *		cred_p  - user credential pointer
11091  *
11092  * Return Code: ENXIO
11093  *		EIO
11094  *		EINVAL
11095  *		value returned by aphysio
11096  *
11097  *     Context: Kernel thread context.
11098  */
11099 /* ARGSUSED */
11100 static int
11101 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11102 {
11103 	struct sd_lun	*un = NULL;
11104 	struct uio	*uio = aio->aio_uio;
11105 	int		secmask;
11106 	int		err = 0;
11107 	sd_ssc_t	*ssc;
11108 
11109 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11110 		return (ENXIO);
11111 	}
11112 
11113 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11114 
11115 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11116 		mutex_enter(SD_MUTEX(un));
11117 		/*
11118 		 * Because the call to sd_ready_and_valid will issue I/O we
11119 		 * must wait here if either the device is suspended or
11120 		 * if it's power level is changing.
11121 		 */
11122 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11123 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11124 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11125 		}
11126 		un->un_ncmds_in_driver++;
11127 		mutex_exit(SD_MUTEX(un));
11128 
11129 		/* Initialize sd_ssc_t for internal uscsi commands */
11130 		ssc = sd_ssc_init(un);
11131 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11132 			err = EIO;
11133 		} else {
11134 			err = 0;
11135 		}
11136 		sd_ssc_fini(ssc);
11137 
11138 		mutex_enter(SD_MUTEX(un));
11139 		un->un_ncmds_in_driver--;
11140 		ASSERT(un->un_ncmds_in_driver >= 0);
11141 		mutex_exit(SD_MUTEX(un));
11142 		if (err != 0)
11143 			return (err);
11144 	}
11145 
11146 	/*
11147 	 * Write requests are restricted to multiples of the system block size.
11148 	 */
11149 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR)
11150 		secmask = un->un_tgt_blocksize - 1;
11151 	else
11152 		secmask = DEV_BSIZE - 1;
11153 
11154 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11155 		SD_ERROR(SD_LOG_READ_WRITE, un,
11156 		    "sdawrite: file offset not modulo %d\n",
11157 		    secmask + 1);
11158 		err = EINVAL;
11159 	} else if (uio->uio_iov->iov_len & (secmask)) {
11160 		SD_ERROR(SD_LOG_READ_WRITE, un,
11161 		    "sdawrite: transfer length not modulo %d\n",
11162 		    secmask + 1);
11163 		err = EINVAL;
11164 	} else {
11165 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11166 	}
11167 
11168 	return (err);
11169 }
11170 
11171 
11172 
11173 
11174 
11175 /*
11176  * Driver IO processing follows the following sequence:
11177  *
11178  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11179  *         |                |                     ^
11180  *         v                v                     |
11181  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11182  *         |                |                     |                   |
11183  *         v                |                     |                   |
11184  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11185  *         |                |                     ^                   ^
11186  *         v                v                     |                   |
11187  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11188  *         |                |                     |                   |
11189  *     +---+                |                     +------------+      +-------+
11190  *     |                    |                                  |              |
11191  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11192  *     |                    v                                  |              |
11193  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11194  *     |                    |                                  ^              |
11195  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11196  *     |                    v                                  |              |
11197  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11198  *     |                    |                                  ^              |
11199  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11200  *     |                    v                                  |              |
11201  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11202  *     |                    |                                  ^              |
11203  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11204  *     |                    v                                  |              |
11205  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11206  *     |                    |                                  ^              |
11207  *     |                    |                                  |              |
11208  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11209  *                          |                           ^
11210  *                          v                           |
11211  *                   sd_core_iostart()                  |
11212  *                          |                           |
11213  *                          |                           +------>(*destroypkt)()
11214  *                          +-> sd_start_cmds() <-+     |           |
11215  *                          |                     |     |           v
11216  *                          |                     |     |  scsi_destroy_pkt(9F)
11217  *                          |                     |     |
11218  *                          +->(*initpkt)()       +- sdintr()
11219  *                          |  |                        |  |
11220  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11221  *                          |  +-> scsi_setup_cdb(9F)   |
11222  *                          |                           |
11223  *                          +--> scsi_transport(9F)     |
11224  *                                     |                |
11225  *                                     +----> SCSA ---->+
11226  *
11227  *
11228  * This code is based upon the following presumptions:
11229  *
11230  *   - iostart and iodone functions operate on buf(9S) structures. These
11231  *     functions perform the necessary operations on the buf(9S) and pass
11232  *     them along to the next function in the chain by using the macros
11233  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11234  *     (for iodone side functions).
11235  *
11236  *   - The iostart side functions may sleep. The iodone side functions
11237  *     are called under interrupt context and may NOT sleep. Therefore
11238  *     iodone side functions also may not call iostart side functions.
11239  *     (NOTE: iostart side functions should NOT sleep for memory, as
11240  *     this could result in deadlock.)
11241  *
11242  *   - An iostart side function may call its corresponding iodone side
11243  *     function directly (if necessary).
11244  *
11245  *   - In the event of an error, an iostart side function can return a buf(9S)
11246  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11247  *     b_error in the usual way of course).
11248  *
11249  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11250  *     requests to the iostart side functions.  The iostart side functions in
11251  *     this case would be called under the context of a taskq thread, so it's
11252  *     OK for them to block/sleep/spin in this case.
11253  *
11254  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11255  *     pass them along to the next function in the chain.  The corresponding
11256  *     iodone side functions must coalesce the "shadow" bufs and return
11257  *     the "original" buf to the next higher layer.
11258  *
11259  *   - The b_private field of the buf(9S) struct holds a pointer to
11260  *     an sd_xbuf struct, which contains information needed to
11261  *     construct the scsi_pkt for the command.
11262  *
11263  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11264  *     layer must acquire & release the SD_MUTEX(un) as needed.
11265  */
11266 
11267 
11268 /*
11269  * Create taskq for all targets in the system. This is created at
11270  * _init(9E) and destroyed at _fini(9E).
11271  *
11272  * Note: here we set the minalloc to a reasonably high number to ensure that
11273  * we will have an adequate supply of task entries available at interrupt time.
11274  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11275  * sd_create_taskq().  Since we do not want to sleep for allocations at
11276  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11277  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11278  * requests any one instant in time.
11279  */
11280 #define	SD_TASKQ_NUMTHREADS	8
11281 #define	SD_TASKQ_MINALLOC	256
11282 #define	SD_TASKQ_MAXALLOC	256
11283 
11284 static taskq_t	*sd_tq = NULL;
11285 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11286 
11287 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11288 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11289 
11290 /*
11291  * The following task queue is being created for the write part of
11292  * read-modify-write of non-512 block size devices.
11293  * Limit the number of threads to 1 for now. This number has been chosen
11294  * considering the fact that it applies only to dvd ram drives/MO drives
11295  * currently. Performance for which is not main criteria at this stage.
11296  * Note: It needs to be explored if we can use a single taskq in future
11297  */
11298 #define	SD_WMR_TASKQ_NUMTHREADS	1
11299 static taskq_t	*sd_wmr_tq = NULL;
11300 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11301 
11302 /*
11303  *    Function: sd_taskq_create
11304  *
11305  * Description: Create taskq thread(s) and preallocate task entries
11306  *
11307  * Return Code: Returns a pointer to the allocated taskq_t.
11308  *
11309  *     Context: Can sleep. Requires blockable context.
11310  *
11311  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11312  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11313  *		- taskq_create() will block for memory, also it will panic
11314  *		  if it cannot create the requested number of threads.
11315  *		- Currently taskq_create() creates threads that cannot be
11316  *		  swapped.
11317  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11318  *		  supply of taskq entries at interrupt time (ie, so that we
11319  *		  do not have to sleep for memory)
11320  */
11321 
11322 static void
11323 sd_taskq_create(void)
11324 {
11325 	char	taskq_name[TASKQ_NAMELEN];
11326 
11327 	ASSERT(sd_tq == NULL);
11328 	ASSERT(sd_wmr_tq == NULL);
11329 
11330 	(void) snprintf(taskq_name, sizeof (taskq_name),
11331 	    "%s_drv_taskq", sd_label);
11332 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11333 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11334 	    TASKQ_PREPOPULATE));
11335 
11336 	(void) snprintf(taskq_name, sizeof (taskq_name),
11337 	    "%s_rmw_taskq", sd_label);
11338 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11339 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11340 	    TASKQ_PREPOPULATE));
11341 }
11342 
11343 
11344 /*
11345  *    Function: sd_taskq_delete
11346  *
11347  * Description: Complementary cleanup routine for sd_taskq_create().
11348  *
11349  *     Context: Kernel thread context.
11350  */
11351 
11352 static void
11353 sd_taskq_delete(void)
11354 {
11355 	ASSERT(sd_tq != NULL);
11356 	ASSERT(sd_wmr_tq != NULL);
11357 	taskq_destroy(sd_tq);
11358 	taskq_destroy(sd_wmr_tq);
11359 	sd_tq = NULL;
11360 	sd_wmr_tq = NULL;
11361 }
11362 
11363 
11364 /*
11365  *    Function: sdstrategy
11366  *
11367  * Description: Driver's strategy (9E) entry point function.
11368  *
11369  *   Arguments: bp - pointer to buf(9S)
11370  *
11371  * Return Code: Always returns zero
11372  *
11373  *     Context: Kernel thread context.
11374  */
11375 
11376 static int
11377 sdstrategy(struct buf *bp)
11378 {
11379 	struct sd_lun *un;
11380 
11381 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11382 	if (un == NULL) {
11383 		bioerror(bp, EIO);
11384 		bp->b_resid = bp->b_bcount;
11385 		biodone(bp);
11386 		return (0);
11387 	}
11388 
11389 	/* As was done in the past, fail new cmds. if state is dumping. */
11390 	if (un->un_state == SD_STATE_DUMPING) {
11391 		bioerror(bp, ENXIO);
11392 		bp->b_resid = bp->b_bcount;
11393 		biodone(bp);
11394 		return (0);
11395 	}
11396 
11397 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11398 
11399 	/*
11400 	 * Commands may sneak in while we released the mutex in
11401 	 * DDI_SUSPEND, we should block new commands. However, old
11402 	 * commands that are still in the driver at this point should
11403 	 * still be allowed to drain.
11404 	 */
11405 	mutex_enter(SD_MUTEX(un));
11406 	/*
11407 	 * Must wait here if either the device is suspended or
11408 	 * if it's power level is changing.
11409 	 */
11410 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11411 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11412 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11413 	}
11414 
11415 	un->un_ncmds_in_driver++;
11416 
11417 	/*
11418 	 * atapi: Since we are running the CD for now in PIO mode we need to
11419 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11420 	 * the HBA's init_pkt routine.
11421 	 */
11422 	if (un->un_f_cfg_is_atapi == TRUE) {
11423 		mutex_exit(SD_MUTEX(un));
11424 		bp_mapin(bp);
11425 		mutex_enter(SD_MUTEX(un));
11426 	}
11427 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11428 	    un->un_ncmds_in_driver);
11429 
11430 	if (bp->b_flags & B_WRITE)
11431 		un->un_f_sync_cache_required = TRUE;
11432 
11433 	mutex_exit(SD_MUTEX(un));
11434 
11435 	/*
11436 	 * This will (eventually) allocate the sd_xbuf area and
11437 	 * call sd_xbuf_strategy().  We just want to return the
11438 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11439 	 * imized tail call which saves us a stack frame.
11440 	 */
11441 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11442 }
11443 
11444 
11445 /*
11446  *    Function: sd_xbuf_strategy
11447  *
11448  * Description: Function for initiating IO operations via the
11449  *		ddi_xbuf_qstrategy() mechanism.
11450  *
11451  *     Context: Kernel thread context.
11452  */
11453 
11454 static void
11455 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11456 {
11457 	struct sd_lun *un = arg;
11458 
11459 	ASSERT(bp != NULL);
11460 	ASSERT(xp != NULL);
11461 	ASSERT(un != NULL);
11462 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11463 
11464 	/*
11465 	 * Initialize the fields in the xbuf and save a pointer to the
11466 	 * xbuf in bp->b_private.
11467 	 */
11468 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11469 
11470 	/* Send the buf down the iostart chain */
11471 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11472 }
11473 
11474 
11475 /*
11476  *    Function: sd_xbuf_init
11477  *
11478  * Description: Prepare the given sd_xbuf struct for use.
11479  *
11480  *   Arguments: un - ptr to softstate
11481  *		bp - ptr to associated buf(9S)
11482  *		xp - ptr to associated sd_xbuf
11483  *		chain_type - IO chain type to use:
11484  *			SD_CHAIN_NULL
11485  *			SD_CHAIN_BUFIO
11486  *			SD_CHAIN_USCSI
11487  *			SD_CHAIN_DIRECT
11488  *			SD_CHAIN_DIRECT_PRIORITY
11489  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11490  *			initialization; may be NULL if none.
11491  *
11492  *     Context: Kernel thread context
11493  */
11494 
11495 static void
11496 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11497 	uchar_t chain_type, void *pktinfop)
11498 {
11499 	int index;
11500 
11501 	ASSERT(un != NULL);
11502 	ASSERT(bp != NULL);
11503 	ASSERT(xp != NULL);
11504 
11505 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11506 	    bp, chain_type);
11507 
11508 	xp->xb_un	= un;
11509 	xp->xb_pktp	= NULL;
11510 	xp->xb_pktinfo	= pktinfop;
11511 	xp->xb_private	= bp->b_private;
11512 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11513 
11514 	/*
11515 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11516 	 * upon the specified chain type to use.
11517 	 */
11518 	switch (chain_type) {
11519 	case SD_CHAIN_NULL:
11520 		/*
11521 		 * Fall thru to just use the values for the buf type, even
11522 		 * tho for the NULL chain these values will never be used.
11523 		 */
11524 		/* FALLTHRU */
11525 	case SD_CHAIN_BUFIO:
11526 		index = un->un_buf_chain_type;
11527 		if ((!un->un_f_has_removable_media) &&
11528 		    (un->un_tgt_blocksize != 0) &&
11529 		    (un->un_tgt_blocksize != DEV_BSIZE)) {
11530 			int secmask = 0, blknomask = 0;
11531 			blknomask =
11532 			    (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11533 			secmask = un->un_tgt_blocksize - 1;
11534 
11535 			if ((bp->b_lblkno & (blknomask)) ||
11536 			    (bp->b_bcount & (secmask))) {
11537 				if (un->un_f_rmw_type !=
11538 				    SD_RMW_TYPE_RETURN_ERROR) {
11539 					if (un->un_f_pm_is_enabled == FALSE)
11540 						index =
11541 						    SD_CHAIN_INFO_MSS_DSK_NO_PM;
11542 					else
11543 						index =
11544 						    SD_CHAIN_INFO_MSS_DISK;
11545 				}
11546 			}
11547 		}
11548 		break;
11549 	case SD_CHAIN_USCSI:
11550 		index = un->un_uscsi_chain_type;
11551 		break;
11552 	case SD_CHAIN_DIRECT:
11553 		index = un->un_direct_chain_type;
11554 		break;
11555 	case SD_CHAIN_DIRECT_PRIORITY:
11556 		index = un->un_priority_chain_type;
11557 		break;
11558 	default:
11559 		/* We're really broken if we ever get here... */
11560 		panic("sd_xbuf_init: illegal chain type!");
11561 		/*NOTREACHED*/
11562 	}
11563 
11564 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11565 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11566 
11567 	/*
11568 	 * It might be a bit easier to simply bzero the entire xbuf above,
11569 	 * but it turns out that since we init a fair number of members anyway,
11570 	 * we save a fair number cycles by doing explicit assignment of zero.
11571 	 */
11572 	xp->xb_pkt_flags	= 0;
11573 	xp->xb_dma_resid	= 0;
11574 	xp->xb_retry_count	= 0;
11575 	xp->xb_victim_retry_count = 0;
11576 	xp->xb_ua_retry_count	= 0;
11577 	xp->xb_nr_retry_count	= 0;
11578 	xp->xb_sense_bp		= NULL;
11579 	xp->xb_sense_status	= 0;
11580 	xp->xb_sense_state	= 0;
11581 	xp->xb_sense_resid	= 0;
11582 	xp->xb_ena		= 0;
11583 
11584 	bp->b_private	= xp;
11585 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11586 	bp->b_resid	= 0;
11587 	bp->av_forw	= NULL;
11588 	bp->av_back	= NULL;
11589 	bioerror(bp, 0);
11590 
11591 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11592 }
11593 
11594 
11595 /*
11596  *    Function: sd_uscsi_strategy
11597  *
11598  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11599  *
11600  *   Arguments: bp - buf struct ptr
11601  *
11602  * Return Code: Always returns 0
11603  *
11604  *     Context: Kernel thread context
11605  */
11606 
11607 static int
11608 sd_uscsi_strategy(struct buf *bp)
11609 {
11610 	struct sd_lun		*un;
11611 	struct sd_uscsi_info	*uip;
11612 	struct sd_xbuf		*xp;
11613 	uchar_t			chain_type;
11614 	uchar_t			cmd;
11615 
11616 	ASSERT(bp != NULL);
11617 
11618 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11619 	if (un == NULL) {
11620 		bioerror(bp, EIO);
11621 		bp->b_resid = bp->b_bcount;
11622 		biodone(bp);
11623 		return (0);
11624 	}
11625 
11626 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11627 
11628 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11629 
11630 	/*
11631 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11632 	 */
11633 	ASSERT(bp->b_private != NULL);
11634 	uip = (struct sd_uscsi_info *)bp->b_private;
11635 	cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11636 
11637 	mutex_enter(SD_MUTEX(un));
11638 	/*
11639 	 * atapi: Since we are running the CD for now in PIO mode we need to
11640 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11641 	 * the HBA's init_pkt routine.
11642 	 */
11643 	if (un->un_f_cfg_is_atapi == TRUE) {
11644 		mutex_exit(SD_MUTEX(un));
11645 		bp_mapin(bp);
11646 		mutex_enter(SD_MUTEX(un));
11647 	}
11648 	un->un_ncmds_in_driver++;
11649 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11650 	    un->un_ncmds_in_driver);
11651 
11652 	if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11653 	    (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11654 		un->un_f_sync_cache_required = TRUE;
11655 
11656 	mutex_exit(SD_MUTEX(un));
11657 
11658 	switch (uip->ui_flags) {
11659 	case SD_PATH_DIRECT:
11660 		chain_type = SD_CHAIN_DIRECT;
11661 		break;
11662 	case SD_PATH_DIRECT_PRIORITY:
11663 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11664 		break;
11665 	default:
11666 		chain_type = SD_CHAIN_USCSI;
11667 		break;
11668 	}
11669 
11670 	/*
11671 	 * We may allocate extra buf for external USCSI commands. If the
11672 	 * application asks for bigger than 20-byte sense data via USCSI,
11673 	 * SCSA layer will allocate 252 bytes sense buf for that command.
11674 	 */
11675 	if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11676 	    SENSE_LENGTH) {
11677 		xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11678 		    MAX_SENSE_LENGTH, KM_SLEEP);
11679 	} else {
11680 		xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11681 	}
11682 
11683 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11684 
11685 	/* Use the index obtained within xbuf_init */
11686 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11687 
11688 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11689 
11690 	return (0);
11691 }
11692 
11693 /*
11694  *    Function: sd_send_scsi_cmd
11695  *
11696  * Description: Runs a USCSI command for user (when called thru sdioctl),
11697  *		or for the driver
11698  *
11699  *   Arguments: dev - the dev_t for the device
11700  *		incmd - ptr to a valid uscsi_cmd struct
11701  *		flag - bit flag, indicating open settings, 32/64 bit type
11702  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11703  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11704  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11705  *			to use the USCSI "direct" chain and bypass the normal
11706  *			command waitq.
11707  *
11708  * Return Code: 0 -  successful completion of the given command
11709  *		EIO - scsi_uscsi_handle_command() failed
11710  *		ENXIO  - soft state not found for specified dev
11711  *		EINVAL
11712  *		EFAULT - copyin/copyout error
11713  *		return code of scsi_uscsi_handle_command():
11714  *			EIO
11715  *			ENXIO
11716  *			EACCES
11717  *
11718  *     Context: Waits for command to complete. Can sleep.
11719  */
11720 
11721 static int
11722 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11723 	enum uio_seg dataspace, int path_flag)
11724 {
11725 	struct sd_lun	*un;
11726 	sd_ssc_t	*ssc;
11727 	int		rval;
11728 
11729 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11730 	if (un == NULL) {
11731 		return (ENXIO);
11732 	}
11733 
11734 	/*
11735 	 * Using sd_ssc_send to handle uscsi cmd
11736 	 */
11737 	ssc = sd_ssc_init(un);
11738 	rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11739 	sd_ssc_fini(ssc);
11740 
11741 	return (rval);
11742 }
11743 
11744 /*
11745  *    Function: sd_ssc_init
11746  *
11747  * Description: Uscsi end-user call this function to initialize necessary
11748  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11749  *
11750  *              The return value of sd_send_scsi_cmd will be treated as a
11751  *              fault in various conditions. Even it is not Zero, some
11752  *              callers may ignore the return value. That is to say, we can
11753  *              not make an accurate assessment in sdintr, since if a
11754  *              command is failed in sdintr it does not mean the caller of
11755  *              sd_send_scsi_cmd will treat it as a real failure.
11756  *
11757  *              To avoid printing too many error logs for a failed uscsi
11758  *              packet that the caller may not treat it as a failure, the
11759  *              sd will keep silent for handling all uscsi commands.
11760  *
11761  *              During detach->attach and attach-open, for some types of
11762  *              problems, the driver should be providing information about
11763  *              the problem encountered. Device use USCSI_SILENT, which
11764  *              suppresses all driver information. The result is that no
11765  *              information about the problem is available. Being
11766  *              completely silent during this time is inappropriate. The
11767  *              driver needs a more selective filter than USCSI_SILENT, so
11768  *              that information related to faults is provided.
11769  *
11770  *              To make the accurate accessment, the caller  of
11771  *              sd_send_scsi_USCSI_CMD should take the ownership and
11772  *              get necessary information to print error messages.
11773  *
11774  *              If we want to print necessary info of uscsi command, we need to
11775  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11776  *              assessment. We use sd_ssc_init to alloc necessary
11777  *              structs for sending an uscsi command and we are also
11778  *              responsible for free the memory by calling
11779  *              sd_ssc_fini.
11780  *
11781  *              The calling secquences will look like:
11782  *              sd_ssc_init->
11783  *
11784  *                  ...
11785  *
11786  *                  sd_send_scsi_USCSI_CMD->
11787  *                      sd_ssc_send-> - - - sdintr
11788  *                  ...
11789  *
11790  *                  if we think the return value should be treated as a
11791  *                  failure, we make the accessment here and print out
11792  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11793  *
11794  *                  ...
11795  *
11796  *              sd_ssc_fini
11797  *
11798  *
11799  *   Arguments: un - pointer to driver soft state (unit) structure for this
11800  *                   target.
11801  *
11802  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11803  *                         uscsi_cmd and sd_uscsi_info.
11804  *                  NULL - if can not alloc memory for sd_ssc_t struct
11805  *
11806  *     Context: Kernel Thread.
11807  */
11808 static sd_ssc_t *
11809 sd_ssc_init(struct sd_lun *un)
11810 {
11811 	sd_ssc_t		*ssc;
11812 	struct uscsi_cmd	*ucmdp;
11813 	struct sd_uscsi_info	*uip;
11814 
11815 	ASSERT(un != NULL);
11816 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11817 
11818 	/*
11819 	 * Allocate sd_ssc_t structure
11820 	 */
11821 	ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11822 
11823 	/*
11824 	 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11825 	 */
11826 	ucmdp = scsi_uscsi_alloc();
11827 
11828 	/*
11829 	 * Allocate sd_uscsi_info structure
11830 	 */
11831 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11832 
11833 	ssc->ssc_uscsi_cmd = ucmdp;
11834 	ssc->ssc_uscsi_info = uip;
11835 	ssc->ssc_un = un;
11836 
11837 	return (ssc);
11838 }
11839 
11840 /*
11841  * Function: sd_ssc_fini
11842  *
11843  * Description: To free sd_ssc_t and it's hanging off
11844  *
11845  * Arguments: ssc - struct pointer of sd_ssc_t.
11846  */
11847 static void
11848 sd_ssc_fini(sd_ssc_t *ssc)
11849 {
11850 	scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11851 
11852 	if (ssc->ssc_uscsi_info != NULL) {
11853 		kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11854 		ssc->ssc_uscsi_info = NULL;
11855 	}
11856 
11857 	kmem_free(ssc, sizeof (sd_ssc_t));
11858 	ssc = NULL;
11859 }
11860 
11861 /*
11862  * Function: sd_ssc_send
11863  *
11864  * Description: Runs a USCSI command for user when called through sdioctl,
11865  *              or for the driver.
11866  *
11867  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11868  *                    sd_uscsi_info in.
11869  *		incmd - ptr to a valid uscsi_cmd struct
11870  *		flag - bit flag, indicating open settings, 32/64 bit type
11871  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11872  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11873  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11874  *			to use the USCSI "direct" chain and bypass the normal
11875  *			command waitq.
11876  *
11877  * Return Code: 0 -  successful completion of the given command
11878  *		EIO - scsi_uscsi_handle_command() failed
11879  *		ENXIO  - soft state not found for specified dev
11880  *		ECANCELED - command cancelled due to low power
11881  *		EINVAL
11882  *		EFAULT - copyin/copyout error
11883  *		return code of scsi_uscsi_handle_command():
11884  *			EIO
11885  *			ENXIO
11886  *			EACCES
11887  *
11888  *     Context: Kernel Thread;
11889  *              Waits for command to complete. Can sleep.
11890  */
11891 static int
11892 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11893 	enum uio_seg dataspace, int path_flag)
11894 {
11895 	struct sd_uscsi_info	*uip;
11896 	struct uscsi_cmd	*uscmd;
11897 	struct sd_lun		*un;
11898 	dev_t			dev;
11899 
11900 	int	format = 0;
11901 	int	rval;
11902 
11903 	ASSERT(ssc != NULL);
11904 	un = ssc->ssc_un;
11905 	ASSERT(un != NULL);
11906 	uscmd = ssc->ssc_uscsi_cmd;
11907 	ASSERT(uscmd != NULL);
11908 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11909 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11910 		/*
11911 		 * If enter here, it indicates that the previous uscsi
11912 		 * command has not been processed by sd_ssc_assessment.
11913 		 * This is violating our rules of FMA telemetry processing.
11914 		 * We should print out this message and the last undisposed
11915 		 * uscsi command.
11916 		 */
11917 		if (uscmd->uscsi_cdb != NULL) {
11918 			SD_INFO(SD_LOG_SDTEST, un,
11919 			    "sd_ssc_send is missing the alternative "
11920 			    "sd_ssc_assessment when running command 0x%x.\n",
11921 			    uscmd->uscsi_cdb[0]);
11922 		}
11923 		/*
11924 		 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11925 		 * the initial status.
11926 		 */
11927 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11928 	}
11929 
11930 	/*
11931 	 * We need to make sure sd_ssc_send will have sd_ssc_assessment
11932 	 * followed to avoid missing FMA telemetries.
11933 	 */
11934 	ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11935 
11936 	/*
11937 	 * if USCSI_PMFAILFAST is set and un is in low power, fail the
11938 	 * command immediately.
11939 	 */
11940 	mutex_enter(SD_MUTEX(un));
11941 	mutex_enter(&un->un_pm_mutex);
11942 	if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11943 	    SD_DEVICE_IS_IN_LOW_POWER(un)) {
11944 		SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11945 		    "un:0x%p is in low power\n", un);
11946 		mutex_exit(&un->un_pm_mutex);
11947 		mutex_exit(SD_MUTEX(un));
11948 		return (ECANCELED);
11949 	}
11950 	mutex_exit(&un->un_pm_mutex);
11951 	mutex_exit(SD_MUTEX(un));
11952 
11953 #ifdef SDDEBUG
11954 	switch (dataspace) {
11955 	case UIO_USERSPACE:
11956 		SD_TRACE(SD_LOG_IO, un,
11957 		    "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
11958 		break;
11959 	case UIO_SYSSPACE:
11960 		SD_TRACE(SD_LOG_IO, un,
11961 		    "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
11962 		break;
11963 	default:
11964 		SD_TRACE(SD_LOG_IO, un,
11965 		    "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
11966 		break;
11967 	}
11968 #endif
11969 
11970 	rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
11971 	    SD_ADDRESS(un), &uscmd);
11972 	if (rval != 0) {
11973 		SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
11974 		    "scsi_uscsi_alloc_and_copyin failed\n", un);
11975 		return (rval);
11976 	}
11977 
11978 	if ((uscmd->uscsi_cdb != NULL) &&
11979 	    (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
11980 		mutex_enter(SD_MUTEX(un));
11981 		un->un_f_format_in_progress = TRUE;
11982 		mutex_exit(SD_MUTEX(un));
11983 		format = 1;
11984 	}
11985 
11986 	/*
11987 	 * Allocate an sd_uscsi_info struct and fill it with the info
11988 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
11989 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
11990 	 * since we allocate the buf here in this function, we do not
11991 	 * need to preserve the prior contents of b_private.
11992 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11993 	 */
11994 	uip = ssc->ssc_uscsi_info;
11995 	uip->ui_flags = path_flag;
11996 	uip->ui_cmdp = uscmd;
11997 
11998 	/*
11999 	 * Commands sent with priority are intended for error recovery
12000 	 * situations, and do not have retries performed.
12001 	 */
12002 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12003 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12004 	}
12005 	uscmd->uscsi_flags &= ~USCSI_NOINTR;
12006 
12007 	dev = SD_GET_DEV(un);
12008 	rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12009 	    sd_uscsi_strategy, NULL, uip);
12010 
12011 	/*
12012 	 * mark ssc_flags right after handle_cmd to make sure
12013 	 * the uscsi has been sent
12014 	 */
12015 	ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12016 
12017 #ifdef SDDEBUG
12018 	SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12019 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12020 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12021 	if (uscmd->uscsi_bufaddr != NULL) {
12022 		SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12023 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12024 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12025 		if (dataspace == UIO_SYSSPACE) {
12026 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12027 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12028 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12029 		}
12030 	}
12031 #endif
12032 
12033 	if (format == 1) {
12034 		mutex_enter(SD_MUTEX(un));
12035 		un->un_f_format_in_progress = FALSE;
12036 		mutex_exit(SD_MUTEX(un));
12037 	}
12038 
12039 	(void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12040 
12041 	return (rval);
12042 }
12043 
12044 /*
12045  *     Function: sd_ssc_print
12046  *
12047  * Description: Print information available to the console.
12048  *
12049  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12050  *                    sd_uscsi_info in.
12051  *            sd_severity - log level.
12052  *     Context: Kernel thread or interrupt context.
12053  */
12054 static void
12055 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12056 {
12057 	struct uscsi_cmd	*ucmdp;
12058 	struct scsi_device	*devp;
12059 	dev_info_t 		*devinfo;
12060 	uchar_t			*sensep;
12061 	int			senlen;
12062 	union scsi_cdb		*cdbp;
12063 	uchar_t			com;
12064 	extern struct scsi_key_strings scsi_cmds[];
12065 
12066 	ASSERT(ssc != NULL);
12067 	ASSERT(ssc->ssc_un != NULL);
12068 
12069 	if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12070 		return;
12071 	ucmdp = ssc->ssc_uscsi_cmd;
12072 	devp = SD_SCSI_DEVP(ssc->ssc_un);
12073 	devinfo = SD_DEVINFO(ssc->ssc_un);
12074 	ASSERT(ucmdp != NULL);
12075 	ASSERT(devp != NULL);
12076 	ASSERT(devinfo != NULL);
12077 	sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12078 	senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12079 	cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12080 
12081 	/* In certain case (like DOORLOCK), the cdb could be NULL. */
12082 	if (cdbp == NULL)
12083 		return;
12084 	/* We don't print log if no sense data available. */
12085 	if (senlen == 0)
12086 		sensep = NULL;
12087 	com = cdbp->scc_cmd;
12088 	scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12089 	    scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12090 }
12091 
12092 /*
12093  *     Function: sd_ssc_assessment
12094  *
12095  * Description: We use this function to make an assessment at the point
12096  *              where SD driver may encounter a potential error.
12097  *
12098  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12099  *                  sd_uscsi_info in.
12100  *            tp_assess - a hint of strategy for ereport posting.
12101  *            Possible values of tp_assess include:
12102  *                SD_FMT_IGNORE - we don't post any ereport because we're
12103  *                sure that it is ok to ignore the underlying problems.
12104  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12105  *                but it might be not correct to ignore the underlying hardware
12106  *                error.
12107  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12108  *                payload driver-assessment of value "fail" or
12109  *                "fatal"(depending on what information we have here). This
12110  *                assessment value is usually set when SD driver think there
12111  *                is a potential error occurred(Typically, when return value
12112  *                of the SCSI command is EIO).
12113  *                SD_FMT_STANDARD - we will post an ereport with the payload
12114  *                driver-assessment of value "info". This assessment value is
12115  *                set when the SCSI command returned successfully and with
12116  *                sense data sent back.
12117  *
12118  *     Context: Kernel thread.
12119  */
12120 static void
12121 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12122 {
12123 	int senlen = 0;
12124 	struct uscsi_cmd *ucmdp = NULL;
12125 	struct sd_lun *un;
12126 
12127 	ASSERT(ssc != NULL);
12128 	un = ssc->ssc_un;
12129 	ASSERT(un != NULL);
12130 	ucmdp = ssc->ssc_uscsi_cmd;
12131 	ASSERT(ucmdp != NULL);
12132 
12133 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12134 		ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12135 	} else {
12136 		/*
12137 		 * If enter here, it indicates that we have a wrong
12138 		 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12139 		 * both of which should be called in a pair in case of
12140 		 * loss of FMA telemetries.
12141 		 */
12142 		if (ucmdp->uscsi_cdb != NULL) {
12143 			SD_INFO(SD_LOG_SDTEST, un,
12144 			    "sd_ssc_assessment is missing the "
12145 			    "alternative sd_ssc_send when running 0x%x, "
12146 			    "or there are superfluous sd_ssc_assessment for "
12147 			    "the same sd_ssc_send.\n",
12148 			    ucmdp->uscsi_cdb[0]);
12149 		}
12150 		/*
12151 		 * Set the ssc_flags to the initial value to avoid passing
12152 		 * down dirty flags to the following sd_ssc_send function.
12153 		 */
12154 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12155 		return;
12156 	}
12157 
12158 	/*
12159 	 * Only handle an issued command which is waiting for assessment.
12160 	 * A command which is not issued will not have
12161 	 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12162 	 */
12163 	if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12164 		sd_ssc_print(ssc, SCSI_ERR_INFO);
12165 		return;
12166 	} else {
12167 		/*
12168 		 * For an issued command, we should clear this flag in
12169 		 * order to make the sd_ssc_t structure be used off
12170 		 * multiple uscsi commands.
12171 		 */
12172 		ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12173 	}
12174 
12175 	/*
12176 	 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12177 	 * commands here. And we should clear the ssc_flags before return.
12178 	 */
12179 	if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12180 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12181 		return;
12182 	}
12183 
12184 	switch (tp_assess) {
12185 	case SD_FMT_IGNORE:
12186 	case SD_FMT_IGNORE_COMPROMISE:
12187 		break;
12188 	case SD_FMT_STATUS_CHECK:
12189 		/*
12190 		 * For a failed command(including the succeeded command
12191 		 * with invalid data sent back).
12192 		 */
12193 		sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12194 		break;
12195 	case SD_FMT_STANDARD:
12196 		/*
12197 		 * Always for the succeeded commands probably with sense
12198 		 * data sent back.
12199 		 * Limitation:
12200 		 *	We can only handle a succeeded command with sense
12201 		 *	data sent back when auto-request-sense is enabled.
12202 		 */
12203 		senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12204 		    ssc->ssc_uscsi_cmd->uscsi_rqresid;
12205 		if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12206 		    (un->un_f_arq_enabled == TRUE) &&
12207 		    senlen > 0 &&
12208 		    ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12209 			sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12210 		}
12211 		break;
12212 	default:
12213 		/*
12214 		 * Should not have other type of assessment.
12215 		 */
12216 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12217 		    "sd_ssc_assessment got wrong "
12218 		    "sd_type_assessment %d.\n", tp_assess);
12219 		break;
12220 	}
12221 	/*
12222 	 * Clear up the ssc_flags before return.
12223 	 */
12224 	ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12225 }
12226 
12227 /*
12228  *    Function: sd_ssc_post
12229  *
12230  * Description: 1. read the driver property to get fm-scsi-log flag.
12231  *              2. print log if fm_log_capable is non-zero.
12232  *              3. call sd_ssc_ereport_post to post ereport if possible.
12233  *
12234  *    Context: May be called from kernel thread or interrupt context.
12235  */
12236 static void
12237 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12238 {
12239 	struct sd_lun	*un;
12240 	int		sd_severity;
12241 
12242 	ASSERT(ssc != NULL);
12243 	un = ssc->ssc_un;
12244 	ASSERT(un != NULL);
12245 
12246 	/*
12247 	 * We may enter here from sd_ssc_assessment(for USCSI command) or
12248 	 * by directly called from sdintr context.
12249 	 * We don't handle a non-disk drive(CD-ROM, removable media).
12250 	 * Clear the ssc_flags before return in case we've set
12251 	 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12252 	 * driver.
12253 	 */
12254 	if (ISCD(un) || un->un_f_has_removable_media) {
12255 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12256 		return;
12257 	}
12258 
12259 	switch (sd_assess) {
12260 		case SD_FM_DRV_FATAL:
12261 			sd_severity = SCSI_ERR_FATAL;
12262 			break;
12263 		case SD_FM_DRV_RECOVERY:
12264 			sd_severity = SCSI_ERR_RECOVERED;
12265 			break;
12266 		case SD_FM_DRV_RETRY:
12267 			sd_severity = SCSI_ERR_RETRYABLE;
12268 			break;
12269 		case SD_FM_DRV_NOTICE:
12270 			sd_severity = SCSI_ERR_INFO;
12271 			break;
12272 		default:
12273 			sd_severity = SCSI_ERR_UNKNOWN;
12274 	}
12275 	/* print log */
12276 	sd_ssc_print(ssc, sd_severity);
12277 
12278 	/* always post ereport */
12279 	sd_ssc_ereport_post(ssc, sd_assess);
12280 }
12281 
12282 /*
12283  *    Function: sd_ssc_set_info
12284  *
12285  * Description: Mark ssc_flags and set ssc_info which would be the
12286  *              payload of uderr ereport. This function will cause
12287  *              sd_ssc_ereport_post to post uderr ereport only.
12288  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12289  *              the function will also call SD_ERROR or scsi_log for a
12290  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12291  *
12292  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12293  *                  sd_uscsi_info in.
12294  *            ssc_flags - indicate the sub-category of a uderr.
12295  *            comp - this argument is meaningful only when
12296  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12297  *                   values include:
12298  *                   > 0, SD_ERROR is used with comp as the driver logging
12299  *                   component;
12300  *                   = 0, scsi-log is used to log error telemetries;
12301  *                   < 0, no log available for this telemetry.
12302  *
12303  *    Context: Kernel thread or interrupt context
12304  */
12305 static void
12306 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12307 {
12308 	va_list	ap;
12309 
12310 	ASSERT(ssc != NULL);
12311 	ASSERT(ssc->ssc_un != NULL);
12312 
12313 	ssc->ssc_flags |= ssc_flags;
12314 	va_start(ap, fmt);
12315 	(void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12316 	va_end(ap);
12317 
12318 	/*
12319 	 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12320 	 * with invalid data sent back. For non-uscsi command, the
12321 	 * following code will be bypassed.
12322 	 */
12323 	if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12324 		if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12325 			/*
12326 			 * If the error belong to certain component and we
12327 			 * do not want it to show up on the console, we
12328 			 * will use SD_ERROR, otherwise scsi_log is
12329 			 * preferred.
12330 			 */
12331 			if (comp > 0) {
12332 				SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12333 			} else if (comp == 0) {
12334 				scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12335 				    CE_WARN, ssc->ssc_info);
12336 			}
12337 		}
12338 	}
12339 }
12340 
12341 /*
12342  *    Function: sd_buf_iodone
12343  *
12344  * Description: Frees the sd_xbuf & returns the buf to its originator.
12345  *
12346  *     Context: May be called from interrupt context.
12347  */
12348 /* ARGSUSED */
12349 static void
12350 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12351 {
12352 	struct sd_xbuf *xp;
12353 
12354 	ASSERT(un != NULL);
12355 	ASSERT(bp != NULL);
12356 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12357 
12358 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12359 
12360 	xp = SD_GET_XBUF(bp);
12361 	ASSERT(xp != NULL);
12362 
12363 	/* xbuf is gone after this */
12364 	if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12365 		mutex_enter(SD_MUTEX(un));
12366 
12367 		/*
12368 		 * Grab time when the cmd completed.
12369 		 * This is used for determining if the system has been
12370 		 * idle long enough to make it idle to the PM framework.
12371 		 * This is for lowering the overhead, and therefore improving
12372 		 * performance per I/O operation.
12373 		 */
12374 		un->un_pm_idle_time = ddi_get_time();
12375 
12376 		un->un_ncmds_in_driver--;
12377 		ASSERT(un->un_ncmds_in_driver >= 0);
12378 		SD_INFO(SD_LOG_IO, un,
12379 		    "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12380 		    un->un_ncmds_in_driver);
12381 
12382 		mutex_exit(SD_MUTEX(un));
12383 	}
12384 
12385 	biodone(bp);				/* bp is gone after this */
12386 
12387 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12388 }
12389 
12390 
12391 /*
12392  *    Function: sd_uscsi_iodone
12393  *
12394  * Description: Frees the sd_xbuf & returns the buf to its originator.
12395  *
12396  *     Context: May be called from interrupt context.
12397  */
12398 /* ARGSUSED */
12399 static void
12400 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12401 {
12402 	struct sd_xbuf *xp;
12403 
12404 	ASSERT(un != NULL);
12405 	ASSERT(bp != NULL);
12406 
12407 	xp = SD_GET_XBUF(bp);
12408 	ASSERT(xp != NULL);
12409 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12410 
12411 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12412 
12413 	bp->b_private = xp->xb_private;
12414 
12415 	mutex_enter(SD_MUTEX(un));
12416 
12417 	/*
12418 	 * Grab time when the cmd completed.
12419 	 * This is used for determining if the system has been
12420 	 * idle long enough to make it idle to the PM framework.
12421 	 * This is for lowering the overhead, and therefore improving
12422 	 * performance per I/O operation.
12423 	 */
12424 	un->un_pm_idle_time = ddi_get_time();
12425 
12426 	un->un_ncmds_in_driver--;
12427 	ASSERT(un->un_ncmds_in_driver >= 0);
12428 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12429 	    un->un_ncmds_in_driver);
12430 
12431 	mutex_exit(SD_MUTEX(un));
12432 
12433 	if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12434 	    SENSE_LENGTH) {
12435 		kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12436 		    MAX_SENSE_LENGTH);
12437 	} else {
12438 		kmem_free(xp, sizeof (struct sd_xbuf));
12439 	}
12440 
12441 	biodone(bp);
12442 
12443 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12444 }
12445 
12446 
12447 /*
12448  *    Function: sd_mapblockaddr_iostart
12449  *
12450  * Description: Verify request lies within the partition limits for
12451  *		the indicated minor device.  Issue "overrun" buf if
12452  *		request would exceed partition range.  Converts
12453  *		partition-relative block address to absolute.
12454  *
12455  *              Upon exit of this function:
12456  *              1.I/O is aligned
12457  *                 xp->xb_blkno represents the absolute sector address
12458  *              2.I/O is misaligned
12459  *                 xp->xb_blkno represents the absolute logical block address
12460  *                 based on DEV_BSIZE. The logical block address will be
12461  *                 converted to physical sector address in sd_mapblocksize_\
12462  *                 iostart.
12463  *              3.I/O is misaligned but is aligned in "overrun" buf
12464  *                 xp->xb_blkno represents the absolute logical block address
12465  *                 based on DEV_BSIZE. The logical block address will be
12466  *                 converted to physical sector address in sd_mapblocksize_\
12467  *                 iostart. But no RMW will be issued in this case.
12468  *
12469  *     Context: Can sleep
12470  *
12471  *      Issues: This follows what the old code did, in terms of accessing
12472  *		some of the partition info in the unit struct without holding
12473  *		the mutext.  This is a general issue, if the partition info
12474  *		can be altered while IO is in progress... as soon as we send
12475  *		a buf, its partitioning can be invalid before it gets to the
12476  *		device.  Probably the right fix is to move partitioning out
12477  *		of the driver entirely.
12478  */
12479 
12480 static void
12481 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12482 {
12483 	diskaddr_t	nblocks;	/* #blocks in the given partition */
12484 	daddr_t	blocknum;	/* Block number specified by the buf */
12485 	size_t	requested_nblocks;
12486 	size_t	available_nblocks;
12487 	int	partition;
12488 	diskaddr_t	partition_offset;
12489 	struct sd_xbuf *xp;
12490 	int secmask = 0, blknomask = 0;
12491 	ushort_t is_aligned = TRUE;
12492 
12493 	ASSERT(un != NULL);
12494 	ASSERT(bp != NULL);
12495 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12496 
12497 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12498 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12499 
12500 	xp = SD_GET_XBUF(bp);
12501 	ASSERT(xp != NULL);
12502 
12503 	/*
12504 	 * If the geometry is not indicated as valid, attempt to access
12505 	 * the unit & verify the geometry/label. This can be the case for
12506 	 * removable-media devices, of if the device was opened in
12507 	 * NDELAY/NONBLOCK mode.
12508 	 */
12509 	partition = SDPART(bp->b_edev);
12510 
12511 	if (!SD_IS_VALID_LABEL(un)) {
12512 		sd_ssc_t *ssc;
12513 		/*
12514 		 * Initialize sd_ssc_t for internal uscsi commands
12515 		 * In case of potential porformance issue, we need
12516 		 * to alloc memory only if there is invalid label
12517 		 */
12518 		ssc = sd_ssc_init(un);
12519 
12520 		if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12521 			/*
12522 			 * For removable devices it is possible to start an
12523 			 * I/O without a media by opening the device in nodelay
12524 			 * mode. Also for writable CDs there can be many
12525 			 * scenarios where there is no geometry yet but volume
12526 			 * manager is trying to issue a read() just because
12527 			 * it can see TOC on the CD. So do not print a message
12528 			 * for removables.
12529 			 */
12530 			if (!un->un_f_has_removable_media) {
12531 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12532 				    "i/o to invalid geometry\n");
12533 			}
12534 			bioerror(bp, EIO);
12535 			bp->b_resid = bp->b_bcount;
12536 			SD_BEGIN_IODONE(index, un, bp);
12537 
12538 			sd_ssc_fini(ssc);
12539 			return;
12540 		}
12541 		sd_ssc_fini(ssc);
12542 	}
12543 
12544 	nblocks = 0;
12545 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
12546 	    &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12547 
12548 	blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12549 	secmask = un->un_tgt_blocksize - 1;
12550 
12551 	if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12552 		is_aligned = FALSE;
12553 	}
12554 
12555 	if (!(NOT_DEVBSIZE(un))) {
12556 		/*
12557 		 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12558 		 * Convert the logical block number to target's physical sector
12559 		 * number.
12560 		 */
12561 		if (is_aligned) {
12562 			xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12563 		} else {
12564 			switch (un->un_f_rmw_type) {
12565 			case SD_RMW_TYPE_RETURN_ERROR:
12566 				bp->b_flags |= B_ERROR;
12567 				goto error_exit;
12568 
12569 			case SD_RMW_TYPE_DEFAULT:
12570 				mutex_enter(SD_MUTEX(un));
12571 				if (un->un_rmw_msg_timeid == NULL) {
12572 					scsi_log(SD_DEVINFO(un), sd_label,
12573 					    CE_WARN, "I/O request is not "
12574 					    "aligned with %d disk sector size. "
12575 					    "It is handled through Read Modify "
12576 					    "Write but the performance is "
12577 					    "very low.\n",
12578 					    un->un_tgt_blocksize);
12579 					un->un_rmw_msg_timeid =
12580 					    timeout(sd_rmw_msg_print_handler,
12581 					    un, SD_RMW_MSG_PRINT_TIMEOUT);
12582 				} else {
12583 					un->un_rmw_incre_count ++;
12584 				}
12585 				mutex_exit(SD_MUTEX(un));
12586 				break;
12587 
12588 			case SD_RMW_TYPE_NO_WARNING:
12589 			default:
12590 				break;
12591 			}
12592 
12593 			nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12594 			partition_offset = SD_TGT2SYSBLOCK(un,
12595 			    partition_offset);
12596 		}
12597 	}
12598 
12599 	/*
12600 	 * blocknum is the starting block number of the request. At this
12601 	 * point it is still relative to the start of the minor device.
12602 	 */
12603 	blocknum = xp->xb_blkno;
12604 
12605 	/*
12606 	 * Legacy: If the starting block number is one past the last block
12607 	 * in the partition, do not set B_ERROR in the buf.
12608 	 */
12609 	if (blocknum == nblocks)  {
12610 		goto error_exit;
12611 	}
12612 
12613 	/*
12614 	 * Confirm that the first block of the request lies within the
12615 	 * partition limits. Also the requested number of bytes must be
12616 	 * a multiple of the system block size.
12617 	 */
12618 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12619 	    ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12620 		bp->b_flags |= B_ERROR;
12621 		goto error_exit;
12622 	}
12623 
12624 	/*
12625 	 * If the requsted # blocks exceeds the available # blocks, that
12626 	 * is an overrun of the partition.
12627 	 */
12628 	if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12629 		requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12630 	} else {
12631 		requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12632 	}
12633 
12634 	available_nblocks = (size_t)(nblocks - blocknum);
12635 	ASSERT(nblocks >= blocknum);
12636 
12637 	if (requested_nblocks > available_nblocks) {
12638 		size_t resid;
12639 
12640 		/*
12641 		 * Allocate an "overrun" buf to allow the request to proceed
12642 		 * for the amount of space available in the partition. The
12643 		 * amount not transferred will be added into the b_resid
12644 		 * when the operation is complete. The overrun buf
12645 		 * replaces the original buf here, and the original buf
12646 		 * is saved inside the overrun buf, for later use.
12647 		 */
12648 		if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12649 			resid = SD_TGTBLOCKS2BYTES(un,
12650 			    (offset_t)(requested_nblocks - available_nblocks));
12651 		} else {
12652 			resid = SD_SYSBLOCKS2BYTES(
12653 			    (offset_t)(requested_nblocks - available_nblocks));
12654 		}
12655 
12656 		size_t count = bp->b_bcount - resid;
12657 		/*
12658 		 * Note: count is an unsigned entity thus it'll NEVER
12659 		 * be less than 0 so ASSERT the original values are
12660 		 * correct.
12661 		 */
12662 		ASSERT(bp->b_bcount >= resid);
12663 
12664 		bp = sd_bioclone_alloc(bp, count, blocknum,
12665 		    (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12666 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12667 		ASSERT(xp != NULL);
12668 	}
12669 
12670 	/* At this point there should be no residual for this buf. */
12671 	ASSERT(bp->b_resid == 0);
12672 
12673 	/* Convert the block number to an absolute address. */
12674 	xp->xb_blkno += partition_offset;
12675 
12676 	SD_NEXT_IOSTART(index, un, bp);
12677 
12678 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12679 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12680 
12681 	return;
12682 
12683 error_exit:
12684 	bp->b_resid = bp->b_bcount;
12685 	SD_BEGIN_IODONE(index, un, bp);
12686 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12687 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12688 }
12689 
12690 
12691 /*
12692  *    Function: sd_mapblockaddr_iodone
12693  *
12694  * Description: Completion-side processing for partition management.
12695  *
12696  *     Context: May be called under interrupt context
12697  */
12698 
12699 static void
12700 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12701 {
12702 	/* int	partition; */	/* Not used, see below. */
12703 	ASSERT(un != NULL);
12704 	ASSERT(bp != NULL);
12705 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12706 
12707 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12708 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12709 
12710 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12711 		/*
12712 		 * We have an "overrun" buf to deal with...
12713 		 */
12714 		struct sd_xbuf	*xp;
12715 		struct buf	*obp;	/* ptr to the original buf */
12716 
12717 		xp = SD_GET_XBUF(bp);
12718 		ASSERT(xp != NULL);
12719 
12720 		/* Retrieve the pointer to the original buf */
12721 		obp = (struct buf *)xp->xb_private;
12722 		ASSERT(obp != NULL);
12723 
12724 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12725 		bioerror(obp, bp->b_error);
12726 
12727 		sd_bioclone_free(bp);
12728 
12729 		/*
12730 		 * Get back the original buf.
12731 		 * Note that since the restoration of xb_blkno below
12732 		 * was removed, the sd_xbuf is not needed.
12733 		 */
12734 		bp = obp;
12735 		/*
12736 		 * xp = SD_GET_XBUF(bp);
12737 		 * ASSERT(xp != NULL);
12738 		 */
12739 	}
12740 
12741 	/*
12742 	 * Convert sd->xb_blkno back to a minor-device relative value.
12743 	 * Note: this has been commented out, as it is not needed in the
12744 	 * current implementation of the driver (ie, since this function
12745 	 * is at the top of the layering chains, so the info will be
12746 	 * discarded) and it is in the "hot" IO path.
12747 	 *
12748 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12749 	 * xp->xb_blkno -= un->un_offset[partition];
12750 	 */
12751 
12752 	SD_NEXT_IODONE(index, un, bp);
12753 
12754 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12755 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12756 }
12757 
12758 
12759 /*
12760  *    Function: sd_mapblocksize_iostart
12761  *
12762  * Description: Convert between system block size (un->un_sys_blocksize)
12763  *		and target block size (un->un_tgt_blocksize).
12764  *
12765  *     Context: Can sleep to allocate resources.
12766  *
12767  * Assumptions: A higher layer has already performed any partition validation,
12768  *		and converted the xp->xb_blkno to an absolute value relative
12769  *		to the start of the device.
12770  *
12771  *		It is also assumed that the higher layer has implemented
12772  *		an "overrun" mechanism for the case where the request would
12773  *		read/write beyond the end of a partition.  In this case we
12774  *		assume (and ASSERT) that bp->b_resid == 0.
12775  *
12776  *		Note: The implementation for this routine assumes the target
12777  *		block size remains constant between allocation and transport.
12778  */
12779 
12780 static void
12781 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12782 {
12783 	struct sd_mapblocksize_info	*bsp;
12784 	struct sd_xbuf			*xp;
12785 	offset_t first_byte;
12786 	daddr_t	start_block, end_block;
12787 	daddr_t	request_bytes;
12788 	ushort_t is_aligned = FALSE;
12789 
12790 	ASSERT(un != NULL);
12791 	ASSERT(bp != NULL);
12792 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12793 	ASSERT(bp->b_resid == 0);
12794 
12795 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12796 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12797 
12798 	/*
12799 	 * For a non-writable CD, a write request is an error
12800 	 */
12801 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12802 	    (un->un_f_mmc_writable_media == FALSE)) {
12803 		bioerror(bp, EIO);
12804 		bp->b_resid = bp->b_bcount;
12805 		SD_BEGIN_IODONE(index, un, bp);
12806 		return;
12807 	}
12808 
12809 	/*
12810 	 * We do not need a shadow buf if the device is using
12811 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12812 	 * In this case there is no layer-private data block allocated.
12813 	 */
12814 	if ((un->un_tgt_blocksize == DEV_BSIZE) ||
12815 	    (bp->b_bcount == 0)) {
12816 		goto done;
12817 	}
12818 
12819 #if defined(__i386) || defined(__amd64)
12820 	/* We do not support non-block-aligned transfers for ROD devices */
12821 	ASSERT(!ISROD(un));
12822 #endif
12823 
12824 	xp = SD_GET_XBUF(bp);
12825 	ASSERT(xp != NULL);
12826 
12827 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12828 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12829 	    un->un_tgt_blocksize, DEV_BSIZE);
12830 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12831 	    "request start block:0x%x\n", xp->xb_blkno);
12832 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12833 	    "request len:0x%x\n", bp->b_bcount);
12834 
12835 	/*
12836 	 * Allocate the layer-private data area for the mapblocksize layer.
12837 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12838 	 * struct to store the pointer to their layer-private data block, but
12839 	 * each layer also has the responsibility of restoring the prior
12840 	 * contents of xb_private before returning the buf/xbuf to the
12841 	 * higher layer that sent it.
12842 	 *
12843 	 * Here we save the prior contents of xp->xb_private into the
12844 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12845 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12846 	 * the layer-private area and returning the buf/xbuf to the layer
12847 	 * that sent it.
12848 	 *
12849 	 * Note that here we use kmem_zalloc for the allocation as there are
12850 	 * parts of the mapblocksize code that expect certain fields to be
12851 	 * zero unless explicitly set to a required value.
12852 	 */
12853 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12854 	bsp->mbs_oprivate = xp->xb_private;
12855 	xp->xb_private = bsp;
12856 
12857 	/*
12858 	 * This treats the data on the disk (target) as an array of bytes.
12859 	 * first_byte is the byte offset, from the beginning of the device,
12860 	 * to the location of the request. This is converted from a
12861 	 * un->un_sys_blocksize block address to a byte offset, and then back
12862 	 * to a block address based upon a un->un_tgt_blocksize block size.
12863 	 *
12864 	 * xp->xb_blkno should be absolute upon entry into this function,
12865 	 * but, but it is based upon partitions that use the "system"
12866 	 * block size. It must be adjusted to reflect the block size of
12867 	 * the target.
12868 	 *
12869 	 * Note that end_block is actually the block that follows the last
12870 	 * block of the request, but that's what is needed for the computation.
12871 	 */
12872 	first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12873 	start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12874 	end_block   = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) /
12875 	    un->un_tgt_blocksize;
12876 
12877 	/* request_bytes is rounded up to a multiple of the target block size */
12878 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12879 
12880 	/*
12881 	 * See if the starting address of the request and the request
12882 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12883 	 * then we do not need to allocate a shadow buf to handle the request.
12884 	 */
12885 	if (((first_byte   % un->un_tgt_blocksize) == 0) &&
12886 	    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12887 		is_aligned = TRUE;
12888 	}
12889 
12890 	if ((bp->b_flags & B_READ) == 0) {
12891 		/*
12892 		 * Lock the range for a write operation. An aligned request is
12893 		 * considered a simple write; otherwise the request must be a
12894 		 * read-modify-write.
12895 		 */
12896 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12897 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12898 	}
12899 
12900 	/*
12901 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12902 	 * where the READ command is generated for a read-modify-write. (The
12903 	 * write phase is deferred until after the read completes.)
12904 	 */
12905 	if (is_aligned == FALSE) {
12906 
12907 		struct sd_mapblocksize_info	*shadow_bsp;
12908 		struct sd_xbuf	*shadow_xp;
12909 		struct buf	*shadow_bp;
12910 
12911 		/*
12912 		 * Allocate the shadow buf and it associated xbuf. Note that
12913 		 * after this call the xb_blkno value in both the original
12914 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12915 		 * same: absolute relative to the start of the device, and
12916 		 * adjusted for the target block size. The b_blkno in the
12917 		 * shadow buf will also be set to this value. We should never
12918 		 * change b_blkno in the original bp however.
12919 		 *
12920 		 * Note also that the shadow buf will always need to be a
12921 		 * READ command, regardless of whether the incoming command
12922 		 * is a READ or a WRITE.
12923 		 */
12924 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12925 		    xp->xb_blkno,
12926 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
12927 
12928 		shadow_xp = SD_GET_XBUF(shadow_bp);
12929 
12930 		/*
12931 		 * Allocate the layer-private data for the shadow buf.
12932 		 * (No need to preserve xb_private in the shadow xbuf.)
12933 		 */
12934 		shadow_xp->xb_private = shadow_bsp =
12935 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12936 
12937 		/*
12938 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12939 		 * to figure out where the start of the user data is (based upon
12940 		 * the system block size) in the data returned by the READ
12941 		 * command (which will be based upon the target blocksize). Note
12942 		 * that this is only really used if the request is unaligned.
12943 		 */
12944 		bsp->mbs_copy_offset = (ssize_t)(first_byte -
12945 		    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12946 		ASSERT((bsp->mbs_copy_offset >= 0) &&
12947 		    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12948 
12949 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12950 
12951 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12952 
12953 		/* Transfer the wmap (if any) to the shadow buf */
12954 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12955 		bsp->mbs_wmp = NULL;
12956 
12957 		/*
12958 		 * The shadow buf goes on from here in place of the
12959 		 * original buf.
12960 		 */
12961 		shadow_bsp->mbs_orig_bp = bp;
12962 		bp = shadow_bp;
12963 	}
12964 
12965 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12966 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12967 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12968 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12969 	    request_bytes);
12970 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12971 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12972 
12973 done:
12974 	SD_NEXT_IOSTART(index, un, bp);
12975 
12976 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12977 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12978 }
12979 
12980 
12981 /*
12982  *    Function: sd_mapblocksize_iodone
12983  *
12984  * Description: Completion side processing for block-size mapping.
12985  *
12986  *     Context: May be called under interrupt context
12987  */
12988 
12989 static void
12990 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12991 {
12992 	struct sd_mapblocksize_info	*bsp;
12993 	struct sd_xbuf	*xp;
12994 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
12995 	struct buf	*orig_bp;	/* ptr to the original buf */
12996 	offset_t	shadow_end;
12997 	offset_t	request_end;
12998 	offset_t	shadow_start;
12999 	ssize_t		copy_offset;
13000 	size_t		copy_length;
13001 	size_t		shortfall;
13002 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
13003 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
13004 
13005 	ASSERT(un != NULL);
13006 	ASSERT(bp != NULL);
13007 
13008 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13009 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13010 
13011 	/*
13012 	 * There is no shadow buf or layer-private data if the target is
13013 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
13014 	 */
13015 	if ((un->un_tgt_blocksize == DEV_BSIZE) ||
13016 	    (bp->b_bcount == 0)) {
13017 		goto exit;
13018 	}
13019 
13020 	xp = SD_GET_XBUF(bp);
13021 	ASSERT(xp != NULL);
13022 
13023 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
13024 	bsp = xp->xb_private;
13025 
13026 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13027 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13028 
13029 	if (is_write) {
13030 		/*
13031 		 * For a WRITE request we must free up the block range that
13032 		 * we have locked up.  This holds regardless of whether this is
13033 		 * an aligned write request or a read-modify-write request.
13034 		 */
13035 		sd_range_unlock(un, bsp->mbs_wmp);
13036 		bsp->mbs_wmp = NULL;
13037 	}
13038 
13039 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13040 		/*
13041 		 * An aligned read or write command will have no shadow buf;
13042 		 * there is not much else to do with it.
13043 		 */
13044 		goto done;
13045 	}
13046 
13047 	orig_bp = bsp->mbs_orig_bp;
13048 	ASSERT(orig_bp != NULL);
13049 	orig_xp = SD_GET_XBUF(orig_bp);
13050 	ASSERT(orig_xp != NULL);
13051 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13052 
13053 	if (!is_write && has_wmap) {
13054 		/*
13055 		 * A READ with a wmap means this is the READ phase of a
13056 		 * read-modify-write. If an error occurred on the READ then
13057 		 * we do not proceed with the WRITE phase or copy any data.
13058 		 * Just release the write maps and return with an error.
13059 		 */
13060 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13061 			orig_bp->b_resid = orig_bp->b_bcount;
13062 			bioerror(orig_bp, bp->b_error);
13063 			sd_range_unlock(un, bsp->mbs_wmp);
13064 			goto freebuf_done;
13065 		}
13066 	}
13067 
13068 	/*
13069 	 * Here is where we set up to copy the data from the shadow buf
13070 	 * into the space associated with the original buf.
13071 	 *
13072 	 * To deal with the conversion between block sizes, these
13073 	 * computations treat the data as an array of bytes, with the
13074 	 * first byte (byte 0) corresponding to the first byte in the
13075 	 * first block on the disk.
13076 	 */
13077 
13078 	/*
13079 	 * shadow_start and shadow_len indicate the location and size of
13080 	 * the data returned with the shadow IO request.
13081 	 */
13082 	shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13083 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13084 
13085 	/*
13086 	 * copy_offset gives the offset (in bytes) from the start of the first
13087 	 * block of the READ request to the beginning of the data.  We retrieve
13088 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13089 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13090 	 * data to be copied (in bytes).
13091 	 */
13092 	copy_offset  = bsp->mbs_copy_offset;
13093 	ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize));
13094 	copy_length  = orig_bp->b_bcount;
13095 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13096 
13097 	/*
13098 	 * Set up the resid and error fields of orig_bp as appropriate.
13099 	 */
13100 	if (shadow_end >= request_end) {
13101 		/* We got all the requested data; set resid to zero */
13102 		orig_bp->b_resid = 0;
13103 	} else {
13104 		/*
13105 		 * We failed to get enough data to fully satisfy the original
13106 		 * request. Just copy back whatever data we got and set
13107 		 * up the residual and error code as required.
13108 		 *
13109 		 * 'shortfall' is the amount by which the data received with the
13110 		 * shadow buf has "fallen short" of the requested amount.
13111 		 */
13112 		shortfall = (size_t)(request_end - shadow_end);
13113 
13114 		if (shortfall > orig_bp->b_bcount) {
13115 			/*
13116 			 * We did not get enough data to even partially
13117 			 * fulfill the original request.  The residual is
13118 			 * equal to the amount requested.
13119 			 */
13120 			orig_bp->b_resid = orig_bp->b_bcount;
13121 		} else {
13122 			/*
13123 			 * We did not get all the data that we requested
13124 			 * from the device, but we will try to return what
13125 			 * portion we did get.
13126 			 */
13127 			orig_bp->b_resid = shortfall;
13128 		}
13129 		ASSERT(copy_length >= orig_bp->b_resid);
13130 		copy_length  -= orig_bp->b_resid;
13131 	}
13132 
13133 	/* Propagate the error code from the shadow buf to the original buf */
13134 	bioerror(orig_bp, bp->b_error);
13135 
13136 	if (is_write) {
13137 		goto freebuf_done;	/* No data copying for a WRITE */
13138 	}
13139 
13140 	if (has_wmap) {
13141 		/*
13142 		 * This is a READ command from the READ phase of a
13143 		 * read-modify-write request. We have to copy the data given
13144 		 * by the user OVER the data returned by the READ command,
13145 		 * then convert the command from a READ to a WRITE and send
13146 		 * it back to the target.
13147 		 */
13148 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13149 		    copy_length);
13150 
13151 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
13152 
13153 		/*
13154 		 * Dispatch the WRITE command to the taskq thread, which
13155 		 * will in turn send the command to the target. When the
13156 		 * WRITE command completes, we (sd_mapblocksize_iodone())
13157 		 * will get called again as part of the iodone chain
13158 		 * processing for it. Note that we will still be dealing
13159 		 * with the shadow buf at that point.
13160 		 */
13161 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13162 		    KM_NOSLEEP) != 0) {
13163 			/*
13164 			 * Dispatch was successful so we are done. Return
13165 			 * without going any higher up the iodone chain. Do
13166 			 * not free up any layer-private data until after the
13167 			 * WRITE completes.
13168 			 */
13169 			return;
13170 		}
13171 
13172 		/*
13173 		 * Dispatch of the WRITE command failed; set up the error
13174 		 * condition and send this IO back up the iodone chain.
13175 		 */
13176 		bioerror(orig_bp, EIO);
13177 		orig_bp->b_resid = orig_bp->b_bcount;
13178 
13179 	} else {
13180 		/*
13181 		 * This is a regular READ request (ie, not a RMW). Copy the
13182 		 * data from the shadow buf into the original buf. The
13183 		 * copy_offset compensates for any "misalignment" between the
13184 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13185 		 * original buf (with its un->un_sys_blocksize blocks).
13186 		 */
13187 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13188 		    copy_length);
13189 	}
13190 
13191 freebuf_done:
13192 
13193 	/*
13194 	 * At this point we still have both the shadow buf AND the original
13195 	 * buf to deal with, as well as the layer-private data area in each.
13196 	 * Local variables are as follows:
13197 	 *
13198 	 * bp -- points to shadow buf
13199 	 * xp -- points to xbuf of shadow buf
13200 	 * bsp -- points to layer-private data area of shadow buf
13201 	 * orig_bp -- points to original buf
13202 	 *
13203 	 * First free the shadow buf and its associated xbuf, then free the
13204 	 * layer-private data area from the shadow buf. There is no need to
13205 	 * restore xb_private in the shadow xbuf.
13206 	 */
13207 	sd_shadow_buf_free(bp);
13208 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13209 
13210 	/*
13211 	 * Now update the local variables to point to the original buf, xbuf,
13212 	 * and layer-private area.
13213 	 */
13214 	bp = orig_bp;
13215 	xp = SD_GET_XBUF(bp);
13216 	ASSERT(xp != NULL);
13217 	ASSERT(xp == orig_xp);
13218 	bsp = xp->xb_private;
13219 	ASSERT(bsp != NULL);
13220 
13221 done:
13222 	/*
13223 	 * Restore xb_private to whatever it was set to by the next higher
13224 	 * layer in the chain, then free the layer-private data area.
13225 	 */
13226 	xp->xb_private = bsp->mbs_oprivate;
13227 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13228 
13229 exit:
13230 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13231 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13232 
13233 	SD_NEXT_IODONE(index, un, bp);
13234 }
13235 
13236 
13237 /*
13238  *    Function: sd_checksum_iostart
13239  *
13240  * Description: A stub function for a layer that's currently not used.
13241  *		For now just a placeholder.
13242  *
13243  *     Context: Kernel thread context
13244  */
13245 
13246 static void
13247 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13248 {
13249 	ASSERT(un != NULL);
13250 	ASSERT(bp != NULL);
13251 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13252 	SD_NEXT_IOSTART(index, un, bp);
13253 }
13254 
13255 
13256 /*
13257  *    Function: sd_checksum_iodone
13258  *
13259  * Description: A stub function for a layer that's currently not used.
13260  *		For now just a placeholder.
13261  *
13262  *     Context: May be called under interrupt context
13263  */
13264 
13265 static void
13266 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13267 {
13268 	ASSERT(un != NULL);
13269 	ASSERT(bp != NULL);
13270 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13271 	SD_NEXT_IODONE(index, un, bp);
13272 }
13273 
13274 
13275 /*
13276  *    Function: sd_checksum_uscsi_iostart
13277  *
13278  * Description: A stub function for a layer that's currently not used.
13279  *		For now just a placeholder.
13280  *
13281  *     Context: Kernel thread context
13282  */
13283 
13284 static void
13285 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13286 {
13287 	ASSERT(un != NULL);
13288 	ASSERT(bp != NULL);
13289 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13290 	SD_NEXT_IOSTART(index, un, bp);
13291 }
13292 
13293 
13294 /*
13295  *    Function: sd_checksum_uscsi_iodone
13296  *
13297  * Description: A stub function for a layer that's currently not used.
13298  *		For now just a placeholder.
13299  *
13300  *     Context: May be called under interrupt context
13301  */
13302 
13303 static void
13304 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13305 {
13306 	ASSERT(un != NULL);
13307 	ASSERT(bp != NULL);
13308 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13309 	SD_NEXT_IODONE(index, un, bp);
13310 }
13311 
13312 
13313 /*
13314  *    Function: sd_pm_iostart
13315  *
13316  * Description: iostart-side routine for Power mangement.
13317  *
13318  *     Context: Kernel thread context
13319  */
13320 
13321 static void
13322 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13323 {
13324 	ASSERT(un != NULL);
13325 	ASSERT(bp != NULL);
13326 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13327 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13328 
13329 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13330 
13331 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13332 		/*
13333 		 * Set up to return the failed buf back up the 'iodone'
13334 		 * side of the calling chain.
13335 		 */
13336 		bioerror(bp, EIO);
13337 		bp->b_resid = bp->b_bcount;
13338 
13339 		SD_BEGIN_IODONE(index, un, bp);
13340 
13341 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13342 		return;
13343 	}
13344 
13345 	SD_NEXT_IOSTART(index, un, bp);
13346 
13347 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13348 }
13349 
13350 
13351 /*
13352  *    Function: sd_pm_iodone
13353  *
13354  * Description: iodone-side routine for power mangement.
13355  *
13356  *     Context: may be called from interrupt context
13357  */
13358 
13359 static void
13360 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13361 {
13362 	ASSERT(un != NULL);
13363 	ASSERT(bp != NULL);
13364 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13365 
13366 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13367 
13368 	/*
13369 	 * After attach the following flag is only read, so don't
13370 	 * take the penalty of acquiring a mutex for it.
13371 	 */
13372 	if (un->un_f_pm_is_enabled == TRUE) {
13373 		sd_pm_exit(un);
13374 	}
13375 
13376 	SD_NEXT_IODONE(index, un, bp);
13377 
13378 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13379 }
13380 
13381 
13382 /*
13383  *    Function: sd_core_iostart
13384  *
13385  * Description: Primary driver function for enqueuing buf(9S) structs from
13386  *		the system and initiating IO to the target device
13387  *
13388  *     Context: Kernel thread context. Can sleep.
13389  *
13390  * Assumptions:  - The given xp->xb_blkno is absolute
13391  *		   (ie, relative to the start of the device).
13392  *		 - The IO is to be done using the native blocksize of
13393  *		   the device, as specified in un->un_tgt_blocksize.
13394  */
13395 /* ARGSUSED */
13396 static void
13397 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13398 {
13399 	struct sd_xbuf *xp;
13400 
13401 	ASSERT(un != NULL);
13402 	ASSERT(bp != NULL);
13403 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13404 	ASSERT(bp->b_resid == 0);
13405 
13406 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13407 
13408 	xp = SD_GET_XBUF(bp);
13409 	ASSERT(xp != NULL);
13410 
13411 	mutex_enter(SD_MUTEX(un));
13412 
13413 	/*
13414 	 * If we are currently in the failfast state, fail any new IO
13415 	 * that has B_FAILFAST set, then return.
13416 	 */
13417 	if ((bp->b_flags & B_FAILFAST) &&
13418 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13419 		mutex_exit(SD_MUTEX(un));
13420 		bioerror(bp, EIO);
13421 		bp->b_resid = bp->b_bcount;
13422 		SD_BEGIN_IODONE(index, un, bp);
13423 		return;
13424 	}
13425 
13426 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13427 		/*
13428 		 * Priority command -- transport it immediately.
13429 		 *
13430 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13431 		 * because all direct priority commands should be associated
13432 		 * with error recovery actions which we don't want to retry.
13433 		 */
13434 		sd_start_cmds(un, bp);
13435 	} else {
13436 		/*
13437 		 * Normal command -- add it to the wait queue, then start
13438 		 * transporting commands from the wait queue.
13439 		 */
13440 		sd_add_buf_to_waitq(un, bp);
13441 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13442 		sd_start_cmds(un, NULL);
13443 	}
13444 
13445 	mutex_exit(SD_MUTEX(un));
13446 
13447 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13448 }
13449 
13450 
13451 /*
13452  *    Function: sd_init_cdb_limits
13453  *
13454  * Description: This is to handle scsi_pkt initialization differences
13455  *		between the driver platforms.
13456  *
13457  *		Legacy behaviors:
13458  *
13459  *		If the block number or the sector count exceeds the
13460  *		capabilities of a Group 0 command, shift over to a
13461  *		Group 1 command. We don't blindly use Group 1
13462  *		commands because a) some drives (CDC Wren IVs) get a
13463  *		bit confused, and b) there is probably a fair amount
13464  *		of speed difference for a target to receive and decode
13465  *		a 10 byte command instead of a 6 byte command.
13466  *
13467  *		The xfer time difference of 6 vs 10 byte CDBs is
13468  *		still significant so this code is still worthwhile.
13469  *		10 byte CDBs are very inefficient with the fas HBA driver
13470  *		and older disks. Each CDB byte took 1 usec with some
13471  *		popular disks.
13472  *
13473  *     Context: Must be called at attach time
13474  */
13475 
13476 static void
13477 sd_init_cdb_limits(struct sd_lun *un)
13478 {
13479 	int hba_cdb_limit;
13480 
13481 	/*
13482 	 * Use CDB_GROUP1 commands for most devices except for
13483 	 * parallel SCSI fixed drives in which case we get better
13484 	 * performance using CDB_GROUP0 commands (where applicable).
13485 	 */
13486 	un->un_mincdb = SD_CDB_GROUP1;
13487 #if !defined(__fibre)
13488 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13489 	    !un->un_f_has_removable_media) {
13490 		un->un_mincdb = SD_CDB_GROUP0;
13491 	}
13492 #endif
13493 
13494 	/*
13495 	 * Try to read the max-cdb-length supported by HBA.
13496 	 */
13497 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13498 	if (0 >= un->un_max_hba_cdb) {
13499 		un->un_max_hba_cdb = CDB_GROUP4;
13500 		hba_cdb_limit = SD_CDB_GROUP4;
13501 	} else if (0 < un->un_max_hba_cdb &&
13502 	    un->un_max_hba_cdb < CDB_GROUP1) {
13503 		hba_cdb_limit = SD_CDB_GROUP0;
13504 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13505 	    un->un_max_hba_cdb < CDB_GROUP5) {
13506 		hba_cdb_limit = SD_CDB_GROUP1;
13507 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13508 	    un->un_max_hba_cdb < CDB_GROUP4) {
13509 		hba_cdb_limit = SD_CDB_GROUP5;
13510 	} else {
13511 		hba_cdb_limit = SD_CDB_GROUP4;
13512 	}
13513 
13514 	/*
13515 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13516 	 * commands for fixed disks unless we are building for a 32 bit
13517 	 * kernel.
13518 	 */
13519 #ifdef _LP64
13520 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13521 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13522 #else
13523 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13524 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13525 #endif
13526 
13527 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13528 	    ? sizeof (struct scsi_arq_status) : 1);
13529 	un->un_cmd_timeout = (ushort_t)sd_io_time;
13530 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13531 }
13532 
13533 
13534 /*
13535  *    Function: sd_initpkt_for_buf
13536  *
13537  * Description: Allocate and initialize for transport a scsi_pkt struct,
13538  *		based upon the info specified in the given buf struct.
13539  *
13540  *		Assumes the xb_blkno in the request is absolute (ie,
13541  *		relative to the start of the device (NOT partition!).
13542  *		Also assumes that the request is using the native block
13543  *		size of the device (as returned by the READ CAPACITY
13544  *		command).
13545  *
13546  * Return Code: SD_PKT_ALLOC_SUCCESS
13547  *		SD_PKT_ALLOC_FAILURE
13548  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13549  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13550  *
13551  *     Context: Kernel thread and may be called from software interrupt context
13552  *		as part of a sdrunout callback. This function may not block or
13553  *		call routines that block
13554  */
13555 
13556 static int
13557 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13558 {
13559 	struct sd_xbuf	*xp;
13560 	struct scsi_pkt *pktp = NULL;
13561 	struct sd_lun	*un;
13562 	size_t		blockcount;
13563 	daddr_t		startblock;
13564 	int		rval;
13565 	int		cmd_flags;
13566 
13567 	ASSERT(bp != NULL);
13568 	ASSERT(pktpp != NULL);
13569 	xp = SD_GET_XBUF(bp);
13570 	ASSERT(xp != NULL);
13571 	un = SD_GET_UN(bp);
13572 	ASSERT(un != NULL);
13573 	ASSERT(mutex_owned(SD_MUTEX(un)));
13574 	ASSERT(bp->b_resid == 0);
13575 
13576 	SD_TRACE(SD_LOG_IO_CORE, un,
13577 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13578 
13579 	mutex_exit(SD_MUTEX(un));
13580 
13581 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13582 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13583 		/*
13584 		 * Already have a scsi_pkt -- just need DMA resources.
13585 		 * We must recompute the CDB in case the mapping returns
13586 		 * a nonzero pkt_resid.
13587 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13588 		 * that is being retried, the unmap/remap of the DMA resouces
13589 		 * will result in the entire transfer starting over again
13590 		 * from the very first block.
13591 		 */
13592 		ASSERT(xp->xb_pktp != NULL);
13593 		pktp = xp->xb_pktp;
13594 	} else {
13595 		pktp = NULL;
13596 	}
13597 #endif /* __i386 || __amd64 */
13598 
13599 	startblock = xp->xb_blkno;	/* Absolute block num. */
13600 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13601 
13602 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13603 
13604 	/*
13605 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13606 	 * call scsi_init_pkt, and build the CDB.
13607 	 */
13608 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13609 	    cmd_flags, sdrunout, (caddr_t)un,
13610 	    startblock, blockcount);
13611 
13612 	if (rval == 0) {
13613 		/*
13614 		 * Success.
13615 		 *
13616 		 * If partial DMA is being used and required for this transfer.
13617 		 * set it up here.
13618 		 */
13619 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13620 		    (pktp->pkt_resid != 0)) {
13621 
13622 			/*
13623 			 * Save the CDB length and pkt_resid for the
13624 			 * next xfer
13625 			 */
13626 			xp->xb_dma_resid = pktp->pkt_resid;
13627 
13628 			/* rezero resid */
13629 			pktp->pkt_resid = 0;
13630 
13631 		} else {
13632 			xp->xb_dma_resid = 0;
13633 		}
13634 
13635 		pktp->pkt_flags = un->un_tagflags;
13636 		pktp->pkt_time  = un->un_cmd_timeout;
13637 		pktp->pkt_comp  = sdintr;
13638 
13639 		pktp->pkt_private = bp;
13640 		*pktpp = pktp;
13641 
13642 		SD_TRACE(SD_LOG_IO_CORE, un,
13643 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13644 
13645 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13646 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13647 #endif
13648 
13649 		mutex_enter(SD_MUTEX(un));
13650 		return (SD_PKT_ALLOC_SUCCESS);
13651 
13652 	}
13653 
13654 	/*
13655 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13656 	 * from sd_setup_rw_pkt.
13657 	 */
13658 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13659 
13660 	if (rval == SD_PKT_ALLOC_FAILURE) {
13661 		*pktpp = NULL;
13662 		/*
13663 		 * Set the driver state to RWAIT to indicate the driver
13664 		 * is waiting on resource allocations. The driver will not
13665 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13666 		 */
13667 		mutex_enter(SD_MUTEX(un));
13668 		New_state(un, SD_STATE_RWAIT);
13669 
13670 		SD_ERROR(SD_LOG_IO_CORE, un,
13671 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13672 
13673 		if ((bp->b_flags & B_ERROR) != 0) {
13674 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13675 		}
13676 		return (SD_PKT_ALLOC_FAILURE);
13677 	} else {
13678 		/*
13679 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13680 		 *
13681 		 * This should never happen.  Maybe someone messed with the
13682 		 * kernel's minphys?
13683 		 */
13684 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13685 		    "Request rejected: too large for CDB: "
13686 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13687 		SD_ERROR(SD_LOG_IO_CORE, un,
13688 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13689 		mutex_enter(SD_MUTEX(un));
13690 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13691 
13692 	}
13693 }
13694 
13695 
13696 /*
13697  *    Function: sd_destroypkt_for_buf
13698  *
13699  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13700  *
13701  *     Context: Kernel thread or interrupt context
13702  */
13703 
13704 static void
13705 sd_destroypkt_for_buf(struct buf *bp)
13706 {
13707 	ASSERT(bp != NULL);
13708 	ASSERT(SD_GET_UN(bp) != NULL);
13709 
13710 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13711 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13712 
13713 	ASSERT(SD_GET_PKTP(bp) != NULL);
13714 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13715 
13716 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13717 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13718 }
13719 
13720 /*
13721  *    Function: sd_setup_rw_pkt
13722  *
13723  * Description: Determines appropriate CDB group for the requested LBA
13724  *		and transfer length, calls scsi_init_pkt, and builds
13725  *		the CDB.  Do not use for partial DMA transfers except
13726  *		for the initial transfer since the CDB size must
13727  *		remain constant.
13728  *
13729  *     Context: Kernel thread and may be called from software interrupt
13730  *		context as part of a sdrunout callback. This function may not
13731  *		block or call routines that block
13732  */
13733 
13734 
13735 int
13736 sd_setup_rw_pkt(struct sd_lun *un,
13737     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13738     int (*callback)(caddr_t), caddr_t callback_arg,
13739     diskaddr_t lba, uint32_t blockcount)
13740 {
13741 	struct scsi_pkt *return_pktp;
13742 	union scsi_cdb *cdbp;
13743 	struct sd_cdbinfo *cp = NULL;
13744 	int i;
13745 
13746 	/*
13747 	 * See which size CDB to use, based upon the request.
13748 	 */
13749 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13750 
13751 		/*
13752 		 * Check lba and block count against sd_cdbtab limits.
13753 		 * In the partial DMA case, we have to use the same size
13754 		 * CDB for all the transfers.  Check lba + blockcount
13755 		 * against the max LBA so we know that segment of the
13756 		 * transfer can use the CDB we select.
13757 		 */
13758 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13759 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13760 
13761 			/*
13762 			 * The command will fit into the CDB type
13763 			 * specified by sd_cdbtab[i].
13764 			 */
13765 			cp = sd_cdbtab + i;
13766 
13767 			/*
13768 			 * Call scsi_init_pkt so we can fill in the
13769 			 * CDB.
13770 			 */
13771 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13772 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13773 			    flags, callback, callback_arg);
13774 
13775 			if (return_pktp != NULL) {
13776 
13777 				/*
13778 				 * Return new value of pkt
13779 				 */
13780 				*pktpp = return_pktp;
13781 
13782 				/*
13783 				 * To be safe, zero the CDB insuring there is
13784 				 * no leftover data from a previous command.
13785 				 */
13786 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13787 
13788 				/*
13789 				 * Handle partial DMA mapping
13790 				 */
13791 				if (return_pktp->pkt_resid != 0) {
13792 
13793 					/*
13794 					 * Not going to xfer as many blocks as
13795 					 * originally expected
13796 					 */
13797 					blockcount -=
13798 					    SD_BYTES2TGTBLOCKS(un,
13799 					    return_pktp->pkt_resid);
13800 				}
13801 
13802 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13803 
13804 				/*
13805 				 * Set command byte based on the CDB
13806 				 * type we matched.
13807 				 */
13808 				cdbp->scc_cmd = cp->sc_grpmask |
13809 				    ((bp->b_flags & B_READ) ?
13810 				    SCMD_READ : SCMD_WRITE);
13811 
13812 				SD_FILL_SCSI1_LUN(un, return_pktp);
13813 
13814 				/*
13815 				 * Fill in LBA and length
13816 				 */
13817 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13818 				    (cp->sc_grpcode == CDB_GROUP4) ||
13819 				    (cp->sc_grpcode == CDB_GROUP0) ||
13820 				    (cp->sc_grpcode == CDB_GROUP5));
13821 
13822 				if (cp->sc_grpcode == CDB_GROUP1) {
13823 					FORMG1ADDR(cdbp, lba);
13824 					FORMG1COUNT(cdbp, blockcount);
13825 					return (0);
13826 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13827 					FORMG4LONGADDR(cdbp, lba);
13828 					FORMG4COUNT(cdbp, blockcount);
13829 					return (0);
13830 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13831 					FORMG0ADDR(cdbp, lba);
13832 					FORMG0COUNT(cdbp, blockcount);
13833 					return (0);
13834 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13835 					FORMG5ADDR(cdbp, lba);
13836 					FORMG5COUNT(cdbp, blockcount);
13837 					return (0);
13838 				}
13839 
13840 				/*
13841 				 * It should be impossible to not match one
13842 				 * of the CDB types above, so we should never
13843 				 * reach this point.  Set the CDB command byte
13844 				 * to test-unit-ready to avoid writing
13845 				 * to somewhere we don't intend.
13846 				 */
13847 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13848 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13849 			} else {
13850 				/*
13851 				 * Couldn't get scsi_pkt
13852 				 */
13853 				return (SD_PKT_ALLOC_FAILURE);
13854 			}
13855 		}
13856 	}
13857 
13858 	/*
13859 	 * None of the available CDB types were suitable.  This really
13860 	 * should never happen:  on a 64 bit system we support
13861 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13862 	 * and on a 32 bit system we will refuse to bind to a device
13863 	 * larger than 2TB so addresses will never be larger than 32 bits.
13864 	 */
13865 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13866 }
13867 
13868 /*
13869  *    Function: sd_setup_next_rw_pkt
13870  *
13871  * Description: Setup packet for partial DMA transfers, except for the
13872  * 		initial transfer.  sd_setup_rw_pkt should be used for
13873  *		the initial transfer.
13874  *
13875  *     Context: Kernel thread and may be called from interrupt context.
13876  */
13877 
13878 int
13879 sd_setup_next_rw_pkt(struct sd_lun *un,
13880     struct scsi_pkt *pktp, struct buf *bp,
13881     diskaddr_t lba, uint32_t blockcount)
13882 {
13883 	uchar_t com;
13884 	union scsi_cdb *cdbp;
13885 	uchar_t cdb_group_id;
13886 
13887 	ASSERT(pktp != NULL);
13888 	ASSERT(pktp->pkt_cdbp != NULL);
13889 
13890 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13891 	com = cdbp->scc_cmd;
13892 	cdb_group_id = CDB_GROUPID(com);
13893 
13894 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13895 	    (cdb_group_id == CDB_GROUPID_1) ||
13896 	    (cdb_group_id == CDB_GROUPID_4) ||
13897 	    (cdb_group_id == CDB_GROUPID_5));
13898 
13899 	/*
13900 	 * Move pkt to the next portion of the xfer.
13901 	 * func is NULL_FUNC so we do not have to release
13902 	 * the disk mutex here.
13903 	 */
13904 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13905 	    NULL_FUNC, NULL) == pktp) {
13906 		/* Success.  Handle partial DMA */
13907 		if (pktp->pkt_resid != 0) {
13908 			blockcount -=
13909 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13910 		}
13911 
13912 		cdbp->scc_cmd = com;
13913 		SD_FILL_SCSI1_LUN(un, pktp);
13914 		if (cdb_group_id == CDB_GROUPID_1) {
13915 			FORMG1ADDR(cdbp, lba);
13916 			FORMG1COUNT(cdbp, blockcount);
13917 			return (0);
13918 		} else if (cdb_group_id == CDB_GROUPID_4) {
13919 			FORMG4LONGADDR(cdbp, lba);
13920 			FORMG4COUNT(cdbp, blockcount);
13921 			return (0);
13922 		} else if (cdb_group_id == CDB_GROUPID_0) {
13923 			FORMG0ADDR(cdbp, lba);
13924 			FORMG0COUNT(cdbp, blockcount);
13925 			return (0);
13926 		} else if (cdb_group_id == CDB_GROUPID_5) {
13927 			FORMG5ADDR(cdbp, lba);
13928 			FORMG5COUNT(cdbp, blockcount);
13929 			return (0);
13930 		}
13931 
13932 		/* Unreachable */
13933 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13934 	}
13935 
13936 	/*
13937 	 * Error setting up next portion of cmd transfer.
13938 	 * Something is definitely very wrong and this
13939 	 * should not happen.
13940 	 */
13941 	return (SD_PKT_ALLOC_FAILURE);
13942 }
13943 
13944 /*
13945  *    Function: sd_initpkt_for_uscsi
13946  *
13947  * Description: Allocate and initialize for transport a scsi_pkt struct,
13948  *		based upon the info specified in the given uscsi_cmd struct.
13949  *
13950  * Return Code: SD_PKT_ALLOC_SUCCESS
13951  *		SD_PKT_ALLOC_FAILURE
13952  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13953  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13954  *
13955  *     Context: Kernel thread and may be called from software interrupt context
13956  *		as part of a sdrunout callback. This function may not block or
13957  *		call routines that block
13958  */
13959 
13960 static int
13961 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13962 {
13963 	struct uscsi_cmd *uscmd;
13964 	struct sd_xbuf	*xp;
13965 	struct scsi_pkt	*pktp;
13966 	struct sd_lun	*un;
13967 	uint32_t	flags = 0;
13968 
13969 	ASSERT(bp != NULL);
13970 	ASSERT(pktpp != NULL);
13971 	xp = SD_GET_XBUF(bp);
13972 	ASSERT(xp != NULL);
13973 	un = SD_GET_UN(bp);
13974 	ASSERT(un != NULL);
13975 	ASSERT(mutex_owned(SD_MUTEX(un)));
13976 
13977 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13978 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13979 	ASSERT(uscmd != NULL);
13980 
13981 	SD_TRACE(SD_LOG_IO_CORE, un,
13982 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13983 
13984 	/*
13985 	 * Allocate the scsi_pkt for the command.
13986 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
13987 	 *	 during scsi_init_pkt time and will continue to use the
13988 	 *	 same path as long as the same scsi_pkt is used without
13989 	 *	 intervening scsi_dma_free(). Since uscsi command does
13990 	 *	 not call scsi_dmafree() before retry failed command, it
13991 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
13992 	 *	 set such that scsi_vhci can use other available path for
13993 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
13994 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
13995 	 */
13996 	if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
13997 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13998 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13999 		    ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14000 		    - sizeof (struct scsi_extended_sense)), 0,
14001 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14002 		    sdrunout, (caddr_t)un);
14003 	} else {
14004 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14005 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14006 		    sizeof (struct scsi_arq_status), 0,
14007 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14008 		    sdrunout, (caddr_t)un);
14009 	}
14010 
14011 	if (pktp == NULL) {
14012 		*pktpp = NULL;
14013 		/*
14014 		 * Set the driver state to RWAIT to indicate the driver
14015 		 * is waiting on resource allocations. The driver will not
14016 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
14017 		 */
14018 		New_state(un, SD_STATE_RWAIT);
14019 
14020 		SD_ERROR(SD_LOG_IO_CORE, un,
14021 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14022 
14023 		if ((bp->b_flags & B_ERROR) != 0) {
14024 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14025 		}
14026 		return (SD_PKT_ALLOC_FAILURE);
14027 	}
14028 
14029 	/*
14030 	 * We do not do DMA breakup for USCSI commands, so return failure
14031 	 * here if all the needed DMA resources were not allocated.
14032 	 */
14033 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14034 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14035 		scsi_destroy_pkt(pktp);
14036 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14037 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14038 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14039 	}
14040 
14041 	/* Init the cdb from the given uscsi struct */
14042 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14043 	    uscmd->uscsi_cdb[0], 0, 0, 0);
14044 
14045 	SD_FILL_SCSI1_LUN(un, pktp);
14046 
14047 	/*
14048 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
14049 	 * for listing of the supported flags.
14050 	 */
14051 
14052 	if (uscmd->uscsi_flags & USCSI_SILENT) {
14053 		flags |= FLAG_SILENT;
14054 	}
14055 
14056 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14057 		flags |= FLAG_DIAGNOSE;
14058 	}
14059 
14060 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14061 		flags |= FLAG_ISOLATE;
14062 	}
14063 
14064 	if (un->un_f_is_fibre == FALSE) {
14065 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14066 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14067 		}
14068 	}
14069 
14070 	/*
14071 	 * Set the pkt flags here so we save time later.
14072 	 * Note: These flags are NOT in the uscsi man page!!!
14073 	 */
14074 	if (uscmd->uscsi_flags & USCSI_HEAD) {
14075 		flags |= FLAG_HEAD;
14076 	}
14077 
14078 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
14079 		flags |= FLAG_NOINTR;
14080 	}
14081 
14082 	/*
14083 	 * For tagged queueing, things get a bit complicated.
14084 	 * Check first for head of queue and last for ordered queue.
14085 	 * If neither head nor order, use the default driver tag flags.
14086 	 */
14087 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14088 		if (uscmd->uscsi_flags & USCSI_HTAG) {
14089 			flags |= FLAG_HTAG;
14090 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
14091 			flags |= FLAG_OTAG;
14092 		} else {
14093 			flags |= un->un_tagflags & FLAG_TAGMASK;
14094 		}
14095 	}
14096 
14097 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
14098 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14099 	}
14100 
14101 	pktp->pkt_flags = flags;
14102 
14103 	/* Transfer uscsi information to scsi_pkt */
14104 	(void) scsi_uscsi_pktinit(uscmd, pktp);
14105 
14106 	/* Copy the caller's CDB into the pkt... */
14107 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14108 
14109 	if (uscmd->uscsi_timeout == 0) {
14110 		pktp->pkt_time = un->un_uscsi_timeout;
14111 	} else {
14112 		pktp->pkt_time = uscmd->uscsi_timeout;
14113 	}
14114 
14115 	/* need it later to identify USCSI request in sdintr */
14116 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
14117 
14118 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
14119 
14120 	pktp->pkt_private = bp;
14121 	pktp->pkt_comp = sdintr;
14122 	*pktpp = pktp;
14123 
14124 	SD_TRACE(SD_LOG_IO_CORE, un,
14125 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14126 
14127 	return (SD_PKT_ALLOC_SUCCESS);
14128 }
14129 
14130 
14131 /*
14132  *    Function: sd_destroypkt_for_uscsi
14133  *
14134  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14135  *		IOs.. Also saves relevant info into the associated uscsi_cmd
14136  *		struct.
14137  *
14138  *     Context: May be called under interrupt context
14139  */
14140 
14141 static void
14142 sd_destroypkt_for_uscsi(struct buf *bp)
14143 {
14144 	struct uscsi_cmd *uscmd;
14145 	struct sd_xbuf	*xp;
14146 	struct scsi_pkt	*pktp;
14147 	struct sd_lun	*un;
14148 	struct sd_uscsi_info *suip;
14149 
14150 	ASSERT(bp != NULL);
14151 	xp = SD_GET_XBUF(bp);
14152 	ASSERT(xp != NULL);
14153 	un = SD_GET_UN(bp);
14154 	ASSERT(un != NULL);
14155 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14156 	pktp = SD_GET_PKTP(bp);
14157 	ASSERT(pktp != NULL);
14158 
14159 	SD_TRACE(SD_LOG_IO_CORE, un,
14160 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14161 
14162 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14163 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14164 	ASSERT(uscmd != NULL);
14165 
14166 	/* Save the status and the residual into the uscsi_cmd struct */
14167 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14168 	uscmd->uscsi_resid  = bp->b_resid;
14169 
14170 	/* Transfer scsi_pkt information to uscsi */
14171 	(void) scsi_uscsi_pktfini(pktp, uscmd);
14172 
14173 	/*
14174 	 * If enabled, copy any saved sense data into the area specified
14175 	 * by the uscsi command.
14176 	 */
14177 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14178 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14179 		/*
14180 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14181 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14182 		 */
14183 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14184 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14185 		if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14186 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14187 			    MAX_SENSE_LENGTH);
14188 		} else {
14189 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14190 			    SENSE_LENGTH);
14191 		}
14192 	}
14193 	/*
14194 	 * The following assignments are for SCSI FMA.
14195 	 */
14196 	ASSERT(xp->xb_private != NULL);
14197 	suip = (struct sd_uscsi_info *)xp->xb_private;
14198 	suip->ui_pkt_reason = pktp->pkt_reason;
14199 	suip->ui_pkt_state = pktp->pkt_state;
14200 	suip->ui_pkt_statistics = pktp->pkt_statistics;
14201 	suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14202 
14203 	/* We are done with the scsi_pkt; free it now */
14204 	ASSERT(SD_GET_PKTP(bp) != NULL);
14205 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14206 
14207 	SD_TRACE(SD_LOG_IO_CORE, un,
14208 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14209 }
14210 
14211 
14212 /*
14213  *    Function: sd_bioclone_alloc
14214  *
14215  * Description: Allocate a buf(9S) and init it as per the given buf
14216  *		and the various arguments.  The associated sd_xbuf
14217  *		struct is (nearly) duplicated.  The struct buf *bp
14218  *		argument is saved in new_xp->xb_private.
14219  *
14220  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14221  *		datalen - size of data area for the shadow bp
14222  *		blkno - starting LBA
14223  *		func - function pointer for b_iodone in the shadow buf. (May
14224  *			be NULL if none.)
14225  *
14226  * Return Code: Pointer to allocates buf(9S) struct
14227  *
14228  *     Context: Can sleep.
14229  */
14230 
14231 static struct buf *
14232 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14233 	daddr_t blkno, int (*func)(struct buf *))
14234 {
14235 	struct	sd_lun	*un;
14236 	struct	sd_xbuf	*xp;
14237 	struct	sd_xbuf	*new_xp;
14238 	struct	buf	*new_bp;
14239 
14240 	ASSERT(bp != NULL);
14241 	xp = SD_GET_XBUF(bp);
14242 	ASSERT(xp != NULL);
14243 	un = SD_GET_UN(bp);
14244 	ASSERT(un != NULL);
14245 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14246 
14247 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14248 	    NULL, KM_SLEEP);
14249 
14250 	new_bp->b_lblkno	= blkno;
14251 
14252 	/*
14253 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14254 	 * original xbuf into it.
14255 	 */
14256 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14257 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14258 
14259 	/*
14260 	 * The given bp is automatically saved in the xb_private member
14261 	 * of the new xbuf.  Callers are allowed to depend on this.
14262 	 */
14263 	new_xp->xb_private = bp;
14264 
14265 	new_bp->b_private  = new_xp;
14266 
14267 	return (new_bp);
14268 }
14269 
14270 /*
14271  *    Function: sd_shadow_buf_alloc
14272  *
14273  * Description: Allocate a buf(9S) and init it as per the given buf
14274  *		and the various arguments.  The associated sd_xbuf
14275  *		struct is (nearly) duplicated.  The struct buf *bp
14276  *		argument is saved in new_xp->xb_private.
14277  *
14278  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14279  *		datalen - size of data area for the shadow bp
14280  *		bflags - B_READ or B_WRITE (pseudo flag)
14281  *		blkno - starting LBA
14282  *		func - function pointer for b_iodone in the shadow buf. (May
14283  *			be NULL if none.)
14284  *
14285  * Return Code: Pointer to allocates buf(9S) struct
14286  *
14287  *     Context: Can sleep.
14288  */
14289 
14290 static struct buf *
14291 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14292 	daddr_t blkno, int (*func)(struct buf *))
14293 {
14294 	struct	sd_lun	*un;
14295 	struct	sd_xbuf	*xp;
14296 	struct	sd_xbuf	*new_xp;
14297 	struct	buf	*new_bp;
14298 
14299 	ASSERT(bp != NULL);
14300 	xp = SD_GET_XBUF(bp);
14301 	ASSERT(xp != NULL);
14302 	un = SD_GET_UN(bp);
14303 	ASSERT(un != NULL);
14304 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14305 
14306 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14307 		bp_mapin(bp);
14308 	}
14309 
14310 	bflags &= (B_READ | B_WRITE);
14311 #if defined(__i386) || defined(__amd64)
14312 	new_bp = getrbuf(KM_SLEEP);
14313 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14314 	new_bp->b_bcount = datalen;
14315 	new_bp->b_flags = bflags |
14316 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14317 #else
14318 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14319 	    datalen, bflags, SLEEP_FUNC, NULL);
14320 #endif
14321 	new_bp->av_forw	= NULL;
14322 	new_bp->av_back	= NULL;
14323 	new_bp->b_dev	= bp->b_dev;
14324 	new_bp->b_blkno	= blkno;
14325 	new_bp->b_iodone = func;
14326 	new_bp->b_edev	= bp->b_edev;
14327 	new_bp->b_resid	= 0;
14328 
14329 	/* We need to preserve the B_FAILFAST flag */
14330 	if (bp->b_flags & B_FAILFAST) {
14331 		new_bp->b_flags |= B_FAILFAST;
14332 	}
14333 
14334 	/*
14335 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14336 	 * original xbuf into it.
14337 	 */
14338 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14339 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14340 
14341 	/* Need later to copy data between the shadow buf & original buf! */
14342 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14343 
14344 	/*
14345 	 * The given bp is automatically saved in the xb_private member
14346 	 * of the new xbuf.  Callers are allowed to depend on this.
14347 	 */
14348 	new_xp->xb_private = bp;
14349 
14350 	new_bp->b_private  = new_xp;
14351 
14352 	return (new_bp);
14353 }
14354 
14355 /*
14356  *    Function: sd_bioclone_free
14357  *
14358  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14359  *		in the larger than partition operation.
14360  *
14361  *     Context: May be called under interrupt context
14362  */
14363 
14364 static void
14365 sd_bioclone_free(struct buf *bp)
14366 {
14367 	struct sd_xbuf	*xp;
14368 
14369 	ASSERT(bp != NULL);
14370 	xp = SD_GET_XBUF(bp);
14371 	ASSERT(xp != NULL);
14372 
14373 	/*
14374 	 * Call bp_mapout() before freeing the buf,  in case a lower
14375 	 * layer or HBA  had done a bp_mapin().  we must do this here
14376 	 * as we are the "originator" of the shadow buf.
14377 	 */
14378 	bp_mapout(bp);
14379 
14380 	/*
14381 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14382 	 * never gets confused by a stale value in this field. (Just a little
14383 	 * extra defensiveness here.)
14384 	 */
14385 	bp->b_iodone = NULL;
14386 
14387 	freerbuf(bp);
14388 
14389 	kmem_free(xp, sizeof (struct sd_xbuf));
14390 }
14391 
14392 /*
14393  *    Function: sd_shadow_buf_free
14394  *
14395  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14396  *
14397  *     Context: May be called under interrupt context
14398  */
14399 
14400 static void
14401 sd_shadow_buf_free(struct buf *bp)
14402 {
14403 	struct sd_xbuf	*xp;
14404 
14405 	ASSERT(bp != NULL);
14406 	xp = SD_GET_XBUF(bp);
14407 	ASSERT(xp != NULL);
14408 
14409 #if defined(__sparc)
14410 	/*
14411 	 * Call bp_mapout() before freeing the buf,  in case a lower
14412 	 * layer or HBA  had done a bp_mapin().  we must do this here
14413 	 * as we are the "originator" of the shadow buf.
14414 	 */
14415 	bp_mapout(bp);
14416 #endif
14417 
14418 	/*
14419 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14420 	 * never gets confused by a stale value in this field. (Just a little
14421 	 * extra defensiveness here.)
14422 	 */
14423 	bp->b_iodone = NULL;
14424 
14425 #if defined(__i386) || defined(__amd64)
14426 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14427 	freerbuf(bp);
14428 #else
14429 	scsi_free_consistent_buf(bp);
14430 #endif
14431 
14432 	kmem_free(xp, sizeof (struct sd_xbuf));
14433 }
14434 
14435 
14436 /*
14437  *    Function: sd_print_transport_rejected_message
14438  *
14439  * Description: This implements the ludicrously complex rules for printing
14440  *		a "transport rejected" message.  This is to address the
14441  *		specific problem of having a flood of this error message
14442  *		produced when a failover occurs.
14443  *
14444  *     Context: Any.
14445  */
14446 
14447 static void
14448 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14449 	int code)
14450 {
14451 	ASSERT(un != NULL);
14452 	ASSERT(mutex_owned(SD_MUTEX(un)));
14453 	ASSERT(xp != NULL);
14454 
14455 	/*
14456 	 * Print the "transport rejected" message under the following
14457 	 * conditions:
14458 	 *
14459 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14460 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14461 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14462 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14463 	 *   scsi_transport(9F) (which indicates that the target might have
14464 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14465 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14466 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14467 	 *   from scsi_transport().
14468 	 *
14469 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14470 	 * the preceeding cases in order for the message to be printed.
14471 	 */
14472 	if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14473 	    (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14474 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14475 		    (code != TRAN_FATAL_ERROR) ||
14476 		    (un->un_tran_fatal_count == 1)) {
14477 			switch (code) {
14478 			case TRAN_BADPKT:
14479 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14480 				    "transport rejected bad packet\n");
14481 				break;
14482 			case TRAN_FATAL_ERROR:
14483 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14484 				    "transport rejected fatal error\n");
14485 				break;
14486 			default:
14487 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14488 				    "transport rejected (%d)\n", code);
14489 				break;
14490 			}
14491 		}
14492 	}
14493 }
14494 
14495 
14496 /*
14497  *    Function: sd_add_buf_to_waitq
14498  *
14499  * Description: Add the given buf(9S) struct to the wait queue for the
14500  *		instance.  If sorting is enabled, then the buf is added
14501  *		to the queue via an elevator sort algorithm (a la
14502  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14503  *		If sorting is not enabled, then the buf is just added
14504  *		to the end of the wait queue.
14505  *
14506  * Return Code: void
14507  *
14508  *     Context: Does not sleep/block, therefore technically can be called
14509  *		from any context.  However if sorting is enabled then the
14510  *		execution time is indeterminate, and may take long if
14511  *		the wait queue grows large.
14512  */
14513 
14514 static void
14515 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14516 {
14517 	struct buf *ap;
14518 
14519 	ASSERT(bp != NULL);
14520 	ASSERT(un != NULL);
14521 	ASSERT(mutex_owned(SD_MUTEX(un)));
14522 
14523 	/* If the queue is empty, add the buf as the only entry & return. */
14524 	if (un->un_waitq_headp == NULL) {
14525 		ASSERT(un->un_waitq_tailp == NULL);
14526 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14527 		bp->av_forw = NULL;
14528 		return;
14529 	}
14530 
14531 	ASSERT(un->un_waitq_tailp != NULL);
14532 
14533 	/*
14534 	 * If sorting is disabled, just add the buf to the tail end of
14535 	 * the wait queue and return.
14536 	 */
14537 	if (un->un_f_disksort_disabled) {
14538 		un->un_waitq_tailp->av_forw = bp;
14539 		un->un_waitq_tailp = bp;
14540 		bp->av_forw = NULL;
14541 		return;
14542 	}
14543 
14544 	/*
14545 	 * Sort thru the list of requests currently on the wait queue
14546 	 * and add the new buf request at the appropriate position.
14547 	 *
14548 	 * The un->un_waitq_headp is an activity chain pointer on which
14549 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14550 	 * first queue holds those requests which are positioned after
14551 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14552 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14553 	 * Thus we implement a one way scan, retracting after reaching
14554 	 * the end of the drive to the first request on the second
14555 	 * queue, at which time it becomes the first queue.
14556 	 * A one-way scan is natural because of the way UNIX read-ahead
14557 	 * blocks are allocated.
14558 	 *
14559 	 * If we lie after the first request, then we must locate the
14560 	 * second request list and add ourselves to it.
14561 	 */
14562 	ap = un->un_waitq_headp;
14563 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14564 		while (ap->av_forw != NULL) {
14565 			/*
14566 			 * Look for an "inversion" in the (normally
14567 			 * ascending) block numbers. This indicates
14568 			 * the start of the second request list.
14569 			 */
14570 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14571 				/*
14572 				 * Search the second request list for the
14573 				 * first request at a larger block number.
14574 				 * We go before that; however if there is
14575 				 * no such request, we go at the end.
14576 				 */
14577 				do {
14578 					if (SD_GET_BLKNO(bp) <
14579 					    SD_GET_BLKNO(ap->av_forw)) {
14580 						goto insert;
14581 					}
14582 					ap = ap->av_forw;
14583 				} while (ap->av_forw != NULL);
14584 				goto insert;		/* after last */
14585 			}
14586 			ap = ap->av_forw;
14587 		}
14588 
14589 		/*
14590 		 * No inversions... we will go after the last, and
14591 		 * be the first request in the second request list.
14592 		 */
14593 		goto insert;
14594 	}
14595 
14596 	/*
14597 	 * Request is at/after the current request...
14598 	 * sort in the first request list.
14599 	 */
14600 	while (ap->av_forw != NULL) {
14601 		/*
14602 		 * We want to go after the current request (1) if
14603 		 * there is an inversion after it (i.e. it is the end
14604 		 * of the first request list), or (2) if the next
14605 		 * request is a larger block no. than our request.
14606 		 */
14607 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14608 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14609 			goto insert;
14610 		}
14611 		ap = ap->av_forw;
14612 	}
14613 
14614 	/*
14615 	 * Neither a second list nor a larger request, therefore
14616 	 * we go at the end of the first list (which is the same
14617 	 * as the end of the whole schebang).
14618 	 */
14619 insert:
14620 	bp->av_forw = ap->av_forw;
14621 	ap->av_forw = bp;
14622 
14623 	/*
14624 	 * If we inserted onto the tail end of the waitq, make sure the
14625 	 * tail pointer is updated.
14626 	 */
14627 	if (ap == un->un_waitq_tailp) {
14628 		un->un_waitq_tailp = bp;
14629 	}
14630 }
14631 
14632 
14633 /*
14634  *    Function: sd_start_cmds
14635  *
14636  * Description: Remove and transport cmds from the driver queues.
14637  *
14638  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14639  *
14640  *		immed_bp - ptr to a buf to be transported immediately. Only
14641  *		the immed_bp is transported; bufs on the waitq are not
14642  *		processed and the un_retry_bp is not checked.  If immed_bp is
14643  *		NULL, then normal queue processing is performed.
14644  *
14645  *     Context: May be called from kernel thread context, interrupt context,
14646  *		or runout callback context. This function may not block or
14647  *		call routines that block.
14648  */
14649 
14650 static void
14651 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14652 {
14653 	struct	sd_xbuf	*xp;
14654 	struct	buf	*bp;
14655 	void	(*statp)(kstat_io_t *);
14656 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14657 	void	(*saved_statp)(kstat_io_t *);
14658 #endif
14659 	int	rval;
14660 	struct sd_fm_internal *sfip = NULL;
14661 
14662 	ASSERT(un != NULL);
14663 	ASSERT(mutex_owned(SD_MUTEX(un)));
14664 	ASSERT(un->un_ncmds_in_transport >= 0);
14665 	ASSERT(un->un_throttle >= 0);
14666 
14667 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14668 
14669 	do {
14670 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14671 		saved_statp = NULL;
14672 #endif
14673 
14674 		/*
14675 		 * If we are syncing or dumping, fail the command to
14676 		 * avoid recursively calling back into scsi_transport().
14677 		 * The dump I/O itself uses a separate code path so this
14678 		 * only prevents non-dump I/O from being sent while dumping.
14679 		 * File system sync takes place before dumping begins.
14680 		 * During panic, filesystem I/O is allowed provided
14681 		 * un_in_callback is <= 1.  This is to prevent recursion
14682 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14683 		 * sd_start_cmds and so on.  See panic.c for more information
14684 		 * about the states the system can be in during panic.
14685 		 */
14686 		if ((un->un_state == SD_STATE_DUMPING) ||
14687 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14688 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14689 			    "sd_start_cmds: panicking\n");
14690 			goto exit;
14691 		}
14692 
14693 		if ((bp = immed_bp) != NULL) {
14694 			/*
14695 			 * We have a bp that must be transported immediately.
14696 			 * It's OK to transport the immed_bp here without doing
14697 			 * the throttle limit check because the immed_bp is
14698 			 * always used in a retry/recovery case. This means
14699 			 * that we know we are not at the throttle limit by
14700 			 * virtue of the fact that to get here we must have
14701 			 * already gotten a command back via sdintr(). This also
14702 			 * relies on (1) the command on un_retry_bp preventing
14703 			 * further commands from the waitq from being issued;
14704 			 * and (2) the code in sd_retry_command checking the
14705 			 * throttle limit before issuing a delayed or immediate
14706 			 * retry. This holds even if the throttle limit is
14707 			 * currently ratcheted down from its maximum value.
14708 			 */
14709 			statp = kstat_runq_enter;
14710 			if (bp == un->un_retry_bp) {
14711 				ASSERT((un->un_retry_statp == NULL) ||
14712 				    (un->un_retry_statp == kstat_waitq_enter) ||
14713 				    (un->un_retry_statp ==
14714 				    kstat_runq_back_to_waitq));
14715 				/*
14716 				 * If the waitq kstat was incremented when
14717 				 * sd_set_retry_bp() queued this bp for a retry,
14718 				 * then we must set up statp so that the waitq
14719 				 * count will get decremented correctly below.
14720 				 * Also we must clear un->un_retry_statp to
14721 				 * ensure that we do not act on a stale value
14722 				 * in this field.
14723 				 */
14724 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14725 				    (un->un_retry_statp ==
14726 				    kstat_runq_back_to_waitq)) {
14727 					statp = kstat_waitq_to_runq;
14728 				}
14729 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14730 				saved_statp = un->un_retry_statp;
14731 #endif
14732 				un->un_retry_statp = NULL;
14733 
14734 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14735 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14736 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14737 				    un, un->un_retry_bp, un->un_throttle,
14738 				    un->un_ncmds_in_transport);
14739 			} else {
14740 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14741 				    "processing priority bp:0x%p\n", bp);
14742 			}
14743 
14744 		} else if ((bp = un->un_waitq_headp) != NULL) {
14745 			/*
14746 			 * A command on the waitq is ready to go, but do not
14747 			 * send it if:
14748 			 *
14749 			 * (1) the throttle limit has been reached, or
14750 			 * (2) a retry is pending, or
14751 			 * (3) a START_STOP_UNIT callback pending, or
14752 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14753 			 *	command is pending.
14754 			 *
14755 			 * For all of these conditions, IO processing will
14756 			 * restart after the condition is cleared.
14757 			 */
14758 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14759 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14760 				    "sd_start_cmds: exiting, "
14761 				    "throttle limit reached!\n");
14762 				goto exit;
14763 			}
14764 			if (un->un_retry_bp != NULL) {
14765 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14766 				    "sd_start_cmds: exiting, retry pending!\n");
14767 				goto exit;
14768 			}
14769 			if (un->un_startstop_timeid != NULL) {
14770 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14771 				    "sd_start_cmds: exiting, "
14772 				    "START_STOP pending!\n");
14773 				goto exit;
14774 			}
14775 			if (un->un_direct_priority_timeid != NULL) {
14776 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14777 				    "sd_start_cmds: exiting, "
14778 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14779 				goto exit;
14780 			}
14781 
14782 			/* Dequeue the command */
14783 			un->un_waitq_headp = bp->av_forw;
14784 			if (un->un_waitq_headp == NULL) {
14785 				un->un_waitq_tailp = NULL;
14786 			}
14787 			bp->av_forw = NULL;
14788 			statp = kstat_waitq_to_runq;
14789 			SD_TRACE(SD_LOG_IO_CORE, un,
14790 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14791 
14792 		} else {
14793 			/* No work to do so bail out now */
14794 			SD_TRACE(SD_LOG_IO_CORE, un,
14795 			    "sd_start_cmds: no more work, exiting!\n");
14796 			goto exit;
14797 		}
14798 
14799 		/*
14800 		 * Reset the state to normal. This is the mechanism by which
14801 		 * the state transitions from either SD_STATE_RWAIT or
14802 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14803 		 * If state is SD_STATE_PM_CHANGING then this command is
14804 		 * part of the device power control and the state must
14805 		 * not be put back to normal. Doing so would would
14806 		 * allow new commands to proceed when they shouldn't,
14807 		 * the device may be going off.
14808 		 */
14809 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14810 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14811 			New_state(un, SD_STATE_NORMAL);
14812 		}
14813 
14814 		xp = SD_GET_XBUF(bp);
14815 		ASSERT(xp != NULL);
14816 
14817 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14818 		/*
14819 		 * Allocate the scsi_pkt if we need one, or attach DMA
14820 		 * resources if we have a scsi_pkt that needs them. The
14821 		 * latter should only occur for commands that are being
14822 		 * retried.
14823 		 */
14824 		if ((xp->xb_pktp == NULL) ||
14825 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14826 #else
14827 		if (xp->xb_pktp == NULL) {
14828 #endif
14829 			/*
14830 			 * There is no scsi_pkt allocated for this buf. Call
14831 			 * the initpkt function to allocate & init one.
14832 			 *
14833 			 * The scsi_init_pkt runout callback functionality is
14834 			 * implemented as follows:
14835 			 *
14836 			 * 1) The initpkt function always calls
14837 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14838 			 *    callback routine.
14839 			 * 2) A successful packet allocation is initialized and
14840 			 *    the I/O is transported.
14841 			 * 3) The I/O associated with an allocation resource
14842 			 *    failure is left on its queue to be retried via
14843 			 *    runout or the next I/O.
14844 			 * 4) The I/O associated with a DMA error is removed
14845 			 *    from the queue and failed with EIO. Processing of
14846 			 *    the transport queues is also halted to be
14847 			 *    restarted via runout or the next I/O.
14848 			 * 5) The I/O associated with a CDB size or packet
14849 			 *    size error is removed from the queue and failed
14850 			 *    with EIO. Processing of the transport queues is
14851 			 *    continued.
14852 			 *
14853 			 * Note: there is no interface for canceling a runout
14854 			 * callback. To prevent the driver from detaching or
14855 			 * suspending while a runout is pending the driver
14856 			 * state is set to SD_STATE_RWAIT
14857 			 *
14858 			 * Note: using the scsi_init_pkt callback facility can
14859 			 * result in an I/O request persisting at the head of
14860 			 * the list which cannot be satisfied even after
14861 			 * multiple retries. In the future the driver may
14862 			 * implement some kind of maximum runout count before
14863 			 * failing an I/O.
14864 			 *
14865 			 * Note: the use of funcp below may seem superfluous,
14866 			 * but it helps warlock figure out the correct
14867 			 * initpkt function calls (see [s]sd.wlcmd).
14868 			 */
14869 			struct scsi_pkt	*pktp;
14870 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14871 
14872 			ASSERT(bp != un->un_rqs_bp);
14873 
14874 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14875 			switch ((*funcp)(bp, &pktp)) {
14876 			case  SD_PKT_ALLOC_SUCCESS:
14877 				xp->xb_pktp = pktp;
14878 				SD_TRACE(SD_LOG_IO_CORE, un,
14879 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14880 				    pktp);
14881 				goto got_pkt;
14882 
14883 			case SD_PKT_ALLOC_FAILURE:
14884 				/*
14885 				 * Temporary (hopefully) resource depletion.
14886 				 * Since retries and RQS commands always have a
14887 				 * scsi_pkt allocated, these cases should never
14888 				 * get here. So the only cases this needs to
14889 				 * handle is a bp from the waitq (which we put
14890 				 * back onto the waitq for sdrunout), or a bp
14891 				 * sent as an immed_bp (which we just fail).
14892 				 */
14893 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14894 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14895 
14896 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14897 
14898 				if (bp == immed_bp) {
14899 					/*
14900 					 * If SD_XB_DMA_FREED is clear, then
14901 					 * this is a failure to allocate a
14902 					 * scsi_pkt, and we must fail the
14903 					 * command.
14904 					 */
14905 					if ((xp->xb_pkt_flags &
14906 					    SD_XB_DMA_FREED) == 0) {
14907 						break;
14908 					}
14909 
14910 					/*
14911 					 * If this immediate command is NOT our
14912 					 * un_retry_bp, then we must fail it.
14913 					 */
14914 					if (bp != un->un_retry_bp) {
14915 						break;
14916 					}
14917 
14918 					/*
14919 					 * We get here if this cmd is our
14920 					 * un_retry_bp that was DMAFREED, but
14921 					 * scsi_init_pkt() failed to reallocate
14922 					 * DMA resources when we attempted to
14923 					 * retry it. This can happen when an
14924 					 * mpxio failover is in progress, but
14925 					 * we don't want to just fail the
14926 					 * command in this case.
14927 					 *
14928 					 * Use timeout(9F) to restart it after
14929 					 * a 100ms delay.  We don't want to
14930 					 * let sdrunout() restart it, because
14931 					 * sdrunout() is just supposed to start
14932 					 * commands that are sitting on the
14933 					 * wait queue.  The un_retry_bp stays
14934 					 * set until the command completes, but
14935 					 * sdrunout can be called many times
14936 					 * before that happens.  Since sdrunout
14937 					 * cannot tell if the un_retry_bp is
14938 					 * already in the transport, it could
14939 					 * end up calling scsi_transport() for
14940 					 * the un_retry_bp multiple times.
14941 					 *
14942 					 * Also: don't schedule the callback
14943 					 * if some other callback is already
14944 					 * pending.
14945 					 */
14946 					if (un->un_retry_statp == NULL) {
14947 						/*
14948 						 * restore the kstat pointer to
14949 						 * keep kstat counts coherent
14950 						 * when we do retry the command.
14951 						 */
14952 						un->un_retry_statp =
14953 						    saved_statp;
14954 					}
14955 
14956 					if ((un->un_startstop_timeid == NULL) &&
14957 					    (un->un_retry_timeid == NULL) &&
14958 					    (un->un_direct_priority_timeid ==
14959 					    NULL)) {
14960 
14961 						un->un_retry_timeid =
14962 						    timeout(
14963 						    sd_start_retry_command,
14964 						    un, SD_RESTART_TIMEOUT);
14965 					}
14966 					goto exit;
14967 				}
14968 
14969 #else
14970 				if (bp == immed_bp) {
14971 					break;	/* Just fail the command */
14972 				}
14973 #endif
14974 
14975 				/* Add the buf back to the head of the waitq */
14976 				bp->av_forw = un->un_waitq_headp;
14977 				un->un_waitq_headp = bp;
14978 				if (un->un_waitq_tailp == NULL) {
14979 					un->un_waitq_tailp = bp;
14980 				}
14981 				goto exit;
14982 
14983 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
14984 				/*
14985 				 * HBA DMA resource failure. Fail the command
14986 				 * and continue processing of the queues.
14987 				 */
14988 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14989 				    "sd_start_cmds: "
14990 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14991 				break;
14992 
14993 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14994 				/*
14995 				 * Note:x86: Partial DMA mapping not supported
14996 				 * for USCSI commands, and all the needed DMA
14997 				 * resources were not allocated.
14998 				 */
14999 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15000 				    "sd_start_cmds: "
15001 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15002 				break;
15003 
15004 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15005 				/*
15006 				 * Note:x86: Request cannot fit into CDB based
15007 				 * on lba and len.
15008 				 */
15009 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15010 				    "sd_start_cmds: "
15011 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15012 				break;
15013 
15014 			default:
15015 				/* Should NEVER get here! */
15016 				panic("scsi_initpkt error");
15017 				/*NOTREACHED*/
15018 			}
15019 
15020 			/*
15021 			 * Fatal error in allocating a scsi_pkt for this buf.
15022 			 * Update kstats & return the buf with an error code.
15023 			 * We must use sd_return_failed_command_no_restart() to
15024 			 * avoid a recursive call back into sd_start_cmds().
15025 			 * However this also means that we must keep processing
15026 			 * the waitq here in order to avoid stalling.
15027 			 */
15028 			if (statp == kstat_waitq_to_runq) {
15029 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15030 			}
15031 			sd_return_failed_command_no_restart(un, bp, EIO);
15032 			if (bp == immed_bp) {
15033 				/* immed_bp is gone by now, so clear this */
15034 				immed_bp = NULL;
15035 			}
15036 			continue;
15037 		}
15038 got_pkt:
15039 		if (bp == immed_bp) {
15040 			/* goto the head of the class.... */
15041 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15042 		}
15043 
15044 		un->un_ncmds_in_transport++;
15045 		SD_UPDATE_KSTATS(un, statp, bp);
15046 
15047 		/*
15048 		 * Call scsi_transport() to send the command to the target.
15049 		 * According to SCSA architecture, we must drop the mutex here
15050 		 * before calling scsi_transport() in order to avoid deadlock.
15051 		 * Note that the scsi_pkt's completion routine can be executed
15052 		 * (from interrupt context) even before the call to
15053 		 * scsi_transport() returns.
15054 		 */
15055 		SD_TRACE(SD_LOG_IO_CORE, un,
15056 		    "sd_start_cmds: calling scsi_transport()\n");
15057 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15058 
15059 		mutex_exit(SD_MUTEX(un));
15060 		rval = scsi_transport(xp->xb_pktp);
15061 		mutex_enter(SD_MUTEX(un));
15062 
15063 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15064 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
15065 
15066 		switch (rval) {
15067 		case TRAN_ACCEPT:
15068 			/* Clear this with every pkt accepted by the HBA */
15069 			un->un_tran_fatal_count = 0;
15070 			break;	/* Success; try the next cmd (if any) */
15071 
15072 		case TRAN_BUSY:
15073 			un->un_ncmds_in_transport--;
15074 			ASSERT(un->un_ncmds_in_transport >= 0);
15075 
15076 			/*
15077 			 * Don't retry request sense, the sense data
15078 			 * is lost when another request is sent.
15079 			 * Free up the rqs buf and retry
15080 			 * the original failed cmd.  Update kstat.
15081 			 */
15082 			if (bp == un->un_rqs_bp) {
15083 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15084 				bp = sd_mark_rqs_idle(un, xp);
15085 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15086 				    NULL, NULL, EIO, un->un_busy_timeout / 500,
15087 				    kstat_waitq_enter);
15088 				goto exit;
15089 			}
15090 
15091 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15092 			/*
15093 			 * Free the DMA resources for the  scsi_pkt. This will
15094 			 * allow mpxio to select another path the next time
15095 			 * we call scsi_transport() with this scsi_pkt.
15096 			 * See sdintr() for the rationalization behind this.
15097 			 */
15098 			if ((un->un_f_is_fibre == TRUE) &&
15099 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15100 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15101 				scsi_dmafree(xp->xb_pktp);
15102 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15103 			}
15104 #endif
15105 
15106 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15107 				/*
15108 				 * Commands that are SD_PATH_DIRECT_PRIORITY
15109 				 * are for error recovery situations. These do
15110 				 * not use the normal command waitq, so if they
15111 				 * get a TRAN_BUSY we cannot put them back onto
15112 				 * the waitq for later retry. One possible
15113 				 * problem is that there could already be some
15114 				 * other command on un_retry_bp that is waiting
15115 				 * for this one to complete, so we would be
15116 				 * deadlocked if we put this command back onto
15117 				 * the waitq for later retry (since un_retry_bp
15118 				 * must complete before the driver gets back to
15119 				 * commands on the waitq).
15120 				 *
15121 				 * To avoid deadlock we must schedule a callback
15122 				 * that will restart this command after a set
15123 				 * interval.  This should keep retrying for as
15124 				 * long as the underlying transport keeps
15125 				 * returning TRAN_BUSY (just like for other
15126 				 * commands).  Use the same timeout interval as
15127 				 * for the ordinary TRAN_BUSY retry.
15128 				 */
15129 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15130 				    "sd_start_cmds: scsi_transport() returned "
15131 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15132 
15133 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15134 				un->un_direct_priority_timeid =
15135 				    timeout(sd_start_direct_priority_command,
15136 				    bp, un->un_busy_timeout / 500);
15137 
15138 				goto exit;
15139 			}
15140 
15141 			/*
15142 			 * For TRAN_BUSY, we want to reduce the throttle value,
15143 			 * unless we are retrying a command.
15144 			 */
15145 			if (bp != un->un_retry_bp) {
15146 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15147 			}
15148 
15149 			/*
15150 			 * Set up the bp to be tried again 10 ms later.
15151 			 * Note:x86: Is there a timeout value in the sd_lun
15152 			 * for this condition?
15153 			 */
15154 			sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15155 			    kstat_runq_back_to_waitq);
15156 			goto exit;
15157 
15158 		case TRAN_FATAL_ERROR:
15159 			un->un_tran_fatal_count++;
15160 			/* FALLTHRU */
15161 
15162 		case TRAN_BADPKT:
15163 		default:
15164 			un->un_ncmds_in_transport--;
15165 			ASSERT(un->un_ncmds_in_transport >= 0);
15166 
15167 			/*
15168 			 * If this is our REQUEST SENSE command with a
15169 			 * transport error, we must get back the pointers
15170 			 * to the original buf, and mark the REQUEST
15171 			 * SENSE command as "available".
15172 			 */
15173 			if (bp == un->un_rqs_bp) {
15174 				bp = sd_mark_rqs_idle(un, xp);
15175 				xp = SD_GET_XBUF(bp);
15176 			} else {
15177 				/*
15178 				 * Legacy behavior: do not update transport
15179 				 * error count for request sense commands.
15180 				 */
15181 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
15182 			}
15183 
15184 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15185 			sd_print_transport_rejected_message(un, xp, rval);
15186 
15187 			/*
15188 			 * This command will be terminated by SD driver due
15189 			 * to a fatal transport error. We should post
15190 			 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15191 			 * of "fail" for any command to indicate this
15192 			 * situation.
15193 			 */
15194 			if (xp->xb_ena > 0) {
15195 				ASSERT(un->un_fm_private != NULL);
15196 				sfip = un->un_fm_private;
15197 				sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15198 				sd_ssc_extract_info(&sfip->fm_ssc, un,
15199 				    xp->xb_pktp, bp, xp);
15200 				sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15201 			}
15202 
15203 			/*
15204 			 * We must use sd_return_failed_command_no_restart() to
15205 			 * avoid a recursive call back into sd_start_cmds().
15206 			 * However this also means that we must keep processing
15207 			 * the waitq here in order to avoid stalling.
15208 			 */
15209 			sd_return_failed_command_no_restart(un, bp, EIO);
15210 
15211 			/*
15212 			 * Notify any threads waiting in sd_ddi_suspend() that
15213 			 * a command completion has occurred.
15214 			 */
15215 			if (un->un_state == SD_STATE_SUSPENDED) {
15216 				cv_broadcast(&un->un_disk_busy_cv);
15217 			}
15218 
15219 			if (bp == immed_bp) {
15220 				/* immed_bp is gone by now, so clear this */
15221 				immed_bp = NULL;
15222 			}
15223 			break;
15224 		}
15225 
15226 	} while (immed_bp == NULL);
15227 
15228 exit:
15229 	ASSERT(mutex_owned(SD_MUTEX(un)));
15230 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15231 }
15232 
15233 
15234 /*
15235  *    Function: sd_return_command
15236  *
15237  * Description: Returns a command to its originator (with or without an
15238  *		error).  Also starts commands waiting to be transported
15239  *		to the target.
15240  *
15241  *     Context: May be called from interrupt, kernel, or timeout context
15242  */
15243 
15244 static void
15245 sd_return_command(struct sd_lun *un, struct buf *bp)
15246 {
15247 	struct sd_xbuf *xp;
15248 	struct scsi_pkt *pktp;
15249 	struct sd_fm_internal *sfip;
15250 
15251 	ASSERT(bp != NULL);
15252 	ASSERT(un != NULL);
15253 	ASSERT(mutex_owned(SD_MUTEX(un)));
15254 	ASSERT(bp != un->un_rqs_bp);
15255 	xp = SD_GET_XBUF(bp);
15256 	ASSERT(xp != NULL);
15257 
15258 	pktp = SD_GET_PKTP(bp);
15259 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15260 	ASSERT(sfip != NULL);
15261 
15262 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15263 
15264 	/*
15265 	 * Note: check for the "sdrestart failed" case.
15266 	 */
15267 	if ((un->un_partial_dma_supported == 1) &&
15268 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15269 	    (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15270 	    (xp->xb_pktp->pkt_resid == 0)) {
15271 
15272 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15273 			/*
15274 			 * Successfully set up next portion of cmd
15275 			 * transfer, try sending it
15276 			 */
15277 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15278 			    NULL, NULL, 0, (clock_t)0, NULL);
15279 			sd_start_cmds(un, NULL);
15280 			return;	/* Note:x86: need a return here? */
15281 		}
15282 	}
15283 
15284 	/*
15285 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15286 	 * can happen if upon being re-tried the failfast bp either
15287 	 * succeeded or encountered another error (possibly even a different
15288 	 * error than the one that precipitated the failfast state, but in
15289 	 * that case it would have had to exhaust retries as well). Regardless,
15290 	 * this should not occur whenever the instance is in the active
15291 	 * failfast state.
15292 	 */
15293 	if (bp == un->un_failfast_bp) {
15294 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15295 		un->un_failfast_bp = NULL;
15296 	}
15297 
15298 	/*
15299 	 * Clear the failfast state upon successful completion of ANY cmd.
15300 	 */
15301 	if (bp->b_error == 0) {
15302 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15303 		/*
15304 		 * If this is a successful command, but used to be retried,
15305 		 * we will take it as a recovered command and post an
15306 		 * ereport with driver-assessment of "recovered".
15307 		 */
15308 		if (xp->xb_ena > 0) {
15309 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15310 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15311 		}
15312 	} else {
15313 		/*
15314 		 * If this is a failed non-USCSI command we will post an
15315 		 * ereport with driver-assessment set accordingly("fail" or
15316 		 * "fatal").
15317 		 */
15318 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15319 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15320 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15321 		}
15322 	}
15323 
15324 	/*
15325 	 * This is used if the command was retried one or more times. Show that
15326 	 * we are done with it, and allow processing of the waitq to resume.
15327 	 */
15328 	if (bp == un->un_retry_bp) {
15329 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15330 		    "sd_return_command: un:0x%p: "
15331 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15332 		un->un_retry_bp = NULL;
15333 		un->un_retry_statp = NULL;
15334 	}
15335 
15336 	SD_UPDATE_RDWR_STATS(un, bp);
15337 	SD_UPDATE_PARTITION_STATS(un, bp);
15338 
15339 	switch (un->un_state) {
15340 	case SD_STATE_SUSPENDED:
15341 		/*
15342 		 * Notify any threads waiting in sd_ddi_suspend() that
15343 		 * a command completion has occurred.
15344 		 */
15345 		cv_broadcast(&un->un_disk_busy_cv);
15346 		break;
15347 	default:
15348 		sd_start_cmds(un, NULL);
15349 		break;
15350 	}
15351 
15352 	/* Return this command up the iodone chain to its originator. */
15353 	mutex_exit(SD_MUTEX(un));
15354 
15355 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15356 	xp->xb_pktp = NULL;
15357 
15358 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15359 
15360 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15361 	mutex_enter(SD_MUTEX(un));
15362 
15363 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15364 }
15365 
15366 
15367 /*
15368  *    Function: sd_return_failed_command
15369  *
15370  * Description: Command completion when an error occurred.
15371  *
15372  *     Context: May be called from interrupt context
15373  */
15374 
15375 static void
15376 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15377 {
15378 	ASSERT(bp != NULL);
15379 	ASSERT(un != NULL);
15380 	ASSERT(mutex_owned(SD_MUTEX(un)));
15381 
15382 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15383 	    "sd_return_failed_command: entry\n");
15384 
15385 	/*
15386 	 * b_resid could already be nonzero due to a partial data
15387 	 * transfer, so do not change it here.
15388 	 */
15389 	SD_BIOERROR(bp, errcode);
15390 
15391 	sd_return_command(un, bp);
15392 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15393 	    "sd_return_failed_command: exit\n");
15394 }
15395 
15396 
15397 /*
15398  *    Function: sd_return_failed_command_no_restart
15399  *
15400  * Description: Same as sd_return_failed_command, but ensures that no
15401  *		call back into sd_start_cmds will be issued.
15402  *
15403  *     Context: May be called from interrupt context
15404  */
15405 
15406 static void
15407 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15408 	int errcode)
15409 {
15410 	struct sd_xbuf *xp;
15411 
15412 	ASSERT(bp != NULL);
15413 	ASSERT(un != NULL);
15414 	ASSERT(mutex_owned(SD_MUTEX(un)));
15415 	xp = SD_GET_XBUF(bp);
15416 	ASSERT(xp != NULL);
15417 	ASSERT(errcode != 0);
15418 
15419 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15420 	    "sd_return_failed_command_no_restart: entry\n");
15421 
15422 	/*
15423 	 * b_resid could already be nonzero due to a partial data
15424 	 * transfer, so do not change it here.
15425 	 */
15426 	SD_BIOERROR(bp, errcode);
15427 
15428 	/*
15429 	 * If this is the failfast bp, clear it. This can happen if the
15430 	 * failfast bp encounterd a fatal error when we attempted to
15431 	 * re-try it (such as a scsi_transport(9F) failure).  However
15432 	 * we should NOT be in an active failfast state if the failfast
15433 	 * bp is not NULL.
15434 	 */
15435 	if (bp == un->un_failfast_bp) {
15436 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15437 		un->un_failfast_bp = NULL;
15438 	}
15439 
15440 	if (bp == un->un_retry_bp) {
15441 		/*
15442 		 * This command was retried one or more times. Show that we are
15443 		 * done with it, and allow processing of the waitq to resume.
15444 		 */
15445 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15446 		    "sd_return_failed_command_no_restart: "
15447 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15448 		un->un_retry_bp = NULL;
15449 		un->un_retry_statp = NULL;
15450 	}
15451 
15452 	SD_UPDATE_RDWR_STATS(un, bp);
15453 	SD_UPDATE_PARTITION_STATS(un, bp);
15454 
15455 	mutex_exit(SD_MUTEX(un));
15456 
15457 	if (xp->xb_pktp != NULL) {
15458 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15459 		xp->xb_pktp = NULL;
15460 	}
15461 
15462 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15463 
15464 	mutex_enter(SD_MUTEX(un));
15465 
15466 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15467 	    "sd_return_failed_command_no_restart: exit\n");
15468 }
15469 
15470 
15471 /*
15472  *    Function: sd_retry_command
15473  *
15474  * Description: queue up a command for retry, or (optionally) fail it
15475  *		if retry counts are exhausted.
15476  *
15477  *   Arguments: un - Pointer to the sd_lun struct for the target.
15478  *
15479  *		bp - Pointer to the buf for the command to be retried.
15480  *
15481  *		retry_check_flag - Flag to see which (if any) of the retry
15482  *		   counts should be decremented/checked. If the indicated
15483  *		   retry count is exhausted, then the command will not be
15484  *		   retried; it will be failed instead. This should use a
15485  *		   value equal to one of the following:
15486  *
15487  *			SD_RETRIES_NOCHECK
15488  *			SD_RESD_RETRIES_STANDARD
15489  *			SD_RETRIES_VICTIM
15490  *
15491  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15492  *		   if the check should be made to see of FLAG_ISOLATE is set
15493  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15494  *		   not retried, it is simply failed.
15495  *
15496  *		user_funcp - Ptr to function to call before dispatching the
15497  *		   command. May be NULL if no action needs to be performed.
15498  *		   (Primarily intended for printing messages.)
15499  *
15500  *		user_arg - Optional argument to be passed along to
15501  *		   the user_funcp call.
15502  *
15503  *		failure_code - errno return code to set in the bp if the
15504  *		   command is going to be failed.
15505  *
15506  *		retry_delay - Retry delay interval in (clock_t) units. May
15507  *		   be zero which indicates that the retry should be retried
15508  *		   immediately (ie, without an intervening delay).
15509  *
15510  *		statp - Ptr to kstat function to be updated if the command
15511  *		   is queued for a delayed retry. May be NULL if no kstat
15512  *		   update is desired.
15513  *
15514  *     Context: May be called from interrupt context.
15515  */
15516 
15517 static void
15518 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15519 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15520 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15521 	void (*statp)(kstat_io_t *))
15522 {
15523 	struct sd_xbuf	*xp;
15524 	struct scsi_pkt	*pktp;
15525 	struct sd_fm_internal *sfip;
15526 
15527 	ASSERT(un != NULL);
15528 	ASSERT(mutex_owned(SD_MUTEX(un)));
15529 	ASSERT(bp != NULL);
15530 	xp = SD_GET_XBUF(bp);
15531 	ASSERT(xp != NULL);
15532 	pktp = SD_GET_PKTP(bp);
15533 	ASSERT(pktp != NULL);
15534 
15535 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15536 	ASSERT(sfip != NULL);
15537 
15538 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15539 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15540 
15541 	/*
15542 	 * If we are syncing or dumping, fail the command to avoid
15543 	 * recursively calling back into scsi_transport().
15544 	 */
15545 	if (ddi_in_panic()) {
15546 		goto fail_command_no_log;
15547 	}
15548 
15549 	/*
15550 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15551 	 * log an error and fail the command.
15552 	 */
15553 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15554 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15555 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15556 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15557 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15558 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15559 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15560 		goto fail_command;
15561 	}
15562 
15563 	/*
15564 	 * If we are suspended, then put the command onto head of the
15565 	 * wait queue since we don't want to start more commands, and
15566 	 * clear the un_retry_bp. Next time when we are resumed, will
15567 	 * handle the command in the wait queue.
15568 	 */
15569 	switch (un->un_state) {
15570 	case SD_STATE_SUSPENDED:
15571 	case SD_STATE_DUMPING:
15572 		bp->av_forw = un->un_waitq_headp;
15573 		un->un_waitq_headp = bp;
15574 		if (un->un_waitq_tailp == NULL) {
15575 			un->un_waitq_tailp = bp;
15576 		}
15577 		if (bp == un->un_retry_bp) {
15578 			un->un_retry_bp = NULL;
15579 			un->un_retry_statp = NULL;
15580 		}
15581 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15582 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15583 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15584 		return;
15585 	default:
15586 		break;
15587 	}
15588 
15589 	/*
15590 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15591 	 * is set; if it is then we do not want to retry the command.
15592 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15593 	 */
15594 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15595 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15596 			goto fail_command;
15597 		}
15598 	}
15599 
15600 
15601 	/*
15602 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15603 	 * command timeout or a selection timeout has occurred. This means
15604 	 * that we were unable to establish an kind of communication with
15605 	 * the target, and subsequent retries and/or commands are likely
15606 	 * to encounter similar results and take a long time to complete.
15607 	 *
15608 	 * If this is a failfast error condition, we need to update the
15609 	 * failfast state, even if this bp does not have B_FAILFAST set.
15610 	 */
15611 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15612 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15613 			ASSERT(un->un_failfast_bp == NULL);
15614 			/*
15615 			 * If we are already in the active failfast state, and
15616 			 * another failfast error condition has been detected,
15617 			 * then fail this command if it has B_FAILFAST set.
15618 			 * If B_FAILFAST is clear, then maintain the legacy
15619 			 * behavior of retrying heroically, even tho this will
15620 			 * take a lot more time to fail the command.
15621 			 */
15622 			if (bp->b_flags & B_FAILFAST) {
15623 				goto fail_command;
15624 			}
15625 		} else {
15626 			/*
15627 			 * We're not in the active failfast state, but we
15628 			 * have a failfast error condition, so we must begin
15629 			 * transition to the next state. We do this regardless
15630 			 * of whether or not this bp has B_FAILFAST set.
15631 			 */
15632 			if (un->un_failfast_bp == NULL) {
15633 				/*
15634 				 * This is the first bp to meet a failfast
15635 				 * condition so save it on un_failfast_bp &
15636 				 * do normal retry processing. Do not enter
15637 				 * active failfast state yet. This marks
15638 				 * entry into the "failfast pending" state.
15639 				 */
15640 				un->un_failfast_bp = bp;
15641 
15642 			} else if (un->un_failfast_bp == bp) {
15643 				/*
15644 				 * This is the second time *this* bp has
15645 				 * encountered a failfast error condition,
15646 				 * so enter active failfast state & flush
15647 				 * queues as appropriate.
15648 				 */
15649 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15650 				un->un_failfast_bp = NULL;
15651 				sd_failfast_flushq(un);
15652 
15653 				/*
15654 				 * Fail this bp now if B_FAILFAST set;
15655 				 * otherwise continue with retries. (It would
15656 				 * be pretty ironic if this bp succeeded on a
15657 				 * subsequent retry after we just flushed all
15658 				 * the queues).
15659 				 */
15660 				if (bp->b_flags & B_FAILFAST) {
15661 					goto fail_command;
15662 				}
15663 
15664 #if !defined(lint) && !defined(__lint)
15665 			} else {
15666 				/*
15667 				 * If neither of the preceeding conditionals
15668 				 * was true, it means that there is some
15669 				 * *other* bp that has met an inital failfast
15670 				 * condition and is currently either being
15671 				 * retried or is waiting to be retried. In
15672 				 * that case we should perform normal retry
15673 				 * processing on *this* bp, since there is a
15674 				 * chance that the current failfast condition
15675 				 * is transient and recoverable. If that does
15676 				 * not turn out to be the case, then retries
15677 				 * will be cleared when the wait queue is
15678 				 * flushed anyway.
15679 				 */
15680 #endif
15681 			}
15682 		}
15683 	} else {
15684 		/*
15685 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15686 		 * likely were able to at least establish some level of
15687 		 * communication with the target and subsequent commands
15688 		 * and/or retries are likely to get through to the target,
15689 		 * In this case we want to be aggressive about clearing
15690 		 * the failfast state. Note that this does not affect
15691 		 * the "failfast pending" condition.
15692 		 */
15693 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15694 	}
15695 
15696 
15697 	/*
15698 	 * Check the specified retry count to see if we can still do
15699 	 * any retries with this pkt before we should fail it.
15700 	 */
15701 	switch (retry_check_flag & SD_RETRIES_MASK) {
15702 	case SD_RETRIES_VICTIM:
15703 		/*
15704 		 * Check the victim retry count. If exhausted, then fall
15705 		 * thru & check against the standard retry count.
15706 		 */
15707 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15708 			/* Increment count & proceed with the retry */
15709 			xp->xb_victim_retry_count++;
15710 			break;
15711 		}
15712 		/* Victim retries exhausted, fall back to std. retries... */
15713 		/* FALLTHRU */
15714 
15715 	case SD_RETRIES_STANDARD:
15716 		if (xp->xb_retry_count >= un->un_retry_count) {
15717 			/* Retries exhausted, fail the command */
15718 			SD_TRACE(SD_LOG_IO_CORE, un,
15719 			    "sd_retry_command: retries exhausted!\n");
15720 			/*
15721 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15722 			 * commands with nonzero pkt_resid.
15723 			 */
15724 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15725 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15726 			    (pktp->pkt_resid != 0)) {
15727 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15728 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15729 					SD_UPDATE_B_RESID(bp, pktp);
15730 				}
15731 			}
15732 			goto fail_command;
15733 		}
15734 		xp->xb_retry_count++;
15735 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15736 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15737 		break;
15738 
15739 	case SD_RETRIES_UA:
15740 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15741 			/* Retries exhausted, fail the command */
15742 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15743 			    "Unit Attention retries exhausted. "
15744 			    "Check the target.\n");
15745 			goto fail_command;
15746 		}
15747 		xp->xb_ua_retry_count++;
15748 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15749 		    "sd_retry_command: retry count:%d\n",
15750 		    xp->xb_ua_retry_count);
15751 		break;
15752 
15753 	case SD_RETRIES_BUSY:
15754 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15755 			/* Retries exhausted, fail the command */
15756 			SD_TRACE(SD_LOG_IO_CORE, un,
15757 			    "sd_retry_command: retries exhausted!\n");
15758 			goto fail_command;
15759 		}
15760 		xp->xb_retry_count++;
15761 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15762 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15763 		break;
15764 
15765 	case SD_RETRIES_NOCHECK:
15766 	default:
15767 		/* No retry count to check. Just proceed with the retry */
15768 		break;
15769 	}
15770 
15771 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15772 
15773 	/*
15774 	 * If this is a non-USCSI command being retried
15775 	 * during execution last time, we should post an ereport with
15776 	 * driver-assessment of the value "retry".
15777 	 * For partial DMA, request sense and STATUS_QFULL, there are no
15778 	 * hardware errors, we bypass ereport posting.
15779 	 */
15780 	if (failure_code != 0) {
15781 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15782 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15783 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15784 		}
15785 	}
15786 
15787 	/*
15788 	 * If we were given a zero timeout, we must attempt to retry the
15789 	 * command immediately (ie, without a delay).
15790 	 */
15791 	if (retry_delay == 0) {
15792 		/*
15793 		 * Check some limiting conditions to see if we can actually
15794 		 * do the immediate retry.  If we cannot, then we must
15795 		 * fall back to queueing up a delayed retry.
15796 		 */
15797 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15798 			/*
15799 			 * We are at the throttle limit for the target,
15800 			 * fall back to delayed retry.
15801 			 */
15802 			retry_delay = un->un_busy_timeout;
15803 			statp = kstat_waitq_enter;
15804 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15805 			    "sd_retry_command: immed. retry hit "
15806 			    "throttle!\n");
15807 		} else {
15808 			/*
15809 			 * We're clear to proceed with the immediate retry.
15810 			 * First call the user-provided function (if any)
15811 			 */
15812 			if (user_funcp != NULL) {
15813 				(*user_funcp)(un, bp, user_arg,
15814 				    SD_IMMEDIATE_RETRY_ISSUED);
15815 #ifdef __lock_lint
15816 				sd_print_incomplete_msg(un, bp, user_arg,
15817 				    SD_IMMEDIATE_RETRY_ISSUED);
15818 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15819 				    SD_IMMEDIATE_RETRY_ISSUED);
15820 				sd_print_sense_failed_msg(un, bp, user_arg,
15821 				    SD_IMMEDIATE_RETRY_ISSUED);
15822 #endif
15823 			}
15824 
15825 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15826 			    "sd_retry_command: issuing immediate retry\n");
15827 
15828 			/*
15829 			 * Call sd_start_cmds() to transport the command to
15830 			 * the target.
15831 			 */
15832 			sd_start_cmds(un, bp);
15833 
15834 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15835 			    "sd_retry_command exit\n");
15836 			return;
15837 		}
15838 	}
15839 
15840 	/*
15841 	 * Set up to retry the command after a delay.
15842 	 * First call the user-provided function (if any)
15843 	 */
15844 	if (user_funcp != NULL) {
15845 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15846 	}
15847 
15848 	sd_set_retry_bp(un, bp, retry_delay, statp);
15849 
15850 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15851 	return;
15852 
15853 fail_command:
15854 
15855 	if (user_funcp != NULL) {
15856 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15857 	}
15858 
15859 fail_command_no_log:
15860 
15861 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15862 	    "sd_retry_command: returning failed command\n");
15863 
15864 	sd_return_failed_command(un, bp, failure_code);
15865 
15866 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15867 }
15868 
15869 
15870 /*
15871  *    Function: sd_set_retry_bp
15872  *
15873  * Description: Set up the given bp for retry.
15874  *
15875  *   Arguments: un - ptr to associated softstate
15876  *		bp - ptr to buf(9S) for the command
15877  *		retry_delay - time interval before issuing retry (may be 0)
15878  *		statp - optional pointer to kstat function
15879  *
15880  *     Context: May be called under interrupt context
15881  */
15882 
15883 static void
15884 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15885 	void (*statp)(kstat_io_t *))
15886 {
15887 	ASSERT(un != NULL);
15888 	ASSERT(mutex_owned(SD_MUTEX(un)));
15889 	ASSERT(bp != NULL);
15890 
15891 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15892 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15893 
15894 	/*
15895 	 * Indicate that the command is being retried. This will not allow any
15896 	 * other commands on the wait queue to be transported to the target
15897 	 * until this command has been completed (success or failure). The
15898 	 * "retry command" is not transported to the target until the given
15899 	 * time delay expires, unless the user specified a 0 retry_delay.
15900 	 *
15901 	 * Note: the timeout(9F) callback routine is what actually calls
15902 	 * sd_start_cmds() to transport the command, with the exception of a
15903 	 * zero retry_delay. The only current implementor of a zero retry delay
15904 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15905 	 */
15906 	if (un->un_retry_bp == NULL) {
15907 		ASSERT(un->un_retry_statp == NULL);
15908 		un->un_retry_bp = bp;
15909 
15910 		/*
15911 		 * If the user has not specified a delay the command should
15912 		 * be queued and no timeout should be scheduled.
15913 		 */
15914 		if (retry_delay == 0) {
15915 			/*
15916 			 * Save the kstat pointer that will be used in the
15917 			 * call to SD_UPDATE_KSTATS() below, so that
15918 			 * sd_start_cmds() can correctly decrement the waitq
15919 			 * count when it is time to transport this command.
15920 			 */
15921 			un->un_retry_statp = statp;
15922 			goto done;
15923 		}
15924 	}
15925 
15926 	if (un->un_retry_bp == bp) {
15927 		/*
15928 		 * Save the kstat pointer that will be used in the call to
15929 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15930 		 * correctly decrement the waitq count when it is time to
15931 		 * transport this command.
15932 		 */
15933 		un->un_retry_statp = statp;
15934 
15935 		/*
15936 		 * Schedule a timeout if:
15937 		 *   1) The user has specified a delay.
15938 		 *   2) There is not a START_STOP_UNIT callback pending.
15939 		 *
15940 		 * If no delay has been specified, then it is up to the caller
15941 		 * to ensure that IO processing continues without stalling.
15942 		 * Effectively, this means that the caller will issue the
15943 		 * required call to sd_start_cmds(). The START_STOP_UNIT
15944 		 * callback does this after the START STOP UNIT command has
15945 		 * completed. In either of these cases we should not schedule
15946 		 * a timeout callback here.  Also don't schedule the timeout if
15947 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15948 		 */
15949 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15950 		    (un->un_direct_priority_timeid == NULL)) {
15951 			un->un_retry_timeid =
15952 			    timeout(sd_start_retry_command, un, retry_delay);
15953 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15954 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
15955 			    " bp:0x%p un_retry_timeid:0x%p\n",
15956 			    un, bp, un->un_retry_timeid);
15957 		}
15958 	} else {
15959 		/*
15960 		 * We only get in here if there is already another command
15961 		 * waiting to be retried.  In this case, we just put the
15962 		 * given command onto the wait queue, so it can be transported
15963 		 * after the current retry command has completed.
15964 		 *
15965 		 * Also we have to make sure that if the command at the head
15966 		 * of the wait queue is the un_failfast_bp, that we do not
15967 		 * put ahead of it any other commands that are to be retried.
15968 		 */
15969 		if ((un->un_failfast_bp != NULL) &&
15970 		    (un->un_failfast_bp == un->un_waitq_headp)) {
15971 			/*
15972 			 * Enqueue this command AFTER the first command on
15973 			 * the wait queue (which is also un_failfast_bp).
15974 			 */
15975 			bp->av_forw = un->un_waitq_headp->av_forw;
15976 			un->un_waitq_headp->av_forw = bp;
15977 			if (un->un_waitq_headp == un->un_waitq_tailp) {
15978 				un->un_waitq_tailp = bp;
15979 			}
15980 		} else {
15981 			/* Enqueue this command at the head of the waitq. */
15982 			bp->av_forw = un->un_waitq_headp;
15983 			un->un_waitq_headp = bp;
15984 			if (un->un_waitq_tailp == NULL) {
15985 				un->un_waitq_tailp = bp;
15986 			}
15987 		}
15988 
15989 		if (statp == NULL) {
15990 			statp = kstat_waitq_enter;
15991 		}
15992 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15993 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15994 	}
15995 
15996 done:
15997 	if (statp != NULL) {
15998 		SD_UPDATE_KSTATS(un, statp, bp);
15999 	}
16000 
16001 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16002 	    "sd_set_retry_bp: exit un:0x%p\n", un);
16003 }
16004 
16005 
16006 /*
16007  *    Function: sd_start_retry_command
16008  *
16009  * Description: Start the command that has been waiting on the target's
16010  *		retry queue.  Called from timeout(9F) context after the
16011  *		retry delay interval has expired.
16012  *
16013  *   Arguments: arg - pointer to associated softstate for the device.
16014  *
16015  *     Context: timeout(9F) thread context.  May not sleep.
16016  */
16017 
16018 static void
16019 sd_start_retry_command(void *arg)
16020 {
16021 	struct sd_lun *un = arg;
16022 
16023 	ASSERT(un != NULL);
16024 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16025 
16026 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16027 	    "sd_start_retry_command: entry\n");
16028 
16029 	mutex_enter(SD_MUTEX(un));
16030 
16031 	un->un_retry_timeid = NULL;
16032 
16033 	if (un->un_retry_bp != NULL) {
16034 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16035 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16036 		    un, un->un_retry_bp);
16037 		sd_start_cmds(un, un->un_retry_bp);
16038 	}
16039 
16040 	mutex_exit(SD_MUTEX(un));
16041 
16042 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16043 	    "sd_start_retry_command: exit\n");
16044 }
16045 
16046 /*
16047  *    Function: sd_rmw_msg_print_handler
16048  *
16049  * Description: If RMW mode is enabled and warning message is triggered
16050  *              print I/O count during a fixed interval.
16051  *
16052  *   Arguments: arg - pointer to associated softstate for the device.
16053  *
16054  *     Context: timeout(9F) thread context. May not sleep.
16055  */
16056 static void
16057 sd_rmw_msg_print_handler(void *arg)
16058 {
16059 	struct sd_lun *un = arg;
16060 
16061 	ASSERT(un != NULL);
16062 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16063 
16064 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16065 	    "sd_rmw_msg_print_handler: entry\n");
16066 
16067 	mutex_enter(SD_MUTEX(un));
16068 
16069 	if (un->un_rmw_incre_count > 0) {
16070 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16071 		    "%"PRIu64" I/O requests are not aligned with %d disk "
16072 		    "sector size in %ld seconds. They are handled through "
16073 		    "Read Modify Write but the performance is very low!\n",
16074 		    un->un_rmw_incre_count, un->un_tgt_blocksize,
16075 		    drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16076 		un->un_rmw_incre_count = 0;
16077 		un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16078 		    un, SD_RMW_MSG_PRINT_TIMEOUT);
16079 	} else {
16080 		un->un_rmw_msg_timeid = NULL;
16081 	}
16082 
16083 	mutex_exit(SD_MUTEX(un));
16084 
16085 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16086 	    "sd_rmw_msg_print_handler: exit\n");
16087 }
16088 
16089 /*
16090  *    Function: sd_start_direct_priority_command
16091  *
16092  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16093  *		received TRAN_BUSY when we called scsi_transport() to send it
16094  *		to the underlying HBA. This function is called from timeout(9F)
16095  *		context after the delay interval has expired.
16096  *
16097  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16098  *
16099  *     Context: timeout(9F) thread context.  May not sleep.
16100  */
16101 
16102 static void
16103 sd_start_direct_priority_command(void *arg)
16104 {
16105 	struct buf	*priority_bp = arg;
16106 	struct sd_lun	*un;
16107 
16108 	ASSERT(priority_bp != NULL);
16109 	un = SD_GET_UN(priority_bp);
16110 	ASSERT(un != NULL);
16111 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16112 
16113 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16114 	    "sd_start_direct_priority_command: entry\n");
16115 
16116 	mutex_enter(SD_MUTEX(un));
16117 	un->un_direct_priority_timeid = NULL;
16118 	sd_start_cmds(un, priority_bp);
16119 	mutex_exit(SD_MUTEX(un));
16120 
16121 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16122 	    "sd_start_direct_priority_command: exit\n");
16123 }
16124 
16125 
16126 /*
16127  *    Function: sd_send_request_sense_command
16128  *
16129  * Description: Sends a REQUEST SENSE command to the target
16130  *
16131  *     Context: May be called from interrupt context.
16132  */
16133 
16134 static void
16135 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16136 	struct scsi_pkt *pktp)
16137 {
16138 	ASSERT(bp != NULL);
16139 	ASSERT(un != NULL);
16140 	ASSERT(mutex_owned(SD_MUTEX(un)));
16141 
16142 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16143 	    "entry: buf:0x%p\n", bp);
16144 
16145 	/*
16146 	 * If we are syncing or dumping, then fail the command to avoid a
16147 	 * recursive callback into scsi_transport(). Also fail the command
16148 	 * if we are suspended (legacy behavior).
16149 	 */
16150 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16151 	    (un->un_state == SD_STATE_DUMPING)) {
16152 		sd_return_failed_command(un, bp, EIO);
16153 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16154 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
16155 		return;
16156 	}
16157 
16158 	/*
16159 	 * Retry the failed command and don't issue the request sense if:
16160 	 *    1) the sense buf is busy
16161 	 *    2) we have 1 or more outstanding commands on the target
16162 	 *    (the sense data will be cleared or invalidated any way)
16163 	 *
16164 	 * Note: There could be an issue with not checking a retry limit here,
16165 	 * the problem is determining which retry limit to check.
16166 	 */
16167 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16168 		/* Don't retry if the command is flagged as non-retryable */
16169 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16170 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16171 			    NULL, NULL, 0, un->un_busy_timeout,
16172 			    kstat_waitq_enter);
16173 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16174 			    "sd_send_request_sense_command: "
16175 			    "at full throttle, retrying exit\n");
16176 		} else {
16177 			sd_return_failed_command(un, bp, EIO);
16178 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16179 			    "sd_send_request_sense_command: "
16180 			    "at full throttle, non-retryable exit\n");
16181 		}
16182 		return;
16183 	}
16184 
16185 	sd_mark_rqs_busy(un, bp);
16186 	sd_start_cmds(un, un->un_rqs_bp);
16187 
16188 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16189 	    "sd_send_request_sense_command: exit\n");
16190 }
16191 
16192 
16193 /*
16194  *    Function: sd_mark_rqs_busy
16195  *
16196  * Description: Indicate that the request sense bp for this instance is
16197  *		in use.
16198  *
16199  *     Context: May be called under interrupt context
16200  */
16201 
16202 static void
16203 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16204 {
16205 	struct sd_xbuf	*sense_xp;
16206 
16207 	ASSERT(un != NULL);
16208 	ASSERT(bp != NULL);
16209 	ASSERT(mutex_owned(SD_MUTEX(un)));
16210 	ASSERT(un->un_sense_isbusy == 0);
16211 
16212 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16213 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16214 
16215 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16216 	ASSERT(sense_xp != NULL);
16217 
16218 	SD_INFO(SD_LOG_IO, un,
16219 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16220 
16221 	ASSERT(sense_xp->xb_pktp != NULL);
16222 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16223 	    == (FLAG_SENSING | FLAG_HEAD));
16224 
16225 	un->un_sense_isbusy = 1;
16226 	un->un_rqs_bp->b_resid = 0;
16227 	sense_xp->xb_pktp->pkt_resid  = 0;
16228 	sense_xp->xb_pktp->pkt_reason = 0;
16229 
16230 	/* So we can get back the bp at interrupt time! */
16231 	sense_xp->xb_sense_bp = bp;
16232 
16233 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16234 
16235 	/*
16236 	 * Mark this buf as awaiting sense data. (This is already set in
16237 	 * the pkt_flags for the RQS packet.)
16238 	 */
16239 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16240 
16241 	/* Request sense down same path */
16242 	if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16243 	    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16244 		sense_xp->xb_pktp->pkt_path_instance =
16245 		    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16246 
16247 	sense_xp->xb_retry_count	= 0;
16248 	sense_xp->xb_victim_retry_count = 0;
16249 	sense_xp->xb_ua_retry_count	= 0;
16250 	sense_xp->xb_nr_retry_count 	= 0;
16251 	sense_xp->xb_dma_resid  = 0;
16252 
16253 	/* Clean up the fields for auto-request sense */
16254 	sense_xp->xb_sense_status = 0;
16255 	sense_xp->xb_sense_state  = 0;
16256 	sense_xp->xb_sense_resid  = 0;
16257 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16258 
16259 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16260 }
16261 
16262 
16263 /*
16264  *    Function: sd_mark_rqs_idle
16265  *
16266  * Description: SD_MUTEX must be held continuously through this routine
16267  *		to prevent reuse of the rqs struct before the caller can
16268  *		complete it's processing.
16269  *
16270  * Return Code: Pointer to the RQS buf
16271  *
16272  *     Context: May be called under interrupt context
16273  */
16274 
16275 static struct buf *
16276 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16277 {
16278 	struct buf *bp;
16279 	ASSERT(un != NULL);
16280 	ASSERT(sense_xp != NULL);
16281 	ASSERT(mutex_owned(SD_MUTEX(un)));
16282 	ASSERT(un->un_sense_isbusy != 0);
16283 
16284 	un->un_sense_isbusy = 0;
16285 	bp = sense_xp->xb_sense_bp;
16286 	sense_xp->xb_sense_bp = NULL;
16287 
16288 	/* This pkt is no longer interested in getting sense data */
16289 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16290 
16291 	return (bp);
16292 }
16293 
16294 
16295 
16296 /*
16297  *    Function: sd_alloc_rqs
16298  *
16299  * Description: Set up the unit to receive auto request sense data
16300  *
16301  * Return Code: DDI_SUCCESS or DDI_FAILURE
16302  *
16303  *     Context: Called under attach(9E) context
16304  */
16305 
16306 static int
16307 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16308 {
16309 	struct sd_xbuf *xp;
16310 
16311 	ASSERT(un != NULL);
16312 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16313 	ASSERT(un->un_rqs_bp == NULL);
16314 	ASSERT(un->un_rqs_pktp == NULL);
16315 
16316 	/*
16317 	 * First allocate the required buf and scsi_pkt structs, then set up
16318 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16319 	 */
16320 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16321 	    MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16322 	if (un->un_rqs_bp == NULL) {
16323 		return (DDI_FAILURE);
16324 	}
16325 
16326 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16327 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16328 
16329 	if (un->un_rqs_pktp == NULL) {
16330 		sd_free_rqs(un);
16331 		return (DDI_FAILURE);
16332 	}
16333 
16334 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16335 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16336 	    SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16337 
16338 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16339 
16340 	/* Set up the other needed members in the ARQ scsi_pkt. */
16341 	un->un_rqs_pktp->pkt_comp   = sdintr;
16342 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16343 	un->un_rqs_pktp->pkt_flags |=
16344 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16345 
16346 	/*
16347 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16348 	 * provide any intpkt, destroypkt routines as we take care of
16349 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16350 	 */
16351 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16352 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16353 	xp->xb_pktp = un->un_rqs_pktp;
16354 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16355 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16356 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16357 
16358 	/*
16359 	 * Save the pointer to the request sense private bp so it can
16360 	 * be retrieved in sdintr.
16361 	 */
16362 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16363 	ASSERT(un->un_rqs_bp->b_private == xp);
16364 
16365 	/*
16366 	 * See if the HBA supports auto-request sense for the specified
16367 	 * target/lun. If it does, then try to enable it (if not already
16368 	 * enabled).
16369 	 *
16370 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16371 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16372 	 * return success.  However, in both of these cases ARQ is always
16373 	 * enabled and scsi_ifgetcap will always return true. The best approach
16374 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16375 	 *
16376 	 * The 3rd case is the HBA (adp) always return enabled on
16377 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16378 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16379 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16380 	 */
16381 
16382 	if (un->un_f_is_fibre == TRUE) {
16383 		un->un_f_arq_enabled = TRUE;
16384 	} else {
16385 #if defined(__i386) || defined(__amd64)
16386 		/*
16387 		 * Circumvent the Adaptec bug, remove this code when
16388 		 * the bug is fixed
16389 		 */
16390 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16391 #endif
16392 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16393 		case 0:
16394 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16395 			    "sd_alloc_rqs: HBA supports ARQ\n");
16396 			/*
16397 			 * ARQ is supported by this HBA but currently is not
16398 			 * enabled. Attempt to enable it and if successful then
16399 			 * mark this instance as ARQ enabled.
16400 			 */
16401 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16402 			    == 1) {
16403 				/* Successfully enabled ARQ in the HBA */
16404 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16405 				    "sd_alloc_rqs: ARQ enabled\n");
16406 				un->un_f_arq_enabled = TRUE;
16407 			} else {
16408 				/* Could not enable ARQ in the HBA */
16409 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16410 				    "sd_alloc_rqs: failed ARQ enable\n");
16411 				un->un_f_arq_enabled = FALSE;
16412 			}
16413 			break;
16414 		case 1:
16415 			/*
16416 			 * ARQ is supported by this HBA and is already enabled.
16417 			 * Just mark ARQ as enabled for this instance.
16418 			 */
16419 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16420 			    "sd_alloc_rqs: ARQ already enabled\n");
16421 			un->un_f_arq_enabled = TRUE;
16422 			break;
16423 		default:
16424 			/*
16425 			 * ARQ is not supported by this HBA; disable it for this
16426 			 * instance.
16427 			 */
16428 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16429 			    "sd_alloc_rqs: HBA does not support ARQ\n");
16430 			un->un_f_arq_enabled = FALSE;
16431 			break;
16432 		}
16433 	}
16434 
16435 	return (DDI_SUCCESS);
16436 }
16437 
16438 
16439 /*
16440  *    Function: sd_free_rqs
16441  *
16442  * Description: Cleanup for the pre-instance RQS command.
16443  *
16444  *     Context: Kernel thread context
16445  */
16446 
16447 static void
16448 sd_free_rqs(struct sd_lun *un)
16449 {
16450 	ASSERT(un != NULL);
16451 
16452 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16453 
16454 	/*
16455 	 * If consistent memory is bound to a scsi_pkt, the pkt
16456 	 * has to be destroyed *before* freeing the consistent memory.
16457 	 * Don't change the sequence of this operations.
16458 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16459 	 * after it was freed in scsi_free_consistent_buf().
16460 	 */
16461 	if (un->un_rqs_pktp != NULL) {
16462 		scsi_destroy_pkt(un->un_rqs_pktp);
16463 		un->un_rqs_pktp = NULL;
16464 	}
16465 
16466 	if (un->un_rqs_bp != NULL) {
16467 		struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16468 		if (xp != NULL) {
16469 			kmem_free(xp, sizeof (struct sd_xbuf));
16470 		}
16471 		scsi_free_consistent_buf(un->un_rqs_bp);
16472 		un->un_rqs_bp = NULL;
16473 	}
16474 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16475 }
16476 
16477 
16478 
16479 /*
16480  *    Function: sd_reduce_throttle
16481  *
16482  * Description: Reduces the maximum # of outstanding commands on a
16483  *		target to the current number of outstanding commands.
16484  *		Queues a tiemout(9F) callback to restore the limit
16485  *		after a specified interval has elapsed.
16486  *		Typically used when we get a TRAN_BUSY return code
16487  *		back from scsi_transport().
16488  *
16489  *   Arguments: un - ptr to the sd_lun softstate struct
16490  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16491  *
16492  *     Context: May be called from interrupt context
16493  */
16494 
16495 static void
16496 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16497 {
16498 	ASSERT(un != NULL);
16499 	ASSERT(mutex_owned(SD_MUTEX(un)));
16500 	ASSERT(un->un_ncmds_in_transport >= 0);
16501 
16502 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16503 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16504 	    un, un->un_throttle, un->un_ncmds_in_transport);
16505 
16506 	if (un->un_throttle > 1) {
16507 		if (un->un_f_use_adaptive_throttle == TRUE) {
16508 			switch (throttle_type) {
16509 			case SD_THROTTLE_TRAN_BUSY:
16510 				if (un->un_busy_throttle == 0) {
16511 					un->un_busy_throttle = un->un_throttle;
16512 				}
16513 				break;
16514 			case SD_THROTTLE_QFULL:
16515 				un->un_busy_throttle = 0;
16516 				break;
16517 			default:
16518 				ASSERT(FALSE);
16519 			}
16520 
16521 			if (un->un_ncmds_in_transport > 0) {
16522 				un->un_throttle = un->un_ncmds_in_transport;
16523 			}
16524 
16525 		} else {
16526 			if (un->un_ncmds_in_transport == 0) {
16527 				un->un_throttle = 1;
16528 			} else {
16529 				un->un_throttle = un->un_ncmds_in_transport;
16530 			}
16531 		}
16532 	}
16533 
16534 	/* Reschedule the timeout if none is currently active */
16535 	if (un->un_reset_throttle_timeid == NULL) {
16536 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16537 		    un, SD_THROTTLE_RESET_INTERVAL);
16538 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16539 		    "sd_reduce_throttle: timeout scheduled!\n");
16540 	}
16541 
16542 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16543 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16544 }
16545 
16546 
16547 
16548 /*
16549  *    Function: sd_restore_throttle
16550  *
16551  * Description: Callback function for timeout(9F).  Resets the current
16552  *		value of un->un_throttle to its default.
16553  *
16554  *   Arguments: arg - pointer to associated softstate for the device.
16555  *
16556  *     Context: May be called from interrupt context
16557  */
16558 
16559 static void
16560 sd_restore_throttle(void *arg)
16561 {
16562 	struct sd_lun	*un = arg;
16563 
16564 	ASSERT(un != NULL);
16565 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16566 
16567 	mutex_enter(SD_MUTEX(un));
16568 
16569 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16570 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16571 
16572 	un->un_reset_throttle_timeid = NULL;
16573 
16574 	if (un->un_f_use_adaptive_throttle == TRUE) {
16575 		/*
16576 		 * If un_busy_throttle is nonzero, then it contains the
16577 		 * value that un_throttle was when we got a TRAN_BUSY back
16578 		 * from scsi_transport(). We want to revert back to this
16579 		 * value.
16580 		 *
16581 		 * In the QFULL case, the throttle limit will incrementally
16582 		 * increase until it reaches max throttle.
16583 		 */
16584 		if (un->un_busy_throttle > 0) {
16585 			un->un_throttle = un->un_busy_throttle;
16586 			un->un_busy_throttle = 0;
16587 		} else {
16588 			/*
16589 			 * increase throttle by 10% open gate slowly, schedule
16590 			 * another restore if saved throttle has not been
16591 			 * reached
16592 			 */
16593 			short throttle;
16594 			if (sd_qfull_throttle_enable) {
16595 				throttle = un->un_throttle +
16596 				    max((un->un_throttle / 10), 1);
16597 				un->un_throttle =
16598 				    (throttle < un->un_saved_throttle) ?
16599 				    throttle : un->un_saved_throttle;
16600 				if (un->un_throttle < un->un_saved_throttle) {
16601 					un->un_reset_throttle_timeid =
16602 					    timeout(sd_restore_throttle,
16603 					    un,
16604 					    SD_QFULL_THROTTLE_RESET_INTERVAL);
16605 				}
16606 			}
16607 		}
16608 
16609 		/*
16610 		 * If un_throttle has fallen below the low-water mark, we
16611 		 * restore the maximum value here (and allow it to ratchet
16612 		 * down again if necessary).
16613 		 */
16614 		if (un->un_throttle < un->un_min_throttle) {
16615 			un->un_throttle = un->un_saved_throttle;
16616 		}
16617 	} else {
16618 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16619 		    "restoring limit from 0x%x to 0x%x\n",
16620 		    un->un_throttle, un->un_saved_throttle);
16621 		un->un_throttle = un->un_saved_throttle;
16622 	}
16623 
16624 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16625 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16626 
16627 	sd_start_cmds(un, NULL);
16628 
16629 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16630 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16631 	    un, un->un_throttle);
16632 
16633 	mutex_exit(SD_MUTEX(un));
16634 
16635 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16636 }
16637 
16638 /*
16639  *    Function: sdrunout
16640  *
16641  * Description: Callback routine for scsi_init_pkt when a resource allocation
16642  *		fails.
16643  *
16644  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16645  *		soft state instance.
16646  *
16647  * Return Code: The scsi_init_pkt routine allows for the callback function to
16648  *		return a 0 indicating the callback should be rescheduled or a 1
16649  *		indicating not to reschedule. This routine always returns 1
16650  *		because the driver always provides a callback function to
16651  *		scsi_init_pkt. This results in a callback always being scheduled
16652  *		(via the scsi_init_pkt callback implementation) if a resource
16653  *		failure occurs.
16654  *
16655  *     Context: This callback function may not block or call routines that block
16656  *
16657  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16658  *		request persisting at the head of the list which cannot be
16659  *		satisfied even after multiple retries. In the future the driver
16660  *		may implement some time of maximum runout count before failing
16661  *		an I/O.
16662  */
16663 
16664 static int
16665 sdrunout(caddr_t arg)
16666 {
16667 	struct sd_lun	*un = (struct sd_lun *)arg;
16668 
16669 	ASSERT(un != NULL);
16670 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16671 
16672 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16673 
16674 	mutex_enter(SD_MUTEX(un));
16675 	sd_start_cmds(un, NULL);
16676 	mutex_exit(SD_MUTEX(un));
16677 	/*
16678 	 * This callback routine always returns 1 (i.e. do not reschedule)
16679 	 * because we always specify sdrunout as the callback handler for
16680 	 * scsi_init_pkt inside the call to sd_start_cmds.
16681 	 */
16682 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16683 	return (1);
16684 }
16685 
16686 
16687 /*
16688  *    Function: sdintr
16689  *
16690  * Description: Completion callback routine for scsi_pkt(9S) structs
16691  *		sent to the HBA driver via scsi_transport(9F).
16692  *
16693  *     Context: Interrupt context
16694  */
16695 
16696 static void
16697 sdintr(struct scsi_pkt *pktp)
16698 {
16699 	struct buf	*bp;
16700 	struct sd_xbuf	*xp;
16701 	struct sd_lun	*un;
16702 	size_t		actual_len;
16703 	sd_ssc_t	*sscp;
16704 
16705 	ASSERT(pktp != NULL);
16706 	bp = (struct buf *)pktp->pkt_private;
16707 	ASSERT(bp != NULL);
16708 	xp = SD_GET_XBUF(bp);
16709 	ASSERT(xp != NULL);
16710 	ASSERT(xp->xb_pktp != NULL);
16711 	un = SD_GET_UN(bp);
16712 	ASSERT(un != NULL);
16713 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16714 
16715 #ifdef SD_FAULT_INJECTION
16716 
16717 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16718 	/* SD FaultInjection */
16719 	sd_faultinjection(pktp);
16720 
16721 #endif /* SD_FAULT_INJECTION */
16722 
16723 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16724 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16725 
16726 	mutex_enter(SD_MUTEX(un));
16727 
16728 	ASSERT(un->un_fm_private != NULL);
16729 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16730 	ASSERT(sscp != NULL);
16731 
16732 	/* Reduce the count of the #commands currently in transport */
16733 	un->un_ncmds_in_transport--;
16734 	ASSERT(un->un_ncmds_in_transport >= 0);
16735 
16736 	/* Increment counter to indicate that the callback routine is active */
16737 	un->un_in_callback++;
16738 
16739 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16740 
16741 #ifdef	SDDEBUG
16742 	if (bp == un->un_retry_bp) {
16743 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16744 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16745 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16746 	}
16747 #endif
16748 
16749 	/*
16750 	 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16751 	 * state if needed.
16752 	 */
16753 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16754 		/* Prevent multiple console messages for the same failure. */
16755 		if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16756 			un->un_last_pkt_reason = CMD_DEV_GONE;
16757 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16758 			    "Command failed to complete...Device is gone\n");
16759 		}
16760 		if (un->un_mediastate != DKIO_DEV_GONE) {
16761 			un->un_mediastate = DKIO_DEV_GONE;
16762 			cv_broadcast(&un->un_state_cv);
16763 		}
16764 		/*
16765 		 * If the command happens to be the REQUEST SENSE command,
16766 		 * free up the rqs buf and fail the original command.
16767 		 */
16768 		if (bp == un->un_rqs_bp) {
16769 			bp = sd_mark_rqs_idle(un, xp);
16770 		}
16771 		sd_return_failed_command(un, bp, EIO);
16772 		goto exit;
16773 	}
16774 
16775 	if (pktp->pkt_state & STATE_XARQ_DONE) {
16776 		SD_TRACE(SD_LOG_COMMON, un,
16777 		    "sdintr: extra sense data received. pkt=%p\n", pktp);
16778 	}
16779 
16780 	/*
16781 	 * First see if the pkt has auto-request sense data with it....
16782 	 * Look at the packet state first so we don't take a performance
16783 	 * hit looking at the arq enabled flag unless absolutely necessary.
16784 	 */
16785 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16786 	    (un->un_f_arq_enabled == TRUE)) {
16787 		/*
16788 		 * The HBA did an auto request sense for this command so check
16789 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16790 		 * driver command that should not be retried.
16791 		 */
16792 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16793 			/*
16794 			 * Save the relevant sense info into the xp for the
16795 			 * original cmd.
16796 			 */
16797 			struct scsi_arq_status *asp;
16798 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16799 			xp->xb_sense_status =
16800 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16801 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16802 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16803 			if (pktp->pkt_state & STATE_XARQ_DONE) {
16804 				actual_len = MAX_SENSE_LENGTH -
16805 				    xp->xb_sense_resid;
16806 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16807 				    MAX_SENSE_LENGTH);
16808 			} else {
16809 				if (xp->xb_sense_resid > SENSE_LENGTH) {
16810 					actual_len = MAX_SENSE_LENGTH -
16811 					    xp->xb_sense_resid;
16812 				} else {
16813 					actual_len = SENSE_LENGTH -
16814 					    xp->xb_sense_resid;
16815 				}
16816 				if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16817 					if ((((struct uscsi_cmd *)
16818 					    (xp->xb_pktinfo))->uscsi_rqlen) >
16819 					    actual_len) {
16820 						xp->xb_sense_resid =
16821 						    (((struct uscsi_cmd *)
16822 						    (xp->xb_pktinfo))->
16823 						    uscsi_rqlen) - actual_len;
16824 					} else {
16825 						xp->xb_sense_resid = 0;
16826 					}
16827 				}
16828 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16829 				    SENSE_LENGTH);
16830 			}
16831 
16832 			/* fail the command */
16833 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16834 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16835 			sd_return_failed_command(un, bp, EIO);
16836 			goto exit;
16837 		}
16838 
16839 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16840 		/*
16841 		 * We want to either retry or fail this command, so free
16842 		 * the DMA resources here.  If we retry the command then
16843 		 * the DMA resources will be reallocated in sd_start_cmds().
16844 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16845 		 * causes the *entire* transfer to start over again from the
16846 		 * beginning of the request, even for PARTIAL chunks that
16847 		 * have already transferred successfully.
16848 		 */
16849 		if ((un->un_f_is_fibre == TRUE) &&
16850 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16851 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16852 			scsi_dmafree(pktp);
16853 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16854 		}
16855 #endif
16856 
16857 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16858 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16859 
16860 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16861 		goto exit;
16862 	}
16863 
16864 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16865 	if (pktp->pkt_flags & FLAG_SENSING)  {
16866 		/* This pktp is from the unit's REQUEST_SENSE command */
16867 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16868 		    "sdintr: sd_handle_request_sense\n");
16869 		sd_handle_request_sense(un, bp, xp, pktp);
16870 		goto exit;
16871 	}
16872 
16873 	/*
16874 	 * Check to see if the command successfully completed as requested;
16875 	 * this is the most common case (and also the hot performance path).
16876 	 *
16877 	 * Requirements for successful completion are:
16878 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16879 	 * In addition:
16880 	 * - A residual of zero indicates successful completion no matter what
16881 	 *   the command is.
16882 	 * - If the residual is not zero and the command is not a read or
16883 	 *   write, then it's still defined as successful completion. In other
16884 	 *   words, if the command is a read or write the residual must be
16885 	 *   zero for successful completion.
16886 	 * - If the residual is not zero and the command is a read or
16887 	 *   write, and it's a USCSICMD, then it's still defined as
16888 	 *   successful completion.
16889 	 */
16890 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16891 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16892 
16893 		/*
16894 		 * Since this command is returned with a good status, we
16895 		 * can reset the count for Sonoma failover.
16896 		 */
16897 		un->un_sonoma_failure_count = 0;
16898 
16899 		/*
16900 		 * Return all USCSI commands on good status
16901 		 */
16902 		if (pktp->pkt_resid == 0) {
16903 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16904 			    "sdintr: returning command for resid == 0\n");
16905 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16906 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16907 			SD_UPDATE_B_RESID(bp, pktp);
16908 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16909 			    "sdintr: returning command for resid != 0\n");
16910 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16911 			SD_UPDATE_B_RESID(bp, pktp);
16912 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16913 			    "sdintr: returning uscsi command\n");
16914 		} else {
16915 			goto not_successful;
16916 		}
16917 		sd_return_command(un, bp);
16918 
16919 		/*
16920 		 * Decrement counter to indicate that the callback routine
16921 		 * is done.
16922 		 */
16923 		un->un_in_callback--;
16924 		ASSERT(un->un_in_callback >= 0);
16925 		mutex_exit(SD_MUTEX(un));
16926 
16927 		return;
16928 	}
16929 
16930 not_successful:
16931 
16932 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16933 	/*
16934 	 * The following is based upon knowledge of the underlying transport
16935 	 * and its use of DMA resources.  This code should be removed when
16936 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16937 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16938 	 * and sd_start_cmds().
16939 	 *
16940 	 * Free any DMA resources associated with this command if there
16941 	 * is a chance it could be retried or enqueued for later retry.
16942 	 * If we keep the DMA binding then mpxio cannot reissue the
16943 	 * command on another path whenever a path failure occurs.
16944 	 *
16945 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16946 	 * causes the *entire* transfer to start over again from the
16947 	 * beginning of the request, even for PARTIAL chunks that
16948 	 * have already transferred successfully.
16949 	 *
16950 	 * This is only done for non-uscsi commands (and also skipped for the
16951 	 * driver's internal RQS command). Also just do this for Fibre Channel
16952 	 * devices as these are the only ones that support mpxio.
16953 	 */
16954 	if ((un->un_f_is_fibre == TRUE) &&
16955 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16956 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16957 		scsi_dmafree(pktp);
16958 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16959 	}
16960 #endif
16961 
16962 	/*
16963 	 * The command did not successfully complete as requested so check
16964 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16965 	 * driver command that should not be retried so just return. If
16966 	 * FLAG_DIAGNOSE is not set the error will be processed below.
16967 	 */
16968 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16969 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16970 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16971 		/*
16972 		 * Issue a request sense if a check condition caused the error
16973 		 * (we handle the auto request sense case above), otherwise
16974 		 * just fail the command.
16975 		 */
16976 		if ((pktp->pkt_reason == CMD_CMPLT) &&
16977 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16978 			sd_send_request_sense_command(un, bp, pktp);
16979 		} else {
16980 			sd_return_failed_command(un, bp, EIO);
16981 		}
16982 		goto exit;
16983 	}
16984 
16985 	/*
16986 	 * The command did not successfully complete as requested so process
16987 	 * the error, retry, and/or attempt recovery.
16988 	 */
16989 	switch (pktp->pkt_reason) {
16990 	case CMD_CMPLT:
16991 		switch (SD_GET_PKT_STATUS(pktp)) {
16992 		case STATUS_GOOD:
16993 			/*
16994 			 * The command completed successfully with a non-zero
16995 			 * residual
16996 			 */
16997 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16998 			    "sdintr: STATUS_GOOD \n");
16999 			sd_pkt_status_good(un, bp, xp, pktp);
17000 			break;
17001 
17002 		case STATUS_CHECK:
17003 		case STATUS_TERMINATED:
17004 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17005 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17006 			sd_pkt_status_check_condition(un, bp, xp, pktp);
17007 			break;
17008 
17009 		case STATUS_BUSY:
17010 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17011 			    "sdintr: STATUS_BUSY\n");
17012 			sd_pkt_status_busy(un, bp, xp, pktp);
17013 			break;
17014 
17015 		case STATUS_RESERVATION_CONFLICT:
17016 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17017 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
17018 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17019 			break;
17020 
17021 		case STATUS_QFULL:
17022 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17023 			    "sdintr: STATUS_QFULL\n");
17024 			sd_pkt_status_qfull(un, bp, xp, pktp);
17025 			break;
17026 
17027 		case STATUS_MET:
17028 		case STATUS_INTERMEDIATE:
17029 		case STATUS_SCSI2:
17030 		case STATUS_INTERMEDIATE_MET:
17031 		case STATUS_ACA_ACTIVE:
17032 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17033 			    "Unexpected SCSI status received: 0x%x\n",
17034 			    SD_GET_PKT_STATUS(pktp));
17035 			/*
17036 			 * Mark the ssc_flags when detected invalid status
17037 			 * code for non-USCSI command.
17038 			 */
17039 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17040 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17041 				    0, "stat-code");
17042 			}
17043 			sd_return_failed_command(un, bp, EIO);
17044 			break;
17045 
17046 		default:
17047 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17048 			    "Invalid SCSI status received: 0x%x\n",
17049 			    SD_GET_PKT_STATUS(pktp));
17050 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17051 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17052 				    0, "stat-code");
17053 			}
17054 			sd_return_failed_command(un, bp, EIO);
17055 			break;
17056 
17057 		}
17058 		break;
17059 
17060 	case CMD_INCOMPLETE:
17061 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17062 		    "sdintr:  CMD_INCOMPLETE\n");
17063 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17064 		break;
17065 	case CMD_TRAN_ERR:
17066 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17067 		    "sdintr: CMD_TRAN_ERR\n");
17068 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17069 		break;
17070 	case CMD_RESET:
17071 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17072 		    "sdintr: CMD_RESET \n");
17073 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17074 		break;
17075 	case CMD_ABORTED:
17076 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17077 		    "sdintr: CMD_ABORTED \n");
17078 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17079 		break;
17080 	case CMD_TIMEOUT:
17081 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17082 		    "sdintr: CMD_TIMEOUT\n");
17083 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17084 		break;
17085 	case CMD_UNX_BUS_FREE:
17086 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17087 		    "sdintr: CMD_UNX_BUS_FREE \n");
17088 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17089 		break;
17090 	case CMD_TAG_REJECT:
17091 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17092 		    "sdintr: CMD_TAG_REJECT\n");
17093 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17094 		break;
17095 	default:
17096 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17097 		    "sdintr: default\n");
17098 		/*
17099 		 * Mark the ssc_flags for detecting invliad pkt_reason.
17100 		 */
17101 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17102 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17103 			    0, "pkt-reason");
17104 		}
17105 		sd_pkt_reason_default(un, bp, xp, pktp);
17106 		break;
17107 	}
17108 
17109 exit:
17110 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17111 
17112 	/* Decrement counter to indicate that the callback routine is done. */
17113 	un->un_in_callback--;
17114 	ASSERT(un->un_in_callback >= 0);
17115 
17116 	/*
17117 	 * At this point, the pkt has been dispatched, ie, it is either
17118 	 * being re-tried or has been returned to its caller and should
17119 	 * not be referenced.
17120 	 */
17121 
17122 	mutex_exit(SD_MUTEX(un));
17123 }
17124 
17125 
17126 /*
17127  *    Function: sd_print_incomplete_msg
17128  *
17129  * Description: Prints the error message for a CMD_INCOMPLETE error.
17130  *
17131  *   Arguments: un - ptr to associated softstate for the device.
17132  *		bp - ptr to the buf(9S) for the command.
17133  *		arg - message string ptr
17134  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17135  *			or SD_NO_RETRY_ISSUED.
17136  *
17137  *     Context: May be called under interrupt context
17138  */
17139 
17140 static void
17141 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17142 {
17143 	struct scsi_pkt	*pktp;
17144 	char	*msgp;
17145 	char	*cmdp = arg;
17146 
17147 	ASSERT(un != NULL);
17148 	ASSERT(mutex_owned(SD_MUTEX(un)));
17149 	ASSERT(bp != NULL);
17150 	ASSERT(arg != NULL);
17151 	pktp = SD_GET_PKTP(bp);
17152 	ASSERT(pktp != NULL);
17153 
17154 	switch (code) {
17155 	case SD_DELAYED_RETRY_ISSUED:
17156 	case SD_IMMEDIATE_RETRY_ISSUED:
17157 		msgp = "retrying";
17158 		break;
17159 	case SD_NO_RETRY_ISSUED:
17160 	default:
17161 		msgp = "giving up";
17162 		break;
17163 	}
17164 
17165 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17166 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17167 		    "incomplete %s- %s\n", cmdp, msgp);
17168 	}
17169 }
17170 
17171 
17172 
17173 /*
17174  *    Function: sd_pkt_status_good
17175  *
17176  * Description: Processing for a STATUS_GOOD code in pkt_status.
17177  *
17178  *     Context: May be called under interrupt context
17179  */
17180 
17181 static void
17182 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17183 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17184 {
17185 	char	*cmdp;
17186 
17187 	ASSERT(un != NULL);
17188 	ASSERT(mutex_owned(SD_MUTEX(un)));
17189 	ASSERT(bp != NULL);
17190 	ASSERT(xp != NULL);
17191 	ASSERT(pktp != NULL);
17192 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17193 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17194 	ASSERT(pktp->pkt_resid != 0);
17195 
17196 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17197 
17198 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17199 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17200 	case SCMD_READ:
17201 		cmdp = "read";
17202 		break;
17203 	case SCMD_WRITE:
17204 		cmdp = "write";
17205 		break;
17206 	default:
17207 		SD_UPDATE_B_RESID(bp, pktp);
17208 		sd_return_command(un, bp);
17209 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17210 		return;
17211 	}
17212 
17213 	/*
17214 	 * See if we can retry the read/write, preferrably immediately.
17215 	 * If retries are exhaused, then sd_retry_command() will update
17216 	 * the b_resid count.
17217 	 */
17218 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17219 	    cmdp, EIO, (clock_t)0, NULL);
17220 
17221 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17222 }
17223 
17224 
17225 
17226 
17227 
17228 /*
17229  *    Function: sd_handle_request_sense
17230  *
17231  * Description: Processing for non-auto Request Sense command.
17232  *
17233  *   Arguments: un - ptr to associated softstate
17234  *		sense_bp - ptr to buf(9S) for the RQS command
17235  *		sense_xp - ptr to the sd_xbuf for the RQS command
17236  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17237  *
17238  *     Context: May be called under interrupt context
17239  */
17240 
17241 static void
17242 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17243 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17244 {
17245 	struct buf	*cmd_bp;	/* buf for the original command */
17246 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17247 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17248 	size_t		actual_len;	/* actual sense data length */
17249 
17250 	ASSERT(un != NULL);
17251 	ASSERT(mutex_owned(SD_MUTEX(un)));
17252 	ASSERT(sense_bp != NULL);
17253 	ASSERT(sense_xp != NULL);
17254 	ASSERT(sense_pktp != NULL);
17255 
17256 	/*
17257 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17258 	 * RQS command and not the original command.
17259 	 */
17260 	ASSERT(sense_pktp == un->un_rqs_pktp);
17261 	ASSERT(sense_bp   == un->un_rqs_bp);
17262 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17263 	    (FLAG_SENSING | FLAG_HEAD));
17264 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17265 	    FLAG_SENSING) == FLAG_SENSING);
17266 
17267 	/* These are the bp, xp, and pktp for the original command */
17268 	cmd_bp = sense_xp->xb_sense_bp;
17269 	cmd_xp = SD_GET_XBUF(cmd_bp);
17270 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17271 
17272 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17273 		/*
17274 		 * The REQUEST SENSE command failed.  Release the REQUEST
17275 		 * SENSE command for re-use, get back the bp for the original
17276 		 * command, and attempt to re-try the original command if
17277 		 * FLAG_DIAGNOSE is not set in the original packet.
17278 		 */
17279 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17280 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17281 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17282 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17283 			    NULL, NULL, EIO, (clock_t)0, NULL);
17284 			return;
17285 		}
17286 	}
17287 
17288 	/*
17289 	 * Save the relevant sense info into the xp for the original cmd.
17290 	 *
17291 	 * Note: if the request sense failed the state info will be zero
17292 	 * as set in sd_mark_rqs_busy()
17293 	 */
17294 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17295 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17296 	actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17297 	if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17298 	    (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17299 	    SENSE_LENGTH)) {
17300 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17301 		    MAX_SENSE_LENGTH);
17302 		cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17303 	} else {
17304 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17305 		    SENSE_LENGTH);
17306 		if (actual_len < SENSE_LENGTH) {
17307 			cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17308 		} else {
17309 			cmd_xp->xb_sense_resid = 0;
17310 		}
17311 	}
17312 
17313 	/*
17314 	 *  Free up the RQS command....
17315 	 *  NOTE:
17316 	 *	Must do this BEFORE calling sd_validate_sense_data!
17317 	 *	sd_validate_sense_data may return the original command in
17318 	 *	which case the pkt will be freed and the flags can no
17319 	 *	longer be touched.
17320 	 *	SD_MUTEX is held through this process until the command
17321 	 *	is dispatched based upon the sense data, so there are
17322 	 *	no race conditions.
17323 	 */
17324 	(void) sd_mark_rqs_idle(un, sense_xp);
17325 
17326 	/*
17327 	 * For a retryable command see if we have valid sense data, if so then
17328 	 * turn it over to sd_decode_sense() to figure out the right course of
17329 	 * action. Just fail a non-retryable command.
17330 	 */
17331 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17332 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17333 		    SD_SENSE_DATA_IS_VALID) {
17334 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17335 		}
17336 	} else {
17337 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17338 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17339 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17340 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17341 		sd_return_failed_command(un, cmd_bp, EIO);
17342 	}
17343 }
17344 
17345 
17346 
17347 
17348 /*
17349  *    Function: sd_handle_auto_request_sense
17350  *
17351  * Description: Processing for auto-request sense information.
17352  *
17353  *   Arguments: un - ptr to associated softstate
17354  *		bp - ptr to buf(9S) for the command
17355  *		xp - ptr to the sd_xbuf for the command
17356  *		pktp - ptr to the scsi_pkt(9S) for the command
17357  *
17358  *     Context: May be called under interrupt context
17359  */
17360 
17361 static void
17362 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17363 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17364 {
17365 	struct scsi_arq_status *asp;
17366 	size_t actual_len;
17367 
17368 	ASSERT(un != NULL);
17369 	ASSERT(mutex_owned(SD_MUTEX(un)));
17370 	ASSERT(bp != NULL);
17371 	ASSERT(xp != NULL);
17372 	ASSERT(pktp != NULL);
17373 	ASSERT(pktp != un->un_rqs_pktp);
17374 	ASSERT(bp   != un->un_rqs_bp);
17375 
17376 	/*
17377 	 * For auto-request sense, we get a scsi_arq_status back from
17378 	 * the HBA, with the sense data in the sts_sensedata member.
17379 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17380 	 */
17381 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17382 
17383 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17384 		/*
17385 		 * The auto REQUEST SENSE failed; see if we can re-try
17386 		 * the original command.
17387 		 */
17388 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17389 		    "auto request sense failed (reason=%s)\n",
17390 		    scsi_rname(asp->sts_rqpkt_reason));
17391 
17392 		sd_reset_target(un, pktp);
17393 
17394 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17395 		    NULL, NULL, EIO, (clock_t)0, NULL);
17396 		return;
17397 	}
17398 
17399 	/* Save the relevant sense info into the xp for the original cmd. */
17400 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17401 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17402 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17403 	if (xp->xb_sense_state & STATE_XARQ_DONE) {
17404 		actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17405 		bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17406 		    MAX_SENSE_LENGTH);
17407 	} else {
17408 		if (xp->xb_sense_resid > SENSE_LENGTH) {
17409 			actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17410 		} else {
17411 			actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17412 		}
17413 		if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17414 			if ((((struct uscsi_cmd *)
17415 			    (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17416 				xp->xb_sense_resid = (((struct uscsi_cmd *)
17417 				    (xp->xb_pktinfo))->uscsi_rqlen) -
17418 				    actual_len;
17419 			} else {
17420 				xp->xb_sense_resid = 0;
17421 			}
17422 		}
17423 		bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17424 	}
17425 
17426 	/*
17427 	 * See if we have valid sense data, if so then turn it over to
17428 	 * sd_decode_sense() to figure out the right course of action.
17429 	 */
17430 	if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17431 	    SD_SENSE_DATA_IS_VALID) {
17432 		sd_decode_sense(un, bp, xp, pktp);
17433 	}
17434 }
17435 
17436 
17437 /*
17438  *    Function: sd_print_sense_failed_msg
17439  *
17440  * Description: Print log message when RQS has failed.
17441  *
17442  *   Arguments: un - ptr to associated softstate
17443  *		bp - ptr to buf(9S) for the command
17444  *		arg - generic message string ptr
17445  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17446  *			or SD_NO_RETRY_ISSUED
17447  *
17448  *     Context: May be called from interrupt context
17449  */
17450 
17451 static void
17452 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17453 	int code)
17454 {
17455 	char	*msgp = arg;
17456 
17457 	ASSERT(un != NULL);
17458 	ASSERT(mutex_owned(SD_MUTEX(un)));
17459 	ASSERT(bp != NULL);
17460 
17461 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17462 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17463 	}
17464 }
17465 
17466 
17467 /*
17468  *    Function: sd_validate_sense_data
17469  *
17470  * Description: Check the given sense data for validity.
17471  *		If the sense data is not valid, the command will
17472  *		be either failed or retried!
17473  *
17474  * Return Code: SD_SENSE_DATA_IS_INVALID
17475  *		SD_SENSE_DATA_IS_VALID
17476  *
17477  *     Context: May be called from interrupt context
17478  */
17479 
17480 static int
17481 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17482 	size_t actual_len)
17483 {
17484 	struct scsi_extended_sense *esp;
17485 	struct	scsi_pkt *pktp;
17486 	char	*msgp = NULL;
17487 	sd_ssc_t *sscp;
17488 
17489 	ASSERT(un != NULL);
17490 	ASSERT(mutex_owned(SD_MUTEX(un)));
17491 	ASSERT(bp != NULL);
17492 	ASSERT(bp != un->un_rqs_bp);
17493 	ASSERT(xp != NULL);
17494 	ASSERT(un->un_fm_private != NULL);
17495 
17496 	pktp = SD_GET_PKTP(bp);
17497 	ASSERT(pktp != NULL);
17498 
17499 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17500 	ASSERT(sscp != NULL);
17501 
17502 	/*
17503 	 * Check the status of the RQS command (auto or manual).
17504 	 */
17505 	switch (xp->xb_sense_status & STATUS_MASK) {
17506 	case STATUS_GOOD:
17507 		break;
17508 
17509 	case STATUS_RESERVATION_CONFLICT:
17510 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17511 		return (SD_SENSE_DATA_IS_INVALID);
17512 
17513 	case STATUS_BUSY:
17514 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17515 		    "Busy Status on REQUEST SENSE\n");
17516 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17517 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17518 		return (SD_SENSE_DATA_IS_INVALID);
17519 
17520 	case STATUS_QFULL:
17521 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17522 		    "QFULL Status on REQUEST SENSE\n");
17523 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17524 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17525 		return (SD_SENSE_DATA_IS_INVALID);
17526 
17527 	case STATUS_CHECK:
17528 	case STATUS_TERMINATED:
17529 		msgp = "Check Condition on REQUEST SENSE\n";
17530 		goto sense_failed;
17531 
17532 	default:
17533 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17534 		goto sense_failed;
17535 	}
17536 
17537 	/*
17538 	 * See if we got the minimum required amount of sense data.
17539 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17540 	 * or less.
17541 	 */
17542 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17543 	    (actual_len == 0)) {
17544 		msgp = "Request Sense couldn't get sense data\n";
17545 		goto sense_failed;
17546 	}
17547 
17548 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17549 		msgp = "Not enough sense information\n";
17550 		/* Mark the ssc_flags for detecting invalid sense data */
17551 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17552 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17553 			    "sense-data");
17554 		}
17555 		goto sense_failed;
17556 	}
17557 
17558 	/*
17559 	 * We require the extended sense data
17560 	 */
17561 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17562 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17563 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17564 			static char tmp[8];
17565 			static char buf[148];
17566 			char *p = (char *)(xp->xb_sense_data);
17567 			int i;
17568 
17569 			mutex_enter(&sd_sense_mutex);
17570 			(void) strcpy(buf, "undecodable sense information:");
17571 			for (i = 0; i < actual_len; i++) {
17572 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17573 				(void) strcpy(&buf[strlen(buf)], tmp);
17574 			}
17575 			i = strlen(buf);
17576 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17577 
17578 			if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17579 				scsi_log(SD_DEVINFO(un), sd_label,
17580 				    CE_WARN, buf);
17581 			}
17582 			mutex_exit(&sd_sense_mutex);
17583 		}
17584 
17585 		/* Mark the ssc_flags for detecting invalid sense data */
17586 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17587 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17588 			    "sense-data");
17589 		}
17590 
17591 		/* Note: Legacy behavior, fail the command with no retry */
17592 		sd_return_failed_command(un, bp, EIO);
17593 		return (SD_SENSE_DATA_IS_INVALID);
17594 	}
17595 
17596 	/*
17597 	 * Check that es_code is valid (es_class concatenated with es_code
17598 	 * make up the "response code" field.  es_class will always be 7, so
17599 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17600 	 * format.
17601 	 */
17602 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17603 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17604 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17605 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17606 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17607 		/* Mark the ssc_flags for detecting invalid sense data */
17608 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17609 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17610 			    "sense-data");
17611 		}
17612 		goto sense_failed;
17613 	}
17614 
17615 	return (SD_SENSE_DATA_IS_VALID);
17616 
17617 sense_failed:
17618 	/*
17619 	 * If the request sense failed (for whatever reason), attempt
17620 	 * to retry the original command.
17621 	 */
17622 #if defined(__i386) || defined(__amd64)
17623 	/*
17624 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17625 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17626 	 * for both SCSI/FC.
17627 	 * The SD_RETRY_DELAY value need to be adjusted here
17628 	 * when SD_RETRY_DELAY change in sddef.h
17629 	 */
17630 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17631 	    sd_print_sense_failed_msg, msgp, EIO,
17632 	    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17633 #else
17634 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17635 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17636 #endif
17637 
17638 	return (SD_SENSE_DATA_IS_INVALID);
17639 }
17640 
17641 /*
17642  *    Function: sd_decode_sense
17643  *
17644  * Description: Take recovery action(s) when SCSI Sense Data is received.
17645  *
17646  *     Context: Interrupt context.
17647  */
17648 
17649 static void
17650 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17651 	struct scsi_pkt *pktp)
17652 {
17653 	uint8_t sense_key;
17654 
17655 	ASSERT(un != NULL);
17656 	ASSERT(mutex_owned(SD_MUTEX(un)));
17657 	ASSERT(bp != NULL);
17658 	ASSERT(bp != un->un_rqs_bp);
17659 	ASSERT(xp != NULL);
17660 	ASSERT(pktp != NULL);
17661 
17662 	sense_key = scsi_sense_key(xp->xb_sense_data);
17663 
17664 	switch (sense_key) {
17665 	case KEY_NO_SENSE:
17666 		sd_sense_key_no_sense(un, bp, xp, pktp);
17667 		break;
17668 	case KEY_RECOVERABLE_ERROR:
17669 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17670 		    bp, xp, pktp);
17671 		break;
17672 	case KEY_NOT_READY:
17673 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17674 		    bp, xp, pktp);
17675 		break;
17676 	case KEY_MEDIUM_ERROR:
17677 	case KEY_HARDWARE_ERROR:
17678 		sd_sense_key_medium_or_hardware_error(un,
17679 		    xp->xb_sense_data, bp, xp, pktp);
17680 		break;
17681 	case KEY_ILLEGAL_REQUEST:
17682 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17683 		break;
17684 	case KEY_UNIT_ATTENTION:
17685 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17686 		    bp, xp, pktp);
17687 		break;
17688 	case KEY_WRITE_PROTECT:
17689 	case KEY_VOLUME_OVERFLOW:
17690 	case KEY_MISCOMPARE:
17691 		sd_sense_key_fail_command(un, bp, xp, pktp);
17692 		break;
17693 	case KEY_BLANK_CHECK:
17694 		sd_sense_key_blank_check(un, bp, xp, pktp);
17695 		break;
17696 	case KEY_ABORTED_COMMAND:
17697 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17698 		break;
17699 	case KEY_VENDOR_UNIQUE:
17700 	case KEY_COPY_ABORTED:
17701 	case KEY_EQUAL:
17702 	case KEY_RESERVED:
17703 	default:
17704 		sd_sense_key_default(un, xp->xb_sense_data,
17705 		    bp, xp, pktp);
17706 		break;
17707 	}
17708 }
17709 
17710 
17711 /*
17712  *    Function: sd_dump_memory
17713  *
17714  * Description: Debug logging routine to print the contents of a user provided
17715  *		buffer. The output of the buffer is broken up into 256 byte
17716  *		segments due to a size constraint of the scsi_log.
17717  *		implementation.
17718  *
17719  *   Arguments: un - ptr to softstate
17720  *		comp - component mask
17721  *		title - "title" string to preceed data when printed
17722  *		data - ptr to data block to be printed
17723  *		len - size of data block to be printed
17724  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17725  *
17726  *     Context: May be called from interrupt context
17727  */
17728 
17729 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17730 
17731 static char *sd_dump_format_string[] = {
17732 		" 0x%02x",
17733 		" %c"
17734 };
17735 
17736 static void
17737 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17738     int len, int fmt)
17739 {
17740 	int	i, j;
17741 	int	avail_count;
17742 	int	start_offset;
17743 	int	end_offset;
17744 	size_t	entry_len;
17745 	char	*bufp;
17746 	char	*local_buf;
17747 	char	*format_string;
17748 
17749 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17750 
17751 	/*
17752 	 * In the debug version of the driver, this function is called from a
17753 	 * number of places which are NOPs in the release driver.
17754 	 * The debug driver therefore has additional methods of filtering
17755 	 * debug output.
17756 	 */
17757 #ifdef SDDEBUG
17758 	/*
17759 	 * In the debug version of the driver we can reduce the amount of debug
17760 	 * messages by setting sd_error_level to something other than
17761 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17762 	 * sd_component_mask.
17763 	 */
17764 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17765 	    (sd_error_level != SCSI_ERR_ALL)) {
17766 		return;
17767 	}
17768 	if (((sd_component_mask & comp) == 0) ||
17769 	    (sd_error_level != SCSI_ERR_ALL)) {
17770 		return;
17771 	}
17772 #else
17773 	if (sd_error_level != SCSI_ERR_ALL) {
17774 		return;
17775 	}
17776 #endif
17777 
17778 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17779 	bufp = local_buf;
17780 	/*
17781 	 * Available length is the length of local_buf[], minus the
17782 	 * length of the title string, minus one for the ":", minus
17783 	 * one for the newline, minus one for the NULL terminator.
17784 	 * This gives the #bytes available for holding the printed
17785 	 * values from the given data buffer.
17786 	 */
17787 	if (fmt == SD_LOG_HEX) {
17788 		format_string = sd_dump_format_string[0];
17789 	} else /* SD_LOG_CHAR */ {
17790 		format_string = sd_dump_format_string[1];
17791 	}
17792 	/*
17793 	 * Available count is the number of elements from the given
17794 	 * data buffer that we can fit into the available length.
17795 	 * This is based upon the size of the format string used.
17796 	 * Make one entry and find it's size.
17797 	 */
17798 	(void) sprintf(bufp, format_string, data[0]);
17799 	entry_len = strlen(bufp);
17800 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17801 
17802 	j = 0;
17803 	while (j < len) {
17804 		bufp = local_buf;
17805 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17806 		start_offset = j;
17807 
17808 		end_offset = start_offset + avail_count;
17809 
17810 		(void) sprintf(bufp, "%s:", title);
17811 		bufp += strlen(bufp);
17812 		for (i = start_offset; ((i < end_offset) && (j < len));
17813 		    i++, j++) {
17814 			(void) sprintf(bufp, format_string, data[i]);
17815 			bufp += entry_len;
17816 		}
17817 		(void) sprintf(bufp, "\n");
17818 
17819 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17820 	}
17821 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17822 }
17823 
17824 /*
17825  *    Function: sd_print_sense_msg
17826  *
17827  * Description: Log a message based upon the given sense data.
17828  *
17829  *   Arguments: un - ptr to associated softstate
17830  *		bp - ptr to buf(9S) for the command
17831  *		arg - ptr to associate sd_sense_info struct
17832  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17833  *			or SD_NO_RETRY_ISSUED
17834  *
17835  *     Context: May be called from interrupt context
17836  */
17837 
17838 static void
17839 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17840 {
17841 	struct sd_xbuf	*xp;
17842 	struct scsi_pkt	*pktp;
17843 	uint8_t *sensep;
17844 	daddr_t request_blkno;
17845 	diskaddr_t err_blkno;
17846 	int severity;
17847 	int pfa_flag;
17848 	extern struct scsi_key_strings scsi_cmds[];
17849 
17850 	ASSERT(un != NULL);
17851 	ASSERT(mutex_owned(SD_MUTEX(un)));
17852 	ASSERT(bp != NULL);
17853 	xp = SD_GET_XBUF(bp);
17854 	ASSERT(xp != NULL);
17855 	pktp = SD_GET_PKTP(bp);
17856 	ASSERT(pktp != NULL);
17857 	ASSERT(arg != NULL);
17858 
17859 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17860 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17861 
17862 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17863 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17864 		severity = SCSI_ERR_RETRYABLE;
17865 	}
17866 
17867 	/* Use absolute block number for the request block number */
17868 	request_blkno = xp->xb_blkno;
17869 
17870 	/*
17871 	 * Now try to get the error block number from the sense data
17872 	 */
17873 	sensep = xp->xb_sense_data;
17874 
17875 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17876 	    (uint64_t *)&err_blkno)) {
17877 		/*
17878 		 * We retrieved the error block number from the information
17879 		 * portion of the sense data.
17880 		 *
17881 		 * For USCSI commands we are better off using the error
17882 		 * block no. as the requested block no. (This is the best
17883 		 * we can estimate.)
17884 		 */
17885 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17886 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17887 			request_blkno = err_blkno;
17888 		}
17889 	} else {
17890 		/*
17891 		 * Without the es_valid bit set (for fixed format) or an
17892 		 * information descriptor (for descriptor format) we cannot
17893 		 * be certain of the error blkno, so just use the
17894 		 * request_blkno.
17895 		 */
17896 		err_blkno = (diskaddr_t)request_blkno;
17897 	}
17898 
17899 	/*
17900 	 * The following will log the buffer contents for the release driver
17901 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17902 	 * level is set to verbose.
17903 	 */
17904 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17905 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17906 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17907 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17908 
17909 	if (pfa_flag == FALSE) {
17910 		/* This is normally only set for USCSI */
17911 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17912 			return;
17913 		}
17914 
17915 		if ((SD_IS_BUFIO(xp) == TRUE) &&
17916 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17917 		    (severity < sd_error_level))) {
17918 			return;
17919 		}
17920 	}
17921 	/*
17922 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
17923 	 */
17924 	if ((SD_IS_LSI(un)) &&
17925 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
17926 	    (scsi_sense_asc(sensep) == 0x94) &&
17927 	    (scsi_sense_ascq(sensep) == 0x01)) {
17928 		un->un_sonoma_failure_count++;
17929 		if (un->un_sonoma_failure_count > 1) {
17930 			return;
17931 		}
17932 	}
17933 
17934 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
17935 	    ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
17936 	    (pktp->pkt_resid == 0))) {
17937 		scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17938 		    request_blkno, err_blkno, scsi_cmds,
17939 		    (struct scsi_extended_sense *)sensep,
17940 		    un->un_additional_codes, NULL);
17941 	}
17942 }
17943 
17944 /*
17945  *    Function: sd_sense_key_no_sense
17946  *
17947  * Description: Recovery action when sense data was not received.
17948  *
17949  *     Context: May be called from interrupt context
17950  */
17951 
17952 static void
17953 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
17954 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17955 {
17956 	struct sd_sense_info	si;
17957 
17958 	ASSERT(un != NULL);
17959 	ASSERT(mutex_owned(SD_MUTEX(un)));
17960 	ASSERT(bp != NULL);
17961 	ASSERT(xp != NULL);
17962 	ASSERT(pktp != NULL);
17963 
17964 	si.ssi_severity = SCSI_ERR_FATAL;
17965 	si.ssi_pfa_flag = FALSE;
17966 
17967 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17968 
17969 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17970 	    &si, EIO, (clock_t)0, NULL);
17971 }
17972 
17973 
17974 /*
17975  *    Function: sd_sense_key_recoverable_error
17976  *
17977  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17978  *
17979  *     Context: May be called from interrupt context
17980  */
17981 
17982 static void
17983 sd_sense_key_recoverable_error(struct sd_lun *un,
17984 	uint8_t *sense_datap,
17985 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17986 {
17987 	struct sd_sense_info	si;
17988 	uint8_t asc = scsi_sense_asc(sense_datap);
17989 
17990 	ASSERT(un != NULL);
17991 	ASSERT(mutex_owned(SD_MUTEX(un)));
17992 	ASSERT(bp != NULL);
17993 	ASSERT(xp != NULL);
17994 	ASSERT(pktp != NULL);
17995 
17996 	/*
17997 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17998 	 */
17999 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18000 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18001 		si.ssi_severity = SCSI_ERR_INFO;
18002 		si.ssi_pfa_flag = TRUE;
18003 	} else {
18004 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
18005 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18006 		si.ssi_severity = SCSI_ERR_RECOVERED;
18007 		si.ssi_pfa_flag = FALSE;
18008 	}
18009 
18010 	if (pktp->pkt_resid == 0) {
18011 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18012 		sd_return_command(un, bp);
18013 		return;
18014 	}
18015 
18016 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18017 	    &si, EIO, (clock_t)0, NULL);
18018 }
18019 
18020 
18021 
18022 
18023 /*
18024  *    Function: sd_sense_key_not_ready
18025  *
18026  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18027  *
18028  *     Context: May be called from interrupt context
18029  */
18030 
18031 static void
18032 sd_sense_key_not_ready(struct sd_lun *un,
18033 	uint8_t *sense_datap,
18034 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18035 {
18036 	struct sd_sense_info	si;
18037 	uint8_t asc = scsi_sense_asc(sense_datap);
18038 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18039 
18040 	ASSERT(un != NULL);
18041 	ASSERT(mutex_owned(SD_MUTEX(un)));
18042 	ASSERT(bp != NULL);
18043 	ASSERT(xp != NULL);
18044 	ASSERT(pktp != NULL);
18045 
18046 	si.ssi_severity = SCSI_ERR_FATAL;
18047 	si.ssi_pfa_flag = FALSE;
18048 
18049 	/*
18050 	 * Update error stats after first NOT READY error. Disks may have
18051 	 * been powered down and may need to be restarted.  For CDROMs,
18052 	 * report NOT READY errors only if media is present.
18053 	 */
18054 	if ((ISCD(un) && (asc == 0x3A)) ||
18055 	    (xp->xb_nr_retry_count > 0)) {
18056 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18057 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18058 	}
18059 
18060 	/*
18061 	 * Just fail if the "not ready" retry limit has been reached.
18062 	 */
18063 	if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18064 		/* Special check for error message printing for removables. */
18065 		if (un->un_f_has_removable_media && (asc == 0x04) &&
18066 		    (ascq >= 0x04)) {
18067 			si.ssi_severity = SCSI_ERR_ALL;
18068 		}
18069 		goto fail_command;
18070 	}
18071 
18072 	/*
18073 	 * Check the ASC and ASCQ in the sense data as needed, to determine
18074 	 * what to do.
18075 	 */
18076 	switch (asc) {
18077 	case 0x04:	/* LOGICAL UNIT NOT READY */
18078 		/*
18079 		 * disk drives that don't spin up result in a very long delay
18080 		 * in format without warning messages. We will log a message
18081 		 * if the error level is set to verbose.
18082 		 */
18083 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18084 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18085 			    "logical unit not ready, resetting disk\n");
18086 		}
18087 
18088 		/*
18089 		 * There are different requirements for CDROMs and disks for
18090 		 * the number of retries.  If a CD-ROM is giving this, it is
18091 		 * probably reading TOC and is in the process of getting
18092 		 * ready, so we should keep on trying for a long time to make
18093 		 * sure that all types of media are taken in account (for
18094 		 * some media the drive takes a long time to read TOC).  For
18095 		 * disks we do not want to retry this too many times as this
18096 		 * can cause a long hang in format when the drive refuses to
18097 		 * spin up (a very common failure).
18098 		 */
18099 		switch (ascq) {
18100 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18101 			/*
18102 			 * Disk drives frequently refuse to spin up which
18103 			 * results in a very long hang in format without
18104 			 * warning messages.
18105 			 *
18106 			 * Note: This code preserves the legacy behavior of
18107 			 * comparing xb_nr_retry_count against zero for fibre
18108 			 * channel targets instead of comparing against the
18109 			 * un_reset_retry_count value.  The reason for this
18110 			 * discrepancy has been so utterly lost beneath the
18111 			 * Sands of Time that even Indiana Jones could not
18112 			 * find it.
18113 			 */
18114 			if (un->un_f_is_fibre == TRUE) {
18115 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18116 				    (xp->xb_nr_retry_count > 0)) &&
18117 				    (un->un_startstop_timeid == NULL)) {
18118 					scsi_log(SD_DEVINFO(un), sd_label,
18119 					    CE_WARN, "logical unit not ready, "
18120 					    "resetting disk\n");
18121 					sd_reset_target(un, pktp);
18122 				}
18123 			} else {
18124 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18125 				    (xp->xb_nr_retry_count >
18126 				    un->un_reset_retry_count)) &&
18127 				    (un->un_startstop_timeid == NULL)) {
18128 					scsi_log(SD_DEVINFO(un), sd_label,
18129 					    CE_WARN, "logical unit not ready, "
18130 					    "resetting disk\n");
18131 					sd_reset_target(un, pktp);
18132 				}
18133 			}
18134 			break;
18135 
18136 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18137 			/*
18138 			 * If the target is in the process of becoming
18139 			 * ready, just proceed with the retry. This can
18140 			 * happen with CD-ROMs that take a long time to
18141 			 * read TOC after a power cycle or reset.
18142 			 */
18143 			goto do_retry;
18144 
18145 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18146 			break;
18147 
18148 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18149 			/*
18150 			 * Retries cannot help here so just fail right away.
18151 			 */
18152 			goto fail_command;
18153 
18154 		case 0x88:
18155 			/*
18156 			 * Vendor-unique code for T3/T4: it indicates a
18157 			 * path problem in a mutipathed config, but as far as
18158 			 * the target driver is concerned it equates to a fatal
18159 			 * error, so we should just fail the command right away
18160 			 * (without printing anything to the console). If this
18161 			 * is not a T3/T4, fall thru to the default recovery
18162 			 * action.
18163 			 * T3/T4 is FC only, don't need to check is_fibre
18164 			 */
18165 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
18166 				sd_return_failed_command(un, bp, EIO);
18167 				return;
18168 			}
18169 			/* FALLTHRU */
18170 
18171 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18172 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18173 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18174 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18175 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18176 		default:    /* Possible future codes in SCSI spec? */
18177 			/*
18178 			 * For removable-media devices, do not retry if
18179 			 * ASCQ > 2 as these result mostly from USCSI commands
18180 			 * on MMC devices issued to check status of an
18181 			 * operation initiated in immediate mode.  Also for
18182 			 * ASCQ >= 4 do not print console messages as these
18183 			 * mainly represent a user-initiated operation
18184 			 * instead of a system failure.
18185 			 */
18186 			if (un->un_f_has_removable_media) {
18187 				si.ssi_severity = SCSI_ERR_ALL;
18188 				goto fail_command;
18189 			}
18190 			break;
18191 		}
18192 
18193 		/*
18194 		 * As part of our recovery attempt for the NOT READY
18195 		 * condition, we issue a START STOP UNIT command. However
18196 		 * we want to wait for a short delay before attempting this
18197 		 * as there may still be more commands coming back from the
18198 		 * target with the check condition. To do this we use
18199 		 * timeout(9F) to call sd_start_stop_unit_callback() after
18200 		 * the delay interval expires. (sd_start_stop_unit_callback()
18201 		 * dispatches sd_start_stop_unit_task(), which will issue
18202 		 * the actual START STOP UNIT command. The delay interval
18203 		 * is one-half of the delay that we will use to retry the
18204 		 * command that generated the NOT READY condition.
18205 		 *
18206 		 * Note that we could just dispatch sd_start_stop_unit_task()
18207 		 * from here and allow it to sleep for the delay interval,
18208 		 * but then we would be tying up the taskq thread
18209 		 * uncesessarily for the duration of the delay.
18210 		 *
18211 		 * Do not issue the START STOP UNIT if the current command
18212 		 * is already a START STOP UNIT.
18213 		 */
18214 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18215 			break;
18216 		}
18217 
18218 		/*
18219 		 * Do not schedule the timeout if one is already pending.
18220 		 */
18221 		if (un->un_startstop_timeid != NULL) {
18222 			SD_INFO(SD_LOG_ERROR, un,
18223 			    "sd_sense_key_not_ready: restart already issued to"
18224 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18225 			    ddi_get_instance(SD_DEVINFO(un)));
18226 			break;
18227 		}
18228 
18229 		/*
18230 		 * Schedule the START STOP UNIT command, then queue the command
18231 		 * for a retry.
18232 		 *
18233 		 * Note: A timeout is not scheduled for this retry because we
18234 		 * want the retry to be serial with the START_STOP_UNIT. The
18235 		 * retry will be started when the START_STOP_UNIT is completed
18236 		 * in sd_start_stop_unit_task.
18237 		 */
18238 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18239 		    un, un->un_busy_timeout / 2);
18240 		xp->xb_nr_retry_count++;
18241 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18242 		return;
18243 
18244 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18245 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18246 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18247 			    "unit does not respond to selection\n");
18248 		}
18249 		break;
18250 
18251 	case 0x3A:	/* MEDIUM NOT PRESENT */
18252 		if (sd_error_level >= SCSI_ERR_FATAL) {
18253 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18254 			    "Caddy not inserted in drive\n");
18255 		}
18256 
18257 		sr_ejected(un);
18258 		un->un_mediastate = DKIO_EJECTED;
18259 		/* The state has changed, inform the media watch routines */
18260 		cv_broadcast(&un->un_state_cv);
18261 		/* Just fail if no media is present in the drive. */
18262 		goto fail_command;
18263 
18264 	default:
18265 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18266 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18267 			    "Unit not Ready. Additional sense code 0x%x\n",
18268 			    asc);
18269 		}
18270 		break;
18271 	}
18272 
18273 do_retry:
18274 
18275 	/*
18276 	 * Retry the command, as some targets may report NOT READY for
18277 	 * several seconds after being reset.
18278 	 */
18279 	xp->xb_nr_retry_count++;
18280 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18281 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18282 	    &si, EIO, un->un_busy_timeout, NULL);
18283 
18284 	return;
18285 
18286 fail_command:
18287 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18288 	sd_return_failed_command(un, bp, EIO);
18289 }
18290 
18291 
18292 
18293 /*
18294  *    Function: sd_sense_key_medium_or_hardware_error
18295  *
18296  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18297  *		sense key.
18298  *
18299  *     Context: May be called from interrupt context
18300  */
18301 
18302 static void
18303 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18304 	uint8_t *sense_datap,
18305 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18306 {
18307 	struct sd_sense_info	si;
18308 	uint8_t sense_key = scsi_sense_key(sense_datap);
18309 	uint8_t asc = scsi_sense_asc(sense_datap);
18310 
18311 	ASSERT(un != NULL);
18312 	ASSERT(mutex_owned(SD_MUTEX(un)));
18313 	ASSERT(bp != NULL);
18314 	ASSERT(xp != NULL);
18315 	ASSERT(pktp != NULL);
18316 
18317 	si.ssi_severity = SCSI_ERR_FATAL;
18318 	si.ssi_pfa_flag = FALSE;
18319 
18320 	if (sense_key == KEY_MEDIUM_ERROR) {
18321 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18322 	}
18323 
18324 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18325 
18326 	if ((un->un_reset_retry_count != 0) &&
18327 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18328 		mutex_exit(SD_MUTEX(un));
18329 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18330 		if (un->un_f_allow_bus_device_reset == TRUE) {
18331 
18332 			boolean_t try_resetting_target = B_TRUE;
18333 
18334 			/*
18335 			 * We need to be able to handle specific ASC when we are
18336 			 * handling a KEY_HARDWARE_ERROR. In particular
18337 			 * taking the default action of resetting the target may
18338 			 * not be the appropriate way to attempt recovery.
18339 			 * Resetting a target because of a single LUN failure
18340 			 * victimizes all LUNs on that target.
18341 			 *
18342 			 * This is true for the LSI arrays, if an LSI
18343 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18344 			 * should trust it.
18345 			 */
18346 
18347 			if (sense_key == KEY_HARDWARE_ERROR) {
18348 				switch (asc) {
18349 				case 0x84:
18350 					if (SD_IS_LSI(un)) {
18351 						try_resetting_target = B_FALSE;
18352 					}
18353 					break;
18354 				default:
18355 					break;
18356 				}
18357 			}
18358 
18359 			if (try_resetting_target == B_TRUE) {
18360 				int reset_retval = 0;
18361 				if (un->un_f_lun_reset_enabled == TRUE) {
18362 					SD_TRACE(SD_LOG_IO_CORE, un,
18363 					    "sd_sense_key_medium_or_hardware_"
18364 					    "error: issuing RESET_LUN\n");
18365 					reset_retval =
18366 					    scsi_reset(SD_ADDRESS(un),
18367 					    RESET_LUN);
18368 				}
18369 				if (reset_retval == 0) {
18370 					SD_TRACE(SD_LOG_IO_CORE, un,
18371 					    "sd_sense_key_medium_or_hardware_"
18372 					    "error: issuing RESET_TARGET\n");
18373 					(void) scsi_reset(SD_ADDRESS(un),
18374 					    RESET_TARGET);
18375 				}
18376 			}
18377 		}
18378 		mutex_enter(SD_MUTEX(un));
18379 	}
18380 
18381 	/*
18382 	 * This really ought to be a fatal error, but we will retry anyway
18383 	 * as some drives report this as a spurious error.
18384 	 */
18385 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18386 	    &si, EIO, (clock_t)0, NULL);
18387 }
18388 
18389 
18390 
18391 /*
18392  *    Function: sd_sense_key_illegal_request
18393  *
18394  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18395  *
18396  *     Context: May be called from interrupt context
18397  */
18398 
18399 static void
18400 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18401 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18402 {
18403 	struct sd_sense_info	si;
18404 
18405 	ASSERT(un != NULL);
18406 	ASSERT(mutex_owned(SD_MUTEX(un)));
18407 	ASSERT(bp != NULL);
18408 	ASSERT(xp != NULL);
18409 	ASSERT(pktp != NULL);
18410 
18411 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18412 
18413 	si.ssi_severity = SCSI_ERR_INFO;
18414 	si.ssi_pfa_flag = FALSE;
18415 
18416 	/* Pointless to retry if the target thinks it's an illegal request */
18417 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18418 	sd_return_failed_command(un, bp, EIO);
18419 }
18420 
18421 
18422 
18423 
18424 /*
18425  *    Function: sd_sense_key_unit_attention
18426  *
18427  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18428  *
18429  *     Context: May be called from interrupt context
18430  */
18431 
18432 static void
18433 sd_sense_key_unit_attention(struct sd_lun *un,
18434 	uint8_t *sense_datap,
18435 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18436 {
18437 	/*
18438 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18439 	 * like Sonoma can return UNIT ATTENTION close to a minute
18440 	 * under certain conditions.
18441 	 */
18442 	int	retry_check_flag = SD_RETRIES_UA;
18443 	boolean_t	kstat_updated = B_FALSE;
18444 	struct	sd_sense_info		si;
18445 	uint8_t asc = scsi_sense_asc(sense_datap);
18446 	uint8_t	ascq = scsi_sense_ascq(sense_datap);
18447 
18448 	ASSERT(un != NULL);
18449 	ASSERT(mutex_owned(SD_MUTEX(un)));
18450 	ASSERT(bp != NULL);
18451 	ASSERT(xp != NULL);
18452 	ASSERT(pktp != NULL);
18453 
18454 	si.ssi_severity = SCSI_ERR_INFO;
18455 	si.ssi_pfa_flag = FALSE;
18456 
18457 
18458 	switch (asc) {
18459 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18460 		if (sd_report_pfa != 0) {
18461 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18462 			si.ssi_pfa_flag = TRUE;
18463 			retry_check_flag = SD_RETRIES_STANDARD;
18464 			goto do_retry;
18465 		}
18466 
18467 		break;
18468 
18469 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18470 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18471 			un->un_resvd_status |=
18472 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18473 		}
18474 #ifdef _LP64
18475 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18476 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18477 			    un, KM_NOSLEEP) == 0) {
18478 				/*
18479 				 * If we can't dispatch the task we'll just
18480 				 * live without descriptor sense.  We can
18481 				 * try again on the next "unit attention"
18482 				 */
18483 				SD_ERROR(SD_LOG_ERROR, un,
18484 				    "sd_sense_key_unit_attention: "
18485 				    "Could not dispatch "
18486 				    "sd_reenable_dsense_task\n");
18487 			}
18488 		}
18489 #endif /* _LP64 */
18490 		/* FALLTHRU */
18491 
18492 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18493 		if (!un->un_f_has_removable_media) {
18494 			break;
18495 		}
18496 
18497 		/*
18498 		 * When we get a unit attention from a removable-media device,
18499 		 * it may be in a state that will take a long time to recover
18500 		 * (e.g., from a reset).  Since we are executing in interrupt
18501 		 * context here, we cannot wait around for the device to come
18502 		 * back. So hand this command off to sd_media_change_task()
18503 		 * for deferred processing under taskq thread context. (Note
18504 		 * that the command still may be failed if a problem is
18505 		 * encountered at a later time.)
18506 		 */
18507 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18508 		    KM_NOSLEEP) == 0) {
18509 			/*
18510 			 * Cannot dispatch the request so fail the command.
18511 			 */
18512 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18513 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18514 			si.ssi_severity = SCSI_ERR_FATAL;
18515 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18516 			sd_return_failed_command(un, bp, EIO);
18517 		}
18518 
18519 		/*
18520 		 * If failed to dispatch sd_media_change_task(), we already
18521 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18522 		 * we should update kstat later if it encounters an error. So,
18523 		 * we update kstat_updated flag here.
18524 		 */
18525 		kstat_updated = B_TRUE;
18526 
18527 		/*
18528 		 * Either the command has been successfully dispatched to a
18529 		 * task Q for retrying, or the dispatch failed. In either case
18530 		 * do NOT retry again by calling sd_retry_command. This sets up
18531 		 * two retries of the same command and when one completes and
18532 		 * frees the resources the other will access freed memory,
18533 		 * a bad thing.
18534 		 */
18535 		return;
18536 
18537 	default:
18538 		break;
18539 	}
18540 
18541 	/*
18542 	 * ASC  ASCQ
18543 	 *  2A   09	Capacity data has changed
18544 	 *  2A   01	Mode parameters changed
18545 	 *  3F   0E	Reported luns data has changed
18546 	 * Arrays that support logical unit expansion should report
18547 	 * capacity changes(2Ah/09). Mode parameters changed and
18548 	 * reported luns data has changed are the approximation.
18549 	 */
18550 	if (((asc == 0x2a) && (ascq == 0x09)) ||
18551 	    ((asc == 0x2a) && (ascq == 0x01)) ||
18552 	    ((asc == 0x3f) && (ascq == 0x0e))) {
18553 		if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18554 		    KM_NOSLEEP) == 0) {
18555 			SD_ERROR(SD_LOG_ERROR, un,
18556 			    "sd_sense_key_unit_attention: "
18557 			    "Could not dispatch sd_target_change_task\n");
18558 		}
18559 	}
18560 
18561 	/*
18562 	 * Update kstat if we haven't done that.
18563 	 */
18564 	if (!kstat_updated) {
18565 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18566 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18567 	}
18568 
18569 do_retry:
18570 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18571 	    EIO, SD_UA_RETRY_DELAY, NULL);
18572 }
18573 
18574 
18575 
18576 /*
18577  *    Function: sd_sense_key_fail_command
18578  *
18579  * Description: Use to fail a command when we don't like the sense key that
18580  *		was returned.
18581  *
18582  *     Context: May be called from interrupt context
18583  */
18584 
18585 static void
18586 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18587 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18588 {
18589 	struct sd_sense_info	si;
18590 
18591 	ASSERT(un != NULL);
18592 	ASSERT(mutex_owned(SD_MUTEX(un)));
18593 	ASSERT(bp != NULL);
18594 	ASSERT(xp != NULL);
18595 	ASSERT(pktp != NULL);
18596 
18597 	si.ssi_severity = SCSI_ERR_FATAL;
18598 	si.ssi_pfa_flag = FALSE;
18599 
18600 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18601 	sd_return_failed_command(un, bp, EIO);
18602 }
18603 
18604 
18605 
18606 /*
18607  *    Function: sd_sense_key_blank_check
18608  *
18609  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18610  *		Has no monetary connotation.
18611  *
18612  *     Context: May be called from interrupt context
18613  */
18614 
18615 static void
18616 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18617 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18618 {
18619 	struct sd_sense_info	si;
18620 
18621 	ASSERT(un != NULL);
18622 	ASSERT(mutex_owned(SD_MUTEX(un)));
18623 	ASSERT(bp != NULL);
18624 	ASSERT(xp != NULL);
18625 	ASSERT(pktp != NULL);
18626 
18627 	/*
18628 	 * Blank check is not fatal for removable devices, therefore
18629 	 * it does not require a console message.
18630 	 */
18631 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18632 	    SCSI_ERR_FATAL;
18633 	si.ssi_pfa_flag = FALSE;
18634 
18635 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18636 	sd_return_failed_command(un, bp, EIO);
18637 }
18638 
18639 
18640 
18641 
18642 /*
18643  *    Function: sd_sense_key_aborted_command
18644  *
18645  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18646  *
18647  *     Context: May be called from interrupt context
18648  */
18649 
18650 static void
18651 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18652 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18653 {
18654 	struct sd_sense_info	si;
18655 
18656 	ASSERT(un != NULL);
18657 	ASSERT(mutex_owned(SD_MUTEX(un)));
18658 	ASSERT(bp != NULL);
18659 	ASSERT(xp != NULL);
18660 	ASSERT(pktp != NULL);
18661 
18662 	si.ssi_severity = SCSI_ERR_FATAL;
18663 	si.ssi_pfa_flag = FALSE;
18664 
18665 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18666 
18667 	/*
18668 	 * This really ought to be a fatal error, but we will retry anyway
18669 	 * as some drives report this as a spurious error.
18670 	 */
18671 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18672 	    &si, EIO, drv_usectohz(100000), NULL);
18673 }
18674 
18675 
18676 
18677 /*
18678  *    Function: sd_sense_key_default
18679  *
18680  * Description: Default recovery action for several SCSI sense keys (basically
18681  *		attempts a retry).
18682  *
18683  *     Context: May be called from interrupt context
18684  */
18685 
18686 static void
18687 sd_sense_key_default(struct sd_lun *un,
18688 	uint8_t *sense_datap,
18689 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18690 {
18691 	struct sd_sense_info	si;
18692 	uint8_t sense_key = scsi_sense_key(sense_datap);
18693 
18694 	ASSERT(un != NULL);
18695 	ASSERT(mutex_owned(SD_MUTEX(un)));
18696 	ASSERT(bp != NULL);
18697 	ASSERT(xp != NULL);
18698 	ASSERT(pktp != NULL);
18699 
18700 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18701 
18702 	/*
18703 	 * Undecoded sense key.	Attempt retries and hope that will fix
18704 	 * the problem.  Otherwise, we're dead.
18705 	 */
18706 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18707 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18708 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18709 	}
18710 
18711 	si.ssi_severity = SCSI_ERR_FATAL;
18712 	si.ssi_pfa_flag = FALSE;
18713 
18714 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18715 	    &si, EIO, (clock_t)0, NULL);
18716 }
18717 
18718 
18719 
18720 /*
18721  *    Function: sd_print_retry_msg
18722  *
18723  * Description: Print a message indicating the retry action being taken.
18724  *
18725  *   Arguments: un - ptr to associated softstate
18726  *		bp - ptr to buf(9S) for the command
18727  *		arg - not used.
18728  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18729  *			or SD_NO_RETRY_ISSUED
18730  *
18731  *     Context: May be called from interrupt context
18732  */
18733 /* ARGSUSED */
18734 static void
18735 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18736 {
18737 	struct sd_xbuf	*xp;
18738 	struct scsi_pkt *pktp;
18739 	char *reasonp;
18740 	char *msgp;
18741 
18742 	ASSERT(un != NULL);
18743 	ASSERT(mutex_owned(SD_MUTEX(un)));
18744 	ASSERT(bp != NULL);
18745 	pktp = SD_GET_PKTP(bp);
18746 	ASSERT(pktp != NULL);
18747 	xp = SD_GET_XBUF(bp);
18748 	ASSERT(xp != NULL);
18749 
18750 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18751 	mutex_enter(&un->un_pm_mutex);
18752 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18753 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18754 	    (pktp->pkt_flags & FLAG_SILENT)) {
18755 		mutex_exit(&un->un_pm_mutex);
18756 		goto update_pkt_reason;
18757 	}
18758 	mutex_exit(&un->un_pm_mutex);
18759 
18760 	/*
18761 	 * Suppress messages if they are all the same pkt_reason; with
18762 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18763 	 * If we are in panic, then suppress the retry messages.
18764 	 */
18765 	switch (flag) {
18766 	case SD_NO_RETRY_ISSUED:
18767 		msgp = "giving up";
18768 		break;
18769 	case SD_IMMEDIATE_RETRY_ISSUED:
18770 	case SD_DELAYED_RETRY_ISSUED:
18771 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18772 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18773 		    (sd_error_level != SCSI_ERR_ALL))) {
18774 			return;
18775 		}
18776 		msgp = "retrying command";
18777 		break;
18778 	default:
18779 		goto update_pkt_reason;
18780 	}
18781 
18782 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18783 	    scsi_rname(pktp->pkt_reason));
18784 
18785 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18786 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18787 		    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18788 	}
18789 
18790 update_pkt_reason:
18791 	/*
18792 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18793 	 * This is to prevent multiple console messages for the same failure
18794 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18795 	 * when the command is retried successfully because there still may be
18796 	 * more commands coming back with the same value of pktp->pkt_reason.
18797 	 */
18798 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18799 		un->un_last_pkt_reason = pktp->pkt_reason;
18800 	}
18801 }
18802 
18803 
18804 /*
18805  *    Function: sd_print_cmd_incomplete_msg
18806  *
18807  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18808  *
18809  *   Arguments: un - ptr to associated softstate
18810  *		bp - ptr to buf(9S) for the command
18811  *		arg - passed to sd_print_retry_msg()
18812  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18813  *			or SD_NO_RETRY_ISSUED
18814  *
18815  *     Context: May be called from interrupt context
18816  */
18817 
18818 static void
18819 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18820 	int code)
18821 {
18822 	dev_info_t	*dip;
18823 
18824 	ASSERT(un != NULL);
18825 	ASSERT(mutex_owned(SD_MUTEX(un)));
18826 	ASSERT(bp != NULL);
18827 
18828 	switch (code) {
18829 	case SD_NO_RETRY_ISSUED:
18830 		/* Command was failed. Someone turned off this target? */
18831 		if (un->un_state != SD_STATE_OFFLINE) {
18832 			/*
18833 			 * Suppress message if we are detaching and
18834 			 * device has been disconnected
18835 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18836 			 * private interface and not part of the DDI
18837 			 */
18838 			dip = un->un_sd->sd_dev;
18839 			if (!(DEVI_IS_DETACHING(dip) &&
18840 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18841 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18842 				"disk not responding to selection\n");
18843 			}
18844 			New_state(un, SD_STATE_OFFLINE);
18845 		}
18846 		break;
18847 
18848 	case SD_DELAYED_RETRY_ISSUED:
18849 	case SD_IMMEDIATE_RETRY_ISSUED:
18850 	default:
18851 		/* Command was successfully queued for retry */
18852 		sd_print_retry_msg(un, bp, arg, code);
18853 		break;
18854 	}
18855 }
18856 
18857 
18858 /*
18859  *    Function: sd_pkt_reason_cmd_incomplete
18860  *
18861  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18862  *
18863  *     Context: May be called from interrupt context
18864  */
18865 
18866 static void
18867 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18868 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18869 {
18870 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18871 
18872 	ASSERT(un != NULL);
18873 	ASSERT(mutex_owned(SD_MUTEX(un)));
18874 	ASSERT(bp != NULL);
18875 	ASSERT(xp != NULL);
18876 	ASSERT(pktp != NULL);
18877 
18878 	/* Do not do a reset if selection did not complete */
18879 	/* Note: Should this not just check the bit? */
18880 	if (pktp->pkt_state != STATE_GOT_BUS) {
18881 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18882 		sd_reset_target(un, pktp);
18883 	}
18884 
18885 	/*
18886 	 * If the target was not successfully selected, then set
18887 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18888 	 * with the target, and further retries and/or commands are
18889 	 * likely to take a long time.
18890 	 */
18891 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18892 		flag |= SD_RETRIES_FAILFAST;
18893 	}
18894 
18895 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18896 
18897 	sd_retry_command(un, bp, flag,
18898 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18899 }
18900 
18901 
18902 
18903 /*
18904  *    Function: sd_pkt_reason_cmd_tran_err
18905  *
18906  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18907  *
18908  *     Context: May be called from interrupt context
18909  */
18910 
18911 static void
18912 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18913 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18914 {
18915 	ASSERT(un != NULL);
18916 	ASSERT(mutex_owned(SD_MUTEX(un)));
18917 	ASSERT(bp != NULL);
18918 	ASSERT(xp != NULL);
18919 	ASSERT(pktp != NULL);
18920 
18921 	/*
18922 	 * Do not reset if we got a parity error, or if
18923 	 * selection did not complete.
18924 	 */
18925 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18926 	/* Note: Should this not just check the bit for pkt_state? */
18927 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18928 	    (pktp->pkt_state != STATE_GOT_BUS)) {
18929 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18930 		sd_reset_target(un, pktp);
18931 	}
18932 
18933 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18934 
18935 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18936 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18937 }
18938 
18939 
18940 
18941 /*
18942  *    Function: sd_pkt_reason_cmd_reset
18943  *
18944  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18945  *
18946  *     Context: May be called from interrupt context
18947  */
18948 
18949 static void
18950 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
18951 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18952 {
18953 	ASSERT(un != NULL);
18954 	ASSERT(mutex_owned(SD_MUTEX(un)));
18955 	ASSERT(bp != NULL);
18956 	ASSERT(xp != NULL);
18957 	ASSERT(pktp != NULL);
18958 
18959 	/* The target may still be running the command, so try to reset. */
18960 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18961 	sd_reset_target(un, pktp);
18962 
18963 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18964 
18965 	/*
18966 	 * If pkt_reason is CMD_RESET chances are that this pkt got
18967 	 * reset because another target on this bus caused it. The target
18968 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18969 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18970 	 */
18971 
18972 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18973 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18974 }
18975 
18976 
18977 
18978 
18979 /*
18980  *    Function: sd_pkt_reason_cmd_aborted
18981  *
18982  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18983  *
18984  *     Context: May be called from interrupt context
18985  */
18986 
18987 static void
18988 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
18989 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18990 {
18991 	ASSERT(un != NULL);
18992 	ASSERT(mutex_owned(SD_MUTEX(un)));
18993 	ASSERT(bp != NULL);
18994 	ASSERT(xp != NULL);
18995 	ASSERT(pktp != NULL);
18996 
18997 	/* The target may still be running the command, so try to reset. */
18998 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18999 	sd_reset_target(un, pktp);
19000 
19001 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19002 
19003 	/*
19004 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19005 	 * aborted because another target on this bus caused it. The target
19006 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19007 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19008 	 */
19009 
19010 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19011 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19012 }
19013 
19014 
19015 
19016 /*
19017  *    Function: sd_pkt_reason_cmd_timeout
19018  *
19019  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19020  *
19021  *     Context: May be called from interrupt context
19022  */
19023 
19024 static void
19025 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19026 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19027 {
19028 	ASSERT(un != NULL);
19029 	ASSERT(mutex_owned(SD_MUTEX(un)));
19030 	ASSERT(bp != NULL);
19031 	ASSERT(xp != NULL);
19032 	ASSERT(pktp != NULL);
19033 
19034 
19035 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19036 	sd_reset_target(un, pktp);
19037 
19038 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19039 
19040 	/*
19041 	 * A command timeout indicates that we could not establish
19042 	 * communication with the target, so set SD_RETRIES_FAILFAST
19043 	 * as further retries/commands are likely to take a long time.
19044 	 */
19045 	sd_retry_command(un, bp,
19046 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19047 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19048 }
19049 
19050 
19051 
19052 /*
19053  *    Function: sd_pkt_reason_cmd_unx_bus_free
19054  *
19055  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19056  *
19057  *     Context: May be called from interrupt context
19058  */
19059 
19060 static void
19061 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19062 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19063 {
19064 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19065 
19066 	ASSERT(un != NULL);
19067 	ASSERT(mutex_owned(SD_MUTEX(un)));
19068 	ASSERT(bp != NULL);
19069 	ASSERT(xp != NULL);
19070 	ASSERT(pktp != NULL);
19071 
19072 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19073 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19074 
19075 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19076 	    sd_print_retry_msg : NULL;
19077 
19078 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19079 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19080 }
19081 
19082 
19083 /*
19084  *    Function: sd_pkt_reason_cmd_tag_reject
19085  *
19086  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19087  *
19088  *     Context: May be called from interrupt context
19089  */
19090 
19091 static void
19092 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19093 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19094 {
19095 	ASSERT(un != NULL);
19096 	ASSERT(mutex_owned(SD_MUTEX(un)));
19097 	ASSERT(bp != NULL);
19098 	ASSERT(xp != NULL);
19099 	ASSERT(pktp != NULL);
19100 
19101 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19102 	pktp->pkt_flags = 0;
19103 	un->un_tagflags = 0;
19104 	if (un->un_f_opt_queueing == TRUE) {
19105 		un->un_throttle = min(un->un_throttle, 3);
19106 	} else {
19107 		un->un_throttle = 1;
19108 	}
19109 	mutex_exit(SD_MUTEX(un));
19110 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19111 	mutex_enter(SD_MUTEX(un));
19112 
19113 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19114 
19115 	/* Legacy behavior not to check retry counts here. */
19116 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19117 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19118 }
19119 
19120 
19121 /*
19122  *    Function: sd_pkt_reason_default
19123  *
19124  * Description: Default recovery actions for SCSA pkt_reason values that
19125  *		do not have more explicit recovery actions.
19126  *
19127  *     Context: May be called from interrupt context
19128  */
19129 
19130 static void
19131 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19132 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19133 {
19134 	ASSERT(un != NULL);
19135 	ASSERT(mutex_owned(SD_MUTEX(un)));
19136 	ASSERT(bp != NULL);
19137 	ASSERT(xp != NULL);
19138 	ASSERT(pktp != NULL);
19139 
19140 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19141 	sd_reset_target(un, pktp);
19142 
19143 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19144 
19145 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19146 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19147 }
19148 
19149 
19150 
19151 /*
19152  *    Function: sd_pkt_status_check_condition
19153  *
19154  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19155  *
19156  *     Context: May be called from interrupt context
19157  */
19158 
19159 static void
19160 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19161 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19162 {
19163 	ASSERT(un != NULL);
19164 	ASSERT(mutex_owned(SD_MUTEX(un)));
19165 	ASSERT(bp != NULL);
19166 	ASSERT(xp != NULL);
19167 	ASSERT(pktp != NULL);
19168 
19169 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19170 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
19171 
19172 	/*
19173 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19174 	 * command will be retried after the request sense). Otherwise, retry
19175 	 * the command. Note: we are issuing the request sense even though the
19176 	 * retry limit may have been reached for the failed command.
19177 	 */
19178 	if (un->un_f_arq_enabled == FALSE) {
19179 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19180 		    "no ARQ, sending request sense command\n");
19181 		sd_send_request_sense_command(un, bp, pktp);
19182 	} else {
19183 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19184 		    "ARQ,retrying request sense command\n");
19185 #if defined(__i386) || defined(__amd64)
19186 		/*
19187 		 * The SD_RETRY_DELAY value need to be adjusted here
19188 		 * when SD_RETRY_DELAY change in sddef.h
19189 		 */
19190 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19191 		    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19192 		    NULL);
19193 #else
19194 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19195 		    EIO, SD_RETRY_DELAY, NULL);
19196 #endif
19197 	}
19198 
19199 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19200 }
19201 
19202 
19203 /*
19204  *    Function: sd_pkt_status_busy
19205  *
19206  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19207  *
19208  *     Context: May be called from interrupt context
19209  */
19210 
19211 static void
19212 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19213 	struct scsi_pkt *pktp)
19214 {
19215 	ASSERT(un != NULL);
19216 	ASSERT(mutex_owned(SD_MUTEX(un)));
19217 	ASSERT(bp != NULL);
19218 	ASSERT(xp != NULL);
19219 	ASSERT(pktp != NULL);
19220 
19221 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19222 	    "sd_pkt_status_busy: entry\n");
19223 
19224 	/* If retries are exhausted, just fail the command. */
19225 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
19226 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19227 		    "device busy too long\n");
19228 		sd_return_failed_command(un, bp, EIO);
19229 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19230 		    "sd_pkt_status_busy: exit\n");
19231 		return;
19232 	}
19233 	xp->xb_retry_count++;
19234 
19235 	/*
19236 	 * Try to reset the target. However, we do not want to perform
19237 	 * more than one reset if the device continues to fail. The reset
19238 	 * will be performed when the retry count reaches the reset
19239 	 * threshold.  This threshold should be set such that at least
19240 	 * one retry is issued before the reset is performed.
19241 	 */
19242 	if (xp->xb_retry_count ==
19243 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19244 		int rval = 0;
19245 		mutex_exit(SD_MUTEX(un));
19246 		if (un->un_f_allow_bus_device_reset == TRUE) {
19247 			/*
19248 			 * First try to reset the LUN; if we cannot then
19249 			 * try to reset the target.
19250 			 */
19251 			if (un->un_f_lun_reset_enabled == TRUE) {
19252 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19253 				    "sd_pkt_status_busy: RESET_LUN\n");
19254 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19255 			}
19256 			if (rval == 0) {
19257 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19258 				    "sd_pkt_status_busy: RESET_TARGET\n");
19259 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19260 			}
19261 		}
19262 		if (rval == 0) {
19263 			/*
19264 			 * If the RESET_LUN and/or RESET_TARGET failed,
19265 			 * try RESET_ALL
19266 			 */
19267 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19268 			    "sd_pkt_status_busy: RESET_ALL\n");
19269 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19270 		}
19271 		mutex_enter(SD_MUTEX(un));
19272 		if (rval == 0) {
19273 			/*
19274 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19275 			 * At this point we give up & fail the command.
19276 			 */
19277 			sd_return_failed_command(un, bp, EIO);
19278 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19279 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19280 			return;
19281 		}
19282 	}
19283 
19284 	/*
19285 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19286 	 * we have already checked the retry counts above.
19287 	 */
19288 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19289 	    EIO, un->un_busy_timeout, NULL);
19290 
19291 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19292 	    "sd_pkt_status_busy: exit\n");
19293 }
19294 
19295 
19296 /*
19297  *    Function: sd_pkt_status_reservation_conflict
19298  *
19299  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19300  *		command status.
19301  *
19302  *     Context: May be called from interrupt context
19303  */
19304 
19305 static void
19306 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19307 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19308 {
19309 	ASSERT(un != NULL);
19310 	ASSERT(mutex_owned(SD_MUTEX(un)));
19311 	ASSERT(bp != NULL);
19312 	ASSERT(xp != NULL);
19313 	ASSERT(pktp != NULL);
19314 
19315 	/*
19316 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19317 	 * conflict could be due to various reasons like incorrect keys, not
19318 	 * registered or not reserved etc. So, we return EACCES to the caller.
19319 	 */
19320 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19321 		int cmd = SD_GET_PKT_OPCODE(pktp);
19322 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19323 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19324 			sd_return_failed_command(un, bp, EACCES);
19325 			return;
19326 		}
19327 	}
19328 
19329 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19330 
19331 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19332 		if (sd_failfast_enable != 0) {
19333 			/* By definition, we must panic here.... */
19334 			sd_panic_for_res_conflict(un);
19335 			/*NOTREACHED*/
19336 		}
19337 		SD_ERROR(SD_LOG_IO, un,
19338 		    "sd_handle_resv_conflict: Disk Reserved\n");
19339 		sd_return_failed_command(un, bp, EACCES);
19340 		return;
19341 	}
19342 
19343 	/*
19344 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19345 	 * property is set (default is 1). Retries will not succeed
19346 	 * on a disk reserved by another initiator. HA systems
19347 	 * may reset this via sd.conf to avoid these retries.
19348 	 *
19349 	 * Note: The legacy return code for this failure is EIO, however EACCES
19350 	 * seems more appropriate for a reservation conflict.
19351 	 */
19352 	if (sd_retry_on_reservation_conflict == 0) {
19353 		SD_ERROR(SD_LOG_IO, un,
19354 		    "sd_handle_resv_conflict: Device Reserved\n");
19355 		sd_return_failed_command(un, bp, EIO);
19356 		return;
19357 	}
19358 
19359 	/*
19360 	 * Retry the command if we can.
19361 	 *
19362 	 * Note: The legacy return code for this failure is EIO, however EACCES
19363 	 * seems more appropriate for a reservation conflict.
19364 	 */
19365 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19366 	    (clock_t)2, NULL);
19367 }
19368 
19369 
19370 
19371 /*
19372  *    Function: sd_pkt_status_qfull
19373  *
19374  * Description: Handle a QUEUE FULL condition from the target.  This can
19375  *		occur if the HBA does not handle the queue full condition.
19376  *		(Basically this means third-party HBAs as Sun HBAs will
19377  *		handle the queue full condition.)  Note that if there are
19378  *		some commands already in the transport, then the queue full
19379  *		has occurred because the queue for this nexus is actually
19380  *		full. If there are no commands in the transport, then the
19381  *		queue full is resulting from some other initiator or lun
19382  *		consuming all the resources at the target.
19383  *
19384  *     Context: May be called from interrupt context
19385  */
19386 
19387 static void
19388 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19389 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19390 {
19391 	ASSERT(un != NULL);
19392 	ASSERT(mutex_owned(SD_MUTEX(un)));
19393 	ASSERT(bp != NULL);
19394 	ASSERT(xp != NULL);
19395 	ASSERT(pktp != NULL);
19396 
19397 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19398 	    "sd_pkt_status_qfull: entry\n");
19399 
19400 	/*
19401 	 * Just lower the QFULL throttle and retry the command.  Note that
19402 	 * we do not limit the number of retries here.
19403 	 */
19404 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19405 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19406 	    SD_RESTART_TIMEOUT, NULL);
19407 
19408 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19409 	    "sd_pkt_status_qfull: exit\n");
19410 }
19411 
19412 
19413 /*
19414  *    Function: sd_reset_target
19415  *
19416  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19417  *		RESET_TARGET, or RESET_ALL.
19418  *
19419  *     Context: May be called under interrupt context.
19420  */
19421 
19422 static void
19423 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19424 {
19425 	int rval = 0;
19426 
19427 	ASSERT(un != NULL);
19428 	ASSERT(mutex_owned(SD_MUTEX(un)));
19429 	ASSERT(pktp != NULL);
19430 
19431 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19432 
19433 	/*
19434 	 * No need to reset if the transport layer has already done so.
19435 	 */
19436 	if ((pktp->pkt_statistics &
19437 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19438 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19439 		    "sd_reset_target: no reset\n");
19440 		return;
19441 	}
19442 
19443 	mutex_exit(SD_MUTEX(un));
19444 
19445 	if (un->un_f_allow_bus_device_reset == TRUE) {
19446 		if (un->un_f_lun_reset_enabled == TRUE) {
19447 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19448 			    "sd_reset_target: RESET_LUN\n");
19449 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19450 		}
19451 		if (rval == 0) {
19452 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19453 			    "sd_reset_target: RESET_TARGET\n");
19454 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19455 		}
19456 	}
19457 
19458 	if (rval == 0) {
19459 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19460 		    "sd_reset_target: RESET_ALL\n");
19461 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19462 	}
19463 
19464 	mutex_enter(SD_MUTEX(un));
19465 
19466 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19467 }
19468 
19469 /*
19470  *    Function: sd_target_change_task
19471  *
19472  * Description: Handle dynamic target change
19473  *
19474  *     Context: Executes in a taskq() thread context
19475  */
19476 static void
19477 sd_target_change_task(void *arg)
19478 {
19479 	struct sd_lun		*un = arg;
19480 	uint64_t		capacity;
19481 	diskaddr_t		label_cap;
19482 	uint_t			lbasize;
19483 	sd_ssc_t		*ssc;
19484 
19485 	ASSERT(un != NULL);
19486 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19487 
19488 	if ((un->un_f_blockcount_is_valid == FALSE) ||
19489 	    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19490 		return;
19491 	}
19492 
19493 	ssc = sd_ssc_init(un);
19494 
19495 	if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19496 	    &lbasize, SD_PATH_DIRECT) != 0) {
19497 		SD_ERROR(SD_LOG_ERROR, un,
19498 		    "sd_target_change_task: fail to read capacity\n");
19499 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19500 		goto task_exit;
19501 	}
19502 
19503 	mutex_enter(SD_MUTEX(un));
19504 	if (capacity <= un->un_blockcount) {
19505 		mutex_exit(SD_MUTEX(un));
19506 		goto task_exit;
19507 	}
19508 
19509 	sd_update_block_info(un, lbasize, capacity);
19510 	mutex_exit(SD_MUTEX(un));
19511 
19512 	/*
19513 	 * If lun is EFI labeled and lun capacity is greater than the
19514 	 * capacity contained in the label, log a sys event.
19515 	 */
19516 	if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19517 	    (void*)SD_PATH_DIRECT) == 0) {
19518 		mutex_enter(SD_MUTEX(un));
19519 		if (un->un_f_blockcount_is_valid &&
19520 		    un->un_blockcount > label_cap) {
19521 			mutex_exit(SD_MUTEX(un));
19522 			sd_log_lun_expansion_event(un, KM_SLEEP);
19523 		} else {
19524 			mutex_exit(SD_MUTEX(un));
19525 		}
19526 	}
19527 
19528 task_exit:
19529 	sd_ssc_fini(ssc);
19530 }
19531 
19532 
19533 /*
19534  *    Function: sd_log_dev_status_event
19535  *
19536  * Description: Log EC_dev_status sysevent
19537  *
19538  *     Context: Never called from interrupt context
19539  */
19540 static void
19541 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19542 {
19543 	int err;
19544 	char			*path;
19545 	nvlist_t		*attr_list;
19546 
19547 	/* Allocate and build sysevent attribute list */
19548 	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19549 	if (err != 0) {
19550 		SD_ERROR(SD_LOG_ERROR, un,
19551 		    "sd_log_dev_status_event: fail to allocate space\n");
19552 		return;
19553 	}
19554 
19555 	path = kmem_alloc(MAXPATHLEN, km_flag);
19556 	if (path == NULL) {
19557 		nvlist_free(attr_list);
19558 		SD_ERROR(SD_LOG_ERROR, un,
19559 		    "sd_log_dev_status_event: fail to allocate space\n");
19560 		return;
19561 	}
19562 	/*
19563 	 * Add path attribute to identify the lun.
19564 	 * We are using minor node 'a' as the sysevent attribute.
19565 	 */
19566 	(void) snprintf(path, MAXPATHLEN, "/devices");
19567 	(void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19568 	(void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19569 	    ":a");
19570 
19571 	err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19572 	if (err != 0) {
19573 		nvlist_free(attr_list);
19574 		kmem_free(path, MAXPATHLEN);
19575 		SD_ERROR(SD_LOG_ERROR, un,
19576 		    "sd_log_dev_status_event: fail to add attribute\n");
19577 		return;
19578 	}
19579 
19580 	/* Log dynamic lun expansion sysevent */
19581 	err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19582 	    esc, attr_list, NULL, km_flag);
19583 	if (err != DDI_SUCCESS) {
19584 		SD_ERROR(SD_LOG_ERROR, un,
19585 		    "sd_log_dev_status_event: fail to log sysevent\n");
19586 	}
19587 
19588 	nvlist_free(attr_list);
19589 	kmem_free(path, MAXPATHLEN);
19590 }
19591 
19592 
19593 /*
19594  *    Function: sd_log_lun_expansion_event
19595  *
19596  * Description: Log lun expansion sys event
19597  *
19598  *     Context: Never called from interrupt context
19599  */
19600 static void
19601 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19602 {
19603 	sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19604 }
19605 
19606 
19607 /*
19608  *    Function: sd_log_eject_request_event
19609  *
19610  * Description: Log eject request sysevent
19611  *
19612  *     Context: Never called from interrupt context
19613  */
19614 static void
19615 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19616 {
19617 	sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19618 }
19619 
19620 
19621 /*
19622  *    Function: sd_media_change_task
19623  *
19624  * Description: Recovery action for CDROM to become available.
19625  *
19626  *     Context: Executes in a taskq() thread context
19627  */
19628 
19629 static void
19630 sd_media_change_task(void *arg)
19631 {
19632 	struct	scsi_pkt	*pktp = arg;
19633 	struct	sd_lun		*un;
19634 	struct	buf		*bp;
19635 	struct	sd_xbuf		*xp;
19636 	int	err		= 0;
19637 	int	retry_count	= 0;
19638 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19639 	struct	sd_sense_info	si;
19640 
19641 	ASSERT(pktp != NULL);
19642 	bp = (struct buf *)pktp->pkt_private;
19643 	ASSERT(bp != NULL);
19644 	xp = SD_GET_XBUF(bp);
19645 	ASSERT(xp != NULL);
19646 	un = SD_GET_UN(bp);
19647 	ASSERT(un != NULL);
19648 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19649 	ASSERT(un->un_f_monitor_media_state);
19650 
19651 	si.ssi_severity = SCSI_ERR_INFO;
19652 	si.ssi_pfa_flag = FALSE;
19653 
19654 	/*
19655 	 * When a reset is issued on a CDROM, it takes a long time to
19656 	 * recover. First few attempts to read capacity and other things
19657 	 * related to handling unit attention fail (with a ASC 0x4 and
19658 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19659 	 * to limit the retries in other cases of genuine failures like
19660 	 * no media in drive.
19661 	 */
19662 	while (retry_count++ < retry_limit) {
19663 		if ((err = sd_handle_mchange(un)) == 0) {
19664 			break;
19665 		}
19666 		if (err == EAGAIN) {
19667 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19668 		}
19669 		/* Sleep for 0.5 sec. & try again */
19670 		delay(drv_usectohz(500000));
19671 	}
19672 
19673 	/*
19674 	 * Dispatch (retry or fail) the original command here,
19675 	 * along with appropriate console messages....
19676 	 *
19677 	 * Must grab the mutex before calling sd_retry_command,
19678 	 * sd_print_sense_msg and sd_return_failed_command.
19679 	 */
19680 	mutex_enter(SD_MUTEX(un));
19681 	if (err != SD_CMD_SUCCESS) {
19682 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19683 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19684 		si.ssi_severity = SCSI_ERR_FATAL;
19685 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19686 		sd_return_failed_command(un, bp, EIO);
19687 	} else {
19688 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
19689 		    &si, EIO, (clock_t)0, NULL);
19690 	}
19691 	mutex_exit(SD_MUTEX(un));
19692 }
19693 
19694 
19695 
19696 /*
19697  *    Function: sd_handle_mchange
19698  *
19699  * Description: Perform geometry validation & other recovery when CDROM
19700  *		has been removed from drive.
19701  *
19702  * Return Code: 0 for success
19703  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19704  *		sd_send_scsi_READ_CAPACITY()
19705  *
19706  *     Context: Executes in a taskq() thread context
19707  */
19708 
19709 static int
19710 sd_handle_mchange(struct sd_lun *un)
19711 {
19712 	uint64_t	capacity;
19713 	uint32_t	lbasize;
19714 	int		rval;
19715 	sd_ssc_t	*ssc;
19716 
19717 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19718 	ASSERT(un->un_f_monitor_media_state);
19719 
19720 	ssc = sd_ssc_init(un);
19721 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19722 	    SD_PATH_DIRECT_PRIORITY);
19723 
19724 	if (rval != 0)
19725 		goto failed;
19726 
19727 	mutex_enter(SD_MUTEX(un));
19728 	sd_update_block_info(un, lbasize, capacity);
19729 
19730 	if (un->un_errstats != NULL) {
19731 		struct	sd_errstats *stp =
19732 		    (struct sd_errstats *)un->un_errstats->ks_data;
19733 		stp->sd_capacity.value.ui64 = (uint64_t)
19734 		    ((uint64_t)un->un_blockcount *
19735 		    (uint64_t)un->un_tgt_blocksize);
19736 	}
19737 
19738 	/*
19739 	 * Check if the media in the device is writable or not
19740 	 */
19741 	if (ISCD(un)) {
19742 		sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19743 	}
19744 
19745 	/*
19746 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19747 	 * valid geometry.
19748 	 */
19749 	mutex_exit(SD_MUTEX(un));
19750 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19751 
19752 
19753 	if (cmlb_validate(un->un_cmlbhandle, 0,
19754 	    (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19755 		sd_ssc_fini(ssc);
19756 		return (EIO);
19757 	} else {
19758 		if (un->un_f_pkstats_enabled) {
19759 			sd_set_pstats(un);
19760 			SD_TRACE(SD_LOG_IO_PARTITION, un,
19761 			    "sd_handle_mchange: un:0x%p pstats created and "
19762 			    "set\n", un);
19763 		}
19764 	}
19765 
19766 	/*
19767 	 * Try to lock the door
19768 	 */
19769 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19770 	    SD_PATH_DIRECT_PRIORITY);
19771 failed:
19772 	if (rval != 0)
19773 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19774 	sd_ssc_fini(ssc);
19775 	return (rval);
19776 }
19777 
19778 
19779 /*
19780  *    Function: sd_send_scsi_DOORLOCK
19781  *
19782  * Description: Issue the scsi DOOR LOCK command
19783  *
19784  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19785  *                      structure for this target.
19786  *		flag  - SD_REMOVAL_ALLOW
19787  *			SD_REMOVAL_PREVENT
19788  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19789  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19790  *			to use the USCSI "direct" chain and bypass the normal
19791  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19792  *			command is issued as part of an error recovery action.
19793  *
19794  * Return Code: 0   - Success
19795  *		errno return code from sd_ssc_send()
19796  *
19797  *     Context: Can sleep.
19798  */
19799 
19800 static int
19801 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19802 {
19803 	struct scsi_extended_sense	sense_buf;
19804 	union scsi_cdb		cdb;
19805 	struct uscsi_cmd	ucmd_buf;
19806 	int			status;
19807 	struct sd_lun		*un;
19808 
19809 	ASSERT(ssc != NULL);
19810 	un = ssc->ssc_un;
19811 	ASSERT(un != NULL);
19812 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19813 
19814 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19815 
19816 	/* already determined doorlock is not supported, fake success */
19817 	if (un->un_f_doorlock_supported == FALSE) {
19818 		return (0);
19819 	}
19820 
19821 	/*
19822 	 * If we are ejecting and see an SD_REMOVAL_PREVENT
19823 	 * ignore the command so we can complete the eject
19824 	 * operation.
19825 	 */
19826 	if (flag == SD_REMOVAL_PREVENT) {
19827 		mutex_enter(SD_MUTEX(un));
19828 		if (un->un_f_ejecting == TRUE) {
19829 			mutex_exit(SD_MUTEX(un));
19830 			return (EAGAIN);
19831 		}
19832 		mutex_exit(SD_MUTEX(un));
19833 	}
19834 
19835 	bzero(&cdb, sizeof (cdb));
19836 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19837 
19838 	cdb.scc_cmd = SCMD_DOORLOCK;
19839 	cdb.cdb_opaque[4] = (uchar_t)flag;
19840 
19841 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19842 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19843 	ucmd_buf.uscsi_bufaddr	= NULL;
19844 	ucmd_buf.uscsi_buflen	= 0;
19845 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19846 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19847 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19848 	ucmd_buf.uscsi_timeout	= 15;
19849 
19850 	SD_TRACE(SD_LOG_IO, un,
19851 	    "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19852 
19853 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19854 	    UIO_SYSSPACE, path_flag);
19855 
19856 	if (status == 0)
19857 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19858 
19859 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19860 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19861 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19862 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19863 
19864 		/* fake success and skip subsequent doorlock commands */
19865 		un->un_f_doorlock_supported = FALSE;
19866 		return (0);
19867 	}
19868 
19869 	return (status);
19870 }
19871 
19872 /*
19873  *    Function: sd_send_scsi_READ_CAPACITY
19874  *
19875  * Description: This routine uses the scsi READ CAPACITY command to determine
19876  *		the device capacity in number of blocks and the device native
19877  *		block size. If this function returns a failure, then the
19878  *		values in *capp and *lbap are undefined.  If the capacity
19879  *		returned is 0xffffffff then the lun is too large for a
19880  *		normal READ CAPACITY command and the results of a
19881  *		READ CAPACITY 16 will be used instead.
19882  *
19883  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19884  *		capp - ptr to unsigned 64-bit variable to receive the
19885  *			capacity value from the command.
19886  *		lbap - ptr to unsigned 32-bit varaible to receive the
19887  *			block size value from the command
19888  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19889  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19890  *			to use the USCSI "direct" chain and bypass the normal
19891  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19892  *			command is issued as part of an error recovery action.
19893  *
19894  * Return Code: 0   - Success
19895  *		EIO - IO error
19896  *		EACCES - Reservation conflict detected
19897  *		EAGAIN - Device is becoming ready
19898  *		errno return code from sd_ssc_send()
19899  *
19900  *     Context: Can sleep.  Blocks until command completes.
19901  */
19902 
19903 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
19904 
19905 static int
19906 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
19907 	int path_flag)
19908 {
19909 	struct	scsi_extended_sense	sense_buf;
19910 	struct	uscsi_cmd	ucmd_buf;
19911 	union	scsi_cdb	cdb;
19912 	uint32_t		*capacity_buf;
19913 	uint64_t		capacity;
19914 	uint32_t		lbasize;
19915 	uint32_t		pbsize;
19916 	int			status;
19917 	struct sd_lun		*un;
19918 
19919 	ASSERT(ssc != NULL);
19920 
19921 	un = ssc->ssc_un;
19922 	ASSERT(un != NULL);
19923 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19924 	ASSERT(capp != NULL);
19925 	ASSERT(lbap != NULL);
19926 
19927 	SD_TRACE(SD_LOG_IO, un,
19928 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19929 
19930 	/*
19931 	 * First send a READ_CAPACITY command to the target.
19932 	 * (This command is mandatory under SCSI-2.)
19933 	 *
19934 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
19935 	 * Medium Indicator bit is cleared.  The address field must be
19936 	 * zero if the PMI bit is zero.
19937 	 */
19938 	bzero(&cdb, sizeof (cdb));
19939 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19940 
19941 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19942 
19943 	cdb.scc_cmd = SCMD_READ_CAPACITY;
19944 
19945 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19946 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19947 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
19948 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
19949 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19950 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19951 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19952 	ucmd_buf.uscsi_timeout	= 60;
19953 
19954 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19955 	    UIO_SYSSPACE, path_flag);
19956 
19957 	switch (status) {
19958 	case 0:
19959 		/* Return failure if we did not get valid capacity data. */
19960 		if (ucmd_buf.uscsi_resid != 0) {
19961 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19962 			    "sd_send_scsi_READ_CAPACITY received invalid "
19963 			    "capacity data");
19964 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19965 			return (EIO);
19966 		}
19967 		/*
19968 		 * Read capacity and block size from the READ CAPACITY 10 data.
19969 		 * This data may be adjusted later due to device specific
19970 		 * issues.
19971 		 *
19972 		 * According to the SCSI spec, the READ CAPACITY 10
19973 		 * command returns the following:
19974 		 *
19975 		 *  bytes 0-3: Maximum logical block address available.
19976 		 *		(MSB in byte:0 & LSB in byte:3)
19977 		 *
19978 		 *  bytes 4-7: Block length in bytes
19979 		 *		(MSB in byte:4 & LSB in byte:7)
19980 		 *
19981 		 */
19982 		capacity = BE_32(capacity_buf[0]);
19983 		lbasize = BE_32(capacity_buf[1]);
19984 
19985 		/*
19986 		 * Done with capacity_buf
19987 		 */
19988 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19989 
19990 		/*
19991 		 * if the reported capacity is set to all 0xf's, then
19992 		 * this disk is too large and requires SBC-2 commands.
19993 		 * Reissue the request using READ CAPACITY 16.
19994 		 */
19995 		if (capacity == 0xffffffff) {
19996 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19997 			status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
19998 			    &lbasize, &pbsize, path_flag);
19999 			if (status != 0) {
20000 				return (status);
20001 			}
20002 		}
20003 		break;	/* Success! */
20004 	case EIO:
20005 		switch (ucmd_buf.uscsi_status) {
20006 		case STATUS_RESERVATION_CONFLICT:
20007 			status = EACCES;
20008 			break;
20009 		case STATUS_CHECK:
20010 			/*
20011 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20012 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20013 			 */
20014 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20015 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20016 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20017 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20018 				return (EAGAIN);
20019 			}
20020 			break;
20021 		default:
20022 			break;
20023 		}
20024 		/* FALLTHRU */
20025 	default:
20026 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20027 		return (status);
20028 	}
20029 
20030 	/*
20031 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20032 	 * (2352 and 0 are common) so for these devices always force the value
20033 	 * to 2048 as required by the ATAPI specs.
20034 	 */
20035 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20036 		lbasize = 2048;
20037 	}
20038 
20039 	/*
20040 	 * Get the maximum LBA value from the READ CAPACITY data.
20041 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20042 	 * was cleared when issuing the command. This means that the LBA
20043 	 * returned from the device is the LBA of the last logical block
20044 	 * on the logical unit.  The actual logical block count will be
20045 	 * this value plus one.
20046 	 */
20047 	capacity += 1;
20048 
20049 	/*
20050 	 * Currently, for removable media, the capacity is saved in terms
20051 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20052 	 */
20053 	if (un->un_f_has_removable_media)
20054 		capacity *= (lbasize / un->un_sys_blocksize);
20055 
20056 	/*
20057 	 * Copy the values from the READ CAPACITY command into the space
20058 	 * provided by the caller.
20059 	 */
20060 	*capp = capacity;
20061 	*lbap = lbasize;
20062 
20063 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20064 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20065 
20066 	/*
20067 	 * Both the lbasize and capacity from the device must be nonzero,
20068 	 * otherwise we assume that the values are not valid and return
20069 	 * failure to the caller. (4203735)
20070 	 */
20071 	if ((capacity == 0) || (lbasize == 0)) {
20072 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20073 		    "sd_send_scsi_READ_CAPACITY received invalid value "
20074 		    "capacity %llu lbasize %d", capacity, lbasize);
20075 		return (EIO);
20076 	}
20077 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20078 	return (0);
20079 }
20080 
20081 /*
20082  *    Function: sd_send_scsi_READ_CAPACITY_16
20083  *
20084  * Description: This routine uses the scsi READ CAPACITY 16 command to
20085  *		determine the device capacity in number of blocks and the
20086  *		device native block size.  If this function returns a failure,
20087  *		then the values in *capp and *lbap are undefined.
20088  *		This routine should be called by sd_send_scsi_READ_CAPACITY
20089  *              which will apply any device specific adjustments to capacity
20090  *              and lbasize. One exception is it is also called by
20091  *              sd_get_media_info_ext. In that function, there is no need to
20092  *              adjust the capacity and lbasize.
20093  *
20094  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20095  *		capp - ptr to unsigned 64-bit variable to receive the
20096  *			capacity value from the command.
20097  *		lbap - ptr to unsigned 32-bit varaible to receive the
20098  *			block size value from the command
20099  *              psp  - ptr to unsigned 32-bit variable to receive the
20100  *                      physical block size value from the command
20101  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20102  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20103  *			to use the USCSI "direct" chain and bypass the normal
20104  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
20105  *			this command is issued as part of an error recovery
20106  *			action.
20107  *
20108  * Return Code: 0   - Success
20109  *		EIO - IO error
20110  *		EACCES - Reservation conflict detected
20111  *		EAGAIN - Device is becoming ready
20112  *		errno return code from sd_ssc_send()
20113  *
20114  *     Context: Can sleep.  Blocks until command completes.
20115  */
20116 
20117 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
20118 
20119 static int
20120 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20121 	uint32_t *lbap, uint32_t *psp, int path_flag)
20122 {
20123 	struct	scsi_extended_sense	sense_buf;
20124 	struct	uscsi_cmd	ucmd_buf;
20125 	union	scsi_cdb	cdb;
20126 	uint64_t		*capacity16_buf;
20127 	uint64_t		capacity;
20128 	uint32_t		lbasize;
20129 	uint32_t		pbsize;
20130 	uint32_t		lbpb_exp;
20131 	int			status;
20132 	struct sd_lun		*un;
20133 
20134 	ASSERT(ssc != NULL);
20135 
20136 	un = ssc->ssc_un;
20137 	ASSERT(un != NULL);
20138 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20139 	ASSERT(capp != NULL);
20140 	ASSERT(lbap != NULL);
20141 
20142 	SD_TRACE(SD_LOG_IO, un,
20143 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20144 
20145 	/*
20146 	 * First send a READ_CAPACITY_16 command to the target.
20147 	 *
20148 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20149 	 * Medium Indicator bit is cleared.  The address field must be
20150 	 * zero if the PMI bit is zero.
20151 	 */
20152 	bzero(&cdb, sizeof (cdb));
20153 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20154 
20155 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20156 
20157 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20158 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
20159 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
20160 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
20161 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20162 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20163 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20164 	ucmd_buf.uscsi_timeout	= 60;
20165 
20166 	/*
20167 	 * Read Capacity (16) is a Service Action In command.  One
20168 	 * command byte (0x9E) is overloaded for multiple operations,
20169 	 * with the second CDB byte specifying the desired operation
20170 	 */
20171 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20172 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20173 
20174 	/*
20175 	 * Fill in allocation length field
20176 	 */
20177 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20178 
20179 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20180 	    UIO_SYSSPACE, path_flag);
20181 
20182 	switch (status) {
20183 	case 0:
20184 		/* Return failure if we did not get valid capacity data. */
20185 		if (ucmd_buf.uscsi_resid > 20) {
20186 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20187 			    "sd_send_scsi_READ_CAPACITY_16 received invalid "
20188 			    "capacity data");
20189 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20190 			return (EIO);
20191 		}
20192 
20193 		/*
20194 		 * Read capacity and block size from the READ CAPACITY 10 data.
20195 		 * This data may be adjusted later due to device specific
20196 		 * issues.
20197 		 *
20198 		 * According to the SCSI spec, the READ CAPACITY 10
20199 		 * command returns the following:
20200 		 *
20201 		 *  bytes 0-7: Maximum logical block address available.
20202 		 *		(MSB in byte:0 & LSB in byte:7)
20203 		 *
20204 		 *  bytes 8-11: Block length in bytes
20205 		 *		(MSB in byte:8 & LSB in byte:11)
20206 		 *
20207 		 *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20208 		 */
20209 		capacity = BE_64(capacity16_buf[0]);
20210 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20211 		lbpb_exp = (BE_64(capacity16_buf[1]) >> 40) & 0x0f;
20212 
20213 		pbsize = lbasize << lbpb_exp;
20214 
20215 		/*
20216 		 * Done with capacity16_buf
20217 		 */
20218 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20219 
20220 		/*
20221 		 * if the reported capacity is set to all 0xf's, then
20222 		 * this disk is too large.  This could only happen with
20223 		 * a device that supports LBAs larger than 64 bits which
20224 		 * are not defined by any current T10 standards.
20225 		 */
20226 		if (capacity == 0xffffffffffffffff) {
20227 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20228 			    "disk is too large");
20229 			return (EIO);
20230 		}
20231 		break;	/* Success! */
20232 	case EIO:
20233 		switch (ucmd_buf.uscsi_status) {
20234 		case STATUS_RESERVATION_CONFLICT:
20235 			status = EACCES;
20236 			break;
20237 		case STATUS_CHECK:
20238 			/*
20239 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20240 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20241 			 */
20242 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20243 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20244 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20245 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20246 				return (EAGAIN);
20247 			}
20248 			break;
20249 		default:
20250 			break;
20251 		}
20252 		/* FALLTHRU */
20253 	default:
20254 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20255 		return (status);
20256 	}
20257 
20258 	*capp = capacity;
20259 	*lbap = lbasize;
20260 	*psp = pbsize;
20261 
20262 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20263 	    "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20264 	    capacity, lbasize, pbsize);
20265 
20266 	return (0);
20267 }
20268 
20269 
20270 /*
20271  *    Function: sd_send_scsi_START_STOP_UNIT
20272  *
20273  * Description: Issue a scsi START STOP UNIT command to the target.
20274  *
20275  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20276  *                       structure for this target.
20277  *      pc_flag - SD_POWER_CONDITION
20278  *                SD_START_STOP
20279  *		flag  - SD_TARGET_START
20280  *			SD_TARGET_STOP
20281  *			SD_TARGET_EJECT
20282  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20283  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20284  *			to use the USCSI "direct" chain and bypass the normal
20285  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20286  *			command is issued as part of an error recovery action.
20287  *
20288  * Return Code: 0   - Success
20289  *		EIO - IO error
20290  *		EACCES - Reservation conflict detected
20291  *		ENXIO  - Not Ready, medium not present
20292  *		errno return code from sd_ssc_send()
20293  *
20294  *     Context: Can sleep.
20295  */
20296 
20297 static int
20298 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20299     int path_flag)
20300 {
20301 	struct	scsi_extended_sense	sense_buf;
20302 	union scsi_cdb		cdb;
20303 	struct uscsi_cmd	ucmd_buf;
20304 	int			status;
20305 	struct sd_lun		*un;
20306 
20307 	ASSERT(ssc != NULL);
20308 	un = ssc->ssc_un;
20309 	ASSERT(un != NULL);
20310 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20311 
20312 	SD_TRACE(SD_LOG_IO, un,
20313 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20314 
20315 	if (un->un_f_check_start_stop &&
20316 	    ((pc_flag == SD_START_STOP) && (flag != SD_TARGET_EJECT)) &&
20317 	    (un->un_f_start_stop_supported != TRUE)) {
20318 		return (0);
20319 	}
20320 
20321 	/*
20322 	 * If we are performing an eject operation and
20323 	 * we receive any command other than SD_TARGET_EJECT
20324 	 * we should immediately return.
20325 	 */
20326 	if (flag != SD_TARGET_EJECT) {
20327 		mutex_enter(SD_MUTEX(un));
20328 		if (un->un_f_ejecting == TRUE) {
20329 			mutex_exit(SD_MUTEX(un));
20330 			return (EAGAIN);
20331 		}
20332 		mutex_exit(SD_MUTEX(un));
20333 	}
20334 
20335 	bzero(&cdb, sizeof (cdb));
20336 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20337 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20338 
20339 	cdb.scc_cmd = SCMD_START_STOP;
20340 	cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20341 	    (uchar_t)(flag << 4) : (uchar_t)flag;
20342 
20343 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20344 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20345 	ucmd_buf.uscsi_bufaddr	= NULL;
20346 	ucmd_buf.uscsi_buflen	= 0;
20347 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20348 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20349 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20350 	ucmd_buf.uscsi_timeout	= 200;
20351 
20352 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20353 	    UIO_SYSSPACE, path_flag);
20354 
20355 	switch (status) {
20356 	case 0:
20357 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20358 		break;	/* Success! */
20359 	case EIO:
20360 		switch (ucmd_buf.uscsi_status) {
20361 		case STATUS_RESERVATION_CONFLICT:
20362 			status = EACCES;
20363 			break;
20364 		case STATUS_CHECK:
20365 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20366 				switch (scsi_sense_key(
20367 				    (uint8_t *)&sense_buf)) {
20368 				case KEY_ILLEGAL_REQUEST:
20369 					status = ENOTSUP;
20370 					break;
20371 				case KEY_NOT_READY:
20372 					if (scsi_sense_asc(
20373 					    (uint8_t *)&sense_buf)
20374 					    == 0x3A) {
20375 						status = ENXIO;
20376 					}
20377 					break;
20378 				default:
20379 					break;
20380 				}
20381 			}
20382 			break;
20383 		default:
20384 			break;
20385 		}
20386 		break;
20387 	default:
20388 		break;
20389 	}
20390 
20391 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20392 
20393 	return (status);
20394 }
20395 
20396 
20397 /*
20398  *    Function: sd_start_stop_unit_callback
20399  *
20400  * Description: timeout(9F) callback to begin recovery process for a
20401  *		device that has spun down.
20402  *
20403  *   Arguments: arg - pointer to associated softstate struct.
20404  *
20405  *     Context: Executes in a timeout(9F) thread context
20406  */
20407 
20408 static void
20409 sd_start_stop_unit_callback(void *arg)
20410 {
20411 	struct sd_lun	*un = arg;
20412 	ASSERT(un != NULL);
20413 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20414 
20415 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20416 
20417 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20418 }
20419 
20420 
20421 /*
20422  *    Function: sd_start_stop_unit_task
20423  *
20424  * Description: Recovery procedure when a drive is spun down.
20425  *
20426  *   Arguments: arg - pointer to associated softstate struct.
20427  *
20428  *     Context: Executes in a taskq() thread context
20429  */
20430 
20431 static void
20432 sd_start_stop_unit_task(void *arg)
20433 {
20434 	struct sd_lun	*un = arg;
20435 	sd_ssc_t	*ssc;
20436 	int		power_level;
20437 	int		rval;
20438 
20439 	ASSERT(un != NULL);
20440 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20441 
20442 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20443 
20444 	/*
20445 	 * Some unformatted drives report not ready error, no need to
20446 	 * restart if format has been initiated.
20447 	 */
20448 	mutex_enter(SD_MUTEX(un));
20449 	if (un->un_f_format_in_progress == TRUE) {
20450 		mutex_exit(SD_MUTEX(un));
20451 		return;
20452 	}
20453 	mutex_exit(SD_MUTEX(un));
20454 
20455 	ssc = sd_ssc_init(un);
20456 	/*
20457 	 * When a START STOP command is issued from here, it is part of a
20458 	 * failure recovery operation and must be issued before any other
20459 	 * commands, including any pending retries. Thus it must be sent
20460 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20461 	 * succeeds or not, we will start I/O after the attempt.
20462 	 * If power condition is supported and the current power level
20463 	 * is capable of performing I/O, we should set the power condition
20464 	 * to that level. Otherwise, set the power condition to ACTIVE.
20465 	 */
20466 	if (un->un_f_power_condition_supported) {
20467 		mutex_enter(SD_MUTEX(un));
20468 		ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20469 		power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20470 		    > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20471 		mutex_exit(SD_MUTEX(un));
20472 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20473 		    sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20474 	} else {
20475 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20476 		    SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20477 	}
20478 
20479 	if (rval != 0)
20480 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20481 	sd_ssc_fini(ssc);
20482 	/*
20483 	 * The above call blocks until the START_STOP_UNIT command completes.
20484 	 * Now that it has completed, we must re-try the original IO that
20485 	 * received the NOT READY condition in the first place. There are
20486 	 * three possible conditions here:
20487 	 *
20488 	 *  (1) The original IO is on un_retry_bp.
20489 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
20490 	 *	is NULL.
20491 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
20492 	 *	points to some other, unrelated bp.
20493 	 *
20494 	 * For each case, we must call sd_start_cmds() with un_retry_bp
20495 	 * as the argument. If un_retry_bp is NULL, this will initiate
20496 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
20497 	 * then this will process the bp on un_retry_bp. That may or may not
20498 	 * be the original IO, but that does not matter: the important thing
20499 	 * is to keep the IO processing going at this point.
20500 	 *
20501 	 * Note: This is a very specific error recovery sequence associated
20502 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20503 	 * serialize the I/O with completion of the spin-up.
20504 	 */
20505 	mutex_enter(SD_MUTEX(un));
20506 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20507 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20508 	    un, un->un_retry_bp);
20509 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20510 	sd_start_cmds(un, un->un_retry_bp);
20511 	mutex_exit(SD_MUTEX(un));
20512 
20513 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20514 }
20515 
20516 
20517 /*
20518  *    Function: sd_send_scsi_INQUIRY
20519  *
20520  * Description: Issue the scsi INQUIRY command.
20521  *
20522  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20523  *                      structure for this target.
20524  *		bufaddr
20525  *		buflen
20526  *		evpd
20527  *		page_code
20528  *		page_length
20529  *
20530  * Return Code: 0   - Success
20531  *		errno return code from sd_ssc_send()
20532  *
20533  *     Context: Can sleep. Does not return until command is completed.
20534  */
20535 
20536 static int
20537 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20538 	uchar_t evpd, uchar_t page_code, size_t *residp)
20539 {
20540 	union scsi_cdb		cdb;
20541 	struct uscsi_cmd	ucmd_buf;
20542 	int			status;
20543 	struct sd_lun		*un;
20544 
20545 	ASSERT(ssc != NULL);
20546 	un = ssc->ssc_un;
20547 	ASSERT(un != NULL);
20548 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20549 	ASSERT(bufaddr != NULL);
20550 
20551 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20552 
20553 	bzero(&cdb, sizeof (cdb));
20554 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20555 	bzero(bufaddr, buflen);
20556 
20557 	cdb.scc_cmd = SCMD_INQUIRY;
20558 	cdb.cdb_opaque[1] = evpd;
20559 	cdb.cdb_opaque[2] = page_code;
20560 	FORMG0COUNT(&cdb, buflen);
20561 
20562 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20563 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20564 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20565 	ucmd_buf.uscsi_buflen	= buflen;
20566 	ucmd_buf.uscsi_rqbuf	= NULL;
20567 	ucmd_buf.uscsi_rqlen	= 0;
20568 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20569 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20570 
20571 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20572 	    UIO_SYSSPACE, SD_PATH_DIRECT);
20573 
20574 	/*
20575 	 * Only handle status == 0, the upper-level caller
20576 	 * will put different assessment based on the context.
20577 	 */
20578 	if (status == 0)
20579 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20580 
20581 	if ((status == 0) && (residp != NULL)) {
20582 		*residp = ucmd_buf.uscsi_resid;
20583 	}
20584 
20585 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20586 
20587 	return (status);
20588 }
20589 
20590 
20591 /*
20592  *    Function: sd_send_scsi_TEST_UNIT_READY
20593  *
20594  * Description: Issue the scsi TEST UNIT READY command.
20595  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20596  *		prevent retrying failed commands. Use this when the intent
20597  *		is either to check for device readiness, to clear a Unit
20598  *		Attention, or to clear any outstanding sense data.
20599  *		However under specific conditions the expected behavior
20600  *		is for retries to bring a device ready, so use the flag
20601  *		with caution.
20602  *
20603  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20604  *                      structure for this target.
20605  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20606  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20607  *			0: dont check for media present, do retries on cmd.
20608  *
20609  * Return Code: 0   - Success
20610  *		EIO - IO error
20611  *		EACCES - Reservation conflict detected
20612  *		ENXIO  - Not Ready, medium not present
20613  *		errno return code from sd_ssc_send()
20614  *
20615  *     Context: Can sleep. Does not return until command is completed.
20616  */
20617 
20618 static int
20619 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20620 {
20621 	struct	scsi_extended_sense	sense_buf;
20622 	union scsi_cdb		cdb;
20623 	struct uscsi_cmd	ucmd_buf;
20624 	int			status;
20625 	struct sd_lun		*un;
20626 
20627 	ASSERT(ssc != NULL);
20628 	un = ssc->ssc_un;
20629 	ASSERT(un != NULL);
20630 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20631 
20632 	SD_TRACE(SD_LOG_IO, un,
20633 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20634 
20635 	/*
20636 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20637 	 * timeouts when they receive a TUR and the queue is not empty. Check
20638 	 * the configuration flag set during attach (indicating the drive has
20639 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20640 	 * TUR. If there are
20641 	 * pending commands return success, this is a bit arbitrary but is ok
20642 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20643 	 * configurations.
20644 	 */
20645 	if (un->un_f_cfg_tur_check == TRUE) {
20646 		mutex_enter(SD_MUTEX(un));
20647 		if (un->un_ncmds_in_transport != 0) {
20648 			mutex_exit(SD_MUTEX(un));
20649 			return (0);
20650 		}
20651 		mutex_exit(SD_MUTEX(un));
20652 	}
20653 
20654 	bzero(&cdb, sizeof (cdb));
20655 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20656 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20657 
20658 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20659 
20660 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20661 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20662 	ucmd_buf.uscsi_bufaddr	= NULL;
20663 	ucmd_buf.uscsi_buflen	= 0;
20664 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20665 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20666 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20667 
20668 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20669 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20670 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20671 	}
20672 	ucmd_buf.uscsi_timeout	= 60;
20673 
20674 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20675 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20676 	    SD_PATH_STANDARD));
20677 
20678 	switch (status) {
20679 	case 0:
20680 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20681 		break;	/* Success! */
20682 	case EIO:
20683 		switch (ucmd_buf.uscsi_status) {
20684 		case STATUS_RESERVATION_CONFLICT:
20685 			status = EACCES;
20686 			break;
20687 		case STATUS_CHECK:
20688 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20689 				break;
20690 			}
20691 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20692 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20693 			    KEY_NOT_READY) &&
20694 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20695 				status = ENXIO;
20696 			}
20697 			break;
20698 		default:
20699 			break;
20700 		}
20701 		break;
20702 	default:
20703 		break;
20704 	}
20705 
20706 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20707 
20708 	return (status);
20709 }
20710 
20711 /*
20712  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20713  *
20714  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20715  *
20716  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20717  *                      structure for this target.
20718  *
20719  * Return Code: 0   - Success
20720  *		EACCES
20721  *		ENOTSUP
20722  *		errno return code from sd_ssc_send()
20723  *
20724  *     Context: Can sleep. Does not return until command is completed.
20725  */
20726 
20727 static int
20728 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t  usr_cmd,
20729 	uint16_t data_len, uchar_t *data_bufp)
20730 {
20731 	struct scsi_extended_sense	sense_buf;
20732 	union scsi_cdb		cdb;
20733 	struct uscsi_cmd	ucmd_buf;
20734 	int			status;
20735 	int			no_caller_buf = FALSE;
20736 	struct sd_lun		*un;
20737 
20738 	ASSERT(ssc != NULL);
20739 	un = ssc->ssc_un;
20740 	ASSERT(un != NULL);
20741 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20742 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20743 
20744 	SD_TRACE(SD_LOG_IO, un,
20745 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20746 
20747 	bzero(&cdb, sizeof (cdb));
20748 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20749 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20750 	if (data_bufp == NULL) {
20751 		/* Allocate a default buf if the caller did not give one */
20752 		ASSERT(data_len == 0);
20753 		data_len  = MHIOC_RESV_KEY_SIZE;
20754 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20755 		no_caller_buf = TRUE;
20756 	}
20757 
20758 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20759 	cdb.cdb_opaque[1] = usr_cmd;
20760 	FORMG1COUNT(&cdb, data_len);
20761 
20762 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20763 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20764 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20765 	ucmd_buf.uscsi_buflen	= data_len;
20766 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20767 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20768 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20769 	ucmd_buf.uscsi_timeout	= 60;
20770 
20771 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20772 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20773 
20774 	switch (status) {
20775 	case 0:
20776 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20777 
20778 		break;	/* Success! */
20779 	case EIO:
20780 		switch (ucmd_buf.uscsi_status) {
20781 		case STATUS_RESERVATION_CONFLICT:
20782 			status = EACCES;
20783 			break;
20784 		case STATUS_CHECK:
20785 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20786 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20787 			    KEY_ILLEGAL_REQUEST)) {
20788 				status = ENOTSUP;
20789 			}
20790 			break;
20791 		default:
20792 			break;
20793 		}
20794 		break;
20795 	default:
20796 		break;
20797 	}
20798 
20799 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20800 
20801 	if (no_caller_buf == TRUE) {
20802 		kmem_free(data_bufp, data_len);
20803 	}
20804 
20805 	return (status);
20806 }
20807 
20808 
20809 /*
20810  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20811  *
20812  * Description: This routine is the driver entry point for handling CD-ROM
20813  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20814  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20815  *		device.
20816  *
20817  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
20818  *                      for the target.
20819  *		usr_cmd SCSI-3 reservation facility command (one of
20820  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20821  *			SD_SCSI3_PREEMPTANDABORT)
20822  *		usr_bufp - user provided pointer register, reserve descriptor or
20823  *			preempt and abort structure (mhioc_register_t,
20824  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20825  *
20826  * Return Code: 0   - Success
20827  *		EACCES
20828  *		ENOTSUP
20829  *		errno return code from sd_ssc_send()
20830  *
20831  *     Context: Can sleep. Does not return until command is completed.
20832  */
20833 
20834 static int
20835 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20836 	uchar_t	*usr_bufp)
20837 {
20838 	struct scsi_extended_sense	sense_buf;
20839 	union scsi_cdb		cdb;
20840 	struct uscsi_cmd	ucmd_buf;
20841 	int			status;
20842 	uchar_t			data_len = sizeof (sd_prout_t);
20843 	sd_prout_t		*prp;
20844 	struct sd_lun		*un;
20845 
20846 	ASSERT(ssc != NULL);
20847 	un = ssc->ssc_un;
20848 	ASSERT(un != NULL);
20849 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20850 	ASSERT(data_len == 24);	/* required by scsi spec */
20851 
20852 	SD_TRACE(SD_LOG_IO, un,
20853 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20854 
20855 	if (usr_bufp == NULL) {
20856 		return (EINVAL);
20857 	}
20858 
20859 	bzero(&cdb, sizeof (cdb));
20860 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20861 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20862 	prp = kmem_zalloc(data_len, KM_SLEEP);
20863 
20864 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20865 	cdb.cdb_opaque[1] = usr_cmd;
20866 	FORMG1COUNT(&cdb, data_len);
20867 
20868 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20869 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20870 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
20871 	ucmd_buf.uscsi_buflen	= data_len;
20872 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20873 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20874 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20875 	ucmd_buf.uscsi_timeout	= 60;
20876 
20877 	switch (usr_cmd) {
20878 	case SD_SCSI3_REGISTER: {
20879 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20880 
20881 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20882 		bcopy(ptr->newkey.key, prp->service_key,
20883 		    MHIOC_RESV_KEY_SIZE);
20884 		prp->aptpl = ptr->aptpl;
20885 		break;
20886 	}
20887 	case SD_SCSI3_RESERVE:
20888 	case SD_SCSI3_RELEASE: {
20889 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20890 
20891 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20892 		prp->scope_address = BE_32(ptr->scope_specific_addr);
20893 		cdb.cdb_opaque[2] = ptr->type;
20894 		break;
20895 	}
20896 	case SD_SCSI3_PREEMPTANDABORT: {
20897 		mhioc_preemptandabort_t *ptr =
20898 		    (mhioc_preemptandabort_t *)usr_bufp;
20899 
20900 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20901 		bcopy(ptr->victim_key.key, prp->service_key,
20902 		    MHIOC_RESV_KEY_SIZE);
20903 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
20904 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
20905 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
20906 		break;
20907 	}
20908 	case SD_SCSI3_REGISTERANDIGNOREKEY:
20909 	{
20910 		mhioc_registerandignorekey_t *ptr;
20911 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
20912 		bcopy(ptr->newkey.key,
20913 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
20914 		prp->aptpl = ptr->aptpl;
20915 		break;
20916 	}
20917 	default:
20918 		ASSERT(FALSE);
20919 		break;
20920 	}
20921 
20922 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20923 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20924 
20925 	switch (status) {
20926 	case 0:
20927 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20928 		break;	/* Success! */
20929 	case EIO:
20930 		switch (ucmd_buf.uscsi_status) {
20931 		case STATUS_RESERVATION_CONFLICT:
20932 			status = EACCES;
20933 			break;
20934 		case STATUS_CHECK:
20935 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20936 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20937 			    KEY_ILLEGAL_REQUEST)) {
20938 				status = ENOTSUP;
20939 			}
20940 			break;
20941 		default:
20942 			break;
20943 		}
20944 		break;
20945 	default:
20946 		break;
20947 	}
20948 
20949 	kmem_free(prp, data_len);
20950 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20951 	return (status);
20952 }
20953 
20954 
20955 /*
20956  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
20957  *
20958  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20959  *
20960  *   Arguments: un - pointer to the target's soft state struct
20961  *              dkc - pointer to the callback structure
20962  *
20963  * Return Code: 0 - success
20964  *		errno-type error code
20965  *
20966  *     Context: kernel thread context only.
20967  *
20968  *  _______________________________________________________________
20969  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
20970  * |FLUSH_VOLATILE|              | operation                       |
20971  * |______________|______________|_________________________________|
20972  * | 0            | NULL         | Synchronous flush on both       |
20973  * |              |              | volatile and non-volatile cache |
20974  * |______________|______________|_________________________________|
20975  * | 1            | NULL         | Synchronous flush on volatile   |
20976  * |              |              | cache; disk drivers may suppress|
20977  * |              |              | flush if disk table indicates   |
20978  * |              |              | non-volatile cache              |
20979  * |______________|______________|_________________________________|
20980  * | 0            | !NULL        | Asynchronous flush on both      |
20981  * |              |              | volatile and non-volatile cache;|
20982  * |______________|______________|_________________________________|
20983  * | 1            | !NULL        | Asynchronous flush on volatile  |
20984  * |              |              | cache; disk drivers may suppress|
20985  * |              |              | flush if disk table indicates   |
20986  * |              |              | non-volatile cache              |
20987  * |______________|______________|_________________________________|
20988  *
20989  */
20990 
20991 static int
20992 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20993 {
20994 	struct sd_uscsi_info	*uip;
20995 	struct uscsi_cmd	*uscmd;
20996 	union scsi_cdb		*cdb;
20997 	struct buf		*bp;
20998 	int			rval = 0;
20999 	int			is_async;
21000 
21001 	SD_TRACE(SD_LOG_IO, un,
21002 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21003 
21004 	ASSERT(un != NULL);
21005 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21006 
21007 	if (dkc == NULL || dkc->dkc_callback == NULL) {
21008 		is_async = FALSE;
21009 	} else {
21010 		is_async = TRUE;
21011 	}
21012 
21013 	mutex_enter(SD_MUTEX(un));
21014 	/* check whether cache flush should be suppressed */
21015 	if (un->un_f_suppress_cache_flush == TRUE) {
21016 		mutex_exit(SD_MUTEX(un));
21017 		/*
21018 		 * suppress the cache flush if the device is told to do
21019 		 * so by sd.conf or disk table
21020 		 */
21021 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21022 		    skip the cache flush since suppress_cache_flush is %d!\n",
21023 		    un->un_f_suppress_cache_flush);
21024 
21025 		if (is_async == TRUE) {
21026 			/* invoke callback for asynchronous flush */
21027 			(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21028 		}
21029 		return (rval);
21030 	}
21031 	mutex_exit(SD_MUTEX(un));
21032 
21033 	/*
21034 	 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21035 	 * set properly
21036 	 */
21037 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21038 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21039 
21040 	mutex_enter(SD_MUTEX(un));
21041 	if (dkc != NULL && un->un_f_sync_nv_supported &&
21042 	    (dkc->dkc_flag & FLUSH_VOLATILE)) {
21043 		/*
21044 		 * if the device supports SYNC_NV bit, turn on
21045 		 * the SYNC_NV bit to only flush volatile cache
21046 		 */
21047 		cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21048 	}
21049 	mutex_exit(SD_MUTEX(un));
21050 
21051 	/*
21052 	 * First get some memory for the uscsi_cmd struct and cdb
21053 	 * and initialize for SYNCHRONIZE_CACHE cmd.
21054 	 */
21055 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21056 	uscmd->uscsi_cdblen = CDB_GROUP1;
21057 	uscmd->uscsi_cdb = (caddr_t)cdb;
21058 	uscmd->uscsi_bufaddr = NULL;
21059 	uscmd->uscsi_buflen = 0;
21060 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21061 	uscmd->uscsi_rqlen = SENSE_LENGTH;
21062 	uscmd->uscsi_rqresid = SENSE_LENGTH;
21063 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21064 	uscmd->uscsi_timeout = sd_io_time;
21065 
21066 	/*
21067 	 * Allocate an sd_uscsi_info struct and fill it with the info
21068 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21069 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21070 	 * since we allocate the buf here in this function, we do not
21071 	 * need to preserve the prior contents of b_private.
21072 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21073 	 */
21074 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21075 	uip->ui_flags = SD_PATH_DIRECT;
21076 	uip->ui_cmdp  = uscmd;
21077 
21078 	bp = getrbuf(KM_SLEEP);
21079 	bp->b_private = uip;
21080 
21081 	/*
21082 	 * Setup buffer to carry uscsi request.
21083 	 */
21084 	bp->b_flags  = B_BUSY;
21085 	bp->b_bcount = 0;
21086 	bp->b_blkno  = 0;
21087 
21088 	if (is_async == TRUE) {
21089 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21090 		uip->ui_dkc = *dkc;
21091 	}
21092 
21093 	bp->b_edev = SD_GET_DEV(un);
21094 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
21095 
21096 	/*
21097 	 * Unset un_f_sync_cache_required flag
21098 	 */
21099 	mutex_enter(SD_MUTEX(un));
21100 	un->un_f_sync_cache_required = FALSE;
21101 	mutex_exit(SD_MUTEX(un));
21102 
21103 	(void) sd_uscsi_strategy(bp);
21104 
21105 	/*
21106 	 * If synchronous request, wait for completion
21107 	 * If async just return and let b_iodone callback
21108 	 * cleanup.
21109 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
21110 	 * but it was also incremented in sd_uscsi_strategy(), so
21111 	 * we should be ok.
21112 	 */
21113 	if (is_async == FALSE) {
21114 		(void) biowait(bp);
21115 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21116 	}
21117 
21118 	return (rval);
21119 }
21120 
21121 
21122 static int
21123 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21124 {
21125 	struct sd_uscsi_info *uip;
21126 	struct uscsi_cmd *uscmd;
21127 	uint8_t *sense_buf;
21128 	struct sd_lun *un;
21129 	int status;
21130 	union scsi_cdb *cdb;
21131 
21132 	uip = (struct sd_uscsi_info *)(bp->b_private);
21133 	ASSERT(uip != NULL);
21134 
21135 	uscmd = uip->ui_cmdp;
21136 	ASSERT(uscmd != NULL);
21137 
21138 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21139 	ASSERT(sense_buf != NULL);
21140 
21141 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21142 	ASSERT(un != NULL);
21143 
21144 	cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21145 
21146 	status = geterror(bp);
21147 	switch (status) {
21148 	case 0:
21149 		break;	/* Success! */
21150 	case EIO:
21151 		switch (uscmd->uscsi_status) {
21152 		case STATUS_RESERVATION_CONFLICT:
21153 			/* Ignore reservation conflict */
21154 			status = 0;
21155 			goto done;
21156 
21157 		case STATUS_CHECK:
21158 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21159 			    (scsi_sense_key(sense_buf) ==
21160 			    KEY_ILLEGAL_REQUEST)) {
21161 				/* Ignore Illegal Request error */
21162 				if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21163 					mutex_enter(SD_MUTEX(un));
21164 					un->un_f_sync_nv_supported = FALSE;
21165 					mutex_exit(SD_MUTEX(un));
21166 					status = 0;
21167 					SD_TRACE(SD_LOG_IO, un,
21168 					    "un_f_sync_nv_supported \
21169 					    is set to false.\n");
21170 					goto done;
21171 				}
21172 
21173 				mutex_enter(SD_MUTEX(un));
21174 				un->un_f_sync_cache_supported = FALSE;
21175 				mutex_exit(SD_MUTEX(un));
21176 				SD_TRACE(SD_LOG_IO, un,
21177 				    "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21178 				    un_f_sync_cache_supported set to false \
21179 				    with asc = %x, ascq = %x\n",
21180 				    scsi_sense_asc(sense_buf),
21181 				    scsi_sense_ascq(sense_buf));
21182 				status = ENOTSUP;
21183 				goto done;
21184 			}
21185 			break;
21186 		default:
21187 			break;
21188 		}
21189 		/* FALLTHRU */
21190 	default:
21191 		/*
21192 		 * Turn on the un_f_sync_cache_required flag
21193 		 * since the SYNC CACHE command failed
21194 		 */
21195 		mutex_enter(SD_MUTEX(un));
21196 		un->un_f_sync_cache_required = TRUE;
21197 		mutex_exit(SD_MUTEX(un));
21198 
21199 		/*
21200 		 * Don't log an error message if this device
21201 		 * has removable media.
21202 		 */
21203 		if (!un->un_f_has_removable_media) {
21204 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21205 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
21206 		}
21207 		break;
21208 	}
21209 
21210 done:
21211 	if (uip->ui_dkc.dkc_callback != NULL) {
21212 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21213 	}
21214 
21215 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
21216 	freerbuf(bp);
21217 	kmem_free(uip, sizeof (struct sd_uscsi_info));
21218 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21219 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21220 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
21221 
21222 	return (status);
21223 }
21224 
21225 
21226 /*
21227  *    Function: sd_send_scsi_GET_CONFIGURATION
21228  *
21229  * Description: Issues the get configuration command to the device.
21230  *		Called from sd_check_for_writable_cd & sd_get_media_info
21231  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21232  *   Arguments: ssc
21233  *		ucmdbuf
21234  *		rqbuf
21235  *		rqbuflen
21236  *		bufaddr
21237  *		buflen
21238  *		path_flag
21239  *
21240  * Return Code: 0   - Success
21241  *		errno return code from sd_ssc_send()
21242  *
21243  *     Context: Can sleep. Does not return until command is completed.
21244  *
21245  */
21246 
21247 static int
21248 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21249 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21250 	int path_flag)
21251 {
21252 	char	cdb[CDB_GROUP1];
21253 	int	status;
21254 	struct sd_lun	*un;
21255 
21256 	ASSERT(ssc != NULL);
21257 	un = ssc->ssc_un;
21258 	ASSERT(un != NULL);
21259 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21260 	ASSERT(bufaddr != NULL);
21261 	ASSERT(ucmdbuf != NULL);
21262 	ASSERT(rqbuf != NULL);
21263 
21264 	SD_TRACE(SD_LOG_IO, un,
21265 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21266 
21267 	bzero(cdb, sizeof (cdb));
21268 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21269 	bzero(rqbuf, rqbuflen);
21270 	bzero(bufaddr, buflen);
21271 
21272 	/*
21273 	 * Set up cdb field for the get configuration command.
21274 	 */
21275 	cdb[0] = SCMD_GET_CONFIGURATION;
21276 	cdb[1] = 0x02;  /* Requested Type */
21277 	cdb[8] = SD_PROFILE_HEADER_LEN;
21278 	ucmdbuf->uscsi_cdb = cdb;
21279 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21280 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21281 	ucmdbuf->uscsi_buflen = buflen;
21282 	ucmdbuf->uscsi_timeout = sd_io_time;
21283 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21284 	ucmdbuf->uscsi_rqlen = rqbuflen;
21285 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21286 
21287 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21288 	    UIO_SYSSPACE, path_flag);
21289 
21290 	switch (status) {
21291 	case 0:
21292 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21293 		break;  /* Success! */
21294 	case EIO:
21295 		switch (ucmdbuf->uscsi_status) {
21296 		case STATUS_RESERVATION_CONFLICT:
21297 			status = EACCES;
21298 			break;
21299 		default:
21300 			break;
21301 		}
21302 		break;
21303 	default:
21304 		break;
21305 	}
21306 
21307 	if (status == 0) {
21308 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21309 		    "sd_send_scsi_GET_CONFIGURATION: data",
21310 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21311 	}
21312 
21313 	SD_TRACE(SD_LOG_IO, un,
21314 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
21315 
21316 	return (status);
21317 }
21318 
21319 /*
21320  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21321  *
21322  * Description: Issues the get configuration command to the device to
21323  *              retrieve a specific feature. Called from
21324  *		sd_check_for_writable_cd & sd_set_mmc_caps.
21325  *   Arguments: ssc
21326  *              ucmdbuf
21327  *              rqbuf
21328  *              rqbuflen
21329  *              bufaddr
21330  *              buflen
21331  *		feature
21332  *
21333  * Return Code: 0   - Success
21334  *              errno return code from sd_ssc_send()
21335  *
21336  *     Context: Can sleep. Does not return until command is completed.
21337  *
21338  */
21339 static int
21340 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
21341 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
21342 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag)
21343 {
21344 	char    cdb[CDB_GROUP1];
21345 	int	status;
21346 	struct sd_lun	*un;
21347 
21348 	ASSERT(ssc != NULL);
21349 	un = ssc->ssc_un;
21350 	ASSERT(un != NULL);
21351 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21352 	ASSERT(bufaddr != NULL);
21353 	ASSERT(ucmdbuf != NULL);
21354 	ASSERT(rqbuf != NULL);
21355 
21356 	SD_TRACE(SD_LOG_IO, un,
21357 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21358 
21359 	bzero(cdb, sizeof (cdb));
21360 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21361 	bzero(rqbuf, rqbuflen);
21362 	bzero(bufaddr, buflen);
21363 
21364 	/*
21365 	 * Set up cdb field for the get configuration command.
21366 	 */
21367 	cdb[0] = SCMD_GET_CONFIGURATION;
21368 	cdb[1] = 0x02;  /* Requested Type */
21369 	cdb[3] = feature;
21370 	cdb[8] = buflen;
21371 	ucmdbuf->uscsi_cdb = cdb;
21372 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21373 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21374 	ucmdbuf->uscsi_buflen = buflen;
21375 	ucmdbuf->uscsi_timeout = sd_io_time;
21376 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21377 	ucmdbuf->uscsi_rqlen = rqbuflen;
21378 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21379 
21380 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21381 	    UIO_SYSSPACE, path_flag);
21382 
21383 	switch (status) {
21384 	case 0:
21385 
21386 		break;  /* Success! */
21387 	case EIO:
21388 		switch (ucmdbuf->uscsi_status) {
21389 		case STATUS_RESERVATION_CONFLICT:
21390 			status = EACCES;
21391 			break;
21392 		default:
21393 			break;
21394 		}
21395 		break;
21396 	default:
21397 		break;
21398 	}
21399 
21400 	if (status == 0) {
21401 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21402 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
21403 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21404 	}
21405 
21406 	SD_TRACE(SD_LOG_IO, un,
21407 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21408 
21409 	return (status);
21410 }
21411 
21412 
21413 /*
21414  *    Function: sd_send_scsi_MODE_SENSE
21415  *
21416  * Description: Utility function for issuing a scsi MODE SENSE command.
21417  *		Note: This routine uses a consistent implementation for Group0,
21418  *		Group1, and Group2 commands across all platforms. ATAPI devices
21419  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21420  *
21421  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21422  *                      structure for this target.
21423  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21424  *			  CDB_GROUP[1|2] (10 byte).
21425  *		bufaddr - buffer for page data retrieved from the target.
21426  *		buflen - size of page to be retrieved.
21427  *		page_code - page code of data to be retrieved from the target.
21428  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21429  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21430  *			to use the USCSI "direct" chain and bypass the normal
21431  *			command waitq.
21432  *
21433  * Return Code: 0   - Success
21434  *		errno return code from sd_ssc_send()
21435  *
21436  *     Context: Can sleep. Does not return until command is completed.
21437  */
21438 
21439 static int
21440 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21441 	size_t buflen,  uchar_t page_code, int path_flag)
21442 {
21443 	struct	scsi_extended_sense	sense_buf;
21444 	union scsi_cdb		cdb;
21445 	struct uscsi_cmd	ucmd_buf;
21446 	int			status;
21447 	int			headlen;
21448 	struct sd_lun		*un;
21449 
21450 	ASSERT(ssc != NULL);
21451 	un = ssc->ssc_un;
21452 	ASSERT(un != NULL);
21453 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21454 	ASSERT(bufaddr != NULL);
21455 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21456 	    (cdbsize == CDB_GROUP2));
21457 
21458 	SD_TRACE(SD_LOG_IO, un,
21459 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21460 
21461 	bzero(&cdb, sizeof (cdb));
21462 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21463 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21464 	bzero(bufaddr, buflen);
21465 
21466 	if (cdbsize == CDB_GROUP0) {
21467 		cdb.scc_cmd = SCMD_MODE_SENSE;
21468 		cdb.cdb_opaque[2] = page_code;
21469 		FORMG0COUNT(&cdb, buflen);
21470 		headlen = MODE_HEADER_LENGTH;
21471 	} else {
21472 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21473 		cdb.cdb_opaque[2] = page_code;
21474 		FORMG1COUNT(&cdb, buflen);
21475 		headlen = MODE_HEADER_LENGTH_GRP2;
21476 	}
21477 
21478 	ASSERT(headlen <= buflen);
21479 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21480 
21481 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21482 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21483 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21484 	ucmd_buf.uscsi_buflen	= buflen;
21485 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21486 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21487 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21488 	ucmd_buf.uscsi_timeout	= 60;
21489 
21490 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21491 	    UIO_SYSSPACE, path_flag);
21492 
21493 	switch (status) {
21494 	case 0:
21495 		/*
21496 		 * sr_check_wp() uses 0x3f page code and check the header of
21497 		 * mode page to determine if target device is write-protected.
21498 		 * But some USB devices return 0 bytes for 0x3f page code. For
21499 		 * this case, make sure that mode page header is returned at
21500 		 * least.
21501 		 */
21502 		if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21503 			status = EIO;
21504 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21505 			    "mode page header is not returned");
21506 		}
21507 		break;	/* Success! */
21508 	case EIO:
21509 		switch (ucmd_buf.uscsi_status) {
21510 		case STATUS_RESERVATION_CONFLICT:
21511 			status = EACCES;
21512 			break;
21513 		default:
21514 			break;
21515 		}
21516 		break;
21517 	default:
21518 		break;
21519 	}
21520 
21521 	if (status == 0) {
21522 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21523 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21524 	}
21525 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21526 
21527 	return (status);
21528 }
21529 
21530 
21531 /*
21532  *    Function: sd_send_scsi_MODE_SELECT
21533  *
21534  * Description: Utility function for issuing a scsi MODE SELECT command.
21535  *		Note: This routine uses a consistent implementation for Group0,
21536  *		Group1, and Group2 commands across all platforms. ATAPI devices
21537  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21538  *
21539  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21540  *                      structure for this target.
21541  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21542  *			  CDB_GROUP[1|2] (10 byte).
21543  *		bufaddr - buffer for page data retrieved from the target.
21544  *		buflen - size of page to be retrieved.
21545  *		save_page - boolean to determin if SP bit should be set.
21546  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21547  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21548  *			to use the USCSI "direct" chain and bypass the normal
21549  *			command waitq.
21550  *
21551  * Return Code: 0   - Success
21552  *		errno return code from sd_ssc_send()
21553  *
21554  *     Context: Can sleep. Does not return until command is completed.
21555  */
21556 
21557 static int
21558 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21559 	size_t buflen,  uchar_t save_page, int path_flag)
21560 {
21561 	struct	scsi_extended_sense	sense_buf;
21562 	union scsi_cdb		cdb;
21563 	struct uscsi_cmd	ucmd_buf;
21564 	int			status;
21565 	struct sd_lun		*un;
21566 
21567 	ASSERT(ssc != NULL);
21568 	un = ssc->ssc_un;
21569 	ASSERT(un != NULL);
21570 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21571 	ASSERT(bufaddr != NULL);
21572 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21573 	    (cdbsize == CDB_GROUP2));
21574 
21575 	SD_TRACE(SD_LOG_IO, un,
21576 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21577 
21578 	bzero(&cdb, sizeof (cdb));
21579 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21580 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21581 
21582 	/* Set the PF bit for many third party drives */
21583 	cdb.cdb_opaque[1] = 0x10;
21584 
21585 	/* Set the savepage(SP) bit if given */
21586 	if (save_page == SD_SAVE_PAGE) {
21587 		cdb.cdb_opaque[1] |= 0x01;
21588 	}
21589 
21590 	if (cdbsize == CDB_GROUP0) {
21591 		cdb.scc_cmd = SCMD_MODE_SELECT;
21592 		FORMG0COUNT(&cdb, buflen);
21593 	} else {
21594 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21595 		FORMG1COUNT(&cdb, buflen);
21596 	}
21597 
21598 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21599 
21600 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21601 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21602 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21603 	ucmd_buf.uscsi_buflen	= buflen;
21604 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21605 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21606 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21607 	ucmd_buf.uscsi_timeout	= 60;
21608 
21609 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21610 	    UIO_SYSSPACE, path_flag);
21611 
21612 	switch (status) {
21613 	case 0:
21614 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21615 		break;	/* Success! */
21616 	case EIO:
21617 		switch (ucmd_buf.uscsi_status) {
21618 		case STATUS_RESERVATION_CONFLICT:
21619 			status = EACCES;
21620 			break;
21621 		default:
21622 			break;
21623 		}
21624 		break;
21625 	default:
21626 		break;
21627 	}
21628 
21629 	if (status == 0) {
21630 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21631 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21632 	}
21633 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21634 
21635 	return (status);
21636 }
21637 
21638 
21639 /*
21640  *    Function: sd_send_scsi_RDWR
21641  *
21642  * Description: Issue a scsi READ or WRITE command with the given parameters.
21643  *
21644  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21645  *                      structure for this target.
21646  *		cmd:	 SCMD_READ or SCMD_WRITE
21647  *		bufaddr: Address of caller's buffer to receive the RDWR data
21648  *		buflen:  Length of caller's buffer receive the RDWR data.
21649  *		start_block: Block number for the start of the RDWR operation.
21650  *			 (Assumes target-native block size.)
21651  *		residp:  Pointer to variable to receive the redisual of the
21652  *			 RDWR operation (may be NULL of no residual requested).
21653  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21654  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21655  *			to use the USCSI "direct" chain and bypass the normal
21656  *			command waitq.
21657  *
21658  * Return Code: 0   - Success
21659  *		errno return code from sd_ssc_send()
21660  *
21661  *     Context: Can sleep. Does not return until command is completed.
21662  */
21663 
21664 static int
21665 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21666 	size_t buflen, daddr_t start_block, int path_flag)
21667 {
21668 	struct	scsi_extended_sense	sense_buf;
21669 	union scsi_cdb		cdb;
21670 	struct uscsi_cmd	ucmd_buf;
21671 	uint32_t		block_count;
21672 	int			status;
21673 	int			cdbsize;
21674 	uchar_t			flag;
21675 	struct sd_lun		*un;
21676 
21677 	ASSERT(ssc != NULL);
21678 	un = ssc->ssc_un;
21679 	ASSERT(un != NULL);
21680 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21681 	ASSERT(bufaddr != NULL);
21682 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21683 
21684 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21685 
21686 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21687 		return (EINVAL);
21688 	}
21689 
21690 	mutex_enter(SD_MUTEX(un));
21691 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21692 	mutex_exit(SD_MUTEX(un));
21693 
21694 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21695 
21696 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21697 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21698 	    bufaddr, buflen, start_block, block_count);
21699 
21700 	bzero(&cdb, sizeof (cdb));
21701 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21702 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21703 
21704 	/* Compute CDB size to use */
21705 	if (start_block > 0xffffffff)
21706 		cdbsize = CDB_GROUP4;
21707 	else if ((start_block & 0xFFE00000) ||
21708 	    (un->un_f_cfg_is_atapi == TRUE))
21709 		cdbsize = CDB_GROUP1;
21710 	else
21711 		cdbsize = CDB_GROUP0;
21712 
21713 	switch (cdbsize) {
21714 	case CDB_GROUP0:	/* 6-byte CDBs */
21715 		cdb.scc_cmd = cmd;
21716 		FORMG0ADDR(&cdb, start_block);
21717 		FORMG0COUNT(&cdb, block_count);
21718 		break;
21719 	case CDB_GROUP1:	/* 10-byte CDBs */
21720 		cdb.scc_cmd = cmd | SCMD_GROUP1;
21721 		FORMG1ADDR(&cdb, start_block);
21722 		FORMG1COUNT(&cdb, block_count);
21723 		break;
21724 	case CDB_GROUP4:	/* 16-byte CDBs */
21725 		cdb.scc_cmd = cmd | SCMD_GROUP4;
21726 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21727 		FORMG4COUNT(&cdb, block_count);
21728 		break;
21729 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
21730 	default:
21731 		/* All others reserved */
21732 		return (EINVAL);
21733 	}
21734 
21735 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21736 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21737 
21738 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21739 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21740 	ucmd_buf.uscsi_bufaddr	= bufaddr;
21741 	ucmd_buf.uscsi_buflen	= buflen;
21742 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21743 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21744 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
21745 	ucmd_buf.uscsi_timeout	= 60;
21746 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21747 	    UIO_SYSSPACE, path_flag);
21748 
21749 	switch (status) {
21750 	case 0:
21751 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21752 		break;	/* Success! */
21753 	case EIO:
21754 		switch (ucmd_buf.uscsi_status) {
21755 		case STATUS_RESERVATION_CONFLICT:
21756 			status = EACCES;
21757 			break;
21758 		default:
21759 			break;
21760 		}
21761 		break;
21762 	default:
21763 		break;
21764 	}
21765 
21766 	if (status == 0) {
21767 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21768 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21769 	}
21770 
21771 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21772 
21773 	return (status);
21774 }
21775 
21776 
21777 /*
21778  *    Function: sd_send_scsi_LOG_SENSE
21779  *
21780  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21781  *
21782  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21783  *                      structure for this target.
21784  *
21785  * Return Code: 0   - Success
21786  *		errno return code from sd_ssc_send()
21787  *
21788  *     Context: Can sleep. Does not return until command is completed.
21789  */
21790 
21791 static int
21792 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21793 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21794 	int path_flag)
21795 
21796 {
21797 	struct scsi_extended_sense	sense_buf;
21798 	union scsi_cdb		cdb;
21799 	struct uscsi_cmd	ucmd_buf;
21800 	int			status;
21801 	struct sd_lun		*un;
21802 
21803 	ASSERT(ssc != NULL);
21804 	un = ssc->ssc_un;
21805 	ASSERT(un != NULL);
21806 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21807 
21808 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21809 
21810 	bzero(&cdb, sizeof (cdb));
21811 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21812 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21813 
21814 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21815 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21816 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21817 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21818 	FORMG1COUNT(&cdb, buflen);
21819 
21820 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21821 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21822 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21823 	ucmd_buf.uscsi_buflen	= buflen;
21824 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21825 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21826 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21827 	ucmd_buf.uscsi_timeout	= 60;
21828 
21829 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21830 	    UIO_SYSSPACE, path_flag);
21831 
21832 	switch (status) {
21833 	case 0:
21834 		break;
21835 	case EIO:
21836 		switch (ucmd_buf.uscsi_status) {
21837 		case STATUS_RESERVATION_CONFLICT:
21838 			status = EACCES;
21839 			break;
21840 		case STATUS_CHECK:
21841 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21842 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21843 				KEY_ILLEGAL_REQUEST) &&
21844 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21845 				/*
21846 				 * ASC 0x24: INVALID FIELD IN CDB
21847 				 */
21848 				switch (page_code) {
21849 				case START_STOP_CYCLE_PAGE:
21850 					/*
21851 					 * The start stop cycle counter is
21852 					 * implemented as page 0x31 in earlier
21853 					 * generation disks. In new generation
21854 					 * disks the start stop cycle counter is
21855 					 * implemented as page 0xE. To properly
21856 					 * handle this case if an attempt for
21857 					 * log page 0xE is made and fails we
21858 					 * will try again using page 0x31.
21859 					 *
21860 					 * Network storage BU committed to
21861 					 * maintain the page 0x31 for this
21862 					 * purpose and will not have any other
21863 					 * page implemented with page code 0x31
21864 					 * until all disks transition to the
21865 					 * standard page.
21866 					 */
21867 					mutex_enter(SD_MUTEX(un));
21868 					un->un_start_stop_cycle_page =
21869 					    START_STOP_CYCLE_VU_PAGE;
21870 					cdb.cdb_opaque[2] =
21871 					    (char)(page_control << 6) |
21872 					    un->un_start_stop_cycle_page;
21873 					mutex_exit(SD_MUTEX(un));
21874 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
21875 					status = sd_ssc_send(
21876 					    ssc, &ucmd_buf, FKIOCTL,
21877 					    UIO_SYSSPACE, path_flag);
21878 
21879 					break;
21880 				case TEMPERATURE_PAGE:
21881 					status = ENOTTY;
21882 					break;
21883 				default:
21884 					break;
21885 				}
21886 			}
21887 			break;
21888 		default:
21889 			break;
21890 		}
21891 		break;
21892 	default:
21893 		break;
21894 	}
21895 
21896 	if (status == 0) {
21897 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21898 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
21899 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21900 	}
21901 
21902 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
21903 
21904 	return (status);
21905 }
21906 
21907 
21908 /*
21909  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
21910  *
21911  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
21912  *
21913  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21914  *                      structure for this target.
21915  *		bufaddr
21916  *		buflen
21917  *		class_req
21918  *
21919  * Return Code: 0   - Success
21920  *		errno return code from sd_ssc_send()
21921  *
21922  *     Context: Can sleep. Does not return until command is completed.
21923  */
21924 
21925 static int
21926 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
21927 	size_t buflen, uchar_t class_req)
21928 {
21929 	union scsi_cdb		cdb;
21930 	struct uscsi_cmd	ucmd_buf;
21931 	int			status;
21932 	struct sd_lun		*un;
21933 
21934 	ASSERT(ssc != NULL);
21935 	un = ssc->ssc_un;
21936 	ASSERT(un != NULL);
21937 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21938 	ASSERT(bufaddr != NULL);
21939 
21940 	SD_TRACE(SD_LOG_IO, un,
21941 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
21942 
21943 	bzero(&cdb, sizeof (cdb));
21944 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21945 	bzero(bufaddr, buflen);
21946 
21947 	cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
21948 	cdb.cdb_opaque[1] = 1; /* polled */
21949 	cdb.cdb_opaque[4] = class_req;
21950 	FORMG1COUNT(&cdb, buflen);
21951 
21952 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21953 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21954 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21955 	ucmd_buf.uscsi_buflen	= buflen;
21956 	ucmd_buf.uscsi_rqbuf	= NULL;
21957 	ucmd_buf.uscsi_rqlen	= 0;
21958 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
21959 	ucmd_buf.uscsi_timeout	= 60;
21960 
21961 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21962 	    UIO_SYSSPACE, SD_PATH_DIRECT);
21963 
21964 	/*
21965 	 * Only handle status == 0, the upper-level caller
21966 	 * will put different assessment based on the context.
21967 	 */
21968 	if (status == 0) {
21969 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21970 
21971 		if (ucmd_buf.uscsi_resid != 0) {
21972 			status = EIO;
21973 		}
21974 	}
21975 
21976 	SD_TRACE(SD_LOG_IO, un,
21977 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
21978 
21979 	return (status);
21980 }
21981 
21982 
21983 static boolean_t
21984 sd_gesn_media_data_valid(uchar_t *data)
21985 {
21986 	uint16_t			len;
21987 
21988 	len = (data[1] << 8) | data[0];
21989 	return ((len >= 6) &&
21990 	    ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
21991 	    ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
21992 	    ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
21993 }
21994 
21995 
21996 /*
21997  *    Function: sdioctl
21998  *
21999  * Description: Driver's ioctl(9e) entry point function.
22000  *
22001  *   Arguments: dev     - device number
22002  *		cmd     - ioctl operation to be performed
22003  *		arg     - user argument, contains data to be set or reference
22004  *			  parameter for get
22005  *		flag    - bit flag, indicating open settings, 32/64 bit type
22006  *		cred_p  - user credential pointer
22007  *		rval_p  - calling process return value (OPT)
22008  *
22009  * Return Code: EINVAL
22010  *		ENOTTY
22011  *		ENXIO
22012  *		EIO
22013  *		EFAULT
22014  *		ENOTSUP
22015  *		EPERM
22016  *
22017  *     Context: Called from the device switch at normal priority.
22018  */
22019 
22020 static int
22021 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22022 {
22023 	struct sd_lun	*un = NULL;
22024 	int		err = 0;
22025 	int		i = 0;
22026 	cred_t		*cr;
22027 	int		tmprval = EINVAL;
22028 	boolean_t	is_valid;
22029 	sd_ssc_t	*ssc;
22030 
22031 	/*
22032 	 * All device accesses go thru sdstrategy where we check on suspend
22033 	 * status
22034 	 */
22035 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22036 		return (ENXIO);
22037 	}
22038 
22039 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22040 
22041 	/* Initialize sd_ssc_t for internal uscsi commands */
22042 	ssc = sd_ssc_init(un);
22043 
22044 	is_valid = SD_IS_VALID_LABEL(un);
22045 
22046 	/*
22047 	 * Moved this wait from sd_uscsi_strategy to here for
22048 	 * reasons of deadlock prevention. Internal driver commands,
22049 	 * specifically those to change a devices power level, result
22050 	 * in a call to sd_uscsi_strategy.
22051 	 */
22052 	mutex_enter(SD_MUTEX(un));
22053 	while ((un->un_state == SD_STATE_SUSPENDED) ||
22054 	    (un->un_state == SD_STATE_PM_CHANGING)) {
22055 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22056 	}
22057 	/*
22058 	 * Twiddling the counter here protects commands from now
22059 	 * through to the top of sd_uscsi_strategy. Without the
22060 	 * counter inc. a power down, for example, could get in
22061 	 * after the above check for state is made and before
22062 	 * execution gets to the top of sd_uscsi_strategy.
22063 	 * That would cause problems.
22064 	 */
22065 	un->un_ncmds_in_driver++;
22066 
22067 	if (!is_valid &&
22068 	    (flag & (FNDELAY | FNONBLOCK))) {
22069 		switch (cmd) {
22070 		case DKIOCGGEOM:	/* SD_PATH_DIRECT */
22071 		case DKIOCGVTOC:
22072 		case DKIOCGEXTVTOC:
22073 		case DKIOCGAPART:
22074 		case DKIOCPARTINFO:
22075 		case DKIOCEXTPARTINFO:
22076 		case DKIOCSGEOM:
22077 		case DKIOCSAPART:
22078 		case DKIOCGETEFI:
22079 		case DKIOCPARTITION:
22080 		case DKIOCSVTOC:
22081 		case DKIOCSEXTVTOC:
22082 		case DKIOCSETEFI:
22083 		case DKIOCGMBOOT:
22084 		case DKIOCSMBOOT:
22085 		case DKIOCG_PHYGEOM:
22086 		case DKIOCG_VIRTGEOM:
22087 #if defined(__i386) || defined(__amd64)
22088 		case DKIOCSETEXTPART:
22089 #endif
22090 			/* let cmlb handle it */
22091 			goto skip_ready_valid;
22092 
22093 		case CDROMPAUSE:
22094 		case CDROMRESUME:
22095 		case CDROMPLAYMSF:
22096 		case CDROMPLAYTRKIND:
22097 		case CDROMREADTOCHDR:
22098 		case CDROMREADTOCENTRY:
22099 		case CDROMSTOP:
22100 		case CDROMSTART:
22101 		case CDROMVOLCTRL:
22102 		case CDROMSUBCHNL:
22103 		case CDROMREADMODE2:
22104 		case CDROMREADMODE1:
22105 		case CDROMREADOFFSET:
22106 		case CDROMSBLKMODE:
22107 		case CDROMGBLKMODE:
22108 		case CDROMGDRVSPEED:
22109 		case CDROMSDRVSPEED:
22110 		case CDROMCDDA:
22111 		case CDROMCDXA:
22112 		case CDROMSUBCODE:
22113 			if (!ISCD(un)) {
22114 				un->un_ncmds_in_driver--;
22115 				ASSERT(un->un_ncmds_in_driver >= 0);
22116 				mutex_exit(SD_MUTEX(un));
22117 				err = ENOTTY;
22118 				goto done_without_assess;
22119 			}
22120 			break;
22121 		case FDEJECT:
22122 		case DKIOCEJECT:
22123 		case CDROMEJECT:
22124 			if (!un->un_f_eject_media_supported) {
22125 				un->un_ncmds_in_driver--;
22126 				ASSERT(un->un_ncmds_in_driver >= 0);
22127 				mutex_exit(SD_MUTEX(un));
22128 				err = ENOTTY;
22129 				goto done_without_assess;
22130 			}
22131 			break;
22132 		case DKIOCFLUSHWRITECACHE:
22133 			mutex_exit(SD_MUTEX(un));
22134 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22135 			if (err != 0) {
22136 				mutex_enter(SD_MUTEX(un));
22137 				un->un_ncmds_in_driver--;
22138 				ASSERT(un->un_ncmds_in_driver >= 0);
22139 				mutex_exit(SD_MUTEX(un));
22140 				err = EIO;
22141 				goto done_quick_assess;
22142 			}
22143 			mutex_enter(SD_MUTEX(un));
22144 			/* FALLTHROUGH */
22145 		case DKIOCREMOVABLE:
22146 		case DKIOCHOTPLUGGABLE:
22147 		case DKIOCINFO:
22148 		case DKIOCGMEDIAINFO:
22149 		case DKIOCGMEDIAINFOEXT:
22150 		case MHIOCENFAILFAST:
22151 		case MHIOCSTATUS:
22152 		case MHIOCTKOWN:
22153 		case MHIOCRELEASE:
22154 		case MHIOCGRP_INKEYS:
22155 		case MHIOCGRP_INRESV:
22156 		case MHIOCGRP_REGISTER:
22157 		case MHIOCGRP_RESERVE:
22158 		case MHIOCGRP_PREEMPTANDABORT:
22159 		case MHIOCGRP_REGISTERANDIGNOREKEY:
22160 		case CDROMCLOSETRAY:
22161 		case USCSICMD:
22162 			goto skip_ready_valid;
22163 		default:
22164 			break;
22165 		}
22166 
22167 		mutex_exit(SD_MUTEX(un));
22168 		err = sd_ready_and_valid(ssc, SDPART(dev));
22169 		mutex_enter(SD_MUTEX(un));
22170 
22171 		if (err != SD_READY_VALID) {
22172 			switch (cmd) {
22173 			case DKIOCSTATE:
22174 			case CDROMGDRVSPEED:
22175 			case CDROMSDRVSPEED:
22176 			case FDEJECT:	/* for eject command */
22177 			case DKIOCEJECT:
22178 			case CDROMEJECT:
22179 			case DKIOCREMOVABLE:
22180 			case DKIOCHOTPLUGGABLE:
22181 				break;
22182 			default:
22183 				if (un->un_f_has_removable_media) {
22184 					err = ENXIO;
22185 				} else {
22186 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
22187 					if (err == SD_RESERVED_BY_OTHERS) {
22188 						err = EACCES;
22189 					} else {
22190 						err = EIO;
22191 					}
22192 				}
22193 				un->un_ncmds_in_driver--;
22194 				ASSERT(un->un_ncmds_in_driver >= 0);
22195 				mutex_exit(SD_MUTEX(un));
22196 
22197 				goto done_without_assess;
22198 			}
22199 		}
22200 	}
22201 
22202 skip_ready_valid:
22203 	mutex_exit(SD_MUTEX(un));
22204 
22205 	switch (cmd) {
22206 	case DKIOCINFO:
22207 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22208 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22209 		break;
22210 
22211 	case DKIOCGMEDIAINFO:
22212 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22213 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
22214 		break;
22215 
22216 	case DKIOCGMEDIAINFOEXT:
22217 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22218 		err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22219 		break;
22220 
22221 	case DKIOCGGEOM:
22222 	case DKIOCGVTOC:
22223 	case DKIOCGEXTVTOC:
22224 	case DKIOCGAPART:
22225 	case DKIOCPARTINFO:
22226 	case DKIOCEXTPARTINFO:
22227 	case DKIOCSGEOM:
22228 	case DKIOCSAPART:
22229 	case DKIOCGETEFI:
22230 	case DKIOCPARTITION:
22231 	case DKIOCSVTOC:
22232 	case DKIOCSEXTVTOC:
22233 	case DKIOCSETEFI:
22234 	case DKIOCGMBOOT:
22235 	case DKIOCSMBOOT:
22236 	case DKIOCG_PHYGEOM:
22237 	case DKIOCG_VIRTGEOM:
22238 #if defined(__i386) || defined(__amd64)
22239 	case DKIOCSETEXTPART:
22240 #endif
22241 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22242 
22243 		/* TUR should spin up */
22244 
22245 		if (un->un_f_has_removable_media)
22246 			err = sd_send_scsi_TEST_UNIT_READY(ssc,
22247 			    SD_CHECK_FOR_MEDIA);
22248 
22249 		else
22250 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22251 
22252 		if (err != 0)
22253 			goto done_with_assess;
22254 
22255 		err = cmlb_ioctl(un->un_cmlbhandle, dev,
22256 		    cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22257 
22258 		if ((err == 0) &&
22259 		    ((cmd == DKIOCSETEFI) ||
22260 		    (un->un_f_pkstats_enabled) &&
22261 		    (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22262 		    cmd == DKIOCSEXTVTOC))) {
22263 
22264 			tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22265 			    (void *)SD_PATH_DIRECT);
22266 			if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22267 				sd_set_pstats(un);
22268 				SD_TRACE(SD_LOG_IO_PARTITION, un,
22269 				    "sd_ioctl: un:0x%p pstats created and "
22270 				    "set\n", un);
22271 			}
22272 		}
22273 
22274 		if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22275 		    ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22276 
22277 			mutex_enter(SD_MUTEX(un));
22278 			if (un->un_f_devid_supported &&
22279 			    (un->un_f_opt_fab_devid == TRUE)) {
22280 				if (un->un_devid == NULL) {
22281 					sd_register_devid(ssc, SD_DEVINFO(un),
22282 					    SD_TARGET_IS_UNRESERVED);
22283 				} else {
22284 					/*
22285 					 * The device id for this disk
22286 					 * has been fabricated. The
22287 					 * device id must be preserved
22288 					 * by writing it back out to
22289 					 * disk.
22290 					 */
22291 					if (sd_write_deviceid(ssc) != 0) {
22292 						ddi_devid_free(un->un_devid);
22293 						un->un_devid = NULL;
22294 					}
22295 				}
22296 			}
22297 			mutex_exit(SD_MUTEX(un));
22298 		}
22299 
22300 		break;
22301 
22302 	case DKIOCLOCK:
22303 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22304 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22305 		    SD_PATH_STANDARD);
22306 		goto done_with_assess;
22307 
22308 	case DKIOCUNLOCK:
22309 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22310 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22311 		    SD_PATH_STANDARD);
22312 		goto done_with_assess;
22313 
22314 	case DKIOCSTATE: {
22315 		enum dkio_state		state;
22316 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22317 
22318 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22319 			err = EFAULT;
22320 		} else {
22321 			err = sd_check_media(dev, state);
22322 			if (err == 0) {
22323 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
22324 				    sizeof (int), flag) != 0)
22325 					err = EFAULT;
22326 			}
22327 		}
22328 		break;
22329 	}
22330 
22331 	case DKIOCREMOVABLE:
22332 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22333 		i = un->un_f_has_removable_media ? 1 : 0;
22334 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22335 			err = EFAULT;
22336 		} else {
22337 			err = 0;
22338 		}
22339 		break;
22340 
22341 	case DKIOCHOTPLUGGABLE:
22342 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22343 		i = un->un_f_is_hotpluggable ? 1 : 0;
22344 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22345 			err = EFAULT;
22346 		} else {
22347 			err = 0;
22348 		}
22349 		break;
22350 
22351 	case DKIOCGTEMPERATURE:
22352 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22353 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22354 		break;
22355 
22356 	case MHIOCENFAILFAST:
22357 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22358 		if ((err = drv_priv(cred_p)) == 0) {
22359 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22360 		}
22361 		break;
22362 
22363 	case MHIOCTKOWN:
22364 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22365 		if ((err = drv_priv(cred_p)) == 0) {
22366 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22367 		}
22368 		break;
22369 
22370 	case MHIOCRELEASE:
22371 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22372 		if ((err = drv_priv(cred_p)) == 0) {
22373 			err = sd_mhdioc_release(dev);
22374 		}
22375 		break;
22376 
22377 	case MHIOCSTATUS:
22378 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22379 		if ((err = drv_priv(cred_p)) == 0) {
22380 			switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22381 			case 0:
22382 				err = 0;
22383 				break;
22384 			case EACCES:
22385 				*rval_p = 1;
22386 				err = 0;
22387 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22388 				break;
22389 			default:
22390 				err = EIO;
22391 				goto done_with_assess;
22392 			}
22393 		}
22394 		break;
22395 
22396 	case MHIOCQRESERVE:
22397 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22398 		if ((err = drv_priv(cred_p)) == 0) {
22399 			err = sd_reserve_release(dev, SD_RESERVE);
22400 		}
22401 		break;
22402 
22403 	case MHIOCREREGISTERDEVID:
22404 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22405 		if (drv_priv(cred_p) == EPERM) {
22406 			err = EPERM;
22407 		} else if (!un->un_f_devid_supported) {
22408 			err = ENOTTY;
22409 		} else {
22410 			err = sd_mhdioc_register_devid(dev);
22411 		}
22412 		break;
22413 
22414 	case MHIOCGRP_INKEYS:
22415 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22416 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22417 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22418 				err = ENOTSUP;
22419 			} else {
22420 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22421 				    flag);
22422 			}
22423 		}
22424 		break;
22425 
22426 	case MHIOCGRP_INRESV:
22427 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22428 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22429 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22430 				err = ENOTSUP;
22431 			} else {
22432 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22433 			}
22434 		}
22435 		break;
22436 
22437 	case MHIOCGRP_REGISTER:
22438 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22439 		if ((err = drv_priv(cred_p)) != EPERM) {
22440 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22441 				err = ENOTSUP;
22442 			} else if (arg != NULL) {
22443 				mhioc_register_t reg;
22444 				if (ddi_copyin((void *)arg, &reg,
22445 				    sizeof (mhioc_register_t), flag) != 0) {
22446 					err = EFAULT;
22447 				} else {
22448 					err =
22449 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22450 					    ssc, SD_SCSI3_REGISTER,
22451 					    (uchar_t *)&reg);
22452 					if (err != 0)
22453 						goto done_with_assess;
22454 				}
22455 			}
22456 		}
22457 		break;
22458 
22459 	case MHIOCGRP_RESERVE:
22460 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22461 		if ((err = drv_priv(cred_p)) != EPERM) {
22462 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22463 				err = ENOTSUP;
22464 			} else if (arg != NULL) {
22465 				mhioc_resv_desc_t resv_desc;
22466 				if (ddi_copyin((void *)arg, &resv_desc,
22467 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
22468 					err = EFAULT;
22469 				} else {
22470 					err =
22471 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22472 					    ssc, SD_SCSI3_RESERVE,
22473 					    (uchar_t *)&resv_desc);
22474 					if (err != 0)
22475 						goto done_with_assess;
22476 				}
22477 			}
22478 		}
22479 		break;
22480 
22481 	case MHIOCGRP_PREEMPTANDABORT:
22482 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22483 		if ((err = drv_priv(cred_p)) != EPERM) {
22484 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22485 				err = ENOTSUP;
22486 			} else if (arg != NULL) {
22487 				mhioc_preemptandabort_t preempt_abort;
22488 				if (ddi_copyin((void *)arg, &preempt_abort,
22489 				    sizeof (mhioc_preemptandabort_t),
22490 				    flag) != 0) {
22491 					err = EFAULT;
22492 				} else {
22493 					err =
22494 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22495 					    ssc, SD_SCSI3_PREEMPTANDABORT,
22496 					    (uchar_t *)&preempt_abort);
22497 					if (err != 0)
22498 						goto done_with_assess;
22499 				}
22500 			}
22501 		}
22502 		break;
22503 
22504 	case MHIOCGRP_REGISTERANDIGNOREKEY:
22505 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22506 		if ((err = drv_priv(cred_p)) != EPERM) {
22507 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22508 				err = ENOTSUP;
22509 			} else if (arg != NULL) {
22510 				mhioc_registerandignorekey_t r_and_i;
22511 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
22512 				    sizeof (mhioc_registerandignorekey_t),
22513 				    flag) != 0) {
22514 					err = EFAULT;
22515 				} else {
22516 					err =
22517 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22518 					    ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22519 					    (uchar_t *)&r_and_i);
22520 					if (err != 0)
22521 						goto done_with_assess;
22522 				}
22523 			}
22524 		}
22525 		break;
22526 
22527 	case USCSICMD:
22528 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22529 		cr = ddi_get_cred();
22530 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22531 			err = EPERM;
22532 		} else {
22533 			enum uio_seg	uioseg;
22534 
22535 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22536 			    UIO_USERSPACE;
22537 			if (un->un_f_format_in_progress == TRUE) {
22538 				err = EAGAIN;
22539 				break;
22540 			}
22541 
22542 			err = sd_ssc_send(ssc,
22543 			    (struct uscsi_cmd *)arg,
22544 			    flag, uioseg, SD_PATH_STANDARD);
22545 			if (err != 0)
22546 				goto done_with_assess;
22547 			else
22548 				sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22549 		}
22550 		break;
22551 
22552 	case CDROMPAUSE:
22553 	case CDROMRESUME:
22554 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22555 		if (!ISCD(un)) {
22556 			err = ENOTTY;
22557 		} else {
22558 			err = sr_pause_resume(dev, cmd);
22559 		}
22560 		break;
22561 
22562 	case CDROMPLAYMSF:
22563 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22564 		if (!ISCD(un)) {
22565 			err = ENOTTY;
22566 		} else {
22567 			err = sr_play_msf(dev, (caddr_t)arg, flag);
22568 		}
22569 		break;
22570 
22571 	case CDROMPLAYTRKIND:
22572 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22573 #if defined(__i386) || defined(__amd64)
22574 		/*
22575 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22576 		 */
22577 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22578 #else
22579 		if (!ISCD(un)) {
22580 #endif
22581 			err = ENOTTY;
22582 		} else {
22583 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
22584 		}
22585 		break;
22586 
22587 	case CDROMREADTOCHDR:
22588 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22589 		if (!ISCD(un)) {
22590 			err = ENOTTY;
22591 		} else {
22592 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22593 		}
22594 		break;
22595 
22596 	case CDROMREADTOCENTRY:
22597 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22598 		if (!ISCD(un)) {
22599 			err = ENOTTY;
22600 		} else {
22601 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22602 		}
22603 		break;
22604 
22605 	case CDROMSTOP:
22606 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22607 		if (!ISCD(un)) {
22608 			err = ENOTTY;
22609 		} else {
22610 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22611 			    SD_TARGET_STOP, SD_PATH_STANDARD);
22612 			goto done_with_assess;
22613 		}
22614 		break;
22615 
22616 	case CDROMSTART:
22617 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22618 		if (!ISCD(un)) {
22619 			err = ENOTTY;
22620 		} else {
22621 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22622 			    SD_TARGET_START, SD_PATH_STANDARD);
22623 			goto done_with_assess;
22624 		}
22625 		break;
22626 
22627 	case CDROMCLOSETRAY:
22628 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22629 		if (!ISCD(un)) {
22630 			err = ENOTTY;
22631 		} else {
22632 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22633 			    SD_TARGET_CLOSE, SD_PATH_STANDARD);
22634 			goto done_with_assess;
22635 		}
22636 		break;
22637 
22638 	case FDEJECT:	/* for eject command */
22639 	case DKIOCEJECT:
22640 	case CDROMEJECT:
22641 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22642 		if (!un->un_f_eject_media_supported) {
22643 			err = ENOTTY;
22644 		} else {
22645 			err = sr_eject(dev);
22646 		}
22647 		break;
22648 
22649 	case CDROMVOLCTRL:
22650 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22651 		if (!ISCD(un)) {
22652 			err = ENOTTY;
22653 		} else {
22654 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22655 		}
22656 		break;
22657 
22658 	case CDROMSUBCHNL:
22659 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22660 		if (!ISCD(un)) {
22661 			err = ENOTTY;
22662 		} else {
22663 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22664 		}
22665 		break;
22666 
22667 	case CDROMREADMODE2:
22668 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22669 		if (!ISCD(un)) {
22670 			err = ENOTTY;
22671 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22672 			/*
22673 			 * If the drive supports READ CD, use that instead of
22674 			 * switching the LBA size via a MODE SELECT
22675 			 * Block Descriptor
22676 			 */
22677 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22678 		} else {
22679 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
22680 		}
22681 		break;
22682 
22683 	case CDROMREADMODE1:
22684 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22685 		if (!ISCD(un)) {
22686 			err = ENOTTY;
22687 		} else {
22688 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
22689 		}
22690 		break;
22691 
22692 	case CDROMREADOFFSET:
22693 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22694 		if (!ISCD(un)) {
22695 			err = ENOTTY;
22696 		} else {
22697 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22698 			    flag);
22699 		}
22700 		break;
22701 
22702 	case CDROMSBLKMODE:
22703 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22704 		/*
22705 		 * There is no means of changing block size in case of atapi
22706 		 * drives, thus return ENOTTY if drive type is atapi
22707 		 */
22708 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22709 			err = ENOTTY;
22710 		} else if (un->un_f_mmc_cap == TRUE) {
22711 
22712 			/*
22713 			 * MMC Devices do not support changing the
22714 			 * logical block size
22715 			 *
22716 			 * Note: EINVAL is being returned instead of ENOTTY to
22717 			 * maintain consistancy with the original mmc
22718 			 * driver update.
22719 			 */
22720 			err = EINVAL;
22721 		} else {
22722 			mutex_enter(SD_MUTEX(un));
22723 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22724 			    (un->un_ncmds_in_transport > 0)) {
22725 				mutex_exit(SD_MUTEX(un));
22726 				err = EINVAL;
22727 			} else {
22728 				mutex_exit(SD_MUTEX(un));
22729 				err = sr_change_blkmode(dev, cmd, arg, flag);
22730 			}
22731 		}
22732 		break;
22733 
22734 	case CDROMGBLKMODE:
22735 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22736 		if (!ISCD(un)) {
22737 			err = ENOTTY;
22738 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
22739 		    (un->un_f_blockcount_is_valid != FALSE)) {
22740 			/*
22741 			 * Drive is an ATAPI drive so return target block
22742 			 * size for ATAPI drives since we cannot change the
22743 			 * blocksize on ATAPI drives. Used primarily to detect
22744 			 * if an ATAPI cdrom is present.
22745 			 */
22746 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22747 			    sizeof (int), flag) != 0) {
22748 				err = EFAULT;
22749 			} else {
22750 				err = 0;
22751 			}
22752 
22753 		} else {
22754 			/*
22755 			 * Drive supports changing block sizes via a Mode
22756 			 * Select.
22757 			 */
22758 			err = sr_change_blkmode(dev, cmd, arg, flag);
22759 		}
22760 		break;
22761 
22762 	case CDROMGDRVSPEED:
22763 	case CDROMSDRVSPEED:
22764 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22765 		if (!ISCD(un)) {
22766 			err = ENOTTY;
22767 		} else if (un->un_f_mmc_cap == TRUE) {
22768 			/*
22769 			 * Note: In the future the driver implementation
22770 			 * for getting and
22771 			 * setting cd speed should entail:
22772 			 * 1) If non-mmc try the Toshiba mode page
22773 			 *    (sr_change_speed)
22774 			 * 2) If mmc but no support for Real Time Streaming try
22775 			 *    the SET CD SPEED (0xBB) command
22776 			 *   (sr_atapi_change_speed)
22777 			 * 3) If mmc and support for Real Time Streaming
22778 			 *    try the GET PERFORMANCE and SET STREAMING
22779 			 *    commands (not yet implemented, 4380808)
22780 			 */
22781 			/*
22782 			 * As per recent MMC spec, CD-ROM speed is variable
22783 			 * and changes with LBA. Since there is no such
22784 			 * things as drive speed now, fail this ioctl.
22785 			 *
22786 			 * Note: EINVAL is returned for consistancy of original
22787 			 * implementation which included support for getting
22788 			 * the drive speed of mmc devices but not setting
22789 			 * the drive speed. Thus EINVAL would be returned
22790 			 * if a set request was made for an mmc device.
22791 			 * We no longer support get or set speed for
22792 			 * mmc but need to remain consistent with regard
22793 			 * to the error code returned.
22794 			 */
22795 			err = EINVAL;
22796 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22797 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
22798 		} else {
22799 			err = sr_change_speed(dev, cmd, arg, flag);
22800 		}
22801 		break;
22802 
22803 	case CDROMCDDA:
22804 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22805 		if (!ISCD(un)) {
22806 			err = ENOTTY;
22807 		} else {
22808 			err = sr_read_cdda(dev, (void *)arg, flag);
22809 		}
22810 		break;
22811 
22812 	case CDROMCDXA:
22813 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
22814 		if (!ISCD(un)) {
22815 			err = ENOTTY;
22816 		} else {
22817 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
22818 		}
22819 		break;
22820 
22821 	case CDROMSUBCODE:
22822 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
22823 		if (!ISCD(un)) {
22824 			err = ENOTTY;
22825 		} else {
22826 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
22827 		}
22828 		break;
22829 
22830 
22831 #ifdef SDDEBUG
22832 /* RESET/ABORTS testing ioctls */
22833 	case DKIOCRESET: {
22834 		int	reset_level;
22835 
22836 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
22837 			err = EFAULT;
22838 		} else {
22839 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
22840 			    "reset_level = 0x%lx\n", reset_level);
22841 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
22842 				err = 0;
22843 			} else {
22844 				err = EIO;
22845 			}
22846 		}
22847 		break;
22848 	}
22849 
22850 	case DKIOCABORT:
22851 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
22852 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
22853 			err = 0;
22854 		} else {
22855 			err = EIO;
22856 		}
22857 		break;
22858 #endif
22859 
22860 #ifdef SD_FAULT_INJECTION
22861 /* SDIOC FaultInjection testing ioctls */
22862 	case SDIOCSTART:
22863 	case SDIOCSTOP:
22864 	case SDIOCINSERTPKT:
22865 	case SDIOCINSERTXB:
22866 	case SDIOCINSERTUN:
22867 	case SDIOCINSERTARQ:
22868 	case SDIOCPUSH:
22869 	case SDIOCRETRIEVE:
22870 	case SDIOCRUN:
22871 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
22872 		    "SDIOC detected cmd:0x%X:\n", cmd);
22873 		/* call error generator */
22874 		sd_faultinjection_ioctl(cmd, arg, un);
22875 		err = 0;
22876 		break;
22877 
22878 #endif /* SD_FAULT_INJECTION */
22879 
22880 	case DKIOCFLUSHWRITECACHE:
22881 		{
22882 			struct dk_callback *dkc = (struct dk_callback *)arg;
22883 
22884 			mutex_enter(SD_MUTEX(un));
22885 			if (!un->un_f_sync_cache_supported ||
22886 			    !un->un_f_write_cache_enabled) {
22887 				err = un->un_f_sync_cache_supported ?
22888 				    0 : ENOTSUP;
22889 				mutex_exit(SD_MUTEX(un));
22890 				if ((flag & FKIOCTL) && dkc != NULL &&
22891 				    dkc->dkc_callback != NULL) {
22892 					(*dkc->dkc_callback)(dkc->dkc_cookie,
22893 					    err);
22894 					/*
22895 					 * Did callback and reported error.
22896 					 * Since we did a callback, ioctl
22897 					 * should return 0.
22898 					 */
22899 					err = 0;
22900 				}
22901 				break;
22902 			}
22903 			mutex_exit(SD_MUTEX(un));
22904 
22905 			if ((flag & FKIOCTL) && dkc != NULL &&
22906 			    dkc->dkc_callback != NULL) {
22907 				/* async SYNC CACHE request */
22908 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
22909 			} else {
22910 				/* synchronous SYNC CACHE request */
22911 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22912 			}
22913 		}
22914 		break;
22915 
22916 	case DKIOCGETWCE: {
22917 
22918 		int wce;
22919 
22920 		if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
22921 			break;
22922 		}
22923 
22924 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
22925 			err = EFAULT;
22926 		}
22927 		break;
22928 	}
22929 
22930 	case DKIOCSETWCE: {
22931 
22932 		int wce, sync_supported;
22933 		int cur_wce = 0;
22934 
22935 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
22936 			err = EFAULT;
22937 			break;
22938 		}
22939 
22940 		/*
22941 		 * Synchronize multiple threads trying to enable
22942 		 * or disable the cache via the un_f_wcc_cv
22943 		 * condition variable.
22944 		 */
22945 		mutex_enter(SD_MUTEX(un));
22946 
22947 		/*
22948 		 * Don't allow the cache to be enabled if the
22949 		 * config file has it disabled.
22950 		 */
22951 		if (un->un_f_opt_disable_cache && wce) {
22952 			mutex_exit(SD_MUTEX(un));
22953 			err = EINVAL;
22954 			break;
22955 		}
22956 
22957 		/*
22958 		 * Wait for write cache change in progress
22959 		 * bit to be clear before proceeding.
22960 		 */
22961 		while (un->un_f_wcc_inprog)
22962 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
22963 
22964 		un->un_f_wcc_inprog = 1;
22965 
22966 		mutex_exit(SD_MUTEX(un));
22967 
22968 		/*
22969 		 * Get the current write cache state
22970 		 */
22971 		if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
22972 			break;
22973 		}
22974 
22975 		mutex_enter(SD_MUTEX(un));
22976 		un->un_f_write_cache_enabled = (cur_wce != 0);
22977 
22978 		if (un->un_f_write_cache_enabled && wce == 0) {
22979 			/*
22980 			 * Disable the write cache.  Don't clear
22981 			 * un_f_write_cache_enabled until after
22982 			 * the mode select and flush are complete.
22983 			 */
22984 			sync_supported = un->un_f_sync_cache_supported;
22985 
22986 			/*
22987 			 * If cache flush is suppressed, we assume that the
22988 			 * controller firmware will take care of managing the
22989 			 * write cache for us: no need to explicitly
22990 			 * disable it.
22991 			 */
22992 			if (!un->un_f_suppress_cache_flush) {
22993 				mutex_exit(SD_MUTEX(un));
22994 				if ((err = sd_cache_control(ssc,
22995 				    SD_CACHE_NOCHANGE,
22996 				    SD_CACHE_DISABLE)) == 0 &&
22997 				    sync_supported) {
22998 					err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
22999 					    NULL);
23000 				}
23001 			} else {
23002 				mutex_exit(SD_MUTEX(un));
23003 			}
23004 
23005 			mutex_enter(SD_MUTEX(un));
23006 			if (err == 0) {
23007 				un->un_f_write_cache_enabled = 0;
23008 			}
23009 
23010 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
23011 			/*
23012 			 * Set un_f_write_cache_enabled first, so there is
23013 			 * no window where the cache is enabled, but the
23014 			 * bit says it isn't.
23015 			 */
23016 			un->un_f_write_cache_enabled = 1;
23017 
23018 			/*
23019 			 * If cache flush is suppressed, we assume that the
23020 			 * controller firmware will take care of managing the
23021 			 * write cache for us: no need to explicitly
23022 			 * enable it.
23023 			 */
23024 			if (!un->un_f_suppress_cache_flush) {
23025 				mutex_exit(SD_MUTEX(un));
23026 				err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23027 				    SD_CACHE_ENABLE);
23028 			} else {
23029 				mutex_exit(SD_MUTEX(un));
23030 			}
23031 
23032 			mutex_enter(SD_MUTEX(un));
23033 
23034 			if (err) {
23035 				un->un_f_write_cache_enabled = 0;
23036 			}
23037 		}
23038 
23039 		un->un_f_wcc_inprog = 0;
23040 		cv_broadcast(&un->un_wcc_cv);
23041 		mutex_exit(SD_MUTEX(un));
23042 		break;
23043 	}
23044 
23045 	default:
23046 		err = ENOTTY;
23047 		break;
23048 	}
23049 	mutex_enter(SD_MUTEX(un));
23050 	un->un_ncmds_in_driver--;
23051 	ASSERT(un->un_ncmds_in_driver >= 0);
23052 	mutex_exit(SD_MUTEX(un));
23053 
23054 
23055 done_without_assess:
23056 	sd_ssc_fini(ssc);
23057 
23058 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23059 	return (err);
23060 
23061 done_with_assess:
23062 	mutex_enter(SD_MUTEX(un));
23063 	un->un_ncmds_in_driver--;
23064 	ASSERT(un->un_ncmds_in_driver >= 0);
23065 	mutex_exit(SD_MUTEX(un));
23066 
23067 done_quick_assess:
23068 	if (err != 0)
23069 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23070 	/* Uninitialize sd_ssc_t pointer */
23071 	sd_ssc_fini(ssc);
23072 
23073 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23074 	return (err);
23075 }
23076 
23077 
23078 /*
23079  *    Function: sd_dkio_ctrl_info
23080  *
23081  * Description: This routine is the driver entry point for handling controller
23082  *		information ioctl requests (DKIOCINFO).
23083  *
23084  *   Arguments: dev  - the device number
23085  *		arg  - pointer to user provided dk_cinfo structure
23086  *		       specifying the controller type and attributes.
23087  *		flag - this argument is a pass through to ddi_copyxxx()
23088  *		       directly from the mode argument of ioctl().
23089  *
23090  * Return Code: 0
23091  *		EFAULT
23092  *		ENXIO
23093  */
23094 
23095 static int
23096 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23097 {
23098 	struct sd_lun	*un = NULL;
23099 	struct dk_cinfo	*info;
23100 	dev_info_t	*pdip;
23101 	int		lun, tgt;
23102 
23103 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23104 		return (ENXIO);
23105 	}
23106 
23107 	info = (struct dk_cinfo *)
23108 	    kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23109 
23110 	switch (un->un_ctype) {
23111 	case CTYPE_CDROM:
23112 		info->dki_ctype = DKC_CDROM;
23113 		break;
23114 	default:
23115 		info->dki_ctype = DKC_SCSI_CCS;
23116 		break;
23117 	}
23118 	pdip = ddi_get_parent(SD_DEVINFO(un));
23119 	info->dki_cnum = ddi_get_instance(pdip);
23120 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23121 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
23122 	} else {
23123 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
23124 		    DK_DEVLEN - 1);
23125 	}
23126 
23127 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23128 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23129 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23130 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23131 
23132 	/* Unit Information */
23133 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23134 	info->dki_slave = ((tgt << 3) | lun);
23135 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23136 	    DK_DEVLEN - 1);
23137 	info->dki_flags = DKI_FMTVOL;
23138 	info->dki_partition = SDPART(dev);
23139 
23140 	/* Max Transfer size of this device in blocks */
23141 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23142 	info->dki_addr = 0;
23143 	info->dki_space = 0;
23144 	info->dki_prio = 0;
23145 	info->dki_vec = 0;
23146 
23147 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23148 		kmem_free(info, sizeof (struct dk_cinfo));
23149 		return (EFAULT);
23150 	} else {
23151 		kmem_free(info, sizeof (struct dk_cinfo));
23152 		return (0);
23153 	}
23154 }
23155 
23156 
23157 /*
23158  *    Function: sd_get_media_info
23159  *
23160  * Description: This routine is the driver entry point for handling ioctl
23161  *		requests for the media type or command set profile used by the
23162  *		drive to operate on the media (DKIOCGMEDIAINFO).
23163  *
23164  *   Arguments: dev	- the device number
23165  *		arg	- pointer to user provided dk_minfo structure
23166  *			  specifying the media type, logical block size and
23167  *			  drive capacity.
23168  *		flag	- this argument is a pass through to ddi_copyxxx()
23169  *			  directly from the mode argument of ioctl().
23170  *
23171  * Return Code: 0
23172  *		EACCESS
23173  *		EFAULT
23174  *		ENXIO
23175  *		EIO
23176  */
23177 
23178 static int
23179 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23180 {
23181 	struct sd_lun		*un = NULL;
23182 	struct uscsi_cmd	com;
23183 	struct scsi_inquiry	*sinq;
23184 	struct dk_minfo		media_info;
23185 	u_longlong_t		media_capacity;
23186 	uint64_t		capacity;
23187 	uint_t			lbasize;
23188 	uchar_t			*out_data;
23189 	uchar_t			*rqbuf;
23190 	int			rval = 0;
23191 	int			rtn;
23192 	sd_ssc_t		*ssc;
23193 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23194 	    (un->un_state == SD_STATE_OFFLINE)) {
23195 		return (ENXIO);
23196 	}
23197 
23198 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
23199 
23200 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23201 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23202 
23203 	/* Issue a TUR to determine if the drive is ready with media present */
23204 	ssc = sd_ssc_init(un);
23205 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23206 	if (rval == ENXIO) {
23207 		goto done;
23208 	} else if (rval != 0) {
23209 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23210 	}
23211 
23212 	/* Now get configuration data */
23213 	if (ISCD(un)) {
23214 		media_info.dki_media_type = DK_CDROM;
23215 
23216 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23217 		if (un->un_f_mmc_cap == TRUE) {
23218 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23219 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23220 			    SD_PATH_STANDARD);
23221 
23222 			if (rtn) {
23223 				/*
23224 				 * We ignore all failures for CD and need to
23225 				 * put the assessment before processing code
23226 				 * to avoid missing assessment for FMA.
23227 				 */
23228 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23229 				/*
23230 				 * Failed for other than an illegal request
23231 				 * or command not supported
23232 				 */
23233 				if ((com.uscsi_status == STATUS_CHECK) &&
23234 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23235 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23236 					    (rqbuf[12] != 0x20)) {
23237 						rval = EIO;
23238 						goto no_assessment;
23239 					}
23240 				}
23241 			} else {
23242 				/*
23243 				 * The GET CONFIGURATION command succeeded
23244 				 * so set the media type according to the
23245 				 * returned data
23246 				 */
23247 				media_info.dki_media_type = out_data[6];
23248 				media_info.dki_media_type <<= 8;
23249 				media_info.dki_media_type |= out_data[7];
23250 			}
23251 		}
23252 	} else {
23253 		/*
23254 		 * The profile list is not available, so we attempt to identify
23255 		 * the media type based on the inquiry data
23256 		 */
23257 		sinq = un->un_sd->sd_inq;
23258 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23259 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23260 			/* This is a direct access device  or optical disk */
23261 			media_info.dki_media_type = DK_FIXED_DISK;
23262 
23263 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23264 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23265 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23266 					media_info.dki_media_type = DK_ZIP;
23267 				} else if (
23268 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23269 					media_info.dki_media_type = DK_JAZ;
23270 				}
23271 			}
23272 		} else {
23273 			/*
23274 			 * Not a CD, direct access or optical disk so return
23275 			 * unknown media
23276 			 */
23277 			media_info.dki_media_type = DK_UNKNOWN;
23278 		}
23279 	}
23280 
23281 	/* Now read the capacity so we can provide the lbasize and capacity */
23282 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23283 	    SD_PATH_DIRECT);
23284 	switch (rval) {
23285 	case 0:
23286 		break;
23287 	case EACCES:
23288 		rval = EACCES;
23289 		goto done;
23290 	default:
23291 		rval = EIO;
23292 		goto done;
23293 	}
23294 
23295 	/*
23296 	 * If lun is expanded dynamically, update the un structure.
23297 	 */
23298 	mutex_enter(SD_MUTEX(un));
23299 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23300 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23301 	    (capacity > un->un_blockcount)) {
23302 		sd_update_block_info(un, lbasize, capacity);
23303 	}
23304 	mutex_exit(SD_MUTEX(un));
23305 
23306 	media_info.dki_lbsize = lbasize;
23307 	media_capacity = capacity;
23308 
23309 	/*
23310 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
23311 	 * un->un_sys_blocksize chunks. So we need to convert it into
23312 	 * cap.lbasize chunks.
23313 	 */
23314 	media_capacity *= un->un_sys_blocksize;
23315 	media_capacity /= lbasize;
23316 	media_info.dki_capacity = media_capacity;
23317 
23318 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
23319 		rval = EFAULT;
23320 		/* Put goto. Anybody might add some code below in future */
23321 		goto no_assessment;
23322 	}
23323 done:
23324 	if (rval != 0) {
23325 		if (rval == EIO)
23326 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23327 		else
23328 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23329 	}
23330 no_assessment:
23331 	sd_ssc_fini(ssc);
23332 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23333 	kmem_free(rqbuf, SENSE_LENGTH);
23334 	return (rval);
23335 }
23336 
23337 /*
23338  *    Function: sd_get_media_info_ext
23339  *
23340  * Description: This routine is the driver entry point for handling ioctl
23341  *		requests for the media type or command set profile used by the
23342  *		drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23343  *		difference this ioctl and DKIOCGMEDIAINFO is the return value
23344  *		of this ioctl contains both logical block size and physical
23345  *		block size.
23346  *
23347  *
23348  *   Arguments: dev	- the device number
23349  *		arg	- pointer to user provided dk_minfo_ext structure
23350  *			  specifying the media type, logical block size,
23351  *			  physical block size and disk capacity.
23352  *		flag	- this argument is a pass through to ddi_copyxxx()
23353  *			  directly from the mode argument of ioctl().
23354  *
23355  * Return Code: 0
23356  *		EACCESS
23357  *		EFAULT
23358  *		ENXIO
23359  *		EIO
23360  */
23361 
23362 static int
23363 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23364 {
23365 	struct sd_lun		*un = NULL;
23366 	struct uscsi_cmd	com;
23367 	struct scsi_inquiry	*sinq;
23368 	struct dk_minfo_ext	media_info_ext;
23369 	u_longlong_t		media_capacity;
23370 	uint64_t		capacity;
23371 	uint_t			lbasize;
23372 	uint_t			pbsize;
23373 	uchar_t			*out_data;
23374 	uchar_t			*rqbuf;
23375 	int			rval = 0;
23376 	int			rtn;
23377 	sd_ssc_t		*ssc;
23378 
23379 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23380 	    (un->un_state == SD_STATE_OFFLINE)) {
23381 		return (ENXIO);
23382 	}
23383 
23384 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_ext: entry\n");
23385 
23386 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23387 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23388 	ssc = sd_ssc_init(un);
23389 
23390 	/* Issue a TUR to determine if the drive is ready with media present */
23391 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23392 	if (rval == ENXIO) {
23393 		goto done;
23394 	} else if (rval != 0) {
23395 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23396 	}
23397 
23398 	/* Now get configuration data */
23399 	if (ISCD(un)) {
23400 		media_info_ext.dki_media_type = DK_CDROM;
23401 
23402 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23403 		if (un->un_f_mmc_cap == TRUE) {
23404 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23405 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23406 			    SD_PATH_STANDARD);
23407 
23408 			if (rtn) {
23409 				/*
23410 				 * We ignore all failures for CD and need to
23411 				 * put the assessment before processing code
23412 				 * to avoid missing assessment for FMA.
23413 				 */
23414 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23415 				/*
23416 				 * Failed for other than an illegal request
23417 				 * or command not supported
23418 				 */
23419 				if ((com.uscsi_status == STATUS_CHECK) &&
23420 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23421 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23422 					    (rqbuf[12] != 0x20)) {
23423 						rval = EIO;
23424 						goto no_assessment;
23425 					}
23426 				}
23427 			} else {
23428 				/*
23429 				 * The GET CONFIGURATION command succeeded
23430 				 * so set the media type according to the
23431 				 * returned data
23432 				 */
23433 				media_info_ext.dki_media_type = out_data[6];
23434 				media_info_ext.dki_media_type <<= 8;
23435 				media_info_ext.dki_media_type |= out_data[7];
23436 			}
23437 		}
23438 	} else {
23439 		/*
23440 		 * The profile list is not available, so we attempt to identify
23441 		 * the media type based on the inquiry data
23442 		 */
23443 		sinq = un->un_sd->sd_inq;
23444 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23445 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23446 			/* This is a direct access device  or optical disk */
23447 			media_info_ext.dki_media_type = DK_FIXED_DISK;
23448 
23449 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23450 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23451 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23452 					media_info_ext.dki_media_type = DK_ZIP;
23453 				} else if (
23454 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23455 					media_info_ext.dki_media_type = DK_JAZ;
23456 				}
23457 			}
23458 		} else {
23459 			/*
23460 			 * Not a CD, direct access or optical disk so return
23461 			 * unknown media
23462 			 */
23463 			media_info_ext.dki_media_type = DK_UNKNOWN;
23464 		}
23465 	}
23466 
23467 	/*
23468 	 * Now read the capacity so we can provide the lbasize,
23469 	 * pbsize and capacity.
23470 	 */
23471 	rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, &pbsize,
23472 	    SD_PATH_DIRECT);
23473 
23474 	if (rval != 0) {
23475 		rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23476 		    SD_PATH_DIRECT);
23477 
23478 		switch (rval) {
23479 		case 0:
23480 			pbsize = lbasize;
23481 			media_capacity = capacity;
23482 			/*
23483 			 * sd_send_scsi_READ_CAPACITY() reports capacity in
23484 			 * un->un_sys_blocksize chunks. So we need to convert
23485 			 * it into cap.lbsize chunks.
23486 			 */
23487 			if (un->un_f_has_removable_media) {
23488 				media_capacity *= un->un_sys_blocksize;
23489 				media_capacity /= lbasize;
23490 			}
23491 			break;
23492 		case EACCES:
23493 			rval = EACCES;
23494 			goto done;
23495 		default:
23496 			rval = EIO;
23497 			goto done;
23498 		}
23499 	} else {
23500 		media_capacity = capacity;
23501 	}
23502 
23503 	/*
23504 	 * If lun is expanded dynamically, update the un structure.
23505 	 */
23506 	mutex_enter(SD_MUTEX(un));
23507 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23508 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23509 	    (capacity > un->un_blockcount)) {
23510 		sd_update_block_info(un, lbasize, capacity);
23511 	}
23512 	mutex_exit(SD_MUTEX(un));
23513 
23514 	media_info_ext.dki_lbsize = lbasize;
23515 	media_info_ext.dki_capacity = media_capacity;
23516 	media_info_ext.dki_pbsize = pbsize;
23517 
23518 	if (ddi_copyout(&media_info_ext, arg, sizeof (struct dk_minfo_ext),
23519 	    flag)) {
23520 		rval = EFAULT;
23521 		goto no_assessment;
23522 	}
23523 done:
23524 	if (rval != 0) {
23525 		if (rval == EIO)
23526 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23527 		else
23528 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23529 	}
23530 no_assessment:
23531 	sd_ssc_fini(ssc);
23532 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23533 	kmem_free(rqbuf, SENSE_LENGTH);
23534 	return (rval);
23535 }
23536 
23537 /*
23538  *    Function: sd_watch_request_submit
23539  *
23540  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23541  *		depending on which is supported by device.
23542  */
23543 static opaque_t
23544 sd_watch_request_submit(struct sd_lun *un)
23545 {
23546 	dev_t			dev;
23547 
23548 	/* All submissions are unified to use same device number */
23549 	dev = sd_make_device(SD_DEVINFO(un));
23550 
23551 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23552 		return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23553 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23554 		    (caddr_t)dev));
23555 	} else {
23556 		return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23557 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23558 		    (caddr_t)dev));
23559 	}
23560 }
23561 
23562 
23563 /*
23564  *    Function: sd_check_media
23565  *
23566  * Description: This utility routine implements the functionality for the
23567  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23568  *		driver state changes from that specified by the user
23569  *		(inserted or ejected). For example, if the user specifies
23570  *		DKIO_EJECTED and the current media state is inserted this
23571  *		routine will immediately return DKIO_INSERTED. However, if the
23572  *		current media state is not inserted the user thread will be
23573  *		blocked until the drive state changes. If DKIO_NONE is specified
23574  *		the user thread will block until a drive state change occurs.
23575  *
23576  *   Arguments: dev  - the device number
23577  *		state  - user pointer to a dkio_state, updated with the current
23578  *			drive state at return.
23579  *
23580  * Return Code: ENXIO
23581  *		EIO
23582  *		EAGAIN
23583  *		EINTR
23584  */
23585 
23586 static int
23587 sd_check_media(dev_t dev, enum dkio_state state)
23588 {
23589 	struct sd_lun		*un = NULL;
23590 	enum dkio_state		prev_state;
23591 	opaque_t		token = NULL;
23592 	int			rval = 0;
23593 	sd_ssc_t		*ssc;
23594 
23595 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23596 		return (ENXIO);
23597 	}
23598 
23599 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23600 
23601 	ssc = sd_ssc_init(un);
23602 
23603 	mutex_enter(SD_MUTEX(un));
23604 
23605 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23606 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
23607 
23608 	prev_state = un->un_mediastate;
23609 
23610 	/* is there anything to do? */
23611 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23612 		/*
23613 		 * submit the request to the scsi_watch service;
23614 		 * scsi_media_watch_cb() does the real work
23615 		 */
23616 		mutex_exit(SD_MUTEX(un));
23617 
23618 		/*
23619 		 * This change handles the case where a scsi watch request is
23620 		 * added to a device that is powered down. To accomplish this
23621 		 * we power up the device before adding the scsi watch request,
23622 		 * since the scsi watch sends a TUR directly to the device
23623 		 * which the device cannot handle if it is powered down.
23624 		 */
23625 		if (sd_pm_entry(un) != DDI_SUCCESS) {
23626 			mutex_enter(SD_MUTEX(un));
23627 			goto done;
23628 		}
23629 
23630 		token = sd_watch_request_submit(un);
23631 
23632 		sd_pm_exit(un);
23633 
23634 		mutex_enter(SD_MUTEX(un));
23635 		if (token == NULL) {
23636 			rval = EAGAIN;
23637 			goto done;
23638 		}
23639 
23640 		/*
23641 		 * This is a special case IOCTL that doesn't return
23642 		 * until the media state changes. Routine sdpower
23643 		 * knows about and handles this so don't count it
23644 		 * as an active cmd in the driver, which would
23645 		 * keep the device busy to the pm framework.
23646 		 * If the count isn't decremented the device can't
23647 		 * be powered down.
23648 		 */
23649 		un->un_ncmds_in_driver--;
23650 		ASSERT(un->un_ncmds_in_driver >= 0);
23651 
23652 		/*
23653 		 * if a prior request had been made, this will be the same
23654 		 * token, as scsi_watch was designed that way.
23655 		 */
23656 		un->un_swr_token = token;
23657 		un->un_specified_mediastate = state;
23658 
23659 		/*
23660 		 * now wait for media change
23661 		 * we will not be signalled unless mediastate == state but it is
23662 		 * still better to test for this condition, since there is a
23663 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23664 		 */
23665 		SD_TRACE(SD_LOG_COMMON, un,
23666 		    "sd_check_media: waiting for media state change\n");
23667 		while (un->un_mediastate == state) {
23668 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23669 				SD_TRACE(SD_LOG_COMMON, un,
23670 				    "sd_check_media: waiting for media state "
23671 				    "was interrupted\n");
23672 				un->un_ncmds_in_driver++;
23673 				rval = EINTR;
23674 				goto done;
23675 			}
23676 			SD_TRACE(SD_LOG_COMMON, un,
23677 			    "sd_check_media: received signal, state=%x\n",
23678 			    un->un_mediastate);
23679 		}
23680 		/*
23681 		 * Inc the counter to indicate the device once again
23682 		 * has an active outstanding cmd.
23683 		 */
23684 		un->un_ncmds_in_driver++;
23685 	}
23686 
23687 	/* invalidate geometry */
23688 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23689 		sr_ejected(un);
23690 	}
23691 
23692 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23693 		uint64_t	capacity;
23694 		uint_t		lbasize;
23695 
23696 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23697 		mutex_exit(SD_MUTEX(un));
23698 		/*
23699 		 * Since the following routines use SD_PATH_DIRECT, we must
23700 		 * call PM directly before the upcoming disk accesses. This
23701 		 * may cause the disk to be power/spin up.
23702 		 */
23703 
23704 		if (sd_pm_entry(un) == DDI_SUCCESS) {
23705 			rval = sd_send_scsi_READ_CAPACITY(ssc,
23706 			    &capacity, &lbasize, SD_PATH_DIRECT);
23707 			if (rval != 0) {
23708 				sd_pm_exit(un);
23709 				if (rval == EIO)
23710 					sd_ssc_assessment(ssc,
23711 					    SD_FMT_STATUS_CHECK);
23712 				else
23713 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23714 				mutex_enter(SD_MUTEX(un));
23715 				goto done;
23716 			}
23717 		} else {
23718 			rval = EIO;
23719 			mutex_enter(SD_MUTEX(un));
23720 			goto done;
23721 		}
23722 		mutex_enter(SD_MUTEX(un));
23723 
23724 		sd_update_block_info(un, lbasize, capacity);
23725 
23726 		/*
23727 		 *  Check if the media in the device is writable or not
23728 		 */
23729 		if (ISCD(un)) {
23730 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23731 		}
23732 
23733 		mutex_exit(SD_MUTEX(un));
23734 		cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23735 		if ((cmlb_validate(un->un_cmlbhandle, 0,
23736 		    (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23737 			sd_set_pstats(un);
23738 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23739 			    "sd_check_media: un:0x%p pstats created and "
23740 			    "set\n", un);
23741 		}
23742 
23743 		rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23744 		    SD_PATH_DIRECT);
23745 
23746 		sd_pm_exit(un);
23747 
23748 		if (rval != 0) {
23749 			if (rval == EIO)
23750 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23751 			else
23752 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23753 		}
23754 
23755 		mutex_enter(SD_MUTEX(un));
23756 	}
23757 done:
23758 	sd_ssc_fini(ssc);
23759 	un->un_f_watcht_stopped = FALSE;
23760 	if (token != NULL && un->un_swr_token != NULL) {
23761 		/*
23762 		 * Use of this local token and the mutex ensures that we avoid
23763 		 * some race conditions associated with terminating the
23764 		 * scsi watch.
23765 		 */
23766 		token = un->un_swr_token;
23767 		mutex_exit(SD_MUTEX(un));
23768 		(void) scsi_watch_request_terminate(token,
23769 		    SCSI_WATCH_TERMINATE_WAIT);
23770 		if (scsi_watch_get_ref_count(token) == 0) {
23771 			mutex_enter(SD_MUTEX(un));
23772 			un->un_swr_token = (opaque_t)NULL;
23773 		} else {
23774 			mutex_enter(SD_MUTEX(un));
23775 		}
23776 	}
23777 
23778 	/*
23779 	 * Update the capacity kstat value, if no media previously
23780 	 * (capacity kstat is 0) and a media has been inserted
23781 	 * (un_f_blockcount_is_valid == TRUE)
23782 	 */
23783 	if (un->un_errstats) {
23784 		struct sd_errstats	*stp = NULL;
23785 
23786 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
23787 		if ((stp->sd_capacity.value.ui64 == 0) &&
23788 		    (un->un_f_blockcount_is_valid == TRUE)) {
23789 			stp->sd_capacity.value.ui64 =
23790 			    (uint64_t)((uint64_t)un->un_blockcount *
23791 			    un->un_sys_blocksize);
23792 		}
23793 	}
23794 	mutex_exit(SD_MUTEX(un));
23795 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23796 	return (rval);
23797 }
23798 
23799 
23800 /*
23801  *    Function: sd_delayed_cv_broadcast
23802  *
23803  * Description: Delayed cv_broadcast to allow for target to recover from media
23804  *		insertion.
23805  *
23806  *   Arguments: arg - driver soft state (unit) structure
23807  */
23808 
23809 static void
23810 sd_delayed_cv_broadcast(void *arg)
23811 {
23812 	struct sd_lun *un = arg;
23813 
23814 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23815 
23816 	mutex_enter(SD_MUTEX(un));
23817 	un->un_dcvb_timeid = NULL;
23818 	cv_broadcast(&un->un_state_cv);
23819 	mutex_exit(SD_MUTEX(un));
23820 }
23821 
23822 
23823 /*
23824  *    Function: sd_media_watch_cb
23825  *
23826  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23827  *		routine processes the TUR sense data and updates the driver
23828  *		state if a transition has occurred. The user thread
23829  *		(sd_check_media) is then signalled.
23830  *
23831  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23832  *			among multiple watches that share this callback function
23833  *		resultp - scsi watch facility result packet containing scsi
23834  *			  packet, status byte and sense data
23835  *
23836  * Return Code: 0 for success, -1 for failure
23837  */
23838 
23839 static int
23840 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23841 {
23842 	struct sd_lun			*un;
23843 	struct scsi_status		*statusp = resultp->statusp;
23844 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
23845 	enum dkio_state			state = DKIO_NONE;
23846 	dev_t				dev = (dev_t)arg;
23847 	uchar_t				actual_sense_length;
23848 	uint8_t				skey, asc, ascq;
23849 
23850 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23851 		return (-1);
23852 	}
23853 	actual_sense_length = resultp->actual_sense_length;
23854 
23855 	mutex_enter(SD_MUTEX(un));
23856 	SD_TRACE(SD_LOG_COMMON, un,
23857 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23858 	    *((char *)statusp), (void *)sensep, actual_sense_length);
23859 
23860 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23861 		un->un_mediastate = DKIO_DEV_GONE;
23862 		cv_broadcast(&un->un_state_cv);
23863 		mutex_exit(SD_MUTEX(un));
23864 
23865 		return (0);
23866 	}
23867 
23868 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23869 		if (sd_gesn_media_data_valid(resultp->mmc_data)) {
23870 			if ((resultp->mmc_data[5] &
23871 			    SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
23872 				state = DKIO_INSERTED;
23873 			} else {
23874 				state = DKIO_EJECTED;
23875 			}
23876 			if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
23877 			    SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
23878 				sd_log_eject_request_event(un, KM_NOSLEEP);
23879 			}
23880 		}
23881 	} else if (sensep != NULL) {
23882 		/*
23883 		 * If there was a check condition then sensep points to valid
23884 		 * sense data. If status was not a check condition but a
23885 		 * reservation or busy status then the new state is DKIO_NONE.
23886 		 */
23887 		skey = scsi_sense_key(sensep);
23888 		asc = scsi_sense_asc(sensep);
23889 		ascq = scsi_sense_ascq(sensep);
23890 
23891 		SD_INFO(SD_LOG_COMMON, un,
23892 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
23893 		    skey, asc, ascq);
23894 		/* This routine only uses up to 13 bytes of sense data. */
23895 		if (actual_sense_length >= 13) {
23896 			if (skey == KEY_UNIT_ATTENTION) {
23897 				if (asc == 0x28) {
23898 					state = DKIO_INSERTED;
23899 				}
23900 			} else if (skey == KEY_NOT_READY) {
23901 				/*
23902 				 * Sense data of 02/06/00 means that the
23903 				 * drive could not read the media (No
23904 				 * reference position found). In this case
23905 				 * to prevent a hang on the DKIOCSTATE IOCTL
23906 				 * we set the media state to DKIO_INSERTED.
23907 				 */
23908 				if (asc == 0x06 && ascq == 0x00)
23909 					state = DKIO_INSERTED;
23910 
23911 				/*
23912 				 * if 02/04/02  means that the host
23913 				 * should send start command. Explicitly
23914 				 * leave the media state as is
23915 				 * (inserted) as the media is inserted
23916 				 * and host has stopped device for PM
23917 				 * reasons. Upon next true read/write
23918 				 * to this media will bring the
23919 				 * device to the right state good for
23920 				 * media access.
23921 				 */
23922 				if (asc == 0x3a) {
23923 					state = DKIO_EJECTED;
23924 				} else {
23925 					/*
23926 					 * If the drive is busy with an
23927 					 * operation or long write, keep the
23928 					 * media in an inserted state.
23929 					 */
23930 
23931 					if ((asc == 0x04) &&
23932 					    ((ascq == 0x02) ||
23933 					    (ascq == 0x07) ||
23934 					    (ascq == 0x08))) {
23935 						state = DKIO_INSERTED;
23936 					}
23937 				}
23938 			} else if (skey == KEY_NO_SENSE) {
23939 				if ((asc == 0x00) && (ascq == 0x00)) {
23940 					/*
23941 					 * Sense Data 00/00/00 does not provide
23942 					 * any information about the state of
23943 					 * the media. Ignore it.
23944 					 */
23945 					mutex_exit(SD_MUTEX(un));
23946 					return (0);
23947 				}
23948 			}
23949 		}
23950 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
23951 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
23952 		state = DKIO_INSERTED;
23953 	}
23954 
23955 	SD_TRACE(SD_LOG_COMMON, un,
23956 	    "sd_media_watch_cb: state=%x, specified=%x\n",
23957 	    state, un->un_specified_mediastate);
23958 
23959 	/*
23960 	 * now signal the waiting thread if this is *not* the specified state;
23961 	 * delay the signal if the state is DKIO_INSERTED to allow the target
23962 	 * to recover
23963 	 */
23964 	if (state != un->un_specified_mediastate) {
23965 		un->un_mediastate = state;
23966 		if (state == DKIO_INSERTED) {
23967 			/*
23968 			 * delay the signal to give the drive a chance
23969 			 * to do what it apparently needs to do
23970 			 */
23971 			SD_TRACE(SD_LOG_COMMON, un,
23972 			    "sd_media_watch_cb: delayed cv_broadcast\n");
23973 			if (un->un_dcvb_timeid == NULL) {
23974 				un->un_dcvb_timeid =
23975 				    timeout(sd_delayed_cv_broadcast, un,
23976 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
23977 			}
23978 		} else {
23979 			SD_TRACE(SD_LOG_COMMON, un,
23980 			    "sd_media_watch_cb: immediate cv_broadcast\n");
23981 			cv_broadcast(&un->un_state_cv);
23982 		}
23983 	}
23984 	mutex_exit(SD_MUTEX(un));
23985 	return (0);
23986 }
23987 
23988 
23989 /*
23990  *    Function: sd_dkio_get_temp
23991  *
23992  * Description: This routine is the driver entry point for handling ioctl
23993  *		requests to get the disk temperature.
23994  *
23995  *   Arguments: dev  - the device number
23996  *		arg  - pointer to user provided dk_temperature structure.
23997  *		flag - this argument is a pass through to ddi_copyxxx()
23998  *		       directly from the mode argument of ioctl().
23999  *
24000  * Return Code: 0
24001  *		EFAULT
24002  *		ENXIO
24003  *		EAGAIN
24004  */
24005 
24006 static int
24007 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24008 {
24009 	struct sd_lun		*un = NULL;
24010 	struct dk_temperature	*dktemp = NULL;
24011 	uchar_t			*temperature_page;
24012 	int			rval = 0;
24013 	int			path_flag = SD_PATH_STANDARD;
24014 	sd_ssc_t		*ssc;
24015 
24016 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24017 		return (ENXIO);
24018 	}
24019 
24020 	ssc = sd_ssc_init(un);
24021 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24022 
24023 	/* copyin the disk temp argument to get the user flags */
24024 	if (ddi_copyin((void *)arg, dktemp,
24025 	    sizeof (struct dk_temperature), flag) != 0) {
24026 		rval = EFAULT;
24027 		goto done;
24028 	}
24029 
24030 	/* Initialize the temperature to invalid. */
24031 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24032 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24033 
24034 	/*
24035 	 * Note: Investigate removing the "bypass pm" semantic.
24036 	 * Can we just bypass PM always?
24037 	 */
24038 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24039 		path_flag = SD_PATH_DIRECT;
24040 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24041 		mutex_enter(&un->un_pm_mutex);
24042 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24043 			/*
24044 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24045 			 * in low power mode, we can not wake it up, Need to
24046 			 * return EAGAIN.
24047 			 */
24048 			mutex_exit(&un->un_pm_mutex);
24049 			rval = EAGAIN;
24050 			goto done;
24051 		} else {
24052 			/*
24053 			 * Indicate to PM the device is busy. This is required
24054 			 * to avoid a race - i.e. the ioctl is issuing a
24055 			 * command and the pm framework brings down the device
24056 			 * to low power mode (possible power cut-off on some
24057 			 * platforms).
24058 			 */
24059 			mutex_exit(&un->un_pm_mutex);
24060 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24061 				rval = EAGAIN;
24062 				goto done;
24063 			}
24064 		}
24065 	}
24066 
24067 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24068 
24069 	rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24070 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24071 	if (rval != 0)
24072 		goto done2;
24073 
24074 	/*
24075 	 * For the current temperature verify that the parameter length is 0x02
24076 	 * and the parameter code is 0x00
24077 	 */
24078 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24079 	    (temperature_page[5] == 0x00)) {
24080 		if (temperature_page[9] == 0xFF) {
24081 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24082 		} else {
24083 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24084 		}
24085 	}
24086 
24087 	/*
24088 	 * For the reference temperature verify that the parameter
24089 	 * length is 0x02 and the parameter code is 0x01
24090 	 */
24091 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24092 	    (temperature_page[11] == 0x01)) {
24093 		if (temperature_page[15] == 0xFF) {
24094 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24095 		} else {
24096 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24097 		}
24098 	}
24099 
24100 	/* Do the copyout regardless of the temperature commands status. */
24101 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24102 	    flag) != 0) {
24103 		rval = EFAULT;
24104 		goto done1;
24105 	}
24106 
24107 done2:
24108 	if (rval != 0) {
24109 		if (rval == EIO)
24110 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24111 		else
24112 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24113 	}
24114 done1:
24115 	if (path_flag == SD_PATH_DIRECT) {
24116 		sd_pm_exit(un);
24117 	}
24118 
24119 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24120 done:
24121 	sd_ssc_fini(ssc);
24122 	if (dktemp != NULL) {
24123 		kmem_free(dktemp, sizeof (struct dk_temperature));
24124 	}
24125 
24126 	return (rval);
24127 }
24128 
24129 
24130 /*
24131  *    Function: sd_log_page_supported
24132  *
24133  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24134  *		supported log pages.
24135  *
24136  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24137  *                      structure for this target.
24138  *		log_page -
24139  *
24140  * Return Code: -1 - on error (log sense is optional and may not be supported).
24141  *		0  - log page not found.
24142  *  		1  - log page found.
24143  */
24144 
24145 static int
24146 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24147 {
24148 	uchar_t *log_page_data;
24149 	int	i;
24150 	int	match = 0;
24151 	int	log_size;
24152 	int	status = 0;
24153 	struct sd_lun	*un;
24154 
24155 	ASSERT(ssc != NULL);
24156 	un = ssc->ssc_un;
24157 	ASSERT(un != NULL);
24158 
24159 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24160 
24161 	status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24162 	    SD_PATH_DIRECT);
24163 
24164 	if (status != 0) {
24165 		if (status == EIO) {
24166 			/*
24167 			 * Some disks do not support log sense, we
24168 			 * should ignore this kind of error(sense key is
24169 			 * 0x5 - illegal request).
24170 			 */
24171 			uint8_t *sensep;
24172 			int senlen;
24173 
24174 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24175 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24176 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
24177 
24178 			if (senlen > 0 &&
24179 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24180 				sd_ssc_assessment(ssc,
24181 				    SD_FMT_IGNORE_COMPROMISE);
24182 			} else {
24183 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24184 			}
24185 		} else {
24186 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24187 		}
24188 
24189 		SD_ERROR(SD_LOG_COMMON, un,
24190 		    "sd_log_page_supported: failed log page retrieval\n");
24191 		kmem_free(log_page_data, 0xFF);
24192 		return (-1);
24193 	}
24194 
24195 	log_size = log_page_data[3];
24196 
24197 	/*
24198 	 * The list of supported log pages start from the fourth byte. Check
24199 	 * until we run out of log pages or a match is found.
24200 	 */
24201 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24202 		if (log_page_data[i] == log_page) {
24203 			match++;
24204 		}
24205 	}
24206 	kmem_free(log_page_data, 0xFF);
24207 	return (match);
24208 }
24209 
24210 
24211 /*
24212  *    Function: sd_mhdioc_failfast
24213  *
24214  * Description: This routine is the driver entry point for handling ioctl
24215  *		requests to enable/disable the multihost failfast option.
24216  *		(MHIOCENFAILFAST)
24217  *
24218  *   Arguments: dev	- the device number
24219  *		arg	- user specified probing interval.
24220  *		flag	- this argument is a pass through to ddi_copyxxx()
24221  *			  directly from the mode argument of ioctl().
24222  *
24223  * Return Code: 0
24224  *		EFAULT
24225  *		ENXIO
24226  */
24227 
24228 static int
24229 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24230 {
24231 	struct sd_lun	*un = NULL;
24232 	int		mh_time;
24233 	int		rval = 0;
24234 
24235 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24236 		return (ENXIO);
24237 	}
24238 
24239 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24240 		return (EFAULT);
24241 
24242 	if (mh_time) {
24243 		mutex_enter(SD_MUTEX(un));
24244 		un->un_resvd_status |= SD_FAILFAST;
24245 		mutex_exit(SD_MUTEX(un));
24246 		/*
24247 		 * If mh_time is INT_MAX, then this ioctl is being used for
24248 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24249 		 */
24250 		if (mh_time != INT_MAX) {
24251 			rval = sd_check_mhd(dev, mh_time);
24252 		}
24253 	} else {
24254 		(void) sd_check_mhd(dev, 0);
24255 		mutex_enter(SD_MUTEX(un));
24256 		un->un_resvd_status &= ~SD_FAILFAST;
24257 		mutex_exit(SD_MUTEX(un));
24258 	}
24259 	return (rval);
24260 }
24261 
24262 
24263 /*
24264  *    Function: sd_mhdioc_takeown
24265  *
24266  * Description: This routine is the driver entry point for handling ioctl
24267  *		requests to forcefully acquire exclusive access rights to the
24268  *		multihost disk (MHIOCTKOWN).
24269  *
24270  *   Arguments: dev	- the device number
24271  *		arg	- user provided structure specifying the delay
24272  *			  parameters in milliseconds
24273  *		flag	- this argument is a pass through to ddi_copyxxx()
24274  *			  directly from the mode argument of ioctl().
24275  *
24276  * Return Code: 0
24277  *		EFAULT
24278  *		ENXIO
24279  */
24280 
24281 static int
24282 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24283 {
24284 	struct sd_lun		*un = NULL;
24285 	struct mhioctkown	*tkown = NULL;
24286 	int			rval = 0;
24287 
24288 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24289 		return (ENXIO);
24290 	}
24291 
24292 	if (arg != NULL) {
24293 		tkown = (struct mhioctkown *)
24294 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24295 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24296 		if (rval != 0) {
24297 			rval = EFAULT;
24298 			goto error;
24299 		}
24300 	}
24301 
24302 	rval = sd_take_ownership(dev, tkown);
24303 	mutex_enter(SD_MUTEX(un));
24304 	if (rval == 0) {
24305 		un->un_resvd_status |= SD_RESERVE;
24306 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24307 			sd_reinstate_resv_delay =
24308 			    tkown->reinstate_resv_delay * 1000;
24309 		} else {
24310 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24311 		}
24312 		/*
24313 		 * Give the scsi_watch routine interval set by
24314 		 * the MHIOCENFAILFAST ioctl precedence here.
24315 		 */
24316 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24317 			mutex_exit(SD_MUTEX(un));
24318 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24319 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24320 			    "sd_mhdioc_takeown : %d\n",
24321 			    sd_reinstate_resv_delay);
24322 		} else {
24323 			mutex_exit(SD_MUTEX(un));
24324 		}
24325 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24326 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24327 	} else {
24328 		un->un_resvd_status &= ~SD_RESERVE;
24329 		mutex_exit(SD_MUTEX(un));
24330 	}
24331 
24332 error:
24333 	if (tkown != NULL) {
24334 		kmem_free(tkown, sizeof (struct mhioctkown));
24335 	}
24336 	return (rval);
24337 }
24338 
24339 
24340 /*
24341  *    Function: sd_mhdioc_release
24342  *
24343  * Description: This routine is the driver entry point for handling ioctl
24344  *		requests to release exclusive access rights to the multihost
24345  *		disk (MHIOCRELEASE).
24346  *
24347  *   Arguments: dev	- the device number
24348  *
24349  * Return Code: 0
24350  *		ENXIO
24351  */
24352 
24353 static int
24354 sd_mhdioc_release(dev_t dev)
24355 {
24356 	struct sd_lun		*un = NULL;
24357 	timeout_id_t		resvd_timeid_save;
24358 	int			resvd_status_save;
24359 	int			rval = 0;
24360 
24361 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24362 		return (ENXIO);
24363 	}
24364 
24365 	mutex_enter(SD_MUTEX(un));
24366 	resvd_status_save = un->un_resvd_status;
24367 	un->un_resvd_status &=
24368 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24369 	if (un->un_resvd_timeid) {
24370 		resvd_timeid_save = un->un_resvd_timeid;
24371 		un->un_resvd_timeid = NULL;
24372 		mutex_exit(SD_MUTEX(un));
24373 		(void) untimeout(resvd_timeid_save);
24374 	} else {
24375 		mutex_exit(SD_MUTEX(un));
24376 	}
24377 
24378 	/*
24379 	 * destroy any pending timeout thread that may be attempting to
24380 	 * reinstate reservation on this device.
24381 	 */
24382 	sd_rmv_resv_reclaim_req(dev);
24383 
24384 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24385 		mutex_enter(SD_MUTEX(un));
24386 		if ((un->un_mhd_token) &&
24387 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24388 			mutex_exit(SD_MUTEX(un));
24389 			(void) sd_check_mhd(dev, 0);
24390 		} else {
24391 			mutex_exit(SD_MUTEX(un));
24392 		}
24393 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24394 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24395 	} else {
24396 		/*
24397 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24398 		 */
24399 		mutex_enter(SD_MUTEX(un));
24400 		un->un_resvd_status = resvd_status_save;
24401 		mutex_exit(SD_MUTEX(un));
24402 	}
24403 	return (rval);
24404 }
24405 
24406 
24407 /*
24408  *    Function: sd_mhdioc_register_devid
24409  *
24410  * Description: This routine is the driver entry point for handling ioctl
24411  *		requests to register the device id (MHIOCREREGISTERDEVID).
24412  *
24413  *		Note: The implementation for this ioctl has been updated to
24414  *		be consistent with the original PSARC case (1999/357)
24415  *		(4375899, 4241671, 4220005)
24416  *
24417  *   Arguments: dev	- the device number
24418  *
24419  * Return Code: 0
24420  *		ENXIO
24421  */
24422 
24423 static int
24424 sd_mhdioc_register_devid(dev_t dev)
24425 {
24426 	struct sd_lun	*un = NULL;
24427 	int		rval = 0;
24428 	sd_ssc_t	*ssc;
24429 
24430 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24431 		return (ENXIO);
24432 	}
24433 
24434 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24435 
24436 	mutex_enter(SD_MUTEX(un));
24437 
24438 	/* If a devid already exists, de-register it */
24439 	if (un->un_devid != NULL) {
24440 		ddi_devid_unregister(SD_DEVINFO(un));
24441 		/*
24442 		 * After unregister devid, needs to free devid memory
24443 		 */
24444 		ddi_devid_free(un->un_devid);
24445 		un->un_devid = NULL;
24446 	}
24447 
24448 	/* Check for reservation conflict */
24449 	mutex_exit(SD_MUTEX(un));
24450 	ssc = sd_ssc_init(un);
24451 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24452 	mutex_enter(SD_MUTEX(un));
24453 
24454 	switch (rval) {
24455 	case 0:
24456 		sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24457 		break;
24458 	case EACCES:
24459 		break;
24460 	default:
24461 		rval = EIO;
24462 	}
24463 
24464 	mutex_exit(SD_MUTEX(un));
24465 	if (rval != 0) {
24466 		if (rval == EIO)
24467 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24468 		else
24469 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24470 	}
24471 	sd_ssc_fini(ssc);
24472 	return (rval);
24473 }
24474 
24475 
24476 /*
24477  *    Function: sd_mhdioc_inkeys
24478  *
24479  * Description: This routine is the driver entry point for handling ioctl
24480  *		requests to issue the SCSI-3 Persistent In Read Keys command
24481  *		to the device (MHIOCGRP_INKEYS).
24482  *
24483  *   Arguments: dev	- the device number
24484  *		arg	- user provided in_keys structure
24485  *		flag	- this argument is a pass through to ddi_copyxxx()
24486  *			  directly from the mode argument of ioctl().
24487  *
24488  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24489  *		ENXIO
24490  *		EFAULT
24491  */
24492 
24493 static int
24494 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24495 {
24496 	struct sd_lun		*un;
24497 	mhioc_inkeys_t		inkeys;
24498 	int			rval = 0;
24499 
24500 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24501 		return (ENXIO);
24502 	}
24503 
24504 #ifdef _MULTI_DATAMODEL
24505 	switch (ddi_model_convert_from(flag & FMODELS)) {
24506 	case DDI_MODEL_ILP32: {
24507 		struct mhioc_inkeys32	inkeys32;
24508 
24509 		if (ddi_copyin(arg, &inkeys32,
24510 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24511 			return (EFAULT);
24512 		}
24513 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24514 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24515 		    &inkeys, flag)) != 0) {
24516 			return (rval);
24517 		}
24518 		inkeys32.generation = inkeys.generation;
24519 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24520 		    flag) != 0) {
24521 			return (EFAULT);
24522 		}
24523 		break;
24524 	}
24525 	case DDI_MODEL_NONE:
24526 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24527 		    flag) != 0) {
24528 			return (EFAULT);
24529 		}
24530 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24531 		    &inkeys, flag)) != 0) {
24532 			return (rval);
24533 		}
24534 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24535 		    flag) != 0) {
24536 			return (EFAULT);
24537 		}
24538 		break;
24539 	}
24540 
24541 #else /* ! _MULTI_DATAMODEL */
24542 
24543 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24544 		return (EFAULT);
24545 	}
24546 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24547 	if (rval != 0) {
24548 		return (rval);
24549 	}
24550 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24551 		return (EFAULT);
24552 	}
24553 
24554 #endif /* _MULTI_DATAMODEL */
24555 
24556 	return (rval);
24557 }
24558 
24559 
24560 /*
24561  *    Function: sd_mhdioc_inresv
24562  *
24563  * Description: This routine is the driver entry point for handling ioctl
24564  *		requests to issue the SCSI-3 Persistent In Read Reservations
24565  *		command to the device (MHIOCGRP_INKEYS).
24566  *
24567  *   Arguments: dev	- the device number
24568  *		arg	- user provided in_resv structure
24569  *		flag	- this argument is a pass through to ddi_copyxxx()
24570  *			  directly from the mode argument of ioctl().
24571  *
24572  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24573  *		ENXIO
24574  *		EFAULT
24575  */
24576 
24577 static int
24578 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24579 {
24580 	struct sd_lun		*un;
24581 	mhioc_inresvs_t		inresvs;
24582 	int			rval = 0;
24583 
24584 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24585 		return (ENXIO);
24586 	}
24587 
24588 #ifdef _MULTI_DATAMODEL
24589 
24590 	switch (ddi_model_convert_from(flag & FMODELS)) {
24591 	case DDI_MODEL_ILP32: {
24592 		struct mhioc_inresvs32	inresvs32;
24593 
24594 		if (ddi_copyin(arg, &inresvs32,
24595 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24596 			return (EFAULT);
24597 		}
24598 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24599 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24600 		    &inresvs, flag)) != 0) {
24601 			return (rval);
24602 		}
24603 		inresvs32.generation = inresvs.generation;
24604 		if (ddi_copyout(&inresvs32, arg,
24605 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24606 			return (EFAULT);
24607 		}
24608 		break;
24609 	}
24610 	case DDI_MODEL_NONE:
24611 		if (ddi_copyin(arg, &inresvs,
24612 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24613 			return (EFAULT);
24614 		}
24615 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24616 		    &inresvs, flag)) != 0) {
24617 			return (rval);
24618 		}
24619 		if (ddi_copyout(&inresvs, arg,
24620 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24621 			return (EFAULT);
24622 		}
24623 		break;
24624 	}
24625 
24626 #else /* ! _MULTI_DATAMODEL */
24627 
24628 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24629 		return (EFAULT);
24630 	}
24631 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24632 	if (rval != 0) {
24633 		return (rval);
24634 	}
24635 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24636 		return (EFAULT);
24637 	}
24638 
24639 #endif /* ! _MULTI_DATAMODEL */
24640 
24641 	return (rval);
24642 }
24643 
24644 
24645 /*
24646  * The following routines support the clustering functionality described below
24647  * and implement lost reservation reclaim functionality.
24648  *
24649  * Clustering
24650  * ----------
24651  * The clustering code uses two different, independent forms of SCSI
24652  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24653  * Persistent Group Reservations. For any particular disk, it will use either
24654  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24655  *
24656  * SCSI-2
24657  * The cluster software takes ownership of a multi-hosted disk by issuing the
24658  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24659  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24660  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24661  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24662  * driver. The meaning of failfast is that if the driver (on this host) ever
24663  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24664  * it should immediately panic the host. The motivation for this ioctl is that
24665  * if this host does encounter reservation conflict, the underlying cause is
24666  * that some other host of the cluster has decided that this host is no longer
24667  * in the cluster and has seized control of the disks for itself. Since this
24668  * host is no longer in the cluster, it ought to panic itself. The
24669  * MHIOCENFAILFAST ioctl does two things:
24670  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24671  *      error to panic the host
24672  *      (b) it sets up a periodic timer to test whether this host still has
24673  *      "access" (in that no other host has reserved the device):  if the
24674  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24675  *      purpose of that periodic timer is to handle scenarios where the host is
24676  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24677  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24678  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24679  * the device itself.
24680  *
24681  * SCSI-3 PGR
24682  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24683  * facility is supported through the shared multihost disk ioctls
24684  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24685  * MHIOCGRP_PREEMPTANDABORT)
24686  *
24687  * Reservation Reclaim:
24688  * --------------------
24689  * To support the lost reservation reclaim operations this driver creates a
24690  * single thread to handle reinstating reservations on all devices that have
24691  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24692  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24693  * and the reservation reclaim thread loops through the requests to regain the
24694  * lost reservations.
24695  */
24696 
24697 /*
24698  *    Function: sd_check_mhd()
24699  *
24700  * Description: This function sets up and submits a scsi watch request or
24701  *		terminates an existing watch request. This routine is used in
24702  *		support of reservation reclaim.
24703  *
24704  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24705  *			 among multiple watches that share the callback function
24706  *		interval - the number of microseconds specifying the watch
24707  *			   interval for issuing TEST UNIT READY commands. If
24708  *			   set to 0 the watch should be terminated. If the
24709  *			   interval is set to 0 and if the device is required
24710  *			   to hold reservation while disabling failfast, the
24711  *			   watch is restarted with an interval of
24712  *			   reinstate_resv_delay.
24713  *
24714  * Return Code: 0	   - Successful submit/terminate of scsi watch request
24715  *		ENXIO      - Indicates an invalid device was specified
24716  *		EAGAIN     - Unable to submit the scsi watch request
24717  */
24718 
24719 static int
24720 sd_check_mhd(dev_t dev, int interval)
24721 {
24722 	struct sd_lun	*un;
24723 	opaque_t	token;
24724 
24725 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24726 		return (ENXIO);
24727 	}
24728 
24729 	/* is this a watch termination request? */
24730 	if (interval == 0) {
24731 		mutex_enter(SD_MUTEX(un));
24732 		/* if there is an existing watch task then terminate it */
24733 		if (un->un_mhd_token) {
24734 			token = un->un_mhd_token;
24735 			un->un_mhd_token = NULL;
24736 			mutex_exit(SD_MUTEX(un));
24737 			(void) scsi_watch_request_terminate(token,
24738 			    SCSI_WATCH_TERMINATE_ALL_WAIT);
24739 			mutex_enter(SD_MUTEX(un));
24740 		} else {
24741 			mutex_exit(SD_MUTEX(un));
24742 			/*
24743 			 * Note: If we return here we don't check for the
24744 			 * failfast case. This is the original legacy
24745 			 * implementation but perhaps we should be checking
24746 			 * the failfast case.
24747 			 */
24748 			return (0);
24749 		}
24750 		/*
24751 		 * If the device is required to hold reservation while
24752 		 * disabling failfast, we need to restart the scsi_watch
24753 		 * routine with an interval of reinstate_resv_delay.
24754 		 */
24755 		if (un->un_resvd_status & SD_RESERVE) {
24756 			interval = sd_reinstate_resv_delay/1000;
24757 		} else {
24758 			/* no failfast so bail */
24759 			mutex_exit(SD_MUTEX(un));
24760 			return (0);
24761 		}
24762 		mutex_exit(SD_MUTEX(un));
24763 	}
24764 
24765 	/*
24766 	 * adjust minimum time interval to 1 second,
24767 	 * and convert from msecs to usecs
24768 	 */
24769 	if (interval > 0 && interval < 1000) {
24770 		interval = 1000;
24771 	}
24772 	interval *= 1000;
24773 
24774 	/*
24775 	 * submit the request to the scsi_watch service
24776 	 */
24777 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24778 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24779 	if (token == NULL) {
24780 		return (EAGAIN);
24781 	}
24782 
24783 	/*
24784 	 * save token for termination later on
24785 	 */
24786 	mutex_enter(SD_MUTEX(un));
24787 	un->un_mhd_token = token;
24788 	mutex_exit(SD_MUTEX(un));
24789 	return (0);
24790 }
24791 
24792 
24793 /*
24794  *    Function: sd_mhd_watch_cb()
24795  *
24796  * Description: This function is the call back function used by the scsi watch
24797  *		facility. The scsi watch facility sends the "Test Unit Ready"
24798  *		and processes the status. If applicable (i.e. a "Unit Attention"
24799  *		status and automatic "Request Sense" not used) the scsi watch
24800  *		facility will send a "Request Sense" and retrieve the sense data
24801  *		to be passed to this callback function. In either case the
24802  *		automatic "Request Sense" or the facility submitting one, this
24803  *		callback is passed the status and sense data.
24804  *
24805  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24806  *			among multiple watches that share this callback function
24807  *		resultp - scsi watch facility result packet containing scsi
24808  *			  packet, status byte and sense data
24809  *
24810  * Return Code: 0 - continue the watch task
24811  *		non-zero - terminate the watch task
24812  */
24813 
24814 static int
24815 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24816 {
24817 	struct sd_lun			*un;
24818 	struct scsi_status		*statusp;
24819 	uint8_t				*sensep;
24820 	struct scsi_pkt			*pkt;
24821 	uchar_t				actual_sense_length;
24822 	dev_t  				dev = (dev_t)arg;
24823 
24824 	ASSERT(resultp != NULL);
24825 	statusp			= resultp->statusp;
24826 	sensep			= (uint8_t *)resultp->sensep;
24827 	pkt			= resultp->pkt;
24828 	actual_sense_length	= resultp->actual_sense_length;
24829 
24830 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24831 		return (ENXIO);
24832 	}
24833 
24834 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
24835 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24836 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24837 
24838 	/* Begin processing of the status and/or sense data */
24839 	if (pkt->pkt_reason != CMD_CMPLT) {
24840 		/* Handle the incomplete packet */
24841 		sd_mhd_watch_incomplete(un, pkt);
24842 		return (0);
24843 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24844 		if (*((unsigned char *)statusp)
24845 		    == STATUS_RESERVATION_CONFLICT) {
24846 			/*
24847 			 * Handle a reservation conflict by panicking if
24848 			 * configured for failfast or by logging the conflict
24849 			 * and updating the reservation status
24850 			 */
24851 			mutex_enter(SD_MUTEX(un));
24852 			if ((un->un_resvd_status & SD_FAILFAST) &&
24853 			    (sd_failfast_enable)) {
24854 				sd_panic_for_res_conflict(un);
24855 				/*NOTREACHED*/
24856 			}
24857 			SD_INFO(SD_LOG_IOCTL_MHD, un,
24858 			    "sd_mhd_watch_cb: Reservation Conflict\n");
24859 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24860 			mutex_exit(SD_MUTEX(un));
24861 		}
24862 	}
24863 
24864 	if (sensep != NULL) {
24865 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24866 			mutex_enter(SD_MUTEX(un));
24867 			if ((scsi_sense_asc(sensep) ==
24868 			    SD_SCSI_RESET_SENSE_CODE) &&
24869 			    (un->un_resvd_status & SD_RESERVE)) {
24870 				/*
24871 				 * The additional sense code indicates a power
24872 				 * on or bus device reset has occurred; update
24873 				 * the reservation status.
24874 				 */
24875 				un->un_resvd_status |=
24876 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
24877 				SD_INFO(SD_LOG_IOCTL_MHD, un,
24878 				    "sd_mhd_watch_cb: Lost Reservation\n");
24879 			}
24880 		} else {
24881 			return (0);
24882 		}
24883 	} else {
24884 		mutex_enter(SD_MUTEX(un));
24885 	}
24886 
24887 	if ((un->un_resvd_status & SD_RESERVE) &&
24888 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
24889 		if (un->un_resvd_status & SD_WANT_RESERVE) {
24890 			/*
24891 			 * A reset occurred in between the last probe and this
24892 			 * one so if a timeout is pending cancel it.
24893 			 */
24894 			if (un->un_resvd_timeid) {
24895 				timeout_id_t temp_id = un->un_resvd_timeid;
24896 				un->un_resvd_timeid = NULL;
24897 				mutex_exit(SD_MUTEX(un));
24898 				(void) untimeout(temp_id);
24899 				mutex_enter(SD_MUTEX(un));
24900 			}
24901 			un->un_resvd_status &= ~SD_WANT_RESERVE;
24902 		}
24903 		if (un->un_resvd_timeid == 0) {
24904 			/* Schedule a timeout to handle the lost reservation */
24905 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
24906 			    (void *)dev,
24907 			    drv_usectohz(sd_reinstate_resv_delay));
24908 		}
24909 	}
24910 	mutex_exit(SD_MUTEX(un));
24911 	return (0);
24912 }
24913 
24914 
24915 /*
24916  *    Function: sd_mhd_watch_incomplete()
24917  *
24918  * Description: This function is used to find out why a scsi pkt sent by the
24919  *		scsi watch facility was not completed. Under some scenarios this
24920  *		routine will return. Otherwise it will send a bus reset to see
24921  *		if the drive is still online.
24922  *
24923  *   Arguments: un  - driver soft state (unit) structure
24924  *		pkt - incomplete scsi pkt
24925  */
24926 
24927 static void
24928 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
24929 {
24930 	int	be_chatty;
24931 	int	perr;
24932 
24933 	ASSERT(pkt != NULL);
24934 	ASSERT(un != NULL);
24935 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
24936 	perr		= (pkt->pkt_statistics & STAT_PERR);
24937 
24938 	mutex_enter(SD_MUTEX(un));
24939 	if (un->un_state == SD_STATE_DUMPING) {
24940 		mutex_exit(SD_MUTEX(un));
24941 		return;
24942 	}
24943 
24944 	switch (pkt->pkt_reason) {
24945 	case CMD_UNX_BUS_FREE:
24946 		/*
24947 		 * If we had a parity error that caused the target to drop BSY*,
24948 		 * don't be chatty about it.
24949 		 */
24950 		if (perr && be_chatty) {
24951 			be_chatty = 0;
24952 		}
24953 		break;
24954 	case CMD_TAG_REJECT:
24955 		/*
24956 		 * The SCSI-2 spec states that a tag reject will be sent by the
24957 		 * target if tagged queuing is not supported. A tag reject may
24958 		 * also be sent during certain initialization periods or to
24959 		 * control internal resources. For the latter case the target
24960 		 * may also return Queue Full.
24961 		 *
24962 		 * If this driver receives a tag reject from a target that is
24963 		 * going through an init period or controlling internal
24964 		 * resources tagged queuing will be disabled. This is a less
24965 		 * than optimal behavior but the driver is unable to determine
24966 		 * the target state and assumes tagged queueing is not supported
24967 		 */
24968 		pkt->pkt_flags = 0;
24969 		un->un_tagflags = 0;
24970 
24971 		if (un->un_f_opt_queueing == TRUE) {
24972 			un->un_throttle = min(un->un_throttle, 3);
24973 		} else {
24974 			un->un_throttle = 1;
24975 		}
24976 		mutex_exit(SD_MUTEX(un));
24977 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
24978 		mutex_enter(SD_MUTEX(un));
24979 		break;
24980 	case CMD_INCOMPLETE:
24981 		/*
24982 		 * The transport stopped with an abnormal state, fallthrough and
24983 		 * reset the target and/or bus unless selection did not complete
24984 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
24985 		 * go through a target/bus reset
24986 		 */
24987 		if (pkt->pkt_state == STATE_GOT_BUS) {
24988 			break;
24989 		}
24990 		/*FALLTHROUGH*/
24991 
24992 	case CMD_TIMEOUT:
24993 	default:
24994 		/*
24995 		 * The lun may still be running the command, so a lun reset
24996 		 * should be attempted. If the lun reset fails or cannot be
24997 		 * issued, than try a target reset. Lastly try a bus reset.
24998 		 */
24999 		if ((pkt->pkt_statistics &
25000 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25001 			int reset_retval = 0;
25002 			mutex_exit(SD_MUTEX(un));
25003 			if (un->un_f_allow_bus_device_reset == TRUE) {
25004 				if (un->un_f_lun_reset_enabled == TRUE) {
25005 					reset_retval =
25006 					    scsi_reset(SD_ADDRESS(un),
25007 					    RESET_LUN);
25008 				}
25009 				if (reset_retval == 0) {
25010 					reset_retval =
25011 					    scsi_reset(SD_ADDRESS(un),
25012 					    RESET_TARGET);
25013 				}
25014 			}
25015 			if (reset_retval == 0) {
25016 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25017 			}
25018 			mutex_enter(SD_MUTEX(un));
25019 		}
25020 		break;
25021 	}
25022 
25023 	/* A device/bus reset has occurred; update the reservation status. */
25024 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25025 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25026 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25027 			un->un_resvd_status |=
25028 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25029 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25030 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25031 		}
25032 	}
25033 
25034 	/*
25035 	 * The disk has been turned off; Update the device state.
25036 	 *
25037 	 * Note: Should we be offlining the disk here?
25038 	 */
25039 	if (pkt->pkt_state == STATE_GOT_BUS) {
25040 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25041 		    "Disk not responding to selection\n");
25042 		if (un->un_state != SD_STATE_OFFLINE) {
25043 			New_state(un, SD_STATE_OFFLINE);
25044 		}
25045 	} else if (be_chatty) {
25046 		/*
25047 		 * suppress messages if they are all the same pkt reason;
25048 		 * with TQ, many (up to 256) are returned with the same
25049 		 * pkt_reason
25050 		 */
25051 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25052 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25053 			    "sd_mhd_watch_incomplete: "
25054 			    "SCSI transport failed: reason '%s'\n",
25055 			    scsi_rname(pkt->pkt_reason));
25056 		}
25057 	}
25058 	un->un_last_pkt_reason = pkt->pkt_reason;
25059 	mutex_exit(SD_MUTEX(un));
25060 }
25061 
25062 
25063 /*
25064  *    Function: sd_sname()
25065  *
25066  * Description: This is a simple little routine to return a string containing
25067  *		a printable description of command status byte for use in
25068  *		logging.
25069  *
25070  *   Arguments: status - pointer to a status byte
25071  *
25072  * Return Code: char * - string containing status description.
25073  */
25074 
25075 static char *
25076 sd_sname(uchar_t status)
25077 {
25078 	switch (status & STATUS_MASK) {
25079 	case STATUS_GOOD:
25080 		return ("good status");
25081 	case STATUS_CHECK:
25082 		return ("check condition");
25083 	case STATUS_MET:
25084 		return ("condition met");
25085 	case STATUS_BUSY:
25086 		return ("busy");
25087 	case STATUS_INTERMEDIATE:
25088 		return ("intermediate");
25089 	case STATUS_INTERMEDIATE_MET:
25090 		return ("intermediate - condition met");
25091 	case STATUS_RESERVATION_CONFLICT:
25092 		return ("reservation_conflict");
25093 	case STATUS_TERMINATED:
25094 		return ("command terminated");
25095 	case STATUS_QFULL:
25096 		return ("queue full");
25097 	default:
25098 		return ("<unknown status>");
25099 	}
25100 }
25101 
25102 
25103 /*
25104  *    Function: sd_mhd_resvd_recover()
25105  *
25106  * Description: This function adds a reservation entry to the
25107  *		sd_resv_reclaim_request list and signals the reservation
25108  *		reclaim thread that there is work pending. If the reservation
25109  *		reclaim thread has not been previously created this function
25110  *		will kick it off.
25111  *
25112  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25113  *			among multiple watches that share this callback function
25114  *
25115  *     Context: This routine is called by timeout() and is run in interrupt
25116  *		context. It must not sleep or call other functions which may
25117  *		sleep.
25118  */
25119 
25120 static void
25121 sd_mhd_resvd_recover(void *arg)
25122 {
25123 	dev_t			dev = (dev_t)arg;
25124 	struct sd_lun		*un;
25125 	struct sd_thr_request	*sd_treq = NULL;
25126 	struct sd_thr_request	*sd_cur = NULL;
25127 	struct sd_thr_request	*sd_prev = NULL;
25128 	int			already_there = 0;
25129 
25130 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25131 		return;
25132 	}
25133 
25134 	mutex_enter(SD_MUTEX(un));
25135 	un->un_resvd_timeid = NULL;
25136 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25137 		/*
25138 		 * There was a reset so don't issue the reserve, allow the
25139 		 * sd_mhd_watch_cb callback function to notice this and
25140 		 * reschedule the timeout for reservation.
25141 		 */
25142 		mutex_exit(SD_MUTEX(un));
25143 		return;
25144 	}
25145 	mutex_exit(SD_MUTEX(un));
25146 
25147 	/*
25148 	 * Add this device to the sd_resv_reclaim_request list and the
25149 	 * sd_resv_reclaim_thread should take care of the rest.
25150 	 *
25151 	 * Note: We can't sleep in this context so if the memory allocation
25152 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25153 	 * reschedule the timeout for reservation.  (4378460)
25154 	 */
25155 	sd_treq = (struct sd_thr_request *)
25156 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25157 	if (sd_treq == NULL) {
25158 		return;
25159 	}
25160 
25161 	sd_treq->sd_thr_req_next = NULL;
25162 	sd_treq->dev = dev;
25163 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25164 	if (sd_tr.srq_thr_req_head == NULL) {
25165 		sd_tr.srq_thr_req_head = sd_treq;
25166 	} else {
25167 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25168 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25169 			if (sd_cur->dev == dev) {
25170 				/*
25171 				 * already in Queue so don't log
25172 				 * another request for the device
25173 				 */
25174 				already_there = 1;
25175 				break;
25176 			}
25177 			sd_prev = sd_cur;
25178 		}
25179 		if (!already_there) {
25180 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25181 			    "logging request for %lx\n", dev);
25182 			sd_prev->sd_thr_req_next = sd_treq;
25183 		} else {
25184 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25185 		}
25186 	}
25187 
25188 	/*
25189 	 * Create a kernel thread to do the reservation reclaim and free up this
25190 	 * thread. We cannot block this thread while we go away to do the
25191 	 * reservation reclaim
25192 	 */
25193 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25194 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25195 		    sd_resv_reclaim_thread, NULL,
25196 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25197 
25198 	/* Tell the reservation reclaim thread that it has work to do */
25199 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25200 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25201 }
25202 
25203 /*
25204  *    Function: sd_resv_reclaim_thread()
25205  *
25206  * Description: This function implements the reservation reclaim operations
25207  *
25208  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25209  *		      among multiple watches that share this callback function
25210  */
25211 
25212 static void
25213 sd_resv_reclaim_thread()
25214 {
25215 	struct sd_lun		*un;
25216 	struct sd_thr_request	*sd_mhreq;
25217 
25218 	/* Wait for work */
25219 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25220 	if (sd_tr.srq_thr_req_head == NULL) {
25221 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25222 		    &sd_tr.srq_resv_reclaim_mutex);
25223 	}
25224 
25225 	/* Loop while we have work */
25226 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25227 		un = ddi_get_soft_state(sd_state,
25228 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25229 		if (un == NULL) {
25230 			/*
25231 			 * softstate structure is NULL so just
25232 			 * dequeue the request and continue
25233 			 */
25234 			sd_tr.srq_thr_req_head =
25235 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25236 			kmem_free(sd_tr.srq_thr_cur_req,
25237 			    sizeof (struct sd_thr_request));
25238 			continue;
25239 		}
25240 
25241 		/* dequeue the request */
25242 		sd_mhreq = sd_tr.srq_thr_cur_req;
25243 		sd_tr.srq_thr_req_head =
25244 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25245 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25246 
25247 		/*
25248 		 * Reclaim reservation only if SD_RESERVE is still set. There
25249 		 * may have been a call to MHIOCRELEASE before we got here.
25250 		 */
25251 		mutex_enter(SD_MUTEX(un));
25252 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25253 			/*
25254 			 * Note: The SD_LOST_RESERVE flag is cleared before
25255 			 * reclaiming the reservation. If this is done after the
25256 			 * call to sd_reserve_release a reservation loss in the
25257 			 * window between pkt completion of reserve cmd and
25258 			 * mutex_enter below may not be recognized
25259 			 */
25260 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25261 			mutex_exit(SD_MUTEX(un));
25262 
25263 			if (sd_reserve_release(sd_mhreq->dev,
25264 			    SD_RESERVE) == 0) {
25265 				mutex_enter(SD_MUTEX(un));
25266 				un->un_resvd_status |= SD_RESERVE;
25267 				mutex_exit(SD_MUTEX(un));
25268 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25269 				    "sd_resv_reclaim_thread: "
25270 				    "Reservation Recovered\n");
25271 			} else {
25272 				mutex_enter(SD_MUTEX(un));
25273 				un->un_resvd_status |= SD_LOST_RESERVE;
25274 				mutex_exit(SD_MUTEX(un));
25275 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25276 				    "sd_resv_reclaim_thread: Failed "
25277 				    "Reservation Recovery\n");
25278 			}
25279 		} else {
25280 			mutex_exit(SD_MUTEX(un));
25281 		}
25282 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25283 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25284 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25285 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25286 		/*
25287 		 * wakeup the destroy thread if anyone is waiting on
25288 		 * us to complete.
25289 		 */
25290 		cv_signal(&sd_tr.srq_inprocess_cv);
25291 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25292 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25293 	}
25294 
25295 	/*
25296 	 * cleanup the sd_tr structure now that this thread will not exist
25297 	 */
25298 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25299 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25300 	sd_tr.srq_resv_reclaim_thread = NULL;
25301 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25302 	thread_exit();
25303 }
25304 
25305 
25306 /*
25307  *    Function: sd_rmv_resv_reclaim_req()
25308  *
25309  * Description: This function removes any pending reservation reclaim requests
25310  *		for the specified device.
25311  *
25312  *   Arguments: dev - the device 'dev_t'
25313  */
25314 
25315 static void
25316 sd_rmv_resv_reclaim_req(dev_t dev)
25317 {
25318 	struct sd_thr_request *sd_mhreq;
25319 	struct sd_thr_request *sd_prev;
25320 
25321 	/* Remove a reservation reclaim request from the list */
25322 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25323 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25324 		/*
25325 		 * We are attempting to reinstate reservation for
25326 		 * this device. We wait for sd_reserve_release()
25327 		 * to return before we return.
25328 		 */
25329 		cv_wait(&sd_tr.srq_inprocess_cv,
25330 		    &sd_tr.srq_resv_reclaim_mutex);
25331 	} else {
25332 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25333 		if (sd_mhreq && sd_mhreq->dev == dev) {
25334 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25335 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25336 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25337 			return;
25338 		}
25339 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25340 			if (sd_mhreq && sd_mhreq->dev == dev) {
25341 				break;
25342 			}
25343 			sd_prev = sd_mhreq;
25344 		}
25345 		if (sd_mhreq != NULL) {
25346 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25347 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25348 		}
25349 	}
25350 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25351 }
25352 
25353 
25354 /*
25355  *    Function: sd_mhd_reset_notify_cb()
25356  *
25357  * Description: This is a call back function for scsi_reset_notify. This
25358  *		function updates the softstate reserved status and logs the
25359  *		reset. The driver scsi watch facility callback function
25360  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25361  *		will reclaim the reservation.
25362  *
25363  *   Arguments: arg  - driver soft state (unit) structure
25364  */
25365 
25366 static void
25367 sd_mhd_reset_notify_cb(caddr_t arg)
25368 {
25369 	struct sd_lun *un = (struct sd_lun *)arg;
25370 
25371 	mutex_enter(SD_MUTEX(un));
25372 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25373 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25374 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25375 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25376 	}
25377 	mutex_exit(SD_MUTEX(un));
25378 }
25379 
25380 
25381 /*
25382  *    Function: sd_take_ownership()
25383  *
25384  * Description: This routine implements an algorithm to achieve a stable
25385  *		reservation on disks which don't implement priority reserve,
25386  *		and makes sure that other host lose re-reservation attempts.
25387  *		This algorithm contains of a loop that keeps issuing the RESERVE
25388  *		for some period of time (min_ownership_delay, default 6 seconds)
25389  *		During that loop, it looks to see if there has been a bus device
25390  *		reset or bus reset (both of which cause an existing reservation
25391  *		to be lost). If the reservation is lost issue RESERVE until a
25392  *		period of min_ownership_delay with no resets has gone by, or
25393  *		until max_ownership_delay has expired. This loop ensures that
25394  *		the host really did manage to reserve the device, in spite of
25395  *		resets. The looping for min_ownership_delay (default six
25396  *		seconds) is important to early generation clustering products,
25397  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25398  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25399  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25400  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25401  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25402  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25403  *		no longer "owns" the disk and will have panicked itself.  Thus,
25404  *		the host issuing the MHIOCTKOWN is assured (with timing
25405  *		dependencies) that by the time it actually starts to use the
25406  *		disk for real work, the old owner is no longer accessing it.
25407  *
25408  *		min_ownership_delay is the minimum amount of time for which the
25409  *		disk must be reserved continuously devoid of resets before the
25410  *		MHIOCTKOWN ioctl will return success.
25411  *
25412  *		max_ownership_delay indicates the amount of time by which the
25413  *		take ownership should succeed or timeout with an error.
25414  *
25415  *   Arguments: dev - the device 'dev_t'
25416  *		*p  - struct containing timing info.
25417  *
25418  * Return Code: 0 for success or error code
25419  */
25420 
25421 static int
25422 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25423 {
25424 	struct sd_lun	*un;
25425 	int		rval;
25426 	int		err;
25427 	int		reservation_count   = 0;
25428 	int		min_ownership_delay =  6000000; /* in usec */
25429 	int		max_ownership_delay = 30000000; /* in usec */
25430 	clock_t		start_time;	/* starting time of this algorithm */
25431 	clock_t		end_time;	/* time limit for giving up */
25432 	clock_t		ownership_time;	/* time limit for stable ownership */
25433 	clock_t		current_time;
25434 	clock_t		previous_current_time;
25435 
25436 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25437 		return (ENXIO);
25438 	}
25439 
25440 	/*
25441 	 * Attempt a device reservation. A priority reservation is requested.
25442 	 */
25443 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25444 	    != SD_SUCCESS) {
25445 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25446 		    "sd_take_ownership: return(1)=%d\n", rval);
25447 		return (rval);
25448 	}
25449 
25450 	/* Update the softstate reserved status to indicate the reservation */
25451 	mutex_enter(SD_MUTEX(un));
25452 	un->un_resvd_status |= SD_RESERVE;
25453 	un->un_resvd_status &=
25454 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25455 	mutex_exit(SD_MUTEX(un));
25456 
25457 	if (p != NULL) {
25458 		if (p->min_ownership_delay != 0) {
25459 			min_ownership_delay = p->min_ownership_delay * 1000;
25460 		}
25461 		if (p->max_ownership_delay != 0) {
25462 			max_ownership_delay = p->max_ownership_delay * 1000;
25463 		}
25464 	}
25465 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25466 	    "sd_take_ownership: min, max delays: %d, %d\n",
25467 	    min_ownership_delay, max_ownership_delay);
25468 
25469 	start_time = ddi_get_lbolt();
25470 	current_time	= start_time;
25471 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25472 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25473 
25474 	while (current_time - end_time < 0) {
25475 		delay(drv_usectohz(500000));
25476 
25477 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25478 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25479 				mutex_enter(SD_MUTEX(un));
25480 				rval = (un->un_resvd_status &
25481 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25482 				mutex_exit(SD_MUTEX(un));
25483 				break;
25484 			}
25485 		}
25486 		previous_current_time = current_time;
25487 		current_time = ddi_get_lbolt();
25488 		mutex_enter(SD_MUTEX(un));
25489 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25490 			ownership_time = ddi_get_lbolt() +
25491 			    drv_usectohz(min_ownership_delay);
25492 			reservation_count = 0;
25493 		} else {
25494 			reservation_count++;
25495 		}
25496 		un->un_resvd_status |= SD_RESERVE;
25497 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25498 		mutex_exit(SD_MUTEX(un));
25499 
25500 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25501 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25502 		    "reservation=%s\n", (current_time - previous_current_time),
25503 		    reservation_count ? "ok" : "reclaimed");
25504 
25505 		if (current_time - ownership_time >= 0 &&
25506 		    reservation_count >= 4) {
25507 			rval = 0; /* Achieved a stable ownership */
25508 			break;
25509 		}
25510 		if (current_time - end_time >= 0) {
25511 			rval = EACCES; /* No ownership in max possible time */
25512 			break;
25513 		}
25514 	}
25515 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25516 	    "sd_take_ownership: return(2)=%d\n", rval);
25517 	return (rval);
25518 }
25519 
25520 
25521 /*
25522  *    Function: sd_reserve_release()
25523  *
25524  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25525  *		PRIORITY RESERVE commands based on a user specified command type
25526  *
25527  *   Arguments: dev - the device 'dev_t'
25528  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25529  *		      SD_RESERVE, SD_RELEASE
25530  *
25531  * Return Code: 0 or Error Code
25532  */
25533 
25534 static int
25535 sd_reserve_release(dev_t dev, int cmd)
25536 {
25537 	struct uscsi_cmd	*com = NULL;
25538 	struct sd_lun		*un = NULL;
25539 	char			cdb[CDB_GROUP0];
25540 	int			rval;
25541 
25542 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25543 	    (cmd == SD_PRIORITY_RESERVE));
25544 
25545 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25546 		return (ENXIO);
25547 	}
25548 
25549 	/* instantiate and initialize the command and cdb */
25550 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25551 	bzero(cdb, CDB_GROUP0);
25552 	com->uscsi_flags   = USCSI_SILENT;
25553 	com->uscsi_timeout = un->un_reserve_release_time;
25554 	com->uscsi_cdblen  = CDB_GROUP0;
25555 	com->uscsi_cdb	   = cdb;
25556 	if (cmd == SD_RELEASE) {
25557 		cdb[0] = SCMD_RELEASE;
25558 	} else {
25559 		cdb[0] = SCMD_RESERVE;
25560 	}
25561 
25562 	/* Send the command. */
25563 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25564 	    SD_PATH_STANDARD);
25565 
25566 	/*
25567 	 * "break" a reservation that is held by another host, by issuing a
25568 	 * reset if priority reserve is desired, and we could not get the
25569 	 * device.
25570 	 */
25571 	if ((cmd == SD_PRIORITY_RESERVE) &&
25572 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25573 		/*
25574 		 * First try to reset the LUN. If we cannot, then try a target
25575 		 * reset, followed by a bus reset if the target reset fails.
25576 		 */
25577 		int reset_retval = 0;
25578 		if (un->un_f_lun_reset_enabled == TRUE) {
25579 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25580 		}
25581 		if (reset_retval == 0) {
25582 			/* The LUN reset either failed or was not issued */
25583 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25584 		}
25585 		if ((reset_retval == 0) &&
25586 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25587 			rval = EIO;
25588 			kmem_free(com, sizeof (*com));
25589 			return (rval);
25590 		}
25591 
25592 		bzero(com, sizeof (struct uscsi_cmd));
25593 		com->uscsi_flags   = USCSI_SILENT;
25594 		com->uscsi_cdb	   = cdb;
25595 		com->uscsi_cdblen  = CDB_GROUP0;
25596 		com->uscsi_timeout = 5;
25597 
25598 		/*
25599 		 * Reissue the last reserve command, this time without request
25600 		 * sense.  Assume that it is just a regular reserve command.
25601 		 */
25602 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25603 		    SD_PATH_STANDARD);
25604 	}
25605 
25606 	/* Return an error if still getting a reservation conflict. */
25607 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25608 		rval = EACCES;
25609 	}
25610 
25611 	kmem_free(com, sizeof (*com));
25612 	return (rval);
25613 }
25614 
25615 
25616 #define	SD_NDUMP_RETRIES	12
25617 /*
25618  *	System Crash Dump routine
25619  */
25620 
25621 static int
25622 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25623 {
25624 	int		instance;
25625 	int		partition;
25626 	int		i;
25627 	int		err;
25628 	struct sd_lun	*un;
25629 	struct scsi_pkt *wr_pktp;
25630 	struct buf	*wr_bp;
25631 	struct buf	wr_buf;
25632 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25633 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25634 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25635 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25636 	size_t		io_start_offset;
25637 	int		doing_rmw = FALSE;
25638 	int		rval;
25639 	ssize_t		dma_resid;
25640 	daddr_t		oblkno;
25641 	diskaddr_t	nblks = 0;
25642 	diskaddr_t	start_block;
25643 
25644 	instance = SDUNIT(dev);
25645 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25646 	    !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25647 		return (ENXIO);
25648 	}
25649 
25650 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25651 
25652 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25653 
25654 	partition = SDPART(dev);
25655 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25656 
25657 	if (!(NOT_DEVBSIZE(un))) {
25658 		int secmask = 0;
25659 		int blknomask = 0;
25660 
25661 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25662 		secmask = un->un_tgt_blocksize - 1;
25663 
25664 		if (blkno & blknomask) {
25665 			SD_TRACE(SD_LOG_DUMP, un,
25666 			    "sddump: dump start block not modulo %d\n",
25667 			    un->un_tgt_blocksize);
25668 			return (EINVAL);
25669 		}
25670 
25671 		if ((nblk * DEV_BSIZE) & secmask) {
25672 			SD_TRACE(SD_LOG_DUMP, un,
25673 			    "sddump: dump length not modulo %d\n",
25674 			    un->un_tgt_blocksize);
25675 			return (EINVAL);
25676 		}
25677 
25678 	}
25679 
25680 	/* Validate blocks to dump at against partition size. */
25681 
25682 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
25683 	    &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25684 
25685 	if (NOT_DEVBSIZE(un)) {
25686 		if ((blkno + nblk) > nblks) {
25687 			SD_TRACE(SD_LOG_DUMP, un,
25688 			    "sddump: dump range larger than partition: "
25689 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25690 			    blkno, nblk, nblks);
25691 			return (EINVAL);
25692 		}
25693 	} else {
25694 		if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25695 		    (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25696 			SD_TRACE(SD_LOG_DUMP, un,
25697 			    "sddump: dump range larger than partition: "
25698 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25699 			    blkno, nblk, nblks);
25700 			return (EINVAL);
25701 		}
25702 	}
25703 
25704 	mutex_enter(&un->un_pm_mutex);
25705 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25706 		struct scsi_pkt *start_pktp;
25707 
25708 		mutex_exit(&un->un_pm_mutex);
25709 
25710 		/*
25711 		 * use pm framework to power on HBA 1st
25712 		 */
25713 		(void) pm_raise_power(SD_DEVINFO(un), 0,
25714 		    SD_PM_STATE_ACTIVE(un));
25715 
25716 		/*
25717 		 * Dump no long uses sdpower to power on a device, it's
25718 		 * in-line here so it can be done in polled mode.
25719 		 */
25720 
25721 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25722 
25723 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25724 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25725 
25726 		if (start_pktp == NULL) {
25727 			/* We were not given a SCSI packet, fail. */
25728 			return (EIO);
25729 		}
25730 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25731 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25732 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25733 		start_pktp->pkt_flags = FLAG_NOINTR;
25734 
25735 		mutex_enter(SD_MUTEX(un));
25736 		SD_FILL_SCSI1_LUN(un, start_pktp);
25737 		mutex_exit(SD_MUTEX(un));
25738 		/*
25739 		 * Scsi_poll returns 0 (success) if the command completes and
25740 		 * the status block is STATUS_GOOD.
25741 		 */
25742 		if (sd_scsi_poll(un, start_pktp) != 0) {
25743 			scsi_destroy_pkt(start_pktp);
25744 			return (EIO);
25745 		}
25746 		scsi_destroy_pkt(start_pktp);
25747 		(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25748 		    SD_PM_STATE_CHANGE);
25749 	} else {
25750 		mutex_exit(&un->un_pm_mutex);
25751 	}
25752 
25753 	mutex_enter(SD_MUTEX(un));
25754 	un->un_throttle = 0;
25755 
25756 	/*
25757 	 * The first time through, reset the specific target device.
25758 	 * However, when cpr calls sddump we know that sd is in a
25759 	 * a good state so no bus reset is required.
25760 	 * Clear sense data via Request Sense cmd.
25761 	 * In sddump we don't care about allow_bus_device_reset anymore
25762 	 */
25763 
25764 	if ((un->un_state != SD_STATE_SUSPENDED) &&
25765 	    (un->un_state != SD_STATE_DUMPING)) {
25766 
25767 		New_state(un, SD_STATE_DUMPING);
25768 
25769 		if (un->un_f_is_fibre == FALSE) {
25770 			mutex_exit(SD_MUTEX(un));
25771 			/*
25772 			 * Attempt a bus reset for parallel scsi.
25773 			 *
25774 			 * Note: A bus reset is required because on some host
25775 			 * systems (i.e. E420R) a bus device reset is
25776 			 * insufficient to reset the state of the target.
25777 			 *
25778 			 * Note: Don't issue the reset for fibre-channel,
25779 			 * because this tends to hang the bus (loop) for
25780 			 * too long while everyone is logging out and in
25781 			 * and the deadman timer for dumping will fire
25782 			 * before the dump is complete.
25783 			 */
25784 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25785 				mutex_enter(SD_MUTEX(un));
25786 				Restore_state(un);
25787 				mutex_exit(SD_MUTEX(un));
25788 				return (EIO);
25789 			}
25790 
25791 			/* Delay to give the device some recovery time. */
25792 			drv_usecwait(10000);
25793 
25794 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
25795 				SD_INFO(SD_LOG_DUMP, un,
25796 				    "sddump: sd_send_polled_RQS failed\n");
25797 			}
25798 			mutex_enter(SD_MUTEX(un));
25799 		}
25800 	}
25801 
25802 	/*
25803 	 * Convert the partition-relative block number to a
25804 	 * disk physical block number.
25805 	 */
25806 	if (NOT_DEVBSIZE(un)) {
25807 		blkno += start_block;
25808 	} else {
25809 		blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25810 		blkno += start_block;
25811 	}
25812 
25813 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25814 
25815 
25816 	/*
25817 	 * Check if the device has a non-512 block size.
25818 	 */
25819 	wr_bp = NULL;
25820 	if (NOT_DEVBSIZE(un)) {
25821 		tgt_byte_offset = blkno * un->un_sys_blocksize;
25822 		tgt_byte_count = nblk * un->un_sys_blocksize;
25823 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25824 		    (tgt_byte_count % un->un_tgt_blocksize)) {
25825 			doing_rmw = TRUE;
25826 			/*
25827 			 * Calculate the block number and number of block
25828 			 * in terms of the media block size.
25829 			 */
25830 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25831 			tgt_nblk =
25832 			    ((tgt_byte_offset + tgt_byte_count +
25833 			    (un->un_tgt_blocksize - 1)) /
25834 			    un->un_tgt_blocksize) - tgt_blkno;
25835 
25836 			/*
25837 			 * Invoke the routine which is going to do read part
25838 			 * of read-modify-write.
25839 			 * Note that this routine returns a pointer to
25840 			 * a valid bp in wr_bp.
25841 			 */
25842 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25843 			    &wr_bp);
25844 			if (err) {
25845 				mutex_exit(SD_MUTEX(un));
25846 				return (err);
25847 			}
25848 			/*
25849 			 * Offset is being calculated as -
25850 			 * (original block # * system block size) -
25851 			 * (new block # * target block size)
25852 			 */
25853 			io_start_offset =
25854 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
25855 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25856 
25857 			ASSERT((io_start_offset >= 0) &&
25858 			    (io_start_offset < un->un_tgt_blocksize));
25859 			/*
25860 			 * Do the modify portion of read modify write.
25861 			 */
25862 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25863 			    (size_t)nblk * un->un_sys_blocksize);
25864 		} else {
25865 			doing_rmw = FALSE;
25866 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25867 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25868 		}
25869 
25870 		/* Convert blkno and nblk to target blocks */
25871 		blkno = tgt_blkno;
25872 		nblk = tgt_nblk;
25873 	} else {
25874 		wr_bp = &wr_buf;
25875 		bzero(wr_bp, sizeof (struct buf));
25876 		wr_bp->b_flags		= B_BUSY;
25877 		wr_bp->b_un.b_addr	= addr;
25878 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
25879 		wr_bp->b_resid		= 0;
25880 	}
25881 
25882 	mutex_exit(SD_MUTEX(un));
25883 
25884 	/*
25885 	 * Obtain a SCSI packet for the write command.
25886 	 * It should be safe to call the allocator here without
25887 	 * worrying about being locked for DVMA mapping because
25888 	 * the address we're passed is already a DVMA mapping
25889 	 *
25890 	 * We are also not going to worry about semaphore ownership
25891 	 * in the dump buffer. Dumping is single threaded at present.
25892 	 */
25893 
25894 	wr_pktp = NULL;
25895 
25896 	dma_resid = wr_bp->b_bcount;
25897 	oblkno = blkno;
25898 
25899 	if (!(NOT_DEVBSIZE(un))) {
25900 		nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
25901 	}
25902 
25903 	while (dma_resid != 0) {
25904 
25905 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25906 		wr_bp->b_flags &= ~B_ERROR;
25907 
25908 		if (un->un_partial_dma_supported == 1) {
25909 			blkno = oblkno +
25910 			    ((wr_bp->b_bcount - dma_resid) /
25911 			    un->un_tgt_blocksize);
25912 			nblk = dma_resid / un->un_tgt_blocksize;
25913 
25914 			if (wr_pktp) {
25915 				/*
25916 				 * Partial DMA transfers after initial transfer
25917 				 */
25918 				rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
25919 				    blkno, nblk);
25920 			} else {
25921 				/* Initial transfer */
25922 				rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25923 				    un->un_pkt_flags, NULL_FUNC, NULL,
25924 				    blkno, nblk);
25925 			}
25926 		} else {
25927 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25928 			    0, NULL_FUNC, NULL, blkno, nblk);
25929 		}
25930 
25931 		if (rval == 0) {
25932 			/* We were given a SCSI packet, continue. */
25933 			break;
25934 		}
25935 
25936 		if (i == 0) {
25937 			if (wr_bp->b_flags & B_ERROR) {
25938 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25939 				    "no resources for dumping; "
25940 				    "error code: 0x%x, retrying",
25941 				    geterror(wr_bp));
25942 			} else {
25943 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25944 				    "no resources for dumping; retrying");
25945 			}
25946 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
25947 			if (wr_bp->b_flags & B_ERROR) {
25948 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25949 				    "no resources for dumping; error code: "
25950 				    "0x%x, retrying\n", geterror(wr_bp));
25951 			}
25952 		} else {
25953 			if (wr_bp->b_flags & B_ERROR) {
25954 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25955 				    "no resources for dumping; "
25956 				    "error code: 0x%x, retries failed, "
25957 				    "giving up.\n", geterror(wr_bp));
25958 			} else {
25959 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25960 				    "no resources for dumping; "
25961 				    "retries failed, giving up.\n");
25962 			}
25963 			mutex_enter(SD_MUTEX(un));
25964 			Restore_state(un);
25965 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
25966 				mutex_exit(SD_MUTEX(un));
25967 				scsi_free_consistent_buf(wr_bp);
25968 			} else {
25969 				mutex_exit(SD_MUTEX(un));
25970 			}
25971 			return (EIO);
25972 		}
25973 		drv_usecwait(10000);
25974 	}
25975 
25976 	if (un->un_partial_dma_supported == 1) {
25977 		/*
25978 		 * save the resid from PARTIAL_DMA
25979 		 */
25980 		dma_resid = wr_pktp->pkt_resid;
25981 		if (dma_resid != 0)
25982 			nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
25983 		wr_pktp->pkt_resid = 0;
25984 	} else {
25985 		dma_resid = 0;
25986 	}
25987 
25988 	/* SunBug 1222170 */
25989 	wr_pktp->pkt_flags = FLAG_NOINTR;
25990 
25991 	err = EIO;
25992 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25993 
25994 		/*
25995 		 * Scsi_poll returns 0 (success) if the command completes and
25996 		 * the status block is STATUS_GOOD.  We should only check
25997 		 * errors if this condition is not true.  Even then we should
25998 		 * send our own request sense packet only if we have a check
25999 		 * condition and auto request sense has not been performed by
26000 		 * the hba.
26001 		 */
26002 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26003 
26004 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26005 		    (wr_pktp->pkt_resid == 0)) {
26006 			err = SD_SUCCESS;
26007 			break;
26008 		}
26009 
26010 		/*
26011 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26012 		 */
26013 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26014 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26015 			    "Error while dumping state...Device is gone\n");
26016 			break;
26017 		}
26018 
26019 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26020 			SD_INFO(SD_LOG_DUMP, un,
26021 			    "sddump: write failed with CHECK, try # %d\n", i);
26022 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26023 				(void) sd_send_polled_RQS(un);
26024 			}
26025 
26026 			continue;
26027 		}
26028 
26029 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26030 			int reset_retval = 0;
26031 
26032 			SD_INFO(SD_LOG_DUMP, un,
26033 			    "sddump: write failed with BUSY, try # %d\n", i);
26034 
26035 			if (un->un_f_lun_reset_enabled == TRUE) {
26036 				reset_retval = scsi_reset(SD_ADDRESS(un),
26037 				    RESET_LUN);
26038 			}
26039 			if (reset_retval == 0) {
26040 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26041 			}
26042 			(void) sd_send_polled_RQS(un);
26043 
26044 		} else {
26045 			SD_INFO(SD_LOG_DUMP, un,
26046 			    "sddump: write failed with 0x%x, try # %d\n",
26047 			    SD_GET_PKT_STATUS(wr_pktp), i);
26048 			mutex_enter(SD_MUTEX(un));
26049 			sd_reset_target(un, wr_pktp);
26050 			mutex_exit(SD_MUTEX(un));
26051 		}
26052 
26053 		/*
26054 		 * If we are not getting anywhere with lun/target resets,
26055 		 * let's reset the bus.
26056 		 */
26057 		if (i == SD_NDUMP_RETRIES/2) {
26058 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26059 			(void) sd_send_polled_RQS(un);
26060 		}
26061 	}
26062 	}
26063 
26064 	scsi_destroy_pkt(wr_pktp);
26065 	mutex_enter(SD_MUTEX(un));
26066 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26067 		mutex_exit(SD_MUTEX(un));
26068 		scsi_free_consistent_buf(wr_bp);
26069 	} else {
26070 		mutex_exit(SD_MUTEX(un));
26071 	}
26072 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26073 	return (err);
26074 }
26075 
26076 /*
26077  *    Function: sd_scsi_poll()
26078  *
26079  * Description: This is a wrapper for the scsi_poll call.
26080  *
26081  *   Arguments: sd_lun - The unit structure
26082  *              scsi_pkt - The scsi packet being sent to the device.
26083  *
26084  * Return Code: 0 - Command completed successfully with good status
26085  *             -1 - Command failed.  This could indicate a check condition
26086  *                  or other status value requiring recovery action.
26087  *
26088  * NOTE: This code is only called off sddump().
26089  */
26090 
26091 static int
26092 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26093 {
26094 	int status;
26095 
26096 	ASSERT(un != NULL);
26097 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26098 	ASSERT(pktp != NULL);
26099 
26100 	status = SD_SUCCESS;
26101 
26102 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26103 		pktp->pkt_flags |= un->un_tagflags;
26104 		pktp->pkt_flags &= ~FLAG_NODISCON;
26105 	}
26106 
26107 	status = sd_ddi_scsi_poll(pktp);
26108 	/*
26109 	 * Scsi_poll returns 0 (success) if the command completes and the
26110 	 * status block is STATUS_GOOD.  We should only check errors if this
26111 	 * condition is not true.  Even then we should send our own request
26112 	 * sense packet only if we have a check condition and auto
26113 	 * request sense has not been performed by the hba.
26114 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26115 	 */
26116 	if ((status != SD_SUCCESS) &&
26117 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26118 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26119 	    (pktp->pkt_reason != CMD_DEV_GONE))
26120 		(void) sd_send_polled_RQS(un);
26121 
26122 	return (status);
26123 }
26124 
26125 /*
26126  *    Function: sd_send_polled_RQS()
26127  *
26128  * Description: This sends the request sense command to a device.
26129  *
26130  *   Arguments: sd_lun - The unit structure
26131  *
26132  * Return Code: 0 - Command completed successfully with good status
26133  *             -1 - Command failed.
26134  *
26135  */
26136 
26137 static int
26138 sd_send_polled_RQS(struct sd_lun *un)
26139 {
26140 	int	ret_val;
26141 	struct	scsi_pkt	*rqs_pktp;
26142 	struct	buf		*rqs_bp;
26143 
26144 	ASSERT(un != NULL);
26145 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26146 
26147 	ret_val = SD_SUCCESS;
26148 
26149 	rqs_pktp = un->un_rqs_pktp;
26150 	rqs_bp	 = un->un_rqs_bp;
26151 
26152 	mutex_enter(SD_MUTEX(un));
26153 
26154 	if (un->un_sense_isbusy) {
26155 		ret_val = SD_FAILURE;
26156 		mutex_exit(SD_MUTEX(un));
26157 		return (ret_val);
26158 	}
26159 
26160 	/*
26161 	 * If the request sense buffer (and packet) is not in use,
26162 	 * let's set the un_sense_isbusy and send our packet
26163 	 */
26164 	un->un_sense_isbusy 	= 1;
26165 	rqs_pktp->pkt_resid  	= 0;
26166 	rqs_pktp->pkt_reason 	= 0;
26167 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26168 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26169 
26170 	mutex_exit(SD_MUTEX(un));
26171 
26172 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26173 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26174 
26175 	/*
26176 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26177 	 * axle - it has a call into us!
26178 	 */
26179 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26180 		SD_INFO(SD_LOG_COMMON, un,
26181 		    "sd_send_polled_RQS: RQS failed\n");
26182 	}
26183 
26184 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26185 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26186 
26187 	mutex_enter(SD_MUTEX(un));
26188 	un->un_sense_isbusy = 0;
26189 	mutex_exit(SD_MUTEX(un));
26190 
26191 	return (ret_val);
26192 }
26193 
26194 /*
26195  * Defines needed for localized version of the scsi_poll routine.
26196  */
26197 #define	CSEC		10000			/* usecs */
26198 #define	SEC_TO_CSEC	(1000000/CSEC)
26199 
26200 /*
26201  *    Function: sd_ddi_scsi_poll()
26202  *
26203  * Description: Localized version of the scsi_poll routine.  The purpose is to
26204  *		send a scsi_pkt to a device as a polled command.  This version
26205  *		is to ensure more robust handling of transport errors.
26206  *		Specifically this routine cures not ready, coming ready
26207  *		transition for power up and reset of sonoma's.  This can take
26208  *		up to 45 seconds for power-on and 20 seconds for reset of a
26209  * 		sonoma lun.
26210  *
26211  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26212  *
26213  * Return Code: 0 - Command completed successfully with good status
26214  *             -1 - Command failed.
26215  *
26216  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26217  * be fixed (removing this code), we need to determine how to handle the
26218  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26219  *
26220  * NOTE: This code is only called off sddump().
26221  */
26222 static int
26223 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26224 {
26225 	int			rval = -1;
26226 	int			savef;
26227 	long			savet;
26228 	void			(*savec)();
26229 	int			timeout;
26230 	int			busy_count;
26231 	int			poll_delay;
26232 	int			rc;
26233 	uint8_t			*sensep;
26234 	struct scsi_arq_status	*arqstat;
26235 	extern int		do_polled_io;
26236 
26237 	ASSERT(pkt->pkt_scbp);
26238 
26239 	/*
26240 	 * save old flags..
26241 	 */
26242 	savef = pkt->pkt_flags;
26243 	savec = pkt->pkt_comp;
26244 	savet = pkt->pkt_time;
26245 
26246 	pkt->pkt_flags |= FLAG_NOINTR;
26247 
26248 	/*
26249 	 * XXX there is nothing in the SCSA spec that states that we should not
26250 	 * do a callback for polled cmds; however, removing this will break sd
26251 	 * and probably other target drivers
26252 	 */
26253 	pkt->pkt_comp = NULL;
26254 
26255 	/*
26256 	 * we don't like a polled command without timeout.
26257 	 * 60 seconds seems long enough.
26258 	 */
26259 	if (pkt->pkt_time == 0)
26260 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26261 
26262 	/*
26263 	 * Send polled cmd.
26264 	 *
26265 	 * We do some error recovery for various errors.  Tran_busy,
26266 	 * queue full, and non-dispatched commands are retried every 10 msec.
26267 	 * as they are typically transient failures.  Busy status and Not
26268 	 * Ready are retried every second as this status takes a while to
26269 	 * change.
26270 	 */
26271 	timeout = pkt->pkt_time * SEC_TO_CSEC;
26272 
26273 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26274 		/*
26275 		 * Initialize pkt status variables.
26276 		 */
26277 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26278 
26279 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26280 			if (rc != TRAN_BUSY) {
26281 				/* Transport failed - give up. */
26282 				break;
26283 			} else {
26284 				/* Transport busy - try again. */
26285 				poll_delay = 1 * CSEC;		/* 10 msec. */
26286 			}
26287 		} else {
26288 			/*
26289 			 * Transport accepted - check pkt status.
26290 			 */
26291 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26292 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26293 			    (rc == STATUS_CHECK) &&
26294 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
26295 				arqstat =
26296 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26297 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26298 			} else {
26299 				sensep = NULL;
26300 			}
26301 
26302 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26303 			    (rc == STATUS_GOOD)) {
26304 				/* No error - we're done */
26305 				rval = 0;
26306 				break;
26307 
26308 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26309 				/* Lost connection - give up */
26310 				break;
26311 
26312 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26313 			    (pkt->pkt_state == 0)) {
26314 				/* Pkt not dispatched - try again. */
26315 				poll_delay = 1 * CSEC;		/* 10 msec. */
26316 
26317 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26318 			    (rc == STATUS_QFULL)) {
26319 				/* Queue full - try again. */
26320 				poll_delay = 1 * CSEC;		/* 10 msec. */
26321 
26322 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26323 			    (rc == STATUS_BUSY)) {
26324 				/* Busy - try again. */
26325 				poll_delay = 100 * CSEC;	/* 1 sec. */
26326 				busy_count += (SEC_TO_CSEC - 1);
26327 
26328 			} else if ((sensep != NULL) &&
26329 			    (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26330 				/*
26331 				 * Unit Attention - try again.
26332 				 * Pretend it took 1 sec.
26333 				 * NOTE: 'continue' avoids poll_delay
26334 				 */
26335 				busy_count += (SEC_TO_CSEC - 1);
26336 				continue;
26337 
26338 			} else if ((sensep != NULL) &&
26339 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26340 			    (scsi_sense_asc(sensep) == 0x04) &&
26341 			    (scsi_sense_ascq(sensep) == 0x01)) {
26342 				/*
26343 				 * Not ready -> ready - try again.
26344 				 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26345 				 * ...same as STATUS_BUSY
26346 				 */
26347 				poll_delay = 100 * CSEC;	/* 1 sec. */
26348 				busy_count += (SEC_TO_CSEC - 1);
26349 
26350 			} else {
26351 				/* BAD status - give up. */
26352 				break;
26353 			}
26354 		}
26355 
26356 		if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26357 		    !do_polled_io) {
26358 			delay(drv_usectohz(poll_delay));
26359 		} else {
26360 			/* we busy wait during cpr_dump or interrupt threads */
26361 			drv_usecwait(poll_delay);
26362 		}
26363 	}
26364 
26365 	pkt->pkt_flags = savef;
26366 	pkt->pkt_comp = savec;
26367 	pkt->pkt_time = savet;
26368 
26369 	/* return on error */
26370 	if (rval)
26371 		return (rval);
26372 
26373 	/*
26374 	 * This is not a performance critical code path.
26375 	 *
26376 	 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26377 	 * issues associated with looking at DMA memory prior to
26378 	 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26379 	 */
26380 	scsi_sync_pkt(pkt);
26381 	return (0);
26382 }
26383 
26384 
26385 
26386 /*
26387  *    Function: sd_persistent_reservation_in_read_keys
26388  *
26389  * Description: This routine is the driver entry point for handling CD-ROM
26390  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26391  *		by sending the SCSI-3 PRIN commands to the device.
26392  *		Processes the read keys command response by copying the
26393  *		reservation key information into the user provided buffer.
26394  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26395  *
26396  *   Arguments: un   -  Pointer to soft state struct for the target.
26397  *		usrp -	user provided pointer to multihost Persistent In Read
26398  *			Keys structure (mhioc_inkeys_t)
26399  *		flag -	this argument is a pass through to ddi_copyxxx()
26400  *			directly from the mode argument of ioctl().
26401  *
26402  * Return Code: 0   - Success
26403  *		EACCES
26404  *		ENOTSUP
26405  *		errno return code from sd_send_scsi_cmd()
26406  *
26407  *     Context: Can sleep. Does not return until command is completed.
26408  */
26409 
26410 static int
26411 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26412     mhioc_inkeys_t *usrp, int flag)
26413 {
26414 #ifdef _MULTI_DATAMODEL
26415 	struct mhioc_key_list32	li32;
26416 #endif
26417 	sd_prin_readkeys_t	*in;
26418 	mhioc_inkeys_t		*ptr;
26419 	mhioc_key_list_t	li;
26420 	uchar_t			*data_bufp;
26421 	int 			data_len;
26422 	int			rval = 0;
26423 	size_t			copysz;
26424 	sd_ssc_t		*ssc;
26425 
26426 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26427 		return (EINVAL);
26428 	}
26429 	bzero(&li, sizeof (mhioc_key_list_t));
26430 
26431 	ssc = sd_ssc_init(un);
26432 
26433 	/*
26434 	 * Get the listsize from user
26435 	 */
26436 #ifdef _MULTI_DATAMODEL
26437 
26438 	switch (ddi_model_convert_from(flag & FMODELS)) {
26439 	case DDI_MODEL_ILP32:
26440 		copysz = sizeof (struct mhioc_key_list32);
26441 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26442 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26443 			    "sd_persistent_reservation_in_read_keys: "
26444 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26445 			rval = EFAULT;
26446 			goto done;
26447 		}
26448 		li.listsize = li32.listsize;
26449 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26450 		break;
26451 
26452 	case DDI_MODEL_NONE:
26453 		copysz = sizeof (mhioc_key_list_t);
26454 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26455 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26456 			    "sd_persistent_reservation_in_read_keys: "
26457 			    "failed ddi_copyin: mhioc_key_list_t\n");
26458 			rval = EFAULT;
26459 			goto done;
26460 		}
26461 		break;
26462 	}
26463 
26464 #else /* ! _MULTI_DATAMODEL */
26465 	copysz = sizeof (mhioc_key_list_t);
26466 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26467 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26468 		    "sd_persistent_reservation_in_read_keys: "
26469 		    "failed ddi_copyin: mhioc_key_list_t\n");
26470 		rval = EFAULT;
26471 		goto done;
26472 	}
26473 #endif
26474 
26475 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26476 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26477 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26478 
26479 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26480 	    data_len, data_bufp);
26481 	if (rval != 0) {
26482 		if (rval == EIO)
26483 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26484 		else
26485 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26486 		goto done;
26487 	}
26488 	in = (sd_prin_readkeys_t *)data_bufp;
26489 	ptr->generation = BE_32(in->generation);
26490 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26491 
26492 	/*
26493 	 * Return the min(listsize, listlen) keys
26494 	 */
26495 #ifdef _MULTI_DATAMODEL
26496 
26497 	switch (ddi_model_convert_from(flag & FMODELS)) {
26498 	case DDI_MODEL_ILP32:
26499 		li32.listlen = li.listlen;
26500 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26501 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26502 			    "sd_persistent_reservation_in_read_keys: "
26503 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26504 			rval = EFAULT;
26505 			goto done;
26506 		}
26507 		break;
26508 
26509 	case DDI_MODEL_NONE:
26510 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26511 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26512 			    "sd_persistent_reservation_in_read_keys: "
26513 			    "failed ddi_copyout: mhioc_key_list_t\n");
26514 			rval = EFAULT;
26515 			goto done;
26516 		}
26517 		break;
26518 	}
26519 
26520 #else /* ! _MULTI_DATAMODEL */
26521 
26522 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26523 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26524 		    "sd_persistent_reservation_in_read_keys: "
26525 		    "failed ddi_copyout: mhioc_key_list_t\n");
26526 		rval = EFAULT;
26527 		goto done;
26528 	}
26529 
26530 #endif /* _MULTI_DATAMODEL */
26531 
26532 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26533 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26534 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26535 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26536 		    "sd_persistent_reservation_in_read_keys: "
26537 		    "failed ddi_copyout: keylist\n");
26538 		rval = EFAULT;
26539 	}
26540 done:
26541 	sd_ssc_fini(ssc);
26542 	kmem_free(data_bufp, data_len);
26543 	return (rval);
26544 }
26545 
26546 
26547 /*
26548  *    Function: sd_persistent_reservation_in_read_resv
26549  *
26550  * Description: This routine is the driver entry point for handling CD-ROM
26551  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26552  *		by sending the SCSI-3 PRIN commands to the device.
26553  *		Process the read persistent reservations command response by
26554  *		copying the reservation information into the user provided
26555  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26556  *
26557  *   Arguments: un   -  Pointer to soft state struct for the target.
26558  *		usrp -	user provided pointer to multihost Persistent In Read
26559  *			Keys structure (mhioc_inkeys_t)
26560  *		flag -	this argument is a pass through to ddi_copyxxx()
26561  *			directly from the mode argument of ioctl().
26562  *
26563  * Return Code: 0   - Success
26564  *		EACCES
26565  *		ENOTSUP
26566  *		errno return code from sd_send_scsi_cmd()
26567  *
26568  *     Context: Can sleep. Does not return until command is completed.
26569  */
26570 
26571 static int
26572 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26573     mhioc_inresvs_t *usrp, int flag)
26574 {
26575 #ifdef _MULTI_DATAMODEL
26576 	struct mhioc_resv_desc_list32 resvlist32;
26577 #endif
26578 	sd_prin_readresv_t	*in;
26579 	mhioc_inresvs_t		*ptr;
26580 	sd_readresv_desc_t	*readresv_ptr;
26581 	mhioc_resv_desc_list_t	resvlist;
26582 	mhioc_resv_desc_t 	resvdesc;
26583 	uchar_t			*data_bufp = NULL;
26584 	int 			data_len;
26585 	int			rval = 0;
26586 	int			i;
26587 	size_t			copysz;
26588 	mhioc_resv_desc_t	*bufp;
26589 	sd_ssc_t		*ssc;
26590 
26591 	if ((ptr = usrp) == NULL) {
26592 		return (EINVAL);
26593 	}
26594 
26595 	ssc = sd_ssc_init(un);
26596 
26597 	/*
26598 	 * Get the listsize from user
26599 	 */
26600 #ifdef _MULTI_DATAMODEL
26601 	switch (ddi_model_convert_from(flag & FMODELS)) {
26602 	case DDI_MODEL_ILP32:
26603 		copysz = sizeof (struct mhioc_resv_desc_list32);
26604 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26605 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26606 			    "sd_persistent_reservation_in_read_resv: "
26607 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26608 			rval = EFAULT;
26609 			goto done;
26610 		}
26611 		resvlist.listsize = resvlist32.listsize;
26612 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26613 		break;
26614 
26615 	case DDI_MODEL_NONE:
26616 		copysz = sizeof (mhioc_resv_desc_list_t);
26617 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26618 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26619 			    "sd_persistent_reservation_in_read_resv: "
26620 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26621 			rval = EFAULT;
26622 			goto done;
26623 		}
26624 		break;
26625 	}
26626 #else /* ! _MULTI_DATAMODEL */
26627 	copysz = sizeof (mhioc_resv_desc_list_t);
26628 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26629 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26630 		    "sd_persistent_reservation_in_read_resv: "
26631 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26632 		rval = EFAULT;
26633 		goto done;
26634 	}
26635 #endif /* ! _MULTI_DATAMODEL */
26636 
26637 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26638 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26639 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26640 
26641 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26642 	    data_len, data_bufp);
26643 	if (rval != 0) {
26644 		if (rval == EIO)
26645 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26646 		else
26647 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26648 		goto done;
26649 	}
26650 	in = (sd_prin_readresv_t *)data_bufp;
26651 	ptr->generation = BE_32(in->generation);
26652 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26653 
26654 	/*
26655 	 * Return the min(listsize, listlen( keys
26656 	 */
26657 #ifdef _MULTI_DATAMODEL
26658 
26659 	switch (ddi_model_convert_from(flag & FMODELS)) {
26660 	case DDI_MODEL_ILP32:
26661 		resvlist32.listlen = resvlist.listlen;
26662 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26663 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26664 			    "sd_persistent_reservation_in_read_resv: "
26665 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26666 			rval = EFAULT;
26667 			goto done;
26668 		}
26669 		break;
26670 
26671 	case DDI_MODEL_NONE:
26672 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26673 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26674 			    "sd_persistent_reservation_in_read_resv: "
26675 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26676 			rval = EFAULT;
26677 			goto done;
26678 		}
26679 		break;
26680 	}
26681 
26682 #else /* ! _MULTI_DATAMODEL */
26683 
26684 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26685 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26686 		    "sd_persistent_reservation_in_read_resv: "
26687 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26688 		rval = EFAULT;
26689 		goto done;
26690 	}
26691 
26692 #endif /* ! _MULTI_DATAMODEL */
26693 
26694 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26695 	bufp = resvlist.list;
26696 	copysz = sizeof (mhioc_resv_desc_t);
26697 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26698 	    i++, readresv_ptr++, bufp++) {
26699 
26700 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26701 		    MHIOC_RESV_KEY_SIZE);
26702 		resvdesc.type  = readresv_ptr->type;
26703 		resvdesc.scope = readresv_ptr->scope;
26704 		resvdesc.scope_specific_addr =
26705 		    BE_32(readresv_ptr->scope_specific_addr);
26706 
26707 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26708 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26709 			    "sd_persistent_reservation_in_read_resv: "
26710 			    "failed ddi_copyout: resvlist\n");
26711 			rval = EFAULT;
26712 			goto done;
26713 		}
26714 	}
26715 done:
26716 	sd_ssc_fini(ssc);
26717 	/* only if data_bufp is allocated, we need to free it */
26718 	if (data_bufp) {
26719 		kmem_free(data_bufp, data_len);
26720 	}
26721 	return (rval);
26722 }
26723 
26724 
26725 /*
26726  *    Function: sr_change_blkmode()
26727  *
26728  * Description: This routine is the driver entry point for handling CD-ROM
26729  *		block mode ioctl requests. Support for returning and changing
26730  *		the current block size in use by the device is implemented. The
26731  *		LBA size is changed via a MODE SELECT Block Descriptor.
26732  *
26733  *		This routine issues a mode sense with an allocation length of
26734  *		12 bytes for the mode page header and a single block descriptor.
26735  *
26736  *   Arguments: dev - the device 'dev_t'
26737  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26738  *		      CDROMSBLKMODE (set)
26739  *		data - current block size or requested block size
26740  *		flag - this argument is a pass through to ddi_copyxxx() directly
26741  *		       from the mode argument of ioctl().
26742  *
26743  * Return Code: the code returned by sd_send_scsi_cmd()
26744  *		EINVAL if invalid arguments are provided
26745  *		EFAULT if ddi_copyxxx() fails
26746  *		ENXIO if fail ddi_get_soft_state
26747  *		EIO if invalid mode sense block descriptor length
26748  *
26749  */
26750 
26751 static int
26752 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26753 {
26754 	struct sd_lun			*un = NULL;
26755 	struct mode_header		*sense_mhp, *select_mhp;
26756 	struct block_descriptor		*sense_desc, *select_desc;
26757 	int				current_bsize;
26758 	int				rval = EINVAL;
26759 	uchar_t				*sense = NULL;
26760 	uchar_t				*select = NULL;
26761 	sd_ssc_t			*ssc;
26762 
26763 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26764 
26765 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26766 		return (ENXIO);
26767 	}
26768 
26769 	/*
26770 	 * The block length is changed via the Mode Select block descriptor, the
26771 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26772 	 * required as part of this routine. Therefore the mode sense allocation
26773 	 * length is specified to be the length of a mode page header and a
26774 	 * block descriptor.
26775 	 */
26776 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26777 
26778 	ssc = sd_ssc_init(un);
26779 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26780 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26781 	sd_ssc_fini(ssc);
26782 	if (rval != 0) {
26783 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26784 		    "sr_change_blkmode: Mode Sense Failed\n");
26785 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26786 		return (rval);
26787 	}
26788 
26789 	/* Check the block descriptor len to handle only 1 block descriptor */
26790 	sense_mhp = (struct mode_header *)sense;
26791 	if ((sense_mhp->bdesc_length == 0) ||
26792 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26793 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26794 		    "sr_change_blkmode: Mode Sense returned invalid block"
26795 		    " descriptor length\n");
26796 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26797 		return (EIO);
26798 	}
26799 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26800 	current_bsize = ((sense_desc->blksize_hi << 16) |
26801 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26802 
26803 	/* Process command */
26804 	switch (cmd) {
26805 	case CDROMGBLKMODE:
26806 		/* Return the block size obtained during the mode sense */
26807 		if (ddi_copyout(&current_bsize, (void *)data,
26808 		    sizeof (int), flag) != 0)
26809 			rval = EFAULT;
26810 		break;
26811 	case CDROMSBLKMODE:
26812 		/* Validate the requested block size */
26813 		switch (data) {
26814 		case CDROM_BLK_512:
26815 		case CDROM_BLK_1024:
26816 		case CDROM_BLK_2048:
26817 		case CDROM_BLK_2056:
26818 		case CDROM_BLK_2336:
26819 		case CDROM_BLK_2340:
26820 		case CDROM_BLK_2352:
26821 		case CDROM_BLK_2368:
26822 		case CDROM_BLK_2448:
26823 		case CDROM_BLK_2646:
26824 		case CDROM_BLK_2647:
26825 			break;
26826 		default:
26827 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26828 			    "sr_change_blkmode: "
26829 			    "Block Size '%ld' Not Supported\n", data);
26830 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26831 			return (EINVAL);
26832 		}
26833 
26834 		/*
26835 		 * The current block size matches the requested block size so
26836 		 * there is no need to send the mode select to change the size
26837 		 */
26838 		if (current_bsize == data) {
26839 			break;
26840 		}
26841 
26842 		/* Build the select data for the requested block size */
26843 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26844 		select_mhp = (struct mode_header *)select;
26845 		select_desc =
26846 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26847 		/*
26848 		 * The LBA size is changed via the block descriptor, so the
26849 		 * descriptor is built according to the user data
26850 		 */
26851 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26852 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26853 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26854 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
26855 
26856 		/* Send the mode select for the requested block size */
26857 		ssc = sd_ssc_init(un);
26858 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26859 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26860 		    SD_PATH_STANDARD);
26861 		sd_ssc_fini(ssc);
26862 		if (rval != 0) {
26863 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26864 			    "sr_change_blkmode: Mode Select Failed\n");
26865 			/*
26866 			 * The mode select failed for the requested block size,
26867 			 * so reset the data for the original block size and
26868 			 * send it to the target. The error is indicated by the
26869 			 * return value for the failed mode select.
26870 			 */
26871 			select_desc->blksize_hi  = sense_desc->blksize_hi;
26872 			select_desc->blksize_mid = sense_desc->blksize_mid;
26873 			select_desc->blksize_lo  = sense_desc->blksize_lo;
26874 			ssc = sd_ssc_init(un);
26875 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26876 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26877 			    SD_PATH_STANDARD);
26878 			sd_ssc_fini(ssc);
26879 		} else {
26880 			ASSERT(!mutex_owned(SD_MUTEX(un)));
26881 			mutex_enter(SD_MUTEX(un));
26882 			sd_update_block_info(un, (uint32_t)data, 0);
26883 			mutex_exit(SD_MUTEX(un));
26884 		}
26885 		break;
26886 	default:
26887 		/* should not reach here, but check anyway */
26888 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26889 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
26890 		rval = EINVAL;
26891 		break;
26892 	}
26893 
26894 	if (select) {
26895 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
26896 	}
26897 	if (sense) {
26898 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26899 	}
26900 	return (rval);
26901 }
26902 
26903 
26904 /*
26905  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
26906  * implement driver support for getting and setting the CD speed. The command
26907  * set used will be based on the device type. If the device has not been
26908  * identified as MMC the Toshiba vendor specific mode page will be used. If
26909  * the device is MMC but does not support the Real Time Streaming feature
26910  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
26911  * be used to read the speed.
26912  */
26913 
26914 /*
26915  *    Function: sr_change_speed()
26916  *
26917  * Description: This routine is the driver entry point for handling CD-ROM
26918  *		drive speed ioctl requests for devices supporting the Toshiba
26919  *		vendor specific drive speed mode page. Support for returning
26920  *		and changing the current drive speed in use by the device is
26921  *		implemented.
26922  *
26923  *   Arguments: dev - the device 'dev_t'
26924  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
26925  *		      CDROMSDRVSPEED (set)
26926  *		data - current drive speed or requested drive speed
26927  *		flag - this argument is a pass through to ddi_copyxxx() directly
26928  *		       from the mode argument of ioctl().
26929  *
26930  * Return Code: the code returned by sd_send_scsi_cmd()
26931  *		EINVAL if invalid arguments are provided
26932  *		EFAULT if ddi_copyxxx() fails
26933  *		ENXIO if fail ddi_get_soft_state
26934  *		EIO if invalid mode sense block descriptor length
26935  */
26936 
26937 static int
26938 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
26939 {
26940 	struct sd_lun			*un = NULL;
26941 	struct mode_header		*sense_mhp, *select_mhp;
26942 	struct mode_speed		*sense_page, *select_page;
26943 	int				current_speed;
26944 	int				rval = EINVAL;
26945 	int				bd_len;
26946 	uchar_t				*sense = NULL;
26947 	uchar_t				*select = NULL;
26948 	sd_ssc_t			*ssc;
26949 
26950 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
26951 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26952 		return (ENXIO);
26953 	}
26954 
26955 	/*
26956 	 * Note: The drive speed is being modified here according to a Toshiba
26957 	 * vendor specific mode page (0x31).
26958 	 */
26959 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
26960 
26961 	ssc = sd_ssc_init(un);
26962 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26963 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
26964 	    SD_PATH_STANDARD);
26965 	sd_ssc_fini(ssc);
26966 	if (rval != 0) {
26967 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26968 		    "sr_change_speed: Mode Sense Failed\n");
26969 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26970 		return (rval);
26971 	}
26972 	sense_mhp  = (struct mode_header *)sense;
26973 
26974 	/* Check the block descriptor len to handle only 1 block descriptor */
26975 	bd_len = sense_mhp->bdesc_length;
26976 	if (bd_len > MODE_BLK_DESC_LENGTH) {
26977 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26978 		    "sr_change_speed: Mode Sense returned invalid block "
26979 		    "descriptor length\n");
26980 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26981 		return (EIO);
26982 	}
26983 
26984 	sense_page = (struct mode_speed *)
26985 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
26986 	current_speed = sense_page->speed;
26987 
26988 	/* Process command */
26989 	switch (cmd) {
26990 	case CDROMGDRVSPEED:
26991 		/* Return the drive speed obtained during the mode sense */
26992 		if (current_speed == 0x2) {
26993 			current_speed = CDROM_TWELVE_SPEED;
26994 		}
26995 		if (ddi_copyout(&current_speed, (void *)data,
26996 		    sizeof (int), flag) != 0) {
26997 			rval = EFAULT;
26998 		}
26999 		break;
27000 	case CDROMSDRVSPEED:
27001 		/* Validate the requested drive speed */
27002 		switch ((uchar_t)data) {
27003 		case CDROM_TWELVE_SPEED:
27004 			data = 0x2;
27005 			/*FALLTHROUGH*/
27006 		case CDROM_NORMAL_SPEED:
27007 		case CDROM_DOUBLE_SPEED:
27008 		case CDROM_QUAD_SPEED:
27009 		case CDROM_MAXIMUM_SPEED:
27010 			break;
27011 		default:
27012 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27013 			    "sr_change_speed: "
27014 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27015 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27016 			return (EINVAL);
27017 		}
27018 
27019 		/*
27020 		 * The current drive speed matches the requested drive speed so
27021 		 * there is no need to send the mode select to change the speed
27022 		 */
27023 		if (current_speed == data) {
27024 			break;
27025 		}
27026 
27027 		/* Build the select data for the requested drive speed */
27028 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27029 		select_mhp = (struct mode_header *)select;
27030 		select_mhp->bdesc_length = 0;
27031 		select_page =
27032 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27033 		select_page =
27034 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27035 		select_page->mode_page.code = CDROM_MODE_SPEED;
27036 		select_page->mode_page.length = 2;
27037 		select_page->speed = (uchar_t)data;
27038 
27039 		/* Send the mode select for the requested block size */
27040 		ssc = sd_ssc_init(un);
27041 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27042 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27043 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27044 		sd_ssc_fini(ssc);
27045 		if (rval != 0) {
27046 			/*
27047 			 * The mode select failed for the requested drive speed,
27048 			 * so reset the data for the original drive speed and
27049 			 * send it to the target. The error is indicated by the
27050 			 * return value for the failed mode select.
27051 			 */
27052 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27053 			    "sr_drive_speed: Mode Select Failed\n");
27054 			select_page->speed = sense_page->speed;
27055 			ssc = sd_ssc_init(un);
27056 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27057 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27058 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27059 			sd_ssc_fini(ssc);
27060 		}
27061 		break;
27062 	default:
27063 		/* should not reach here, but check anyway */
27064 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27065 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27066 		rval = EINVAL;
27067 		break;
27068 	}
27069 
27070 	if (select) {
27071 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27072 	}
27073 	if (sense) {
27074 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27075 	}
27076 
27077 	return (rval);
27078 }
27079 
27080 
27081 /*
27082  *    Function: sr_atapi_change_speed()
27083  *
27084  * Description: This routine is the driver entry point for handling CD-ROM
27085  *		drive speed ioctl requests for MMC devices that do not support
27086  *		the Real Time Streaming feature (0x107).
27087  *
27088  *		Note: This routine will use the SET SPEED command which may not
27089  *		be supported by all devices.
27090  *
27091  *   Arguments: dev- the device 'dev_t'
27092  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27093  *		     CDROMSDRVSPEED (set)
27094  *		data- current drive speed or requested drive speed
27095  *		flag- this argument is a pass through to ddi_copyxxx() directly
27096  *		      from the mode argument of ioctl().
27097  *
27098  * Return Code: the code returned by sd_send_scsi_cmd()
27099  *		EINVAL if invalid arguments are provided
27100  *		EFAULT if ddi_copyxxx() fails
27101  *		ENXIO if fail ddi_get_soft_state
27102  *		EIO if invalid mode sense block descriptor length
27103  */
27104 
27105 static int
27106 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27107 {
27108 	struct sd_lun			*un;
27109 	struct uscsi_cmd		*com = NULL;
27110 	struct mode_header_grp2		*sense_mhp;
27111 	uchar_t				*sense_page;
27112 	uchar_t				*sense = NULL;
27113 	char				cdb[CDB_GROUP5];
27114 	int				bd_len;
27115 	int				current_speed = 0;
27116 	int				max_speed = 0;
27117 	int				rval;
27118 	sd_ssc_t			*ssc;
27119 
27120 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27121 
27122 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27123 		return (ENXIO);
27124 	}
27125 
27126 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27127 
27128 	ssc = sd_ssc_init(un);
27129 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27130 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27131 	    SD_PATH_STANDARD);
27132 	sd_ssc_fini(ssc);
27133 	if (rval != 0) {
27134 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27135 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27136 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27137 		return (rval);
27138 	}
27139 
27140 	/* Check the block descriptor len to handle only 1 block descriptor */
27141 	sense_mhp = (struct mode_header_grp2 *)sense;
27142 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27143 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27144 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27145 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27146 		    "block descriptor length\n");
27147 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27148 		return (EIO);
27149 	}
27150 
27151 	/* Calculate the current and maximum drive speeds */
27152 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27153 	current_speed = (sense_page[14] << 8) | sense_page[15];
27154 	max_speed = (sense_page[8] << 8) | sense_page[9];
27155 
27156 	/* Process the command */
27157 	switch (cmd) {
27158 	case CDROMGDRVSPEED:
27159 		current_speed /= SD_SPEED_1X;
27160 		if (ddi_copyout(&current_speed, (void *)data,
27161 		    sizeof (int), flag) != 0)
27162 			rval = EFAULT;
27163 		break;
27164 	case CDROMSDRVSPEED:
27165 		/* Convert the speed code to KB/sec */
27166 		switch ((uchar_t)data) {
27167 		case CDROM_NORMAL_SPEED:
27168 			current_speed = SD_SPEED_1X;
27169 			break;
27170 		case CDROM_DOUBLE_SPEED:
27171 			current_speed = 2 * SD_SPEED_1X;
27172 			break;
27173 		case CDROM_QUAD_SPEED:
27174 			current_speed = 4 * SD_SPEED_1X;
27175 			break;
27176 		case CDROM_TWELVE_SPEED:
27177 			current_speed = 12 * SD_SPEED_1X;
27178 			break;
27179 		case CDROM_MAXIMUM_SPEED:
27180 			current_speed = 0xffff;
27181 			break;
27182 		default:
27183 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27184 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27185 			    (uchar_t)data);
27186 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27187 			return (EINVAL);
27188 		}
27189 
27190 		/* Check the request against the drive's max speed. */
27191 		if (current_speed != 0xffff) {
27192 			if (current_speed > max_speed) {
27193 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27194 				return (EINVAL);
27195 			}
27196 		}
27197 
27198 		/*
27199 		 * Build and send the SET SPEED command
27200 		 *
27201 		 * Note: The SET SPEED (0xBB) command used in this routine is
27202 		 * obsolete per the SCSI MMC spec but still supported in the
27203 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27204 		 * therefore the command is still implemented in this routine.
27205 		 */
27206 		bzero(cdb, sizeof (cdb));
27207 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27208 		cdb[2] = (uchar_t)(current_speed >> 8);
27209 		cdb[3] = (uchar_t)current_speed;
27210 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27211 		com->uscsi_cdb	   = (caddr_t)cdb;
27212 		com->uscsi_cdblen  = CDB_GROUP5;
27213 		com->uscsi_bufaddr = NULL;
27214 		com->uscsi_buflen  = 0;
27215 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27216 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27217 		break;
27218 	default:
27219 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27220 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27221 		rval = EINVAL;
27222 	}
27223 
27224 	if (sense) {
27225 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27226 	}
27227 	if (com) {
27228 		kmem_free(com, sizeof (*com));
27229 	}
27230 	return (rval);
27231 }
27232 
27233 
27234 /*
27235  *    Function: sr_pause_resume()
27236  *
27237  * Description: This routine is the driver entry point for handling CD-ROM
27238  *		pause/resume ioctl requests. This only affects the audio play
27239  *		operation.
27240  *
27241  *   Arguments: dev - the device 'dev_t'
27242  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27243  *		      for setting the resume bit of the cdb.
27244  *
27245  * Return Code: the code returned by sd_send_scsi_cmd()
27246  *		EINVAL if invalid mode specified
27247  *
27248  */
27249 
27250 static int
27251 sr_pause_resume(dev_t dev, int cmd)
27252 {
27253 	struct sd_lun		*un;
27254 	struct uscsi_cmd	*com;
27255 	char			cdb[CDB_GROUP1];
27256 	int			rval;
27257 
27258 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27259 		return (ENXIO);
27260 	}
27261 
27262 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27263 	bzero(cdb, CDB_GROUP1);
27264 	cdb[0] = SCMD_PAUSE_RESUME;
27265 	switch (cmd) {
27266 	case CDROMRESUME:
27267 		cdb[8] = 1;
27268 		break;
27269 	case CDROMPAUSE:
27270 		cdb[8] = 0;
27271 		break;
27272 	default:
27273 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27274 		    " Command '%x' Not Supported\n", cmd);
27275 		rval = EINVAL;
27276 		goto done;
27277 	}
27278 
27279 	com->uscsi_cdb    = cdb;
27280 	com->uscsi_cdblen = CDB_GROUP1;
27281 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27282 
27283 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27284 	    SD_PATH_STANDARD);
27285 
27286 done:
27287 	kmem_free(com, sizeof (*com));
27288 	return (rval);
27289 }
27290 
27291 
27292 /*
27293  *    Function: sr_play_msf()
27294  *
27295  * Description: This routine is the driver entry point for handling CD-ROM
27296  *		ioctl requests to output the audio signals at the specified
27297  *		starting address and continue the audio play until the specified
27298  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27299  *		Frame (MSF) format.
27300  *
27301  *   Arguments: dev	- the device 'dev_t'
27302  *		data	- pointer to user provided audio msf structure,
27303  *		          specifying start/end addresses.
27304  *		flag	- this argument is a pass through to ddi_copyxxx()
27305  *		          directly from the mode argument of ioctl().
27306  *
27307  * Return Code: the code returned by sd_send_scsi_cmd()
27308  *		EFAULT if ddi_copyxxx() fails
27309  *		ENXIO if fail ddi_get_soft_state
27310  *		EINVAL if data pointer is NULL
27311  */
27312 
27313 static int
27314 sr_play_msf(dev_t dev, caddr_t data, int flag)
27315 {
27316 	struct sd_lun		*un;
27317 	struct uscsi_cmd	*com;
27318 	struct cdrom_msf	msf_struct;
27319 	struct cdrom_msf	*msf = &msf_struct;
27320 	char			cdb[CDB_GROUP1];
27321 	int			rval;
27322 
27323 	if (data == NULL) {
27324 		return (EINVAL);
27325 	}
27326 
27327 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27328 		return (ENXIO);
27329 	}
27330 
27331 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27332 		return (EFAULT);
27333 	}
27334 
27335 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27336 	bzero(cdb, CDB_GROUP1);
27337 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27338 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27339 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27340 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27341 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27342 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27343 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27344 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27345 	} else {
27346 		cdb[3] = msf->cdmsf_min0;
27347 		cdb[4] = msf->cdmsf_sec0;
27348 		cdb[5] = msf->cdmsf_frame0;
27349 		cdb[6] = msf->cdmsf_min1;
27350 		cdb[7] = msf->cdmsf_sec1;
27351 		cdb[8] = msf->cdmsf_frame1;
27352 	}
27353 	com->uscsi_cdb    = cdb;
27354 	com->uscsi_cdblen = CDB_GROUP1;
27355 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27356 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27357 	    SD_PATH_STANDARD);
27358 	kmem_free(com, sizeof (*com));
27359 	return (rval);
27360 }
27361 
27362 
27363 /*
27364  *    Function: sr_play_trkind()
27365  *
27366  * Description: This routine is the driver entry point for handling CD-ROM
27367  *		ioctl requests to output the audio signals at the specified
27368  *		starting address and continue the audio play until the specified
27369  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27370  *		format.
27371  *
27372  *   Arguments: dev	- the device 'dev_t'
27373  *		data	- pointer to user provided audio track/index structure,
27374  *		          specifying start/end addresses.
27375  *		flag	- this argument is a pass through to ddi_copyxxx()
27376  *		          directly from the mode argument of ioctl().
27377  *
27378  * Return Code: the code returned by sd_send_scsi_cmd()
27379  *		EFAULT if ddi_copyxxx() fails
27380  *		ENXIO if fail ddi_get_soft_state
27381  *		EINVAL if data pointer is NULL
27382  */
27383 
27384 static int
27385 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27386 {
27387 	struct cdrom_ti		ti_struct;
27388 	struct cdrom_ti		*ti = &ti_struct;
27389 	struct uscsi_cmd	*com = NULL;
27390 	char			cdb[CDB_GROUP1];
27391 	int			rval;
27392 
27393 	if (data == NULL) {
27394 		return (EINVAL);
27395 	}
27396 
27397 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27398 		return (EFAULT);
27399 	}
27400 
27401 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27402 	bzero(cdb, CDB_GROUP1);
27403 	cdb[0] = SCMD_PLAYAUDIO_TI;
27404 	cdb[4] = ti->cdti_trk0;
27405 	cdb[5] = ti->cdti_ind0;
27406 	cdb[7] = ti->cdti_trk1;
27407 	cdb[8] = ti->cdti_ind1;
27408 	com->uscsi_cdb    = cdb;
27409 	com->uscsi_cdblen = CDB_GROUP1;
27410 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27411 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27412 	    SD_PATH_STANDARD);
27413 	kmem_free(com, sizeof (*com));
27414 	return (rval);
27415 }
27416 
27417 
27418 /*
27419  *    Function: sr_read_all_subcodes()
27420  *
27421  * Description: This routine is the driver entry point for handling CD-ROM
27422  *		ioctl requests to return raw subcode data while the target is
27423  *		playing audio (CDROMSUBCODE).
27424  *
27425  *   Arguments: dev	- the device 'dev_t'
27426  *		data	- pointer to user provided cdrom subcode structure,
27427  *		          specifying the transfer length and address.
27428  *		flag	- this argument is a pass through to ddi_copyxxx()
27429  *		          directly from the mode argument of ioctl().
27430  *
27431  * Return Code: the code returned by sd_send_scsi_cmd()
27432  *		EFAULT if ddi_copyxxx() fails
27433  *		ENXIO if fail ddi_get_soft_state
27434  *		EINVAL if data pointer is NULL
27435  */
27436 
27437 static int
27438 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27439 {
27440 	struct sd_lun		*un = NULL;
27441 	struct uscsi_cmd	*com = NULL;
27442 	struct cdrom_subcode	*subcode = NULL;
27443 	int			rval;
27444 	size_t			buflen;
27445 	char			cdb[CDB_GROUP5];
27446 
27447 #ifdef _MULTI_DATAMODEL
27448 	/* To support ILP32 applications in an LP64 world */
27449 	struct cdrom_subcode32		cdrom_subcode32;
27450 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27451 #endif
27452 	if (data == NULL) {
27453 		return (EINVAL);
27454 	}
27455 
27456 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27457 		return (ENXIO);
27458 	}
27459 
27460 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27461 
27462 #ifdef _MULTI_DATAMODEL
27463 	switch (ddi_model_convert_from(flag & FMODELS)) {
27464 	case DDI_MODEL_ILP32:
27465 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27466 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27467 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27468 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27469 			return (EFAULT);
27470 		}
27471 		/* Convert the ILP32 uscsi data from the application to LP64 */
27472 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27473 		break;
27474 	case DDI_MODEL_NONE:
27475 		if (ddi_copyin(data, subcode,
27476 		    sizeof (struct cdrom_subcode), flag)) {
27477 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27478 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27479 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27480 			return (EFAULT);
27481 		}
27482 		break;
27483 	}
27484 #else /* ! _MULTI_DATAMODEL */
27485 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27486 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27487 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27488 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27489 		return (EFAULT);
27490 	}
27491 #endif /* _MULTI_DATAMODEL */
27492 
27493 	/*
27494 	 * Since MMC-2 expects max 3 bytes for length, check if the
27495 	 * length input is greater than 3 bytes
27496 	 */
27497 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27498 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27499 		    "sr_read_all_subcodes: "
27500 		    "cdrom transfer length too large: %d (limit %d)\n",
27501 		    subcode->cdsc_length, 0xFFFFFF);
27502 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27503 		return (EINVAL);
27504 	}
27505 
27506 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27507 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27508 	bzero(cdb, CDB_GROUP5);
27509 
27510 	if (un->un_f_mmc_cap == TRUE) {
27511 		cdb[0] = (char)SCMD_READ_CD;
27512 		cdb[2] = (char)0xff;
27513 		cdb[3] = (char)0xff;
27514 		cdb[4] = (char)0xff;
27515 		cdb[5] = (char)0xff;
27516 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27517 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27518 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27519 		cdb[10] = 1;
27520 	} else {
27521 		/*
27522 		 * Note: A vendor specific command (0xDF) is being used her to
27523 		 * request a read of all subcodes.
27524 		 */
27525 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27526 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27527 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27528 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27529 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27530 	}
27531 	com->uscsi_cdb	   = cdb;
27532 	com->uscsi_cdblen  = CDB_GROUP5;
27533 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27534 	com->uscsi_buflen  = buflen;
27535 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27536 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27537 	    SD_PATH_STANDARD);
27538 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27539 	kmem_free(com, sizeof (*com));
27540 	return (rval);
27541 }
27542 
27543 
27544 /*
27545  *    Function: sr_read_subchannel()
27546  *
27547  * Description: This routine is the driver entry point for handling CD-ROM
27548  *		ioctl requests to return the Q sub-channel data of the CD
27549  *		current position block. (CDROMSUBCHNL) The data includes the
27550  *		track number, index number, absolute CD-ROM address (LBA or MSF
27551  *		format per the user) , track relative CD-ROM address (LBA or MSF
27552  *		format per the user), control data and audio status.
27553  *
27554  *   Arguments: dev	- the device 'dev_t'
27555  *		data	- pointer to user provided cdrom sub-channel structure
27556  *		flag	- this argument is a pass through to ddi_copyxxx()
27557  *		          directly from the mode argument of ioctl().
27558  *
27559  * Return Code: the code returned by sd_send_scsi_cmd()
27560  *		EFAULT if ddi_copyxxx() fails
27561  *		ENXIO if fail ddi_get_soft_state
27562  *		EINVAL if data pointer is NULL
27563  */
27564 
27565 static int
27566 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27567 {
27568 	struct sd_lun		*un;
27569 	struct uscsi_cmd	*com;
27570 	struct cdrom_subchnl	subchanel;
27571 	struct cdrom_subchnl	*subchnl = &subchanel;
27572 	char			cdb[CDB_GROUP1];
27573 	caddr_t			buffer;
27574 	int			rval;
27575 
27576 	if (data == NULL) {
27577 		return (EINVAL);
27578 	}
27579 
27580 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27581 	    (un->un_state == SD_STATE_OFFLINE)) {
27582 		return (ENXIO);
27583 	}
27584 
27585 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27586 		return (EFAULT);
27587 	}
27588 
27589 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27590 	bzero(cdb, CDB_GROUP1);
27591 	cdb[0] = SCMD_READ_SUBCHANNEL;
27592 	/* Set the MSF bit based on the user requested address format */
27593 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27594 	/*
27595 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27596 	 * returned
27597 	 */
27598 	cdb[2] = 0x40;
27599 	/*
27600 	 * Set byte 3 to specify the return data format. A value of 0x01
27601 	 * indicates that the CD-ROM current position should be returned.
27602 	 */
27603 	cdb[3] = 0x01;
27604 	cdb[8] = 0x10;
27605 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27606 	com->uscsi_cdb	   = cdb;
27607 	com->uscsi_cdblen  = CDB_GROUP1;
27608 	com->uscsi_bufaddr = buffer;
27609 	com->uscsi_buflen  = 16;
27610 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27611 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27612 	    SD_PATH_STANDARD);
27613 	if (rval != 0) {
27614 		kmem_free(buffer, 16);
27615 		kmem_free(com, sizeof (*com));
27616 		return (rval);
27617 	}
27618 
27619 	/* Process the returned Q sub-channel data */
27620 	subchnl->cdsc_audiostatus = buffer[1];
27621 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
27622 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27623 	subchnl->cdsc_trk	= buffer[6];
27624 	subchnl->cdsc_ind	= buffer[7];
27625 	if (subchnl->cdsc_format & CDROM_LBA) {
27626 		subchnl->cdsc_absaddr.lba =
27627 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27628 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27629 		subchnl->cdsc_reladdr.lba =
27630 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27631 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27632 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27633 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27634 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27635 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27636 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27637 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27638 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27639 	} else {
27640 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27641 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27642 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27643 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27644 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27645 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27646 	}
27647 	kmem_free(buffer, 16);
27648 	kmem_free(com, sizeof (*com));
27649 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27650 	    != 0) {
27651 		return (EFAULT);
27652 	}
27653 	return (rval);
27654 }
27655 
27656 
27657 /*
27658  *    Function: sr_read_tocentry()
27659  *
27660  * Description: This routine is the driver entry point for handling CD-ROM
27661  *		ioctl requests to read from the Table of Contents (TOC)
27662  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27663  *		fields, the starting address (LBA or MSF format per the user)
27664  *		and the data mode if the user specified track is a data track.
27665  *
27666  *		Note: The READ HEADER (0x44) command used in this routine is
27667  *		obsolete per the SCSI MMC spec but still supported in the
27668  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27669  *		therefore the command is still implemented in this routine.
27670  *
27671  *   Arguments: dev	- the device 'dev_t'
27672  *		data	- pointer to user provided toc entry structure,
27673  *			  specifying the track # and the address format
27674  *			  (LBA or MSF).
27675  *		flag	- this argument is a pass through to ddi_copyxxx()
27676  *		          directly from the mode argument of ioctl().
27677  *
27678  * Return Code: the code returned by sd_send_scsi_cmd()
27679  *		EFAULT if ddi_copyxxx() fails
27680  *		ENXIO if fail ddi_get_soft_state
27681  *		EINVAL if data pointer is NULL
27682  */
27683 
27684 static int
27685 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27686 {
27687 	struct sd_lun		*un = NULL;
27688 	struct uscsi_cmd	*com;
27689 	struct cdrom_tocentry	toc_entry;
27690 	struct cdrom_tocentry	*entry = &toc_entry;
27691 	caddr_t			buffer;
27692 	int			rval;
27693 	char			cdb[CDB_GROUP1];
27694 
27695 	if (data == NULL) {
27696 		return (EINVAL);
27697 	}
27698 
27699 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27700 	    (un->un_state == SD_STATE_OFFLINE)) {
27701 		return (ENXIO);
27702 	}
27703 
27704 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27705 		return (EFAULT);
27706 	}
27707 
27708 	/* Validate the requested track and address format */
27709 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27710 		return (EINVAL);
27711 	}
27712 
27713 	if (entry->cdte_track == 0) {
27714 		return (EINVAL);
27715 	}
27716 
27717 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27718 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27719 	bzero(cdb, CDB_GROUP1);
27720 
27721 	cdb[0] = SCMD_READ_TOC;
27722 	/* Set the MSF bit based on the user requested address format  */
27723 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27724 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27725 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27726 	} else {
27727 		cdb[6] = entry->cdte_track;
27728 	}
27729 
27730 	/*
27731 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27732 	 * (4 byte TOC response header + 8 byte track descriptor)
27733 	 */
27734 	cdb[8] = 12;
27735 	com->uscsi_cdb	   = cdb;
27736 	com->uscsi_cdblen  = CDB_GROUP1;
27737 	com->uscsi_bufaddr = buffer;
27738 	com->uscsi_buflen  = 0x0C;
27739 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27740 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27741 	    SD_PATH_STANDARD);
27742 	if (rval != 0) {
27743 		kmem_free(buffer, 12);
27744 		kmem_free(com, sizeof (*com));
27745 		return (rval);
27746 	}
27747 
27748 	/* Process the toc entry */
27749 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27750 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27751 	if (entry->cdte_format & CDROM_LBA) {
27752 		entry->cdte_addr.lba =
27753 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27754 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27755 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27756 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27757 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27758 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27759 		/*
27760 		 * Send a READ TOC command using the LBA address format to get
27761 		 * the LBA for the track requested so it can be used in the
27762 		 * READ HEADER request
27763 		 *
27764 		 * Note: The MSF bit of the READ HEADER command specifies the
27765 		 * output format. The block address specified in that command
27766 		 * must be in LBA format.
27767 		 */
27768 		cdb[1] = 0;
27769 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27770 		    SD_PATH_STANDARD);
27771 		if (rval != 0) {
27772 			kmem_free(buffer, 12);
27773 			kmem_free(com, sizeof (*com));
27774 			return (rval);
27775 		}
27776 	} else {
27777 		entry->cdte_addr.msf.minute	= buffer[9];
27778 		entry->cdte_addr.msf.second	= buffer[10];
27779 		entry->cdte_addr.msf.frame	= buffer[11];
27780 		/*
27781 		 * Send a READ TOC command using the LBA address format to get
27782 		 * the LBA for the track requested so it can be used in the
27783 		 * READ HEADER request
27784 		 *
27785 		 * Note: The MSF bit of the READ HEADER command specifies the
27786 		 * output format. The block address specified in that command
27787 		 * must be in LBA format.
27788 		 */
27789 		cdb[1] = 0;
27790 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27791 		    SD_PATH_STANDARD);
27792 		if (rval != 0) {
27793 			kmem_free(buffer, 12);
27794 			kmem_free(com, sizeof (*com));
27795 			return (rval);
27796 		}
27797 	}
27798 
27799 	/*
27800 	 * Build and send the READ HEADER command to determine the data mode of
27801 	 * the user specified track.
27802 	 */
27803 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27804 	    (entry->cdte_track != CDROM_LEADOUT)) {
27805 		bzero(cdb, CDB_GROUP1);
27806 		cdb[0] = SCMD_READ_HEADER;
27807 		cdb[2] = buffer[8];
27808 		cdb[3] = buffer[9];
27809 		cdb[4] = buffer[10];
27810 		cdb[5] = buffer[11];
27811 		cdb[8] = 0x08;
27812 		com->uscsi_buflen = 0x08;
27813 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27814 		    SD_PATH_STANDARD);
27815 		if (rval == 0) {
27816 			entry->cdte_datamode = buffer[0];
27817 		} else {
27818 			/*
27819 			 * READ HEADER command failed, since this is
27820 			 * obsoleted in one spec, its better to return
27821 			 * -1 for an invlid track so that we can still
27822 			 * receive the rest of the TOC data.
27823 			 */
27824 			entry->cdte_datamode = (uchar_t)-1;
27825 		}
27826 	} else {
27827 		entry->cdte_datamode = (uchar_t)-1;
27828 	}
27829 
27830 	kmem_free(buffer, 12);
27831 	kmem_free(com, sizeof (*com));
27832 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27833 		return (EFAULT);
27834 
27835 	return (rval);
27836 }
27837 
27838 
27839 /*
27840  *    Function: sr_read_tochdr()
27841  *
27842  * Description: This routine is the driver entry point for handling CD-ROM
27843  * 		ioctl requests to read the Table of Contents (TOC) header
27844  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
27845  *		and ending track numbers
27846  *
27847  *   Arguments: dev	- the device 'dev_t'
27848  *		data	- pointer to user provided toc header structure,
27849  *			  specifying the starting and ending track numbers.
27850  *		flag	- this argument is a pass through to ddi_copyxxx()
27851  *			  directly from the mode argument of ioctl().
27852  *
27853  * Return Code: the code returned by sd_send_scsi_cmd()
27854  *		EFAULT if ddi_copyxxx() fails
27855  *		ENXIO if fail ddi_get_soft_state
27856  *		EINVAL if data pointer is NULL
27857  */
27858 
27859 static int
27860 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27861 {
27862 	struct sd_lun		*un;
27863 	struct uscsi_cmd	*com;
27864 	struct cdrom_tochdr	toc_header;
27865 	struct cdrom_tochdr	*hdr = &toc_header;
27866 	char			cdb[CDB_GROUP1];
27867 	int			rval;
27868 	caddr_t			buffer;
27869 
27870 	if (data == NULL) {
27871 		return (EINVAL);
27872 	}
27873 
27874 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27875 	    (un->un_state == SD_STATE_OFFLINE)) {
27876 		return (ENXIO);
27877 	}
27878 
27879 	buffer = kmem_zalloc(4, KM_SLEEP);
27880 	bzero(cdb, CDB_GROUP1);
27881 	cdb[0] = SCMD_READ_TOC;
27882 	/*
27883 	 * Specifying a track number of 0x00 in the READ TOC command indicates
27884 	 * that the TOC header should be returned
27885 	 */
27886 	cdb[6] = 0x00;
27887 	/*
27888 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27889 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27890 	 */
27891 	cdb[8] = 0x04;
27892 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27893 	com->uscsi_cdb	   = cdb;
27894 	com->uscsi_cdblen  = CDB_GROUP1;
27895 	com->uscsi_bufaddr = buffer;
27896 	com->uscsi_buflen  = 0x04;
27897 	com->uscsi_timeout = 300;
27898 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27899 
27900 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27901 	    SD_PATH_STANDARD);
27902 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27903 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
27904 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
27905 	} else {
27906 		hdr->cdth_trk0 = buffer[2];
27907 		hdr->cdth_trk1 = buffer[3];
27908 	}
27909 	kmem_free(buffer, 4);
27910 	kmem_free(com, sizeof (*com));
27911 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
27912 		return (EFAULT);
27913 	}
27914 	return (rval);
27915 }
27916 
27917 
27918 /*
27919  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
27920  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
27921  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
27922  * digital audio and extended architecture digital audio. These modes are
27923  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
27924  * MMC specs.
27925  *
27926  * In addition to support for the various data formats these routines also
27927  * include support for devices that implement only the direct access READ
27928  * commands (0x08, 0x28), devices that implement the READ_CD commands
27929  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
27930  * READ CDXA commands (0xD8, 0xDB)
27931  */
27932 
27933 /*
27934  *    Function: sr_read_mode1()
27935  *
27936  * Description: This routine is the driver entry point for handling CD-ROM
27937  *		ioctl read mode1 requests (CDROMREADMODE1).
27938  *
27939  *   Arguments: dev	- the device 'dev_t'
27940  *		data	- pointer to user provided cd read structure specifying
27941  *			  the lba buffer address and length.
27942  *		flag	- this argument is a pass through to ddi_copyxxx()
27943  *			  directly from the mode argument of ioctl().
27944  *
27945  * Return Code: the code returned by sd_send_scsi_cmd()
27946  *		EFAULT if ddi_copyxxx() fails
27947  *		ENXIO if fail ddi_get_soft_state
27948  *		EINVAL if data pointer is NULL
27949  */
27950 
27951 static int
27952 sr_read_mode1(dev_t dev, caddr_t data, int flag)
27953 {
27954 	struct sd_lun		*un;
27955 	struct cdrom_read	mode1_struct;
27956 	struct cdrom_read	*mode1 = &mode1_struct;
27957 	int			rval;
27958 	sd_ssc_t		*ssc;
27959 
27960 #ifdef _MULTI_DATAMODEL
27961 	/* To support ILP32 applications in an LP64 world */
27962 	struct cdrom_read32	cdrom_read32;
27963 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
27964 #endif /* _MULTI_DATAMODEL */
27965 
27966 	if (data == NULL) {
27967 		return (EINVAL);
27968 	}
27969 
27970 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27971 	    (un->un_state == SD_STATE_OFFLINE)) {
27972 		return (ENXIO);
27973 	}
27974 
27975 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27976 	    "sd_read_mode1: entry: un:0x%p\n", un);
27977 
27978 #ifdef _MULTI_DATAMODEL
27979 	switch (ddi_model_convert_from(flag & FMODELS)) {
27980 	case DDI_MODEL_ILP32:
27981 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27982 			return (EFAULT);
27983 		}
27984 		/* Convert the ILP32 uscsi data from the application to LP64 */
27985 		cdrom_read32tocdrom_read(cdrd32, mode1);
27986 		break;
27987 	case DDI_MODEL_NONE:
27988 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27989 			return (EFAULT);
27990 		}
27991 	}
27992 #else /* ! _MULTI_DATAMODEL */
27993 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27994 		return (EFAULT);
27995 	}
27996 #endif /* _MULTI_DATAMODEL */
27997 
27998 	ssc = sd_ssc_init(un);
27999 	rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28000 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28001 	sd_ssc_fini(ssc);
28002 
28003 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28004 	    "sd_read_mode1: exit: un:0x%p\n", un);
28005 
28006 	return (rval);
28007 }
28008 
28009 
28010 /*
28011  *    Function: sr_read_cd_mode2()
28012  *
28013  * Description: This routine is the driver entry point for handling CD-ROM
28014  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28015  *		support the READ CD (0xBE) command or the 1st generation
28016  *		READ CD (0xD4) command.
28017  *
28018  *   Arguments: dev	- the device 'dev_t'
28019  *		data	- pointer to user provided cd read structure specifying
28020  *			  the lba buffer address and length.
28021  *		flag	- this argument is a pass through to ddi_copyxxx()
28022  *			  directly from the mode argument of ioctl().
28023  *
28024  * Return Code: the code returned by sd_send_scsi_cmd()
28025  *		EFAULT if ddi_copyxxx() fails
28026  *		ENXIO if fail ddi_get_soft_state
28027  *		EINVAL if data pointer is NULL
28028  */
28029 
28030 static int
28031 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28032 {
28033 	struct sd_lun		*un;
28034 	struct uscsi_cmd	*com;
28035 	struct cdrom_read	mode2_struct;
28036 	struct cdrom_read	*mode2 = &mode2_struct;
28037 	uchar_t			cdb[CDB_GROUP5];
28038 	int			nblocks;
28039 	int			rval;
28040 #ifdef _MULTI_DATAMODEL
28041 	/*  To support ILP32 applications in an LP64 world */
28042 	struct cdrom_read32	cdrom_read32;
28043 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28044 #endif /* _MULTI_DATAMODEL */
28045 
28046 	if (data == NULL) {
28047 		return (EINVAL);
28048 	}
28049 
28050 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28051 	    (un->un_state == SD_STATE_OFFLINE)) {
28052 		return (ENXIO);
28053 	}
28054 
28055 #ifdef _MULTI_DATAMODEL
28056 	switch (ddi_model_convert_from(flag & FMODELS)) {
28057 	case DDI_MODEL_ILP32:
28058 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28059 			return (EFAULT);
28060 		}
28061 		/* Convert the ILP32 uscsi data from the application to LP64 */
28062 		cdrom_read32tocdrom_read(cdrd32, mode2);
28063 		break;
28064 	case DDI_MODEL_NONE:
28065 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28066 			return (EFAULT);
28067 		}
28068 		break;
28069 	}
28070 
28071 #else /* ! _MULTI_DATAMODEL */
28072 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28073 		return (EFAULT);
28074 	}
28075 #endif /* _MULTI_DATAMODEL */
28076 
28077 	bzero(cdb, sizeof (cdb));
28078 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28079 		/* Read command supported by 1st generation atapi drives */
28080 		cdb[0] = SCMD_READ_CDD4;
28081 	} else {
28082 		/* Universal CD Access Command */
28083 		cdb[0] = SCMD_READ_CD;
28084 	}
28085 
28086 	/*
28087 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28088 	 */
28089 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28090 
28091 	/* set the start address */
28092 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28093 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28094 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28095 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28096 
28097 	/* set the transfer length */
28098 	nblocks = mode2->cdread_buflen / 2336;
28099 	cdb[6] = (uchar_t)(nblocks >> 16);
28100 	cdb[7] = (uchar_t)(nblocks >> 8);
28101 	cdb[8] = (uchar_t)nblocks;
28102 
28103 	/* set the filter bits */
28104 	cdb[9] = CDROM_READ_CD_USERDATA;
28105 
28106 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28107 	com->uscsi_cdb = (caddr_t)cdb;
28108 	com->uscsi_cdblen = sizeof (cdb);
28109 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28110 	com->uscsi_buflen = mode2->cdread_buflen;
28111 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28112 
28113 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28114 	    SD_PATH_STANDARD);
28115 	kmem_free(com, sizeof (*com));
28116 	return (rval);
28117 }
28118 
28119 
28120 /*
28121  *    Function: sr_read_mode2()
28122  *
28123  * Description: This routine is the driver entry point for handling CD-ROM
28124  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28125  *		do not support the READ CD (0xBE) command.
28126  *
28127  *   Arguments: dev	- the device 'dev_t'
28128  *		data	- pointer to user provided cd read structure specifying
28129  *			  the lba buffer address and length.
28130  *		flag	- this argument is a pass through to ddi_copyxxx()
28131  *			  directly from the mode argument of ioctl().
28132  *
28133  * Return Code: the code returned by sd_send_scsi_cmd()
28134  *		EFAULT if ddi_copyxxx() fails
28135  *		ENXIO if fail ddi_get_soft_state
28136  *		EINVAL if data pointer is NULL
28137  *		EIO if fail to reset block size
28138  *		EAGAIN if commands are in progress in the driver
28139  */
28140 
28141 static int
28142 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28143 {
28144 	struct sd_lun		*un;
28145 	struct cdrom_read	mode2_struct;
28146 	struct cdrom_read	*mode2 = &mode2_struct;
28147 	int			rval;
28148 	uint32_t		restore_blksize;
28149 	struct uscsi_cmd	*com;
28150 	uchar_t			cdb[CDB_GROUP0];
28151 	int			nblocks;
28152 
28153 #ifdef _MULTI_DATAMODEL
28154 	/* To support ILP32 applications in an LP64 world */
28155 	struct cdrom_read32	cdrom_read32;
28156 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28157 #endif /* _MULTI_DATAMODEL */
28158 
28159 	if (data == NULL) {
28160 		return (EINVAL);
28161 	}
28162 
28163 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28164 	    (un->un_state == SD_STATE_OFFLINE)) {
28165 		return (ENXIO);
28166 	}
28167 
28168 	/*
28169 	 * Because this routine will update the device and driver block size
28170 	 * being used we want to make sure there are no commands in progress.
28171 	 * If commands are in progress the user will have to try again.
28172 	 *
28173 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28174 	 * in sdioctl to protect commands from sdioctl through to the top of
28175 	 * sd_uscsi_strategy. See sdioctl for details.
28176 	 */
28177 	mutex_enter(SD_MUTEX(un));
28178 	if (un->un_ncmds_in_driver != 1) {
28179 		mutex_exit(SD_MUTEX(un));
28180 		return (EAGAIN);
28181 	}
28182 	mutex_exit(SD_MUTEX(un));
28183 
28184 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28185 	    "sd_read_mode2: entry: un:0x%p\n", un);
28186 
28187 #ifdef _MULTI_DATAMODEL
28188 	switch (ddi_model_convert_from(flag & FMODELS)) {
28189 	case DDI_MODEL_ILP32:
28190 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28191 			return (EFAULT);
28192 		}
28193 		/* Convert the ILP32 uscsi data from the application to LP64 */
28194 		cdrom_read32tocdrom_read(cdrd32, mode2);
28195 		break;
28196 	case DDI_MODEL_NONE:
28197 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28198 			return (EFAULT);
28199 		}
28200 		break;
28201 	}
28202 #else /* ! _MULTI_DATAMODEL */
28203 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28204 		return (EFAULT);
28205 	}
28206 #endif /* _MULTI_DATAMODEL */
28207 
28208 	/* Store the current target block size for restoration later */
28209 	restore_blksize = un->un_tgt_blocksize;
28210 
28211 	/* Change the device and soft state target block size to 2336 */
28212 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28213 		rval = EIO;
28214 		goto done;
28215 	}
28216 
28217 
28218 	bzero(cdb, sizeof (cdb));
28219 
28220 	/* set READ operation */
28221 	cdb[0] = SCMD_READ;
28222 
28223 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28224 	mode2->cdread_lba >>= 2;
28225 
28226 	/* set the start address */
28227 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28228 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28229 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28230 
28231 	/* set the transfer length */
28232 	nblocks = mode2->cdread_buflen / 2336;
28233 	cdb[4] = (uchar_t)nblocks & 0xFF;
28234 
28235 	/* build command */
28236 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28237 	com->uscsi_cdb = (caddr_t)cdb;
28238 	com->uscsi_cdblen = sizeof (cdb);
28239 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28240 	com->uscsi_buflen = mode2->cdread_buflen;
28241 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28242 
28243 	/*
28244 	 * Issue SCSI command with user space address for read buffer.
28245 	 *
28246 	 * This sends the command through main channel in the driver.
28247 	 *
28248 	 * Since this is accessed via an IOCTL call, we go through the
28249 	 * standard path, so that if the device was powered down, then
28250 	 * it would be 'awakened' to handle the command.
28251 	 */
28252 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28253 	    SD_PATH_STANDARD);
28254 
28255 	kmem_free(com, sizeof (*com));
28256 
28257 	/* Restore the device and soft state target block size */
28258 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28259 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28260 		    "can't do switch back to mode 1\n");
28261 		/*
28262 		 * If sd_send_scsi_READ succeeded we still need to report
28263 		 * an error because we failed to reset the block size
28264 		 */
28265 		if (rval == 0) {
28266 			rval = EIO;
28267 		}
28268 	}
28269 
28270 done:
28271 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28272 	    "sd_read_mode2: exit: un:0x%p\n", un);
28273 
28274 	return (rval);
28275 }
28276 
28277 
28278 /*
28279  *    Function: sr_sector_mode()
28280  *
28281  * Description: This utility function is used by sr_read_mode2 to set the target
28282  *		block size based on the user specified size. This is a legacy
28283  *		implementation based upon a vendor specific mode page
28284  *
28285  *   Arguments: dev	- the device 'dev_t'
28286  *		data	- flag indicating if block size is being set to 2336 or
28287  *			  512.
28288  *
28289  * Return Code: the code returned by sd_send_scsi_cmd()
28290  *		EFAULT if ddi_copyxxx() fails
28291  *		ENXIO if fail ddi_get_soft_state
28292  *		EINVAL if data pointer is NULL
28293  */
28294 
28295 static int
28296 sr_sector_mode(dev_t dev, uint32_t blksize)
28297 {
28298 	struct sd_lun	*un;
28299 	uchar_t		*sense;
28300 	uchar_t		*select;
28301 	int		rval;
28302 	sd_ssc_t	*ssc;
28303 
28304 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28305 	    (un->un_state == SD_STATE_OFFLINE)) {
28306 		return (ENXIO);
28307 	}
28308 
28309 	sense = kmem_zalloc(20, KM_SLEEP);
28310 
28311 	/* Note: This is a vendor specific mode page (0x81) */
28312 	ssc = sd_ssc_init(un);
28313 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28314 	    SD_PATH_STANDARD);
28315 	sd_ssc_fini(ssc);
28316 	if (rval != 0) {
28317 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28318 		    "sr_sector_mode: Mode Sense failed\n");
28319 		kmem_free(sense, 20);
28320 		return (rval);
28321 	}
28322 	select = kmem_zalloc(20, KM_SLEEP);
28323 	select[3] = 0x08;
28324 	select[10] = ((blksize >> 8) & 0xff);
28325 	select[11] = (blksize & 0xff);
28326 	select[12] = 0x01;
28327 	select[13] = 0x06;
28328 	select[14] = sense[14];
28329 	select[15] = sense[15];
28330 	if (blksize == SD_MODE2_BLKSIZE) {
28331 		select[14] |= 0x01;
28332 	}
28333 
28334 	ssc = sd_ssc_init(un);
28335 	rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28336 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28337 	sd_ssc_fini(ssc);
28338 	if (rval != 0) {
28339 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28340 		    "sr_sector_mode: Mode Select failed\n");
28341 	} else {
28342 		/*
28343 		 * Only update the softstate block size if we successfully
28344 		 * changed the device block mode.
28345 		 */
28346 		mutex_enter(SD_MUTEX(un));
28347 		sd_update_block_info(un, blksize, 0);
28348 		mutex_exit(SD_MUTEX(un));
28349 	}
28350 	kmem_free(sense, 20);
28351 	kmem_free(select, 20);
28352 	return (rval);
28353 }
28354 
28355 
28356 /*
28357  *    Function: sr_read_cdda()
28358  *
28359  * Description: This routine is the driver entry point for handling CD-ROM
28360  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28361  *		the target supports CDDA these requests are handled via a vendor
28362  *		specific command (0xD8) If the target does not support CDDA
28363  *		these requests are handled via the READ CD command (0xBE).
28364  *
28365  *   Arguments: dev	- the device 'dev_t'
28366  *		data	- pointer to user provided CD-DA structure specifying
28367  *			  the track starting address, transfer length, and
28368  *			  subcode options.
28369  *		flag	- this argument is a pass through to ddi_copyxxx()
28370  *			  directly from the mode argument of ioctl().
28371  *
28372  * Return Code: the code returned by sd_send_scsi_cmd()
28373  *		EFAULT if ddi_copyxxx() fails
28374  *		ENXIO if fail ddi_get_soft_state
28375  *		EINVAL if invalid arguments are provided
28376  *		ENOTTY
28377  */
28378 
28379 static int
28380 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28381 {
28382 	struct sd_lun			*un;
28383 	struct uscsi_cmd		*com;
28384 	struct cdrom_cdda		*cdda;
28385 	int				rval;
28386 	size_t				buflen;
28387 	char				cdb[CDB_GROUP5];
28388 
28389 #ifdef _MULTI_DATAMODEL
28390 	/* To support ILP32 applications in an LP64 world */
28391 	struct cdrom_cdda32	cdrom_cdda32;
28392 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28393 #endif /* _MULTI_DATAMODEL */
28394 
28395 	if (data == NULL) {
28396 		return (EINVAL);
28397 	}
28398 
28399 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28400 		return (ENXIO);
28401 	}
28402 
28403 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28404 
28405 #ifdef _MULTI_DATAMODEL
28406 	switch (ddi_model_convert_from(flag & FMODELS)) {
28407 	case DDI_MODEL_ILP32:
28408 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28409 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28410 			    "sr_read_cdda: ddi_copyin Failed\n");
28411 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28412 			return (EFAULT);
28413 		}
28414 		/* Convert the ILP32 uscsi data from the application to LP64 */
28415 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28416 		break;
28417 	case DDI_MODEL_NONE:
28418 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28419 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28420 			    "sr_read_cdda: ddi_copyin Failed\n");
28421 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28422 			return (EFAULT);
28423 		}
28424 		break;
28425 	}
28426 #else /* ! _MULTI_DATAMODEL */
28427 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28428 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28429 		    "sr_read_cdda: ddi_copyin Failed\n");
28430 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28431 		return (EFAULT);
28432 	}
28433 #endif /* _MULTI_DATAMODEL */
28434 
28435 	/*
28436 	 * Since MMC-2 expects max 3 bytes for length, check if the
28437 	 * length input is greater than 3 bytes
28438 	 */
28439 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28440 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28441 		    "cdrom transfer length too large: %d (limit %d)\n",
28442 		    cdda->cdda_length, 0xFFFFFF);
28443 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28444 		return (EINVAL);
28445 	}
28446 
28447 	switch (cdda->cdda_subcode) {
28448 	case CDROM_DA_NO_SUBCODE:
28449 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28450 		break;
28451 	case CDROM_DA_SUBQ:
28452 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28453 		break;
28454 	case CDROM_DA_ALL_SUBCODE:
28455 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28456 		break;
28457 	case CDROM_DA_SUBCODE_ONLY:
28458 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28459 		break;
28460 	default:
28461 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28462 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28463 		    cdda->cdda_subcode);
28464 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28465 		return (EINVAL);
28466 	}
28467 
28468 	/* Build and send the command */
28469 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28470 	bzero(cdb, CDB_GROUP5);
28471 
28472 	if (un->un_f_cfg_cdda == TRUE) {
28473 		cdb[0] = (char)SCMD_READ_CD;
28474 		cdb[1] = 0x04;
28475 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28476 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28477 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28478 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28479 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28480 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28481 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28482 		cdb[9] = 0x10;
28483 		switch (cdda->cdda_subcode) {
28484 		case CDROM_DA_NO_SUBCODE :
28485 			cdb[10] = 0x0;
28486 			break;
28487 		case CDROM_DA_SUBQ :
28488 			cdb[10] = 0x2;
28489 			break;
28490 		case CDROM_DA_ALL_SUBCODE :
28491 			cdb[10] = 0x1;
28492 			break;
28493 		case CDROM_DA_SUBCODE_ONLY :
28494 			/* FALLTHROUGH */
28495 		default :
28496 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28497 			kmem_free(com, sizeof (*com));
28498 			return (ENOTTY);
28499 		}
28500 	} else {
28501 		cdb[0] = (char)SCMD_READ_CDDA;
28502 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28503 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28504 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28505 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28506 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28507 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28508 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28509 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28510 		cdb[10] = cdda->cdda_subcode;
28511 	}
28512 
28513 	com->uscsi_cdb = cdb;
28514 	com->uscsi_cdblen = CDB_GROUP5;
28515 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28516 	com->uscsi_buflen = buflen;
28517 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28518 
28519 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28520 	    SD_PATH_STANDARD);
28521 
28522 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28523 	kmem_free(com, sizeof (*com));
28524 	return (rval);
28525 }
28526 
28527 
28528 /*
28529  *    Function: sr_read_cdxa()
28530  *
28531  * Description: This routine is the driver entry point for handling CD-ROM
28532  *		ioctl requests to return CD-XA (Extended Architecture) data.
28533  *		(CDROMCDXA).
28534  *
28535  *   Arguments: dev	- the device 'dev_t'
28536  *		data	- pointer to user provided CD-XA structure specifying
28537  *			  the data starting address, transfer length, and format
28538  *		flag	- this argument is a pass through to ddi_copyxxx()
28539  *			  directly from the mode argument of ioctl().
28540  *
28541  * Return Code: the code returned by sd_send_scsi_cmd()
28542  *		EFAULT if ddi_copyxxx() fails
28543  *		ENXIO if fail ddi_get_soft_state
28544  *		EINVAL if data pointer is NULL
28545  */
28546 
28547 static int
28548 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28549 {
28550 	struct sd_lun		*un;
28551 	struct uscsi_cmd	*com;
28552 	struct cdrom_cdxa	*cdxa;
28553 	int			rval;
28554 	size_t			buflen;
28555 	char			cdb[CDB_GROUP5];
28556 	uchar_t			read_flags;
28557 
28558 #ifdef _MULTI_DATAMODEL
28559 	/* To support ILP32 applications in an LP64 world */
28560 	struct cdrom_cdxa32		cdrom_cdxa32;
28561 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28562 #endif /* _MULTI_DATAMODEL */
28563 
28564 	if (data == NULL) {
28565 		return (EINVAL);
28566 	}
28567 
28568 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28569 		return (ENXIO);
28570 	}
28571 
28572 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28573 
28574 #ifdef _MULTI_DATAMODEL
28575 	switch (ddi_model_convert_from(flag & FMODELS)) {
28576 	case DDI_MODEL_ILP32:
28577 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28578 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28579 			return (EFAULT);
28580 		}
28581 		/*
28582 		 * Convert the ILP32 uscsi data from the
28583 		 * application to LP64 for internal use.
28584 		 */
28585 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28586 		break;
28587 	case DDI_MODEL_NONE:
28588 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28589 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28590 			return (EFAULT);
28591 		}
28592 		break;
28593 	}
28594 #else /* ! _MULTI_DATAMODEL */
28595 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28596 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28597 		return (EFAULT);
28598 	}
28599 #endif /* _MULTI_DATAMODEL */
28600 
28601 	/*
28602 	 * Since MMC-2 expects max 3 bytes for length, check if the
28603 	 * length input is greater than 3 bytes
28604 	 */
28605 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28606 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28607 		    "cdrom transfer length too large: %d (limit %d)\n",
28608 		    cdxa->cdxa_length, 0xFFFFFF);
28609 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28610 		return (EINVAL);
28611 	}
28612 
28613 	switch (cdxa->cdxa_format) {
28614 	case CDROM_XA_DATA:
28615 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28616 		read_flags = 0x10;
28617 		break;
28618 	case CDROM_XA_SECTOR_DATA:
28619 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28620 		read_flags = 0xf8;
28621 		break;
28622 	case CDROM_XA_DATA_W_ERROR:
28623 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28624 		read_flags = 0xfc;
28625 		break;
28626 	default:
28627 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28628 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28629 		    cdxa->cdxa_format);
28630 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28631 		return (EINVAL);
28632 	}
28633 
28634 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28635 	bzero(cdb, CDB_GROUP5);
28636 	if (un->un_f_mmc_cap == TRUE) {
28637 		cdb[0] = (char)SCMD_READ_CD;
28638 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28639 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28640 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28641 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28642 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28643 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28644 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28645 		cdb[9] = (char)read_flags;
28646 	} else {
28647 		/*
28648 		 * Note: A vendor specific command (0xDB) is being used her to
28649 		 * request a read of all subcodes.
28650 		 */
28651 		cdb[0] = (char)SCMD_READ_CDXA;
28652 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28653 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28654 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28655 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28656 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28657 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28658 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28659 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28660 		cdb[10] = cdxa->cdxa_format;
28661 	}
28662 	com->uscsi_cdb	   = cdb;
28663 	com->uscsi_cdblen  = CDB_GROUP5;
28664 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28665 	com->uscsi_buflen  = buflen;
28666 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28667 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28668 	    SD_PATH_STANDARD);
28669 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28670 	kmem_free(com, sizeof (*com));
28671 	return (rval);
28672 }
28673 
28674 
28675 /*
28676  *    Function: sr_eject()
28677  *
28678  * Description: This routine is the driver entry point for handling CD-ROM
28679  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28680  *
28681  *   Arguments: dev	- the device 'dev_t'
28682  *
28683  * Return Code: the code returned by sd_send_scsi_cmd()
28684  */
28685 
28686 static int
28687 sr_eject(dev_t dev)
28688 {
28689 	struct sd_lun	*un;
28690 	int		rval;
28691 	sd_ssc_t	*ssc;
28692 
28693 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28694 	    (un->un_state == SD_STATE_OFFLINE)) {
28695 		return (ENXIO);
28696 	}
28697 
28698 	/*
28699 	 * To prevent race conditions with the eject
28700 	 * command, keep track of an eject command as
28701 	 * it progresses. If we are already handling
28702 	 * an eject command in the driver for the given
28703 	 * unit and another request to eject is received
28704 	 * immediately return EAGAIN so we don't lose
28705 	 * the command if the current eject command fails.
28706 	 */
28707 	mutex_enter(SD_MUTEX(un));
28708 	if (un->un_f_ejecting == TRUE) {
28709 		mutex_exit(SD_MUTEX(un));
28710 		return (EAGAIN);
28711 	}
28712 	un->un_f_ejecting = TRUE;
28713 	mutex_exit(SD_MUTEX(un));
28714 
28715 	ssc = sd_ssc_init(un);
28716 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28717 	    SD_PATH_STANDARD);
28718 	sd_ssc_fini(ssc);
28719 
28720 	if (rval != 0) {
28721 		mutex_enter(SD_MUTEX(un));
28722 		un->un_f_ejecting = FALSE;
28723 		mutex_exit(SD_MUTEX(un));
28724 		return (rval);
28725 	}
28726 
28727 	ssc = sd_ssc_init(un);
28728 	rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28729 	    SD_TARGET_EJECT, SD_PATH_STANDARD);
28730 	sd_ssc_fini(ssc);
28731 
28732 	if (rval == 0) {
28733 		mutex_enter(SD_MUTEX(un));
28734 		sr_ejected(un);
28735 		un->un_mediastate = DKIO_EJECTED;
28736 		un->un_f_ejecting = FALSE;
28737 		cv_broadcast(&un->un_state_cv);
28738 		mutex_exit(SD_MUTEX(un));
28739 	} else {
28740 		mutex_enter(SD_MUTEX(un));
28741 		un->un_f_ejecting = FALSE;
28742 		mutex_exit(SD_MUTEX(un));
28743 	}
28744 	return (rval);
28745 }
28746 
28747 
28748 /*
28749  *    Function: sr_ejected()
28750  *
28751  * Description: This routine updates the soft state structure to invalidate the
28752  *		geometry information after the media has been ejected or a
28753  *		media eject has been detected.
28754  *
28755  *   Arguments: un - driver soft state (unit) structure
28756  */
28757 
28758 static void
28759 sr_ejected(struct sd_lun *un)
28760 {
28761 	struct sd_errstats *stp;
28762 
28763 	ASSERT(un != NULL);
28764 	ASSERT(mutex_owned(SD_MUTEX(un)));
28765 
28766 	un->un_f_blockcount_is_valid	= FALSE;
28767 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28768 	mutex_exit(SD_MUTEX(un));
28769 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28770 	mutex_enter(SD_MUTEX(un));
28771 
28772 	if (un->un_errstats != NULL) {
28773 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28774 		stp->sd_capacity.value.ui64 = 0;
28775 	}
28776 }
28777 
28778 
28779 /*
28780  *    Function: sr_check_wp()
28781  *
28782  * Description: This routine checks the write protection of a removable
28783  *      media disk and hotpluggable devices via the write protect bit of
28784  *      the Mode Page Header device specific field. Some devices choke
28785  *      on unsupported mode page. In order to workaround this issue,
28786  *      this routine has been implemented to use 0x3f mode page(request
28787  *      for all pages) for all device types.
28788  *
28789  *   Arguments: dev             - the device 'dev_t'
28790  *
28791  * Return Code: int indicating if the device is write protected (1) or not (0)
28792  *
28793  *     Context: Kernel thread.
28794  *
28795  */
28796 
28797 static int
28798 sr_check_wp(dev_t dev)
28799 {
28800 	struct sd_lun	*un;
28801 	uchar_t		device_specific;
28802 	uchar_t		*sense;
28803 	int		hdrlen;
28804 	int		rval = FALSE;
28805 	int		status;
28806 	sd_ssc_t	*ssc;
28807 
28808 	/*
28809 	 * Note: The return codes for this routine should be reworked to
28810 	 * properly handle the case of a NULL softstate.
28811 	 */
28812 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28813 		return (FALSE);
28814 	}
28815 
28816 	if (un->un_f_cfg_is_atapi == TRUE) {
28817 		/*
28818 		 * The mode page contents are not required; set the allocation
28819 		 * length for the mode page header only
28820 		 */
28821 		hdrlen = MODE_HEADER_LENGTH_GRP2;
28822 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28823 		ssc = sd_ssc_init(un);
28824 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28825 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28826 		sd_ssc_fini(ssc);
28827 		if (status != 0)
28828 			goto err_exit;
28829 		device_specific =
28830 		    ((struct mode_header_grp2 *)sense)->device_specific;
28831 	} else {
28832 		hdrlen = MODE_HEADER_LENGTH;
28833 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28834 		ssc = sd_ssc_init(un);
28835 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28836 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28837 		sd_ssc_fini(ssc);
28838 		if (status != 0)
28839 			goto err_exit;
28840 		device_specific =
28841 		    ((struct mode_header *)sense)->device_specific;
28842 	}
28843 
28844 
28845 	/*
28846 	 * Write protect mode sense failed; not all disks
28847 	 * understand this query. Return FALSE assuming that
28848 	 * these devices are not writable.
28849 	 */
28850 	if (device_specific & WRITE_PROTECT) {
28851 		rval = TRUE;
28852 	}
28853 
28854 err_exit:
28855 	kmem_free(sense, hdrlen);
28856 	return (rval);
28857 }
28858 
28859 /*
28860  *    Function: sr_volume_ctrl()
28861  *
28862  * Description: This routine is the driver entry point for handling CD-ROM
28863  *		audio output volume ioctl requests. (CDROMVOLCTRL)
28864  *
28865  *   Arguments: dev	- the device 'dev_t'
28866  *		data	- pointer to user audio volume control structure
28867  *		flag	- this argument is a pass through to ddi_copyxxx()
28868  *			  directly from the mode argument of ioctl().
28869  *
28870  * Return Code: the code returned by sd_send_scsi_cmd()
28871  *		EFAULT if ddi_copyxxx() fails
28872  *		ENXIO if fail ddi_get_soft_state
28873  *		EINVAL if data pointer is NULL
28874  *
28875  */
28876 
28877 static int
28878 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28879 {
28880 	struct sd_lun		*un;
28881 	struct cdrom_volctrl    volume;
28882 	struct cdrom_volctrl    *vol = &volume;
28883 	uchar_t			*sense_page;
28884 	uchar_t			*select_page;
28885 	uchar_t			*sense;
28886 	uchar_t			*select;
28887 	int			sense_buflen;
28888 	int			select_buflen;
28889 	int			rval;
28890 	sd_ssc_t		*ssc;
28891 
28892 	if (data == NULL) {
28893 		return (EINVAL);
28894 	}
28895 
28896 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28897 	    (un->un_state == SD_STATE_OFFLINE)) {
28898 		return (ENXIO);
28899 	}
28900 
28901 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
28902 		return (EFAULT);
28903 	}
28904 
28905 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28906 		struct mode_header_grp2		*sense_mhp;
28907 		struct mode_header_grp2		*select_mhp;
28908 		int				bd_len;
28909 
28910 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
28911 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
28912 		    MODEPAGE_AUDIO_CTRL_LEN;
28913 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28914 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28915 		ssc = sd_ssc_init(un);
28916 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
28917 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28918 		    SD_PATH_STANDARD);
28919 		sd_ssc_fini(ssc);
28920 
28921 		if (rval != 0) {
28922 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28923 			    "sr_volume_ctrl: Mode Sense Failed\n");
28924 			kmem_free(sense, sense_buflen);
28925 			kmem_free(select, select_buflen);
28926 			return (rval);
28927 		}
28928 		sense_mhp = (struct mode_header_grp2 *)sense;
28929 		select_mhp = (struct mode_header_grp2 *)select;
28930 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
28931 		    sense_mhp->bdesc_length_lo;
28932 		if (bd_len > MODE_BLK_DESC_LENGTH) {
28933 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28934 			    "sr_volume_ctrl: Mode Sense returned invalid "
28935 			    "block descriptor length\n");
28936 			kmem_free(sense, sense_buflen);
28937 			kmem_free(select, select_buflen);
28938 			return (EIO);
28939 		}
28940 		sense_page = (uchar_t *)
28941 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
28942 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
28943 		select_mhp->length_msb = 0;
28944 		select_mhp->length_lsb = 0;
28945 		select_mhp->bdesc_length_hi = 0;
28946 		select_mhp->bdesc_length_lo = 0;
28947 	} else {
28948 		struct mode_header		*sense_mhp, *select_mhp;
28949 
28950 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28951 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28952 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28953 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28954 		ssc = sd_ssc_init(un);
28955 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
28956 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28957 		    SD_PATH_STANDARD);
28958 		sd_ssc_fini(ssc);
28959 
28960 		if (rval != 0) {
28961 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28962 			    "sr_volume_ctrl: Mode Sense Failed\n");
28963 			kmem_free(sense, sense_buflen);
28964 			kmem_free(select, select_buflen);
28965 			return (rval);
28966 		}
28967 		sense_mhp  = (struct mode_header *)sense;
28968 		select_mhp = (struct mode_header *)select;
28969 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
28970 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28971 			    "sr_volume_ctrl: Mode Sense returned invalid "
28972 			    "block descriptor length\n");
28973 			kmem_free(sense, sense_buflen);
28974 			kmem_free(select, select_buflen);
28975 			return (EIO);
28976 		}
28977 		sense_page = (uchar_t *)
28978 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
28979 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
28980 		select_mhp->length = 0;
28981 		select_mhp->bdesc_length = 0;
28982 	}
28983 	/*
28984 	 * Note: An audio control data structure could be created and overlayed
28985 	 * on the following in place of the array indexing method implemented.
28986 	 */
28987 
28988 	/* Build the select data for the user volume data */
28989 	select_page[0] = MODEPAGE_AUDIO_CTRL;
28990 	select_page[1] = 0xE;
28991 	/* Set the immediate bit */
28992 	select_page[2] = 0x04;
28993 	/* Zero out reserved fields */
28994 	select_page[3] = 0x00;
28995 	select_page[4] = 0x00;
28996 	/* Return sense data for fields not to be modified */
28997 	select_page[5] = sense_page[5];
28998 	select_page[6] = sense_page[6];
28999 	select_page[7] = sense_page[7];
29000 	/* Set the user specified volume levels for channel 0 and 1 */
29001 	select_page[8] = 0x01;
29002 	select_page[9] = vol->channel0;
29003 	select_page[10] = 0x02;
29004 	select_page[11] = vol->channel1;
29005 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29006 	select_page[12] = sense_page[12];
29007 	select_page[13] = sense_page[13];
29008 	select_page[14] = sense_page[14];
29009 	select_page[15] = sense_page[15];
29010 
29011 	ssc = sd_ssc_init(un);
29012 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29013 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29014 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29015 	} else {
29016 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29017 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29018 	}
29019 	sd_ssc_fini(ssc);
29020 
29021 	kmem_free(sense, sense_buflen);
29022 	kmem_free(select, select_buflen);
29023 	return (rval);
29024 }
29025 
29026 
29027 /*
29028  *    Function: sr_read_sony_session_offset()
29029  *
29030  * Description: This routine is the driver entry point for handling CD-ROM
29031  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29032  *		The address of the first track in the last session of a
29033  *		multi-session CD-ROM is returned
29034  *
29035  *		Note: This routine uses a vendor specific key value in the
29036  *		command control field without implementing any vendor check here
29037  *		or in the ioctl routine.
29038  *
29039  *   Arguments: dev	- the device 'dev_t'
29040  *		data	- pointer to an int to hold the requested address
29041  *		flag	- this argument is a pass through to ddi_copyxxx()
29042  *			  directly from the mode argument of ioctl().
29043  *
29044  * Return Code: the code returned by sd_send_scsi_cmd()
29045  *		EFAULT if ddi_copyxxx() fails
29046  *		ENXIO if fail ddi_get_soft_state
29047  *		EINVAL if data pointer is NULL
29048  */
29049 
29050 static int
29051 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29052 {
29053 	struct sd_lun		*un;
29054 	struct uscsi_cmd	*com;
29055 	caddr_t			buffer;
29056 	char			cdb[CDB_GROUP1];
29057 	int			session_offset = 0;
29058 	int			rval;
29059 
29060 	if (data == NULL) {
29061 		return (EINVAL);
29062 	}
29063 
29064 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29065 	    (un->un_state == SD_STATE_OFFLINE)) {
29066 		return (ENXIO);
29067 	}
29068 
29069 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29070 	bzero(cdb, CDB_GROUP1);
29071 	cdb[0] = SCMD_READ_TOC;
29072 	/*
29073 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29074 	 * (4 byte TOC response header + 8 byte response data)
29075 	 */
29076 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29077 	/* Byte 9 is the control byte. A vendor specific value is used */
29078 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29079 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29080 	com->uscsi_cdb = cdb;
29081 	com->uscsi_cdblen = CDB_GROUP1;
29082 	com->uscsi_bufaddr = buffer;
29083 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29084 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29085 
29086 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29087 	    SD_PATH_STANDARD);
29088 	if (rval != 0) {
29089 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29090 		kmem_free(com, sizeof (*com));
29091 		return (rval);
29092 	}
29093 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29094 		session_offset =
29095 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29096 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29097 		/*
29098 		 * Offset returned offset in current lbasize block's. Convert to
29099 		 * 2k block's to return to the user
29100 		 */
29101 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29102 			session_offset >>= 2;
29103 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29104 			session_offset >>= 1;
29105 		}
29106 	}
29107 
29108 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29109 		rval = EFAULT;
29110 	}
29111 
29112 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29113 	kmem_free(com, sizeof (*com));
29114 	return (rval);
29115 }
29116 
29117 
29118 /*
29119  *    Function: sd_wm_cache_constructor()
29120  *
29121  * Description: Cache Constructor for the wmap cache for the read/modify/write
29122  * 		devices.
29123  *
29124  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29125  *		un	- sd_lun structure for the device.
29126  *		flag	- the km flags passed to constructor
29127  *
29128  * Return Code: 0 on success.
29129  *		-1 on failure.
29130  */
29131 
29132 /*ARGSUSED*/
29133 static int
29134 sd_wm_cache_constructor(void *wm, void *un, int flags)
29135 {
29136 	bzero(wm, sizeof (struct sd_w_map));
29137 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29138 	return (0);
29139 }
29140 
29141 
29142 /*
29143  *    Function: sd_wm_cache_destructor()
29144  *
29145  * Description: Cache destructor for the wmap cache for the read/modify/write
29146  * 		devices.
29147  *
29148  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29149  *		un	- sd_lun structure for the device.
29150  */
29151 /*ARGSUSED*/
29152 static void
29153 sd_wm_cache_destructor(void *wm, void *un)
29154 {
29155 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29156 }
29157 
29158 
29159 /*
29160  *    Function: sd_range_lock()
29161  *
29162  * Description: Lock the range of blocks specified as parameter to ensure
29163  *		that read, modify write is atomic and no other i/o writes
29164  *		to the same location. The range is specified in terms
29165  *		of start and end blocks. Block numbers are the actual
29166  *		media block numbers and not system.
29167  *
29168  *   Arguments: un	- sd_lun structure for the device.
29169  *		startb - The starting block number
29170  *		endb - The end block number
29171  *		typ - type of i/o - simple/read_modify_write
29172  *
29173  * Return Code: wm  - pointer to the wmap structure.
29174  *
29175  *     Context: This routine can sleep.
29176  */
29177 
29178 static struct sd_w_map *
29179 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29180 {
29181 	struct sd_w_map *wmp = NULL;
29182 	struct sd_w_map *sl_wmp = NULL;
29183 	struct sd_w_map *tmp_wmp;
29184 	wm_state state = SD_WM_CHK_LIST;
29185 
29186 
29187 	ASSERT(un != NULL);
29188 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29189 
29190 	mutex_enter(SD_MUTEX(un));
29191 
29192 	while (state != SD_WM_DONE) {
29193 
29194 		switch (state) {
29195 		case SD_WM_CHK_LIST:
29196 			/*
29197 			 * This is the starting state. Check the wmap list
29198 			 * to see if the range is currently available.
29199 			 */
29200 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29201 				/*
29202 				 * If this is a simple write and no rmw
29203 				 * i/o is pending then try to lock the
29204 				 * range as the range should be available.
29205 				 */
29206 				state = SD_WM_LOCK_RANGE;
29207 			} else {
29208 				tmp_wmp = sd_get_range(un, startb, endb);
29209 				if (tmp_wmp != NULL) {
29210 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29211 						/*
29212 						 * Should not keep onlist wmps
29213 						 * while waiting this macro
29214 						 * will also do wmp = NULL;
29215 						 */
29216 						FREE_ONLIST_WMAP(un, wmp);
29217 					}
29218 					/*
29219 					 * sl_wmp is the wmap on which wait
29220 					 * is done, since the tmp_wmp points
29221 					 * to the inuse wmap, set sl_wmp to
29222 					 * tmp_wmp and change the state to sleep
29223 					 */
29224 					sl_wmp = tmp_wmp;
29225 					state = SD_WM_WAIT_MAP;
29226 				} else {
29227 					state = SD_WM_LOCK_RANGE;
29228 				}
29229 
29230 			}
29231 			break;
29232 
29233 		case SD_WM_LOCK_RANGE:
29234 			ASSERT(un->un_wm_cache);
29235 			/*
29236 			 * The range need to be locked, try to get a wmap.
29237 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29238 			 * if possible as we will have to release the sd mutex
29239 			 * if we have to sleep.
29240 			 */
29241 			if (wmp == NULL)
29242 				wmp = kmem_cache_alloc(un->un_wm_cache,
29243 				    KM_NOSLEEP);
29244 			if (wmp == NULL) {
29245 				mutex_exit(SD_MUTEX(un));
29246 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29247 				    (sd_lun::un_wm_cache))
29248 				wmp = kmem_cache_alloc(un->un_wm_cache,
29249 				    KM_SLEEP);
29250 				mutex_enter(SD_MUTEX(un));
29251 				/*
29252 				 * we released the mutex so recheck and go to
29253 				 * check list state.
29254 				 */
29255 				state = SD_WM_CHK_LIST;
29256 			} else {
29257 				/*
29258 				 * We exit out of state machine since we
29259 				 * have the wmap. Do the housekeeping first.
29260 				 * place the wmap on the wmap list if it is not
29261 				 * on it already and then set the state to done.
29262 				 */
29263 				wmp->wm_start = startb;
29264 				wmp->wm_end = endb;
29265 				wmp->wm_flags = typ | SD_WM_BUSY;
29266 				if (typ & SD_WTYPE_RMW) {
29267 					un->un_rmw_count++;
29268 				}
29269 				/*
29270 				 * If not already on the list then link
29271 				 */
29272 				if (!ONLIST(un, wmp)) {
29273 					wmp->wm_next = un->un_wm;
29274 					wmp->wm_prev = NULL;
29275 					if (wmp->wm_next)
29276 						wmp->wm_next->wm_prev = wmp;
29277 					un->un_wm = wmp;
29278 				}
29279 				state = SD_WM_DONE;
29280 			}
29281 			break;
29282 
29283 		case SD_WM_WAIT_MAP:
29284 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29285 			/*
29286 			 * Wait is done on sl_wmp, which is set in the
29287 			 * check_list state.
29288 			 */
29289 			sl_wmp->wm_wanted_count++;
29290 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29291 			sl_wmp->wm_wanted_count--;
29292 			/*
29293 			 * We can reuse the memory from the completed sl_wmp
29294 			 * lock range for our new lock, but only if noone is
29295 			 * waiting for it.
29296 			 */
29297 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29298 			if (sl_wmp->wm_wanted_count == 0) {
29299 				if (wmp != NULL)
29300 					CHK_N_FREEWMP(un, wmp);
29301 				wmp = sl_wmp;
29302 			}
29303 			sl_wmp = NULL;
29304 			/*
29305 			 * After waking up, need to recheck for availability of
29306 			 * range.
29307 			 */
29308 			state = SD_WM_CHK_LIST;
29309 			break;
29310 
29311 		default:
29312 			panic("sd_range_lock: "
29313 			    "Unknown state %d in sd_range_lock", state);
29314 			/*NOTREACHED*/
29315 		} /* switch(state) */
29316 
29317 	} /* while(state != SD_WM_DONE) */
29318 
29319 	mutex_exit(SD_MUTEX(un));
29320 
29321 	ASSERT(wmp != NULL);
29322 
29323 	return (wmp);
29324 }
29325 
29326 
29327 /*
29328  *    Function: sd_get_range()
29329  *
29330  * Description: Find if there any overlapping I/O to this one
29331  *		Returns the write-map of 1st such I/O, NULL otherwise.
29332  *
29333  *   Arguments: un	- sd_lun structure for the device.
29334  *		startb - The starting block number
29335  *		endb - The end block number
29336  *
29337  * Return Code: wm  - pointer to the wmap structure.
29338  */
29339 
29340 static struct sd_w_map *
29341 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29342 {
29343 	struct sd_w_map *wmp;
29344 
29345 	ASSERT(un != NULL);
29346 
29347 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29348 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29349 			continue;
29350 		}
29351 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29352 			break;
29353 		}
29354 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29355 			break;
29356 		}
29357 	}
29358 
29359 	return (wmp);
29360 }
29361 
29362 
29363 /*
29364  *    Function: sd_free_inlist_wmap()
29365  *
29366  * Description: Unlink and free a write map struct.
29367  *
29368  *   Arguments: un      - sd_lun structure for the device.
29369  *		wmp	- sd_w_map which needs to be unlinked.
29370  */
29371 
29372 static void
29373 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29374 {
29375 	ASSERT(un != NULL);
29376 
29377 	if (un->un_wm == wmp) {
29378 		un->un_wm = wmp->wm_next;
29379 	} else {
29380 		wmp->wm_prev->wm_next = wmp->wm_next;
29381 	}
29382 
29383 	if (wmp->wm_next) {
29384 		wmp->wm_next->wm_prev = wmp->wm_prev;
29385 	}
29386 
29387 	wmp->wm_next = wmp->wm_prev = NULL;
29388 
29389 	kmem_cache_free(un->un_wm_cache, wmp);
29390 }
29391 
29392 
29393 /*
29394  *    Function: sd_range_unlock()
29395  *
29396  * Description: Unlock the range locked by wm.
29397  *		Free write map if nobody else is waiting on it.
29398  *
29399  *   Arguments: un      - sd_lun structure for the device.
29400  *              wmp     - sd_w_map which needs to be unlinked.
29401  */
29402 
29403 static void
29404 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29405 {
29406 	ASSERT(un != NULL);
29407 	ASSERT(wm != NULL);
29408 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29409 
29410 	mutex_enter(SD_MUTEX(un));
29411 
29412 	if (wm->wm_flags & SD_WTYPE_RMW) {
29413 		un->un_rmw_count--;
29414 	}
29415 
29416 	if (wm->wm_wanted_count) {
29417 		wm->wm_flags = 0;
29418 		/*
29419 		 * Broadcast that the wmap is available now.
29420 		 */
29421 		cv_broadcast(&wm->wm_avail);
29422 	} else {
29423 		/*
29424 		 * If no one is waiting on the map, it should be free'ed.
29425 		 */
29426 		sd_free_inlist_wmap(un, wm);
29427 	}
29428 
29429 	mutex_exit(SD_MUTEX(un));
29430 }
29431 
29432 
29433 /*
29434  *    Function: sd_read_modify_write_task
29435  *
29436  * Description: Called from a taskq thread to initiate the write phase of
29437  *		a read-modify-write request.  This is used for targets where
29438  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29439  *
29440  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29441  *
29442  *     Context: Called under taskq thread context.
29443  */
29444 
29445 static void
29446 sd_read_modify_write_task(void *arg)
29447 {
29448 	struct sd_mapblocksize_info	*bsp;
29449 	struct buf	*bp;
29450 	struct sd_xbuf	*xp;
29451 	struct sd_lun	*un;
29452 
29453 	bp = arg;	/* The bp is given in arg */
29454 	ASSERT(bp != NULL);
29455 
29456 	/* Get the pointer to the layer-private data struct */
29457 	xp = SD_GET_XBUF(bp);
29458 	ASSERT(xp != NULL);
29459 	bsp = xp->xb_private;
29460 	ASSERT(bsp != NULL);
29461 
29462 	un = SD_GET_UN(bp);
29463 	ASSERT(un != NULL);
29464 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29465 
29466 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29467 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29468 
29469 	/*
29470 	 * This is the write phase of a read-modify-write request, called
29471 	 * under the context of a taskq thread in response to the completion
29472 	 * of the read portion of the rmw request completing under interrupt
29473 	 * context. The write request must be sent from here down the iostart
29474 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29475 	 * we use the layer index saved in the layer-private data area.
29476 	 */
29477 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29478 
29479 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29480 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29481 }
29482 
29483 
29484 /*
29485  *    Function: sddump_do_read_of_rmw()
29486  *
29487  * Description: This routine will be called from sddump, If sddump is called
29488  *		with an I/O which not aligned on device blocksize boundary
29489  *		then the write has to be converted to read-modify-write.
29490  *		Do the read part here in order to keep sddump simple.
29491  *		Note - That the sd_mutex is held across the call to this
29492  *		routine.
29493  *
29494  *   Arguments: un	- sd_lun
29495  *		blkno	- block number in terms of media block size.
29496  *		nblk	- number of blocks.
29497  *		bpp	- pointer to pointer to the buf structure. On return
29498  *			from this function, *bpp points to the valid buffer
29499  *			to which the write has to be done.
29500  *
29501  * Return Code: 0 for success or errno-type return code
29502  */
29503 
29504 static int
29505 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29506 	struct buf **bpp)
29507 {
29508 	int err;
29509 	int i;
29510 	int rval;
29511 	struct buf *bp;
29512 	struct scsi_pkt *pkt = NULL;
29513 	uint32_t target_blocksize;
29514 
29515 	ASSERT(un != NULL);
29516 	ASSERT(mutex_owned(SD_MUTEX(un)));
29517 
29518 	target_blocksize = un->un_tgt_blocksize;
29519 
29520 	mutex_exit(SD_MUTEX(un));
29521 
29522 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29523 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29524 	if (bp == NULL) {
29525 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29526 		    "no resources for dumping; giving up");
29527 		err = ENOMEM;
29528 		goto done;
29529 	}
29530 
29531 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29532 	    blkno, nblk);
29533 	if (rval != 0) {
29534 		scsi_free_consistent_buf(bp);
29535 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29536 		    "no resources for dumping; giving up");
29537 		err = ENOMEM;
29538 		goto done;
29539 	}
29540 
29541 	pkt->pkt_flags |= FLAG_NOINTR;
29542 
29543 	err = EIO;
29544 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29545 
29546 		/*
29547 		 * Scsi_poll returns 0 (success) if the command completes and
29548 		 * the status block is STATUS_GOOD.  We should only check
29549 		 * errors if this condition is not true.  Even then we should
29550 		 * send our own request sense packet only if we have a check
29551 		 * condition and auto request sense has not been performed by
29552 		 * the hba.
29553 		 */
29554 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29555 
29556 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29557 			err = 0;
29558 			break;
29559 		}
29560 
29561 		/*
29562 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29563 		 * no need to read RQS data.
29564 		 */
29565 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29566 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29567 			    "Error while dumping state with rmw..."
29568 			    "Device is gone\n");
29569 			break;
29570 		}
29571 
29572 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29573 			SD_INFO(SD_LOG_DUMP, un,
29574 			    "sddump: read failed with CHECK, try # %d\n", i);
29575 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29576 				(void) sd_send_polled_RQS(un);
29577 			}
29578 
29579 			continue;
29580 		}
29581 
29582 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29583 			int reset_retval = 0;
29584 
29585 			SD_INFO(SD_LOG_DUMP, un,
29586 			    "sddump: read failed with BUSY, try # %d\n", i);
29587 
29588 			if (un->un_f_lun_reset_enabled == TRUE) {
29589 				reset_retval = scsi_reset(SD_ADDRESS(un),
29590 				    RESET_LUN);
29591 			}
29592 			if (reset_retval == 0) {
29593 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29594 			}
29595 			(void) sd_send_polled_RQS(un);
29596 
29597 		} else {
29598 			SD_INFO(SD_LOG_DUMP, un,
29599 			    "sddump: read failed with 0x%x, try # %d\n",
29600 			    SD_GET_PKT_STATUS(pkt), i);
29601 			mutex_enter(SD_MUTEX(un));
29602 			sd_reset_target(un, pkt);
29603 			mutex_exit(SD_MUTEX(un));
29604 		}
29605 
29606 		/*
29607 		 * If we are not getting anywhere with lun/target resets,
29608 		 * let's reset the bus.
29609 		 */
29610 		if (i > SD_NDUMP_RETRIES/2) {
29611 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29612 			(void) sd_send_polled_RQS(un);
29613 		}
29614 
29615 	}
29616 	scsi_destroy_pkt(pkt);
29617 
29618 	if (err != 0) {
29619 		scsi_free_consistent_buf(bp);
29620 		*bpp = NULL;
29621 	} else {
29622 		*bpp = bp;
29623 	}
29624 
29625 done:
29626 	mutex_enter(SD_MUTEX(un));
29627 	return (err);
29628 }
29629 
29630 
29631 /*
29632  *    Function: sd_failfast_flushq
29633  *
29634  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29635  *		in b_flags and move them onto the failfast queue, then kick
29636  *		off a thread to return all bp's on the failfast queue to
29637  *		their owners with an error set.
29638  *
29639  *   Arguments: un - pointer to the soft state struct for the instance.
29640  *
29641  *     Context: may execute in interrupt context.
29642  */
29643 
29644 static void
29645 sd_failfast_flushq(struct sd_lun *un)
29646 {
29647 	struct buf *bp;
29648 	struct buf *next_waitq_bp;
29649 	struct buf *prev_waitq_bp = NULL;
29650 
29651 	ASSERT(un != NULL);
29652 	ASSERT(mutex_owned(SD_MUTEX(un)));
29653 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29654 	ASSERT(un->un_failfast_bp == NULL);
29655 
29656 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29657 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29658 
29659 	/*
29660 	 * Check if we should flush all bufs when entering failfast state, or
29661 	 * just those with B_FAILFAST set.
29662 	 */
29663 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29664 		/*
29665 		 * Move *all* bp's on the wait queue to the failfast flush
29666 		 * queue, including those that do NOT have B_FAILFAST set.
29667 		 */
29668 		if (un->un_failfast_headp == NULL) {
29669 			ASSERT(un->un_failfast_tailp == NULL);
29670 			un->un_failfast_headp = un->un_waitq_headp;
29671 		} else {
29672 			ASSERT(un->un_failfast_tailp != NULL);
29673 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29674 		}
29675 
29676 		un->un_failfast_tailp = un->un_waitq_tailp;
29677 
29678 		/* update kstat for each bp moved out of the waitq */
29679 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29680 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29681 		}
29682 
29683 		/* empty the waitq */
29684 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29685 
29686 	} else {
29687 		/*
29688 		 * Go thru the wait queue, pick off all entries with
29689 		 * B_FAILFAST set, and move these onto the failfast queue.
29690 		 */
29691 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29692 			/*
29693 			 * Save the pointer to the next bp on the wait queue,
29694 			 * so we get to it on the next iteration of this loop.
29695 			 */
29696 			next_waitq_bp = bp->av_forw;
29697 
29698 			/*
29699 			 * If this bp from the wait queue does NOT have
29700 			 * B_FAILFAST set, just move on to the next element
29701 			 * in the wait queue. Note, this is the only place
29702 			 * where it is correct to set prev_waitq_bp.
29703 			 */
29704 			if ((bp->b_flags & B_FAILFAST) == 0) {
29705 				prev_waitq_bp = bp;
29706 				continue;
29707 			}
29708 
29709 			/*
29710 			 * Remove the bp from the wait queue.
29711 			 */
29712 			if (bp == un->un_waitq_headp) {
29713 				/* The bp is the first element of the waitq. */
29714 				un->un_waitq_headp = next_waitq_bp;
29715 				if (un->un_waitq_headp == NULL) {
29716 					/* The wait queue is now empty */
29717 					un->un_waitq_tailp = NULL;
29718 				}
29719 			} else {
29720 				/*
29721 				 * The bp is either somewhere in the middle
29722 				 * or at the end of the wait queue.
29723 				 */
29724 				ASSERT(un->un_waitq_headp != NULL);
29725 				ASSERT(prev_waitq_bp != NULL);
29726 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29727 				    == 0);
29728 				if (bp == un->un_waitq_tailp) {
29729 					/* bp is the last entry on the waitq. */
29730 					ASSERT(next_waitq_bp == NULL);
29731 					un->un_waitq_tailp = prev_waitq_bp;
29732 				}
29733 				prev_waitq_bp->av_forw = next_waitq_bp;
29734 			}
29735 			bp->av_forw = NULL;
29736 
29737 			/*
29738 			 * update kstat since the bp is moved out of
29739 			 * the waitq
29740 			 */
29741 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29742 
29743 			/*
29744 			 * Now put the bp onto the failfast queue.
29745 			 */
29746 			if (un->un_failfast_headp == NULL) {
29747 				/* failfast queue is currently empty */
29748 				ASSERT(un->un_failfast_tailp == NULL);
29749 				un->un_failfast_headp =
29750 				    un->un_failfast_tailp = bp;
29751 			} else {
29752 				/* Add the bp to the end of the failfast q */
29753 				ASSERT(un->un_failfast_tailp != NULL);
29754 				ASSERT(un->un_failfast_tailp->b_flags &
29755 				    B_FAILFAST);
29756 				un->un_failfast_tailp->av_forw = bp;
29757 				un->un_failfast_tailp = bp;
29758 			}
29759 		}
29760 	}
29761 
29762 	/*
29763 	 * Now return all bp's on the failfast queue to their owners.
29764 	 */
29765 	while ((bp = un->un_failfast_headp) != NULL) {
29766 
29767 		un->un_failfast_headp = bp->av_forw;
29768 		if (un->un_failfast_headp == NULL) {
29769 			un->un_failfast_tailp = NULL;
29770 		}
29771 
29772 		/*
29773 		 * We want to return the bp with a failure error code, but
29774 		 * we do not want a call to sd_start_cmds() to occur here,
29775 		 * so use sd_return_failed_command_no_restart() instead of
29776 		 * sd_return_failed_command().
29777 		 */
29778 		sd_return_failed_command_no_restart(un, bp, EIO);
29779 	}
29780 
29781 	/* Flush the xbuf queues if required. */
29782 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29783 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29784 	}
29785 
29786 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29787 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29788 }
29789 
29790 
29791 /*
29792  *    Function: sd_failfast_flushq_callback
29793  *
29794  * Description: Return TRUE if the given bp meets the criteria for failfast
29795  *		flushing. Used with ddi_xbuf_flushq(9F).
29796  *
29797  *   Arguments: bp - ptr to buf struct to be examined.
29798  *
29799  *     Context: Any
29800  */
29801 
29802 static int
29803 sd_failfast_flushq_callback(struct buf *bp)
29804 {
29805 	/*
29806 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29807 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29808 	 */
29809 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29810 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29811 }
29812 
29813 
29814 
29815 /*
29816  * Function: sd_setup_next_xfer
29817  *
29818  * Description: Prepare next I/O operation using DMA_PARTIAL
29819  *
29820  */
29821 
29822 static int
29823 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29824     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29825 {
29826 	ssize_t	num_blks_not_xfered;
29827 	daddr_t	strt_blk_num;
29828 	ssize_t	bytes_not_xfered;
29829 	int	rval;
29830 
29831 	ASSERT(pkt->pkt_resid == 0);
29832 
29833 	/*
29834 	 * Calculate next block number and amount to be transferred.
29835 	 *
29836 	 * How much data NOT transfered to the HBA yet.
29837 	 */
29838 	bytes_not_xfered = xp->xb_dma_resid;
29839 
29840 	/*
29841 	 * figure how many blocks NOT transfered to the HBA yet.
29842 	 */
29843 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29844 
29845 	/*
29846 	 * set starting block number to the end of what WAS transfered.
29847 	 */
29848 	strt_blk_num = xp->xb_blkno +
29849 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29850 
29851 	/*
29852 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29853 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29854 	 * the disk mutex here.
29855 	 */
29856 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
29857 	    strt_blk_num, num_blks_not_xfered);
29858 
29859 	if (rval == 0) {
29860 
29861 		/*
29862 		 * Success.
29863 		 *
29864 		 * Adjust things if there are still more blocks to be
29865 		 * transfered.
29866 		 */
29867 		xp->xb_dma_resid = pkt->pkt_resid;
29868 		pkt->pkt_resid = 0;
29869 
29870 		return (1);
29871 	}
29872 
29873 	/*
29874 	 * There's really only one possible return value from
29875 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29876 	 * returns NULL.
29877 	 */
29878 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29879 
29880 	bp->b_resid = bp->b_bcount;
29881 	bp->b_flags |= B_ERROR;
29882 
29883 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29884 	    "Error setting up next portion of DMA transfer\n");
29885 
29886 	return (0);
29887 }
29888 
29889 /*
29890  *    Function: sd_panic_for_res_conflict
29891  *
29892  * Description: Call panic with a string formatted with "Reservation Conflict"
29893  *		and a human readable identifier indicating the SD instance
29894  *		that experienced the reservation conflict.
29895  *
29896  *   Arguments: un - pointer to the soft state struct for the instance.
29897  *
29898  *     Context: may execute in interrupt context.
29899  */
29900 
29901 #define	SD_RESV_CONFLICT_FMT_LEN 40
29902 void
29903 sd_panic_for_res_conflict(struct sd_lun *un)
29904 {
29905 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
29906 	char path_str[MAXPATHLEN];
29907 
29908 	(void) snprintf(panic_str, sizeof (panic_str),
29909 	    "Reservation Conflict\nDisk: %s",
29910 	    ddi_pathname(SD_DEVINFO(un), path_str));
29911 
29912 	panic(panic_str);
29913 }
29914 
29915 /*
29916  * Note: The following sd_faultinjection_ioctl( ) routines implement
29917  * driver support for handling fault injection for error analysis
29918  * causing faults in multiple layers of the driver.
29919  *
29920  */
29921 
29922 #ifdef SD_FAULT_INJECTION
29923 static uint_t   sd_fault_injection_on = 0;
29924 
29925 /*
29926  *    Function: sd_faultinjection_ioctl()
29927  *
29928  * Description: This routine is the driver entry point for handling
29929  *              faultinjection ioctls to inject errors into the
29930  *              layer model
29931  *
29932  *   Arguments: cmd	- the ioctl cmd received
29933  *		arg	- the arguments from user and returns
29934  */
29935 
29936 static void
29937 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
29938 
29939 	uint_t i = 0;
29940 	uint_t rval;
29941 
29942 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
29943 
29944 	mutex_enter(SD_MUTEX(un));
29945 
29946 	switch (cmd) {
29947 	case SDIOCRUN:
29948 		/* Allow pushed faults to be injected */
29949 		SD_INFO(SD_LOG_SDTEST, un,
29950 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
29951 
29952 		sd_fault_injection_on = 1;
29953 
29954 		SD_INFO(SD_LOG_IOERR, un,
29955 		    "sd_faultinjection_ioctl: run finished\n");
29956 		break;
29957 
29958 	case SDIOCSTART:
29959 		/* Start Injection Session */
29960 		SD_INFO(SD_LOG_SDTEST, un,
29961 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
29962 
29963 		sd_fault_injection_on = 0;
29964 		un->sd_injection_mask = 0xFFFFFFFF;
29965 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29966 			un->sd_fi_fifo_pkt[i] = NULL;
29967 			un->sd_fi_fifo_xb[i] = NULL;
29968 			un->sd_fi_fifo_un[i] = NULL;
29969 			un->sd_fi_fifo_arq[i] = NULL;
29970 		}
29971 		un->sd_fi_fifo_start = 0;
29972 		un->sd_fi_fifo_end = 0;
29973 
29974 		mutex_enter(&(un->un_fi_mutex));
29975 		un->sd_fi_log[0] = '\0';
29976 		un->sd_fi_buf_len = 0;
29977 		mutex_exit(&(un->un_fi_mutex));
29978 
29979 		SD_INFO(SD_LOG_IOERR, un,
29980 		    "sd_faultinjection_ioctl: start finished\n");
29981 		break;
29982 
29983 	case SDIOCSTOP:
29984 		/* Stop Injection Session */
29985 		SD_INFO(SD_LOG_SDTEST, un,
29986 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
29987 		sd_fault_injection_on = 0;
29988 		un->sd_injection_mask = 0x0;
29989 
29990 		/* Empty stray or unuseds structs from fifo */
29991 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29992 			if (un->sd_fi_fifo_pkt[i] != NULL) {
29993 				kmem_free(un->sd_fi_fifo_pkt[i],
29994 				    sizeof (struct sd_fi_pkt));
29995 			}
29996 			if (un->sd_fi_fifo_xb[i] != NULL) {
29997 				kmem_free(un->sd_fi_fifo_xb[i],
29998 				    sizeof (struct sd_fi_xb));
29999 			}
30000 			if (un->sd_fi_fifo_un[i] != NULL) {
30001 				kmem_free(un->sd_fi_fifo_un[i],
30002 				    sizeof (struct sd_fi_un));
30003 			}
30004 			if (un->sd_fi_fifo_arq[i] != NULL) {
30005 				kmem_free(un->sd_fi_fifo_arq[i],
30006 				    sizeof (struct sd_fi_arq));
30007 			}
30008 			un->sd_fi_fifo_pkt[i] = NULL;
30009 			un->sd_fi_fifo_un[i] = NULL;
30010 			un->sd_fi_fifo_xb[i] = NULL;
30011 			un->sd_fi_fifo_arq[i] = NULL;
30012 		}
30013 		un->sd_fi_fifo_start = 0;
30014 		un->sd_fi_fifo_end = 0;
30015 
30016 		SD_INFO(SD_LOG_IOERR, un,
30017 		    "sd_faultinjection_ioctl: stop finished\n");
30018 		break;
30019 
30020 	case SDIOCINSERTPKT:
30021 		/* Store a packet struct to be pushed onto fifo */
30022 		SD_INFO(SD_LOG_SDTEST, un,
30023 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30024 
30025 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30026 
30027 		sd_fault_injection_on = 0;
30028 
30029 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30030 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30031 			kmem_free(un->sd_fi_fifo_pkt[i],
30032 			    sizeof (struct sd_fi_pkt));
30033 		}
30034 		if (arg != NULL) {
30035 			un->sd_fi_fifo_pkt[i] =
30036 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30037 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30038 				/* Alloc failed don't store anything */
30039 				break;
30040 			}
30041 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30042 			    sizeof (struct sd_fi_pkt), 0);
30043 			if (rval == -1) {
30044 				kmem_free(un->sd_fi_fifo_pkt[i],
30045 				    sizeof (struct sd_fi_pkt));
30046 				un->sd_fi_fifo_pkt[i] = NULL;
30047 			}
30048 		} else {
30049 			SD_INFO(SD_LOG_IOERR, un,
30050 			    "sd_faultinjection_ioctl: pkt null\n");
30051 		}
30052 		break;
30053 
30054 	case SDIOCINSERTXB:
30055 		/* Store a xb struct to be pushed onto fifo */
30056 		SD_INFO(SD_LOG_SDTEST, un,
30057 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30058 
30059 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30060 
30061 		sd_fault_injection_on = 0;
30062 
30063 		if (un->sd_fi_fifo_xb[i] != NULL) {
30064 			kmem_free(un->sd_fi_fifo_xb[i],
30065 			    sizeof (struct sd_fi_xb));
30066 			un->sd_fi_fifo_xb[i] = NULL;
30067 		}
30068 		if (arg != NULL) {
30069 			un->sd_fi_fifo_xb[i] =
30070 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30071 			if (un->sd_fi_fifo_xb[i] == NULL) {
30072 				/* Alloc failed don't store anything */
30073 				break;
30074 			}
30075 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30076 			    sizeof (struct sd_fi_xb), 0);
30077 
30078 			if (rval == -1) {
30079 				kmem_free(un->sd_fi_fifo_xb[i],
30080 				    sizeof (struct sd_fi_xb));
30081 				un->sd_fi_fifo_xb[i] = NULL;
30082 			}
30083 		} else {
30084 			SD_INFO(SD_LOG_IOERR, un,
30085 			    "sd_faultinjection_ioctl: xb null\n");
30086 		}
30087 		break;
30088 
30089 	case SDIOCINSERTUN:
30090 		/* Store a un struct to be pushed onto fifo */
30091 		SD_INFO(SD_LOG_SDTEST, un,
30092 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30093 
30094 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30095 
30096 		sd_fault_injection_on = 0;
30097 
30098 		if (un->sd_fi_fifo_un[i] != NULL) {
30099 			kmem_free(un->sd_fi_fifo_un[i],
30100 			    sizeof (struct sd_fi_un));
30101 			un->sd_fi_fifo_un[i] = NULL;
30102 		}
30103 		if (arg != NULL) {
30104 			un->sd_fi_fifo_un[i] =
30105 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30106 			if (un->sd_fi_fifo_un[i] == NULL) {
30107 				/* Alloc failed don't store anything */
30108 				break;
30109 			}
30110 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30111 			    sizeof (struct sd_fi_un), 0);
30112 			if (rval == -1) {
30113 				kmem_free(un->sd_fi_fifo_un[i],
30114 				    sizeof (struct sd_fi_un));
30115 				un->sd_fi_fifo_un[i] = NULL;
30116 			}
30117 
30118 		} else {
30119 			SD_INFO(SD_LOG_IOERR, un,
30120 			    "sd_faultinjection_ioctl: un null\n");
30121 		}
30122 
30123 		break;
30124 
30125 	case SDIOCINSERTARQ:
30126 		/* Store a arq struct to be pushed onto fifo */
30127 		SD_INFO(SD_LOG_SDTEST, un,
30128 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30129 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30130 
30131 		sd_fault_injection_on = 0;
30132 
30133 		if (un->sd_fi_fifo_arq[i] != NULL) {
30134 			kmem_free(un->sd_fi_fifo_arq[i],
30135 			    sizeof (struct sd_fi_arq));
30136 			un->sd_fi_fifo_arq[i] = NULL;
30137 		}
30138 		if (arg != NULL) {
30139 			un->sd_fi_fifo_arq[i] =
30140 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30141 			if (un->sd_fi_fifo_arq[i] == NULL) {
30142 				/* Alloc failed don't store anything */
30143 				break;
30144 			}
30145 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30146 			    sizeof (struct sd_fi_arq), 0);
30147 			if (rval == -1) {
30148 				kmem_free(un->sd_fi_fifo_arq[i],
30149 				    sizeof (struct sd_fi_arq));
30150 				un->sd_fi_fifo_arq[i] = NULL;
30151 			}
30152 
30153 		} else {
30154 			SD_INFO(SD_LOG_IOERR, un,
30155 			    "sd_faultinjection_ioctl: arq null\n");
30156 		}
30157 
30158 		break;
30159 
30160 	case SDIOCPUSH:
30161 		/* Push stored xb, pkt, un, and arq onto fifo */
30162 		sd_fault_injection_on = 0;
30163 
30164 		if (arg != NULL) {
30165 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30166 			if (rval != -1 &&
30167 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30168 				un->sd_fi_fifo_end += i;
30169 			}
30170 		} else {
30171 			SD_INFO(SD_LOG_IOERR, un,
30172 			    "sd_faultinjection_ioctl: push arg null\n");
30173 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30174 				un->sd_fi_fifo_end++;
30175 			}
30176 		}
30177 		SD_INFO(SD_LOG_IOERR, un,
30178 		    "sd_faultinjection_ioctl: push to end=%d\n",
30179 		    un->sd_fi_fifo_end);
30180 		break;
30181 
30182 	case SDIOCRETRIEVE:
30183 		/* Return buffer of log from Injection session */
30184 		SD_INFO(SD_LOG_SDTEST, un,
30185 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30186 
30187 		sd_fault_injection_on = 0;
30188 
30189 		mutex_enter(&(un->un_fi_mutex));
30190 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30191 		    un->sd_fi_buf_len+1, 0);
30192 		mutex_exit(&(un->un_fi_mutex));
30193 
30194 		if (rval == -1) {
30195 			/*
30196 			 * arg is possibly invalid setting
30197 			 * it to NULL for return
30198 			 */
30199 			arg = NULL;
30200 		}
30201 		break;
30202 	}
30203 
30204 	mutex_exit(SD_MUTEX(un));
30205 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30206 			    " exit\n");
30207 }
30208 
30209 
30210 /*
30211  *    Function: sd_injection_log()
30212  *
30213  * Description: This routine adds buff to the already existing injection log
30214  *              for retrieval via faultinjection_ioctl for use in fault
30215  *              detection and recovery
30216  *
30217  *   Arguments: buf - the string to add to the log
30218  */
30219 
30220 static void
30221 sd_injection_log(char *buf, struct sd_lun *un)
30222 {
30223 	uint_t len;
30224 
30225 	ASSERT(un != NULL);
30226 	ASSERT(buf != NULL);
30227 
30228 	mutex_enter(&(un->un_fi_mutex));
30229 
30230 	len = min(strlen(buf), 255);
30231 	/* Add logged value to Injection log to be returned later */
30232 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30233 		uint_t	offset = strlen((char *)un->sd_fi_log);
30234 		char *destp = (char *)un->sd_fi_log + offset;
30235 		int i;
30236 		for (i = 0; i < len; i++) {
30237 			*destp++ = *buf++;
30238 		}
30239 		un->sd_fi_buf_len += len;
30240 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30241 	}
30242 
30243 	mutex_exit(&(un->un_fi_mutex));
30244 }
30245 
30246 
30247 /*
30248  *    Function: sd_faultinjection()
30249  *
30250  * Description: This routine takes the pkt and changes its
30251  *		content based on error injection scenerio.
30252  *
30253  *   Arguments: pktp	- packet to be changed
30254  */
30255 
30256 static void
30257 sd_faultinjection(struct scsi_pkt *pktp)
30258 {
30259 	uint_t i;
30260 	struct sd_fi_pkt *fi_pkt;
30261 	struct sd_fi_xb *fi_xb;
30262 	struct sd_fi_un *fi_un;
30263 	struct sd_fi_arq *fi_arq;
30264 	struct buf *bp;
30265 	struct sd_xbuf *xb;
30266 	struct sd_lun *un;
30267 
30268 	ASSERT(pktp != NULL);
30269 
30270 	/* pull bp xb and un from pktp */
30271 	bp = (struct buf *)pktp->pkt_private;
30272 	xb = SD_GET_XBUF(bp);
30273 	un = SD_GET_UN(bp);
30274 
30275 	ASSERT(un != NULL);
30276 
30277 	mutex_enter(SD_MUTEX(un));
30278 
30279 	SD_TRACE(SD_LOG_SDTEST, un,
30280 	    "sd_faultinjection: entry Injection from sdintr\n");
30281 
30282 	/* if injection is off return */
30283 	if (sd_fault_injection_on == 0 ||
30284 	    un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30285 		mutex_exit(SD_MUTEX(un));
30286 		return;
30287 	}
30288 
30289 	SD_INFO(SD_LOG_SDTEST, un,
30290 	    "sd_faultinjection: is working for copying\n");
30291 
30292 	/* take next set off fifo */
30293 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30294 
30295 	fi_pkt = un->sd_fi_fifo_pkt[i];
30296 	fi_xb = un->sd_fi_fifo_xb[i];
30297 	fi_un = un->sd_fi_fifo_un[i];
30298 	fi_arq = un->sd_fi_fifo_arq[i];
30299 
30300 
30301 	/* set variables accordingly */
30302 	/* set pkt if it was on fifo */
30303 	if (fi_pkt != NULL) {
30304 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30305 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30306 		if (fi_pkt->pkt_cdbp != 0xff)
30307 			SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30308 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30309 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30310 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30311 
30312 	}
30313 	/* set xb if it was on fifo */
30314 	if (fi_xb != NULL) {
30315 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30316 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30317 		if (fi_xb->xb_retry_count != 0)
30318 			SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30319 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30320 		    "xb_victim_retry_count");
30321 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30322 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30323 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30324 
30325 		/* copy in block data from sense */
30326 		/*
30327 		 * if (fi_xb->xb_sense_data[0] != -1) {
30328 		 *	bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30329 		 *	SENSE_LENGTH);
30330 		 * }
30331 		 */
30332 		bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30333 
30334 		/* copy in extended sense codes */
30335 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30336 		    xb, es_code, "es_code");
30337 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30338 		    xb, es_key, "es_key");
30339 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30340 		    xb, es_add_code, "es_add_code");
30341 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30342 		    xb, es_qual_code, "es_qual_code");
30343 		struct scsi_extended_sense *esp;
30344 		esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30345 		esp->es_class = CLASS_EXTENDED_SENSE;
30346 	}
30347 
30348 	/* set un if it was on fifo */
30349 	if (fi_un != NULL) {
30350 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30351 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30352 		SD_CONDSET(un, un, un_reset_retry_count,
30353 		    "un_reset_retry_count");
30354 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30355 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30356 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30357 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30358 		    "un_f_allow_bus_device_reset");
30359 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30360 
30361 	}
30362 
30363 	/* copy in auto request sense if it was on fifo */
30364 	if (fi_arq != NULL) {
30365 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30366 	}
30367 
30368 	/* free structs */
30369 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30370 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30371 	}
30372 	if (un->sd_fi_fifo_xb[i] != NULL) {
30373 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30374 	}
30375 	if (un->sd_fi_fifo_un[i] != NULL) {
30376 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30377 	}
30378 	if (un->sd_fi_fifo_arq[i] != NULL) {
30379 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30380 	}
30381 
30382 	/*
30383 	 * kmem_free does not gurantee to set to NULL
30384 	 * since we uses these to determine if we set
30385 	 * values or not lets confirm they are always
30386 	 * NULL after free
30387 	 */
30388 	un->sd_fi_fifo_pkt[i] = NULL;
30389 	un->sd_fi_fifo_un[i] = NULL;
30390 	un->sd_fi_fifo_xb[i] = NULL;
30391 	un->sd_fi_fifo_arq[i] = NULL;
30392 
30393 	un->sd_fi_fifo_start++;
30394 
30395 	mutex_exit(SD_MUTEX(un));
30396 
30397 	SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30398 }
30399 
30400 #endif /* SD_FAULT_INJECTION */
30401 
30402 /*
30403  * This routine is invoked in sd_unit_attach(). Before calling it, the
30404  * properties in conf file should be processed already, and "hotpluggable"
30405  * property was processed also.
30406  *
30407  * The sd driver distinguishes 3 different type of devices: removable media,
30408  * non-removable media, and hotpluggable. Below the differences are defined:
30409  *
30410  * 1. Device ID
30411  *
30412  *     The device ID of a device is used to identify this device. Refer to
30413  *     ddi_devid_register(9F).
30414  *
30415  *     For a non-removable media disk device which can provide 0x80 or 0x83
30416  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30417  *     device ID is created to identify this device. For other non-removable
30418  *     media devices, a default device ID is created only if this device has
30419  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30420  *
30421  *     -------------------------------------------------------
30422  *     removable media   hotpluggable  | Can Have Device ID
30423  *     -------------------------------------------------------
30424  *         false             false     |     Yes
30425  *         false             true      |     Yes
30426  *         true                x       |     No
30427  *     ------------------------------------------------------
30428  *
30429  *
30430  * 2. SCSI group 4 commands
30431  *
30432  *     In SCSI specs, only some commands in group 4 command set can use
30433  *     8-byte addresses that can be used to access >2TB storage spaces.
30434  *     Other commands have no such capability. Without supporting group4,
30435  *     it is impossible to make full use of storage spaces of a disk with
30436  *     capacity larger than 2TB.
30437  *
30438  *     -----------------------------------------------
30439  *     removable media   hotpluggable   LP64  |  Group
30440  *     -----------------------------------------------
30441  *           false          false       false |   1
30442  *           false          false       true  |   4
30443  *           false          true        false |   1
30444  *           false          true        true  |   4
30445  *           true             x           x   |   5
30446  *     -----------------------------------------------
30447  *
30448  *
30449  * 3. Check for VTOC Label
30450  *
30451  *     If a direct-access disk has no EFI label, sd will check if it has a
30452  *     valid VTOC label. Now, sd also does that check for removable media
30453  *     and hotpluggable devices.
30454  *
30455  *     --------------------------------------------------------------
30456  *     Direct-Access   removable media    hotpluggable |  Check Label
30457  *     -------------------------------------------------------------
30458  *         false          false           false        |   No
30459  *         false          false           true         |   No
30460  *         false          true            false        |   Yes
30461  *         false          true            true         |   Yes
30462  *         true            x                x          |   Yes
30463  *     --------------------------------------------------------------
30464  *
30465  *
30466  * 4. Building default VTOC label
30467  *
30468  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30469  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30470  *     create default VTOC for them. Currently sd creates default VTOC label
30471  *     for all devices on x86 platform (VTOC_16), but only for removable
30472  *     media devices on SPARC (VTOC_8).
30473  *
30474  *     -----------------------------------------------------------
30475  *       removable media hotpluggable platform   |   Default Label
30476  *     -----------------------------------------------------------
30477  *             false          false    sparc     |     No
30478  *             false          true      x86      |     Yes
30479  *             false          true     sparc     |     Yes
30480  *             true             x        x       |     Yes
30481  *     ----------------------------------------------------------
30482  *
30483  *
30484  * 5. Supported blocksizes of target devices
30485  *
30486  *     Sd supports non-512-byte blocksize for removable media devices only.
30487  *     For other devices, only 512-byte blocksize is supported. This may be
30488  *     changed in near future because some RAID devices require non-512-byte
30489  *     blocksize
30490  *
30491  *     -----------------------------------------------------------
30492  *     removable media    hotpluggable    | non-512-byte blocksize
30493  *     -----------------------------------------------------------
30494  *           false          false         |   No
30495  *           false          true          |   No
30496  *           true             x           |   Yes
30497  *     -----------------------------------------------------------
30498  *
30499  *
30500  * 6. Automatic mount & unmount
30501  *
30502  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30503  *     if a device is removable media device. It return 1 for removable media
30504  *     devices, and 0 for others.
30505  *
30506  *     The automatic mounting subsystem should distinguish between the types
30507  *     of devices and apply automounting policies to each.
30508  *
30509  *
30510  * 7. fdisk partition management
30511  *
30512  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30513  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30514  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30515  *     fdisk partitions on both x86 and SPARC platform.
30516  *
30517  *     -----------------------------------------------------------
30518  *       platform   removable media  USB/1394  |  fdisk supported
30519  *     -----------------------------------------------------------
30520  *        x86         X               X        |       true
30521  *     ------------------------------------------------------------
30522  *        sparc       X               X        |       false
30523  *     ------------------------------------------------------------
30524  *
30525  *
30526  * 8. MBOOT/MBR
30527  *
30528  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30529  *     read/write mboot for removable media devices on sparc platform.
30530  *
30531  *     -----------------------------------------------------------
30532  *       platform   removable media  USB/1394  |  mboot supported
30533  *     -----------------------------------------------------------
30534  *        x86         X               X        |       true
30535  *     ------------------------------------------------------------
30536  *        sparc      false           false     |       false
30537  *        sparc      false           true      |       true
30538  *        sparc      true            false     |       true
30539  *        sparc      true            true      |       true
30540  *     ------------------------------------------------------------
30541  *
30542  *
30543  * 9.  error handling during opening device
30544  *
30545  *     If failed to open a disk device, an errno is returned. For some kinds
30546  *     of errors, different errno is returned depending on if this device is
30547  *     a removable media device. This brings USB/1394 hard disks in line with
30548  *     expected hard disk behavior. It is not expected that this breaks any
30549  *     application.
30550  *
30551  *     ------------------------------------------------------
30552  *       removable media    hotpluggable   |  errno
30553  *     ------------------------------------------------------
30554  *             false          false        |   EIO
30555  *             false          true         |   EIO
30556  *             true             x          |   ENXIO
30557  *     ------------------------------------------------------
30558  *
30559  *
30560  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30561  *
30562  *     These IOCTLs are applicable only to removable media devices.
30563  *
30564  *     -----------------------------------------------------------
30565  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30566  *     -----------------------------------------------------------
30567  *             false          false        |     No
30568  *             false          true         |     No
30569  *             true            x           |     Yes
30570  *     -----------------------------------------------------------
30571  *
30572  *
30573  * 12. Kstats for partitions
30574  *
30575  *     sd creates partition kstat for non-removable media devices. USB and
30576  *     Firewire hard disks now have partition kstats
30577  *
30578  *      ------------------------------------------------------
30579  *       removable media    hotpluggable   |   kstat
30580  *      ------------------------------------------------------
30581  *             false          false        |    Yes
30582  *             false          true         |    Yes
30583  *             true             x          |    No
30584  *       ------------------------------------------------------
30585  *
30586  *
30587  * 13. Removable media & hotpluggable properties
30588  *
30589  *     Sd driver creates a "removable-media" property for removable media
30590  *     devices. Parent nexus drivers create a "hotpluggable" property if
30591  *     it supports hotplugging.
30592  *
30593  *     ---------------------------------------------------------------------
30594  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30595  *     ---------------------------------------------------------------------
30596  *       false            false       |    No                   No
30597  *       false            true        |    No                   Yes
30598  *       true             false       |    Yes                  No
30599  *       true             true        |    Yes                  Yes
30600  *     ---------------------------------------------------------------------
30601  *
30602  *
30603  * 14. Power Management
30604  *
30605  *     sd only power manages removable media devices or devices that support
30606  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30607  *
30608  *     A parent nexus that supports hotplugging can also set "pm-capable"
30609  *     if the disk can be power managed.
30610  *
30611  *     ------------------------------------------------------------
30612  *       removable media hotpluggable pm-capable  |   power manage
30613  *     ------------------------------------------------------------
30614  *             false          false     false     |     No
30615  *             false          false     true      |     Yes
30616  *             false          true      false     |     No
30617  *             false          true      true      |     Yes
30618  *             true             x        x        |     Yes
30619  *     ------------------------------------------------------------
30620  *
30621  *      USB and firewire hard disks can now be power managed independently
30622  *      of the framebuffer
30623  *
30624  *
30625  * 15. Support for USB disks with capacity larger than 1TB
30626  *
30627  *     Currently, sd doesn't permit a fixed disk device with capacity
30628  *     larger than 1TB to be used in a 32-bit operating system environment.
30629  *     However, sd doesn't do that for removable media devices. Instead, it
30630  *     assumes that removable media devices cannot have a capacity larger
30631  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30632  *     supported, which can cause some unexpected results.
30633  *
30634  *     ---------------------------------------------------------------------
30635  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30636  *     ---------------------------------------------------------------------
30637  *             false          false  |   true         |     no
30638  *             false          true   |   true         |     no
30639  *             true           false  |   true         |     Yes
30640  *             true           true   |   true         |     Yes
30641  *     ---------------------------------------------------------------------
30642  *
30643  *
30644  * 16. Check write-protection at open time
30645  *
30646  *     When a removable media device is being opened for writing without NDELAY
30647  *     flag, sd will check if this device is writable. If attempting to open
30648  *     without NDELAY flag a write-protected device, this operation will abort.
30649  *
30650  *     ------------------------------------------------------------
30651  *       removable media    USB/1394   |   WP Check
30652  *     ------------------------------------------------------------
30653  *             false          false    |     No
30654  *             false          true     |     No
30655  *             true           false    |     Yes
30656  *             true           true     |     Yes
30657  *     ------------------------------------------------------------
30658  *
30659  *
30660  * 17. syslog when corrupted VTOC is encountered
30661  *
30662  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30663  *      for fixed SCSI disks.
30664  *     ------------------------------------------------------------
30665  *       removable media    USB/1394   |   print syslog
30666  *     ------------------------------------------------------------
30667  *             false          false    |     Yes
30668  *             false          true     |     No
30669  *             true           false    |     No
30670  *             true           true     |     No
30671  *     ------------------------------------------------------------
30672  */
30673 static void
30674 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30675 {
30676 	int	pm_cap;
30677 
30678 	ASSERT(un->un_sd);
30679 	ASSERT(un->un_sd->sd_inq);
30680 
30681 	/*
30682 	 * Enable SYNC CACHE support for all devices.
30683 	 */
30684 	un->un_f_sync_cache_supported = TRUE;
30685 
30686 	/*
30687 	 * Set the sync cache required flag to false.
30688 	 * This would ensure that there is no SYNC CACHE
30689 	 * sent when there are no writes
30690 	 */
30691 	un->un_f_sync_cache_required = FALSE;
30692 
30693 	if (un->un_sd->sd_inq->inq_rmb) {
30694 		/*
30695 		 * The media of this device is removable. And for this kind
30696 		 * of devices, it is possible to change medium after opening
30697 		 * devices. Thus we should support this operation.
30698 		 */
30699 		un->un_f_has_removable_media = TRUE;
30700 
30701 		/*
30702 		 * support non-512-byte blocksize of removable media devices
30703 		 */
30704 		un->un_f_non_devbsize_supported = TRUE;
30705 
30706 		/*
30707 		 * Assume that all removable media devices support DOOR_LOCK
30708 		 */
30709 		un->un_f_doorlock_supported = TRUE;
30710 
30711 		/*
30712 		 * For a removable media device, it is possible to be opened
30713 		 * with NDELAY flag when there is no media in drive, in this
30714 		 * case we don't care if device is writable. But if without
30715 		 * NDELAY flag, we need to check if media is write-protected.
30716 		 */
30717 		un->un_f_chk_wp_open = TRUE;
30718 
30719 		/*
30720 		 * need to start a SCSI watch thread to monitor media state,
30721 		 * when media is being inserted or ejected, notify syseventd.
30722 		 */
30723 		un->un_f_monitor_media_state = TRUE;
30724 
30725 		/*
30726 		 * Some devices don't support START_STOP_UNIT command.
30727 		 * Therefore, we'd better check if a device supports it
30728 		 * before sending it.
30729 		 */
30730 		un->un_f_check_start_stop = TRUE;
30731 
30732 		/*
30733 		 * support eject media ioctl:
30734 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
30735 		 */
30736 		un->un_f_eject_media_supported = TRUE;
30737 
30738 		/*
30739 		 * Because many removable-media devices don't support
30740 		 * LOG_SENSE, we couldn't use this command to check if
30741 		 * a removable media device support power-management.
30742 		 * We assume that they support power-management via
30743 		 * START_STOP_UNIT command and can be spun up and down
30744 		 * without limitations.
30745 		 */
30746 		un->un_f_pm_supported = TRUE;
30747 
30748 		/*
30749 		 * Need to create a zero length (Boolean) property
30750 		 * removable-media for the removable media devices.
30751 		 * Note that the return value of the property is not being
30752 		 * checked, since if unable to create the property
30753 		 * then do not want the attach to fail altogether. Consistent
30754 		 * with other property creation in attach.
30755 		 */
30756 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30757 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30758 
30759 	} else {
30760 		/*
30761 		 * create device ID for device
30762 		 */
30763 		un->un_f_devid_supported = TRUE;
30764 
30765 		/*
30766 		 * Spin up non-removable-media devices once it is attached
30767 		 */
30768 		un->un_f_attach_spinup = TRUE;
30769 
30770 		/*
30771 		 * According to SCSI specification, Sense data has two kinds of
30772 		 * format: fixed format, and descriptor format. At present, we
30773 		 * don't support descriptor format sense data for removable
30774 		 * media.
30775 		 */
30776 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30777 			un->un_f_descr_format_supported = TRUE;
30778 		}
30779 
30780 		/*
30781 		 * kstats are created only for non-removable media devices.
30782 		 *
30783 		 * Set this in sd.conf to 0 in order to disable kstats.  The
30784 		 * default is 1, so they are enabled by default.
30785 		 */
30786 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30787 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
30788 		    "enable-partition-kstats", 1));
30789 
30790 		/*
30791 		 * Check if HBA has set the "pm-capable" property.
30792 		 * If "pm-capable" exists and is non-zero then we can
30793 		 * power manage the device without checking the start/stop
30794 		 * cycle count log sense page.
30795 		 *
30796 		 * If "pm-capable" exists and is set to be false (0),
30797 		 * then we should not power manage the device.
30798 		 *
30799 		 * If "pm-capable" doesn't exist then pm_cap will
30800 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
30801 		 * sd will check the start/stop cycle count log sense page
30802 		 * and power manage the device if the cycle count limit has
30803 		 * not been exceeded.
30804 		 */
30805 		pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30806 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30807 		if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30808 			un->un_f_log_sense_supported = TRUE;
30809 			if (!un->un_f_power_condition_disabled &&
30810 			    SD_INQUIRY(un)->inq_ansi == 6) {
30811 				un->un_f_power_condition_supported = TRUE;
30812 			}
30813 		} else {
30814 			/*
30815 			 * pm-capable property exists.
30816 			 *
30817 			 * Convert "TRUE" values for pm_cap to
30818 			 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30819 			 * later. "TRUE" values are any values defined in
30820 			 * inquiry.h.
30821 			 */
30822 			if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30823 				un->un_f_log_sense_supported = FALSE;
30824 			} else {
30825 				/* SD_PM_CAPABLE_IS_TRUE case */
30826 				un->un_f_pm_supported = TRUE;
30827 				if (!un->un_f_power_condition_disabled &&
30828 				    SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30829 					un->un_f_power_condition_supported =
30830 					    TRUE;
30831 				}
30832 				if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30833 					un->un_f_log_sense_supported = TRUE;
30834 					un->un_f_pm_log_sense_smart =
30835 					    SD_PM_CAP_SMART_LOG(pm_cap);
30836 				}
30837 			}
30838 
30839 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
30840 			    "sd_unit_attach: un:0x%p pm-capable "
30841 			    "property set to %d.\n", un, un->un_f_pm_supported);
30842 		}
30843 	}
30844 
30845 	if (un->un_f_is_hotpluggable) {
30846 
30847 		/*
30848 		 * Have to watch hotpluggable devices as well, since
30849 		 * that's the only way for userland applications to
30850 		 * detect hot removal while device is busy/mounted.
30851 		 */
30852 		un->un_f_monitor_media_state = TRUE;
30853 
30854 		un->un_f_check_start_stop = TRUE;
30855 
30856 	}
30857 }
30858 
30859 /*
30860  * sd_tg_rdwr:
30861  * Provides rdwr access for cmlb via sd_tgops. The start_block is
30862  * in sys block size, req_length in bytes.
30863  *
30864  */
30865 static int
30866 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
30867     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
30868 {
30869 	struct sd_lun *un;
30870 	int path_flag = (int)(uintptr_t)tg_cookie;
30871 	char *dkl = NULL;
30872 	diskaddr_t real_addr = start_block;
30873 	diskaddr_t first_byte, end_block;
30874 
30875 	size_t	buffer_size = reqlength;
30876 	int rval = 0;
30877 	diskaddr_t	cap;
30878 	uint32_t	lbasize;
30879 	sd_ssc_t	*ssc;
30880 
30881 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30882 	if (un == NULL)
30883 		return (ENXIO);
30884 
30885 	if (cmd != TG_READ && cmd != TG_WRITE)
30886 		return (EINVAL);
30887 
30888 	ssc = sd_ssc_init(un);
30889 	mutex_enter(SD_MUTEX(un));
30890 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
30891 		mutex_exit(SD_MUTEX(un));
30892 		rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30893 		    &lbasize, path_flag);
30894 		if (rval != 0)
30895 			goto done1;
30896 		mutex_enter(SD_MUTEX(un));
30897 		sd_update_block_info(un, lbasize, cap);
30898 		if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
30899 			mutex_exit(SD_MUTEX(un));
30900 			rval = EIO;
30901 			goto done;
30902 		}
30903 	}
30904 
30905 	if (NOT_DEVBSIZE(un)) {
30906 		/*
30907 		 * sys_blocksize != tgt_blocksize, need to re-adjust
30908 		 * blkno and save the index to beginning of dk_label
30909 		 */
30910 		first_byte  = SD_SYSBLOCKS2BYTES(start_block);
30911 		real_addr = first_byte / un->un_tgt_blocksize;
30912 
30913 		end_block = (first_byte + reqlength +
30914 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
30915 
30916 		/* round up buffer size to multiple of target block size */
30917 		buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
30918 
30919 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
30920 		    "label_addr: 0x%x allocation size: 0x%x\n",
30921 		    real_addr, buffer_size);
30922 
30923 		if (((first_byte % un->un_tgt_blocksize) != 0) ||
30924 		    (reqlength % un->un_tgt_blocksize) != 0)
30925 			/* the request is not aligned */
30926 			dkl = kmem_zalloc(buffer_size, KM_SLEEP);
30927 	}
30928 
30929 	/*
30930 	 * The MMC standard allows READ CAPACITY to be
30931 	 * inaccurate by a bounded amount (in the interest of
30932 	 * response latency).  As a result, failed READs are
30933 	 * commonplace (due to the reading of metadata and not
30934 	 * data). Depending on the per-Vendor/drive Sense data,
30935 	 * the failed READ can cause many (unnecessary) retries.
30936 	 */
30937 
30938 	if (ISCD(un) && (cmd == TG_READ) &&
30939 	    (un->un_f_blockcount_is_valid == TRUE) &&
30940 	    ((start_block == (un->un_blockcount - 1))||
30941 	    (start_block == (un->un_blockcount - 2)))) {
30942 			path_flag = SD_PATH_DIRECT_PRIORITY;
30943 	}
30944 
30945 	mutex_exit(SD_MUTEX(un));
30946 	if (cmd == TG_READ) {
30947 		rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
30948 		    buffer_size, real_addr, path_flag);
30949 		if (dkl != NULL)
30950 			bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
30951 			    real_addr), bufaddr, reqlength);
30952 	} else {
30953 		if (dkl) {
30954 			rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
30955 			    real_addr, path_flag);
30956 			if (rval) {
30957 				goto done1;
30958 			}
30959 			bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
30960 			    real_addr), reqlength);
30961 		}
30962 		rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
30963 		    buffer_size, real_addr, path_flag);
30964 	}
30965 
30966 done1:
30967 	if (dkl != NULL)
30968 		kmem_free(dkl, buffer_size);
30969 
30970 	if (rval != 0) {
30971 		if (rval == EIO)
30972 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
30973 		else
30974 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
30975 	}
30976 done:
30977 	sd_ssc_fini(ssc);
30978 	return (rval);
30979 }
30980 
30981 
30982 static int
30983 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
30984 {
30985 
30986 	struct sd_lun *un;
30987 	diskaddr_t	cap;
30988 	uint32_t	lbasize;
30989 	int		path_flag = (int)(uintptr_t)tg_cookie;
30990 	int		ret = 0;
30991 
30992 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30993 	if (un == NULL)
30994 		return (ENXIO);
30995 
30996 	switch (cmd) {
30997 	case TG_GETPHYGEOM:
30998 	case TG_GETVIRTGEOM:
30999 	case TG_GETCAPACITY:
31000 	case TG_GETBLOCKSIZE:
31001 		mutex_enter(SD_MUTEX(un));
31002 
31003 		if ((un->un_f_blockcount_is_valid == TRUE) &&
31004 		    (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31005 			cap = un->un_blockcount;
31006 			lbasize = un->un_tgt_blocksize;
31007 			mutex_exit(SD_MUTEX(un));
31008 		} else {
31009 			sd_ssc_t	*ssc;
31010 			mutex_exit(SD_MUTEX(un));
31011 			ssc = sd_ssc_init(un);
31012 			ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31013 			    &lbasize, path_flag);
31014 			if (ret != 0) {
31015 				if (ret == EIO)
31016 					sd_ssc_assessment(ssc,
31017 					    SD_FMT_STATUS_CHECK);
31018 				else
31019 					sd_ssc_assessment(ssc,
31020 					    SD_FMT_IGNORE);
31021 				sd_ssc_fini(ssc);
31022 				return (ret);
31023 			}
31024 			sd_ssc_fini(ssc);
31025 			mutex_enter(SD_MUTEX(un));
31026 			sd_update_block_info(un, lbasize, cap);
31027 			if ((un->un_f_blockcount_is_valid == FALSE) ||
31028 			    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31029 				mutex_exit(SD_MUTEX(un));
31030 				return (EIO);
31031 			}
31032 			mutex_exit(SD_MUTEX(un));
31033 		}
31034 
31035 		if (cmd == TG_GETCAPACITY) {
31036 			*(diskaddr_t *)arg = cap;
31037 			return (0);
31038 		}
31039 
31040 		if (cmd == TG_GETBLOCKSIZE) {
31041 			*(uint32_t *)arg = lbasize;
31042 			return (0);
31043 		}
31044 
31045 		if (cmd == TG_GETPHYGEOM)
31046 			ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31047 			    cap, lbasize, path_flag);
31048 		else
31049 			/* TG_GETVIRTGEOM */
31050 			ret = sd_get_virtual_geometry(un,
31051 			    (cmlb_geom_t *)arg, cap, lbasize);
31052 
31053 		return (ret);
31054 
31055 	case TG_GETATTR:
31056 		mutex_enter(SD_MUTEX(un));
31057 		((tg_attribute_t *)arg)->media_is_writable =
31058 		    un->un_f_mmc_writable_media;
31059 		((tg_attribute_t *)arg)->media_is_solid_state =
31060 		    un->un_f_is_solid_state;
31061 		mutex_exit(SD_MUTEX(un));
31062 		return (0);
31063 	default:
31064 		return (ENOTTY);
31065 
31066 	}
31067 }
31068 
31069 /*
31070  *    Function: sd_ssc_ereport_post
31071  *
31072  * Description: Will be called when SD driver need to post an ereport.
31073  *
31074  *    Context: Kernel thread or interrupt context.
31075  */
31076 static void
31077 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31078 {
31079 	int uscsi_path_instance = 0;
31080 	uchar_t	uscsi_pkt_reason;
31081 	uint32_t uscsi_pkt_state;
31082 	uint32_t uscsi_pkt_statistics;
31083 	uint64_t uscsi_ena;
31084 	uchar_t op_code;
31085 	uint8_t *sensep;
31086 	union scsi_cdb *cdbp;
31087 	uint_t cdblen = 0;
31088 	uint_t senlen = 0;
31089 	struct sd_lun *un;
31090 	dev_info_t *dip;
31091 	char *devid;
31092 	int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31093 	    SSC_FLAGS_INVALID_STATUS |
31094 	    SSC_FLAGS_INVALID_SENSE |
31095 	    SSC_FLAGS_INVALID_DATA;
31096 	char assessment[16];
31097 
31098 	ASSERT(ssc != NULL);
31099 	ASSERT(ssc->ssc_uscsi_cmd != NULL);
31100 	ASSERT(ssc->ssc_uscsi_info != NULL);
31101 
31102 	un = ssc->ssc_un;
31103 	ASSERT(un != NULL);
31104 
31105 	dip = un->un_sd->sd_dev;
31106 
31107 	/*
31108 	 * Get the devid:
31109 	 *	devid will only be passed to non-transport error reports.
31110 	 */
31111 	devid = DEVI(dip)->devi_devid_str;
31112 
31113 	/*
31114 	 * If we are syncing or dumping, the command will not be executed
31115 	 * so we bypass this situation.
31116 	 */
31117 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31118 	    (un->un_state == SD_STATE_DUMPING))
31119 		return;
31120 
31121 	uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31122 	uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31123 	uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31124 	uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31125 	uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31126 
31127 	sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31128 	cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31129 
31130 	/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31131 	if (cdbp == NULL) {
31132 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31133 		    "sd_ssc_ereport_post meet empty cdb\n");
31134 		return;
31135 	}
31136 
31137 	op_code = cdbp->scc_cmd;
31138 
31139 	cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31140 	senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31141 	    ssc->ssc_uscsi_cmd->uscsi_rqresid);
31142 
31143 	if (senlen > 0)
31144 		ASSERT(sensep != NULL);
31145 
31146 	/*
31147 	 * Initialize drv_assess to corresponding values.
31148 	 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31149 	 * on the sense-key returned back.
31150 	 */
31151 	switch (drv_assess) {
31152 		case SD_FM_DRV_RECOVERY:
31153 			(void) sprintf(assessment, "%s", "recovered");
31154 			break;
31155 		case SD_FM_DRV_RETRY:
31156 			(void) sprintf(assessment, "%s", "retry");
31157 			break;
31158 		case SD_FM_DRV_NOTICE:
31159 			(void) sprintf(assessment, "%s", "info");
31160 			break;
31161 		case SD_FM_DRV_FATAL:
31162 		default:
31163 			(void) sprintf(assessment, "%s", "unknown");
31164 	}
31165 	/*
31166 	 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31167 	 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31168 	 * driver-assessment will always be "recovered" here.
31169 	 */
31170 	if (drv_assess == SD_FM_DRV_RECOVERY) {
31171 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31172 		    "cmd.disk.recovered", uscsi_ena, devid, DDI_NOSLEEP,
31173 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31174 		    "driver-assessment", DATA_TYPE_STRING, assessment,
31175 		    "op-code", DATA_TYPE_UINT8, op_code,
31176 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31177 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31178 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31179 		    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31180 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31181 		    NULL);
31182 		return;
31183 	}
31184 
31185 	/*
31186 	 * If there is un-expected/un-decodable data, we should post
31187 	 * ereport.io.scsi.cmd.disk.dev.uderr.
31188 	 * driver-assessment will be set based on parameter drv_assess.
31189 	 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31190 	 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31191 	 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31192 	 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31193 	 */
31194 	if (ssc->ssc_flags & ssc_invalid_flags) {
31195 		if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31196 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31197 			    "cmd.disk.dev.uderr", uscsi_ena, devid, DDI_NOSLEEP,
31198 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31199 			    "driver-assessment", DATA_TYPE_STRING,
31200 			    drv_assess == SD_FM_DRV_FATAL ?
31201 			    "fail" : assessment,
31202 			    "op-code", DATA_TYPE_UINT8, op_code,
31203 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31204 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31205 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31206 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31207 			    "pkt-stats", DATA_TYPE_UINT32,
31208 			    uscsi_pkt_statistics,
31209 			    "stat-code", DATA_TYPE_UINT8,
31210 			    ssc->ssc_uscsi_cmd->uscsi_status,
31211 			    "un-decode-info", DATA_TYPE_STRING,
31212 			    ssc->ssc_info,
31213 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31214 			    senlen, sensep,
31215 			    NULL);
31216 		} else {
31217 			/*
31218 			 * For other type of invalid data, the
31219 			 * un-decode-value field would be empty because the
31220 			 * un-decodable content could be seen from upper
31221 			 * level payload or inside un-decode-info.
31222 			 */
31223 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31224 			    "cmd.disk.dev.uderr", uscsi_ena, devid, DDI_NOSLEEP,
31225 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31226 			    "driver-assessment", DATA_TYPE_STRING,
31227 			    drv_assess == SD_FM_DRV_FATAL ?
31228 			    "fail" : assessment,
31229 			    "op-code", DATA_TYPE_UINT8, op_code,
31230 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31231 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31232 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31233 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31234 			    "pkt-stats", DATA_TYPE_UINT32,
31235 			    uscsi_pkt_statistics,
31236 			    "stat-code", DATA_TYPE_UINT8,
31237 			    ssc->ssc_uscsi_cmd->uscsi_status,
31238 			    "un-decode-info", DATA_TYPE_STRING,
31239 			    ssc->ssc_info,
31240 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31241 			    0, NULL,
31242 			    NULL);
31243 		}
31244 		ssc->ssc_flags &= ~ssc_invalid_flags;
31245 		return;
31246 	}
31247 
31248 	if (uscsi_pkt_reason != CMD_CMPLT ||
31249 	    (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31250 		/*
31251 		 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31252 		 * set inside sd_start_cmds due to errors(bad packet or
31253 		 * fatal transport error), we should take it as a
31254 		 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31255 		 * driver-assessment will be set based on drv_assess.
31256 		 * We will set devid to NULL because it is a transport
31257 		 * error.
31258 		 */
31259 		if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31260 			ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31261 
31262 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31263 		    "cmd.disk.tran", uscsi_ena, NULL, DDI_NOSLEEP, FM_VERSION,
31264 		    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31265 		    "driver-assessment", DATA_TYPE_STRING,
31266 		    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31267 		    "op-code", DATA_TYPE_UINT8, op_code,
31268 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31269 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31270 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31271 		    "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31272 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31273 		    NULL);
31274 	} else {
31275 		/*
31276 		 * If we got here, we have a completed command, and we need
31277 		 * to further investigate the sense data to see what kind
31278 		 * of ereport we should post.
31279 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr
31280 		 * if sense-key == 0x3.
31281 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31282 		 * driver-assessment will be set based on the parameter
31283 		 * drv_assess.
31284 		 */
31285 		if (senlen > 0) {
31286 			/*
31287 			 * Here we have sense data available.
31288 			 */
31289 			uint8_t sense_key;
31290 			sense_key = scsi_sense_key(sensep);
31291 			if (sense_key == 0x3) {
31292 				/*
31293 				 * sense-key == 0x3(medium error),
31294 				 * driver-assessment should be "fatal" if
31295 				 * drv_assess is SD_FM_DRV_FATAL.
31296 				 */
31297 				scsi_fm_ereport_post(un->un_sd,
31298 				    uscsi_path_instance,
31299 				    "cmd.disk.dev.rqs.merr",
31300 				    uscsi_ena, devid, DDI_NOSLEEP, FM_VERSION,
31301 				    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31302 				    "driver-assessment",
31303 				    DATA_TYPE_STRING,
31304 				    drv_assess == SD_FM_DRV_FATAL ?
31305 				    "fatal" : assessment,
31306 				    "op-code",
31307 				    DATA_TYPE_UINT8, op_code,
31308 				    "cdb",
31309 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31310 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31311 				    "pkt-reason",
31312 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31313 				    "pkt-state",
31314 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31315 				    "pkt-stats",
31316 				    DATA_TYPE_UINT32,
31317 				    uscsi_pkt_statistics,
31318 				    "stat-code",
31319 				    DATA_TYPE_UINT8,
31320 				    ssc->ssc_uscsi_cmd->uscsi_status,
31321 				    "key",
31322 				    DATA_TYPE_UINT8,
31323 				    scsi_sense_key(sensep),
31324 				    "asc",
31325 				    DATA_TYPE_UINT8,
31326 				    scsi_sense_asc(sensep),
31327 				    "ascq",
31328 				    DATA_TYPE_UINT8,
31329 				    scsi_sense_ascq(sensep),
31330 				    "sense-data",
31331 				    DATA_TYPE_UINT8_ARRAY,
31332 				    senlen, sensep,
31333 				    "lba",
31334 				    DATA_TYPE_UINT64,
31335 				    ssc->ssc_uscsi_info->ui_lba,
31336 				    NULL);
31337 				} else {
31338 					/*
31339 					 * if sense-key == 0x4(hardware
31340 					 * error), driver-assessment should
31341 					 * be "fatal" if drv_assess is
31342 					 * SD_FM_DRV_FATAL.
31343 					 */
31344 					scsi_fm_ereport_post(un->un_sd,
31345 					    uscsi_path_instance,
31346 					    "cmd.disk.dev.rqs.derr",
31347 					    uscsi_ena, devid, DDI_NOSLEEP,
31348 					    FM_VERSION,
31349 					    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31350 					    "driver-assessment",
31351 					    DATA_TYPE_STRING,
31352 					    drv_assess == SD_FM_DRV_FATAL ?
31353 					    (sense_key == 0x4 ?
31354 					    "fatal" : "fail") : assessment,
31355 					    "op-code",
31356 					    DATA_TYPE_UINT8, op_code,
31357 					    "cdb",
31358 					    DATA_TYPE_UINT8_ARRAY, cdblen,
31359 					    ssc->ssc_uscsi_cmd->uscsi_cdb,
31360 					    "pkt-reason",
31361 					    DATA_TYPE_UINT8, uscsi_pkt_reason,
31362 					    "pkt-state",
31363 					    DATA_TYPE_UINT8, uscsi_pkt_state,
31364 					    "pkt-stats",
31365 					    DATA_TYPE_UINT32,
31366 					    uscsi_pkt_statistics,
31367 					    "stat-code",
31368 					    DATA_TYPE_UINT8,
31369 					    ssc->ssc_uscsi_cmd->uscsi_status,
31370 					    "key",
31371 					    DATA_TYPE_UINT8,
31372 					    scsi_sense_key(sensep),
31373 					    "asc",
31374 					    DATA_TYPE_UINT8,
31375 					    scsi_sense_asc(sensep),
31376 					    "ascq",
31377 					    DATA_TYPE_UINT8,
31378 					    scsi_sense_ascq(sensep),
31379 					    "sense-data",
31380 					    DATA_TYPE_UINT8_ARRAY,
31381 					    senlen, sensep,
31382 					    NULL);
31383 				}
31384 		} else {
31385 			/*
31386 			 * For stat_code == STATUS_GOOD, this is not a
31387 			 * hardware error.
31388 			 */
31389 			if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31390 				return;
31391 
31392 			/*
31393 			 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31394 			 * stat-code but with sense data unavailable.
31395 			 * driver-assessment will be set based on parameter
31396 			 * drv_assess.
31397 			 */
31398 			scsi_fm_ereport_post(un->un_sd,
31399 			    uscsi_path_instance, "cmd.disk.dev.serr", uscsi_ena,
31400 			    devid, DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8,
31401 			    FM_EREPORT_VERS0,
31402 			    "driver-assessment", DATA_TYPE_STRING,
31403 			    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31404 			    "op-code", DATA_TYPE_UINT8, op_code,
31405 			    "cdb",
31406 			    DATA_TYPE_UINT8_ARRAY,
31407 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31408 			    "pkt-reason",
31409 			    DATA_TYPE_UINT8, uscsi_pkt_reason,
31410 			    "pkt-state",
31411 			    DATA_TYPE_UINT8, uscsi_pkt_state,
31412 			    "pkt-stats",
31413 			    DATA_TYPE_UINT32, uscsi_pkt_statistics,
31414 			    "stat-code",
31415 			    DATA_TYPE_UINT8,
31416 			    ssc->ssc_uscsi_cmd->uscsi_status,
31417 			    NULL);
31418 		}
31419 	}
31420 }
31421 
31422 /*
31423  *     Function: sd_ssc_extract_info
31424  *
31425  * Description: Extract information available to help generate ereport.
31426  *
31427  *     Context: Kernel thread or interrupt context.
31428  */
31429 static void
31430 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31431     struct buf *bp, struct sd_xbuf *xp)
31432 {
31433 	size_t senlen = 0;
31434 	union scsi_cdb *cdbp;
31435 	int path_instance;
31436 	/*
31437 	 * Need scsi_cdb_size array to determine the cdb length.
31438 	 */
31439 	extern uchar_t	scsi_cdb_size[];
31440 
31441 	ASSERT(un != NULL);
31442 	ASSERT(pktp != NULL);
31443 	ASSERT(bp != NULL);
31444 	ASSERT(xp != NULL);
31445 	ASSERT(ssc != NULL);
31446 	ASSERT(mutex_owned(SD_MUTEX(un)));
31447 
31448 	/*
31449 	 * Transfer the cdb buffer pointer here.
31450 	 */
31451 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31452 
31453 	ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31454 	ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31455 
31456 	/*
31457 	 * Transfer the sense data buffer pointer if sense data is available,
31458 	 * calculate the sense data length first.
31459 	 */
31460 	if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31461 	    (xp->xb_sense_state & STATE_ARQ_DONE)) {
31462 		/*
31463 		 * For arq case, we will enter here.
31464 		 */
31465 		if (xp->xb_sense_state & STATE_XARQ_DONE) {
31466 			senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31467 		} else {
31468 			senlen = SENSE_LENGTH;
31469 		}
31470 	} else {
31471 		/*
31472 		 * For non-arq case, we will enter this branch.
31473 		 */
31474 		if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31475 		    (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31476 			senlen = SENSE_LENGTH - xp->xb_sense_resid;
31477 		}
31478 
31479 	}
31480 
31481 	ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31482 	ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31483 	ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31484 
31485 	ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31486 
31487 	/*
31488 	 * Only transfer path_instance when scsi_pkt was properly allocated.
31489 	 */
31490 	path_instance = pktp->pkt_path_instance;
31491 	if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31492 		ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31493 	else
31494 		ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31495 
31496 	/*
31497 	 * Copy in the other fields we may need when posting ereport.
31498 	 */
31499 	ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31500 	ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31501 	ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31502 	ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31503 
31504 	/*
31505 	 * For partially read/write command, we will not create ena
31506 	 * in case of a successful command be reconized as recovered.
31507 	 */
31508 	if ((pktp->pkt_reason == CMD_CMPLT) &&
31509 	    (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31510 	    (senlen == 0)) {
31511 		return;
31512 	}
31513 
31514 	/*
31515 	 * To associate ereports of a single command execution flow, we
31516 	 * need a shared ena for a specific command.
31517 	 */
31518 	if (xp->xb_ena == 0)
31519 		xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31520 	ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31521 }
31522 
31523 
31524 /*
31525  *     Function: sd_check_solid_state
31526  *
31527  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31528  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31529  *              RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the
31530  *              device is a solid state drive.
31531  *
31532  *     Context: Kernel thread or interrupt context.
31533  */
31534 
31535 static void
31536 sd_check_solid_state(sd_ssc_t *ssc)
31537 {
31538 	int		rval		= 0;
31539 	uchar_t		*inqb1		= NULL;
31540 	size_t		inqb1_len	= MAX_INQUIRY_SIZE;
31541 	size_t		inqb1_resid	= 0;
31542 	struct sd_lun	*un;
31543 
31544 	ASSERT(ssc != NULL);
31545 	un = ssc->ssc_un;
31546 	ASSERT(un != NULL);
31547 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31548 
31549 	mutex_enter(SD_MUTEX(un));
31550 	un->un_f_is_solid_state = FALSE;
31551 
31552 	if (ISCD(un)) {
31553 		mutex_exit(SD_MUTEX(un));
31554 		return;
31555 	}
31556 
31557 	if (sd_check_vpd_page_support(ssc) == 0 &&
31558 	    un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31559 		mutex_exit(SD_MUTEX(un));
31560 		/* collect page b1 data */
31561 		inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31562 
31563 		rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31564 		    0x01, 0xB1, &inqb1_resid);
31565 
31566 		if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31567 			SD_TRACE(SD_LOG_COMMON, un,
31568 			    "sd_check_solid_state: \
31569 			    successfully get VPD page: %x \
31570 			    PAGE LENGTH: %x BYTE 4: %x \
31571 			    BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31572 			    inqb1[5]);
31573 
31574 			mutex_enter(SD_MUTEX(un));
31575 			/*
31576 			 * Check the MEDIUM ROTATION RATE. If it is set
31577 			 * to 1, the device is a solid state drive.
31578 			 */
31579 			if (inqb1[4] == 0 && inqb1[5] == 1) {
31580 				un->un_f_is_solid_state = TRUE;
31581 			}
31582 			mutex_exit(SD_MUTEX(un));
31583 		} else if (rval != 0) {
31584 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31585 		}
31586 
31587 		kmem_free(inqb1, inqb1_len);
31588 	} else {
31589 		mutex_exit(SD_MUTEX(un));
31590 	}
31591 }
31592