xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 15db2897)
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 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16755 		    "Command failed to complete...Device is gone\n");
16756 		if (un->un_mediastate != DKIO_DEV_GONE) {
16757 			un->un_mediastate = DKIO_DEV_GONE;
16758 			cv_broadcast(&un->un_state_cv);
16759 		}
16760 		/*
16761 		 * If the command happens to be the REQUEST SENSE command,
16762 		 * free up the rqs buf and fail the original command.
16763 		 */
16764 		if (bp == un->un_rqs_bp) {
16765 			bp = sd_mark_rqs_idle(un, xp);
16766 		}
16767 		sd_return_failed_command(un, bp, EIO);
16768 		goto exit;
16769 	}
16770 
16771 	if (pktp->pkt_state & STATE_XARQ_DONE) {
16772 		SD_TRACE(SD_LOG_COMMON, un,
16773 		    "sdintr: extra sense data received. pkt=%p\n", pktp);
16774 	}
16775 
16776 	/*
16777 	 * First see if the pkt has auto-request sense data with it....
16778 	 * Look at the packet state first so we don't take a performance
16779 	 * hit looking at the arq enabled flag unless absolutely necessary.
16780 	 */
16781 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16782 	    (un->un_f_arq_enabled == TRUE)) {
16783 		/*
16784 		 * The HBA did an auto request sense for this command so check
16785 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16786 		 * driver command that should not be retried.
16787 		 */
16788 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16789 			/*
16790 			 * Save the relevant sense info into the xp for the
16791 			 * original cmd.
16792 			 */
16793 			struct scsi_arq_status *asp;
16794 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16795 			xp->xb_sense_status =
16796 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16797 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16798 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16799 			if (pktp->pkt_state & STATE_XARQ_DONE) {
16800 				actual_len = MAX_SENSE_LENGTH -
16801 				    xp->xb_sense_resid;
16802 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16803 				    MAX_SENSE_LENGTH);
16804 			} else {
16805 				if (xp->xb_sense_resid > SENSE_LENGTH) {
16806 					actual_len = MAX_SENSE_LENGTH -
16807 					    xp->xb_sense_resid;
16808 				} else {
16809 					actual_len = SENSE_LENGTH -
16810 					    xp->xb_sense_resid;
16811 				}
16812 				if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16813 					if ((((struct uscsi_cmd *)
16814 					    (xp->xb_pktinfo))->uscsi_rqlen) >
16815 					    actual_len) {
16816 						xp->xb_sense_resid =
16817 						    (((struct uscsi_cmd *)
16818 						    (xp->xb_pktinfo))->
16819 						    uscsi_rqlen) - actual_len;
16820 					} else {
16821 						xp->xb_sense_resid = 0;
16822 					}
16823 				}
16824 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16825 				    SENSE_LENGTH);
16826 			}
16827 
16828 			/* fail the command */
16829 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16830 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16831 			sd_return_failed_command(un, bp, EIO);
16832 			goto exit;
16833 		}
16834 
16835 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16836 		/*
16837 		 * We want to either retry or fail this command, so free
16838 		 * the DMA resources here.  If we retry the command then
16839 		 * the DMA resources will be reallocated in sd_start_cmds().
16840 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16841 		 * causes the *entire* transfer to start over again from the
16842 		 * beginning of the request, even for PARTIAL chunks that
16843 		 * have already transferred successfully.
16844 		 */
16845 		if ((un->un_f_is_fibre == TRUE) &&
16846 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16847 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16848 			scsi_dmafree(pktp);
16849 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16850 		}
16851 #endif
16852 
16853 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16854 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16855 
16856 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16857 		goto exit;
16858 	}
16859 
16860 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16861 	if (pktp->pkt_flags & FLAG_SENSING)  {
16862 		/* This pktp is from the unit's REQUEST_SENSE command */
16863 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16864 		    "sdintr: sd_handle_request_sense\n");
16865 		sd_handle_request_sense(un, bp, xp, pktp);
16866 		goto exit;
16867 	}
16868 
16869 	/*
16870 	 * Check to see if the command successfully completed as requested;
16871 	 * this is the most common case (and also the hot performance path).
16872 	 *
16873 	 * Requirements for successful completion are:
16874 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16875 	 * In addition:
16876 	 * - A residual of zero indicates successful completion no matter what
16877 	 *   the command is.
16878 	 * - If the residual is not zero and the command is not a read or
16879 	 *   write, then it's still defined as successful completion. In other
16880 	 *   words, if the command is a read or write the residual must be
16881 	 *   zero for successful completion.
16882 	 * - If the residual is not zero and the command is a read or
16883 	 *   write, and it's a USCSICMD, then it's still defined as
16884 	 *   successful completion.
16885 	 */
16886 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16887 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16888 
16889 		/*
16890 		 * Since this command is returned with a good status, we
16891 		 * can reset the count for Sonoma failover.
16892 		 */
16893 		un->un_sonoma_failure_count = 0;
16894 
16895 		/*
16896 		 * Return all USCSI commands on good status
16897 		 */
16898 		if (pktp->pkt_resid == 0) {
16899 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16900 			    "sdintr: returning command for resid == 0\n");
16901 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16902 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16903 			SD_UPDATE_B_RESID(bp, pktp);
16904 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16905 			    "sdintr: returning command for resid != 0\n");
16906 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16907 			SD_UPDATE_B_RESID(bp, pktp);
16908 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16909 			    "sdintr: returning uscsi command\n");
16910 		} else {
16911 			goto not_successful;
16912 		}
16913 		sd_return_command(un, bp);
16914 
16915 		/*
16916 		 * Decrement counter to indicate that the callback routine
16917 		 * is done.
16918 		 */
16919 		un->un_in_callback--;
16920 		ASSERT(un->un_in_callback >= 0);
16921 		mutex_exit(SD_MUTEX(un));
16922 
16923 		return;
16924 	}
16925 
16926 not_successful:
16927 
16928 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16929 	/*
16930 	 * The following is based upon knowledge of the underlying transport
16931 	 * and its use of DMA resources.  This code should be removed when
16932 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16933 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16934 	 * and sd_start_cmds().
16935 	 *
16936 	 * Free any DMA resources associated with this command if there
16937 	 * is a chance it could be retried or enqueued for later retry.
16938 	 * If we keep the DMA binding then mpxio cannot reissue the
16939 	 * command on another path whenever a path failure occurs.
16940 	 *
16941 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16942 	 * causes the *entire* transfer to start over again from the
16943 	 * beginning of the request, even for PARTIAL chunks that
16944 	 * have already transferred successfully.
16945 	 *
16946 	 * This is only done for non-uscsi commands (and also skipped for the
16947 	 * driver's internal RQS command). Also just do this for Fibre Channel
16948 	 * devices as these are the only ones that support mpxio.
16949 	 */
16950 	if ((un->un_f_is_fibre == TRUE) &&
16951 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16952 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16953 		scsi_dmafree(pktp);
16954 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16955 	}
16956 #endif
16957 
16958 	/*
16959 	 * The command did not successfully complete as requested so check
16960 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16961 	 * driver command that should not be retried so just return. If
16962 	 * FLAG_DIAGNOSE is not set the error will be processed below.
16963 	 */
16964 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16965 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16966 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16967 		/*
16968 		 * Issue a request sense if a check condition caused the error
16969 		 * (we handle the auto request sense case above), otherwise
16970 		 * just fail the command.
16971 		 */
16972 		if ((pktp->pkt_reason == CMD_CMPLT) &&
16973 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16974 			sd_send_request_sense_command(un, bp, pktp);
16975 		} else {
16976 			sd_return_failed_command(un, bp, EIO);
16977 		}
16978 		goto exit;
16979 	}
16980 
16981 	/*
16982 	 * The command did not successfully complete as requested so process
16983 	 * the error, retry, and/or attempt recovery.
16984 	 */
16985 	switch (pktp->pkt_reason) {
16986 	case CMD_CMPLT:
16987 		switch (SD_GET_PKT_STATUS(pktp)) {
16988 		case STATUS_GOOD:
16989 			/*
16990 			 * The command completed successfully with a non-zero
16991 			 * residual
16992 			 */
16993 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16994 			    "sdintr: STATUS_GOOD \n");
16995 			sd_pkt_status_good(un, bp, xp, pktp);
16996 			break;
16997 
16998 		case STATUS_CHECK:
16999 		case STATUS_TERMINATED:
17000 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17001 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17002 			sd_pkt_status_check_condition(un, bp, xp, pktp);
17003 			break;
17004 
17005 		case STATUS_BUSY:
17006 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17007 			    "sdintr: STATUS_BUSY\n");
17008 			sd_pkt_status_busy(un, bp, xp, pktp);
17009 			break;
17010 
17011 		case STATUS_RESERVATION_CONFLICT:
17012 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17013 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
17014 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17015 			break;
17016 
17017 		case STATUS_QFULL:
17018 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17019 			    "sdintr: STATUS_QFULL\n");
17020 			sd_pkt_status_qfull(un, bp, xp, pktp);
17021 			break;
17022 
17023 		case STATUS_MET:
17024 		case STATUS_INTERMEDIATE:
17025 		case STATUS_SCSI2:
17026 		case STATUS_INTERMEDIATE_MET:
17027 		case STATUS_ACA_ACTIVE:
17028 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17029 			    "Unexpected SCSI status received: 0x%x\n",
17030 			    SD_GET_PKT_STATUS(pktp));
17031 			/*
17032 			 * Mark the ssc_flags when detected invalid status
17033 			 * code for non-USCSI command.
17034 			 */
17035 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17036 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17037 				    0, "stat-code");
17038 			}
17039 			sd_return_failed_command(un, bp, EIO);
17040 			break;
17041 
17042 		default:
17043 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17044 			    "Invalid SCSI status received: 0x%x\n",
17045 			    SD_GET_PKT_STATUS(pktp));
17046 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17047 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17048 				    0, "stat-code");
17049 			}
17050 			sd_return_failed_command(un, bp, EIO);
17051 			break;
17052 
17053 		}
17054 		break;
17055 
17056 	case CMD_INCOMPLETE:
17057 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17058 		    "sdintr:  CMD_INCOMPLETE\n");
17059 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17060 		break;
17061 	case CMD_TRAN_ERR:
17062 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17063 		    "sdintr: CMD_TRAN_ERR\n");
17064 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17065 		break;
17066 	case CMD_RESET:
17067 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17068 		    "sdintr: CMD_RESET \n");
17069 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17070 		break;
17071 	case CMD_ABORTED:
17072 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17073 		    "sdintr: CMD_ABORTED \n");
17074 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17075 		break;
17076 	case CMD_TIMEOUT:
17077 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17078 		    "sdintr: CMD_TIMEOUT\n");
17079 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17080 		break;
17081 	case CMD_UNX_BUS_FREE:
17082 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17083 		    "sdintr: CMD_UNX_BUS_FREE \n");
17084 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17085 		break;
17086 	case CMD_TAG_REJECT:
17087 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17088 		    "sdintr: CMD_TAG_REJECT\n");
17089 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17090 		break;
17091 	default:
17092 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17093 		    "sdintr: default\n");
17094 		/*
17095 		 * Mark the ssc_flags for detecting invliad pkt_reason.
17096 		 */
17097 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17098 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17099 			    0, "pkt-reason");
17100 		}
17101 		sd_pkt_reason_default(un, bp, xp, pktp);
17102 		break;
17103 	}
17104 
17105 exit:
17106 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17107 
17108 	/* Decrement counter to indicate that the callback routine is done. */
17109 	un->un_in_callback--;
17110 	ASSERT(un->un_in_callback >= 0);
17111 
17112 	/*
17113 	 * At this point, the pkt has been dispatched, ie, it is either
17114 	 * being re-tried or has been returned to its caller and should
17115 	 * not be referenced.
17116 	 */
17117 
17118 	mutex_exit(SD_MUTEX(un));
17119 }
17120 
17121 
17122 /*
17123  *    Function: sd_print_incomplete_msg
17124  *
17125  * Description: Prints the error message for a CMD_INCOMPLETE error.
17126  *
17127  *   Arguments: un - ptr to associated softstate for the device.
17128  *		bp - ptr to the buf(9S) for the command.
17129  *		arg - message string ptr
17130  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17131  *			or SD_NO_RETRY_ISSUED.
17132  *
17133  *     Context: May be called under interrupt context
17134  */
17135 
17136 static void
17137 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17138 {
17139 	struct scsi_pkt	*pktp;
17140 	char	*msgp;
17141 	char	*cmdp = arg;
17142 
17143 	ASSERT(un != NULL);
17144 	ASSERT(mutex_owned(SD_MUTEX(un)));
17145 	ASSERT(bp != NULL);
17146 	ASSERT(arg != NULL);
17147 	pktp = SD_GET_PKTP(bp);
17148 	ASSERT(pktp != NULL);
17149 
17150 	switch (code) {
17151 	case SD_DELAYED_RETRY_ISSUED:
17152 	case SD_IMMEDIATE_RETRY_ISSUED:
17153 		msgp = "retrying";
17154 		break;
17155 	case SD_NO_RETRY_ISSUED:
17156 	default:
17157 		msgp = "giving up";
17158 		break;
17159 	}
17160 
17161 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17162 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17163 		    "incomplete %s- %s\n", cmdp, msgp);
17164 	}
17165 }
17166 
17167 
17168 
17169 /*
17170  *    Function: sd_pkt_status_good
17171  *
17172  * Description: Processing for a STATUS_GOOD code in pkt_status.
17173  *
17174  *     Context: May be called under interrupt context
17175  */
17176 
17177 static void
17178 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17179 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17180 {
17181 	char	*cmdp;
17182 
17183 	ASSERT(un != NULL);
17184 	ASSERT(mutex_owned(SD_MUTEX(un)));
17185 	ASSERT(bp != NULL);
17186 	ASSERT(xp != NULL);
17187 	ASSERT(pktp != NULL);
17188 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17189 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17190 	ASSERT(pktp->pkt_resid != 0);
17191 
17192 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17193 
17194 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17195 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17196 	case SCMD_READ:
17197 		cmdp = "read";
17198 		break;
17199 	case SCMD_WRITE:
17200 		cmdp = "write";
17201 		break;
17202 	default:
17203 		SD_UPDATE_B_RESID(bp, pktp);
17204 		sd_return_command(un, bp);
17205 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17206 		return;
17207 	}
17208 
17209 	/*
17210 	 * See if we can retry the read/write, preferrably immediately.
17211 	 * If retries are exhaused, then sd_retry_command() will update
17212 	 * the b_resid count.
17213 	 */
17214 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17215 	    cmdp, EIO, (clock_t)0, NULL);
17216 
17217 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17218 }
17219 
17220 
17221 
17222 
17223 
17224 /*
17225  *    Function: sd_handle_request_sense
17226  *
17227  * Description: Processing for non-auto Request Sense command.
17228  *
17229  *   Arguments: un - ptr to associated softstate
17230  *		sense_bp - ptr to buf(9S) for the RQS command
17231  *		sense_xp - ptr to the sd_xbuf for the RQS command
17232  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17233  *
17234  *     Context: May be called under interrupt context
17235  */
17236 
17237 static void
17238 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17239 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17240 {
17241 	struct buf	*cmd_bp;	/* buf for the original command */
17242 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17243 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17244 	size_t		actual_len;	/* actual sense data length */
17245 
17246 	ASSERT(un != NULL);
17247 	ASSERT(mutex_owned(SD_MUTEX(un)));
17248 	ASSERT(sense_bp != NULL);
17249 	ASSERT(sense_xp != NULL);
17250 	ASSERT(sense_pktp != NULL);
17251 
17252 	/*
17253 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17254 	 * RQS command and not the original command.
17255 	 */
17256 	ASSERT(sense_pktp == un->un_rqs_pktp);
17257 	ASSERT(sense_bp   == un->un_rqs_bp);
17258 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17259 	    (FLAG_SENSING | FLAG_HEAD));
17260 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17261 	    FLAG_SENSING) == FLAG_SENSING);
17262 
17263 	/* These are the bp, xp, and pktp for the original command */
17264 	cmd_bp = sense_xp->xb_sense_bp;
17265 	cmd_xp = SD_GET_XBUF(cmd_bp);
17266 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17267 
17268 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17269 		/*
17270 		 * The REQUEST SENSE command failed.  Release the REQUEST
17271 		 * SENSE command for re-use, get back the bp for the original
17272 		 * command, and attempt to re-try the original command if
17273 		 * FLAG_DIAGNOSE is not set in the original packet.
17274 		 */
17275 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17276 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17277 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17278 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17279 			    NULL, NULL, EIO, (clock_t)0, NULL);
17280 			return;
17281 		}
17282 	}
17283 
17284 	/*
17285 	 * Save the relevant sense info into the xp for the original cmd.
17286 	 *
17287 	 * Note: if the request sense failed the state info will be zero
17288 	 * as set in sd_mark_rqs_busy()
17289 	 */
17290 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17291 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17292 	actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17293 	if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17294 	    (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17295 	    SENSE_LENGTH)) {
17296 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17297 		    MAX_SENSE_LENGTH);
17298 		cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17299 	} else {
17300 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17301 		    SENSE_LENGTH);
17302 		if (actual_len < SENSE_LENGTH) {
17303 			cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17304 		} else {
17305 			cmd_xp->xb_sense_resid = 0;
17306 		}
17307 	}
17308 
17309 	/*
17310 	 *  Free up the RQS command....
17311 	 *  NOTE:
17312 	 *	Must do this BEFORE calling sd_validate_sense_data!
17313 	 *	sd_validate_sense_data may return the original command in
17314 	 *	which case the pkt will be freed and the flags can no
17315 	 *	longer be touched.
17316 	 *	SD_MUTEX is held through this process until the command
17317 	 *	is dispatched based upon the sense data, so there are
17318 	 *	no race conditions.
17319 	 */
17320 	(void) sd_mark_rqs_idle(un, sense_xp);
17321 
17322 	/*
17323 	 * For a retryable command see if we have valid sense data, if so then
17324 	 * turn it over to sd_decode_sense() to figure out the right course of
17325 	 * action. Just fail a non-retryable command.
17326 	 */
17327 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17328 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17329 		    SD_SENSE_DATA_IS_VALID) {
17330 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17331 		}
17332 	} else {
17333 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17334 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17335 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17336 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17337 		sd_return_failed_command(un, cmd_bp, EIO);
17338 	}
17339 }
17340 
17341 
17342 
17343 
17344 /*
17345  *    Function: sd_handle_auto_request_sense
17346  *
17347  * Description: Processing for auto-request sense information.
17348  *
17349  *   Arguments: un - ptr to associated softstate
17350  *		bp - ptr to buf(9S) for the command
17351  *		xp - ptr to the sd_xbuf for the command
17352  *		pktp - ptr to the scsi_pkt(9S) for the command
17353  *
17354  *     Context: May be called under interrupt context
17355  */
17356 
17357 static void
17358 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17359 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17360 {
17361 	struct scsi_arq_status *asp;
17362 	size_t actual_len;
17363 
17364 	ASSERT(un != NULL);
17365 	ASSERT(mutex_owned(SD_MUTEX(un)));
17366 	ASSERT(bp != NULL);
17367 	ASSERT(xp != NULL);
17368 	ASSERT(pktp != NULL);
17369 	ASSERT(pktp != un->un_rqs_pktp);
17370 	ASSERT(bp   != un->un_rqs_bp);
17371 
17372 	/*
17373 	 * For auto-request sense, we get a scsi_arq_status back from
17374 	 * the HBA, with the sense data in the sts_sensedata member.
17375 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17376 	 */
17377 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17378 
17379 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17380 		/*
17381 		 * The auto REQUEST SENSE failed; see if we can re-try
17382 		 * the original command.
17383 		 */
17384 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17385 		    "auto request sense failed (reason=%s)\n",
17386 		    scsi_rname(asp->sts_rqpkt_reason));
17387 
17388 		sd_reset_target(un, pktp);
17389 
17390 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17391 		    NULL, NULL, EIO, (clock_t)0, NULL);
17392 		return;
17393 	}
17394 
17395 	/* Save the relevant sense info into the xp for the original cmd. */
17396 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17397 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17398 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17399 	if (xp->xb_sense_state & STATE_XARQ_DONE) {
17400 		actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17401 		bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17402 		    MAX_SENSE_LENGTH);
17403 	} else {
17404 		if (xp->xb_sense_resid > SENSE_LENGTH) {
17405 			actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17406 		} else {
17407 			actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17408 		}
17409 		if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17410 			if ((((struct uscsi_cmd *)
17411 			    (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17412 				xp->xb_sense_resid = (((struct uscsi_cmd *)
17413 				    (xp->xb_pktinfo))->uscsi_rqlen) -
17414 				    actual_len;
17415 			} else {
17416 				xp->xb_sense_resid = 0;
17417 			}
17418 		}
17419 		bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17420 	}
17421 
17422 	/*
17423 	 * See if we have valid sense data, if so then turn it over to
17424 	 * sd_decode_sense() to figure out the right course of action.
17425 	 */
17426 	if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17427 	    SD_SENSE_DATA_IS_VALID) {
17428 		sd_decode_sense(un, bp, xp, pktp);
17429 	}
17430 }
17431 
17432 
17433 /*
17434  *    Function: sd_print_sense_failed_msg
17435  *
17436  * Description: Print log message when RQS has failed.
17437  *
17438  *   Arguments: un - ptr to associated softstate
17439  *		bp - ptr to buf(9S) for the command
17440  *		arg - generic message string ptr
17441  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17442  *			or SD_NO_RETRY_ISSUED
17443  *
17444  *     Context: May be called from interrupt context
17445  */
17446 
17447 static void
17448 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17449 	int code)
17450 {
17451 	char	*msgp = arg;
17452 
17453 	ASSERT(un != NULL);
17454 	ASSERT(mutex_owned(SD_MUTEX(un)));
17455 	ASSERT(bp != NULL);
17456 
17457 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17458 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17459 	}
17460 }
17461 
17462 
17463 /*
17464  *    Function: sd_validate_sense_data
17465  *
17466  * Description: Check the given sense data for validity.
17467  *		If the sense data is not valid, the command will
17468  *		be either failed or retried!
17469  *
17470  * Return Code: SD_SENSE_DATA_IS_INVALID
17471  *		SD_SENSE_DATA_IS_VALID
17472  *
17473  *     Context: May be called from interrupt context
17474  */
17475 
17476 static int
17477 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17478 	size_t actual_len)
17479 {
17480 	struct scsi_extended_sense *esp;
17481 	struct	scsi_pkt *pktp;
17482 	char	*msgp = NULL;
17483 	sd_ssc_t *sscp;
17484 
17485 	ASSERT(un != NULL);
17486 	ASSERT(mutex_owned(SD_MUTEX(un)));
17487 	ASSERT(bp != NULL);
17488 	ASSERT(bp != un->un_rqs_bp);
17489 	ASSERT(xp != NULL);
17490 	ASSERT(un->un_fm_private != NULL);
17491 
17492 	pktp = SD_GET_PKTP(bp);
17493 	ASSERT(pktp != NULL);
17494 
17495 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17496 	ASSERT(sscp != NULL);
17497 
17498 	/*
17499 	 * Check the status of the RQS command (auto or manual).
17500 	 */
17501 	switch (xp->xb_sense_status & STATUS_MASK) {
17502 	case STATUS_GOOD:
17503 		break;
17504 
17505 	case STATUS_RESERVATION_CONFLICT:
17506 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17507 		return (SD_SENSE_DATA_IS_INVALID);
17508 
17509 	case STATUS_BUSY:
17510 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17511 		    "Busy Status on REQUEST SENSE\n");
17512 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17513 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17514 		return (SD_SENSE_DATA_IS_INVALID);
17515 
17516 	case STATUS_QFULL:
17517 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17518 		    "QFULL Status on REQUEST SENSE\n");
17519 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17520 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17521 		return (SD_SENSE_DATA_IS_INVALID);
17522 
17523 	case STATUS_CHECK:
17524 	case STATUS_TERMINATED:
17525 		msgp = "Check Condition on REQUEST SENSE\n";
17526 		goto sense_failed;
17527 
17528 	default:
17529 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17530 		goto sense_failed;
17531 	}
17532 
17533 	/*
17534 	 * See if we got the minimum required amount of sense data.
17535 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17536 	 * or less.
17537 	 */
17538 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17539 	    (actual_len == 0)) {
17540 		msgp = "Request Sense couldn't get sense data\n";
17541 		goto sense_failed;
17542 	}
17543 
17544 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17545 		msgp = "Not enough sense information\n";
17546 		/* Mark the ssc_flags for detecting invalid sense data */
17547 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17548 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17549 			    "sense-data");
17550 		}
17551 		goto sense_failed;
17552 	}
17553 
17554 	/*
17555 	 * We require the extended sense data
17556 	 */
17557 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17558 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17559 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17560 			static char tmp[8];
17561 			static char buf[148];
17562 			char *p = (char *)(xp->xb_sense_data);
17563 			int i;
17564 
17565 			mutex_enter(&sd_sense_mutex);
17566 			(void) strcpy(buf, "undecodable sense information:");
17567 			for (i = 0; i < actual_len; i++) {
17568 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17569 				(void) strcpy(&buf[strlen(buf)], tmp);
17570 			}
17571 			i = strlen(buf);
17572 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17573 
17574 			if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17575 				scsi_log(SD_DEVINFO(un), sd_label,
17576 				    CE_WARN, buf);
17577 			}
17578 			mutex_exit(&sd_sense_mutex);
17579 		}
17580 
17581 		/* Mark the ssc_flags for detecting invalid sense data */
17582 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17583 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17584 			    "sense-data");
17585 		}
17586 
17587 		/* Note: Legacy behavior, fail the command with no retry */
17588 		sd_return_failed_command(un, bp, EIO);
17589 		return (SD_SENSE_DATA_IS_INVALID);
17590 	}
17591 
17592 	/*
17593 	 * Check that es_code is valid (es_class concatenated with es_code
17594 	 * make up the "response code" field.  es_class will always be 7, so
17595 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17596 	 * format.
17597 	 */
17598 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17599 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17600 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17601 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17602 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17603 		/* Mark the ssc_flags for detecting invalid sense data */
17604 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17605 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17606 			    "sense-data");
17607 		}
17608 		goto sense_failed;
17609 	}
17610 
17611 	return (SD_SENSE_DATA_IS_VALID);
17612 
17613 sense_failed:
17614 	/*
17615 	 * If the request sense failed (for whatever reason), attempt
17616 	 * to retry the original command.
17617 	 */
17618 #if defined(__i386) || defined(__amd64)
17619 	/*
17620 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17621 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17622 	 * for both SCSI/FC.
17623 	 * The SD_RETRY_DELAY value need to be adjusted here
17624 	 * when SD_RETRY_DELAY change in sddef.h
17625 	 */
17626 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17627 	    sd_print_sense_failed_msg, msgp, EIO,
17628 	    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17629 #else
17630 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17631 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17632 #endif
17633 
17634 	return (SD_SENSE_DATA_IS_INVALID);
17635 }
17636 
17637 /*
17638  *    Function: sd_decode_sense
17639  *
17640  * Description: Take recovery action(s) when SCSI Sense Data is received.
17641  *
17642  *     Context: Interrupt context.
17643  */
17644 
17645 static void
17646 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17647 	struct scsi_pkt *pktp)
17648 {
17649 	uint8_t sense_key;
17650 
17651 	ASSERT(un != NULL);
17652 	ASSERT(mutex_owned(SD_MUTEX(un)));
17653 	ASSERT(bp != NULL);
17654 	ASSERT(bp != un->un_rqs_bp);
17655 	ASSERT(xp != NULL);
17656 	ASSERT(pktp != NULL);
17657 
17658 	sense_key = scsi_sense_key(xp->xb_sense_data);
17659 
17660 	switch (sense_key) {
17661 	case KEY_NO_SENSE:
17662 		sd_sense_key_no_sense(un, bp, xp, pktp);
17663 		break;
17664 	case KEY_RECOVERABLE_ERROR:
17665 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17666 		    bp, xp, pktp);
17667 		break;
17668 	case KEY_NOT_READY:
17669 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17670 		    bp, xp, pktp);
17671 		break;
17672 	case KEY_MEDIUM_ERROR:
17673 	case KEY_HARDWARE_ERROR:
17674 		sd_sense_key_medium_or_hardware_error(un,
17675 		    xp->xb_sense_data, bp, xp, pktp);
17676 		break;
17677 	case KEY_ILLEGAL_REQUEST:
17678 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17679 		break;
17680 	case KEY_UNIT_ATTENTION:
17681 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17682 		    bp, xp, pktp);
17683 		break;
17684 	case KEY_WRITE_PROTECT:
17685 	case KEY_VOLUME_OVERFLOW:
17686 	case KEY_MISCOMPARE:
17687 		sd_sense_key_fail_command(un, bp, xp, pktp);
17688 		break;
17689 	case KEY_BLANK_CHECK:
17690 		sd_sense_key_blank_check(un, bp, xp, pktp);
17691 		break;
17692 	case KEY_ABORTED_COMMAND:
17693 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17694 		break;
17695 	case KEY_VENDOR_UNIQUE:
17696 	case KEY_COPY_ABORTED:
17697 	case KEY_EQUAL:
17698 	case KEY_RESERVED:
17699 	default:
17700 		sd_sense_key_default(un, xp->xb_sense_data,
17701 		    bp, xp, pktp);
17702 		break;
17703 	}
17704 }
17705 
17706 
17707 /*
17708  *    Function: sd_dump_memory
17709  *
17710  * Description: Debug logging routine to print the contents of a user provided
17711  *		buffer. The output of the buffer is broken up into 256 byte
17712  *		segments due to a size constraint of the scsi_log.
17713  *		implementation.
17714  *
17715  *   Arguments: un - ptr to softstate
17716  *		comp - component mask
17717  *		title - "title" string to preceed data when printed
17718  *		data - ptr to data block to be printed
17719  *		len - size of data block to be printed
17720  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17721  *
17722  *     Context: May be called from interrupt context
17723  */
17724 
17725 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17726 
17727 static char *sd_dump_format_string[] = {
17728 		" 0x%02x",
17729 		" %c"
17730 };
17731 
17732 static void
17733 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17734     int len, int fmt)
17735 {
17736 	int	i, j;
17737 	int	avail_count;
17738 	int	start_offset;
17739 	int	end_offset;
17740 	size_t	entry_len;
17741 	char	*bufp;
17742 	char	*local_buf;
17743 	char	*format_string;
17744 
17745 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17746 
17747 	/*
17748 	 * In the debug version of the driver, this function is called from a
17749 	 * number of places which are NOPs in the release driver.
17750 	 * The debug driver therefore has additional methods of filtering
17751 	 * debug output.
17752 	 */
17753 #ifdef SDDEBUG
17754 	/*
17755 	 * In the debug version of the driver we can reduce the amount of debug
17756 	 * messages by setting sd_error_level to something other than
17757 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17758 	 * sd_component_mask.
17759 	 */
17760 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17761 	    (sd_error_level != SCSI_ERR_ALL)) {
17762 		return;
17763 	}
17764 	if (((sd_component_mask & comp) == 0) ||
17765 	    (sd_error_level != SCSI_ERR_ALL)) {
17766 		return;
17767 	}
17768 #else
17769 	if (sd_error_level != SCSI_ERR_ALL) {
17770 		return;
17771 	}
17772 #endif
17773 
17774 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17775 	bufp = local_buf;
17776 	/*
17777 	 * Available length is the length of local_buf[], minus the
17778 	 * length of the title string, minus one for the ":", minus
17779 	 * one for the newline, minus one for the NULL terminator.
17780 	 * This gives the #bytes available for holding the printed
17781 	 * values from the given data buffer.
17782 	 */
17783 	if (fmt == SD_LOG_HEX) {
17784 		format_string = sd_dump_format_string[0];
17785 	} else /* SD_LOG_CHAR */ {
17786 		format_string = sd_dump_format_string[1];
17787 	}
17788 	/*
17789 	 * Available count is the number of elements from the given
17790 	 * data buffer that we can fit into the available length.
17791 	 * This is based upon the size of the format string used.
17792 	 * Make one entry and find it's size.
17793 	 */
17794 	(void) sprintf(bufp, format_string, data[0]);
17795 	entry_len = strlen(bufp);
17796 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17797 
17798 	j = 0;
17799 	while (j < len) {
17800 		bufp = local_buf;
17801 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17802 		start_offset = j;
17803 
17804 		end_offset = start_offset + avail_count;
17805 
17806 		(void) sprintf(bufp, "%s:", title);
17807 		bufp += strlen(bufp);
17808 		for (i = start_offset; ((i < end_offset) && (j < len));
17809 		    i++, j++) {
17810 			(void) sprintf(bufp, format_string, data[i]);
17811 			bufp += entry_len;
17812 		}
17813 		(void) sprintf(bufp, "\n");
17814 
17815 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17816 	}
17817 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17818 }
17819 
17820 /*
17821  *    Function: sd_print_sense_msg
17822  *
17823  * Description: Log a message based upon the given sense data.
17824  *
17825  *   Arguments: un - ptr to associated softstate
17826  *		bp - ptr to buf(9S) for the command
17827  *		arg - ptr to associate sd_sense_info struct
17828  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17829  *			or SD_NO_RETRY_ISSUED
17830  *
17831  *     Context: May be called from interrupt context
17832  */
17833 
17834 static void
17835 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17836 {
17837 	struct sd_xbuf	*xp;
17838 	struct scsi_pkt	*pktp;
17839 	uint8_t *sensep;
17840 	daddr_t request_blkno;
17841 	diskaddr_t err_blkno;
17842 	int severity;
17843 	int pfa_flag;
17844 	extern struct scsi_key_strings scsi_cmds[];
17845 
17846 	ASSERT(un != NULL);
17847 	ASSERT(mutex_owned(SD_MUTEX(un)));
17848 	ASSERT(bp != NULL);
17849 	xp = SD_GET_XBUF(bp);
17850 	ASSERT(xp != NULL);
17851 	pktp = SD_GET_PKTP(bp);
17852 	ASSERT(pktp != NULL);
17853 	ASSERT(arg != NULL);
17854 
17855 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17856 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17857 
17858 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17859 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17860 		severity = SCSI_ERR_RETRYABLE;
17861 	}
17862 
17863 	/* Use absolute block number for the request block number */
17864 	request_blkno = xp->xb_blkno;
17865 
17866 	/*
17867 	 * Now try to get the error block number from the sense data
17868 	 */
17869 	sensep = xp->xb_sense_data;
17870 
17871 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17872 	    (uint64_t *)&err_blkno)) {
17873 		/*
17874 		 * We retrieved the error block number from the information
17875 		 * portion of the sense data.
17876 		 *
17877 		 * For USCSI commands we are better off using the error
17878 		 * block no. as the requested block no. (This is the best
17879 		 * we can estimate.)
17880 		 */
17881 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17882 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17883 			request_blkno = err_blkno;
17884 		}
17885 	} else {
17886 		/*
17887 		 * Without the es_valid bit set (for fixed format) or an
17888 		 * information descriptor (for descriptor format) we cannot
17889 		 * be certain of the error blkno, so just use the
17890 		 * request_blkno.
17891 		 */
17892 		err_blkno = (diskaddr_t)request_blkno;
17893 	}
17894 
17895 	/*
17896 	 * The following will log the buffer contents for the release driver
17897 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17898 	 * level is set to verbose.
17899 	 */
17900 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17901 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17902 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17903 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17904 
17905 	if (pfa_flag == FALSE) {
17906 		/* This is normally only set for USCSI */
17907 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17908 			return;
17909 		}
17910 
17911 		if ((SD_IS_BUFIO(xp) == TRUE) &&
17912 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17913 		    (severity < sd_error_level))) {
17914 			return;
17915 		}
17916 	}
17917 	/*
17918 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
17919 	 */
17920 	if ((SD_IS_LSI(un)) &&
17921 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
17922 	    (scsi_sense_asc(sensep) == 0x94) &&
17923 	    (scsi_sense_ascq(sensep) == 0x01)) {
17924 		un->un_sonoma_failure_count++;
17925 		if (un->un_sonoma_failure_count > 1) {
17926 			return;
17927 		}
17928 	}
17929 
17930 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
17931 	    ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
17932 	    (pktp->pkt_resid == 0))) {
17933 		scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17934 		    request_blkno, err_blkno, scsi_cmds,
17935 		    (struct scsi_extended_sense *)sensep,
17936 		    un->un_additional_codes, NULL);
17937 	}
17938 }
17939 
17940 /*
17941  *    Function: sd_sense_key_no_sense
17942  *
17943  * Description: Recovery action when sense data was not received.
17944  *
17945  *     Context: May be called from interrupt context
17946  */
17947 
17948 static void
17949 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
17950 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17951 {
17952 	struct sd_sense_info	si;
17953 
17954 	ASSERT(un != NULL);
17955 	ASSERT(mutex_owned(SD_MUTEX(un)));
17956 	ASSERT(bp != NULL);
17957 	ASSERT(xp != NULL);
17958 	ASSERT(pktp != NULL);
17959 
17960 	si.ssi_severity = SCSI_ERR_FATAL;
17961 	si.ssi_pfa_flag = FALSE;
17962 
17963 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17964 
17965 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17966 	    &si, EIO, (clock_t)0, NULL);
17967 }
17968 
17969 
17970 /*
17971  *    Function: sd_sense_key_recoverable_error
17972  *
17973  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17974  *
17975  *     Context: May be called from interrupt context
17976  */
17977 
17978 static void
17979 sd_sense_key_recoverable_error(struct sd_lun *un,
17980 	uint8_t *sense_datap,
17981 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17982 {
17983 	struct sd_sense_info	si;
17984 	uint8_t asc = scsi_sense_asc(sense_datap);
17985 
17986 	ASSERT(un != NULL);
17987 	ASSERT(mutex_owned(SD_MUTEX(un)));
17988 	ASSERT(bp != NULL);
17989 	ASSERT(xp != NULL);
17990 	ASSERT(pktp != NULL);
17991 
17992 	/*
17993 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17994 	 */
17995 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17996 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17997 		si.ssi_severity = SCSI_ERR_INFO;
17998 		si.ssi_pfa_flag = TRUE;
17999 	} else {
18000 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
18001 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18002 		si.ssi_severity = SCSI_ERR_RECOVERED;
18003 		si.ssi_pfa_flag = FALSE;
18004 	}
18005 
18006 	if (pktp->pkt_resid == 0) {
18007 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18008 		sd_return_command(un, bp);
18009 		return;
18010 	}
18011 
18012 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18013 	    &si, EIO, (clock_t)0, NULL);
18014 }
18015 
18016 
18017 
18018 
18019 /*
18020  *    Function: sd_sense_key_not_ready
18021  *
18022  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18023  *
18024  *     Context: May be called from interrupt context
18025  */
18026 
18027 static void
18028 sd_sense_key_not_ready(struct sd_lun *un,
18029 	uint8_t *sense_datap,
18030 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18031 {
18032 	struct sd_sense_info	si;
18033 	uint8_t asc = scsi_sense_asc(sense_datap);
18034 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18035 
18036 	ASSERT(un != NULL);
18037 	ASSERT(mutex_owned(SD_MUTEX(un)));
18038 	ASSERT(bp != NULL);
18039 	ASSERT(xp != NULL);
18040 	ASSERT(pktp != NULL);
18041 
18042 	si.ssi_severity = SCSI_ERR_FATAL;
18043 	si.ssi_pfa_flag = FALSE;
18044 
18045 	/*
18046 	 * Update error stats after first NOT READY error. Disks may have
18047 	 * been powered down and may need to be restarted.  For CDROMs,
18048 	 * report NOT READY errors only if media is present.
18049 	 */
18050 	if ((ISCD(un) && (asc == 0x3A)) ||
18051 	    (xp->xb_nr_retry_count > 0)) {
18052 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18053 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18054 	}
18055 
18056 	/*
18057 	 * Just fail if the "not ready" retry limit has been reached.
18058 	 */
18059 	if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18060 		/* Special check for error message printing for removables. */
18061 		if (un->un_f_has_removable_media && (asc == 0x04) &&
18062 		    (ascq >= 0x04)) {
18063 			si.ssi_severity = SCSI_ERR_ALL;
18064 		}
18065 		goto fail_command;
18066 	}
18067 
18068 	/*
18069 	 * Check the ASC and ASCQ in the sense data as needed, to determine
18070 	 * what to do.
18071 	 */
18072 	switch (asc) {
18073 	case 0x04:	/* LOGICAL UNIT NOT READY */
18074 		/*
18075 		 * disk drives that don't spin up result in a very long delay
18076 		 * in format without warning messages. We will log a message
18077 		 * if the error level is set to verbose.
18078 		 */
18079 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18080 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18081 			    "logical unit not ready, resetting disk\n");
18082 		}
18083 
18084 		/*
18085 		 * There are different requirements for CDROMs and disks for
18086 		 * the number of retries.  If a CD-ROM is giving this, it is
18087 		 * probably reading TOC and is in the process of getting
18088 		 * ready, so we should keep on trying for a long time to make
18089 		 * sure that all types of media are taken in account (for
18090 		 * some media the drive takes a long time to read TOC).  For
18091 		 * disks we do not want to retry this too many times as this
18092 		 * can cause a long hang in format when the drive refuses to
18093 		 * spin up (a very common failure).
18094 		 */
18095 		switch (ascq) {
18096 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18097 			/*
18098 			 * Disk drives frequently refuse to spin up which
18099 			 * results in a very long hang in format without
18100 			 * warning messages.
18101 			 *
18102 			 * Note: This code preserves the legacy behavior of
18103 			 * comparing xb_nr_retry_count against zero for fibre
18104 			 * channel targets instead of comparing against the
18105 			 * un_reset_retry_count value.  The reason for this
18106 			 * discrepancy has been so utterly lost beneath the
18107 			 * Sands of Time that even Indiana Jones could not
18108 			 * find it.
18109 			 */
18110 			if (un->un_f_is_fibre == TRUE) {
18111 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18112 				    (xp->xb_nr_retry_count > 0)) &&
18113 				    (un->un_startstop_timeid == NULL)) {
18114 					scsi_log(SD_DEVINFO(un), sd_label,
18115 					    CE_WARN, "logical unit not ready, "
18116 					    "resetting disk\n");
18117 					sd_reset_target(un, pktp);
18118 				}
18119 			} else {
18120 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18121 				    (xp->xb_nr_retry_count >
18122 				    un->un_reset_retry_count)) &&
18123 				    (un->un_startstop_timeid == NULL)) {
18124 					scsi_log(SD_DEVINFO(un), sd_label,
18125 					    CE_WARN, "logical unit not ready, "
18126 					    "resetting disk\n");
18127 					sd_reset_target(un, pktp);
18128 				}
18129 			}
18130 			break;
18131 
18132 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18133 			/*
18134 			 * If the target is in the process of becoming
18135 			 * ready, just proceed with the retry. This can
18136 			 * happen with CD-ROMs that take a long time to
18137 			 * read TOC after a power cycle or reset.
18138 			 */
18139 			goto do_retry;
18140 
18141 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18142 			break;
18143 
18144 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18145 			/*
18146 			 * Retries cannot help here so just fail right away.
18147 			 */
18148 			goto fail_command;
18149 
18150 		case 0x88:
18151 			/*
18152 			 * Vendor-unique code for T3/T4: it indicates a
18153 			 * path problem in a mutipathed config, but as far as
18154 			 * the target driver is concerned it equates to a fatal
18155 			 * error, so we should just fail the command right away
18156 			 * (without printing anything to the console). If this
18157 			 * is not a T3/T4, fall thru to the default recovery
18158 			 * action.
18159 			 * T3/T4 is FC only, don't need to check is_fibre
18160 			 */
18161 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
18162 				sd_return_failed_command(un, bp, EIO);
18163 				return;
18164 			}
18165 			/* FALLTHRU */
18166 
18167 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18168 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18169 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18170 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18171 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18172 		default:    /* Possible future codes in SCSI spec? */
18173 			/*
18174 			 * For removable-media devices, do not retry if
18175 			 * ASCQ > 2 as these result mostly from USCSI commands
18176 			 * on MMC devices issued to check status of an
18177 			 * operation initiated in immediate mode.  Also for
18178 			 * ASCQ >= 4 do not print console messages as these
18179 			 * mainly represent a user-initiated operation
18180 			 * instead of a system failure.
18181 			 */
18182 			if (un->un_f_has_removable_media) {
18183 				si.ssi_severity = SCSI_ERR_ALL;
18184 				goto fail_command;
18185 			}
18186 			break;
18187 		}
18188 
18189 		/*
18190 		 * As part of our recovery attempt for the NOT READY
18191 		 * condition, we issue a START STOP UNIT command. However
18192 		 * we want to wait for a short delay before attempting this
18193 		 * as there may still be more commands coming back from the
18194 		 * target with the check condition. To do this we use
18195 		 * timeout(9F) to call sd_start_stop_unit_callback() after
18196 		 * the delay interval expires. (sd_start_stop_unit_callback()
18197 		 * dispatches sd_start_stop_unit_task(), which will issue
18198 		 * the actual START STOP UNIT command. The delay interval
18199 		 * is one-half of the delay that we will use to retry the
18200 		 * command that generated the NOT READY condition.
18201 		 *
18202 		 * Note that we could just dispatch sd_start_stop_unit_task()
18203 		 * from here and allow it to sleep for the delay interval,
18204 		 * but then we would be tying up the taskq thread
18205 		 * uncesessarily for the duration of the delay.
18206 		 *
18207 		 * Do not issue the START STOP UNIT if the current command
18208 		 * is already a START STOP UNIT.
18209 		 */
18210 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18211 			break;
18212 		}
18213 
18214 		/*
18215 		 * Do not schedule the timeout if one is already pending.
18216 		 */
18217 		if (un->un_startstop_timeid != NULL) {
18218 			SD_INFO(SD_LOG_ERROR, un,
18219 			    "sd_sense_key_not_ready: restart already issued to"
18220 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18221 			    ddi_get_instance(SD_DEVINFO(un)));
18222 			break;
18223 		}
18224 
18225 		/*
18226 		 * Schedule the START STOP UNIT command, then queue the command
18227 		 * for a retry.
18228 		 *
18229 		 * Note: A timeout is not scheduled for this retry because we
18230 		 * want the retry to be serial with the START_STOP_UNIT. The
18231 		 * retry will be started when the START_STOP_UNIT is completed
18232 		 * in sd_start_stop_unit_task.
18233 		 */
18234 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18235 		    un, un->un_busy_timeout / 2);
18236 		xp->xb_nr_retry_count++;
18237 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18238 		return;
18239 
18240 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18241 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18242 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18243 			    "unit does not respond to selection\n");
18244 		}
18245 		break;
18246 
18247 	case 0x3A:	/* MEDIUM NOT PRESENT */
18248 		if (sd_error_level >= SCSI_ERR_FATAL) {
18249 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18250 			    "Caddy not inserted in drive\n");
18251 		}
18252 
18253 		sr_ejected(un);
18254 		un->un_mediastate = DKIO_EJECTED;
18255 		/* The state has changed, inform the media watch routines */
18256 		cv_broadcast(&un->un_state_cv);
18257 		/* Just fail if no media is present in the drive. */
18258 		goto fail_command;
18259 
18260 	default:
18261 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18262 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18263 			    "Unit not Ready. Additional sense code 0x%x\n",
18264 			    asc);
18265 		}
18266 		break;
18267 	}
18268 
18269 do_retry:
18270 
18271 	/*
18272 	 * Retry the command, as some targets may report NOT READY for
18273 	 * several seconds after being reset.
18274 	 */
18275 	xp->xb_nr_retry_count++;
18276 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18277 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18278 	    &si, EIO, un->un_busy_timeout, NULL);
18279 
18280 	return;
18281 
18282 fail_command:
18283 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18284 	sd_return_failed_command(un, bp, EIO);
18285 }
18286 
18287 
18288 
18289 /*
18290  *    Function: sd_sense_key_medium_or_hardware_error
18291  *
18292  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18293  *		sense key.
18294  *
18295  *     Context: May be called from interrupt context
18296  */
18297 
18298 static void
18299 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18300 	uint8_t *sense_datap,
18301 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18302 {
18303 	struct sd_sense_info	si;
18304 	uint8_t sense_key = scsi_sense_key(sense_datap);
18305 	uint8_t asc = scsi_sense_asc(sense_datap);
18306 
18307 	ASSERT(un != NULL);
18308 	ASSERT(mutex_owned(SD_MUTEX(un)));
18309 	ASSERT(bp != NULL);
18310 	ASSERT(xp != NULL);
18311 	ASSERT(pktp != NULL);
18312 
18313 	si.ssi_severity = SCSI_ERR_FATAL;
18314 	si.ssi_pfa_flag = FALSE;
18315 
18316 	if (sense_key == KEY_MEDIUM_ERROR) {
18317 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18318 	}
18319 
18320 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18321 
18322 	if ((un->un_reset_retry_count != 0) &&
18323 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18324 		mutex_exit(SD_MUTEX(un));
18325 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18326 		if (un->un_f_allow_bus_device_reset == TRUE) {
18327 
18328 			boolean_t try_resetting_target = B_TRUE;
18329 
18330 			/*
18331 			 * We need to be able to handle specific ASC when we are
18332 			 * handling a KEY_HARDWARE_ERROR. In particular
18333 			 * taking the default action of resetting the target may
18334 			 * not be the appropriate way to attempt recovery.
18335 			 * Resetting a target because of a single LUN failure
18336 			 * victimizes all LUNs on that target.
18337 			 *
18338 			 * This is true for the LSI arrays, if an LSI
18339 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18340 			 * should trust it.
18341 			 */
18342 
18343 			if (sense_key == KEY_HARDWARE_ERROR) {
18344 				switch (asc) {
18345 				case 0x84:
18346 					if (SD_IS_LSI(un)) {
18347 						try_resetting_target = B_FALSE;
18348 					}
18349 					break;
18350 				default:
18351 					break;
18352 				}
18353 			}
18354 
18355 			if (try_resetting_target == B_TRUE) {
18356 				int reset_retval = 0;
18357 				if (un->un_f_lun_reset_enabled == TRUE) {
18358 					SD_TRACE(SD_LOG_IO_CORE, un,
18359 					    "sd_sense_key_medium_or_hardware_"
18360 					    "error: issuing RESET_LUN\n");
18361 					reset_retval =
18362 					    scsi_reset(SD_ADDRESS(un),
18363 					    RESET_LUN);
18364 				}
18365 				if (reset_retval == 0) {
18366 					SD_TRACE(SD_LOG_IO_CORE, un,
18367 					    "sd_sense_key_medium_or_hardware_"
18368 					    "error: issuing RESET_TARGET\n");
18369 					(void) scsi_reset(SD_ADDRESS(un),
18370 					    RESET_TARGET);
18371 				}
18372 			}
18373 		}
18374 		mutex_enter(SD_MUTEX(un));
18375 	}
18376 
18377 	/*
18378 	 * This really ought to be a fatal error, but we will retry anyway
18379 	 * as some drives report this as a spurious error.
18380 	 */
18381 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18382 	    &si, EIO, (clock_t)0, NULL);
18383 }
18384 
18385 
18386 
18387 /*
18388  *    Function: sd_sense_key_illegal_request
18389  *
18390  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18391  *
18392  *     Context: May be called from interrupt context
18393  */
18394 
18395 static void
18396 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18397 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18398 {
18399 	struct sd_sense_info	si;
18400 
18401 	ASSERT(un != NULL);
18402 	ASSERT(mutex_owned(SD_MUTEX(un)));
18403 	ASSERT(bp != NULL);
18404 	ASSERT(xp != NULL);
18405 	ASSERT(pktp != NULL);
18406 
18407 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18408 
18409 	si.ssi_severity = SCSI_ERR_INFO;
18410 	si.ssi_pfa_flag = FALSE;
18411 
18412 	/* Pointless to retry if the target thinks it's an illegal request */
18413 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18414 	sd_return_failed_command(un, bp, EIO);
18415 }
18416 
18417 
18418 
18419 
18420 /*
18421  *    Function: sd_sense_key_unit_attention
18422  *
18423  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18424  *
18425  *     Context: May be called from interrupt context
18426  */
18427 
18428 static void
18429 sd_sense_key_unit_attention(struct sd_lun *un,
18430 	uint8_t *sense_datap,
18431 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18432 {
18433 	/*
18434 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18435 	 * like Sonoma can return UNIT ATTENTION close to a minute
18436 	 * under certain conditions.
18437 	 */
18438 	int	retry_check_flag = SD_RETRIES_UA;
18439 	boolean_t	kstat_updated = B_FALSE;
18440 	struct	sd_sense_info		si;
18441 	uint8_t asc = scsi_sense_asc(sense_datap);
18442 	uint8_t	ascq = scsi_sense_ascq(sense_datap);
18443 
18444 	ASSERT(un != NULL);
18445 	ASSERT(mutex_owned(SD_MUTEX(un)));
18446 	ASSERT(bp != NULL);
18447 	ASSERT(xp != NULL);
18448 	ASSERT(pktp != NULL);
18449 
18450 	si.ssi_severity = SCSI_ERR_INFO;
18451 	si.ssi_pfa_flag = FALSE;
18452 
18453 
18454 	switch (asc) {
18455 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18456 		if (sd_report_pfa != 0) {
18457 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18458 			si.ssi_pfa_flag = TRUE;
18459 			retry_check_flag = SD_RETRIES_STANDARD;
18460 			goto do_retry;
18461 		}
18462 
18463 		break;
18464 
18465 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18466 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18467 			un->un_resvd_status |=
18468 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18469 		}
18470 #ifdef _LP64
18471 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18472 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18473 			    un, KM_NOSLEEP) == 0) {
18474 				/*
18475 				 * If we can't dispatch the task we'll just
18476 				 * live without descriptor sense.  We can
18477 				 * try again on the next "unit attention"
18478 				 */
18479 				SD_ERROR(SD_LOG_ERROR, un,
18480 				    "sd_sense_key_unit_attention: "
18481 				    "Could not dispatch "
18482 				    "sd_reenable_dsense_task\n");
18483 			}
18484 		}
18485 #endif /* _LP64 */
18486 		/* FALLTHRU */
18487 
18488 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18489 		if (!un->un_f_has_removable_media) {
18490 			break;
18491 		}
18492 
18493 		/*
18494 		 * When we get a unit attention from a removable-media device,
18495 		 * it may be in a state that will take a long time to recover
18496 		 * (e.g., from a reset).  Since we are executing in interrupt
18497 		 * context here, we cannot wait around for the device to come
18498 		 * back. So hand this command off to sd_media_change_task()
18499 		 * for deferred processing under taskq thread context. (Note
18500 		 * that the command still may be failed if a problem is
18501 		 * encountered at a later time.)
18502 		 */
18503 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18504 		    KM_NOSLEEP) == 0) {
18505 			/*
18506 			 * Cannot dispatch the request so fail the command.
18507 			 */
18508 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18509 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18510 			si.ssi_severity = SCSI_ERR_FATAL;
18511 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18512 			sd_return_failed_command(un, bp, EIO);
18513 		}
18514 
18515 		/*
18516 		 * If failed to dispatch sd_media_change_task(), we already
18517 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18518 		 * we should update kstat later if it encounters an error. So,
18519 		 * we update kstat_updated flag here.
18520 		 */
18521 		kstat_updated = B_TRUE;
18522 
18523 		/*
18524 		 * Either the command has been successfully dispatched to a
18525 		 * task Q for retrying, or the dispatch failed. In either case
18526 		 * do NOT retry again by calling sd_retry_command. This sets up
18527 		 * two retries of the same command and when one completes and
18528 		 * frees the resources the other will access freed memory,
18529 		 * a bad thing.
18530 		 */
18531 		return;
18532 
18533 	default:
18534 		break;
18535 	}
18536 
18537 	/*
18538 	 * ASC  ASCQ
18539 	 *  2A   09	Capacity data has changed
18540 	 *  2A   01	Mode parameters changed
18541 	 *  3F   0E	Reported luns data has changed
18542 	 * Arrays that support logical unit expansion should report
18543 	 * capacity changes(2Ah/09). Mode parameters changed and
18544 	 * reported luns data has changed are the approximation.
18545 	 */
18546 	if (((asc == 0x2a) && (ascq == 0x09)) ||
18547 	    ((asc == 0x2a) && (ascq == 0x01)) ||
18548 	    ((asc == 0x3f) && (ascq == 0x0e))) {
18549 		if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18550 		    KM_NOSLEEP) == 0) {
18551 			SD_ERROR(SD_LOG_ERROR, un,
18552 			    "sd_sense_key_unit_attention: "
18553 			    "Could not dispatch sd_target_change_task\n");
18554 		}
18555 	}
18556 
18557 	/*
18558 	 * Update kstat if we haven't done that.
18559 	 */
18560 	if (!kstat_updated) {
18561 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18562 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18563 	}
18564 
18565 do_retry:
18566 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18567 	    EIO, SD_UA_RETRY_DELAY, NULL);
18568 }
18569 
18570 
18571 
18572 /*
18573  *    Function: sd_sense_key_fail_command
18574  *
18575  * Description: Use to fail a command when we don't like the sense key that
18576  *		was returned.
18577  *
18578  *     Context: May be called from interrupt context
18579  */
18580 
18581 static void
18582 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18583 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18584 {
18585 	struct sd_sense_info	si;
18586 
18587 	ASSERT(un != NULL);
18588 	ASSERT(mutex_owned(SD_MUTEX(un)));
18589 	ASSERT(bp != NULL);
18590 	ASSERT(xp != NULL);
18591 	ASSERT(pktp != NULL);
18592 
18593 	si.ssi_severity = SCSI_ERR_FATAL;
18594 	si.ssi_pfa_flag = FALSE;
18595 
18596 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18597 	sd_return_failed_command(un, bp, EIO);
18598 }
18599 
18600 
18601 
18602 /*
18603  *    Function: sd_sense_key_blank_check
18604  *
18605  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18606  *		Has no monetary connotation.
18607  *
18608  *     Context: May be called from interrupt context
18609  */
18610 
18611 static void
18612 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18613 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18614 {
18615 	struct sd_sense_info	si;
18616 
18617 	ASSERT(un != NULL);
18618 	ASSERT(mutex_owned(SD_MUTEX(un)));
18619 	ASSERT(bp != NULL);
18620 	ASSERT(xp != NULL);
18621 	ASSERT(pktp != NULL);
18622 
18623 	/*
18624 	 * Blank check is not fatal for removable devices, therefore
18625 	 * it does not require a console message.
18626 	 */
18627 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18628 	    SCSI_ERR_FATAL;
18629 	si.ssi_pfa_flag = FALSE;
18630 
18631 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18632 	sd_return_failed_command(un, bp, EIO);
18633 }
18634 
18635 
18636 
18637 
18638 /*
18639  *    Function: sd_sense_key_aborted_command
18640  *
18641  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18642  *
18643  *     Context: May be called from interrupt context
18644  */
18645 
18646 static void
18647 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18648 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18649 {
18650 	struct sd_sense_info	si;
18651 
18652 	ASSERT(un != NULL);
18653 	ASSERT(mutex_owned(SD_MUTEX(un)));
18654 	ASSERT(bp != NULL);
18655 	ASSERT(xp != NULL);
18656 	ASSERT(pktp != NULL);
18657 
18658 	si.ssi_severity = SCSI_ERR_FATAL;
18659 	si.ssi_pfa_flag = FALSE;
18660 
18661 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18662 
18663 	/*
18664 	 * This really ought to be a fatal error, but we will retry anyway
18665 	 * as some drives report this as a spurious error.
18666 	 */
18667 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18668 	    &si, EIO, drv_usectohz(100000), NULL);
18669 }
18670 
18671 
18672 
18673 /*
18674  *    Function: sd_sense_key_default
18675  *
18676  * Description: Default recovery action for several SCSI sense keys (basically
18677  *		attempts a retry).
18678  *
18679  *     Context: May be called from interrupt context
18680  */
18681 
18682 static void
18683 sd_sense_key_default(struct sd_lun *un,
18684 	uint8_t *sense_datap,
18685 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18686 {
18687 	struct sd_sense_info	si;
18688 	uint8_t sense_key = scsi_sense_key(sense_datap);
18689 
18690 	ASSERT(un != NULL);
18691 	ASSERT(mutex_owned(SD_MUTEX(un)));
18692 	ASSERT(bp != NULL);
18693 	ASSERT(xp != NULL);
18694 	ASSERT(pktp != NULL);
18695 
18696 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18697 
18698 	/*
18699 	 * Undecoded sense key.	Attempt retries and hope that will fix
18700 	 * the problem.  Otherwise, we're dead.
18701 	 */
18702 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18703 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18704 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18705 	}
18706 
18707 	si.ssi_severity = SCSI_ERR_FATAL;
18708 	si.ssi_pfa_flag = FALSE;
18709 
18710 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18711 	    &si, EIO, (clock_t)0, NULL);
18712 }
18713 
18714 
18715 
18716 /*
18717  *    Function: sd_print_retry_msg
18718  *
18719  * Description: Print a message indicating the retry action being taken.
18720  *
18721  *   Arguments: un - ptr to associated softstate
18722  *		bp - ptr to buf(9S) for the command
18723  *		arg - not used.
18724  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18725  *			or SD_NO_RETRY_ISSUED
18726  *
18727  *     Context: May be called from interrupt context
18728  */
18729 /* ARGSUSED */
18730 static void
18731 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18732 {
18733 	struct sd_xbuf	*xp;
18734 	struct scsi_pkt *pktp;
18735 	char *reasonp;
18736 	char *msgp;
18737 
18738 	ASSERT(un != NULL);
18739 	ASSERT(mutex_owned(SD_MUTEX(un)));
18740 	ASSERT(bp != NULL);
18741 	pktp = SD_GET_PKTP(bp);
18742 	ASSERT(pktp != NULL);
18743 	xp = SD_GET_XBUF(bp);
18744 	ASSERT(xp != NULL);
18745 
18746 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18747 	mutex_enter(&un->un_pm_mutex);
18748 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18749 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18750 	    (pktp->pkt_flags & FLAG_SILENT)) {
18751 		mutex_exit(&un->un_pm_mutex);
18752 		goto update_pkt_reason;
18753 	}
18754 	mutex_exit(&un->un_pm_mutex);
18755 
18756 	/*
18757 	 * Suppress messages if they are all the same pkt_reason; with
18758 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18759 	 * If we are in panic, then suppress the retry messages.
18760 	 */
18761 	switch (flag) {
18762 	case SD_NO_RETRY_ISSUED:
18763 		msgp = "giving up";
18764 		break;
18765 	case SD_IMMEDIATE_RETRY_ISSUED:
18766 	case SD_DELAYED_RETRY_ISSUED:
18767 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18768 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18769 		    (sd_error_level != SCSI_ERR_ALL))) {
18770 			return;
18771 		}
18772 		msgp = "retrying command";
18773 		break;
18774 	default:
18775 		goto update_pkt_reason;
18776 	}
18777 
18778 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18779 	    scsi_rname(pktp->pkt_reason));
18780 
18781 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18782 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18783 		    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18784 	}
18785 
18786 update_pkt_reason:
18787 	/*
18788 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18789 	 * This is to prevent multiple console messages for the same failure
18790 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18791 	 * when the command is retried successfully because there still may be
18792 	 * more commands coming back with the same value of pktp->pkt_reason.
18793 	 */
18794 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18795 		un->un_last_pkt_reason = pktp->pkt_reason;
18796 	}
18797 }
18798 
18799 
18800 /*
18801  *    Function: sd_print_cmd_incomplete_msg
18802  *
18803  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18804  *
18805  *   Arguments: un - ptr to associated softstate
18806  *		bp - ptr to buf(9S) for the command
18807  *		arg - passed to sd_print_retry_msg()
18808  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18809  *			or SD_NO_RETRY_ISSUED
18810  *
18811  *     Context: May be called from interrupt context
18812  */
18813 
18814 static void
18815 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18816 	int code)
18817 {
18818 	dev_info_t	*dip;
18819 
18820 	ASSERT(un != NULL);
18821 	ASSERT(mutex_owned(SD_MUTEX(un)));
18822 	ASSERT(bp != NULL);
18823 
18824 	switch (code) {
18825 	case SD_NO_RETRY_ISSUED:
18826 		/* Command was failed. Someone turned off this target? */
18827 		if (un->un_state != SD_STATE_OFFLINE) {
18828 			/*
18829 			 * Suppress message if we are detaching and
18830 			 * device has been disconnected
18831 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18832 			 * private interface and not part of the DDI
18833 			 */
18834 			dip = un->un_sd->sd_dev;
18835 			if (!(DEVI_IS_DETACHING(dip) &&
18836 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18837 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18838 				"disk not responding to selection\n");
18839 			}
18840 			New_state(un, SD_STATE_OFFLINE);
18841 		}
18842 		break;
18843 
18844 	case SD_DELAYED_RETRY_ISSUED:
18845 	case SD_IMMEDIATE_RETRY_ISSUED:
18846 	default:
18847 		/* Command was successfully queued for retry */
18848 		sd_print_retry_msg(un, bp, arg, code);
18849 		break;
18850 	}
18851 }
18852 
18853 
18854 /*
18855  *    Function: sd_pkt_reason_cmd_incomplete
18856  *
18857  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18858  *
18859  *     Context: May be called from interrupt context
18860  */
18861 
18862 static void
18863 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18864 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18865 {
18866 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18867 
18868 	ASSERT(un != NULL);
18869 	ASSERT(mutex_owned(SD_MUTEX(un)));
18870 	ASSERT(bp != NULL);
18871 	ASSERT(xp != NULL);
18872 	ASSERT(pktp != NULL);
18873 
18874 	/* Do not do a reset if selection did not complete */
18875 	/* Note: Should this not just check the bit? */
18876 	if (pktp->pkt_state != STATE_GOT_BUS) {
18877 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18878 		sd_reset_target(un, pktp);
18879 	}
18880 
18881 	/*
18882 	 * If the target was not successfully selected, then set
18883 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18884 	 * with the target, and further retries and/or commands are
18885 	 * likely to take a long time.
18886 	 */
18887 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18888 		flag |= SD_RETRIES_FAILFAST;
18889 	}
18890 
18891 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18892 
18893 	sd_retry_command(un, bp, flag,
18894 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18895 }
18896 
18897 
18898 
18899 /*
18900  *    Function: sd_pkt_reason_cmd_tran_err
18901  *
18902  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18903  *
18904  *     Context: May be called from interrupt context
18905  */
18906 
18907 static void
18908 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18909 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18910 {
18911 	ASSERT(un != NULL);
18912 	ASSERT(mutex_owned(SD_MUTEX(un)));
18913 	ASSERT(bp != NULL);
18914 	ASSERT(xp != NULL);
18915 	ASSERT(pktp != NULL);
18916 
18917 	/*
18918 	 * Do not reset if we got a parity error, or if
18919 	 * selection did not complete.
18920 	 */
18921 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18922 	/* Note: Should this not just check the bit for pkt_state? */
18923 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18924 	    (pktp->pkt_state != STATE_GOT_BUS)) {
18925 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18926 		sd_reset_target(un, pktp);
18927 	}
18928 
18929 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18930 
18931 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18932 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18933 }
18934 
18935 
18936 
18937 /*
18938  *    Function: sd_pkt_reason_cmd_reset
18939  *
18940  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18941  *
18942  *     Context: May be called from interrupt context
18943  */
18944 
18945 static void
18946 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
18947 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18948 {
18949 	ASSERT(un != NULL);
18950 	ASSERT(mutex_owned(SD_MUTEX(un)));
18951 	ASSERT(bp != NULL);
18952 	ASSERT(xp != NULL);
18953 	ASSERT(pktp != NULL);
18954 
18955 	/* The target may still be running the command, so try to reset. */
18956 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18957 	sd_reset_target(un, pktp);
18958 
18959 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18960 
18961 	/*
18962 	 * If pkt_reason is CMD_RESET chances are that this pkt got
18963 	 * reset because another target on this bus caused it. The target
18964 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18965 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18966 	 */
18967 
18968 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18969 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18970 }
18971 
18972 
18973 
18974 
18975 /*
18976  *    Function: sd_pkt_reason_cmd_aborted
18977  *
18978  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18979  *
18980  *     Context: May be called from interrupt context
18981  */
18982 
18983 static void
18984 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
18985 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18986 {
18987 	ASSERT(un != NULL);
18988 	ASSERT(mutex_owned(SD_MUTEX(un)));
18989 	ASSERT(bp != NULL);
18990 	ASSERT(xp != NULL);
18991 	ASSERT(pktp != NULL);
18992 
18993 	/* The target may still be running the command, so try to reset. */
18994 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18995 	sd_reset_target(un, pktp);
18996 
18997 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18998 
18999 	/*
19000 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19001 	 * aborted because another target on this bus caused it. The target
19002 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19003 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19004 	 */
19005 
19006 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19007 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19008 }
19009 
19010 
19011 
19012 /*
19013  *    Function: sd_pkt_reason_cmd_timeout
19014  *
19015  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19016  *
19017  *     Context: May be called from interrupt context
19018  */
19019 
19020 static void
19021 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19022 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19023 {
19024 	ASSERT(un != NULL);
19025 	ASSERT(mutex_owned(SD_MUTEX(un)));
19026 	ASSERT(bp != NULL);
19027 	ASSERT(xp != NULL);
19028 	ASSERT(pktp != NULL);
19029 
19030 
19031 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19032 	sd_reset_target(un, pktp);
19033 
19034 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19035 
19036 	/*
19037 	 * A command timeout indicates that we could not establish
19038 	 * communication with the target, so set SD_RETRIES_FAILFAST
19039 	 * as further retries/commands are likely to take a long time.
19040 	 */
19041 	sd_retry_command(un, bp,
19042 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19043 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19044 }
19045 
19046 
19047 
19048 /*
19049  *    Function: sd_pkt_reason_cmd_unx_bus_free
19050  *
19051  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19052  *
19053  *     Context: May be called from interrupt context
19054  */
19055 
19056 static void
19057 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19058 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19059 {
19060 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19061 
19062 	ASSERT(un != NULL);
19063 	ASSERT(mutex_owned(SD_MUTEX(un)));
19064 	ASSERT(bp != NULL);
19065 	ASSERT(xp != NULL);
19066 	ASSERT(pktp != NULL);
19067 
19068 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19069 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19070 
19071 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19072 	    sd_print_retry_msg : NULL;
19073 
19074 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19075 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19076 }
19077 
19078 
19079 /*
19080  *    Function: sd_pkt_reason_cmd_tag_reject
19081  *
19082  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19083  *
19084  *     Context: May be called from interrupt context
19085  */
19086 
19087 static void
19088 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19089 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19090 {
19091 	ASSERT(un != NULL);
19092 	ASSERT(mutex_owned(SD_MUTEX(un)));
19093 	ASSERT(bp != NULL);
19094 	ASSERT(xp != NULL);
19095 	ASSERT(pktp != NULL);
19096 
19097 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19098 	pktp->pkt_flags = 0;
19099 	un->un_tagflags = 0;
19100 	if (un->un_f_opt_queueing == TRUE) {
19101 		un->un_throttle = min(un->un_throttle, 3);
19102 	} else {
19103 		un->un_throttle = 1;
19104 	}
19105 	mutex_exit(SD_MUTEX(un));
19106 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19107 	mutex_enter(SD_MUTEX(un));
19108 
19109 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19110 
19111 	/* Legacy behavior not to check retry counts here. */
19112 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19113 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19114 }
19115 
19116 
19117 /*
19118  *    Function: sd_pkt_reason_default
19119  *
19120  * Description: Default recovery actions for SCSA pkt_reason values that
19121  *		do not have more explicit recovery actions.
19122  *
19123  *     Context: May be called from interrupt context
19124  */
19125 
19126 static void
19127 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19128 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19129 {
19130 	ASSERT(un != NULL);
19131 	ASSERT(mutex_owned(SD_MUTEX(un)));
19132 	ASSERT(bp != NULL);
19133 	ASSERT(xp != NULL);
19134 	ASSERT(pktp != NULL);
19135 
19136 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19137 	sd_reset_target(un, pktp);
19138 
19139 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19140 
19141 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19142 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19143 }
19144 
19145 
19146 
19147 /*
19148  *    Function: sd_pkt_status_check_condition
19149  *
19150  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19151  *
19152  *     Context: May be called from interrupt context
19153  */
19154 
19155 static void
19156 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19157 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19158 {
19159 	ASSERT(un != NULL);
19160 	ASSERT(mutex_owned(SD_MUTEX(un)));
19161 	ASSERT(bp != NULL);
19162 	ASSERT(xp != NULL);
19163 	ASSERT(pktp != NULL);
19164 
19165 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19166 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
19167 
19168 	/*
19169 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19170 	 * command will be retried after the request sense). Otherwise, retry
19171 	 * the command. Note: we are issuing the request sense even though the
19172 	 * retry limit may have been reached for the failed command.
19173 	 */
19174 	if (un->un_f_arq_enabled == FALSE) {
19175 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19176 		    "no ARQ, sending request sense command\n");
19177 		sd_send_request_sense_command(un, bp, pktp);
19178 	} else {
19179 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19180 		    "ARQ,retrying request sense command\n");
19181 #if defined(__i386) || defined(__amd64)
19182 		/*
19183 		 * The SD_RETRY_DELAY value need to be adjusted here
19184 		 * when SD_RETRY_DELAY change in sddef.h
19185 		 */
19186 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19187 		    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19188 		    NULL);
19189 #else
19190 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19191 		    EIO, SD_RETRY_DELAY, NULL);
19192 #endif
19193 	}
19194 
19195 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19196 }
19197 
19198 
19199 /*
19200  *    Function: sd_pkt_status_busy
19201  *
19202  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19203  *
19204  *     Context: May be called from interrupt context
19205  */
19206 
19207 static void
19208 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19209 	struct scsi_pkt *pktp)
19210 {
19211 	ASSERT(un != NULL);
19212 	ASSERT(mutex_owned(SD_MUTEX(un)));
19213 	ASSERT(bp != NULL);
19214 	ASSERT(xp != NULL);
19215 	ASSERT(pktp != NULL);
19216 
19217 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19218 	    "sd_pkt_status_busy: entry\n");
19219 
19220 	/* If retries are exhausted, just fail the command. */
19221 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
19222 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19223 		    "device busy too long\n");
19224 		sd_return_failed_command(un, bp, EIO);
19225 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19226 		    "sd_pkt_status_busy: exit\n");
19227 		return;
19228 	}
19229 	xp->xb_retry_count++;
19230 
19231 	/*
19232 	 * Try to reset the target. However, we do not want to perform
19233 	 * more than one reset if the device continues to fail. The reset
19234 	 * will be performed when the retry count reaches the reset
19235 	 * threshold.  This threshold should be set such that at least
19236 	 * one retry is issued before the reset is performed.
19237 	 */
19238 	if (xp->xb_retry_count ==
19239 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19240 		int rval = 0;
19241 		mutex_exit(SD_MUTEX(un));
19242 		if (un->un_f_allow_bus_device_reset == TRUE) {
19243 			/*
19244 			 * First try to reset the LUN; if we cannot then
19245 			 * try to reset the target.
19246 			 */
19247 			if (un->un_f_lun_reset_enabled == TRUE) {
19248 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19249 				    "sd_pkt_status_busy: RESET_LUN\n");
19250 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19251 			}
19252 			if (rval == 0) {
19253 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19254 				    "sd_pkt_status_busy: RESET_TARGET\n");
19255 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19256 			}
19257 		}
19258 		if (rval == 0) {
19259 			/*
19260 			 * If the RESET_LUN and/or RESET_TARGET failed,
19261 			 * try RESET_ALL
19262 			 */
19263 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19264 			    "sd_pkt_status_busy: RESET_ALL\n");
19265 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19266 		}
19267 		mutex_enter(SD_MUTEX(un));
19268 		if (rval == 0) {
19269 			/*
19270 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19271 			 * At this point we give up & fail the command.
19272 			 */
19273 			sd_return_failed_command(un, bp, EIO);
19274 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19275 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19276 			return;
19277 		}
19278 	}
19279 
19280 	/*
19281 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19282 	 * we have already checked the retry counts above.
19283 	 */
19284 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19285 	    EIO, un->un_busy_timeout, NULL);
19286 
19287 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19288 	    "sd_pkt_status_busy: exit\n");
19289 }
19290 
19291 
19292 /*
19293  *    Function: sd_pkt_status_reservation_conflict
19294  *
19295  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19296  *		command status.
19297  *
19298  *     Context: May be called from interrupt context
19299  */
19300 
19301 static void
19302 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19303 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19304 {
19305 	ASSERT(un != NULL);
19306 	ASSERT(mutex_owned(SD_MUTEX(un)));
19307 	ASSERT(bp != NULL);
19308 	ASSERT(xp != NULL);
19309 	ASSERT(pktp != NULL);
19310 
19311 	/*
19312 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19313 	 * conflict could be due to various reasons like incorrect keys, not
19314 	 * registered or not reserved etc. So, we return EACCES to the caller.
19315 	 */
19316 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19317 		int cmd = SD_GET_PKT_OPCODE(pktp);
19318 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19319 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19320 			sd_return_failed_command(un, bp, EACCES);
19321 			return;
19322 		}
19323 	}
19324 
19325 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19326 
19327 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19328 		if (sd_failfast_enable != 0) {
19329 			/* By definition, we must panic here.... */
19330 			sd_panic_for_res_conflict(un);
19331 			/*NOTREACHED*/
19332 		}
19333 		SD_ERROR(SD_LOG_IO, un,
19334 		    "sd_handle_resv_conflict: Disk Reserved\n");
19335 		sd_return_failed_command(un, bp, EACCES);
19336 		return;
19337 	}
19338 
19339 	/*
19340 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19341 	 * property is set (default is 1). Retries will not succeed
19342 	 * on a disk reserved by another initiator. HA systems
19343 	 * may reset this via sd.conf to avoid these retries.
19344 	 *
19345 	 * Note: The legacy return code for this failure is EIO, however EACCES
19346 	 * seems more appropriate for a reservation conflict.
19347 	 */
19348 	if (sd_retry_on_reservation_conflict == 0) {
19349 		SD_ERROR(SD_LOG_IO, un,
19350 		    "sd_handle_resv_conflict: Device Reserved\n");
19351 		sd_return_failed_command(un, bp, EIO);
19352 		return;
19353 	}
19354 
19355 	/*
19356 	 * Retry the command if we can.
19357 	 *
19358 	 * Note: The legacy return code for this failure is EIO, however EACCES
19359 	 * seems more appropriate for a reservation conflict.
19360 	 */
19361 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19362 	    (clock_t)2, NULL);
19363 }
19364 
19365 
19366 
19367 /*
19368  *    Function: sd_pkt_status_qfull
19369  *
19370  * Description: Handle a QUEUE FULL condition from the target.  This can
19371  *		occur if the HBA does not handle the queue full condition.
19372  *		(Basically this means third-party HBAs as Sun HBAs will
19373  *		handle the queue full condition.)  Note that if there are
19374  *		some commands already in the transport, then the queue full
19375  *		has occurred because the queue for this nexus is actually
19376  *		full. If there are no commands in the transport, then the
19377  *		queue full is resulting from some other initiator or lun
19378  *		consuming all the resources at the target.
19379  *
19380  *     Context: May be called from interrupt context
19381  */
19382 
19383 static void
19384 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19385 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19386 {
19387 	ASSERT(un != NULL);
19388 	ASSERT(mutex_owned(SD_MUTEX(un)));
19389 	ASSERT(bp != NULL);
19390 	ASSERT(xp != NULL);
19391 	ASSERT(pktp != NULL);
19392 
19393 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19394 	    "sd_pkt_status_qfull: entry\n");
19395 
19396 	/*
19397 	 * Just lower the QFULL throttle and retry the command.  Note that
19398 	 * we do not limit the number of retries here.
19399 	 */
19400 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19401 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19402 	    SD_RESTART_TIMEOUT, NULL);
19403 
19404 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19405 	    "sd_pkt_status_qfull: exit\n");
19406 }
19407 
19408 
19409 /*
19410  *    Function: sd_reset_target
19411  *
19412  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19413  *		RESET_TARGET, or RESET_ALL.
19414  *
19415  *     Context: May be called under interrupt context.
19416  */
19417 
19418 static void
19419 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19420 {
19421 	int rval = 0;
19422 
19423 	ASSERT(un != NULL);
19424 	ASSERT(mutex_owned(SD_MUTEX(un)));
19425 	ASSERT(pktp != NULL);
19426 
19427 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19428 
19429 	/*
19430 	 * No need to reset if the transport layer has already done so.
19431 	 */
19432 	if ((pktp->pkt_statistics &
19433 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19434 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19435 		    "sd_reset_target: no reset\n");
19436 		return;
19437 	}
19438 
19439 	mutex_exit(SD_MUTEX(un));
19440 
19441 	if (un->un_f_allow_bus_device_reset == TRUE) {
19442 		if (un->un_f_lun_reset_enabled == TRUE) {
19443 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19444 			    "sd_reset_target: RESET_LUN\n");
19445 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19446 		}
19447 		if (rval == 0) {
19448 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19449 			    "sd_reset_target: RESET_TARGET\n");
19450 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19451 		}
19452 	}
19453 
19454 	if (rval == 0) {
19455 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19456 		    "sd_reset_target: RESET_ALL\n");
19457 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19458 	}
19459 
19460 	mutex_enter(SD_MUTEX(un));
19461 
19462 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19463 }
19464 
19465 /*
19466  *    Function: sd_target_change_task
19467  *
19468  * Description: Handle dynamic target change
19469  *
19470  *     Context: Executes in a taskq() thread context
19471  */
19472 static void
19473 sd_target_change_task(void *arg)
19474 {
19475 	struct sd_lun		*un = arg;
19476 	uint64_t		capacity;
19477 	diskaddr_t		label_cap;
19478 	uint_t			lbasize;
19479 	sd_ssc_t		*ssc;
19480 
19481 	ASSERT(un != NULL);
19482 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19483 
19484 	if ((un->un_f_blockcount_is_valid == FALSE) ||
19485 	    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19486 		return;
19487 	}
19488 
19489 	ssc = sd_ssc_init(un);
19490 
19491 	if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19492 	    &lbasize, SD_PATH_DIRECT) != 0) {
19493 		SD_ERROR(SD_LOG_ERROR, un,
19494 		    "sd_target_change_task: fail to read capacity\n");
19495 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19496 		goto task_exit;
19497 	}
19498 
19499 	mutex_enter(SD_MUTEX(un));
19500 	if (capacity <= un->un_blockcount) {
19501 		mutex_exit(SD_MUTEX(un));
19502 		goto task_exit;
19503 	}
19504 
19505 	sd_update_block_info(un, lbasize, capacity);
19506 	mutex_exit(SD_MUTEX(un));
19507 
19508 	/*
19509 	 * If lun is EFI labeled and lun capacity is greater than the
19510 	 * capacity contained in the label, log a sys event.
19511 	 */
19512 	if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19513 	    (void*)SD_PATH_DIRECT) == 0) {
19514 		mutex_enter(SD_MUTEX(un));
19515 		if (un->un_f_blockcount_is_valid &&
19516 		    un->un_blockcount > label_cap) {
19517 			mutex_exit(SD_MUTEX(un));
19518 			sd_log_lun_expansion_event(un, KM_SLEEP);
19519 		} else {
19520 			mutex_exit(SD_MUTEX(un));
19521 		}
19522 	}
19523 
19524 task_exit:
19525 	sd_ssc_fini(ssc);
19526 }
19527 
19528 
19529 /*
19530  *    Function: sd_log_dev_status_event
19531  *
19532  * Description: Log EC_dev_status sysevent
19533  *
19534  *     Context: Never called from interrupt context
19535  */
19536 static void
19537 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19538 {
19539 	int err;
19540 	char			*path;
19541 	nvlist_t		*attr_list;
19542 
19543 	/* Allocate and build sysevent attribute list */
19544 	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19545 	if (err != 0) {
19546 		SD_ERROR(SD_LOG_ERROR, un,
19547 		    "sd_log_dev_status_event: fail to allocate space\n");
19548 		return;
19549 	}
19550 
19551 	path = kmem_alloc(MAXPATHLEN, km_flag);
19552 	if (path == NULL) {
19553 		nvlist_free(attr_list);
19554 		SD_ERROR(SD_LOG_ERROR, un,
19555 		    "sd_log_dev_status_event: fail to allocate space\n");
19556 		return;
19557 	}
19558 	/*
19559 	 * Add path attribute to identify the lun.
19560 	 * We are using minor node 'a' as the sysevent attribute.
19561 	 */
19562 	(void) snprintf(path, MAXPATHLEN, "/devices");
19563 	(void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19564 	(void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19565 	    ":a");
19566 
19567 	err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19568 	if (err != 0) {
19569 		nvlist_free(attr_list);
19570 		kmem_free(path, MAXPATHLEN);
19571 		SD_ERROR(SD_LOG_ERROR, un,
19572 		    "sd_log_dev_status_event: fail to add attribute\n");
19573 		return;
19574 	}
19575 
19576 	/* Log dynamic lun expansion sysevent */
19577 	err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19578 	    esc, attr_list, NULL, km_flag);
19579 	if (err != DDI_SUCCESS) {
19580 		SD_ERROR(SD_LOG_ERROR, un,
19581 		    "sd_log_dev_status_event: fail to log sysevent\n");
19582 	}
19583 
19584 	nvlist_free(attr_list);
19585 	kmem_free(path, MAXPATHLEN);
19586 }
19587 
19588 
19589 /*
19590  *    Function: sd_log_lun_expansion_event
19591  *
19592  * Description: Log lun expansion sys event
19593  *
19594  *     Context: Never called from interrupt context
19595  */
19596 static void
19597 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19598 {
19599 	sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19600 }
19601 
19602 
19603 /*
19604  *    Function: sd_log_eject_request_event
19605  *
19606  * Description: Log eject request sysevent
19607  *
19608  *     Context: Never called from interrupt context
19609  */
19610 static void
19611 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19612 {
19613 	sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19614 }
19615 
19616 
19617 /*
19618  *    Function: sd_media_change_task
19619  *
19620  * Description: Recovery action for CDROM to become available.
19621  *
19622  *     Context: Executes in a taskq() thread context
19623  */
19624 
19625 static void
19626 sd_media_change_task(void *arg)
19627 {
19628 	struct	scsi_pkt	*pktp = arg;
19629 	struct	sd_lun		*un;
19630 	struct	buf		*bp;
19631 	struct	sd_xbuf		*xp;
19632 	int	err		= 0;
19633 	int	retry_count	= 0;
19634 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19635 	struct	sd_sense_info	si;
19636 
19637 	ASSERT(pktp != NULL);
19638 	bp = (struct buf *)pktp->pkt_private;
19639 	ASSERT(bp != NULL);
19640 	xp = SD_GET_XBUF(bp);
19641 	ASSERT(xp != NULL);
19642 	un = SD_GET_UN(bp);
19643 	ASSERT(un != NULL);
19644 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19645 	ASSERT(un->un_f_monitor_media_state);
19646 
19647 	si.ssi_severity = SCSI_ERR_INFO;
19648 	si.ssi_pfa_flag = FALSE;
19649 
19650 	/*
19651 	 * When a reset is issued on a CDROM, it takes a long time to
19652 	 * recover. First few attempts to read capacity and other things
19653 	 * related to handling unit attention fail (with a ASC 0x4 and
19654 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19655 	 * to limit the retries in other cases of genuine failures like
19656 	 * no media in drive.
19657 	 */
19658 	while (retry_count++ < retry_limit) {
19659 		if ((err = sd_handle_mchange(un)) == 0) {
19660 			break;
19661 		}
19662 		if (err == EAGAIN) {
19663 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19664 		}
19665 		/* Sleep for 0.5 sec. & try again */
19666 		delay(drv_usectohz(500000));
19667 	}
19668 
19669 	/*
19670 	 * Dispatch (retry or fail) the original command here,
19671 	 * along with appropriate console messages....
19672 	 *
19673 	 * Must grab the mutex before calling sd_retry_command,
19674 	 * sd_print_sense_msg and sd_return_failed_command.
19675 	 */
19676 	mutex_enter(SD_MUTEX(un));
19677 	if (err != SD_CMD_SUCCESS) {
19678 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19679 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19680 		si.ssi_severity = SCSI_ERR_FATAL;
19681 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19682 		sd_return_failed_command(un, bp, EIO);
19683 	} else {
19684 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
19685 		    &si, EIO, (clock_t)0, NULL);
19686 	}
19687 	mutex_exit(SD_MUTEX(un));
19688 }
19689 
19690 
19691 
19692 /*
19693  *    Function: sd_handle_mchange
19694  *
19695  * Description: Perform geometry validation & other recovery when CDROM
19696  *		has been removed from drive.
19697  *
19698  * Return Code: 0 for success
19699  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19700  *		sd_send_scsi_READ_CAPACITY()
19701  *
19702  *     Context: Executes in a taskq() thread context
19703  */
19704 
19705 static int
19706 sd_handle_mchange(struct sd_lun *un)
19707 {
19708 	uint64_t	capacity;
19709 	uint32_t	lbasize;
19710 	int		rval;
19711 	sd_ssc_t	*ssc;
19712 
19713 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19714 	ASSERT(un->un_f_monitor_media_state);
19715 
19716 	ssc = sd_ssc_init(un);
19717 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19718 	    SD_PATH_DIRECT_PRIORITY);
19719 
19720 	if (rval != 0)
19721 		goto failed;
19722 
19723 	mutex_enter(SD_MUTEX(un));
19724 	sd_update_block_info(un, lbasize, capacity);
19725 
19726 	if (un->un_errstats != NULL) {
19727 		struct	sd_errstats *stp =
19728 		    (struct sd_errstats *)un->un_errstats->ks_data;
19729 		stp->sd_capacity.value.ui64 = (uint64_t)
19730 		    ((uint64_t)un->un_blockcount *
19731 		    (uint64_t)un->un_tgt_blocksize);
19732 	}
19733 
19734 	/*
19735 	 * Check if the media in the device is writable or not
19736 	 */
19737 	if (ISCD(un)) {
19738 		sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19739 	}
19740 
19741 	/*
19742 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19743 	 * valid geometry.
19744 	 */
19745 	mutex_exit(SD_MUTEX(un));
19746 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19747 
19748 
19749 	if (cmlb_validate(un->un_cmlbhandle, 0,
19750 	    (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19751 		sd_ssc_fini(ssc);
19752 		return (EIO);
19753 	} else {
19754 		if (un->un_f_pkstats_enabled) {
19755 			sd_set_pstats(un);
19756 			SD_TRACE(SD_LOG_IO_PARTITION, un,
19757 			    "sd_handle_mchange: un:0x%p pstats created and "
19758 			    "set\n", un);
19759 		}
19760 	}
19761 
19762 	/*
19763 	 * Try to lock the door
19764 	 */
19765 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19766 	    SD_PATH_DIRECT_PRIORITY);
19767 failed:
19768 	if (rval != 0)
19769 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19770 	sd_ssc_fini(ssc);
19771 	return (rval);
19772 }
19773 
19774 
19775 /*
19776  *    Function: sd_send_scsi_DOORLOCK
19777  *
19778  * Description: Issue the scsi DOOR LOCK command
19779  *
19780  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19781  *                      structure for this target.
19782  *		flag  - SD_REMOVAL_ALLOW
19783  *			SD_REMOVAL_PREVENT
19784  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19785  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19786  *			to use the USCSI "direct" chain and bypass the normal
19787  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19788  *			command is issued as part of an error recovery action.
19789  *
19790  * Return Code: 0   - Success
19791  *		errno return code from sd_ssc_send()
19792  *
19793  *     Context: Can sleep.
19794  */
19795 
19796 static int
19797 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19798 {
19799 	struct scsi_extended_sense	sense_buf;
19800 	union scsi_cdb		cdb;
19801 	struct uscsi_cmd	ucmd_buf;
19802 	int			status;
19803 	struct sd_lun		*un;
19804 
19805 	ASSERT(ssc != NULL);
19806 	un = ssc->ssc_un;
19807 	ASSERT(un != NULL);
19808 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19809 
19810 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19811 
19812 	/* already determined doorlock is not supported, fake success */
19813 	if (un->un_f_doorlock_supported == FALSE) {
19814 		return (0);
19815 	}
19816 
19817 	/*
19818 	 * If we are ejecting and see an SD_REMOVAL_PREVENT
19819 	 * ignore the command so we can complete the eject
19820 	 * operation.
19821 	 */
19822 	if (flag == SD_REMOVAL_PREVENT) {
19823 		mutex_enter(SD_MUTEX(un));
19824 		if (un->un_f_ejecting == TRUE) {
19825 			mutex_exit(SD_MUTEX(un));
19826 			return (EAGAIN);
19827 		}
19828 		mutex_exit(SD_MUTEX(un));
19829 	}
19830 
19831 	bzero(&cdb, sizeof (cdb));
19832 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19833 
19834 	cdb.scc_cmd = SCMD_DOORLOCK;
19835 	cdb.cdb_opaque[4] = (uchar_t)flag;
19836 
19837 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19838 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19839 	ucmd_buf.uscsi_bufaddr	= NULL;
19840 	ucmd_buf.uscsi_buflen	= 0;
19841 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19842 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19843 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19844 	ucmd_buf.uscsi_timeout	= 15;
19845 
19846 	SD_TRACE(SD_LOG_IO, un,
19847 	    "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19848 
19849 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19850 	    UIO_SYSSPACE, path_flag);
19851 
19852 	if (status == 0)
19853 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19854 
19855 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19856 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19857 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19858 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19859 
19860 		/* fake success and skip subsequent doorlock commands */
19861 		un->un_f_doorlock_supported = FALSE;
19862 		return (0);
19863 	}
19864 
19865 	return (status);
19866 }
19867 
19868 /*
19869  *    Function: sd_send_scsi_READ_CAPACITY
19870  *
19871  * Description: This routine uses the scsi READ CAPACITY command to determine
19872  *		the device capacity in number of blocks and the device native
19873  *		block size. If this function returns a failure, then the
19874  *		values in *capp and *lbap are undefined.  If the capacity
19875  *		returned is 0xffffffff then the lun is too large for a
19876  *		normal READ CAPACITY command and the results of a
19877  *		READ CAPACITY 16 will be used instead.
19878  *
19879  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19880  *		capp - ptr to unsigned 64-bit variable to receive the
19881  *			capacity value from the command.
19882  *		lbap - ptr to unsigned 32-bit varaible to receive the
19883  *			block size value from the command
19884  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19885  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19886  *			to use the USCSI "direct" chain and bypass the normal
19887  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19888  *			command is issued as part of an error recovery action.
19889  *
19890  * Return Code: 0   - Success
19891  *		EIO - IO error
19892  *		EACCES - Reservation conflict detected
19893  *		EAGAIN - Device is becoming ready
19894  *		errno return code from sd_ssc_send()
19895  *
19896  *     Context: Can sleep.  Blocks until command completes.
19897  */
19898 
19899 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
19900 
19901 static int
19902 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
19903 	int path_flag)
19904 {
19905 	struct	scsi_extended_sense	sense_buf;
19906 	struct	uscsi_cmd	ucmd_buf;
19907 	union	scsi_cdb	cdb;
19908 	uint32_t		*capacity_buf;
19909 	uint64_t		capacity;
19910 	uint32_t		lbasize;
19911 	uint32_t		pbsize;
19912 	int			status;
19913 	struct sd_lun		*un;
19914 
19915 	ASSERT(ssc != NULL);
19916 
19917 	un = ssc->ssc_un;
19918 	ASSERT(un != NULL);
19919 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19920 	ASSERT(capp != NULL);
19921 	ASSERT(lbap != NULL);
19922 
19923 	SD_TRACE(SD_LOG_IO, un,
19924 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19925 
19926 	/*
19927 	 * First send a READ_CAPACITY command to the target.
19928 	 * (This command is mandatory under SCSI-2.)
19929 	 *
19930 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
19931 	 * Medium Indicator bit is cleared.  The address field must be
19932 	 * zero if the PMI bit is zero.
19933 	 */
19934 	bzero(&cdb, sizeof (cdb));
19935 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19936 
19937 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19938 
19939 	cdb.scc_cmd = SCMD_READ_CAPACITY;
19940 
19941 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19942 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19943 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
19944 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
19945 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19946 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19947 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19948 	ucmd_buf.uscsi_timeout	= 60;
19949 
19950 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19951 	    UIO_SYSSPACE, path_flag);
19952 
19953 	switch (status) {
19954 	case 0:
19955 		/* Return failure if we did not get valid capacity data. */
19956 		if (ucmd_buf.uscsi_resid != 0) {
19957 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
19958 			    "sd_send_scsi_READ_CAPACITY received invalid "
19959 			    "capacity data");
19960 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19961 			return (EIO);
19962 		}
19963 		/*
19964 		 * Read capacity and block size from the READ CAPACITY 10 data.
19965 		 * This data may be adjusted later due to device specific
19966 		 * issues.
19967 		 *
19968 		 * According to the SCSI spec, the READ CAPACITY 10
19969 		 * command returns the following:
19970 		 *
19971 		 *  bytes 0-3: Maximum logical block address available.
19972 		 *		(MSB in byte:0 & LSB in byte:3)
19973 		 *
19974 		 *  bytes 4-7: Block length in bytes
19975 		 *		(MSB in byte:4 & LSB in byte:7)
19976 		 *
19977 		 */
19978 		capacity = BE_32(capacity_buf[0]);
19979 		lbasize = BE_32(capacity_buf[1]);
19980 
19981 		/*
19982 		 * Done with capacity_buf
19983 		 */
19984 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19985 
19986 		/*
19987 		 * if the reported capacity is set to all 0xf's, then
19988 		 * this disk is too large and requires SBC-2 commands.
19989 		 * Reissue the request using READ CAPACITY 16.
19990 		 */
19991 		if (capacity == 0xffffffff) {
19992 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19993 			status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
19994 			    &lbasize, &pbsize, path_flag);
19995 			if (status != 0) {
19996 				return (status);
19997 			}
19998 		}
19999 		break;	/* Success! */
20000 	case EIO:
20001 		switch (ucmd_buf.uscsi_status) {
20002 		case STATUS_RESERVATION_CONFLICT:
20003 			status = EACCES;
20004 			break;
20005 		case STATUS_CHECK:
20006 			/*
20007 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20008 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20009 			 */
20010 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20011 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20012 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20013 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20014 				return (EAGAIN);
20015 			}
20016 			break;
20017 		default:
20018 			break;
20019 		}
20020 		/* FALLTHRU */
20021 	default:
20022 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20023 		return (status);
20024 	}
20025 
20026 	/*
20027 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20028 	 * (2352 and 0 are common) so for these devices always force the value
20029 	 * to 2048 as required by the ATAPI specs.
20030 	 */
20031 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20032 		lbasize = 2048;
20033 	}
20034 
20035 	/*
20036 	 * Get the maximum LBA value from the READ CAPACITY data.
20037 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20038 	 * was cleared when issuing the command. This means that the LBA
20039 	 * returned from the device is the LBA of the last logical block
20040 	 * on the logical unit.  The actual logical block count will be
20041 	 * this value plus one.
20042 	 */
20043 	capacity += 1;
20044 
20045 	/*
20046 	 * Currently, for removable media, the capacity is saved in terms
20047 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20048 	 */
20049 	if (un->un_f_has_removable_media)
20050 		capacity *= (lbasize / un->un_sys_blocksize);
20051 
20052 	/*
20053 	 * Copy the values from the READ CAPACITY command into the space
20054 	 * provided by the caller.
20055 	 */
20056 	*capp = capacity;
20057 	*lbap = lbasize;
20058 
20059 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20060 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20061 
20062 	/*
20063 	 * Both the lbasize and capacity from the device must be nonzero,
20064 	 * otherwise we assume that the values are not valid and return
20065 	 * failure to the caller. (4203735)
20066 	 */
20067 	if ((capacity == 0) || (lbasize == 0)) {
20068 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20069 		    "sd_send_scsi_READ_CAPACITY received invalid value "
20070 		    "capacity %llu lbasize %d", capacity, lbasize);
20071 		return (EIO);
20072 	}
20073 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20074 	return (0);
20075 }
20076 
20077 /*
20078  *    Function: sd_send_scsi_READ_CAPACITY_16
20079  *
20080  * Description: This routine uses the scsi READ CAPACITY 16 command to
20081  *		determine the device capacity in number of blocks and the
20082  *		device native block size.  If this function returns a failure,
20083  *		then the values in *capp and *lbap are undefined.
20084  *		This routine should be called by sd_send_scsi_READ_CAPACITY
20085  *              which will apply any device specific adjustments to capacity
20086  *              and lbasize. One exception is it is also called by
20087  *              sd_get_media_info_ext. In that function, there is no need to
20088  *              adjust the capacity and lbasize.
20089  *
20090  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20091  *		capp - ptr to unsigned 64-bit variable to receive the
20092  *			capacity value from the command.
20093  *		lbap - ptr to unsigned 32-bit varaible to receive the
20094  *			block size value from the command
20095  *              psp  - ptr to unsigned 32-bit variable to receive the
20096  *                      physical block size value from the command
20097  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20098  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20099  *			to use the USCSI "direct" chain and bypass the normal
20100  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
20101  *			this command is issued as part of an error recovery
20102  *			action.
20103  *
20104  * Return Code: 0   - Success
20105  *		EIO - IO error
20106  *		EACCES - Reservation conflict detected
20107  *		EAGAIN - Device is becoming ready
20108  *		errno return code from sd_ssc_send()
20109  *
20110  *     Context: Can sleep.  Blocks until command completes.
20111  */
20112 
20113 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
20114 
20115 static int
20116 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20117 	uint32_t *lbap, uint32_t *psp, int path_flag)
20118 {
20119 	struct	scsi_extended_sense	sense_buf;
20120 	struct	uscsi_cmd	ucmd_buf;
20121 	union	scsi_cdb	cdb;
20122 	uint64_t		*capacity16_buf;
20123 	uint64_t		capacity;
20124 	uint32_t		lbasize;
20125 	uint32_t		pbsize;
20126 	uint32_t		lbpb_exp;
20127 	int			status;
20128 	struct sd_lun		*un;
20129 
20130 	ASSERT(ssc != NULL);
20131 
20132 	un = ssc->ssc_un;
20133 	ASSERT(un != NULL);
20134 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20135 	ASSERT(capp != NULL);
20136 	ASSERT(lbap != NULL);
20137 
20138 	SD_TRACE(SD_LOG_IO, un,
20139 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20140 
20141 	/*
20142 	 * First send a READ_CAPACITY_16 command to the target.
20143 	 *
20144 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20145 	 * Medium Indicator bit is cleared.  The address field must be
20146 	 * zero if the PMI bit is zero.
20147 	 */
20148 	bzero(&cdb, sizeof (cdb));
20149 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20150 
20151 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20152 
20153 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20154 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
20155 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
20156 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
20157 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20158 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20159 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20160 	ucmd_buf.uscsi_timeout	= 60;
20161 
20162 	/*
20163 	 * Read Capacity (16) is a Service Action In command.  One
20164 	 * command byte (0x9E) is overloaded for multiple operations,
20165 	 * with the second CDB byte specifying the desired operation
20166 	 */
20167 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20168 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20169 
20170 	/*
20171 	 * Fill in allocation length field
20172 	 */
20173 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20174 
20175 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20176 	    UIO_SYSSPACE, path_flag);
20177 
20178 	switch (status) {
20179 	case 0:
20180 		/* Return failure if we did not get valid capacity data. */
20181 		if (ucmd_buf.uscsi_resid > 20) {
20182 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20183 			    "sd_send_scsi_READ_CAPACITY_16 received invalid "
20184 			    "capacity data");
20185 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20186 			return (EIO);
20187 		}
20188 
20189 		/*
20190 		 * Read capacity and block size from the READ CAPACITY 10 data.
20191 		 * This data may be adjusted later due to device specific
20192 		 * issues.
20193 		 *
20194 		 * According to the SCSI spec, the READ CAPACITY 10
20195 		 * command returns the following:
20196 		 *
20197 		 *  bytes 0-7: Maximum logical block address available.
20198 		 *		(MSB in byte:0 & LSB in byte:7)
20199 		 *
20200 		 *  bytes 8-11: Block length in bytes
20201 		 *		(MSB in byte:8 & LSB in byte:11)
20202 		 *
20203 		 *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20204 		 */
20205 		capacity = BE_64(capacity16_buf[0]);
20206 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20207 		lbpb_exp = (BE_64(capacity16_buf[1]) >> 40) & 0x0f;
20208 
20209 		pbsize = lbasize << lbpb_exp;
20210 
20211 		/*
20212 		 * Done with capacity16_buf
20213 		 */
20214 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20215 
20216 		/*
20217 		 * if the reported capacity is set to all 0xf's, then
20218 		 * this disk is too large.  This could only happen with
20219 		 * a device that supports LBAs larger than 64 bits which
20220 		 * are not defined by any current T10 standards.
20221 		 */
20222 		if (capacity == 0xffffffffffffffff) {
20223 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20224 			    "disk is too large");
20225 			return (EIO);
20226 		}
20227 		break;	/* Success! */
20228 	case EIO:
20229 		switch (ucmd_buf.uscsi_status) {
20230 		case STATUS_RESERVATION_CONFLICT:
20231 			status = EACCES;
20232 			break;
20233 		case STATUS_CHECK:
20234 			/*
20235 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20236 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20237 			 */
20238 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20239 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20240 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20241 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20242 				return (EAGAIN);
20243 			}
20244 			break;
20245 		default:
20246 			break;
20247 		}
20248 		/* FALLTHRU */
20249 	default:
20250 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20251 		return (status);
20252 	}
20253 
20254 	*capp = capacity;
20255 	*lbap = lbasize;
20256 	*psp = pbsize;
20257 
20258 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20259 	    "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20260 	    capacity, lbasize, pbsize);
20261 
20262 	return (0);
20263 }
20264 
20265 
20266 /*
20267  *    Function: sd_send_scsi_START_STOP_UNIT
20268  *
20269  * Description: Issue a scsi START STOP UNIT command to the target.
20270  *
20271  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20272  *                       structure for this target.
20273  *      pc_flag - SD_POWER_CONDITION
20274  *                SD_START_STOP
20275  *		flag  - SD_TARGET_START
20276  *			SD_TARGET_STOP
20277  *			SD_TARGET_EJECT
20278  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20279  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20280  *			to use the USCSI "direct" chain and bypass the normal
20281  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20282  *			command is issued as part of an error recovery action.
20283  *
20284  * Return Code: 0   - Success
20285  *		EIO - IO error
20286  *		EACCES - Reservation conflict detected
20287  *		ENXIO  - Not Ready, medium not present
20288  *		errno return code from sd_ssc_send()
20289  *
20290  *     Context: Can sleep.
20291  */
20292 
20293 static int
20294 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20295     int path_flag)
20296 {
20297 	struct	scsi_extended_sense	sense_buf;
20298 	union scsi_cdb		cdb;
20299 	struct uscsi_cmd	ucmd_buf;
20300 	int			status;
20301 	struct sd_lun		*un;
20302 
20303 	ASSERT(ssc != NULL);
20304 	un = ssc->ssc_un;
20305 	ASSERT(un != NULL);
20306 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20307 
20308 	SD_TRACE(SD_LOG_IO, un,
20309 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20310 
20311 	if (un->un_f_check_start_stop &&
20312 	    ((pc_flag == SD_START_STOP) && (flag != SD_TARGET_EJECT)) &&
20313 	    (un->un_f_start_stop_supported != TRUE)) {
20314 		return (0);
20315 	}
20316 
20317 	/*
20318 	 * If we are performing an eject operation and
20319 	 * we receive any command other than SD_TARGET_EJECT
20320 	 * we should immediately return.
20321 	 */
20322 	if (flag != SD_TARGET_EJECT) {
20323 		mutex_enter(SD_MUTEX(un));
20324 		if (un->un_f_ejecting == TRUE) {
20325 			mutex_exit(SD_MUTEX(un));
20326 			return (EAGAIN);
20327 		}
20328 		mutex_exit(SD_MUTEX(un));
20329 	}
20330 
20331 	bzero(&cdb, sizeof (cdb));
20332 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20333 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20334 
20335 	cdb.scc_cmd = SCMD_START_STOP;
20336 	cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20337 	    (uchar_t)(flag << 4) : (uchar_t)flag;
20338 
20339 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20340 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20341 	ucmd_buf.uscsi_bufaddr	= NULL;
20342 	ucmd_buf.uscsi_buflen	= 0;
20343 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20344 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20345 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20346 	ucmd_buf.uscsi_timeout	= 200;
20347 
20348 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20349 	    UIO_SYSSPACE, path_flag);
20350 
20351 	switch (status) {
20352 	case 0:
20353 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20354 		break;	/* Success! */
20355 	case EIO:
20356 		switch (ucmd_buf.uscsi_status) {
20357 		case STATUS_RESERVATION_CONFLICT:
20358 			status = EACCES;
20359 			break;
20360 		case STATUS_CHECK:
20361 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20362 				switch (scsi_sense_key(
20363 				    (uint8_t *)&sense_buf)) {
20364 				case KEY_ILLEGAL_REQUEST:
20365 					status = ENOTSUP;
20366 					break;
20367 				case KEY_NOT_READY:
20368 					if (scsi_sense_asc(
20369 					    (uint8_t *)&sense_buf)
20370 					    == 0x3A) {
20371 						status = ENXIO;
20372 					}
20373 					break;
20374 				default:
20375 					break;
20376 				}
20377 			}
20378 			break;
20379 		default:
20380 			break;
20381 		}
20382 		break;
20383 	default:
20384 		break;
20385 	}
20386 
20387 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20388 
20389 	return (status);
20390 }
20391 
20392 
20393 /*
20394  *    Function: sd_start_stop_unit_callback
20395  *
20396  * Description: timeout(9F) callback to begin recovery process for a
20397  *		device that has spun down.
20398  *
20399  *   Arguments: arg - pointer to associated softstate struct.
20400  *
20401  *     Context: Executes in a timeout(9F) thread context
20402  */
20403 
20404 static void
20405 sd_start_stop_unit_callback(void *arg)
20406 {
20407 	struct sd_lun	*un = arg;
20408 	ASSERT(un != NULL);
20409 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20410 
20411 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20412 
20413 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20414 }
20415 
20416 
20417 /*
20418  *    Function: sd_start_stop_unit_task
20419  *
20420  * Description: Recovery procedure when a drive is spun down.
20421  *
20422  *   Arguments: arg - pointer to associated softstate struct.
20423  *
20424  *     Context: Executes in a taskq() thread context
20425  */
20426 
20427 static void
20428 sd_start_stop_unit_task(void *arg)
20429 {
20430 	struct sd_lun	*un = arg;
20431 	sd_ssc_t	*ssc;
20432 	int		power_level;
20433 	int		rval;
20434 
20435 	ASSERT(un != NULL);
20436 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20437 
20438 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20439 
20440 	/*
20441 	 * Some unformatted drives report not ready error, no need to
20442 	 * restart if format has been initiated.
20443 	 */
20444 	mutex_enter(SD_MUTEX(un));
20445 	if (un->un_f_format_in_progress == TRUE) {
20446 		mutex_exit(SD_MUTEX(un));
20447 		return;
20448 	}
20449 	mutex_exit(SD_MUTEX(un));
20450 
20451 	ssc = sd_ssc_init(un);
20452 	/*
20453 	 * When a START STOP command is issued from here, it is part of a
20454 	 * failure recovery operation and must be issued before any other
20455 	 * commands, including any pending retries. Thus it must be sent
20456 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20457 	 * succeeds or not, we will start I/O after the attempt.
20458 	 * If power condition is supported and the current power level
20459 	 * is capable of performing I/O, we should set the power condition
20460 	 * to that level. Otherwise, set the power condition to ACTIVE.
20461 	 */
20462 	if (un->un_f_power_condition_supported) {
20463 		mutex_enter(SD_MUTEX(un));
20464 		ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20465 		power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20466 		    > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20467 		mutex_exit(SD_MUTEX(un));
20468 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20469 		    sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20470 	} else {
20471 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20472 		    SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20473 	}
20474 
20475 	if (rval != 0)
20476 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20477 	sd_ssc_fini(ssc);
20478 	/*
20479 	 * The above call blocks until the START_STOP_UNIT command completes.
20480 	 * Now that it has completed, we must re-try the original IO that
20481 	 * received the NOT READY condition in the first place. There are
20482 	 * three possible conditions here:
20483 	 *
20484 	 *  (1) The original IO is on un_retry_bp.
20485 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
20486 	 *	is NULL.
20487 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
20488 	 *	points to some other, unrelated bp.
20489 	 *
20490 	 * For each case, we must call sd_start_cmds() with un_retry_bp
20491 	 * as the argument. If un_retry_bp is NULL, this will initiate
20492 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
20493 	 * then this will process the bp on un_retry_bp. That may or may not
20494 	 * be the original IO, but that does not matter: the important thing
20495 	 * is to keep the IO processing going at this point.
20496 	 *
20497 	 * Note: This is a very specific error recovery sequence associated
20498 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20499 	 * serialize the I/O with completion of the spin-up.
20500 	 */
20501 	mutex_enter(SD_MUTEX(un));
20502 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20503 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20504 	    un, un->un_retry_bp);
20505 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20506 	sd_start_cmds(un, un->un_retry_bp);
20507 	mutex_exit(SD_MUTEX(un));
20508 
20509 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20510 }
20511 
20512 
20513 /*
20514  *    Function: sd_send_scsi_INQUIRY
20515  *
20516  * Description: Issue the scsi INQUIRY command.
20517  *
20518  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20519  *                      structure for this target.
20520  *		bufaddr
20521  *		buflen
20522  *		evpd
20523  *		page_code
20524  *		page_length
20525  *
20526  * Return Code: 0   - Success
20527  *		errno return code from sd_ssc_send()
20528  *
20529  *     Context: Can sleep. Does not return until command is completed.
20530  */
20531 
20532 static int
20533 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20534 	uchar_t evpd, uchar_t page_code, size_t *residp)
20535 {
20536 	union scsi_cdb		cdb;
20537 	struct uscsi_cmd	ucmd_buf;
20538 	int			status;
20539 	struct sd_lun		*un;
20540 
20541 	ASSERT(ssc != NULL);
20542 	un = ssc->ssc_un;
20543 	ASSERT(un != NULL);
20544 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20545 	ASSERT(bufaddr != NULL);
20546 
20547 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20548 
20549 	bzero(&cdb, sizeof (cdb));
20550 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20551 	bzero(bufaddr, buflen);
20552 
20553 	cdb.scc_cmd = SCMD_INQUIRY;
20554 	cdb.cdb_opaque[1] = evpd;
20555 	cdb.cdb_opaque[2] = page_code;
20556 	FORMG0COUNT(&cdb, buflen);
20557 
20558 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20559 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20560 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20561 	ucmd_buf.uscsi_buflen	= buflen;
20562 	ucmd_buf.uscsi_rqbuf	= NULL;
20563 	ucmd_buf.uscsi_rqlen	= 0;
20564 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20565 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20566 
20567 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20568 	    UIO_SYSSPACE, SD_PATH_DIRECT);
20569 
20570 	/*
20571 	 * Only handle status == 0, the upper-level caller
20572 	 * will put different assessment based on the context.
20573 	 */
20574 	if (status == 0)
20575 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20576 
20577 	if ((status == 0) && (residp != NULL)) {
20578 		*residp = ucmd_buf.uscsi_resid;
20579 	}
20580 
20581 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20582 
20583 	return (status);
20584 }
20585 
20586 
20587 /*
20588  *    Function: sd_send_scsi_TEST_UNIT_READY
20589  *
20590  * Description: Issue the scsi TEST UNIT READY command.
20591  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20592  *		prevent retrying failed commands. Use this when the intent
20593  *		is either to check for device readiness, to clear a Unit
20594  *		Attention, or to clear any outstanding sense data.
20595  *		However under specific conditions the expected behavior
20596  *		is for retries to bring a device ready, so use the flag
20597  *		with caution.
20598  *
20599  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20600  *                      structure for this target.
20601  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20602  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20603  *			0: dont check for media present, do retries on cmd.
20604  *
20605  * Return Code: 0   - Success
20606  *		EIO - IO error
20607  *		EACCES - Reservation conflict detected
20608  *		ENXIO  - Not Ready, medium not present
20609  *		errno return code from sd_ssc_send()
20610  *
20611  *     Context: Can sleep. Does not return until command is completed.
20612  */
20613 
20614 static int
20615 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20616 {
20617 	struct	scsi_extended_sense	sense_buf;
20618 	union scsi_cdb		cdb;
20619 	struct uscsi_cmd	ucmd_buf;
20620 	int			status;
20621 	struct sd_lun		*un;
20622 
20623 	ASSERT(ssc != NULL);
20624 	un = ssc->ssc_un;
20625 	ASSERT(un != NULL);
20626 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20627 
20628 	SD_TRACE(SD_LOG_IO, un,
20629 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20630 
20631 	/*
20632 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20633 	 * timeouts when they receive a TUR and the queue is not empty. Check
20634 	 * the configuration flag set during attach (indicating the drive has
20635 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20636 	 * TUR. If there are
20637 	 * pending commands return success, this is a bit arbitrary but is ok
20638 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20639 	 * configurations.
20640 	 */
20641 	if (un->un_f_cfg_tur_check == TRUE) {
20642 		mutex_enter(SD_MUTEX(un));
20643 		if (un->un_ncmds_in_transport != 0) {
20644 			mutex_exit(SD_MUTEX(un));
20645 			return (0);
20646 		}
20647 		mutex_exit(SD_MUTEX(un));
20648 	}
20649 
20650 	bzero(&cdb, sizeof (cdb));
20651 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20652 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20653 
20654 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20655 
20656 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20657 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20658 	ucmd_buf.uscsi_bufaddr	= NULL;
20659 	ucmd_buf.uscsi_buflen	= 0;
20660 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20661 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20662 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20663 
20664 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20665 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20666 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20667 	}
20668 	ucmd_buf.uscsi_timeout	= 60;
20669 
20670 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20671 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20672 	    SD_PATH_STANDARD));
20673 
20674 	switch (status) {
20675 	case 0:
20676 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20677 		break;	/* Success! */
20678 	case EIO:
20679 		switch (ucmd_buf.uscsi_status) {
20680 		case STATUS_RESERVATION_CONFLICT:
20681 			status = EACCES;
20682 			break;
20683 		case STATUS_CHECK:
20684 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20685 				break;
20686 			}
20687 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20688 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20689 			    KEY_NOT_READY) &&
20690 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20691 				status = ENXIO;
20692 			}
20693 			break;
20694 		default:
20695 			break;
20696 		}
20697 		break;
20698 	default:
20699 		break;
20700 	}
20701 
20702 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20703 
20704 	return (status);
20705 }
20706 
20707 /*
20708  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20709  *
20710  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20711  *
20712  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20713  *                      structure for this target.
20714  *
20715  * Return Code: 0   - Success
20716  *		EACCES
20717  *		ENOTSUP
20718  *		errno return code from sd_ssc_send()
20719  *
20720  *     Context: Can sleep. Does not return until command is completed.
20721  */
20722 
20723 static int
20724 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t  usr_cmd,
20725 	uint16_t data_len, uchar_t *data_bufp)
20726 {
20727 	struct scsi_extended_sense	sense_buf;
20728 	union scsi_cdb		cdb;
20729 	struct uscsi_cmd	ucmd_buf;
20730 	int			status;
20731 	int			no_caller_buf = FALSE;
20732 	struct sd_lun		*un;
20733 
20734 	ASSERT(ssc != NULL);
20735 	un = ssc->ssc_un;
20736 	ASSERT(un != NULL);
20737 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20738 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20739 
20740 	SD_TRACE(SD_LOG_IO, un,
20741 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20742 
20743 	bzero(&cdb, sizeof (cdb));
20744 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20745 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20746 	if (data_bufp == NULL) {
20747 		/* Allocate a default buf if the caller did not give one */
20748 		ASSERT(data_len == 0);
20749 		data_len  = MHIOC_RESV_KEY_SIZE;
20750 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20751 		no_caller_buf = TRUE;
20752 	}
20753 
20754 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20755 	cdb.cdb_opaque[1] = usr_cmd;
20756 	FORMG1COUNT(&cdb, data_len);
20757 
20758 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20759 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20760 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20761 	ucmd_buf.uscsi_buflen	= data_len;
20762 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20763 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20764 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20765 	ucmd_buf.uscsi_timeout	= 60;
20766 
20767 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20768 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20769 
20770 	switch (status) {
20771 	case 0:
20772 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20773 
20774 		break;	/* Success! */
20775 	case EIO:
20776 		switch (ucmd_buf.uscsi_status) {
20777 		case STATUS_RESERVATION_CONFLICT:
20778 			status = EACCES;
20779 			break;
20780 		case STATUS_CHECK:
20781 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20782 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20783 			    KEY_ILLEGAL_REQUEST)) {
20784 				status = ENOTSUP;
20785 			}
20786 			break;
20787 		default:
20788 			break;
20789 		}
20790 		break;
20791 	default:
20792 		break;
20793 	}
20794 
20795 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20796 
20797 	if (no_caller_buf == TRUE) {
20798 		kmem_free(data_bufp, data_len);
20799 	}
20800 
20801 	return (status);
20802 }
20803 
20804 
20805 /*
20806  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20807  *
20808  * Description: This routine is the driver entry point for handling CD-ROM
20809  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20810  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20811  *		device.
20812  *
20813  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
20814  *                      for the target.
20815  *		usr_cmd SCSI-3 reservation facility command (one of
20816  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20817  *			SD_SCSI3_PREEMPTANDABORT)
20818  *		usr_bufp - user provided pointer register, reserve descriptor or
20819  *			preempt and abort structure (mhioc_register_t,
20820  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20821  *
20822  * Return Code: 0   - Success
20823  *		EACCES
20824  *		ENOTSUP
20825  *		errno return code from sd_ssc_send()
20826  *
20827  *     Context: Can sleep. Does not return until command is completed.
20828  */
20829 
20830 static int
20831 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20832 	uchar_t	*usr_bufp)
20833 {
20834 	struct scsi_extended_sense	sense_buf;
20835 	union scsi_cdb		cdb;
20836 	struct uscsi_cmd	ucmd_buf;
20837 	int			status;
20838 	uchar_t			data_len = sizeof (sd_prout_t);
20839 	sd_prout_t		*prp;
20840 	struct sd_lun		*un;
20841 
20842 	ASSERT(ssc != NULL);
20843 	un = ssc->ssc_un;
20844 	ASSERT(un != NULL);
20845 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20846 	ASSERT(data_len == 24);	/* required by scsi spec */
20847 
20848 	SD_TRACE(SD_LOG_IO, un,
20849 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20850 
20851 	if (usr_bufp == NULL) {
20852 		return (EINVAL);
20853 	}
20854 
20855 	bzero(&cdb, sizeof (cdb));
20856 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20857 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20858 	prp = kmem_zalloc(data_len, KM_SLEEP);
20859 
20860 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
20861 	cdb.cdb_opaque[1] = usr_cmd;
20862 	FORMG1COUNT(&cdb, data_len);
20863 
20864 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20865 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20866 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
20867 	ucmd_buf.uscsi_buflen	= data_len;
20868 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20869 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20870 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20871 	ucmd_buf.uscsi_timeout	= 60;
20872 
20873 	switch (usr_cmd) {
20874 	case SD_SCSI3_REGISTER: {
20875 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
20876 
20877 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20878 		bcopy(ptr->newkey.key, prp->service_key,
20879 		    MHIOC_RESV_KEY_SIZE);
20880 		prp->aptpl = ptr->aptpl;
20881 		break;
20882 	}
20883 	case SD_SCSI3_RESERVE:
20884 	case SD_SCSI3_RELEASE: {
20885 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
20886 
20887 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20888 		prp->scope_address = BE_32(ptr->scope_specific_addr);
20889 		cdb.cdb_opaque[2] = ptr->type;
20890 		break;
20891 	}
20892 	case SD_SCSI3_PREEMPTANDABORT: {
20893 		mhioc_preemptandabort_t *ptr =
20894 		    (mhioc_preemptandabort_t *)usr_bufp;
20895 
20896 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
20897 		bcopy(ptr->victim_key.key, prp->service_key,
20898 		    MHIOC_RESV_KEY_SIZE);
20899 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
20900 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
20901 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
20902 		break;
20903 	}
20904 	case SD_SCSI3_REGISTERANDIGNOREKEY:
20905 	{
20906 		mhioc_registerandignorekey_t *ptr;
20907 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
20908 		bcopy(ptr->newkey.key,
20909 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
20910 		prp->aptpl = ptr->aptpl;
20911 		break;
20912 	}
20913 	default:
20914 		ASSERT(FALSE);
20915 		break;
20916 	}
20917 
20918 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20919 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20920 
20921 	switch (status) {
20922 	case 0:
20923 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20924 		break;	/* Success! */
20925 	case EIO:
20926 		switch (ucmd_buf.uscsi_status) {
20927 		case STATUS_RESERVATION_CONFLICT:
20928 			status = EACCES;
20929 			break;
20930 		case STATUS_CHECK:
20931 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20932 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20933 			    KEY_ILLEGAL_REQUEST)) {
20934 				status = ENOTSUP;
20935 			}
20936 			break;
20937 		default:
20938 			break;
20939 		}
20940 		break;
20941 	default:
20942 		break;
20943 	}
20944 
20945 	kmem_free(prp, data_len);
20946 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20947 	return (status);
20948 }
20949 
20950 
20951 /*
20952  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
20953  *
20954  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20955  *
20956  *   Arguments: un - pointer to the target's soft state struct
20957  *              dkc - pointer to the callback structure
20958  *
20959  * Return Code: 0 - success
20960  *		errno-type error code
20961  *
20962  *     Context: kernel thread context only.
20963  *
20964  *  _______________________________________________________________
20965  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
20966  * |FLUSH_VOLATILE|              | operation                       |
20967  * |______________|______________|_________________________________|
20968  * | 0            | NULL         | Synchronous flush on both       |
20969  * |              |              | volatile and non-volatile cache |
20970  * |______________|______________|_________________________________|
20971  * | 1            | NULL         | Synchronous flush on volatile   |
20972  * |              |              | cache; disk drivers may suppress|
20973  * |              |              | flush if disk table indicates   |
20974  * |              |              | non-volatile cache              |
20975  * |______________|______________|_________________________________|
20976  * | 0            | !NULL        | Asynchronous flush on both      |
20977  * |              |              | volatile and non-volatile cache;|
20978  * |______________|______________|_________________________________|
20979  * | 1            | !NULL        | Asynchronous flush on volatile  |
20980  * |              |              | cache; disk drivers may suppress|
20981  * |              |              | flush if disk table indicates   |
20982  * |              |              | non-volatile cache              |
20983  * |______________|______________|_________________________________|
20984  *
20985  */
20986 
20987 static int
20988 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20989 {
20990 	struct sd_uscsi_info	*uip;
20991 	struct uscsi_cmd	*uscmd;
20992 	union scsi_cdb		*cdb;
20993 	struct buf		*bp;
20994 	int			rval = 0;
20995 	int			is_async;
20996 
20997 	SD_TRACE(SD_LOG_IO, un,
20998 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20999 
21000 	ASSERT(un != NULL);
21001 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21002 
21003 	if (dkc == NULL || dkc->dkc_callback == NULL) {
21004 		is_async = FALSE;
21005 	} else {
21006 		is_async = TRUE;
21007 	}
21008 
21009 	mutex_enter(SD_MUTEX(un));
21010 	/* check whether cache flush should be suppressed */
21011 	if (un->un_f_suppress_cache_flush == TRUE) {
21012 		mutex_exit(SD_MUTEX(un));
21013 		/*
21014 		 * suppress the cache flush if the device is told to do
21015 		 * so by sd.conf or disk table
21016 		 */
21017 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21018 		    skip the cache flush since suppress_cache_flush is %d!\n",
21019 		    un->un_f_suppress_cache_flush);
21020 
21021 		if (is_async == TRUE) {
21022 			/* invoke callback for asynchronous flush */
21023 			(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21024 		}
21025 		return (rval);
21026 	}
21027 	mutex_exit(SD_MUTEX(un));
21028 
21029 	/*
21030 	 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21031 	 * set properly
21032 	 */
21033 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21034 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21035 
21036 	mutex_enter(SD_MUTEX(un));
21037 	if (dkc != NULL && un->un_f_sync_nv_supported &&
21038 	    (dkc->dkc_flag & FLUSH_VOLATILE)) {
21039 		/*
21040 		 * if the device supports SYNC_NV bit, turn on
21041 		 * the SYNC_NV bit to only flush volatile cache
21042 		 */
21043 		cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21044 	}
21045 	mutex_exit(SD_MUTEX(un));
21046 
21047 	/*
21048 	 * First get some memory for the uscsi_cmd struct and cdb
21049 	 * and initialize for SYNCHRONIZE_CACHE cmd.
21050 	 */
21051 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21052 	uscmd->uscsi_cdblen = CDB_GROUP1;
21053 	uscmd->uscsi_cdb = (caddr_t)cdb;
21054 	uscmd->uscsi_bufaddr = NULL;
21055 	uscmd->uscsi_buflen = 0;
21056 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21057 	uscmd->uscsi_rqlen = SENSE_LENGTH;
21058 	uscmd->uscsi_rqresid = SENSE_LENGTH;
21059 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21060 	uscmd->uscsi_timeout = sd_io_time;
21061 
21062 	/*
21063 	 * Allocate an sd_uscsi_info struct and fill it with the info
21064 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21065 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21066 	 * since we allocate the buf here in this function, we do not
21067 	 * need to preserve the prior contents of b_private.
21068 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21069 	 */
21070 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21071 	uip->ui_flags = SD_PATH_DIRECT;
21072 	uip->ui_cmdp  = uscmd;
21073 
21074 	bp = getrbuf(KM_SLEEP);
21075 	bp->b_private = uip;
21076 
21077 	/*
21078 	 * Setup buffer to carry uscsi request.
21079 	 */
21080 	bp->b_flags  = B_BUSY;
21081 	bp->b_bcount = 0;
21082 	bp->b_blkno  = 0;
21083 
21084 	if (is_async == TRUE) {
21085 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21086 		uip->ui_dkc = *dkc;
21087 	}
21088 
21089 	bp->b_edev = SD_GET_DEV(un);
21090 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
21091 
21092 	/*
21093 	 * Unset un_f_sync_cache_required flag
21094 	 */
21095 	mutex_enter(SD_MUTEX(un));
21096 	un->un_f_sync_cache_required = FALSE;
21097 	mutex_exit(SD_MUTEX(un));
21098 
21099 	(void) sd_uscsi_strategy(bp);
21100 
21101 	/*
21102 	 * If synchronous request, wait for completion
21103 	 * If async just return and let b_iodone callback
21104 	 * cleanup.
21105 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
21106 	 * but it was also incremented in sd_uscsi_strategy(), so
21107 	 * we should be ok.
21108 	 */
21109 	if (is_async == FALSE) {
21110 		(void) biowait(bp);
21111 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21112 	}
21113 
21114 	return (rval);
21115 }
21116 
21117 
21118 static int
21119 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21120 {
21121 	struct sd_uscsi_info *uip;
21122 	struct uscsi_cmd *uscmd;
21123 	uint8_t *sense_buf;
21124 	struct sd_lun *un;
21125 	int status;
21126 	union scsi_cdb *cdb;
21127 
21128 	uip = (struct sd_uscsi_info *)(bp->b_private);
21129 	ASSERT(uip != NULL);
21130 
21131 	uscmd = uip->ui_cmdp;
21132 	ASSERT(uscmd != NULL);
21133 
21134 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21135 	ASSERT(sense_buf != NULL);
21136 
21137 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21138 	ASSERT(un != NULL);
21139 
21140 	cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21141 
21142 	status = geterror(bp);
21143 	switch (status) {
21144 	case 0:
21145 		break;	/* Success! */
21146 	case EIO:
21147 		switch (uscmd->uscsi_status) {
21148 		case STATUS_RESERVATION_CONFLICT:
21149 			/* Ignore reservation conflict */
21150 			status = 0;
21151 			goto done;
21152 
21153 		case STATUS_CHECK:
21154 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21155 			    (scsi_sense_key(sense_buf) ==
21156 			    KEY_ILLEGAL_REQUEST)) {
21157 				/* Ignore Illegal Request error */
21158 				if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21159 					mutex_enter(SD_MUTEX(un));
21160 					un->un_f_sync_nv_supported = FALSE;
21161 					mutex_exit(SD_MUTEX(un));
21162 					status = 0;
21163 					SD_TRACE(SD_LOG_IO, un,
21164 					    "un_f_sync_nv_supported \
21165 					    is set to false.\n");
21166 					goto done;
21167 				}
21168 
21169 				mutex_enter(SD_MUTEX(un));
21170 				un->un_f_sync_cache_supported = FALSE;
21171 				mutex_exit(SD_MUTEX(un));
21172 				SD_TRACE(SD_LOG_IO, un,
21173 				    "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21174 				    un_f_sync_cache_supported set to false \
21175 				    with asc = %x, ascq = %x\n",
21176 				    scsi_sense_asc(sense_buf),
21177 				    scsi_sense_ascq(sense_buf));
21178 				status = ENOTSUP;
21179 				goto done;
21180 			}
21181 			break;
21182 		default:
21183 			break;
21184 		}
21185 		/* FALLTHRU */
21186 	default:
21187 		/*
21188 		 * Turn on the un_f_sync_cache_required flag
21189 		 * since the SYNC CACHE command failed
21190 		 */
21191 		mutex_enter(SD_MUTEX(un));
21192 		un->un_f_sync_cache_required = TRUE;
21193 		mutex_exit(SD_MUTEX(un));
21194 
21195 		/*
21196 		 * Don't log an error message if this device
21197 		 * has removable media.
21198 		 */
21199 		if (!un->un_f_has_removable_media) {
21200 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21201 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
21202 		}
21203 		break;
21204 	}
21205 
21206 done:
21207 	if (uip->ui_dkc.dkc_callback != NULL) {
21208 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21209 	}
21210 
21211 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
21212 	freerbuf(bp);
21213 	kmem_free(uip, sizeof (struct sd_uscsi_info));
21214 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21215 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21216 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
21217 
21218 	return (status);
21219 }
21220 
21221 
21222 /*
21223  *    Function: sd_send_scsi_GET_CONFIGURATION
21224  *
21225  * Description: Issues the get configuration command to the device.
21226  *		Called from sd_check_for_writable_cd & sd_get_media_info
21227  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21228  *   Arguments: ssc
21229  *		ucmdbuf
21230  *		rqbuf
21231  *		rqbuflen
21232  *		bufaddr
21233  *		buflen
21234  *		path_flag
21235  *
21236  * Return Code: 0   - Success
21237  *		errno return code from sd_ssc_send()
21238  *
21239  *     Context: Can sleep. Does not return until command is completed.
21240  *
21241  */
21242 
21243 static int
21244 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21245 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21246 	int path_flag)
21247 {
21248 	char	cdb[CDB_GROUP1];
21249 	int	status;
21250 	struct sd_lun	*un;
21251 
21252 	ASSERT(ssc != NULL);
21253 	un = ssc->ssc_un;
21254 	ASSERT(un != NULL);
21255 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21256 	ASSERT(bufaddr != NULL);
21257 	ASSERT(ucmdbuf != NULL);
21258 	ASSERT(rqbuf != NULL);
21259 
21260 	SD_TRACE(SD_LOG_IO, un,
21261 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21262 
21263 	bzero(cdb, sizeof (cdb));
21264 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21265 	bzero(rqbuf, rqbuflen);
21266 	bzero(bufaddr, buflen);
21267 
21268 	/*
21269 	 * Set up cdb field for the get configuration command.
21270 	 */
21271 	cdb[0] = SCMD_GET_CONFIGURATION;
21272 	cdb[1] = 0x02;  /* Requested Type */
21273 	cdb[8] = SD_PROFILE_HEADER_LEN;
21274 	ucmdbuf->uscsi_cdb = cdb;
21275 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21276 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21277 	ucmdbuf->uscsi_buflen = buflen;
21278 	ucmdbuf->uscsi_timeout = sd_io_time;
21279 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21280 	ucmdbuf->uscsi_rqlen = rqbuflen;
21281 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21282 
21283 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21284 	    UIO_SYSSPACE, path_flag);
21285 
21286 	switch (status) {
21287 	case 0:
21288 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21289 		break;  /* Success! */
21290 	case EIO:
21291 		switch (ucmdbuf->uscsi_status) {
21292 		case STATUS_RESERVATION_CONFLICT:
21293 			status = EACCES;
21294 			break;
21295 		default:
21296 			break;
21297 		}
21298 		break;
21299 	default:
21300 		break;
21301 	}
21302 
21303 	if (status == 0) {
21304 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21305 		    "sd_send_scsi_GET_CONFIGURATION: data",
21306 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21307 	}
21308 
21309 	SD_TRACE(SD_LOG_IO, un,
21310 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
21311 
21312 	return (status);
21313 }
21314 
21315 /*
21316  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21317  *
21318  * Description: Issues the get configuration command to the device to
21319  *              retrieve a specific feature. Called from
21320  *		sd_check_for_writable_cd & sd_set_mmc_caps.
21321  *   Arguments: ssc
21322  *              ucmdbuf
21323  *              rqbuf
21324  *              rqbuflen
21325  *              bufaddr
21326  *              buflen
21327  *		feature
21328  *
21329  * Return Code: 0   - Success
21330  *              errno return code from sd_ssc_send()
21331  *
21332  *     Context: Can sleep. Does not return until command is completed.
21333  *
21334  */
21335 static int
21336 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
21337 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
21338 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag)
21339 {
21340 	char    cdb[CDB_GROUP1];
21341 	int	status;
21342 	struct sd_lun	*un;
21343 
21344 	ASSERT(ssc != NULL);
21345 	un = ssc->ssc_un;
21346 	ASSERT(un != NULL);
21347 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21348 	ASSERT(bufaddr != NULL);
21349 	ASSERT(ucmdbuf != NULL);
21350 	ASSERT(rqbuf != NULL);
21351 
21352 	SD_TRACE(SD_LOG_IO, un,
21353 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21354 
21355 	bzero(cdb, sizeof (cdb));
21356 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21357 	bzero(rqbuf, rqbuflen);
21358 	bzero(bufaddr, buflen);
21359 
21360 	/*
21361 	 * Set up cdb field for the get configuration command.
21362 	 */
21363 	cdb[0] = SCMD_GET_CONFIGURATION;
21364 	cdb[1] = 0x02;  /* Requested Type */
21365 	cdb[3] = feature;
21366 	cdb[8] = buflen;
21367 	ucmdbuf->uscsi_cdb = cdb;
21368 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21369 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21370 	ucmdbuf->uscsi_buflen = buflen;
21371 	ucmdbuf->uscsi_timeout = sd_io_time;
21372 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21373 	ucmdbuf->uscsi_rqlen = rqbuflen;
21374 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21375 
21376 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21377 	    UIO_SYSSPACE, path_flag);
21378 
21379 	switch (status) {
21380 	case 0:
21381 
21382 		break;  /* Success! */
21383 	case EIO:
21384 		switch (ucmdbuf->uscsi_status) {
21385 		case STATUS_RESERVATION_CONFLICT:
21386 			status = EACCES;
21387 			break;
21388 		default:
21389 			break;
21390 		}
21391 		break;
21392 	default:
21393 		break;
21394 	}
21395 
21396 	if (status == 0) {
21397 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21398 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
21399 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21400 	}
21401 
21402 	SD_TRACE(SD_LOG_IO, un,
21403 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21404 
21405 	return (status);
21406 }
21407 
21408 
21409 /*
21410  *    Function: sd_send_scsi_MODE_SENSE
21411  *
21412  * Description: Utility function for issuing a scsi MODE SENSE command.
21413  *		Note: This routine uses a consistent implementation for Group0,
21414  *		Group1, and Group2 commands across all platforms. ATAPI devices
21415  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21416  *
21417  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21418  *                      structure for this target.
21419  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21420  *			  CDB_GROUP[1|2] (10 byte).
21421  *		bufaddr - buffer for page data retrieved from the target.
21422  *		buflen - size of page to be retrieved.
21423  *		page_code - page code of data to be retrieved from the target.
21424  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21425  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21426  *			to use the USCSI "direct" chain and bypass the normal
21427  *			command waitq.
21428  *
21429  * Return Code: 0   - Success
21430  *		errno return code from sd_ssc_send()
21431  *
21432  *     Context: Can sleep. Does not return until command is completed.
21433  */
21434 
21435 static int
21436 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21437 	size_t buflen,  uchar_t page_code, int path_flag)
21438 {
21439 	struct	scsi_extended_sense	sense_buf;
21440 	union scsi_cdb		cdb;
21441 	struct uscsi_cmd	ucmd_buf;
21442 	int			status;
21443 	int			headlen;
21444 	struct sd_lun		*un;
21445 
21446 	ASSERT(ssc != NULL);
21447 	un = ssc->ssc_un;
21448 	ASSERT(un != NULL);
21449 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21450 	ASSERT(bufaddr != NULL);
21451 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21452 	    (cdbsize == CDB_GROUP2));
21453 
21454 	SD_TRACE(SD_LOG_IO, un,
21455 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21456 
21457 	bzero(&cdb, sizeof (cdb));
21458 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21459 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21460 	bzero(bufaddr, buflen);
21461 
21462 	if (cdbsize == CDB_GROUP0) {
21463 		cdb.scc_cmd = SCMD_MODE_SENSE;
21464 		cdb.cdb_opaque[2] = page_code;
21465 		FORMG0COUNT(&cdb, buflen);
21466 		headlen = MODE_HEADER_LENGTH;
21467 	} else {
21468 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21469 		cdb.cdb_opaque[2] = page_code;
21470 		FORMG1COUNT(&cdb, buflen);
21471 		headlen = MODE_HEADER_LENGTH_GRP2;
21472 	}
21473 
21474 	ASSERT(headlen <= buflen);
21475 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21476 
21477 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21478 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21479 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21480 	ucmd_buf.uscsi_buflen	= buflen;
21481 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21482 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21483 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21484 	ucmd_buf.uscsi_timeout	= 60;
21485 
21486 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21487 	    UIO_SYSSPACE, path_flag);
21488 
21489 	switch (status) {
21490 	case 0:
21491 		/*
21492 		 * sr_check_wp() uses 0x3f page code and check the header of
21493 		 * mode page to determine if target device is write-protected.
21494 		 * But some USB devices return 0 bytes for 0x3f page code. For
21495 		 * this case, make sure that mode page header is returned at
21496 		 * least.
21497 		 */
21498 		if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21499 			status = EIO;
21500 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21501 			    "mode page header is not returned");
21502 		}
21503 		break;	/* Success! */
21504 	case EIO:
21505 		switch (ucmd_buf.uscsi_status) {
21506 		case STATUS_RESERVATION_CONFLICT:
21507 			status = EACCES;
21508 			break;
21509 		default:
21510 			break;
21511 		}
21512 		break;
21513 	default:
21514 		break;
21515 	}
21516 
21517 	if (status == 0) {
21518 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21519 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21520 	}
21521 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21522 
21523 	return (status);
21524 }
21525 
21526 
21527 /*
21528  *    Function: sd_send_scsi_MODE_SELECT
21529  *
21530  * Description: Utility function for issuing a scsi MODE SELECT command.
21531  *		Note: This routine uses a consistent implementation for Group0,
21532  *		Group1, and Group2 commands across all platforms. ATAPI devices
21533  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21534  *
21535  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21536  *                      structure for this target.
21537  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21538  *			  CDB_GROUP[1|2] (10 byte).
21539  *		bufaddr - buffer for page data retrieved from the target.
21540  *		buflen - size of page to be retrieved.
21541  *		save_page - boolean to determin if SP bit should be set.
21542  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21543  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21544  *			to use the USCSI "direct" chain and bypass the normal
21545  *			command waitq.
21546  *
21547  * Return Code: 0   - Success
21548  *		errno return code from sd_ssc_send()
21549  *
21550  *     Context: Can sleep. Does not return until command is completed.
21551  */
21552 
21553 static int
21554 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21555 	size_t buflen,  uchar_t save_page, int path_flag)
21556 {
21557 	struct	scsi_extended_sense	sense_buf;
21558 	union scsi_cdb		cdb;
21559 	struct uscsi_cmd	ucmd_buf;
21560 	int			status;
21561 	struct sd_lun		*un;
21562 
21563 	ASSERT(ssc != NULL);
21564 	un = ssc->ssc_un;
21565 	ASSERT(un != NULL);
21566 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21567 	ASSERT(bufaddr != NULL);
21568 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21569 	    (cdbsize == CDB_GROUP2));
21570 
21571 	SD_TRACE(SD_LOG_IO, un,
21572 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21573 
21574 	bzero(&cdb, sizeof (cdb));
21575 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21576 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21577 
21578 	/* Set the PF bit for many third party drives */
21579 	cdb.cdb_opaque[1] = 0x10;
21580 
21581 	/* Set the savepage(SP) bit if given */
21582 	if (save_page == SD_SAVE_PAGE) {
21583 		cdb.cdb_opaque[1] |= 0x01;
21584 	}
21585 
21586 	if (cdbsize == CDB_GROUP0) {
21587 		cdb.scc_cmd = SCMD_MODE_SELECT;
21588 		FORMG0COUNT(&cdb, buflen);
21589 	} else {
21590 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21591 		FORMG1COUNT(&cdb, buflen);
21592 	}
21593 
21594 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21595 
21596 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21597 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21598 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21599 	ucmd_buf.uscsi_buflen	= buflen;
21600 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21601 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21602 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21603 	ucmd_buf.uscsi_timeout	= 60;
21604 
21605 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21606 	    UIO_SYSSPACE, path_flag);
21607 
21608 	switch (status) {
21609 	case 0:
21610 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21611 		break;	/* Success! */
21612 	case EIO:
21613 		switch (ucmd_buf.uscsi_status) {
21614 		case STATUS_RESERVATION_CONFLICT:
21615 			status = EACCES;
21616 			break;
21617 		default:
21618 			break;
21619 		}
21620 		break;
21621 	default:
21622 		break;
21623 	}
21624 
21625 	if (status == 0) {
21626 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21627 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21628 	}
21629 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21630 
21631 	return (status);
21632 }
21633 
21634 
21635 /*
21636  *    Function: sd_send_scsi_RDWR
21637  *
21638  * Description: Issue a scsi READ or WRITE command with the given parameters.
21639  *
21640  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21641  *                      structure for this target.
21642  *		cmd:	 SCMD_READ or SCMD_WRITE
21643  *		bufaddr: Address of caller's buffer to receive the RDWR data
21644  *		buflen:  Length of caller's buffer receive the RDWR data.
21645  *		start_block: Block number for the start of the RDWR operation.
21646  *			 (Assumes target-native block size.)
21647  *		residp:  Pointer to variable to receive the redisual of the
21648  *			 RDWR operation (may be NULL of no residual requested).
21649  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21650  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21651  *			to use the USCSI "direct" chain and bypass the normal
21652  *			command waitq.
21653  *
21654  * Return Code: 0   - Success
21655  *		errno return code from sd_ssc_send()
21656  *
21657  *     Context: Can sleep. Does not return until command is completed.
21658  */
21659 
21660 static int
21661 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21662 	size_t buflen, daddr_t start_block, int path_flag)
21663 {
21664 	struct	scsi_extended_sense	sense_buf;
21665 	union scsi_cdb		cdb;
21666 	struct uscsi_cmd	ucmd_buf;
21667 	uint32_t		block_count;
21668 	int			status;
21669 	int			cdbsize;
21670 	uchar_t			flag;
21671 	struct sd_lun		*un;
21672 
21673 	ASSERT(ssc != NULL);
21674 	un = ssc->ssc_un;
21675 	ASSERT(un != NULL);
21676 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21677 	ASSERT(bufaddr != NULL);
21678 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21679 
21680 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21681 
21682 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21683 		return (EINVAL);
21684 	}
21685 
21686 	mutex_enter(SD_MUTEX(un));
21687 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21688 	mutex_exit(SD_MUTEX(un));
21689 
21690 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21691 
21692 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21693 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21694 	    bufaddr, buflen, start_block, block_count);
21695 
21696 	bzero(&cdb, sizeof (cdb));
21697 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21698 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21699 
21700 	/* Compute CDB size to use */
21701 	if (start_block > 0xffffffff)
21702 		cdbsize = CDB_GROUP4;
21703 	else if ((start_block & 0xFFE00000) ||
21704 	    (un->un_f_cfg_is_atapi == TRUE))
21705 		cdbsize = CDB_GROUP1;
21706 	else
21707 		cdbsize = CDB_GROUP0;
21708 
21709 	switch (cdbsize) {
21710 	case CDB_GROUP0:	/* 6-byte CDBs */
21711 		cdb.scc_cmd = cmd;
21712 		FORMG0ADDR(&cdb, start_block);
21713 		FORMG0COUNT(&cdb, block_count);
21714 		break;
21715 	case CDB_GROUP1:	/* 10-byte CDBs */
21716 		cdb.scc_cmd = cmd | SCMD_GROUP1;
21717 		FORMG1ADDR(&cdb, start_block);
21718 		FORMG1COUNT(&cdb, block_count);
21719 		break;
21720 	case CDB_GROUP4:	/* 16-byte CDBs */
21721 		cdb.scc_cmd = cmd | SCMD_GROUP4;
21722 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21723 		FORMG4COUNT(&cdb, block_count);
21724 		break;
21725 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
21726 	default:
21727 		/* All others reserved */
21728 		return (EINVAL);
21729 	}
21730 
21731 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21732 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21733 
21734 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21735 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21736 	ucmd_buf.uscsi_bufaddr	= bufaddr;
21737 	ucmd_buf.uscsi_buflen	= buflen;
21738 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21739 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21740 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
21741 	ucmd_buf.uscsi_timeout	= 60;
21742 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21743 	    UIO_SYSSPACE, path_flag);
21744 
21745 	switch (status) {
21746 	case 0:
21747 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21748 		break;	/* Success! */
21749 	case EIO:
21750 		switch (ucmd_buf.uscsi_status) {
21751 		case STATUS_RESERVATION_CONFLICT:
21752 			status = EACCES;
21753 			break;
21754 		default:
21755 			break;
21756 		}
21757 		break;
21758 	default:
21759 		break;
21760 	}
21761 
21762 	if (status == 0) {
21763 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21764 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21765 	}
21766 
21767 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21768 
21769 	return (status);
21770 }
21771 
21772 
21773 /*
21774  *    Function: sd_send_scsi_LOG_SENSE
21775  *
21776  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21777  *
21778  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21779  *                      structure for this target.
21780  *
21781  * Return Code: 0   - Success
21782  *		errno return code from sd_ssc_send()
21783  *
21784  *     Context: Can sleep. Does not return until command is completed.
21785  */
21786 
21787 static int
21788 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21789 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21790 	int path_flag)
21791 
21792 {
21793 	struct scsi_extended_sense	sense_buf;
21794 	union scsi_cdb		cdb;
21795 	struct uscsi_cmd	ucmd_buf;
21796 	int			status;
21797 	struct sd_lun		*un;
21798 
21799 	ASSERT(ssc != NULL);
21800 	un = ssc->ssc_un;
21801 	ASSERT(un != NULL);
21802 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21803 
21804 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21805 
21806 	bzero(&cdb, sizeof (cdb));
21807 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21808 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21809 
21810 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21811 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21812 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21813 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21814 	FORMG1COUNT(&cdb, buflen);
21815 
21816 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21817 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21818 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21819 	ucmd_buf.uscsi_buflen	= buflen;
21820 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21821 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21822 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21823 	ucmd_buf.uscsi_timeout	= 60;
21824 
21825 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21826 	    UIO_SYSSPACE, path_flag);
21827 
21828 	switch (status) {
21829 	case 0:
21830 		break;
21831 	case EIO:
21832 		switch (ucmd_buf.uscsi_status) {
21833 		case STATUS_RESERVATION_CONFLICT:
21834 			status = EACCES;
21835 			break;
21836 		case STATUS_CHECK:
21837 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21838 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21839 				KEY_ILLEGAL_REQUEST) &&
21840 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21841 				/*
21842 				 * ASC 0x24: INVALID FIELD IN CDB
21843 				 */
21844 				switch (page_code) {
21845 				case START_STOP_CYCLE_PAGE:
21846 					/*
21847 					 * The start stop cycle counter is
21848 					 * implemented as page 0x31 in earlier
21849 					 * generation disks. In new generation
21850 					 * disks the start stop cycle counter is
21851 					 * implemented as page 0xE. To properly
21852 					 * handle this case if an attempt for
21853 					 * log page 0xE is made and fails we
21854 					 * will try again using page 0x31.
21855 					 *
21856 					 * Network storage BU committed to
21857 					 * maintain the page 0x31 for this
21858 					 * purpose and will not have any other
21859 					 * page implemented with page code 0x31
21860 					 * until all disks transition to the
21861 					 * standard page.
21862 					 */
21863 					mutex_enter(SD_MUTEX(un));
21864 					un->un_start_stop_cycle_page =
21865 					    START_STOP_CYCLE_VU_PAGE;
21866 					cdb.cdb_opaque[2] =
21867 					    (char)(page_control << 6) |
21868 					    un->un_start_stop_cycle_page;
21869 					mutex_exit(SD_MUTEX(un));
21870 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
21871 					status = sd_ssc_send(
21872 					    ssc, &ucmd_buf, FKIOCTL,
21873 					    UIO_SYSSPACE, path_flag);
21874 
21875 					break;
21876 				case TEMPERATURE_PAGE:
21877 					status = ENOTTY;
21878 					break;
21879 				default:
21880 					break;
21881 				}
21882 			}
21883 			break;
21884 		default:
21885 			break;
21886 		}
21887 		break;
21888 	default:
21889 		break;
21890 	}
21891 
21892 	if (status == 0) {
21893 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21894 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
21895 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21896 	}
21897 
21898 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
21899 
21900 	return (status);
21901 }
21902 
21903 
21904 /*
21905  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
21906  *
21907  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
21908  *
21909  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21910  *                      structure for this target.
21911  *		bufaddr
21912  *		buflen
21913  *		class_req
21914  *
21915  * Return Code: 0   - Success
21916  *		errno return code from sd_ssc_send()
21917  *
21918  *     Context: Can sleep. Does not return until command is completed.
21919  */
21920 
21921 static int
21922 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
21923 	size_t buflen, uchar_t class_req)
21924 {
21925 	union scsi_cdb		cdb;
21926 	struct uscsi_cmd	ucmd_buf;
21927 	int			status;
21928 	struct sd_lun		*un;
21929 
21930 	ASSERT(ssc != NULL);
21931 	un = ssc->ssc_un;
21932 	ASSERT(un != NULL);
21933 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21934 	ASSERT(bufaddr != NULL);
21935 
21936 	SD_TRACE(SD_LOG_IO, un,
21937 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
21938 
21939 	bzero(&cdb, sizeof (cdb));
21940 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21941 	bzero(bufaddr, buflen);
21942 
21943 	cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
21944 	cdb.cdb_opaque[1] = 1; /* polled */
21945 	cdb.cdb_opaque[4] = class_req;
21946 	FORMG1COUNT(&cdb, buflen);
21947 
21948 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21949 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21950 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21951 	ucmd_buf.uscsi_buflen	= buflen;
21952 	ucmd_buf.uscsi_rqbuf	= NULL;
21953 	ucmd_buf.uscsi_rqlen	= 0;
21954 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
21955 	ucmd_buf.uscsi_timeout	= 60;
21956 
21957 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21958 	    UIO_SYSSPACE, SD_PATH_DIRECT);
21959 
21960 	/*
21961 	 * Only handle status == 0, the upper-level caller
21962 	 * will put different assessment based on the context.
21963 	 */
21964 	if (status == 0) {
21965 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21966 
21967 		if (ucmd_buf.uscsi_resid != 0) {
21968 			status = EIO;
21969 		}
21970 	}
21971 
21972 	SD_TRACE(SD_LOG_IO, un,
21973 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
21974 
21975 	return (status);
21976 }
21977 
21978 
21979 static boolean_t
21980 sd_gesn_media_data_valid(uchar_t *data)
21981 {
21982 	uint16_t			len;
21983 
21984 	len = (data[1] << 8) | data[0];
21985 	return ((len >= 6) &&
21986 	    ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
21987 	    ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
21988 	    ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
21989 }
21990 
21991 
21992 /*
21993  *    Function: sdioctl
21994  *
21995  * Description: Driver's ioctl(9e) entry point function.
21996  *
21997  *   Arguments: dev     - device number
21998  *		cmd     - ioctl operation to be performed
21999  *		arg     - user argument, contains data to be set or reference
22000  *			  parameter for get
22001  *		flag    - bit flag, indicating open settings, 32/64 bit type
22002  *		cred_p  - user credential pointer
22003  *		rval_p  - calling process return value (OPT)
22004  *
22005  * Return Code: EINVAL
22006  *		ENOTTY
22007  *		ENXIO
22008  *		EIO
22009  *		EFAULT
22010  *		ENOTSUP
22011  *		EPERM
22012  *
22013  *     Context: Called from the device switch at normal priority.
22014  */
22015 
22016 static int
22017 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22018 {
22019 	struct sd_lun	*un = NULL;
22020 	int		err = 0;
22021 	int		i = 0;
22022 	cred_t		*cr;
22023 	int		tmprval = EINVAL;
22024 	boolean_t	is_valid;
22025 	sd_ssc_t	*ssc;
22026 
22027 	/*
22028 	 * All device accesses go thru sdstrategy where we check on suspend
22029 	 * status
22030 	 */
22031 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22032 		return (ENXIO);
22033 	}
22034 
22035 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22036 
22037 	/* Initialize sd_ssc_t for internal uscsi commands */
22038 	ssc = sd_ssc_init(un);
22039 
22040 	is_valid = SD_IS_VALID_LABEL(un);
22041 
22042 	/*
22043 	 * Moved this wait from sd_uscsi_strategy to here for
22044 	 * reasons of deadlock prevention. Internal driver commands,
22045 	 * specifically those to change a devices power level, result
22046 	 * in a call to sd_uscsi_strategy.
22047 	 */
22048 	mutex_enter(SD_MUTEX(un));
22049 	while ((un->un_state == SD_STATE_SUSPENDED) ||
22050 	    (un->un_state == SD_STATE_PM_CHANGING)) {
22051 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22052 	}
22053 	/*
22054 	 * Twiddling the counter here protects commands from now
22055 	 * through to the top of sd_uscsi_strategy. Without the
22056 	 * counter inc. a power down, for example, could get in
22057 	 * after the above check for state is made and before
22058 	 * execution gets to the top of sd_uscsi_strategy.
22059 	 * That would cause problems.
22060 	 */
22061 	un->un_ncmds_in_driver++;
22062 
22063 	if (!is_valid &&
22064 	    (flag & (FNDELAY | FNONBLOCK))) {
22065 		switch (cmd) {
22066 		case DKIOCGGEOM:	/* SD_PATH_DIRECT */
22067 		case DKIOCGVTOC:
22068 		case DKIOCGEXTVTOC:
22069 		case DKIOCGAPART:
22070 		case DKIOCPARTINFO:
22071 		case DKIOCEXTPARTINFO:
22072 		case DKIOCSGEOM:
22073 		case DKIOCSAPART:
22074 		case DKIOCGETEFI:
22075 		case DKIOCPARTITION:
22076 		case DKIOCSVTOC:
22077 		case DKIOCSEXTVTOC:
22078 		case DKIOCSETEFI:
22079 		case DKIOCGMBOOT:
22080 		case DKIOCSMBOOT:
22081 		case DKIOCG_PHYGEOM:
22082 		case DKIOCG_VIRTGEOM:
22083 #if defined(__i386) || defined(__amd64)
22084 		case DKIOCSETEXTPART:
22085 #endif
22086 			/* let cmlb handle it */
22087 			goto skip_ready_valid;
22088 
22089 		case CDROMPAUSE:
22090 		case CDROMRESUME:
22091 		case CDROMPLAYMSF:
22092 		case CDROMPLAYTRKIND:
22093 		case CDROMREADTOCHDR:
22094 		case CDROMREADTOCENTRY:
22095 		case CDROMSTOP:
22096 		case CDROMSTART:
22097 		case CDROMVOLCTRL:
22098 		case CDROMSUBCHNL:
22099 		case CDROMREADMODE2:
22100 		case CDROMREADMODE1:
22101 		case CDROMREADOFFSET:
22102 		case CDROMSBLKMODE:
22103 		case CDROMGBLKMODE:
22104 		case CDROMGDRVSPEED:
22105 		case CDROMSDRVSPEED:
22106 		case CDROMCDDA:
22107 		case CDROMCDXA:
22108 		case CDROMSUBCODE:
22109 			if (!ISCD(un)) {
22110 				un->un_ncmds_in_driver--;
22111 				ASSERT(un->un_ncmds_in_driver >= 0);
22112 				mutex_exit(SD_MUTEX(un));
22113 				err = ENOTTY;
22114 				goto done_without_assess;
22115 			}
22116 			break;
22117 		case FDEJECT:
22118 		case DKIOCEJECT:
22119 		case CDROMEJECT:
22120 			if (!un->un_f_eject_media_supported) {
22121 				un->un_ncmds_in_driver--;
22122 				ASSERT(un->un_ncmds_in_driver >= 0);
22123 				mutex_exit(SD_MUTEX(un));
22124 				err = ENOTTY;
22125 				goto done_without_assess;
22126 			}
22127 			break;
22128 		case DKIOCFLUSHWRITECACHE:
22129 			mutex_exit(SD_MUTEX(un));
22130 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22131 			if (err != 0) {
22132 				mutex_enter(SD_MUTEX(un));
22133 				un->un_ncmds_in_driver--;
22134 				ASSERT(un->un_ncmds_in_driver >= 0);
22135 				mutex_exit(SD_MUTEX(un));
22136 				err = EIO;
22137 				goto done_quick_assess;
22138 			}
22139 			mutex_enter(SD_MUTEX(un));
22140 			/* FALLTHROUGH */
22141 		case DKIOCREMOVABLE:
22142 		case DKIOCHOTPLUGGABLE:
22143 		case DKIOCINFO:
22144 		case DKIOCGMEDIAINFO:
22145 		case DKIOCGMEDIAINFOEXT:
22146 		case MHIOCENFAILFAST:
22147 		case MHIOCSTATUS:
22148 		case MHIOCTKOWN:
22149 		case MHIOCRELEASE:
22150 		case MHIOCGRP_INKEYS:
22151 		case MHIOCGRP_INRESV:
22152 		case MHIOCGRP_REGISTER:
22153 		case MHIOCGRP_RESERVE:
22154 		case MHIOCGRP_PREEMPTANDABORT:
22155 		case MHIOCGRP_REGISTERANDIGNOREKEY:
22156 		case CDROMCLOSETRAY:
22157 		case USCSICMD:
22158 			goto skip_ready_valid;
22159 		default:
22160 			break;
22161 		}
22162 
22163 		mutex_exit(SD_MUTEX(un));
22164 		err = sd_ready_and_valid(ssc, SDPART(dev));
22165 		mutex_enter(SD_MUTEX(un));
22166 
22167 		if (err != SD_READY_VALID) {
22168 			switch (cmd) {
22169 			case DKIOCSTATE:
22170 			case CDROMGDRVSPEED:
22171 			case CDROMSDRVSPEED:
22172 			case FDEJECT:	/* for eject command */
22173 			case DKIOCEJECT:
22174 			case CDROMEJECT:
22175 			case DKIOCREMOVABLE:
22176 			case DKIOCHOTPLUGGABLE:
22177 				break;
22178 			default:
22179 				if (un->un_f_has_removable_media) {
22180 					err = ENXIO;
22181 				} else {
22182 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
22183 					if (err == SD_RESERVED_BY_OTHERS) {
22184 						err = EACCES;
22185 					} else {
22186 						err = EIO;
22187 					}
22188 				}
22189 				un->un_ncmds_in_driver--;
22190 				ASSERT(un->un_ncmds_in_driver >= 0);
22191 				mutex_exit(SD_MUTEX(un));
22192 
22193 				goto done_without_assess;
22194 			}
22195 		}
22196 	}
22197 
22198 skip_ready_valid:
22199 	mutex_exit(SD_MUTEX(un));
22200 
22201 	switch (cmd) {
22202 	case DKIOCINFO:
22203 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22204 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22205 		break;
22206 
22207 	case DKIOCGMEDIAINFO:
22208 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22209 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
22210 		break;
22211 
22212 	case DKIOCGMEDIAINFOEXT:
22213 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22214 		err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22215 		break;
22216 
22217 	case DKIOCGGEOM:
22218 	case DKIOCGVTOC:
22219 	case DKIOCGEXTVTOC:
22220 	case DKIOCGAPART:
22221 	case DKIOCPARTINFO:
22222 	case DKIOCEXTPARTINFO:
22223 	case DKIOCSGEOM:
22224 	case DKIOCSAPART:
22225 	case DKIOCGETEFI:
22226 	case DKIOCPARTITION:
22227 	case DKIOCSVTOC:
22228 	case DKIOCSEXTVTOC:
22229 	case DKIOCSETEFI:
22230 	case DKIOCGMBOOT:
22231 	case DKIOCSMBOOT:
22232 	case DKIOCG_PHYGEOM:
22233 	case DKIOCG_VIRTGEOM:
22234 #if defined(__i386) || defined(__amd64)
22235 	case DKIOCSETEXTPART:
22236 #endif
22237 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22238 
22239 		/* TUR should spin up */
22240 
22241 		if (un->un_f_has_removable_media)
22242 			err = sd_send_scsi_TEST_UNIT_READY(ssc,
22243 			    SD_CHECK_FOR_MEDIA);
22244 
22245 		else
22246 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22247 
22248 		if (err != 0)
22249 			goto done_with_assess;
22250 
22251 		err = cmlb_ioctl(un->un_cmlbhandle, dev,
22252 		    cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22253 
22254 		if ((err == 0) &&
22255 		    ((cmd == DKIOCSETEFI) ||
22256 		    (un->un_f_pkstats_enabled) &&
22257 		    (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22258 		    cmd == DKIOCSEXTVTOC))) {
22259 
22260 			tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22261 			    (void *)SD_PATH_DIRECT);
22262 			if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22263 				sd_set_pstats(un);
22264 				SD_TRACE(SD_LOG_IO_PARTITION, un,
22265 				    "sd_ioctl: un:0x%p pstats created and "
22266 				    "set\n", un);
22267 			}
22268 		}
22269 
22270 		if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22271 		    ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22272 
22273 			mutex_enter(SD_MUTEX(un));
22274 			if (un->un_f_devid_supported &&
22275 			    (un->un_f_opt_fab_devid == TRUE)) {
22276 				if (un->un_devid == NULL) {
22277 					sd_register_devid(ssc, SD_DEVINFO(un),
22278 					    SD_TARGET_IS_UNRESERVED);
22279 				} else {
22280 					/*
22281 					 * The device id for this disk
22282 					 * has been fabricated. The
22283 					 * device id must be preserved
22284 					 * by writing it back out to
22285 					 * disk.
22286 					 */
22287 					if (sd_write_deviceid(ssc) != 0) {
22288 						ddi_devid_free(un->un_devid);
22289 						un->un_devid = NULL;
22290 					}
22291 				}
22292 			}
22293 			mutex_exit(SD_MUTEX(un));
22294 		}
22295 
22296 		break;
22297 
22298 	case DKIOCLOCK:
22299 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22300 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22301 		    SD_PATH_STANDARD);
22302 		goto done_with_assess;
22303 
22304 	case DKIOCUNLOCK:
22305 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22306 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22307 		    SD_PATH_STANDARD);
22308 		goto done_with_assess;
22309 
22310 	case DKIOCSTATE: {
22311 		enum dkio_state		state;
22312 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22313 
22314 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22315 			err = EFAULT;
22316 		} else {
22317 			err = sd_check_media(dev, state);
22318 			if (err == 0) {
22319 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
22320 				    sizeof (int), flag) != 0)
22321 					err = EFAULT;
22322 			}
22323 		}
22324 		break;
22325 	}
22326 
22327 	case DKIOCREMOVABLE:
22328 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22329 		i = un->un_f_has_removable_media ? 1 : 0;
22330 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22331 			err = EFAULT;
22332 		} else {
22333 			err = 0;
22334 		}
22335 		break;
22336 
22337 	case DKIOCHOTPLUGGABLE:
22338 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22339 		i = un->un_f_is_hotpluggable ? 1 : 0;
22340 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22341 			err = EFAULT;
22342 		} else {
22343 			err = 0;
22344 		}
22345 		break;
22346 
22347 	case DKIOCGTEMPERATURE:
22348 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22349 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22350 		break;
22351 
22352 	case MHIOCENFAILFAST:
22353 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22354 		if ((err = drv_priv(cred_p)) == 0) {
22355 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22356 		}
22357 		break;
22358 
22359 	case MHIOCTKOWN:
22360 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22361 		if ((err = drv_priv(cred_p)) == 0) {
22362 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22363 		}
22364 		break;
22365 
22366 	case MHIOCRELEASE:
22367 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22368 		if ((err = drv_priv(cred_p)) == 0) {
22369 			err = sd_mhdioc_release(dev);
22370 		}
22371 		break;
22372 
22373 	case MHIOCSTATUS:
22374 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22375 		if ((err = drv_priv(cred_p)) == 0) {
22376 			switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22377 			case 0:
22378 				err = 0;
22379 				break;
22380 			case EACCES:
22381 				*rval_p = 1;
22382 				err = 0;
22383 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22384 				break;
22385 			default:
22386 				err = EIO;
22387 				goto done_with_assess;
22388 			}
22389 		}
22390 		break;
22391 
22392 	case MHIOCQRESERVE:
22393 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22394 		if ((err = drv_priv(cred_p)) == 0) {
22395 			err = sd_reserve_release(dev, SD_RESERVE);
22396 		}
22397 		break;
22398 
22399 	case MHIOCREREGISTERDEVID:
22400 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22401 		if (drv_priv(cred_p) == EPERM) {
22402 			err = EPERM;
22403 		} else if (!un->un_f_devid_supported) {
22404 			err = ENOTTY;
22405 		} else {
22406 			err = sd_mhdioc_register_devid(dev);
22407 		}
22408 		break;
22409 
22410 	case MHIOCGRP_INKEYS:
22411 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22412 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22413 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22414 				err = ENOTSUP;
22415 			} else {
22416 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22417 				    flag);
22418 			}
22419 		}
22420 		break;
22421 
22422 	case MHIOCGRP_INRESV:
22423 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22424 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22425 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22426 				err = ENOTSUP;
22427 			} else {
22428 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22429 			}
22430 		}
22431 		break;
22432 
22433 	case MHIOCGRP_REGISTER:
22434 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22435 		if ((err = drv_priv(cred_p)) != EPERM) {
22436 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22437 				err = ENOTSUP;
22438 			} else if (arg != NULL) {
22439 				mhioc_register_t reg;
22440 				if (ddi_copyin((void *)arg, &reg,
22441 				    sizeof (mhioc_register_t), flag) != 0) {
22442 					err = EFAULT;
22443 				} else {
22444 					err =
22445 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22446 					    ssc, SD_SCSI3_REGISTER,
22447 					    (uchar_t *)&reg);
22448 					if (err != 0)
22449 						goto done_with_assess;
22450 				}
22451 			}
22452 		}
22453 		break;
22454 
22455 	case MHIOCGRP_RESERVE:
22456 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22457 		if ((err = drv_priv(cred_p)) != EPERM) {
22458 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22459 				err = ENOTSUP;
22460 			} else if (arg != NULL) {
22461 				mhioc_resv_desc_t resv_desc;
22462 				if (ddi_copyin((void *)arg, &resv_desc,
22463 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
22464 					err = EFAULT;
22465 				} else {
22466 					err =
22467 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22468 					    ssc, SD_SCSI3_RESERVE,
22469 					    (uchar_t *)&resv_desc);
22470 					if (err != 0)
22471 						goto done_with_assess;
22472 				}
22473 			}
22474 		}
22475 		break;
22476 
22477 	case MHIOCGRP_PREEMPTANDABORT:
22478 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22479 		if ((err = drv_priv(cred_p)) != EPERM) {
22480 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22481 				err = ENOTSUP;
22482 			} else if (arg != NULL) {
22483 				mhioc_preemptandabort_t preempt_abort;
22484 				if (ddi_copyin((void *)arg, &preempt_abort,
22485 				    sizeof (mhioc_preemptandabort_t),
22486 				    flag) != 0) {
22487 					err = EFAULT;
22488 				} else {
22489 					err =
22490 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22491 					    ssc, SD_SCSI3_PREEMPTANDABORT,
22492 					    (uchar_t *)&preempt_abort);
22493 					if (err != 0)
22494 						goto done_with_assess;
22495 				}
22496 			}
22497 		}
22498 		break;
22499 
22500 	case MHIOCGRP_REGISTERANDIGNOREKEY:
22501 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22502 		if ((err = drv_priv(cred_p)) != EPERM) {
22503 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22504 				err = ENOTSUP;
22505 			} else if (arg != NULL) {
22506 				mhioc_registerandignorekey_t r_and_i;
22507 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
22508 				    sizeof (mhioc_registerandignorekey_t),
22509 				    flag) != 0) {
22510 					err = EFAULT;
22511 				} else {
22512 					err =
22513 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22514 					    ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22515 					    (uchar_t *)&r_and_i);
22516 					if (err != 0)
22517 						goto done_with_assess;
22518 				}
22519 			}
22520 		}
22521 		break;
22522 
22523 	case USCSICMD:
22524 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22525 		cr = ddi_get_cred();
22526 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22527 			err = EPERM;
22528 		} else {
22529 			enum uio_seg	uioseg;
22530 
22531 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22532 			    UIO_USERSPACE;
22533 			if (un->un_f_format_in_progress == TRUE) {
22534 				err = EAGAIN;
22535 				break;
22536 			}
22537 
22538 			err = sd_ssc_send(ssc,
22539 			    (struct uscsi_cmd *)arg,
22540 			    flag, uioseg, SD_PATH_STANDARD);
22541 			if (err != 0)
22542 				goto done_with_assess;
22543 			else
22544 				sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22545 		}
22546 		break;
22547 
22548 	case CDROMPAUSE:
22549 	case CDROMRESUME:
22550 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22551 		if (!ISCD(un)) {
22552 			err = ENOTTY;
22553 		} else {
22554 			err = sr_pause_resume(dev, cmd);
22555 		}
22556 		break;
22557 
22558 	case CDROMPLAYMSF:
22559 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22560 		if (!ISCD(un)) {
22561 			err = ENOTTY;
22562 		} else {
22563 			err = sr_play_msf(dev, (caddr_t)arg, flag);
22564 		}
22565 		break;
22566 
22567 	case CDROMPLAYTRKIND:
22568 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22569 #if defined(__i386) || defined(__amd64)
22570 		/*
22571 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22572 		 */
22573 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22574 #else
22575 		if (!ISCD(un)) {
22576 #endif
22577 			err = ENOTTY;
22578 		} else {
22579 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
22580 		}
22581 		break;
22582 
22583 	case CDROMREADTOCHDR:
22584 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22585 		if (!ISCD(un)) {
22586 			err = ENOTTY;
22587 		} else {
22588 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22589 		}
22590 		break;
22591 
22592 	case CDROMREADTOCENTRY:
22593 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22594 		if (!ISCD(un)) {
22595 			err = ENOTTY;
22596 		} else {
22597 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22598 		}
22599 		break;
22600 
22601 	case CDROMSTOP:
22602 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22603 		if (!ISCD(un)) {
22604 			err = ENOTTY;
22605 		} else {
22606 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22607 			    SD_TARGET_STOP, SD_PATH_STANDARD);
22608 			goto done_with_assess;
22609 		}
22610 		break;
22611 
22612 	case CDROMSTART:
22613 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22614 		if (!ISCD(un)) {
22615 			err = ENOTTY;
22616 		} else {
22617 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22618 			    SD_TARGET_START, SD_PATH_STANDARD);
22619 			goto done_with_assess;
22620 		}
22621 		break;
22622 
22623 	case CDROMCLOSETRAY:
22624 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22625 		if (!ISCD(un)) {
22626 			err = ENOTTY;
22627 		} else {
22628 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22629 			    SD_TARGET_CLOSE, SD_PATH_STANDARD);
22630 			goto done_with_assess;
22631 		}
22632 		break;
22633 
22634 	case FDEJECT:	/* for eject command */
22635 	case DKIOCEJECT:
22636 	case CDROMEJECT:
22637 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22638 		if (!un->un_f_eject_media_supported) {
22639 			err = ENOTTY;
22640 		} else {
22641 			err = sr_eject(dev);
22642 		}
22643 		break;
22644 
22645 	case CDROMVOLCTRL:
22646 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22647 		if (!ISCD(un)) {
22648 			err = ENOTTY;
22649 		} else {
22650 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22651 		}
22652 		break;
22653 
22654 	case CDROMSUBCHNL:
22655 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22656 		if (!ISCD(un)) {
22657 			err = ENOTTY;
22658 		} else {
22659 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22660 		}
22661 		break;
22662 
22663 	case CDROMREADMODE2:
22664 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22665 		if (!ISCD(un)) {
22666 			err = ENOTTY;
22667 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22668 			/*
22669 			 * If the drive supports READ CD, use that instead of
22670 			 * switching the LBA size via a MODE SELECT
22671 			 * Block Descriptor
22672 			 */
22673 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22674 		} else {
22675 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
22676 		}
22677 		break;
22678 
22679 	case CDROMREADMODE1:
22680 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22681 		if (!ISCD(un)) {
22682 			err = ENOTTY;
22683 		} else {
22684 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
22685 		}
22686 		break;
22687 
22688 	case CDROMREADOFFSET:
22689 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22690 		if (!ISCD(un)) {
22691 			err = ENOTTY;
22692 		} else {
22693 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22694 			    flag);
22695 		}
22696 		break;
22697 
22698 	case CDROMSBLKMODE:
22699 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22700 		/*
22701 		 * There is no means of changing block size in case of atapi
22702 		 * drives, thus return ENOTTY if drive type is atapi
22703 		 */
22704 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22705 			err = ENOTTY;
22706 		} else if (un->un_f_mmc_cap == TRUE) {
22707 
22708 			/*
22709 			 * MMC Devices do not support changing the
22710 			 * logical block size
22711 			 *
22712 			 * Note: EINVAL is being returned instead of ENOTTY to
22713 			 * maintain consistancy with the original mmc
22714 			 * driver update.
22715 			 */
22716 			err = EINVAL;
22717 		} else {
22718 			mutex_enter(SD_MUTEX(un));
22719 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22720 			    (un->un_ncmds_in_transport > 0)) {
22721 				mutex_exit(SD_MUTEX(un));
22722 				err = EINVAL;
22723 			} else {
22724 				mutex_exit(SD_MUTEX(un));
22725 				err = sr_change_blkmode(dev, cmd, arg, flag);
22726 			}
22727 		}
22728 		break;
22729 
22730 	case CDROMGBLKMODE:
22731 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22732 		if (!ISCD(un)) {
22733 			err = ENOTTY;
22734 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
22735 		    (un->un_f_blockcount_is_valid != FALSE)) {
22736 			/*
22737 			 * Drive is an ATAPI drive so return target block
22738 			 * size for ATAPI drives since we cannot change the
22739 			 * blocksize on ATAPI drives. Used primarily to detect
22740 			 * if an ATAPI cdrom is present.
22741 			 */
22742 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22743 			    sizeof (int), flag) != 0) {
22744 				err = EFAULT;
22745 			} else {
22746 				err = 0;
22747 			}
22748 
22749 		} else {
22750 			/*
22751 			 * Drive supports changing block sizes via a Mode
22752 			 * Select.
22753 			 */
22754 			err = sr_change_blkmode(dev, cmd, arg, flag);
22755 		}
22756 		break;
22757 
22758 	case CDROMGDRVSPEED:
22759 	case CDROMSDRVSPEED:
22760 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22761 		if (!ISCD(un)) {
22762 			err = ENOTTY;
22763 		} else if (un->un_f_mmc_cap == TRUE) {
22764 			/*
22765 			 * Note: In the future the driver implementation
22766 			 * for getting and
22767 			 * setting cd speed should entail:
22768 			 * 1) If non-mmc try the Toshiba mode page
22769 			 *    (sr_change_speed)
22770 			 * 2) If mmc but no support for Real Time Streaming try
22771 			 *    the SET CD SPEED (0xBB) command
22772 			 *   (sr_atapi_change_speed)
22773 			 * 3) If mmc and support for Real Time Streaming
22774 			 *    try the GET PERFORMANCE and SET STREAMING
22775 			 *    commands (not yet implemented, 4380808)
22776 			 */
22777 			/*
22778 			 * As per recent MMC spec, CD-ROM speed is variable
22779 			 * and changes with LBA. Since there is no such
22780 			 * things as drive speed now, fail this ioctl.
22781 			 *
22782 			 * Note: EINVAL is returned for consistancy of original
22783 			 * implementation which included support for getting
22784 			 * the drive speed of mmc devices but not setting
22785 			 * the drive speed. Thus EINVAL would be returned
22786 			 * if a set request was made for an mmc device.
22787 			 * We no longer support get or set speed for
22788 			 * mmc but need to remain consistent with regard
22789 			 * to the error code returned.
22790 			 */
22791 			err = EINVAL;
22792 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22793 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
22794 		} else {
22795 			err = sr_change_speed(dev, cmd, arg, flag);
22796 		}
22797 		break;
22798 
22799 	case CDROMCDDA:
22800 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22801 		if (!ISCD(un)) {
22802 			err = ENOTTY;
22803 		} else {
22804 			err = sr_read_cdda(dev, (void *)arg, flag);
22805 		}
22806 		break;
22807 
22808 	case CDROMCDXA:
22809 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
22810 		if (!ISCD(un)) {
22811 			err = ENOTTY;
22812 		} else {
22813 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
22814 		}
22815 		break;
22816 
22817 	case CDROMSUBCODE:
22818 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
22819 		if (!ISCD(un)) {
22820 			err = ENOTTY;
22821 		} else {
22822 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
22823 		}
22824 		break;
22825 
22826 
22827 #ifdef SDDEBUG
22828 /* RESET/ABORTS testing ioctls */
22829 	case DKIOCRESET: {
22830 		int	reset_level;
22831 
22832 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
22833 			err = EFAULT;
22834 		} else {
22835 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
22836 			    "reset_level = 0x%lx\n", reset_level);
22837 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
22838 				err = 0;
22839 			} else {
22840 				err = EIO;
22841 			}
22842 		}
22843 		break;
22844 	}
22845 
22846 	case DKIOCABORT:
22847 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
22848 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
22849 			err = 0;
22850 		} else {
22851 			err = EIO;
22852 		}
22853 		break;
22854 #endif
22855 
22856 #ifdef SD_FAULT_INJECTION
22857 /* SDIOC FaultInjection testing ioctls */
22858 	case SDIOCSTART:
22859 	case SDIOCSTOP:
22860 	case SDIOCINSERTPKT:
22861 	case SDIOCINSERTXB:
22862 	case SDIOCINSERTUN:
22863 	case SDIOCINSERTARQ:
22864 	case SDIOCPUSH:
22865 	case SDIOCRETRIEVE:
22866 	case SDIOCRUN:
22867 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
22868 		    "SDIOC detected cmd:0x%X:\n", cmd);
22869 		/* call error generator */
22870 		sd_faultinjection_ioctl(cmd, arg, un);
22871 		err = 0;
22872 		break;
22873 
22874 #endif /* SD_FAULT_INJECTION */
22875 
22876 	case DKIOCFLUSHWRITECACHE:
22877 		{
22878 			struct dk_callback *dkc = (struct dk_callback *)arg;
22879 
22880 			mutex_enter(SD_MUTEX(un));
22881 			if (!un->un_f_sync_cache_supported ||
22882 			    !un->un_f_write_cache_enabled) {
22883 				err = un->un_f_sync_cache_supported ?
22884 				    0 : ENOTSUP;
22885 				mutex_exit(SD_MUTEX(un));
22886 				if ((flag & FKIOCTL) && dkc != NULL &&
22887 				    dkc->dkc_callback != NULL) {
22888 					(*dkc->dkc_callback)(dkc->dkc_cookie,
22889 					    err);
22890 					/*
22891 					 * Did callback and reported error.
22892 					 * Since we did a callback, ioctl
22893 					 * should return 0.
22894 					 */
22895 					err = 0;
22896 				}
22897 				break;
22898 			}
22899 			mutex_exit(SD_MUTEX(un));
22900 
22901 			if ((flag & FKIOCTL) && dkc != NULL &&
22902 			    dkc->dkc_callback != NULL) {
22903 				/* async SYNC CACHE request */
22904 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
22905 			} else {
22906 				/* synchronous SYNC CACHE request */
22907 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
22908 			}
22909 		}
22910 		break;
22911 
22912 	case DKIOCGETWCE: {
22913 
22914 		int wce;
22915 
22916 		if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
22917 			break;
22918 		}
22919 
22920 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
22921 			err = EFAULT;
22922 		}
22923 		break;
22924 	}
22925 
22926 	case DKIOCSETWCE: {
22927 
22928 		int wce, sync_supported;
22929 
22930 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
22931 			err = EFAULT;
22932 			break;
22933 		}
22934 
22935 		/*
22936 		 * Synchronize multiple threads trying to enable
22937 		 * or disable the cache via the un_f_wcc_cv
22938 		 * condition variable.
22939 		 */
22940 		mutex_enter(SD_MUTEX(un));
22941 
22942 		/*
22943 		 * Don't allow the cache to be enabled if the
22944 		 * config file has it disabled.
22945 		 */
22946 		if (un->un_f_opt_disable_cache && wce) {
22947 			mutex_exit(SD_MUTEX(un));
22948 			err = EINVAL;
22949 			break;
22950 		}
22951 
22952 		/*
22953 		 * Wait for write cache change in progress
22954 		 * bit to be clear before proceeding.
22955 		 */
22956 		while (un->un_f_wcc_inprog)
22957 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
22958 
22959 		un->un_f_wcc_inprog = 1;
22960 
22961 		if (un->un_f_write_cache_enabled && wce == 0) {
22962 			/*
22963 			 * Disable the write cache.  Don't clear
22964 			 * un_f_write_cache_enabled until after
22965 			 * the mode select and flush are complete.
22966 			 */
22967 			sync_supported = un->un_f_sync_cache_supported;
22968 
22969 			/*
22970 			 * If cache flush is suppressed, we assume that the
22971 			 * controller firmware will take care of managing the
22972 			 * write cache for us: no need to explicitly
22973 			 * disable it.
22974 			 */
22975 			if (!un->un_f_suppress_cache_flush) {
22976 				mutex_exit(SD_MUTEX(un));
22977 				if ((err = sd_cache_control(ssc,
22978 				    SD_CACHE_NOCHANGE,
22979 				    SD_CACHE_DISABLE)) == 0 &&
22980 				    sync_supported) {
22981 					err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
22982 					    NULL);
22983 				}
22984 			} else {
22985 				mutex_exit(SD_MUTEX(un));
22986 			}
22987 
22988 			mutex_enter(SD_MUTEX(un));
22989 			if (err == 0) {
22990 				un->un_f_write_cache_enabled = 0;
22991 			}
22992 
22993 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
22994 			/*
22995 			 * Set un_f_write_cache_enabled first, so there is
22996 			 * no window where the cache is enabled, but the
22997 			 * bit says it isn't.
22998 			 */
22999 			un->un_f_write_cache_enabled = 1;
23000 
23001 			/*
23002 			 * If cache flush is suppressed, we assume that the
23003 			 * controller firmware will take care of managing the
23004 			 * write cache for us: no need to explicitly
23005 			 * enable it.
23006 			 */
23007 			if (!un->un_f_suppress_cache_flush) {
23008 				mutex_exit(SD_MUTEX(un));
23009 				err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23010 				    SD_CACHE_ENABLE);
23011 			} else {
23012 				mutex_exit(SD_MUTEX(un));
23013 			}
23014 
23015 			mutex_enter(SD_MUTEX(un));
23016 
23017 			if (err) {
23018 				un->un_f_write_cache_enabled = 0;
23019 			}
23020 		}
23021 
23022 		un->un_f_wcc_inprog = 0;
23023 		cv_broadcast(&un->un_wcc_cv);
23024 		mutex_exit(SD_MUTEX(un));
23025 		break;
23026 	}
23027 
23028 	default:
23029 		err = ENOTTY;
23030 		break;
23031 	}
23032 	mutex_enter(SD_MUTEX(un));
23033 	un->un_ncmds_in_driver--;
23034 	ASSERT(un->un_ncmds_in_driver >= 0);
23035 	mutex_exit(SD_MUTEX(un));
23036 
23037 
23038 done_without_assess:
23039 	sd_ssc_fini(ssc);
23040 
23041 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23042 	return (err);
23043 
23044 done_with_assess:
23045 	mutex_enter(SD_MUTEX(un));
23046 	un->un_ncmds_in_driver--;
23047 	ASSERT(un->un_ncmds_in_driver >= 0);
23048 	mutex_exit(SD_MUTEX(un));
23049 
23050 done_quick_assess:
23051 	if (err != 0)
23052 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23053 	/* Uninitialize sd_ssc_t pointer */
23054 	sd_ssc_fini(ssc);
23055 
23056 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23057 	return (err);
23058 }
23059 
23060 
23061 /*
23062  *    Function: sd_dkio_ctrl_info
23063  *
23064  * Description: This routine is the driver entry point for handling controller
23065  *		information ioctl requests (DKIOCINFO).
23066  *
23067  *   Arguments: dev  - the device number
23068  *		arg  - pointer to user provided dk_cinfo structure
23069  *		       specifying the controller type and attributes.
23070  *		flag - this argument is a pass through to ddi_copyxxx()
23071  *		       directly from the mode argument of ioctl().
23072  *
23073  * Return Code: 0
23074  *		EFAULT
23075  *		ENXIO
23076  */
23077 
23078 static int
23079 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23080 {
23081 	struct sd_lun	*un = NULL;
23082 	struct dk_cinfo	*info;
23083 	dev_info_t	*pdip;
23084 	int		lun, tgt;
23085 
23086 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23087 		return (ENXIO);
23088 	}
23089 
23090 	info = (struct dk_cinfo *)
23091 	    kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23092 
23093 	switch (un->un_ctype) {
23094 	case CTYPE_CDROM:
23095 		info->dki_ctype = DKC_CDROM;
23096 		break;
23097 	default:
23098 		info->dki_ctype = DKC_SCSI_CCS;
23099 		break;
23100 	}
23101 	pdip = ddi_get_parent(SD_DEVINFO(un));
23102 	info->dki_cnum = ddi_get_instance(pdip);
23103 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23104 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
23105 	} else {
23106 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
23107 		    DK_DEVLEN - 1);
23108 	}
23109 
23110 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23111 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23112 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23113 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23114 
23115 	/* Unit Information */
23116 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23117 	info->dki_slave = ((tgt << 3) | lun);
23118 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23119 	    DK_DEVLEN - 1);
23120 	info->dki_flags = DKI_FMTVOL;
23121 	info->dki_partition = SDPART(dev);
23122 
23123 	/* Max Transfer size of this device in blocks */
23124 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23125 	info->dki_addr = 0;
23126 	info->dki_space = 0;
23127 	info->dki_prio = 0;
23128 	info->dki_vec = 0;
23129 
23130 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23131 		kmem_free(info, sizeof (struct dk_cinfo));
23132 		return (EFAULT);
23133 	} else {
23134 		kmem_free(info, sizeof (struct dk_cinfo));
23135 		return (0);
23136 	}
23137 }
23138 
23139 
23140 /*
23141  *    Function: sd_get_media_info
23142  *
23143  * Description: This routine is the driver entry point for handling ioctl
23144  *		requests for the media type or command set profile used by the
23145  *		drive to operate on the media (DKIOCGMEDIAINFO).
23146  *
23147  *   Arguments: dev	- the device number
23148  *		arg	- pointer to user provided dk_minfo structure
23149  *			  specifying the media type, logical block size and
23150  *			  drive capacity.
23151  *		flag	- this argument is a pass through to ddi_copyxxx()
23152  *			  directly from the mode argument of ioctl().
23153  *
23154  * Return Code: 0
23155  *		EACCESS
23156  *		EFAULT
23157  *		ENXIO
23158  *		EIO
23159  */
23160 
23161 static int
23162 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23163 {
23164 	struct sd_lun		*un = NULL;
23165 	struct uscsi_cmd	com;
23166 	struct scsi_inquiry	*sinq;
23167 	struct dk_minfo		media_info;
23168 	u_longlong_t		media_capacity;
23169 	uint64_t		capacity;
23170 	uint_t			lbasize;
23171 	uchar_t			*out_data;
23172 	uchar_t			*rqbuf;
23173 	int			rval = 0;
23174 	int			rtn;
23175 	sd_ssc_t		*ssc;
23176 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23177 	    (un->un_state == SD_STATE_OFFLINE)) {
23178 		return (ENXIO);
23179 	}
23180 
23181 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
23182 
23183 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23184 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23185 
23186 	/* Issue a TUR to determine if the drive is ready with media present */
23187 	ssc = sd_ssc_init(un);
23188 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23189 	if (rval == ENXIO) {
23190 		goto done;
23191 	} else if (rval != 0) {
23192 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23193 	}
23194 
23195 	/* Now get configuration data */
23196 	if (ISCD(un)) {
23197 		media_info.dki_media_type = DK_CDROM;
23198 
23199 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23200 		if (un->un_f_mmc_cap == TRUE) {
23201 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23202 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23203 			    SD_PATH_STANDARD);
23204 
23205 			if (rtn) {
23206 				/*
23207 				 * We ignore all failures for CD and need to
23208 				 * put the assessment before processing code
23209 				 * to avoid missing assessment for FMA.
23210 				 */
23211 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23212 				/*
23213 				 * Failed for other than an illegal request
23214 				 * or command not supported
23215 				 */
23216 				if ((com.uscsi_status == STATUS_CHECK) &&
23217 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23218 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23219 					    (rqbuf[12] != 0x20)) {
23220 						rval = EIO;
23221 						goto no_assessment;
23222 					}
23223 				}
23224 			} else {
23225 				/*
23226 				 * The GET CONFIGURATION command succeeded
23227 				 * so set the media type according to the
23228 				 * returned data
23229 				 */
23230 				media_info.dki_media_type = out_data[6];
23231 				media_info.dki_media_type <<= 8;
23232 				media_info.dki_media_type |= out_data[7];
23233 			}
23234 		}
23235 	} else {
23236 		/*
23237 		 * The profile list is not available, so we attempt to identify
23238 		 * the media type based on the inquiry data
23239 		 */
23240 		sinq = un->un_sd->sd_inq;
23241 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23242 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23243 			/* This is a direct access device  or optical disk */
23244 			media_info.dki_media_type = DK_FIXED_DISK;
23245 
23246 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23247 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23248 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23249 					media_info.dki_media_type = DK_ZIP;
23250 				} else if (
23251 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23252 					media_info.dki_media_type = DK_JAZ;
23253 				}
23254 			}
23255 		} else {
23256 			/*
23257 			 * Not a CD, direct access or optical disk so return
23258 			 * unknown media
23259 			 */
23260 			media_info.dki_media_type = DK_UNKNOWN;
23261 		}
23262 	}
23263 
23264 	/* Now read the capacity so we can provide the lbasize and capacity */
23265 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23266 	    SD_PATH_DIRECT);
23267 	switch (rval) {
23268 	case 0:
23269 		break;
23270 	case EACCES:
23271 		rval = EACCES;
23272 		goto done;
23273 	default:
23274 		rval = EIO;
23275 		goto done;
23276 	}
23277 
23278 	/*
23279 	 * If lun is expanded dynamically, update the un structure.
23280 	 */
23281 	mutex_enter(SD_MUTEX(un));
23282 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23283 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23284 	    (capacity > un->un_blockcount)) {
23285 		sd_update_block_info(un, lbasize, capacity);
23286 	}
23287 	mutex_exit(SD_MUTEX(un));
23288 
23289 	media_info.dki_lbsize = lbasize;
23290 	media_capacity = capacity;
23291 
23292 	/*
23293 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
23294 	 * un->un_sys_blocksize chunks. So we need to convert it into
23295 	 * cap.lbasize chunks.
23296 	 */
23297 	media_capacity *= un->un_sys_blocksize;
23298 	media_capacity /= lbasize;
23299 	media_info.dki_capacity = media_capacity;
23300 
23301 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
23302 		rval = EFAULT;
23303 		/* Put goto. Anybody might add some code below in future */
23304 		goto no_assessment;
23305 	}
23306 done:
23307 	if (rval != 0) {
23308 		if (rval == EIO)
23309 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23310 		else
23311 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23312 	}
23313 no_assessment:
23314 	sd_ssc_fini(ssc);
23315 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23316 	kmem_free(rqbuf, SENSE_LENGTH);
23317 	return (rval);
23318 }
23319 
23320 /*
23321  *    Function: sd_get_media_info_ext
23322  *
23323  * Description: This routine is the driver entry point for handling ioctl
23324  *		requests for the media type or command set profile used by the
23325  *		drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23326  *		difference this ioctl and DKIOCGMEDIAINFO is the return value
23327  *		of this ioctl contains both logical block size and physical
23328  *		block size.
23329  *
23330  *
23331  *   Arguments: dev	- the device number
23332  *		arg	- pointer to user provided dk_minfo_ext structure
23333  *			  specifying the media type, logical block size,
23334  *			  physical block size and disk capacity.
23335  *		flag	- this argument is a pass through to ddi_copyxxx()
23336  *			  directly from the mode argument of ioctl().
23337  *
23338  * Return Code: 0
23339  *		EACCESS
23340  *		EFAULT
23341  *		ENXIO
23342  *		EIO
23343  */
23344 
23345 static int
23346 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23347 {
23348 	struct sd_lun		*un = NULL;
23349 	struct uscsi_cmd	com;
23350 	struct scsi_inquiry	*sinq;
23351 	struct dk_minfo_ext	media_info_ext;
23352 	u_longlong_t		media_capacity;
23353 	uint64_t		capacity;
23354 	uint_t			lbasize;
23355 	uint_t			pbsize;
23356 	uchar_t			*out_data;
23357 	uchar_t			*rqbuf;
23358 	int			rval = 0;
23359 	int			rtn;
23360 	sd_ssc_t		*ssc;
23361 
23362 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23363 	    (un->un_state == SD_STATE_OFFLINE)) {
23364 		return (ENXIO);
23365 	}
23366 
23367 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_ext: entry\n");
23368 
23369 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23370 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23371 	ssc = sd_ssc_init(un);
23372 
23373 	/* Issue a TUR to determine if the drive is ready with media present */
23374 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23375 	if (rval == ENXIO) {
23376 		goto done;
23377 	} else if (rval != 0) {
23378 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23379 	}
23380 
23381 	/* Now get configuration data */
23382 	if (ISCD(un)) {
23383 		media_info_ext.dki_media_type = DK_CDROM;
23384 
23385 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23386 		if (un->un_f_mmc_cap == TRUE) {
23387 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23388 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23389 			    SD_PATH_STANDARD);
23390 
23391 			if (rtn) {
23392 				/*
23393 				 * We ignore all failures for CD and need to
23394 				 * put the assessment before processing code
23395 				 * to avoid missing assessment for FMA.
23396 				 */
23397 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23398 				/*
23399 				 * Failed for other than an illegal request
23400 				 * or command not supported
23401 				 */
23402 				if ((com.uscsi_status == STATUS_CHECK) &&
23403 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23404 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23405 					    (rqbuf[12] != 0x20)) {
23406 						rval = EIO;
23407 						goto no_assessment;
23408 					}
23409 				}
23410 			} else {
23411 				/*
23412 				 * The GET CONFIGURATION command succeeded
23413 				 * so set the media type according to the
23414 				 * returned data
23415 				 */
23416 				media_info_ext.dki_media_type = out_data[6];
23417 				media_info_ext.dki_media_type <<= 8;
23418 				media_info_ext.dki_media_type |= out_data[7];
23419 			}
23420 		}
23421 	} else {
23422 		/*
23423 		 * The profile list is not available, so we attempt to identify
23424 		 * the media type based on the inquiry data
23425 		 */
23426 		sinq = un->un_sd->sd_inq;
23427 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23428 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23429 			/* This is a direct access device  or optical disk */
23430 			media_info_ext.dki_media_type = DK_FIXED_DISK;
23431 
23432 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23433 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23434 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23435 					media_info_ext.dki_media_type = DK_ZIP;
23436 				} else if (
23437 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23438 					media_info_ext.dki_media_type = DK_JAZ;
23439 				}
23440 			}
23441 		} else {
23442 			/*
23443 			 * Not a CD, direct access or optical disk so return
23444 			 * unknown media
23445 			 */
23446 			media_info_ext.dki_media_type = DK_UNKNOWN;
23447 		}
23448 	}
23449 
23450 	/*
23451 	 * Now read the capacity so we can provide the lbasize,
23452 	 * pbsize and capacity.
23453 	 */
23454 	rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize, &pbsize,
23455 	    SD_PATH_DIRECT);
23456 
23457 	if (rval != 0) {
23458 		rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23459 		    SD_PATH_DIRECT);
23460 
23461 		switch (rval) {
23462 		case 0:
23463 			pbsize = lbasize;
23464 			media_capacity = capacity;
23465 			/*
23466 			 * sd_send_scsi_READ_CAPACITY() reports capacity in
23467 			 * un->un_sys_blocksize chunks. So we need to convert
23468 			 * it into cap.lbsize chunks.
23469 			 */
23470 			if (un->un_f_has_removable_media) {
23471 				media_capacity *= un->un_sys_blocksize;
23472 				media_capacity /= lbasize;
23473 			}
23474 			break;
23475 		case EACCES:
23476 			rval = EACCES;
23477 			goto done;
23478 		default:
23479 			rval = EIO;
23480 			goto done;
23481 		}
23482 	} else {
23483 		media_capacity = capacity;
23484 	}
23485 
23486 	/*
23487 	 * If lun is expanded dynamically, update the un structure.
23488 	 */
23489 	mutex_enter(SD_MUTEX(un));
23490 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23491 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23492 	    (capacity > un->un_blockcount)) {
23493 		sd_update_block_info(un, lbasize, capacity);
23494 	}
23495 	mutex_exit(SD_MUTEX(un));
23496 
23497 	media_info_ext.dki_lbsize = lbasize;
23498 	media_info_ext.dki_capacity = media_capacity;
23499 	media_info_ext.dki_pbsize = pbsize;
23500 
23501 	if (ddi_copyout(&media_info_ext, arg, sizeof (struct dk_minfo_ext),
23502 	    flag)) {
23503 		rval = EFAULT;
23504 		goto no_assessment;
23505 	}
23506 done:
23507 	if (rval != 0) {
23508 		if (rval == EIO)
23509 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23510 		else
23511 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23512 	}
23513 no_assessment:
23514 	sd_ssc_fini(ssc);
23515 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23516 	kmem_free(rqbuf, SENSE_LENGTH);
23517 	return (rval);
23518 }
23519 
23520 /*
23521  *    Function: sd_watch_request_submit
23522  *
23523  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23524  *		depending on which is supported by device.
23525  */
23526 static opaque_t
23527 sd_watch_request_submit(struct sd_lun *un)
23528 {
23529 	dev_t			dev;
23530 
23531 	/* All submissions are unified to use same device number */
23532 	dev = sd_make_device(SD_DEVINFO(un));
23533 
23534 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23535 		return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23536 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23537 		    (caddr_t)dev));
23538 	} else {
23539 		return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23540 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23541 		    (caddr_t)dev));
23542 	}
23543 }
23544 
23545 
23546 /*
23547  *    Function: sd_check_media
23548  *
23549  * Description: This utility routine implements the functionality for the
23550  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23551  *		driver state changes from that specified by the user
23552  *		(inserted or ejected). For example, if the user specifies
23553  *		DKIO_EJECTED and the current media state is inserted this
23554  *		routine will immediately return DKIO_INSERTED. However, if the
23555  *		current media state is not inserted the user thread will be
23556  *		blocked until the drive state changes. If DKIO_NONE is specified
23557  *		the user thread will block until a drive state change occurs.
23558  *
23559  *   Arguments: dev  - the device number
23560  *		state  - user pointer to a dkio_state, updated with the current
23561  *			drive state at return.
23562  *
23563  * Return Code: ENXIO
23564  *		EIO
23565  *		EAGAIN
23566  *		EINTR
23567  */
23568 
23569 static int
23570 sd_check_media(dev_t dev, enum dkio_state state)
23571 {
23572 	struct sd_lun		*un = NULL;
23573 	enum dkio_state		prev_state;
23574 	opaque_t		token = NULL;
23575 	int			rval = 0;
23576 	sd_ssc_t		*ssc;
23577 
23578 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23579 		return (ENXIO);
23580 	}
23581 
23582 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23583 
23584 	ssc = sd_ssc_init(un);
23585 
23586 	mutex_enter(SD_MUTEX(un));
23587 
23588 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23589 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
23590 
23591 	prev_state = un->un_mediastate;
23592 
23593 	/* is there anything to do? */
23594 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23595 		/*
23596 		 * submit the request to the scsi_watch service;
23597 		 * scsi_media_watch_cb() does the real work
23598 		 */
23599 		mutex_exit(SD_MUTEX(un));
23600 
23601 		/*
23602 		 * This change handles the case where a scsi watch request is
23603 		 * added to a device that is powered down. To accomplish this
23604 		 * we power up the device before adding the scsi watch request,
23605 		 * since the scsi watch sends a TUR directly to the device
23606 		 * which the device cannot handle if it is powered down.
23607 		 */
23608 		if (sd_pm_entry(un) != DDI_SUCCESS) {
23609 			mutex_enter(SD_MUTEX(un));
23610 			goto done;
23611 		}
23612 
23613 		token = sd_watch_request_submit(un);
23614 
23615 		sd_pm_exit(un);
23616 
23617 		mutex_enter(SD_MUTEX(un));
23618 		if (token == NULL) {
23619 			rval = EAGAIN;
23620 			goto done;
23621 		}
23622 
23623 		/*
23624 		 * This is a special case IOCTL that doesn't return
23625 		 * until the media state changes. Routine sdpower
23626 		 * knows about and handles this so don't count it
23627 		 * as an active cmd in the driver, which would
23628 		 * keep the device busy to the pm framework.
23629 		 * If the count isn't decremented the device can't
23630 		 * be powered down.
23631 		 */
23632 		un->un_ncmds_in_driver--;
23633 		ASSERT(un->un_ncmds_in_driver >= 0);
23634 
23635 		/*
23636 		 * if a prior request had been made, this will be the same
23637 		 * token, as scsi_watch was designed that way.
23638 		 */
23639 		un->un_swr_token = token;
23640 		un->un_specified_mediastate = state;
23641 
23642 		/*
23643 		 * now wait for media change
23644 		 * we will not be signalled unless mediastate == state but it is
23645 		 * still better to test for this condition, since there is a
23646 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23647 		 */
23648 		SD_TRACE(SD_LOG_COMMON, un,
23649 		    "sd_check_media: waiting for media state change\n");
23650 		while (un->un_mediastate == state) {
23651 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23652 				SD_TRACE(SD_LOG_COMMON, un,
23653 				    "sd_check_media: waiting for media state "
23654 				    "was interrupted\n");
23655 				un->un_ncmds_in_driver++;
23656 				rval = EINTR;
23657 				goto done;
23658 			}
23659 			SD_TRACE(SD_LOG_COMMON, un,
23660 			    "sd_check_media: received signal, state=%x\n",
23661 			    un->un_mediastate);
23662 		}
23663 		/*
23664 		 * Inc the counter to indicate the device once again
23665 		 * has an active outstanding cmd.
23666 		 */
23667 		un->un_ncmds_in_driver++;
23668 	}
23669 
23670 	/* invalidate geometry */
23671 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23672 		sr_ejected(un);
23673 	}
23674 
23675 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23676 		uint64_t	capacity;
23677 		uint_t		lbasize;
23678 
23679 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23680 		mutex_exit(SD_MUTEX(un));
23681 		/*
23682 		 * Since the following routines use SD_PATH_DIRECT, we must
23683 		 * call PM directly before the upcoming disk accesses. This
23684 		 * may cause the disk to be power/spin up.
23685 		 */
23686 
23687 		if (sd_pm_entry(un) == DDI_SUCCESS) {
23688 			rval = sd_send_scsi_READ_CAPACITY(ssc,
23689 			    &capacity, &lbasize, SD_PATH_DIRECT);
23690 			if (rval != 0) {
23691 				sd_pm_exit(un);
23692 				if (rval == EIO)
23693 					sd_ssc_assessment(ssc,
23694 					    SD_FMT_STATUS_CHECK);
23695 				else
23696 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23697 				mutex_enter(SD_MUTEX(un));
23698 				goto done;
23699 			}
23700 		} else {
23701 			rval = EIO;
23702 			mutex_enter(SD_MUTEX(un));
23703 			goto done;
23704 		}
23705 		mutex_enter(SD_MUTEX(un));
23706 
23707 		sd_update_block_info(un, lbasize, capacity);
23708 
23709 		/*
23710 		 *  Check if the media in the device is writable or not
23711 		 */
23712 		if (ISCD(un)) {
23713 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23714 		}
23715 
23716 		mutex_exit(SD_MUTEX(un));
23717 		cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23718 		if ((cmlb_validate(un->un_cmlbhandle, 0,
23719 		    (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23720 			sd_set_pstats(un);
23721 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23722 			    "sd_check_media: un:0x%p pstats created and "
23723 			    "set\n", un);
23724 		}
23725 
23726 		rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23727 		    SD_PATH_DIRECT);
23728 
23729 		sd_pm_exit(un);
23730 
23731 		if (rval != 0) {
23732 			if (rval == EIO)
23733 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23734 			else
23735 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23736 		}
23737 
23738 		mutex_enter(SD_MUTEX(un));
23739 	}
23740 done:
23741 	sd_ssc_fini(ssc);
23742 	un->un_f_watcht_stopped = FALSE;
23743 	if (token != NULL && un->un_swr_token != NULL) {
23744 		/*
23745 		 * Use of this local token and the mutex ensures that we avoid
23746 		 * some race conditions associated with terminating the
23747 		 * scsi watch.
23748 		 */
23749 		token = un->un_swr_token;
23750 		mutex_exit(SD_MUTEX(un));
23751 		(void) scsi_watch_request_terminate(token,
23752 		    SCSI_WATCH_TERMINATE_WAIT);
23753 		if (scsi_watch_get_ref_count(token) == 0) {
23754 			mutex_enter(SD_MUTEX(un));
23755 			un->un_swr_token = (opaque_t)NULL;
23756 		} else {
23757 			mutex_enter(SD_MUTEX(un));
23758 		}
23759 	}
23760 
23761 	/*
23762 	 * Update the capacity kstat value, if no media previously
23763 	 * (capacity kstat is 0) and a media has been inserted
23764 	 * (un_f_blockcount_is_valid == TRUE)
23765 	 */
23766 	if (un->un_errstats) {
23767 		struct sd_errstats	*stp = NULL;
23768 
23769 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
23770 		if ((stp->sd_capacity.value.ui64 == 0) &&
23771 		    (un->un_f_blockcount_is_valid == TRUE)) {
23772 			stp->sd_capacity.value.ui64 =
23773 			    (uint64_t)((uint64_t)un->un_blockcount *
23774 			    un->un_sys_blocksize);
23775 		}
23776 	}
23777 	mutex_exit(SD_MUTEX(un));
23778 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23779 	return (rval);
23780 }
23781 
23782 
23783 /*
23784  *    Function: sd_delayed_cv_broadcast
23785  *
23786  * Description: Delayed cv_broadcast to allow for target to recover from media
23787  *		insertion.
23788  *
23789  *   Arguments: arg - driver soft state (unit) structure
23790  */
23791 
23792 static void
23793 sd_delayed_cv_broadcast(void *arg)
23794 {
23795 	struct sd_lun *un = arg;
23796 
23797 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23798 
23799 	mutex_enter(SD_MUTEX(un));
23800 	un->un_dcvb_timeid = NULL;
23801 	cv_broadcast(&un->un_state_cv);
23802 	mutex_exit(SD_MUTEX(un));
23803 }
23804 
23805 
23806 /*
23807  *    Function: sd_media_watch_cb
23808  *
23809  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23810  *		routine processes the TUR sense data and updates the driver
23811  *		state if a transition has occurred. The user thread
23812  *		(sd_check_media) is then signalled.
23813  *
23814  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23815  *			among multiple watches that share this callback function
23816  *		resultp - scsi watch facility result packet containing scsi
23817  *			  packet, status byte and sense data
23818  *
23819  * Return Code: 0 for success, -1 for failure
23820  */
23821 
23822 static int
23823 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23824 {
23825 	struct sd_lun			*un;
23826 	struct scsi_status		*statusp = resultp->statusp;
23827 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
23828 	enum dkio_state			state = DKIO_NONE;
23829 	dev_t				dev = (dev_t)arg;
23830 	uchar_t				actual_sense_length;
23831 	uint8_t				skey, asc, ascq;
23832 
23833 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23834 		return (-1);
23835 	}
23836 	actual_sense_length = resultp->actual_sense_length;
23837 
23838 	mutex_enter(SD_MUTEX(un));
23839 	SD_TRACE(SD_LOG_COMMON, un,
23840 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23841 	    *((char *)statusp), (void *)sensep, actual_sense_length);
23842 
23843 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23844 		un->un_mediastate = DKIO_DEV_GONE;
23845 		cv_broadcast(&un->un_state_cv);
23846 		mutex_exit(SD_MUTEX(un));
23847 
23848 		return (0);
23849 	}
23850 
23851 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23852 		if (sd_gesn_media_data_valid(resultp->mmc_data)) {
23853 			if ((resultp->mmc_data[5] &
23854 			    SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
23855 				state = DKIO_INSERTED;
23856 			} else {
23857 				state = DKIO_EJECTED;
23858 			}
23859 			if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
23860 			    SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
23861 				sd_log_eject_request_event(un, KM_NOSLEEP);
23862 			}
23863 		}
23864 	} else if (sensep != NULL) {
23865 		/*
23866 		 * If there was a check condition then sensep points to valid
23867 		 * sense data. If status was not a check condition but a
23868 		 * reservation or busy status then the new state is DKIO_NONE.
23869 		 */
23870 		skey = scsi_sense_key(sensep);
23871 		asc = scsi_sense_asc(sensep);
23872 		ascq = scsi_sense_ascq(sensep);
23873 
23874 		SD_INFO(SD_LOG_COMMON, un,
23875 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
23876 		    skey, asc, ascq);
23877 		/* This routine only uses up to 13 bytes of sense data. */
23878 		if (actual_sense_length >= 13) {
23879 			if (skey == KEY_UNIT_ATTENTION) {
23880 				if (asc == 0x28) {
23881 					state = DKIO_INSERTED;
23882 				}
23883 			} else if (skey == KEY_NOT_READY) {
23884 				/*
23885 				 * Sense data of 02/06/00 means that the
23886 				 * drive could not read the media (No
23887 				 * reference position found). In this case
23888 				 * to prevent a hang on the DKIOCSTATE IOCTL
23889 				 * we set the media state to DKIO_INSERTED.
23890 				 */
23891 				if (asc == 0x06 && ascq == 0x00)
23892 					state = DKIO_INSERTED;
23893 
23894 				/*
23895 				 * if 02/04/02  means that the host
23896 				 * should send start command. Explicitly
23897 				 * leave the media state as is
23898 				 * (inserted) as the media is inserted
23899 				 * and host has stopped device for PM
23900 				 * reasons. Upon next true read/write
23901 				 * to this media will bring the
23902 				 * device to the right state good for
23903 				 * media access.
23904 				 */
23905 				if (asc == 0x3a) {
23906 					state = DKIO_EJECTED;
23907 				} else {
23908 					/*
23909 					 * If the drive is busy with an
23910 					 * operation or long write, keep the
23911 					 * media in an inserted state.
23912 					 */
23913 
23914 					if ((asc == 0x04) &&
23915 					    ((ascq == 0x02) ||
23916 					    (ascq == 0x07) ||
23917 					    (ascq == 0x08))) {
23918 						state = DKIO_INSERTED;
23919 					}
23920 				}
23921 			} else if (skey == KEY_NO_SENSE) {
23922 				if ((asc == 0x00) && (ascq == 0x00)) {
23923 					/*
23924 					 * Sense Data 00/00/00 does not provide
23925 					 * any information about the state of
23926 					 * the media. Ignore it.
23927 					 */
23928 					mutex_exit(SD_MUTEX(un));
23929 					return (0);
23930 				}
23931 			}
23932 		}
23933 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
23934 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
23935 		state = DKIO_INSERTED;
23936 	}
23937 
23938 	SD_TRACE(SD_LOG_COMMON, un,
23939 	    "sd_media_watch_cb: state=%x, specified=%x\n",
23940 	    state, un->un_specified_mediastate);
23941 
23942 	/*
23943 	 * now signal the waiting thread if this is *not* the specified state;
23944 	 * delay the signal if the state is DKIO_INSERTED to allow the target
23945 	 * to recover
23946 	 */
23947 	if (state != un->un_specified_mediastate) {
23948 		un->un_mediastate = state;
23949 		if (state == DKIO_INSERTED) {
23950 			/*
23951 			 * delay the signal to give the drive a chance
23952 			 * to do what it apparently needs to do
23953 			 */
23954 			SD_TRACE(SD_LOG_COMMON, un,
23955 			    "sd_media_watch_cb: delayed cv_broadcast\n");
23956 			if (un->un_dcvb_timeid == NULL) {
23957 				un->un_dcvb_timeid =
23958 				    timeout(sd_delayed_cv_broadcast, un,
23959 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
23960 			}
23961 		} else {
23962 			SD_TRACE(SD_LOG_COMMON, un,
23963 			    "sd_media_watch_cb: immediate cv_broadcast\n");
23964 			cv_broadcast(&un->un_state_cv);
23965 		}
23966 	}
23967 	mutex_exit(SD_MUTEX(un));
23968 	return (0);
23969 }
23970 
23971 
23972 /*
23973  *    Function: sd_dkio_get_temp
23974  *
23975  * Description: This routine is the driver entry point for handling ioctl
23976  *		requests to get the disk temperature.
23977  *
23978  *   Arguments: dev  - the device number
23979  *		arg  - pointer to user provided dk_temperature structure.
23980  *		flag - this argument is a pass through to ddi_copyxxx()
23981  *		       directly from the mode argument of ioctl().
23982  *
23983  * Return Code: 0
23984  *		EFAULT
23985  *		ENXIO
23986  *		EAGAIN
23987  */
23988 
23989 static int
23990 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
23991 {
23992 	struct sd_lun		*un = NULL;
23993 	struct dk_temperature	*dktemp = NULL;
23994 	uchar_t			*temperature_page;
23995 	int			rval = 0;
23996 	int			path_flag = SD_PATH_STANDARD;
23997 	sd_ssc_t		*ssc;
23998 
23999 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24000 		return (ENXIO);
24001 	}
24002 
24003 	ssc = sd_ssc_init(un);
24004 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24005 
24006 	/* copyin the disk temp argument to get the user flags */
24007 	if (ddi_copyin((void *)arg, dktemp,
24008 	    sizeof (struct dk_temperature), flag) != 0) {
24009 		rval = EFAULT;
24010 		goto done;
24011 	}
24012 
24013 	/* Initialize the temperature to invalid. */
24014 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24015 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24016 
24017 	/*
24018 	 * Note: Investigate removing the "bypass pm" semantic.
24019 	 * Can we just bypass PM always?
24020 	 */
24021 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24022 		path_flag = SD_PATH_DIRECT;
24023 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24024 		mutex_enter(&un->un_pm_mutex);
24025 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24026 			/*
24027 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24028 			 * in low power mode, we can not wake it up, Need to
24029 			 * return EAGAIN.
24030 			 */
24031 			mutex_exit(&un->un_pm_mutex);
24032 			rval = EAGAIN;
24033 			goto done;
24034 		} else {
24035 			/*
24036 			 * Indicate to PM the device is busy. This is required
24037 			 * to avoid a race - i.e. the ioctl is issuing a
24038 			 * command and the pm framework brings down the device
24039 			 * to low power mode (possible power cut-off on some
24040 			 * platforms).
24041 			 */
24042 			mutex_exit(&un->un_pm_mutex);
24043 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24044 				rval = EAGAIN;
24045 				goto done;
24046 			}
24047 		}
24048 	}
24049 
24050 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24051 
24052 	rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24053 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24054 	if (rval != 0)
24055 		goto done2;
24056 
24057 	/*
24058 	 * For the current temperature verify that the parameter length is 0x02
24059 	 * and the parameter code is 0x00
24060 	 */
24061 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24062 	    (temperature_page[5] == 0x00)) {
24063 		if (temperature_page[9] == 0xFF) {
24064 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24065 		} else {
24066 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24067 		}
24068 	}
24069 
24070 	/*
24071 	 * For the reference temperature verify that the parameter
24072 	 * length is 0x02 and the parameter code is 0x01
24073 	 */
24074 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24075 	    (temperature_page[11] == 0x01)) {
24076 		if (temperature_page[15] == 0xFF) {
24077 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24078 		} else {
24079 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24080 		}
24081 	}
24082 
24083 	/* Do the copyout regardless of the temperature commands status. */
24084 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24085 	    flag) != 0) {
24086 		rval = EFAULT;
24087 		goto done1;
24088 	}
24089 
24090 done2:
24091 	if (rval != 0) {
24092 		if (rval == EIO)
24093 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24094 		else
24095 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24096 	}
24097 done1:
24098 	if (path_flag == SD_PATH_DIRECT) {
24099 		sd_pm_exit(un);
24100 	}
24101 
24102 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24103 done:
24104 	sd_ssc_fini(ssc);
24105 	if (dktemp != NULL) {
24106 		kmem_free(dktemp, sizeof (struct dk_temperature));
24107 	}
24108 
24109 	return (rval);
24110 }
24111 
24112 
24113 /*
24114  *    Function: sd_log_page_supported
24115  *
24116  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24117  *		supported log pages.
24118  *
24119  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24120  *                      structure for this target.
24121  *		log_page -
24122  *
24123  * Return Code: -1 - on error (log sense is optional and may not be supported).
24124  *		0  - log page not found.
24125  *  		1  - log page found.
24126  */
24127 
24128 static int
24129 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24130 {
24131 	uchar_t *log_page_data;
24132 	int	i;
24133 	int	match = 0;
24134 	int	log_size;
24135 	int	status = 0;
24136 	struct sd_lun	*un;
24137 
24138 	ASSERT(ssc != NULL);
24139 	un = ssc->ssc_un;
24140 	ASSERT(un != NULL);
24141 
24142 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24143 
24144 	status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24145 	    SD_PATH_DIRECT);
24146 
24147 	if (status != 0) {
24148 		if (status == EIO) {
24149 			/*
24150 			 * Some disks do not support log sense, we
24151 			 * should ignore this kind of error(sense key is
24152 			 * 0x5 - illegal request).
24153 			 */
24154 			uint8_t *sensep;
24155 			int senlen;
24156 
24157 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24158 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24159 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
24160 
24161 			if (senlen > 0 &&
24162 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24163 				sd_ssc_assessment(ssc,
24164 				    SD_FMT_IGNORE_COMPROMISE);
24165 			} else {
24166 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24167 			}
24168 		} else {
24169 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24170 		}
24171 
24172 		SD_ERROR(SD_LOG_COMMON, un,
24173 		    "sd_log_page_supported: failed log page retrieval\n");
24174 		kmem_free(log_page_data, 0xFF);
24175 		return (-1);
24176 	}
24177 
24178 	log_size = log_page_data[3];
24179 
24180 	/*
24181 	 * The list of supported log pages start from the fourth byte. Check
24182 	 * until we run out of log pages or a match is found.
24183 	 */
24184 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24185 		if (log_page_data[i] == log_page) {
24186 			match++;
24187 		}
24188 	}
24189 	kmem_free(log_page_data, 0xFF);
24190 	return (match);
24191 }
24192 
24193 
24194 /*
24195  *    Function: sd_mhdioc_failfast
24196  *
24197  * Description: This routine is the driver entry point for handling ioctl
24198  *		requests to enable/disable the multihost failfast option.
24199  *		(MHIOCENFAILFAST)
24200  *
24201  *   Arguments: dev	- the device number
24202  *		arg	- user specified probing interval.
24203  *		flag	- this argument is a pass through to ddi_copyxxx()
24204  *			  directly from the mode argument of ioctl().
24205  *
24206  * Return Code: 0
24207  *		EFAULT
24208  *		ENXIO
24209  */
24210 
24211 static int
24212 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24213 {
24214 	struct sd_lun	*un = NULL;
24215 	int		mh_time;
24216 	int		rval = 0;
24217 
24218 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24219 		return (ENXIO);
24220 	}
24221 
24222 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24223 		return (EFAULT);
24224 
24225 	if (mh_time) {
24226 		mutex_enter(SD_MUTEX(un));
24227 		un->un_resvd_status |= SD_FAILFAST;
24228 		mutex_exit(SD_MUTEX(un));
24229 		/*
24230 		 * If mh_time is INT_MAX, then this ioctl is being used for
24231 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24232 		 */
24233 		if (mh_time != INT_MAX) {
24234 			rval = sd_check_mhd(dev, mh_time);
24235 		}
24236 	} else {
24237 		(void) sd_check_mhd(dev, 0);
24238 		mutex_enter(SD_MUTEX(un));
24239 		un->un_resvd_status &= ~SD_FAILFAST;
24240 		mutex_exit(SD_MUTEX(un));
24241 	}
24242 	return (rval);
24243 }
24244 
24245 
24246 /*
24247  *    Function: sd_mhdioc_takeown
24248  *
24249  * Description: This routine is the driver entry point for handling ioctl
24250  *		requests to forcefully acquire exclusive access rights to the
24251  *		multihost disk (MHIOCTKOWN).
24252  *
24253  *   Arguments: dev	- the device number
24254  *		arg	- user provided structure specifying the delay
24255  *			  parameters in milliseconds
24256  *		flag	- this argument is a pass through to ddi_copyxxx()
24257  *			  directly from the mode argument of ioctl().
24258  *
24259  * Return Code: 0
24260  *		EFAULT
24261  *		ENXIO
24262  */
24263 
24264 static int
24265 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24266 {
24267 	struct sd_lun		*un = NULL;
24268 	struct mhioctkown	*tkown = NULL;
24269 	int			rval = 0;
24270 
24271 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24272 		return (ENXIO);
24273 	}
24274 
24275 	if (arg != NULL) {
24276 		tkown = (struct mhioctkown *)
24277 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24278 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24279 		if (rval != 0) {
24280 			rval = EFAULT;
24281 			goto error;
24282 		}
24283 	}
24284 
24285 	rval = sd_take_ownership(dev, tkown);
24286 	mutex_enter(SD_MUTEX(un));
24287 	if (rval == 0) {
24288 		un->un_resvd_status |= SD_RESERVE;
24289 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24290 			sd_reinstate_resv_delay =
24291 			    tkown->reinstate_resv_delay * 1000;
24292 		} else {
24293 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24294 		}
24295 		/*
24296 		 * Give the scsi_watch routine interval set by
24297 		 * the MHIOCENFAILFAST ioctl precedence here.
24298 		 */
24299 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24300 			mutex_exit(SD_MUTEX(un));
24301 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24302 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24303 			    "sd_mhdioc_takeown : %d\n",
24304 			    sd_reinstate_resv_delay);
24305 		} else {
24306 			mutex_exit(SD_MUTEX(un));
24307 		}
24308 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24309 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24310 	} else {
24311 		un->un_resvd_status &= ~SD_RESERVE;
24312 		mutex_exit(SD_MUTEX(un));
24313 	}
24314 
24315 error:
24316 	if (tkown != NULL) {
24317 		kmem_free(tkown, sizeof (struct mhioctkown));
24318 	}
24319 	return (rval);
24320 }
24321 
24322 
24323 /*
24324  *    Function: sd_mhdioc_release
24325  *
24326  * Description: This routine is the driver entry point for handling ioctl
24327  *		requests to release exclusive access rights to the multihost
24328  *		disk (MHIOCRELEASE).
24329  *
24330  *   Arguments: dev	- the device number
24331  *
24332  * Return Code: 0
24333  *		ENXIO
24334  */
24335 
24336 static int
24337 sd_mhdioc_release(dev_t dev)
24338 {
24339 	struct sd_lun		*un = NULL;
24340 	timeout_id_t		resvd_timeid_save;
24341 	int			resvd_status_save;
24342 	int			rval = 0;
24343 
24344 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24345 		return (ENXIO);
24346 	}
24347 
24348 	mutex_enter(SD_MUTEX(un));
24349 	resvd_status_save = un->un_resvd_status;
24350 	un->un_resvd_status &=
24351 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24352 	if (un->un_resvd_timeid) {
24353 		resvd_timeid_save = un->un_resvd_timeid;
24354 		un->un_resvd_timeid = NULL;
24355 		mutex_exit(SD_MUTEX(un));
24356 		(void) untimeout(resvd_timeid_save);
24357 	} else {
24358 		mutex_exit(SD_MUTEX(un));
24359 	}
24360 
24361 	/*
24362 	 * destroy any pending timeout thread that may be attempting to
24363 	 * reinstate reservation on this device.
24364 	 */
24365 	sd_rmv_resv_reclaim_req(dev);
24366 
24367 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24368 		mutex_enter(SD_MUTEX(un));
24369 		if ((un->un_mhd_token) &&
24370 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24371 			mutex_exit(SD_MUTEX(un));
24372 			(void) sd_check_mhd(dev, 0);
24373 		} else {
24374 			mutex_exit(SD_MUTEX(un));
24375 		}
24376 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24377 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24378 	} else {
24379 		/*
24380 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24381 		 */
24382 		mutex_enter(SD_MUTEX(un));
24383 		un->un_resvd_status = resvd_status_save;
24384 		mutex_exit(SD_MUTEX(un));
24385 	}
24386 	return (rval);
24387 }
24388 
24389 
24390 /*
24391  *    Function: sd_mhdioc_register_devid
24392  *
24393  * Description: This routine is the driver entry point for handling ioctl
24394  *		requests to register the device id (MHIOCREREGISTERDEVID).
24395  *
24396  *		Note: The implementation for this ioctl has been updated to
24397  *		be consistent with the original PSARC case (1999/357)
24398  *		(4375899, 4241671, 4220005)
24399  *
24400  *   Arguments: dev	- the device number
24401  *
24402  * Return Code: 0
24403  *		ENXIO
24404  */
24405 
24406 static int
24407 sd_mhdioc_register_devid(dev_t dev)
24408 {
24409 	struct sd_lun	*un = NULL;
24410 	int		rval = 0;
24411 	sd_ssc_t	*ssc;
24412 
24413 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24414 		return (ENXIO);
24415 	}
24416 
24417 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24418 
24419 	mutex_enter(SD_MUTEX(un));
24420 
24421 	/* If a devid already exists, de-register it */
24422 	if (un->un_devid != NULL) {
24423 		ddi_devid_unregister(SD_DEVINFO(un));
24424 		/*
24425 		 * After unregister devid, needs to free devid memory
24426 		 */
24427 		ddi_devid_free(un->un_devid);
24428 		un->un_devid = NULL;
24429 	}
24430 
24431 	/* Check for reservation conflict */
24432 	mutex_exit(SD_MUTEX(un));
24433 	ssc = sd_ssc_init(un);
24434 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24435 	mutex_enter(SD_MUTEX(un));
24436 
24437 	switch (rval) {
24438 	case 0:
24439 		sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24440 		break;
24441 	case EACCES:
24442 		break;
24443 	default:
24444 		rval = EIO;
24445 	}
24446 
24447 	mutex_exit(SD_MUTEX(un));
24448 	if (rval != 0) {
24449 		if (rval == EIO)
24450 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24451 		else
24452 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24453 	}
24454 	sd_ssc_fini(ssc);
24455 	return (rval);
24456 }
24457 
24458 
24459 /*
24460  *    Function: sd_mhdioc_inkeys
24461  *
24462  * Description: This routine is the driver entry point for handling ioctl
24463  *		requests to issue the SCSI-3 Persistent In Read Keys command
24464  *		to the device (MHIOCGRP_INKEYS).
24465  *
24466  *   Arguments: dev	- the device number
24467  *		arg	- user provided in_keys structure
24468  *		flag	- this argument is a pass through to ddi_copyxxx()
24469  *			  directly from the mode argument of ioctl().
24470  *
24471  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24472  *		ENXIO
24473  *		EFAULT
24474  */
24475 
24476 static int
24477 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24478 {
24479 	struct sd_lun		*un;
24480 	mhioc_inkeys_t		inkeys;
24481 	int			rval = 0;
24482 
24483 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24484 		return (ENXIO);
24485 	}
24486 
24487 #ifdef _MULTI_DATAMODEL
24488 	switch (ddi_model_convert_from(flag & FMODELS)) {
24489 	case DDI_MODEL_ILP32: {
24490 		struct mhioc_inkeys32	inkeys32;
24491 
24492 		if (ddi_copyin(arg, &inkeys32,
24493 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24494 			return (EFAULT);
24495 		}
24496 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24497 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24498 		    &inkeys, flag)) != 0) {
24499 			return (rval);
24500 		}
24501 		inkeys32.generation = inkeys.generation;
24502 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24503 		    flag) != 0) {
24504 			return (EFAULT);
24505 		}
24506 		break;
24507 	}
24508 	case DDI_MODEL_NONE:
24509 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24510 		    flag) != 0) {
24511 			return (EFAULT);
24512 		}
24513 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24514 		    &inkeys, flag)) != 0) {
24515 			return (rval);
24516 		}
24517 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24518 		    flag) != 0) {
24519 			return (EFAULT);
24520 		}
24521 		break;
24522 	}
24523 
24524 #else /* ! _MULTI_DATAMODEL */
24525 
24526 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24527 		return (EFAULT);
24528 	}
24529 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24530 	if (rval != 0) {
24531 		return (rval);
24532 	}
24533 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24534 		return (EFAULT);
24535 	}
24536 
24537 #endif /* _MULTI_DATAMODEL */
24538 
24539 	return (rval);
24540 }
24541 
24542 
24543 /*
24544  *    Function: sd_mhdioc_inresv
24545  *
24546  * Description: This routine is the driver entry point for handling ioctl
24547  *		requests to issue the SCSI-3 Persistent In Read Reservations
24548  *		command to the device (MHIOCGRP_INKEYS).
24549  *
24550  *   Arguments: dev	- the device number
24551  *		arg	- user provided in_resv structure
24552  *		flag	- this argument is a pass through to ddi_copyxxx()
24553  *			  directly from the mode argument of ioctl().
24554  *
24555  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24556  *		ENXIO
24557  *		EFAULT
24558  */
24559 
24560 static int
24561 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24562 {
24563 	struct sd_lun		*un;
24564 	mhioc_inresvs_t		inresvs;
24565 	int			rval = 0;
24566 
24567 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24568 		return (ENXIO);
24569 	}
24570 
24571 #ifdef _MULTI_DATAMODEL
24572 
24573 	switch (ddi_model_convert_from(flag & FMODELS)) {
24574 	case DDI_MODEL_ILP32: {
24575 		struct mhioc_inresvs32	inresvs32;
24576 
24577 		if (ddi_copyin(arg, &inresvs32,
24578 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24579 			return (EFAULT);
24580 		}
24581 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24582 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24583 		    &inresvs, flag)) != 0) {
24584 			return (rval);
24585 		}
24586 		inresvs32.generation = inresvs.generation;
24587 		if (ddi_copyout(&inresvs32, arg,
24588 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24589 			return (EFAULT);
24590 		}
24591 		break;
24592 	}
24593 	case DDI_MODEL_NONE:
24594 		if (ddi_copyin(arg, &inresvs,
24595 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24596 			return (EFAULT);
24597 		}
24598 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24599 		    &inresvs, flag)) != 0) {
24600 			return (rval);
24601 		}
24602 		if (ddi_copyout(&inresvs, arg,
24603 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24604 			return (EFAULT);
24605 		}
24606 		break;
24607 	}
24608 
24609 #else /* ! _MULTI_DATAMODEL */
24610 
24611 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24612 		return (EFAULT);
24613 	}
24614 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24615 	if (rval != 0) {
24616 		return (rval);
24617 	}
24618 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24619 		return (EFAULT);
24620 	}
24621 
24622 #endif /* ! _MULTI_DATAMODEL */
24623 
24624 	return (rval);
24625 }
24626 
24627 
24628 /*
24629  * The following routines support the clustering functionality described below
24630  * and implement lost reservation reclaim functionality.
24631  *
24632  * Clustering
24633  * ----------
24634  * The clustering code uses two different, independent forms of SCSI
24635  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24636  * Persistent Group Reservations. For any particular disk, it will use either
24637  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24638  *
24639  * SCSI-2
24640  * The cluster software takes ownership of a multi-hosted disk by issuing the
24641  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24642  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24643  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24644  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24645  * driver. The meaning of failfast is that if the driver (on this host) ever
24646  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24647  * it should immediately panic the host. The motivation for this ioctl is that
24648  * if this host does encounter reservation conflict, the underlying cause is
24649  * that some other host of the cluster has decided that this host is no longer
24650  * in the cluster and has seized control of the disks for itself. Since this
24651  * host is no longer in the cluster, it ought to panic itself. The
24652  * MHIOCENFAILFAST ioctl does two things:
24653  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24654  *      error to panic the host
24655  *      (b) it sets up a periodic timer to test whether this host still has
24656  *      "access" (in that no other host has reserved the device):  if the
24657  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24658  *      purpose of that periodic timer is to handle scenarios where the host is
24659  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24660  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24661  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24662  * the device itself.
24663  *
24664  * SCSI-3 PGR
24665  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24666  * facility is supported through the shared multihost disk ioctls
24667  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24668  * MHIOCGRP_PREEMPTANDABORT)
24669  *
24670  * Reservation Reclaim:
24671  * --------------------
24672  * To support the lost reservation reclaim operations this driver creates a
24673  * single thread to handle reinstating reservations on all devices that have
24674  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24675  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24676  * and the reservation reclaim thread loops through the requests to regain the
24677  * lost reservations.
24678  */
24679 
24680 /*
24681  *    Function: sd_check_mhd()
24682  *
24683  * Description: This function sets up and submits a scsi watch request or
24684  *		terminates an existing watch request. This routine is used in
24685  *		support of reservation reclaim.
24686  *
24687  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24688  *			 among multiple watches that share the callback function
24689  *		interval - the number of microseconds specifying the watch
24690  *			   interval for issuing TEST UNIT READY commands. If
24691  *			   set to 0 the watch should be terminated. If the
24692  *			   interval is set to 0 and if the device is required
24693  *			   to hold reservation while disabling failfast, the
24694  *			   watch is restarted with an interval of
24695  *			   reinstate_resv_delay.
24696  *
24697  * Return Code: 0	   - Successful submit/terminate of scsi watch request
24698  *		ENXIO      - Indicates an invalid device was specified
24699  *		EAGAIN     - Unable to submit the scsi watch request
24700  */
24701 
24702 static int
24703 sd_check_mhd(dev_t dev, int interval)
24704 {
24705 	struct sd_lun	*un;
24706 	opaque_t	token;
24707 
24708 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24709 		return (ENXIO);
24710 	}
24711 
24712 	/* is this a watch termination request? */
24713 	if (interval == 0) {
24714 		mutex_enter(SD_MUTEX(un));
24715 		/* if there is an existing watch task then terminate it */
24716 		if (un->un_mhd_token) {
24717 			token = un->un_mhd_token;
24718 			un->un_mhd_token = NULL;
24719 			mutex_exit(SD_MUTEX(un));
24720 			(void) scsi_watch_request_terminate(token,
24721 			    SCSI_WATCH_TERMINATE_ALL_WAIT);
24722 			mutex_enter(SD_MUTEX(un));
24723 		} else {
24724 			mutex_exit(SD_MUTEX(un));
24725 			/*
24726 			 * Note: If we return here we don't check for the
24727 			 * failfast case. This is the original legacy
24728 			 * implementation but perhaps we should be checking
24729 			 * the failfast case.
24730 			 */
24731 			return (0);
24732 		}
24733 		/*
24734 		 * If the device is required to hold reservation while
24735 		 * disabling failfast, we need to restart the scsi_watch
24736 		 * routine with an interval of reinstate_resv_delay.
24737 		 */
24738 		if (un->un_resvd_status & SD_RESERVE) {
24739 			interval = sd_reinstate_resv_delay/1000;
24740 		} else {
24741 			/* no failfast so bail */
24742 			mutex_exit(SD_MUTEX(un));
24743 			return (0);
24744 		}
24745 		mutex_exit(SD_MUTEX(un));
24746 	}
24747 
24748 	/*
24749 	 * adjust minimum time interval to 1 second,
24750 	 * and convert from msecs to usecs
24751 	 */
24752 	if (interval > 0 && interval < 1000) {
24753 		interval = 1000;
24754 	}
24755 	interval *= 1000;
24756 
24757 	/*
24758 	 * submit the request to the scsi_watch service
24759 	 */
24760 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24761 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24762 	if (token == NULL) {
24763 		return (EAGAIN);
24764 	}
24765 
24766 	/*
24767 	 * save token for termination later on
24768 	 */
24769 	mutex_enter(SD_MUTEX(un));
24770 	un->un_mhd_token = token;
24771 	mutex_exit(SD_MUTEX(un));
24772 	return (0);
24773 }
24774 
24775 
24776 /*
24777  *    Function: sd_mhd_watch_cb()
24778  *
24779  * Description: This function is the call back function used by the scsi watch
24780  *		facility. The scsi watch facility sends the "Test Unit Ready"
24781  *		and processes the status. If applicable (i.e. a "Unit Attention"
24782  *		status and automatic "Request Sense" not used) the scsi watch
24783  *		facility will send a "Request Sense" and retrieve the sense data
24784  *		to be passed to this callback function. In either case the
24785  *		automatic "Request Sense" or the facility submitting one, this
24786  *		callback is passed the status and sense data.
24787  *
24788  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24789  *			among multiple watches that share this callback function
24790  *		resultp - scsi watch facility result packet containing scsi
24791  *			  packet, status byte and sense data
24792  *
24793  * Return Code: 0 - continue the watch task
24794  *		non-zero - terminate the watch task
24795  */
24796 
24797 static int
24798 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24799 {
24800 	struct sd_lun			*un;
24801 	struct scsi_status		*statusp;
24802 	uint8_t				*sensep;
24803 	struct scsi_pkt			*pkt;
24804 	uchar_t				actual_sense_length;
24805 	dev_t  				dev = (dev_t)arg;
24806 
24807 	ASSERT(resultp != NULL);
24808 	statusp			= resultp->statusp;
24809 	sensep			= (uint8_t *)resultp->sensep;
24810 	pkt			= resultp->pkt;
24811 	actual_sense_length	= resultp->actual_sense_length;
24812 
24813 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24814 		return (ENXIO);
24815 	}
24816 
24817 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
24818 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24819 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24820 
24821 	/* Begin processing of the status and/or sense data */
24822 	if (pkt->pkt_reason != CMD_CMPLT) {
24823 		/* Handle the incomplete packet */
24824 		sd_mhd_watch_incomplete(un, pkt);
24825 		return (0);
24826 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24827 		if (*((unsigned char *)statusp)
24828 		    == STATUS_RESERVATION_CONFLICT) {
24829 			/*
24830 			 * Handle a reservation conflict by panicking if
24831 			 * configured for failfast or by logging the conflict
24832 			 * and updating the reservation status
24833 			 */
24834 			mutex_enter(SD_MUTEX(un));
24835 			if ((un->un_resvd_status & SD_FAILFAST) &&
24836 			    (sd_failfast_enable)) {
24837 				sd_panic_for_res_conflict(un);
24838 				/*NOTREACHED*/
24839 			}
24840 			SD_INFO(SD_LOG_IOCTL_MHD, un,
24841 			    "sd_mhd_watch_cb: Reservation Conflict\n");
24842 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24843 			mutex_exit(SD_MUTEX(un));
24844 		}
24845 	}
24846 
24847 	if (sensep != NULL) {
24848 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24849 			mutex_enter(SD_MUTEX(un));
24850 			if ((scsi_sense_asc(sensep) ==
24851 			    SD_SCSI_RESET_SENSE_CODE) &&
24852 			    (un->un_resvd_status & SD_RESERVE)) {
24853 				/*
24854 				 * The additional sense code indicates a power
24855 				 * on or bus device reset has occurred; update
24856 				 * the reservation status.
24857 				 */
24858 				un->un_resvd_status |=
24859 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
24860 				SD_INFO(SD_LOG_IOCTL_MHD, un,
24861 				    "sd_mhd_watch_cb: Lost Reservation\n");
24862 			}
24863 		} else {
24864 			return (0);
24865 		}
24866 	} else {
24867 		mutex_enter(SD_MUTEX(un));
24868 	}
24869 
24870 	if ((un->un_resvd_status & SD_RESERVE) &&
24871 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
24872 		if (un->un_resvd_status & SD_WANT_RESERVE) {
24873 			/*
24874 			 * A reset occurred in between the last probe and this
24875 			 * one so if a timeout is pending cancel it.
24876 			 */
24877 			if (un->un_resvd_timeid) {
24878 				timeout_id_t temp_id = un->un_resvd_timeid;
24879 				un->un_resvd_timeid = NULL;
24880 				mutex_exit(SD_MUTEX(un));
24881 				(void) untimeout(temp_id);
24882 				mutex_enter(SD_MUTEX(un));
24883 			}
24884 			un->un_resvd_status &= ~SD_WANT_RESERVE;
24885 		}
24886 		if (un->un_resvd_timeid == 0) {
24887 			/* Schedule a timeout to handle the lost reservation */
24888 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
24889 			    (void *)dev,
24890 			    drv_usectohz(sd_reinstate_resv_delay));
24891 		}
24892 	}
24893 	mutex_exit(SD_MUTEX(un));
24894 	return (0);
24895 }
24896 
24897 
24898 /*
24899  *    Function: sd_mhd_watch_incomplete()
24900  *
24901  * Description: This function is used to find out why a scsi pkt sent by the
24902  *		scsi watch facility was not completed. Under some scenarios this
24903  *		routine will return. Otherwise it will send a bus reset to see
24904  *		if the drive is still online.
24905  *
24906  *   Arguments: un  - driver soft state (unit) structure
24907  *		pkt - incomplete scsi pkt
24908  */
24909 
24910 static void
24911 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
24912 {
24913 	int	be_chatty;
24914 	int	perr;
24915 
24916 	ASSERT(pkt != NULL);
24917 	ASSERT(un != NULL);
24918 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
24919 	perr		= (pkt->pkt_statistics & STAT_PERR);
24920 
24921 	mutex_enter(SD_MUTEX(un));
24922 	if (un->un_state == SD_STATE_DUMPING) {
24923 		mutex_exit(SD_MUTEX(un));
24924 		return;
24925 	}
24926 
24927 	switch (pkt->pkt_reason) {
24928 	case CMD_UNX_BUS_FREE:
24929 		/*
24930 		 * If we had a parity error that caused the target to drop BSY*,
24931 		 * don't be chatty about it.
24932 		 */
24933 		if (perr && be_chatty) {
24934 			be_chatty = 0;
24935 		}
24936 		break;
24937 	case CMD_TAG_REJECT:
24938 		/*
24939 		 * The SCSI-2 spec states that a tag reject will be sent by the
24940 		 * target if tagged queuing is not supported. A tag reject may
24941 		 * also be sent during certain initialization periods or to
24942 		 * control internal resources. For the latter case the target
24943 		 * may also return Queue Full.
24944 		 *
24945 		 * If this driver receives a tag reject from a target that is
24946 		 * going through an init period or controlling internal
24947 		 * resources tagged queuing will be disabled. This is a less
24948 		 * than optimal behavior but the driver is unable to determine
24949 		 * the target state and assumes tagged queueing is not supported
24950 		 */
24951 		pkt->pkt_flags = 0;
24952 		un->un_tagflags = 0;
24953 
24954 		if (un->un_f_opt_queueing == TRUE) {
24955 			un->un_throttle = min(un->un_throttle, 3);
24956 		} else {
24957 			un->un_throttle = 1;
24958 		}
24959 		mutex_exit(SD_MUTEX(un));
24960 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
24961 		mutex_enter(SD_MUTEX(un));
24962 		break;
24963 	case CMD_INCOMPLETE:
24964 		/*
24965 		 * The transport stopped with an abnormal state, fallthrough and
24966 		 * reset the target and/or bus unless selection did not complete
24967 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
24968 		 * go through a target/bus reset
24969 		 */
24970 		if (pkt->pkt_state == STATE_GOT_BUS) {
24971 			break;
24972 		}
24973 		/*FALLTHROUGH*/
24974 
24975 	case CMD_TIMEOUT:
24976 	default:
24977 		/*
24978 		 * The lun may still be running the command, so a lun reset
24979 		 * should be attempted. If the lun reset fails or cannot be
24980 		 * issued, than try a target reset. Lastly try a bus reset.
24981 		 */
24982 		if ((pkt->pkt_statistics &
24983 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
24984 			int reset_retval = 0;
24985 			mutex_exit(SD_MUTEX(un));
24986 			if (un->un_f_allow_bus_device_reset == TRUE) {
24987 				if (un->un_f_lun_reset_enabled == TRUE) {
24988 					reset_retval =
24989 					    scsi_reset(SD_ADDRESS(un),
24990 					    RESET_LUN);
24991 				}
24992 				if (reset_retval == 0) {
24993 					reset_retval =
24994 					    scsi_reset(SD_ADDRESS(un),
24995 					    RESET_TARGET);
24996 				}
24997 			}
24998 			if (reset_retval == 0) {
24999 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25000 			}
25001 			mutex_enter(SD_MUTEX(un));
25002 		}
25003 		break;
25004 	}
25005 
25006 	/* A device/bus reset has occurred; update the reservation status. */
25007 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25008 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25009 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25010 			un->un_resvd_status |=
25011 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25012 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25013 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25014 		}
25015 	}
25016 
25017 	/*
25018 	 * The disk has been turned off; Update the device state.
25019 	 *
25020 	 * Note: Should we be offlining the disk here?
25021 	 */
25022 	if (pkt->pkt_state == STATE_GOT_BUS) {
25023 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25024 		    "Disk not responding to selection\n");
25025 		if (un->un_state != SD_STATE_OFFLINE) {
25026 			New_state(un, SD_STATE_OFFLINE);
25027 		}
25028 	} else if (be_chatty) {
25029 		/*
25030 		 * suppress messages if they are all the same pkt reason;
25031 		 * with TQ, many (up to 256) are returned with the same
25032 		 * pkt_reason
25033 		 */
25034 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25035 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25036 			    "sd_mhd_watch_incomplete: "
25037 			    "SCSI transport failed: reason '%s'\n",
25038 			    scsi_rname(pkt->pkt_reason));
25039 		}
25040 	}
25041 	un->un_last_pkt_reason = pkt->pkt_reason;
25042 	mutex_exit(SD_MUTEX(un));
25043 }
25044 
25045 
25046 /*
25047  *    Function: sd_sname()
25048  *
25049  * Description: This is a simple little routine to return a string containing
25050  *		a printable description of command status byte for use in
25051  *		logging.
25052  *
25053  *   Arguments: status - pointer to a status byte
25054  *
25055  * Return Code: char * - string containing status description.
25056  */
25057 
25058 static char *
25059 sd_sname(uchar_t status)
25060 {
25061 	switch (status & STATUS_MASK) {
25062 	case STATUS_GOOD:
25063 		return ("good status");
25064 	case STATUS_CHECK:
25065 		return ("check condition");
25066 	case STATUS_MET:
25067 		return ("condition met");
25068 	case STATUS_BUSY:
25069 		return ("busy");
25070 	case STATUS_INTERMEDIATE:
25071 		return ("intermediate");
25072 	case STATUS_INTERMEDIATE_MET:
25073 		return ("intermediate - condition met");
25074 	case STATUS_RESERVATION_CONFLICT:
25075 		return ("reservation_conflict");
25076 	case STATUS_TERMINATED:
25077 		return ("command terminated");
25078 	case STATUS_QFULL:
25079 		return ("queue full");
25080 	default:
25081 		return ("<unknown status>");
25082 	}
25083 }
25084 
25085 
25086 /*
25087  *    Function: sd_mhd_resvd_recover()
25088  *
25089  * Description: This function adds a reservation entry to the
25090  *		sd_resv_reclaim_request list and signals the reservation
25091  *		reclaim thread that there is work pending. If the reservation
25092  *		reclaim thread has not been previously created this function
25093  *		will kick it off.
25094  *
25095  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25096  *			among multiple watches that share this callback function
25097  *
25098  *     Context: This routine is called by timeout() and is run in interrupt
25099  *		context. It must not sleep or call other functions which may
25100  *		sleep.
25101  */
25102 
25103 static void
25104 sd_mhd_resvd_recover(void *arg)
25105 {
25106 	dev_t			dev = (dev_t)arg;
25107 	struct sd_lun		*un;
25108 	struct sd_thr_request	*sd_treq = NULL;
25109 	struct sd_thr_request	*sd_cur = NULL;
25110 	struct sd_thr_request	*sd_prev = NULL;
25111 	int			already_there = 0;
25112 
25113 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25114 		return;
25115 	}
25116 
25117 	mutex_enter(SD_MUTEX(un));
25118 	un->un_resvd_timeid = NULL;
25119 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25120 		/*
25121 		 * There was a reset so don't issue the reserve, allow the
25122 		 * sd_mhd_watch_cb callback function to notice this and
25123 		 * reschedule the timeout for reservation.
25124 		 */
25125 		mutex_exit(SD_MUTEX(un));
25126 		return;
25127 	}
25128 	mutex_exit(SD_MUTEX(un));
25129 
25130 	/*
25131 	 * Add this device to the sd_resv_reclaim_request list and the
25132 	 * sd_resv_reclaim_thread should take care of the rest.
25133 	 *
25134 	 * Note: We can't sleep in this context so if the memory allocation
25135 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25136 	 * reschedule the timeout for reservation.  (4378460)
25137 	 */
25138 	sd_treq = (struct sd_thr_request *)
25139 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25140 	if (sd_treq == NULL) {
25141 		return;
25142 	}
25143 
25144 	sd_treq->sd_thr_req_next = NULL;
25145 	sd_treq->dev = dev;
25146 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25147 	if (sd_tr.srq_thr_req_head == NULL) {
25148 		sd_tr.srq_thr_req_head = sd_treq;
25149 	} else {
25150 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25151 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25152 			if (sd_cur->dev == dev) {
25153 				/*
25154 				 * already in Queue so don't log
25155 				 * another request for the device
25156 				 */
25157 				already_there = 1;
25158 				break;
25159 			}
25160 			sd_prev = sd_cur;
25161 		}
25162 		if (!already_there) {
25163 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25164 			    "logging request for %lx\n", dev);
25165 			sd_prev->sd_thr_req_next = sd_treq;
25166 		} else {
25167 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25168 		}
25169 	}
25170 
25171 	/*
25172 	 * Create a kernel thread to do the reservation reclaim and free up this
25173 	 * thread. We cannot block this thread while we go away to do the
25174 	 * reservation reclaim
25175 	 */
25176 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25177 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25178 		    sd_resv_reclaim_thread, NULL,
25179 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25180 
25181 	/* Tell the reservation reclaim thread that it has work to do */
25182 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25183 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25184 }
25185 
25186 /*
25187  *    Function: sd_resv_reclaim_thread()
25188  *
25189  * Description: This function implements the reservation reclaim operations
25190  *
25191  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25192  *		      among multiple watches that share this callback function
25193  */
25194 
25195 static void
25196 sd_resv_reclaim_thread()
25197 {
25198 	struct sd_lun		*un;
25199 	struct sd_thr_request	*sd_mhreq;
25200 
25201 	/* Wait for work */
25202 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25203 	if (sd_tr.srq_thr_req_head == NULL) {
25204 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25205 		    &sd_tr.srq_resv_reclaim_mutex);
25206 	}
25207 
25208 	/* Loop while we have work */
25209 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25210 		un = ddi_get_soft_state(sd_state,
25211 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25212 		if (un == NULL) {
25213 			/*
25214 			 * softstate structure is NULL so just
25215 			 * dequeue the request and continue
25216 			 */
25217 			sd_tr.srq_thr_req_head =
25218 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25219 			kmem_free(sd_tr.srq_thr_cur_req,
25220 			    sizeof (struct sd_thr_request));
25221 			continue;
25222 		}
25223 
25224 		/* dequeue the request */
25225 		sd_mhreq = sd_tr.srq_thr_cur_req;
25226 		sd_tr.srq_thr_req_head =
25227 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25228 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25229 
25230 		/*
25231 		 * Reclaim reservation only if SD_RESERVE is still set. There
25232 		 * may have been a call to MHIOCRELEASE before we got here.
25233 		 */
25234 		mutex_enter(SD_MUTEX(un));
25235 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25236 			/*
25237 			 * Note: The SD_LOST_RESERVE flag is cleared before
25238 			 * reclaiming the reservation. If this is done after the
25239 			 * call to sd_reserve_release a reservation loss in the
25240 			 * window between pkt completion of reserve cmd and
25241 			 * mutex_enter below may not be recognized
25242 			 */
25243 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25244 			mutex_exit(SD_MUTEX(un));
25245 
25246 			if (sd_reserve_release(sd_mhreq->dev,
25247 			    SD_RESERVE) == 0) {
25248 				mutex_enter(SD_MUTEX(un));
25249 				un->un_resvd_status |= SD_RESERVE;
25250 				mutex_exit(SD_MUTEX(un));
25251 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25252 				    "sd_resv_reclaim_thread: "
25253 				    "Reservation Recovered\n");
25254 			} else {
25255 				mutex_enter(SD_MUTEX(un));
25256 				un->un_resvd_status |= SD_LOST_RESERVE;
25257 				mutex_exit(SD_MUTEX(un));
25258 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25259 				    "sd_resv_reclaim_thread: Failed "
25260 				    "Reservation Recovery\n");
25261 			}
25262 		} else {
25263 			mutex_exit(SD_MUTEX(un));
25264 		}
25265 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25266 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25267 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25268 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25269 		/*
25270 		 * wakeup the destroy thread if anyone is waiting on
25271 		 * us to complete.
25272 		 */
25273 		cv_signal(&sd_tr.srq_inprocess_cv);
25274 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25275 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25276 	}
25277 
25278 	/*
25279 	 * cleanup the sd_tr structure now that this thread will not exist
25280 	 */
25281 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25282 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25283 	sd_tr.srq_resv_reclaim_thread = NULL;
25284 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25285 	thread_exit();
25286 }
25287 
25288 
25289 /*
25290  *    Function: sd_rmv_resv_reclaim_req()
25291  *
25292  * Description: This function removes any pending reservation reclaim requests
25293  *		for the specified device.
25294  *
25295  *   Arguments: dev - the device 'dev_t'
25296  */
25297 
25298 static void
25299 sd_rmv_resv_reclaim_req(dev_t dev)
25300 {
25301 	struct sd_thr_request *sd_mhreq;
25302 	struct sd_thr_request *sd_prev;
25303 
25304 	/* Remove a reservation reclaim request from the list */
25305 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25306 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25307 		/*
25308 		 * We are attempting to reinstate reservation for
25309 		 * this device. We wait for sd_reserve_release()
25310 		 * to return before we return.
25311 		 */
25312 		cv_wait(&sd_tr.srq_inprocess_cv,
25313 		    &sd_tr.srq_resv_reclaim_mutex);
25314 	} else {
25315 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25316 		if (sd_mhreq && sd_mhreq->dev == dev) {
25317 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25318 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25319 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25320 			return;
25321 		}
25322 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25323 			if (sd_mhreq && sd_mhreq->dev == dev) {
25324 				break;
25325 			}
25326 			sd_prev = sd_mhreq;
25327 		}
25328 		if (sd_mhreq != NULL) {
25329 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25330 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25331 		}
25332 	}
25333 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25334 }
25335 
25336 
25337 /*
25338  *    Function: sd_mhd_reset_notify_cb()
25339  *
25340  * Description: This is a call back function for scsi_reset_notify. This
25341  *		function updates the softstate reserved status and logs the
25342  *		reset. The driver scsi watch facility callback function
25343  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25344  *		will reclaim the reservation.
25345  *
25346  *   Arguments: arg  - driver soft state (unit) structure
25347  */
25348 
25349 static void
25350 sd_mhd_reset_notify_cb(caddr_t arg)
25351 {
25352 	struct sd_lun *un = (struct sd_lun *)arg;
25353 
25354 	mutex_enter(SD_MUTEX(un));
25355 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25356 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25357 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25358 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25359 	}
25360 	mutex_exit(SD_MUTEX(un));
25361 }
25362 
25363 
25364 /*
25365  *    Function: sd_take_ownership()
25366  *
25367  * Description: This routine implements an algorithm to achieve a stable
25368  *		reservation on disks which don't implement priority reserve,
25369  *		and makes sure that other host lose re-reservation attempts.
25370  *		This algorithm contains of a loop that keeps issuing the RESERVE
25371  *		for some period of time (min_ownership_delay, default 6 seconds)
25372  *		During that loop, it looks to see if there has been a bus device
25373  *		reset or bus reset (both of which cause an existing reservation
25374  *		to be lost). If the reservation is lost issue RESERVE until a
25375  *		period of min_ownership_delay with no resets has gone by, or
25376  *		until max_ownership_delay has expired. This loop ensures that
25377  *		the host really did manage to reserve the device, in spite of
25378  *		resets. The looping for min_ownership_delay (default six
25379  *		seconds) is important to early generation clustering products,
25380  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25381  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25382  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25383  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25384  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25385  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25386  *		no longer "owns" the disk and will have panicked itself.  Thus,
25387  *		the host issuing the MHIOCTKOWN is assured (with timing
25388  *		dependencies) that by the time it actually starts to use the
25389  *		disk for real work, the old owner is no longer accessing it.
25390  *
25391  *		min_ownership_delay is the minimum amount of time for which the
25392  *		disk must be reserved continuously devoid of resets before the
25393  *		MHIOCTKOWN ioctl will return success.
25394  *
25395  *		max_ownership_delay indicates the amount of time by which the
25396  *		take ownership should succeed or timeout with an error.
25397  *
25398  *   Arguments: dev - the device 'dev_t'
25399  *		*p  - struct containing timing info.
25400  *
25401  * Return Code: 0 for success or error code
25402  */
25403 
25404 static int
25405 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25406 {
25407 	struct sd_lun	*un;
25408 	int		rval;
25409 	int		err;
25410 	int		reservation_count   = 0;
25411 	int		min_ownership_delay =  6000000; /* in usec */
25412 	int		max_ownership_delay = 30000000; /* in usec */
25413 	clock_t		start_time;	/* starting time of this algorithm */
25414 	clock_t		end_time;	/* time limit for giving up */
25415 	clock_t		ownership_time;	/* time limit for stable ownership */
25416 	clock_t		current_time;
25417 	clock_t		previous_current_time;
25418 
25419 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25420 		return (ENXIO);
25421 	}
25422 
25423 	/*
25424 	 * Attempt a device reservation. A priority reservation is requested.
25425 	 */
25426 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25427 	    != SD_SUCCESS) {
25428 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25429 		    "sd_take_ownership: return(1)=%d\n", rval);
25430 		return (rval);
25431 	}
25432 
25433 	/* Update the softstate reserved status to indicate the reservation */
25434 	mutex_enter(SD_MUTEX(un));
25435 	un->un_resvd_status |= SD_RESERVE;
25436 	un->un_resvd_status &=
25437 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25438 	mutex_exit(SD_MUTEX(un));
25439 
25440 	if (p != NULL) {
25441 		if (p->min_ownership_delay != 0) {
25442 			min_ownership_delay = p->min_ownership_delay * 1000;
25443 		}
25444 		if (p->max_ownership_delay != 0) {
25445 			max_ownership_delay = p->max_ownership_delay * 1000;
25446 		}
25447 	}
25448 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25449 	    "sd_take_ownership: min, max delays: %d, %d\n",
25450 	    min_ownership_delay, max_ownership_delay);
25451 
25452 	start_time = ddi_get_lbolt();
25453 	current_time	= start_time;
25454 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25455 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25456 
25457 	while (current_time - end_time < 0) {
25458 		delay(drv_usectohz(500000));
25459 
25460 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25461 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25462 				mutex_enter(SD_MUTEX(un));
25463 				rval = (un->un_resvd_status &
25464 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25465 				mutex_exit(SD_MUTEX(un));
25466 				break;
25467 			}
25468 		}
25469 		previous_current_time = current_time;
25470 		current_time = ddi_get_lbolt();
25471 		mutex_enter(SD_MUTEX(un));
25472 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25473 			ownership_time = ddi_get_lbolt() +
25474 			    drv_usectohz(min_ownership_delay);
25475 			reservation_count = 0;
25476 		} else {
25477 			reservation_count++;
25478 		}
25479 		un->un_resvd_status |= SD_RESERVE;
25480 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25481 		mutex_exit(SD_MUTEX(un));
25482 
25483 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25484 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25485 		    "reservation=%s\n", (current_time - previous_current_time),
25486 		    reservation_count ? "ok" : "reclaimed");
25487 
25488 		if (current_time - ownership_time >= 0 &&
25489 		    reservation_count >= 4) {
25490 			rval = 0; /* Achieved a stable ownership */
25491 			break;
25492 		}
25493 		if (current_time - end_time >= 0) {
25494 			rval = EACCES; /* No ownership in max possible time */
25495 			break;
25496 		}
25497 	}
25498 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25499 	    "sd_take_ownership: return(2)=%d\n", rval);
25500 	return (rval);
25501 }
25502 
25503 
25504 /*
25505  *    Function: sd_reserve_release()
25506  *
25507  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25508  *		PRIORITY RESERVE commands based on a user specified command type
25509  *
25510  *   Arguments: dev - the device 'dev_t'
25511  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25512  *		      SD_RESERVE, SD_RELEASE
25513  *
25514  * Return Code: 0 or Error Code
25515  */
25516 
25517 static int
25518 sd_reserve_release(dev_t dev, int cmd)
25519 {
25520 	struct uscsi_cmd	*com = NULL;
25521 	struct sd_lun		*un = NULL;
25522 	char			cdb[CDB_GROUP0];
25523 	int			rval;
25524 
25525 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25526 	    (cmd == SD_PRIORITY_RESERVE));
25527 
25528 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25529 		return (ENXIO);
25530 	}
25531 
25532 	/* instantiate and initialize the command and cdb */
25533 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25534 	bzero(cdb, CDB_GROUP0);
25535 	com->uscsi_flags   = USCSI_SILENT;
25536 	com->uscsi_timeout = un->un_reserve_release_time;
25537 	com->uscsi_cdblen  = CDB_GROUP0;
25538 	com->uscsi_cdb	   = cdb;
25539 	if (cmd == SD_RELEASE) {
25540 		cdb[0] = SCMD_RELEASE;
25541 	} else {
25542 		cdb[0] = SCMD_RESERVE;
25543 	}
25544 
25545 	/* Send the command. */
25546 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25547 	    SD_PATH_STANDARD);
25548 
25549 	/*
25550 	 * "break" a reservation that is held by another host, by issuing a
25551 	 * reset if priority reserve is desired, and we could not get the
25552 	 * device.
25553 	 */
25554 	if ((cmd == SD_PRIORITY_RESERVE) &&
25555 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25556 		/*
25557 		 * First try to reset the LUN. If we cannot, then try a target
25558 		 * reset, followed by a bus reset if the target reset fails.
25559 		 */
25560 		int reset_retval = 0;
25561 		if (un->un_f_lun_reset_enabled == TRUE) {
25562 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25563 		}
25564 		if (reset_retval == 0) {
25565 			/* The LUN reset either failed or was not issued */
25566 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25567 		}
25568 		if ((reset_retval == 0) &&
25569 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25570 			rval = EIO;
25571 			kmem_free(com, sizeof (*com));
25572 			return (rval);
25573 		}
25574 
25575 		bzero(com, sizeof (struct uscsi_cmd));
25576 		com->uscsi_flags   = USCSI_SILENT;
25577 		com->uscsi_cdb	   = cdb;
25578 		com->uscsi_cdblen  = CDB_GROUP0;
25579 		com->uscsi_timeout = 5;
25580 
25581 		/*
25582 		 * Reissue the last reserve command, this time without request
25583 		 * sense.  Assume that it is just a regular reserve command.
25584 		 */
25585 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25586 		    SD_PATH_STANDARD);
25587 	}
25588 
25589 	/* Return an error if still getting a reservation conflict. */
25590 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25591 		rval = EACCES;
25592 	}
25593 
25594 	kmem_free(com, sizeof (*com));
25595 	return (rval);
25596 }
25597 
25598 
25599 #define	SD_NDUMP_RETRIES	12
25600 /*
25601  *	System Crash Dump routine
25602  */
25603 
25604 static int
25605 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25606 {
25607 	int		instance;
25608 	int		partition;
25609 	int		i;
25610 	int		err;
25611 	struct sd_lun	*un;
25612 	struct scsi_pkt *wr_pktp;
25613 	struct buf	*wr_bp;
25614 	struct buf	wr_buf;
25615 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25616 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25617 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25618 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25619 	size_t		io_start_offset;
25620 	int		doing_rmw = FALSE;
25621 	int		rval;
25622 	ssize_t		dma_resid;
25623 	daddr_t		oblkno;
25624 	diskaddr_t	nblks = 0;
25625 	diskaddr_t	start_block;
25626 
25627 	instance = SDUNIT(dev);
25628 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25629 	    !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25630 		return (ENXIO);
25631 	}
25632 
25633 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25634 
25635 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25636 
25637 	partition = SDPART(dev);
25638 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25639 
25640 	if (!(NOT_DEVBSIZE(un))) {
25641 		int secmask = 0;
25642 		int blknomask = 0;
25643 
25644 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25645 		secmask = un->un_tgt_blocksize - 1;
25646 
25647 		if (blkno & blknomask) {
25648 			SD_TRACE(SD_LOG_DUMP, un,
25649 			    "sddump: dump start block not modulo %d\n",
25650 			    un->un_tgt_blocksize);
25651 			return (EINVAL);
25652 		}
25653 
25654 		if ((nblk * DEV_BSIZE) & secmask) {
25655 			SD_TRACE(SD_LOG_DUMP, un,
25656 			    "sddump: dump length not modulo %d\n",
25657 			    un->un_tgt_blocksize);
25658 			return (EINVAL);
25659 		}
25660 
25661 	}
25662 
25663 	/* Validate blocks to dump at against partition size. */
25664 
25665 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
25666 	    &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25667 
25668 	if (NOT_DEVBSIZE(un)) {
25669 		if ((blkno + nblk) > nblks) {
25670 			SD_TRACE(SD_LOG_DUMP, un,
25671 			    "sddump: dump range larger than partition: "
25672 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25673 			    blkno, nblk, nblks);
25674 			return (EINVAL);
25675 		}
25676 	} else {
25677 		if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25678 		    (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25679 			SD_TRACE(SD_LOG_DUMP, un,
25680 			    "sddump: dump range larger than partition: "
25681 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25682 			    blkno, nblk, nblks);
25683 			return (EINVAL);
25684 		}
25685 	}
25686 
25687 	mutex_enter(&un->un_pm_mutex);
25688 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25689 		struct scsi_pkt *start_pktp;
25690 
25691 		mutex_exit(&un->un_pm_mutex);
25692 
25693 		/*
25694 		 * use pm framework to power on HBA 1st
25695 		 */
25696 		(void) pm_raise_power(SD_DEVINFO(un), 0,
25697 		    SD_PM_STATE_ACTIVE(un));
25698 
25699 		/*
25700 		 * Dump no long uses sdpower to power on a device, it's
25701 		 * in-line here so it can be done in polled mode.
25702 		 */
25703 
25704 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25705 
25706 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25707 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25708 
25709 		if (start_pktp == NULL) {
25710 			/* We were not given a SCSI packet, fail. */
25711 			return (EIO);
25712 		}
25713 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25714 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25715 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25716 		start_pktp->pkt_flags = FLAG_NOINTR;
25717 
25718 		mutex_enter(SD_MUTEX(un));
25719 		SD_FILL_SCSI1_LUN(un, start_pktp);
25720 		mutex_exit(SD_MUTEX(un));
25721 		/*
25722 		 * Scsi_poll returns 0 (success) if the command completes and
25723 		 * the status block is STATUS_GOOD.
25724 		 */
25725 		if (sd_scsi_poll(un, start_pktp) != 0) {
25726 			scsi_destroy_pkt(start_pktp);
25727 			return (EIO);
25728 		}
25729 		scsi_destroy_pkt(start_pktp);
25730 		(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25731 		    SD_PM_STATE_CHANGE);
25732 	} else {
25733 		mutex_exit(&un->un_pm_mutex);
25734 	}
25735 
25736 	mutex_enter(SD_MUTEX(un));
25737 	un->un_throttle = 0;
25738 
25739 	/*
25740 	 * The first time through, reset the specific target device.
25741 	 * However, when cpr calls sddump we know that sd is in a
25742 	 * a good state so no bus reset is required.
25743 	 * Clear sense data via Request Sense cmd.
25744 	 * In sddump we don't care about allow_bus_device_reset anymore
25745 	 */
25746 
25747 	if ((un->un_state != SD_STATE_SUSPENDED) &&
25748 	    (un->un_state != SD_STATE_DUMPING)) {
25749 
25750 		New_state(un, SD_STATE_DUMPING);
25751 
25752 		if (un->un_f_is_fibre == FALSE) {
25753 			mutex_exit(SD_MUTEX(un));
25754 			/*
25755 			 * Attempt a bus reset for parallel scsi.
25756 			 *
25757 			 * Note: A bus reset is required because on some host
25758 			 * systems (i.e. E420R) a bus device reset is
25759 			 * insufficient to reset the state of the target.
25760 			 *
25761 			 * Note: Don't issue the reset for fibre-channel,
25762 			 * because this tends to hang the bus (loop) for
25763 			 * too long while everyone is logging out and in
25764 			 * and the deadman timer for dumping will fire
25765 			 * before the dump is complete.
25766 			 */
25767 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25768 				mutex_enter(SD_MUTEX(un));
25769 				Restore_state(un);
25770 				mutex_exit(SD_MUTEX(un));
25771 				return (EIO);
25772 			}
25773 
25774 			/* Delay to give the device some recovery time. */
25775 			drv_usecwait(10000);
25776 
25777 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
25778 				SD_INFO(SD_LOG_DUMP, un,
25779 				    "sddump: sd_send_polled_RQS failed\n");
25780 			}
25781 			mutex_enter(SD_MUTEX(un));
25782 		}
25783 	}
25784 
25785 	/*
25786 	 * Convert the partition-relative block number to a
25787 	 * disk physical block number.
25788 	 */
25789 	if (NOT_DEVBSIZE(un)) {
25790 		blkno += start_block;
25791 	} else {
25792 		blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25793 		blkno += start_block;
25794 	}
25795 
25796 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25797 
25798 
25799 	/*
25800 	 * Check if the device has a non-512 block size.
25801 	 */
25802 	wr_bp = NULL;
25803 	if (NOT_DEVBSIZE(un)) {
25804 		tgt_byte_offset = blkno * un->un_sys_blocksize;
25805 		tgt_byte_count = nblk * un->un_sys_blocksize;
25806 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25807 		    (tgt_byte_count % un->un_tgt_blocksize)) {
25808 			doing_rmw = TRUE;
25809 			/*
25810 			 * Calculate the block number and number of block
25811 			 * in terms of the media block size.
25812 			 */
25813 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25814 			tgt_nblk =
25815 			    ((tgt_byte_offset + tgt_byte_count +
25816 			    (un->un_tgt_blocksize - 1)) /
25817 			    un->un_tgt_blocksize) - tgt_blkno;
25818 
25819 			/*
25820 			 * Invoke the routine which is going to do read part
25821 			 * of read-modify-write.
25822 			 * Note that this routine returns a pointer to
25823 			 * a valid bp in wr_bp.
25824 			 */
25825 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25826 			    &wr_bp);
25827 			if (err) {
25828 				mutex_exit(SD_MUTEX(un));
25829 				return (err);
25830 			}
25831 			/*
25832 			 * Offset is being calculated as -
25833 			 * (original block # * system block size) -
25834 			 * (new block # * target block size)
25835 			 */
25836 			io_start_offset =
25837 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
25838 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25839 
25840 			ASSERT((io_start_offset >= 0) &&
25841 			    (io_start_offset < un->un_tgt_blocksize));
25842 			/*
25843 			 * Do the modify portion of read modify write.
25844 			 */
25845 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25846 			    (size_t)nblk * un->un_sys_blocksize);
25847 		} else {
25848 			doing_rmw = FALSE;
25849 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25850 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25851 		}
25852 
25853 		/* Convert blkno and nblk to target blocks */
25854 		blkno = tgt_blkno;
25855 		nblk = tgt_nblk;
25856 	} else {
25857 		wr_bp = &wr_buf;
25858 		bzero(wr_bp, sizeof (struct buf));
25859 		wr_bp->b_flags		= B_BUSY;
25860 		wr_bp->b_un.b_addr	= addr;
25861 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
25862 		wr_bp->b_resid		= 0;
25863 	}
25864 
25865 	mutex_exit(SD_MUTEX(un));
25866 
25867 	/*
25868 	 * Obtain a SCSI packet for the write command.
25869 	 * It should be safe to call the allocator here without
25870 	 * worrying about being locked for DVMA mapping because
25871 	 * the address we're passed is already a DVMA mapping
25872 	 *
25873 	 * We are also not going to worry about semaphore ownership
25874 	 * in the dump buffer. Dumping is single threaded at present.
25875 	 */
25876 
25877 	wr_pktp = NULL;
25878 
25879 	dma_resid = wr_bp->b_bcount;
25880 	oblkno = blkno;
25881 
25882 	if (!(NOT_DEVBSIZE(un))) {
25883 		nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
25884 	}
25885 
25886 	while (dma_resid != 0) {
25887 
25888 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25889 		wr_bp->b_flags &= ~B_ERROR;
25890 
25891 		if (un->un_partial_dma_supported == 1) {
25892 			blkno = oblkno +
25893 			    ((wr_bp->b_bcount - dma_resid) /
25894 			    un->un_tgt_blocksize);
25895 			nblk = dma_resid / un->un_tgt_blocksize;
25896 
25897 			if (wr_pktp) {
25898 				/*
25899 				 * Partial DMA transfers after initial transfer
25900 				 */
25901 				rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
25902 				    blkno, nblk);
25903 			} else {
25904 				/* Initial transfer */
25905 				rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25906 				    un->un_pkt_flags, NULL_FUNC, NULL,
25907 				    blkno, nblk);
25908 			}
25909 		} else {
25910 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25911 			    0, NULL_FUNC, NULL, blkno, nblk);
25912 		}
25913 
25914 		if (rval == 0) {
25915 			/* We were given a SCSI packet, continue. */
25916 			break;
25917 		}
25918 
25919 		if (i == 0) {
25920 			if (wr_bp->b_flags & B_ERROR) {
25921 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25922 				    "no resources for dumping; "
25923 				    "error code: 0x%x, retrying",
25924 				    geterror(wr_bp));
25925 			} else {
25926 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25927 				    "no resources for dumping; retrying");
25928 			}
25929 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
25930 			if (wr_bp->b_flags & B_ERROR) {
25931 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25932 				    "no resources for dumping; error code: "
25933 				    "0x%x, retrying\n", geterror(wr_bp));
25934 			}
25935 		} else {
25936 			if (wr_bp->b_flags & B_ERROR) {
25937 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25938 				    "no resources for dumping; "
25939 				    "error code: 0x%x, retries failed, "
25940 				    "giving up.\n", geterror(wr_bp));
25941 			} else {
25942 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25943 				    "no resources for dumping; "
25944 				    "retries failed, giving up.\n");
25945 			}
25946 			mutex_enter(SD_MUTEX(un));
25947 			Restore_state(un);
25948 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
25949 				mutex_exit(SD_MUTEX(un));
25950 				scsi_free_consistent_buf(wr_bp);
25951 			} else {
25952 				mutex_exit(SD_MUTEX(un));
25953 			}
25954 			return (EIO);
25955 		}
25956 		drv_usecwait(10000);
25957 	}
25958 
25959 	if (un->un_partial_dma_supported == 1) {
25960 		/*
25961 		 * save the resid from PARTIAL_DMA
25962 		 */
25963 		dma_resid = wr_pktp->pkt_resid;
25964 		if (dma_resid != 0)
25965 			nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
25966 		wr_pktp->pkt_resid = 0;
25967 	} else {
25968 		dma_resid = 0;
25969 	}
25970 
25971 	/* SunBug 1222170 */
25972 	wr_pktp->pkt_flags = FLAG_NOINTR;
25973 
25974 	err = EIO;
25975 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25976 
25977 		/*
25978 		 * Scsi_poll returns 0 (success) if the command completes and
25979 		 * the status block is STATUS_GOOD.  We should only check
25980 		 * errors if this condition is not true.  Even then we should
25981 		 * send our own request sense packet only if we have a check
25982 		 * condition and auto request sense has not been performed by
25983 		 * the hba.
25984 		 */
25985 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
25986 
25987 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
25988 		    (wr_pktp->pkt_resid == 0)) {
25989 			err = SD_SUCCESS;
25990 			break;
25991 		}
25992 
25993 		/*
25994 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
25995 		 */
25996 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
25997 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25998 			    "Error while dumping state...Device is gone\n");
25999 			break;
26000 		}
26001 
26002 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26003 			SD_INFO(SD_LOG_DUMP, un,
26004 			    "sddump: write failed with CHECK, try # %d\n", i);
26005 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26006 				(void) sd_send_polled_RQS(un);
26007 			}
26008 
26009 			continue;
26010 		}
26011 
26012 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26013 			int reset_retval = 0;
26014 
26015 			SD_INFO(SD_LOG_DUMP, un,
26016 			    "sddump: write failed with BUSY, try # %d\n", i);
26017 
26018 			if (un->un_f_lun_reset_enabled == TRUE) {
26019 				reset_retval = scsi_reset(SD_ADDRESS(un),
26020 				    RESET_LUN);
26021 			}
26022 			if (reset_retval == 0) {
26023 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26024 			}
26025 			(void) sd_send_polled_RQS(un);
26026 
26027 		} else {
26028 			SD_INFO(SD_LOG_DUMP, un,
26029 			    "sddump: write failed with 0x%x, try # %d\n",
26030 			    SD_GET_PKT_STATUS(wr_pktp), i);
26031 			mutex_enter(SD_MUTEX(un));
26032 			sd_reset_target(un, wr_pktp);
26033 			mutex_exit(SD_MUTEX(un));
26034 		}
26035 
26036 		/*
26037 		 * If we are not getting anywhere with lun/target resets,
26038 		 * let's reset the bus.
26039 		 */
26040 		if (i == SD_NDUMP_RETRIES/2) {
26041 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26042 			(void) sd_send_polled_RQS(un);
26043 		}
26044 	}
26045 	}
26046 
26047 	scsi_destroy_pkt(wr_pktp);
26048 	mutex_enter(SD_MUTEX(un));
26049 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26050 		mutex_exit(SD_MUTEX(un));
26051 		scsi_free_consistent_buf(wr_bp);
26052 	} else {
26053 		mutex_exit(SD_MUTEX(un));
26054 	}
26055 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26056 	return (err);
26057 }
26058 
26059 /*
26060  *    Function: sd_scsi_poll()
26061  *
26062  * Description: This is a wrapper for the scsi_poll call.
26063  *
26064  *   Arguments: sd_lun - The unit structure
26065  *              scsi_pkt - The scsi packet being sent to the device.
26066  *
26067  * Return Code: 0 - Command completed successfully with good status
26068  *             -1 - Command failed.  This could indicate a check condition
26069  *                  or other status value requiring recovery action.
26070  *
26071  * NOTE: This code is only called off sddump().
26072  */
26073 
26074 static int
26075 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26076 {
26077 	int status;
26078 
26079 	ASSERT(un != NULL);
26080 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26081 	ASSERT(pktp != NULL);
26082 
26083 	status = SD_SUCCESS;
26084 
26085 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26086 		pktp->pkt_flags |= un->un_tagflags;
26087 		pktp->pkt_flags &= ~FLAG_NODISCON;
26088 	}
26089 
26090 	status = sd_ddi_scsi_poll(pktp);
26091 	/*
26092 	 * Scsi_poll returns 0 (success) if the command completes and the
26093 	 * status block is STATUS_GOOD.  We should only check errors if this
26094 	 * condition is not true.  Even then we should send our own request
26095 	 * sense packet only if we have a check condition and auto
26096 	 * request sense has not been performed by the hba.
26097 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26098 	 */
26099 	if ((status != SD_SUCCESS) &&
26100 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26101 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26102 	    (pktp->pkt_reason != CMD_DEV_GONE))
26103 		(void) sd_send_polled_RQS(un);
26104 
26105 	return (status);
26106 }
26107 
26108 /*
26109  *    Function: sd_send_polled_RQS()
26110  *
26111  * Description: This sends the request sense command to a device.
26112  *
26113  *   Arguments: sd_lun - The unit structure
26114  *
26115  * Return Code: 0 - Command completed successfully with good status
26116  *             -1 - Command failed.
26117  *
26118  */
26119 
26120 static int
26121 sd_send_polled_RQS(struct sd_lun *un)
26122 {
26123 	int	ret_val;
26124 	struct	scsi_pkt	*rqs_pktp;
26125 	struct	buf		*rqs_bp;
26126 
26127 	ASSERT(un != NULL);
26128 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26129 
26130 	ret_val = SD_SUCCESS;
26131 
26132 	rqs_pktp = un->un_rqs_pktp;
26133 	rqs_bp	 = un->un_rqs_bp;
26134 
26135 	mutex_enter(SD_MUTEX(un));
26136 
26137 	if (un->un_sense_isbusy) {
26138 		ret_val = SD_FAILURE;
26139 		mutex_exit(SD_MUTEX(un));
26140 		return (ret_val);
26141 	}
26142 
26143 	/*
26144 	 * If the request sense buffer (and packet) is not in use,
26145 	 * let's set the un_sense_isbusy and send our packet
26146 	 */
26147 	un->un_sense_isbusy 	= 1;
26148 	rqs_pktp->pkt_resid  	= 0;
26149 	rqs_pktp->pkt_reason 	= 0;
26150 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26151 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26152 
26153 	mutex_exit(SD_MUTEX(un));
26154 
26155 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26156 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26157 
26158 	/*
26159 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26160 	 * axle - it has a call into us!
26161 	 */
26162 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26163 		SD_INFO(SD_LOG_COMMON, un,
26164 		    "sd_send_polled_RQS: RQS failed\n");
26165 	}
26166 
26167 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26168 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26169 
26170 	mutex_enter(SD_MUTEX(un));
26171 	un->un_sense_isbusy = 0;
26172 	mutex_exit(SD_MUTEX(un));
26173 
26174 	return (ret_val);
26175 }
26176 
26177 /*
26178  * Defines needed for localized version of the scsi_poll routine.
26179  */
26180 #define	CSEC		10000			/* usecs */
26181 #define	SEC_TO_CSEC	(1000000/CSEC)
26182 
26183 /*
26184  *    Function: sd_ddi_scsi_poll()
26185  *
26186  * Description: Localized version of the scsi_poll routine.  The purpose is to
26187  *		send a scsi_pkt to a device as a polled command.  This version
26188  *		is to ensure more robust handling of transport errors.
26189  *		Specifically this routine cures not ready, coming ready
26190  *		transition for power up and reset of sonoma's.  This can take
26191  *		up to 45 seconds for power-on and 20 seconds for reset of a
26192  * 		sonoma lun.
26193  *
26194  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26195  *
26196  * Return Code: 0 - Command completed successfully with good status
26197  *             -1 - Command failed.
26198  *
26199  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26200  * be fixed (removing this code), we need to determine how to handle the
26201  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26202  *
26203  * NOTE: This code is only called off sddump().
26204  */
26205 static int
26206 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26207 {
26208 	int			rval = -1;
26209 	int			savef;
26210 	long			savet;
26211 	void			(*savec)();
26212 	int			timeout;
26213 	int			busy_count;
26214 	int			poll_delay;
26215 	int			rc;
26216 	uint8_t			*sensep;
26217 	struct scsi_arq_status	*arqstat;
26218 	extern int		do_polled_io;
26219 
26220 	ASSERT(pkt->pkt_scbp);
26221 
26222 	/*
26223 	 * save old flags..
26224 	 */
26225 	savef = pkt->pkt_flags;
26226 	savec = pkt->pkt_comp;
26227 	savet = pkt->pkt_time;
26228 
26229 	pkt->pkt_flags |= FLAG_NOINTR;
26230 
26231 	/*
26232 	 * XXX there is nothing in the SCSA spec that states that we should not
26233 	 * do a callback for polled cmds; however, removing this will break sd
26234 	 * and probably other target drivers
26235 	 */
26236 	pkt->pkt_comp = NULL;
26237 
26238 	/*
26239 	 * we don't like a polled command without timeout.
26240 	 * 60 seconds seems long enough.
26241 	 */
26242 	if (pkt->pkt_time == 0)
26243 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26244 
26245 	/*
26246 	 * Send polled cmd.
26247 	 *
26248 	 * We do some error recovery for various errors.  Tran_busy,
26249 	 * queue full, and non-dispatched commands are retried every 10 msec.
26250 	 * as they are typically transient failures.  Busy status and Not
26251 	 * Ready are retried every second as this status takes a while to
26252 	 * change.
26253 	 */
26254 	timeout = pkt->pkt_time * SEC_TO_CSEC;
26255 
26256 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26257 		/*
26258 		 * Initialize pkt status variables.
26259 		 */
26260 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26261 
26262 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26263 			if (rc != TRAN_BUSY) {
26264 				/* Transport failed - give up. */
26265 				break;
26266 			} else {
26267 				/* Transport busy - try again. */
26268 				poll_delay = 1 * CSEC;		/* 10 msec. */
26269 			}
26270 		} else {
26271 			/*
26272 			 * Transport accepted - check pkt status.
26273 			 */
26274 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26275 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26276 			    (rc == STATUS_CHECK) &&
26277 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
26278 				arqstat =
26279 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26280 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26281 			} else {
26282 				sensep = NULL;
26283 			}
26284 
26285 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26286 			    (rc == STATUS_GOOD)) {
26287 				/* No error - we're done */
26288 				rval = 0;
26289 				break;
26290 
26291 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26292 				/* Lost connection - give up */
26293 				break;
26294 
26295 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26296 			    (pkt->pkt_state == 0)) {
26297 				/* Pkt not dispatched - try again. */
26298 				poll_delay = 1 * CSEC;		/* 10 msec. */
26299 
26300 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26301 			    (rc == STATUS_QFULL)) {
26302 				/* Queue full - try again. */
26303 				poll_delay = 1 * CSEC;		/* 10 msec. */
26304 
26305 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26306 			    (rc == STATUS_BUSY)) {
26307 				/* Busy - try again. */
26308 				poll_delay = 100 * CSEC;	/* 1 sec. */
26309 				busy_count += (SEC_TO_CSEC - 1);
26310 
26311 			} else if ((sensep != NULL) &&
26312 			    (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26313 				/*
26314 				 * Unit Attention - try again.
26315 				 * Pretend it took 1 sec.
26316 				 * NOTE: 'continue' avoids poll_delay
26317 				 */
26318 				busy_count += (SEC_TO_CSEC - 1);
26319 				continue;
26320 
26321 			} else if ((sensep != NULL) &&
26322 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26323 			    (scsi_sense_asc(sensep) == 0x04) &&
26324 			    (scsi_sense_ascq(sensep) == 0x01)) {
26325 				/*
26326 				 * Not ready -> ready - try again.
26327 				 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26328 				 * ...same as STATUS_BUSY
26329 				 */
26330 				poll_delay = 100 * CSEC;	/* 1 sec. */
26331 				busy_count += (SEC_TO_CSEC - 1);
26332 
26333 			} else {
26334 				/* BAD status - give up. */
26335 				break;
26336 			}
26337 		}
26338 
26339 		if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26340 		    !do_polled_io) {
26341 			delay(drv_usectohz(poll_delay));
26342 		} else {
26343 			/* we busy wait during cpr_dump or interrupt threads */
26344 			drv_usecwait(poll_delay);
26345 		}
26346 	}
26347 
26348 	pkt->pkt_flags = savef;
26349 	pkt->pkt_comp = savec;
26350 	pkt->pkt_time = savet;
26351 
26352 	/* return on error */
26353 	if (rval)
26354 		return (rval);
26355 
26356 	/*
26357 	 * This is not a performance critical code path.
26358 	 *
26359 	 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26360 	 * issues associated with looking at DMA memory prior to
26361 	 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26362 	 */
26363 	scsi_sync_pkt(pkt);
26364 	return (0);
26365 }
26366 
26367 
26368 
26369 /*
26370  *    Function: sd_persistent_reservation_in_read_keys
26371  *
26372  * Description: This routine is the driver entry point for handling CD-ROM
26373  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26374  *		by sending the SCSI-3 PRIN commands to the device.
26375  *		Processes the read keys command response by copying the
26376  *		reservation key information into the user provided buffer.
26377  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26378  *
26379  *   Arguments: un   -  Pointer to soft state struct for the target.
26380  *		usrp -	user provided pointer to multihost Persistent In Read
26381  *			Keys structure (mhioc_inkeys_t)
26382  *		flag -	this argument is a pass through to ddi_copyxxx()
26383  *			directly from the mode argument of ioctl().
26384  *
26385  * Return Code: 0   - Success
26386  *		EACCES
26387  *		ENOTSUP
26388  *		errno return code from sd_send_scsi_cmd()
26389  *
26390  *     Context: Can sleep. Does not return until command is completed.
26391  */
26392 
26393 static int
26394 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26395     mhioc_inkeys_t *usrp, int flag)
26396 {
26397 #ifdef _MULTI_DATAMODEL
26398 	struct mhioc_key_list32	li32;
26399 #endif
26400 	sd_prin_readkeys_t	*in;
26401 	mhioc_inkeys_t		*ptr;
26402 	mhioc_key_list_t	li;
26403 	uchar_t			*data_bufp;
26404 	int 			data_len;
26405 	int			rval = 0;
26406 	size_t			copysz;
26407 	sd_ssc_t		*ssc;
26408 
26409 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26410 		return (EINVAL);
26411 	}
26412 	bzero(&li, sizeof (mhioc_key_list_t));
26413 
26414 	ssc = sd_ssc_init(un);
26415 
26416 	/*
26417 	 * Get the listsize from user
26418 	 */
26419 #ifdef _MULTI_DATAMODEL
26420 
26421 	switch (ddi_model_convert_from(flag & FMODELS)) {
26422 	case DDI_MODEL_ILP32:
26423 		copysz = sizeof (struct mhioc_key_list32);
26424 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26425 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26426 			    "sd_persistent_reservation_in_read_keys: "
26427 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26428 			rval = EFAULT;
26429 			goto done;
26430 		}
26431 		li.listsize = li32.listsize;
26432 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26433 		break;
26434 
26435 	case DDI_MODEL_NONE:
26436 		copysz = sizeof (mhioc_key_list_t);
26437 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26438 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26439 			    "sd_persistent_reservation_in_read_keys: "
26440 			    "failed ddi_copyin: mhioc_key_list_t\n");
26441 			rval = EFAULT;
26442 			goto done;
26443 		}
26444 		break;
26445 	}
26446 
26447 #else /* ! _MULTI_DATAMODEL */
26448 	copysz = sizeof (mhioc_key_list_t);
26449 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26450 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26451 		    "sd_persistent_reservation_in_read_keys: "
26452 		    "failed ddi_copyin: mhioc_key_list_t\n");
26453 		rval = EFAULT;
26454 		goto done;
26455 	}
26456 #endif
26457 
26458 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26459 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26460 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26461 
26462 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26463 	    data_len, data_bufp);
26464 	if (rval != 0) {
26465 		if (rval == EIO)
26466 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26467 		else
26468 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26469 		goto done;
26470 	}
26471 	in = (sd_prin_readkeys_t *)data_bufp;
26472 	ptr->generation = BE_32(in->generation);
26473 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26474 
26475 	/*
26476 	 * Return the min(listsize, listlen) keys
26477 	 */
26478 #ifdef _MULTI_DATAMODEL
26479 
26480 	switch (ddi_model_convert_from(flag & FMODELS)) {
26481 	case DDI_MODEL_ILP32:
26482 		li32.listlen = li.listlen;
26483 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26484 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26485 			    "sd_persistent_reservation_in_read_keys: "
26486 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26487 			rval = EFAULT;
26488 			goto done;
26489 		}
26490 		break;
26491 
26492 	case DDI_MODEL_NONE:
26493 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26494 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26495 			    "sd_persistent_reservation_in_read_keys: "
26496 			    "failed ddi_copyout: mhioc_key_list_t\n");
26497 			rval = EFAULT;
26498 			goto done;
26499 		}
26500 		break;
26501 	}
26502 
26503 #else /* ! _MULTI_DATAMODEL */
26504 
26505 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26506 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26507 		    "sd_persistent_reservation_in_read_keys: "
26508 		    "failed ddi_copyout: mhioc_key_list_t\n");
26509 		rval = EFAULT;
26510 		goto done;
26511 	}
26512 
26513 #endif /* _MULTI_DATAMODEL */
26514 
26515 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26516 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26517 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26518 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26519 		    "sd_persistent_reservation_in_read_keys: "
26520 		    "failed ddi_copyout: keylist\n");
26521 		rval = EFAULT;
26522 	}
26523 done:
26524 	sd_ssc_fini(ssc);
26525 	kmem_free(data_bufp, data_len);
26526 	return (rval);
26527 }
26528 
26529 
26530 /*
26531  *    Function: sd_persistent_reservation_in_read_resv
26532  *
26533  * Description: This routine is the driver entry point for handling CD-ROM
26534  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26535  *		by sending the SCSI-3 PRIN commands to the device.
26536  *		Process the read persistent reservations command response by
26537  *		copying the reservation information into the user provided
26538  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26539  *
26540  *   Arguments: un   -  Pointer to soft state struct for the target.
26541  *		usrp -	user provided pointer to multihost Persistent In Read
26542  *			Keys structure (mhioc_inkeys_t)
26543  *		flag -	this argument is a pass through to ddi_copyxxx()
26544  *			directly from the mode argument of ioctl().
26545  *
26546  * Return Code: 0   - Success
26547  *		EACCES
26548  *		ENOTSUP
26549  *		errno return code from sd_send_scsi_cmd()
26550  *
26551  *     Context: Can sleep. Does not return until command is completed.
26552  */
26553 
26554 static int
26555 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26556     mhioc_inresvs_t *usrp, int flag)
26557 {
26558 #ifdef _MULTI_DATAMODEL
26559 	struct mhioc_resv_desc_list32 resvlist32;
26560 #endif
26561 	sd_prin_readresv_t	*in;
26562 	mhioc_inresvs_t		*ptr;
26563 	sd_readresv_desc_t	*readresv_ptr;
26564 	mhioc_resv_desc_list_t	resvlist;
26565 	mhioc_resv_desc_t 	resvdesc;
26566 	uchar_t			*data_bufp = NULL;
26567 	int 			data_len;
26568 	int			rval = 0;
26569 	int			i;
26570 	size_t			copysz;
26571 	mhioc_resv_desc_t	*bufp;
26572 	sd_ssc_t		*ssc;
26573 
26574 	if ((ptr = usrp) == NULL) {
26575 		return (EINVAL);
26576 	}
26577 
26578 	ssc = sd_ssc_init(un);
26579 
26580 	/*
26581 	 * Get the listsize from user
26582 	 */
26583 #ifdef _MULTI_DATAMODEL
26584 	switch (ddi_model_convert_from(flag & FMODELS)) {
26585 	case DDI_MODEL_ILP32:
26586 		copysz = sizeof (struct mhioc_resv_desc_list32);
26587 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26588 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26589 			    "sd_persistent_reservation_in_read_resv: "
26590 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26591 			rval = EFAULT;
26592 			goto done;
26593 		}
26594 		resvlist.listsize = resvlist32.listsize;
26595 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26596 		break;
26597 
26598 	case DDI_MODEL_NONE:
26599 		copysz = sizeof (mhioc_resv_desc_list_t);
26600 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26601 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26602 			    "sd_persistent_reservation_in_read_resv: "
26603 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26604 			rval = EFAULT;
26605 			goto done;
26606 		}
26607 		break;
26608 	}
26609 #else /* ! _MULTI_DATAMODEL */
26610 	copysz = sizeof (mhioc_resv_desc_list_t);
26611 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26612 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26613 		    "sd_persistent_reservation_in_read_resv: "
26614 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26615 		rval = EFAULT;
26616 		goto done;
26617 	}
26618 #endif /* ! _MULTI_DATAMODEL */
26619 
26620 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26621 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26622 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26623 
26624 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26625 	    data_len, data_bufp);
26626 	if (rval != 0) {
26627 		if (rval == EIO)
26628 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26629 		else
26630 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26631 		goto done;
26632 	}
26633 	in = (sd_prin_readresv_t *)data_bufp;
26634 	ptr->generation = BE_32(in->generation);
26635 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26636 
26637 	/*
26638 	 * Return the min(listsize, listlen( keys
26639 	 */
26640 #ifdef _MULTI_DATAMODEL
26641 
26642 	switch (ddi_model_convert_from(flag & FMODELS)) {
26643 	case DDI_MODEL_ILP32:
26644 		resvlist32.listlen = resvlist.listlen;
26645 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26646 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26647 			    "sd_persistent_reservation_in_read_resv: "
26648 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26649 			rval = EFAULT;
26650 			goto done;
26651 		}
26652 		break;
26653 
26654 	case DDI_MODEL_NONE:
26655 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26656 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26657 			    "sd_persistent_reservation_in_read_resv: "
26658 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26659 			rval = EFAULT;
26660 			goto done;
26661 		}
26662 		break;
26663 	}
26664 
26665 #else /* ! _MULTI_DATAMODEL */
26666 
26667 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26668 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26669 		    "sd_persistent_reservation_in_read_resv: "
26670 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26671 		rval = EFAULT;
26672 		goto done;
26673 	}
26674 
26675 #endif /* ! _MULTI_DATAMODEL */
26676 
26677 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26678 	bufp = resvlist.list;
26679 	copysz = sizeof (mhioc_resv_desc_t);
26680 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26681 	    i++, readresv_ptr++, bufp++) {
26682 
26683 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26684 		    MHIOC_RESV_KEY_SIZE);
26685 		resvdesc.type  = readresv_ptr->type;
26686 		resvdesc.scope = readresv_ptr->scope;
26687 		resvdesc.scope_specific_addr =
26688 		    BE_32(readresv_ptr->scope_specific_addr);
26689 
26690 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26691 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26692 			    "sd_persistent_reservation_in_read_resv: "
26693 			    "failed ddi_copyout: resvlist\n");
26694 			rval = EFAULT;
26695 			goto done;
26696 		}
26697 	}
26698 done:
26699 	sd_ssc_fini(ssc);
26700 	/* only if data_bufp is allocated, we need to free it */
26701 	if (data_bufp) {
26702 		kmem_free(data_bufp, data_len);
26703 	}
26704 	return (rval);
26705 }
26706 
26707 
26708 /*
26709  *    Function: sr_change_blkmode()
26710  *
26711  * Description: This routine is the driver entry point for handling CD-ROM
26712  *		block mode ioctl requests. Support for returning and changing
26713  *		the current block size in use by the device is implemented. The
26714  *		LBA size is changed via a MODE SELECT Block Descriptor.
26715  *
26716  *		This routine issues a mode sense with an allocation length of
26717  *		12 bytes for the mode page header and a single block descriptor.
26718  *
26719  *   Arguments: dev - the device 'dev_t'
26720  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26721  *		      CDROMSBLKMODE (set)
26722  *		data - current block size or requested block size
26723  *		flag - this argument is a pass through to ddi_copyxxx() directly
26724  *		       from the mode argument of ioctl().
26725  *
26726  * Return Code: the code returned by sd_send_scsi_cmd()
26727  *		EINVAL if invalid arguments are provided
26728  *		EFAULT if ddi_copyxxx() fails
26729  *		ENXIO if fail ddi_get_soft_state
26730  *		EIO if invalid mode sense block descriptor length
26731  *
26732  */
26733 
26734 static int
26735 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26736 {
26737 	struct sd_lun			*un = NULL;
26738 	struct mode_header		*sense_mhp, *select_mhp;
26739 	struct block_descriptor		*sense_desc, *select_desc;
26740 	int				current_bsize;
26741 	int				rval = EINVAL;
26742 	uchar_t				*sense = NULL;
26743 	uchar_t				*select = NULL;
26744 	sd_ssc_t			*ssc;
26745 
26746 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26747 
26748 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26749 		return (ENXIO);
26750 	}
26751 
26752 	/*
26753 	 * The block length is changed via the Mode Select block descriptor, the
26754 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26755 	 * required as part of this routine. Therefore the mode sense allocation
26756 	 * length is specified to be the length of a mode page header and a
26757 	 * block descriptor.
26758 	 */
26759 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26760 
26761 	ssc = sd_ssc_init(un);
26762 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26763 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26764 	sd_ssc_fini(ssc);
26765 	if (rval != 0) {
26766 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26767 		    "sr_change_blkmode: Mode Sense Failed\n");
26768 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26769 		return (rval);
26770 	}
26771 
26772 	/* Check the block descriptor len to handle only 1 block descriptor */
26773 	sense_mhp = (struct mode_header *)sense;
26774 	if ((sense_mhp->bdesc_length == 0) ||
26775 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26776 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26777 		    "sr_change_blkmode: Mode Sense returned invalid block"
26778 		    " descriptor length\n");
26779 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26780 		return (EIO);
26781 	}
26782 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26783 	current_bsize = ((sense_desc->blksize_hi << 16) |
26784 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26785 
26786 	/* Process command */
26787 	switch (cmd) {
26788 	case CDROMGBLKMODE:
26789 		/* Return the block size obtained during the mode sense */
26790 		if (ddi_copyout(&current_bsize, (void *)data,
26791 		    sizeof (int), flag) != 0)
26792 			rval = EFAULT;
26793 		break;
26794 	case CDROMSBLKMODE:
26795 		/* Validate the requested block size */
26796 		switch (data) {
26797 		case CDROM_BLK_512:
26798 		case CDROM_BLK_1024:
26799 		case CDROM_BLK_2048:
26800 		case CDROM_BLK_2056:
26801 		case CDROM_BLK_2336:
26802 		case CDROM_BLK_2340:
26803 		case CDROM_BLK_2352:
26804 		case CDROM_BLK_2368:
26805 		case CDROM_BLK_2448:
26806 		case CDROM_BLK_2646:
26807 		case CDROM_BLK_2647:
26808 			break;
26809 		default:
26810 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26811 			    "sr_change_blkmode: "
26812 			    "Block Size '%ld' Not Supported\n", data);
26813 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26814 			return (EINVAL);
26815 		}
26816 
26817 		/*
26818 		 * The current block size matches the requested block size so
26819 		 * there is no need to send the mode select to change the size
26820 		 */
26821 		if (current_bsize == data) {
26822 			break;
26823 		}
26824 
26825 		/* Build the select data for the requested block size */
26826 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26827 		select_mhp = (struct mode_header *)select;
26828 		select_desc =
26829 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26830 		/*
26831 		 * The LBA size is changed via the block descriptor, so the
26832 		 * descriptor is built according to the user data
26833 		 */
26834 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26835 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26836 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26837 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
26838 
26839 		/* Send the mode select for the requested block size */
26840 		ssc = sd_ssc_init(un);
26841 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26842 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26843 		    SD_PATH_STANDARD);
26844 		sd_ssc_fini(ssc);
26845 		if (rval != 0) {
26846 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26847 			    "sr_change_blkmode: Mode Select Failed\n");
26848 			/*
26849 			 * The mode select failed for the requested block size,
26850 			 * so reset the data for the original block size and
26851 			 * send it to the target. The error is indicated by the
26852 			 * return value for the failed mode select.
26853 			 */
26854 			select_desc->blksize_hi  = sense_desc->blksize_hi;
26855 			select_desc->blksize_mid = sense_desc->blksize_mid;
26856 			select_desc->blksize_lo  = sense_desc->blksize_lo;
26857 			ssc = sd_ssc_init(un);
26858 			(void) 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 		} else {
26863 			ASSERT(!mutex_owned(SD_MUTEX(un)));
26864 			mutex_enter(SD_MUTEX(un));
26865 			sd_update_block_info(un, (uint32_t)data, 0);
26866 			mutex_exit(SD_MUTEX(un));
26867 		}
26868 		break;
26869 	default:
26870 		/* should not reach here, but check anyway */
26871 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26872 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
26873 		rval = EINVAL;
26874 		break;
26875 	}
26876 
26877 	if (select) {
26878 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
26879 	}
26880 	if (sense) {
26881 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26882 	}
26883 	return (rval);
26884 }
26885 
26886 
26887 /*
26888  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
26889  * implement driver support for getting and setting the CD speed. The command
26890  * set used will be based on the device type. If the device has not been
26891  * identified as MMC the Toshiba vendor specific mode page will be used. If
26892  * the device is MMC but does not support the Real Time Streaming feature
26893  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
26894  * be used to read the speed.
26895  */
26896 
26897 /*
26898  *    Function: sr_change_speed()
26899  *
26900  * Description: This routine is the driver entry point for handling CD-ROM
26901  *		drive speed ioctl requests for devices supporting the Toshiba
26902  *		vendor specific drive speed mode page. Support for returning
26903  *		and changing the current drive speed in use by the device is
26904  *		implemented.
26905  *
26906  *   Arguments: dev - the device 'dev_t'
26907  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
26908  *		      CDROMSDRVSPEED (set)
26909  *		data - current drive speed or requested drive speed
26910  *		flag - this argument is a pass through to ddi_copyxxx() directly
26911  *		       from the mode argument of ioctl().
26912  *
26913  * Return Code: the code returned by sd_send_scsi_cmd()
26914  *		EINVAL if invalid arguments are provided
26915  *		EFAULT if ddi_copyxxx() fails
26916  *		ENXIO if fail ddi_get_soft_state
26917  *		EIO if invalid mode sense block descriptor length
26918  */
26919 
26920 static int
26921 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
26922 {
26923 	struct sd_lun			*un = NULL;
26924 	struct mode_header		*sense_mhp, *select_mhp;
26925 	struct mode_speed		*sense_page, *select_page;
26926 	int				current_speed;
26927 	int				rval = EINVAL;
26928 	int				bd_len;
26929 	uchar_t				*sense = NULL;
26930 	uchar_t				*select = NULL;
26931 	sd_ssc_t			*ssc;
26932 
26933 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
26934 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26935 		return (ENXIO);
26936 	}
26937 
26938 	/*
26939 	 * Note: The drive speed is being modified here according to a Toshiba
26940 	 * vendor specific mode page (0x31).
26941 	 */
26942 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
26943 
26944 	ssc = sd_ssc_init(un);
26945 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26946 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
26947 	    SD_PATH_STANDARD);
26948 	sd_ssc_fini(ssc);
26949 	if (rval != 0) {
26950 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26951 		    "sr_change_speed: Mode Sense Failed\n");
26952 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26953 		return (rval);
26954 	}
26955 	sense_mhp  = (struct mode_header *)sense;
26956 
26957 	/* Check the block descriptor len to handle only 1 block descriptor */
26958 	bd_len = sense_mhp->bdesc_length;
26959 	if (bd_len > MODE_BLK_DESC_LENGTH) {
26960 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26961 		    "sr_change_speed: Mode Sense returned invalid block "
26962 		    "descriptor length\n");
26963 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26964 		return (EIO);
26965 	}
26966 
26967 	sense_page = (struct mode_speed *)
26968 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
26969 	current_speed = sense_page->speed;
26970 
26971 	/* Process command */
26972 	switch (cmd) {
26973 	case CDROMGDRVSPEED:
26974 		/* Return the drive speed obtained during the mode sense */
26975 		if (current_speed == 0x2) {
26976 			current_speed = CDROM_TWELVE_SPEED;
26977 		}
26978 		if (ddi_copyout(&current_speed, (void *)data,
26979 		    sizeof (int), flag) != 0) {
26980 			rval = EFAULT;
26981 		}
26982 		break;
26983 	case CDROMSDRVSPEED:
26984 		/* Validate the requested drive speed */
26985 		switch ((uchar_t)data) {
26986 		case CDROM_TWELVE_SPEED:
26987 			data = 0x2;
26988 			/*FALLTHROUGH*/
26989 		case CDROM_NORMAL_SPEED:
26990 		case CDROM_DOUBLE_SPEED:
26991 		case CDROM_QUAD_SPEED:
26992 		case CDROM_MAXIMUM_SPEED:
26993 			break;
26994 		default:
26995 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26996 			    "sr_change_speed: "
26997 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
26998 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26999 			return (EINVAL);
27000 		}
27001 
27002 		/*
27003 		 * The current drive speed matches the requested drive speed so
27004 		 * there is no need to send the mode select to change the speed
27005 		 */
27006 		if (current_speed == data) {
27007 			break;
27008 		}
27009 
27010 		/* Build the select data for the requested drive speed */
27011 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27012 		select_mhp = (struct mode_header *)select;
27013 		select_mhp->bdesc_length = 0;
27014 		select_page =
27015 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27016 		select_page =
27017 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27018 		select_page->mode_page.code = CDROM_MODE_SPEED;
27019 		select_page->mode_page.length = 2;
27020 		select_page->speed = (uchar_t)data;
27021 
27022 		/* Send the mode select for the requested block size */
27023 		ssc = sd_ssc_init(un);
27024 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27025 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27026 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27027 		sd_ssc_fini(ssc);
27028 		if (rval != 0) {
27029 			/*
27030 			 * The mode select failed for the requested drive speed,
27031 			 * so reset the data for the original drive speed and
27032 			 * send it to the target. The error is indicated by the
27033 			 * return value for the failed mode select.
27034 			 */
27035 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27036 			    "sr_drive_speed: Mode Select Failed\n");
27037 			select_page->speed = sense_page->speed;
27038 			ssc = sd_ssc_init(un);
27039 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27040 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27041 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27042 			sd_ssc_fini(ssc);
27043 		}
27044 		break;
27045 	default:
27046 		/* should not reach here, but check anyway */
27047 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27048 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27049 		rval = EINVAL;
27050 		break;
27051 	}
27052 
27053 	if (select) {
27054 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27055 	}
27056 	if (sense) {
27057 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27058 	}
27059 
27060 	return (rval);
27061 }
27062 
27063 
27064 /*
27065  *    Function: sr_atapi_change_speed()
27066  *
27067  * Description: This routine is the driver entry point for handling CD-ROM
27068  *		drive speed ioctl requests for MMC devices that do not support
27069  *		the Real Time Streaming feature (0x107).
27070  *
27071  *		Note: This routine will use the SET SPEED command which may not
27072  *		be supported by all devices.
27073  *
27074  *   Arguments: dev- the device 'dev_t'
27075  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27076  *		     CDROMSDRVSPEED (set)
27077  *		data- current drive speed or requested drive speed
27078  *		flag- this argument is a pass through to ddi_copyxxx() directly
27079  *		      from the mode argument of ioctl().
27080  *
27081  * Return Code: the code returned by sd_send_scsi_cmd()
27082  *		EINVAL if invalid arguments are provided
27083  *		EFAULT if ddi_copyxxx() fails
27084  *		ENXIO if fail ddi_get_soft_state
27085  *		EIO if invalid mode sense block descriptor length
27086  */
27087 
27088 static int
27089 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27090 {
27091 	struct sd_lun			*un;
27092 	struct uscsi_cmd		*com = NULL;
27093 	struct mode_header_grp2		*sense_mhp;
27094 	uchar_t				*sense_page;
27095 	uchar_t				*sense = NULL;
27096 	char				cdb[CDB_GROUP5];
27097 	int				bd_len;
27098 	int				current_speed = 0;
27099 	int				max_speed = 0;
27100 	int				rval;
27101 	sd_ssc_t			*ssc;
27102 
27103 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27104 
27105 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27106 		return (ENXIO);
27107 	}
27108 
27109 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27110 
27111 	ssc = sd_ssc_init(un);
27112 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27113 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27114 	    SD_PATH_STANDARD);
27115 	sd_ssc_fini(ssc);
27116 	if (rval != 0) {
27117 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27118 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27119 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27120 		return (rval);
27121 	}
27122 
27123 	/* Check the block descriptor len to handle only 1 block descriptor */
27124 	sense_mhp = (struct mode_header_grp2 *)sense;
27125 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27126 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27127 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27128 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27129 		    "block descriptor length\n");
27130 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27131 		return (EIO);
27132 	}
27133 
27134 	/* Calculate the current and maximum drive speeds */
27135 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27136 	current_speed = (sense_page[14] << 8) | sense_page[15];
27137 	max_speed = (sense_page[8] << 8) | sense_page[9];
27138 
27139 	/* Process the command */
27140 	switch (cmd) {
27141 	case CDROMGDRVSPEED:
27142 		current_speed /= SD_SPEED_1X;
27143 		if (ddi_copyout(&current_speed, (void *)data,
27144 		    sizeof (int), flag) != 0)
27145 			rval = EFAULT;
27146 		break;
27147 	case CDROMSDRVSPEED:
27148 		/* Convert the speed code to KB/sec */
27149 		switch ((uchar_t)data) {
27150 		case CDROM_NORMAL_SPEED:
27151 			current_speed = SD_SPEED_1X;
27152 			break;
27153 		case CDROM_DOUBLE_SPEED:
27154 			current_speed = 2 * SD_SPEED_1X;
27155 			break;
27156 		case CDROM_QUAD_SPEED:
27157 			current_speed = 4 * SD_SPEED_1X;
27158 			break;
27159 		case CDROM_TWELVE_SPEED:
27160 			current_speed = 12 * SD_SPEED_1X;
27161 			break;
27162 		case CDROM_MAXIMUM_SPEED:
27163 			current_speed = 0xffff;
27164 			break;
27165 		default:
27166 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27167 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27168 			    (uchar_t)data);
27169 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27170 			return (EINVAL);
27171 		}
27172 
27173 		/* Check the request against the drive's max speed. */
27174 		if (current_speed != 0xffff) {
27175 			if (current_speed > max_speed) {
27176 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27177 				return (EINVAL);
27178 			}
27179 		}
27180 
27181 		/*
27182 		 * Build and send the SET SPEED command
27183 		 *
27184 		 * Note: The SET SPEED (0xBB) command used in this routine is
27185 		 * obsolete per the SCSI MMC spec but still supported in the
27186 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27187 		 * therefore the command is still implemented in this routine.
27188 		 */
27189 		bzero(cdb, sizeof (cdb));
27190 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27191 		cdb[2] = (uchar_t)(current_speed >> 8);
27192 		cdb[3] = (uchar_t)current_speed;
27193 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27194 		com->uscsi_cdb	   = (caddr_t)cdb;
27195 		com->uscsi_cdblen  = CDB_GROUP5;
27196 		com->uscsi_bufaddr = NULL;
27197 		com->uscsi_buflen  = 0;
27198 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27199 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27200 		break;
27201 	default:
27202 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27203 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27204 		rval = EINVAL;
27205 	}
27206 
27207 	if (sense) {
27208 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27209 	}
27210 	if (com) {
27211 		kmem_free(com, sizeof (*com));
27212 	}
27213 	return (rval);
27214 }
27215 
27216 
27217 /*
27218  *    Function: sr_pause_resume()
27219  *
27220  * Description: This routine is the driver entry point for handling CD-ROM
27221  *		pause/resume ioctl requests. This only affects the audio play
27222  *		operation.
27223  *
27224  *   Arguments: dev - the device 'dev_t'
27225  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27226  *		      for setting the resume bit of the cdb.
27227  *
27228  * Return Code: the code returned by sd_send_scsi_cmd()
27229  *		EINVAL if invalid mode specified
27230  *
27231  */
27232 
27233 static int
27234 sr_pause_resume(dev_t dev, int cmd)
27235 {
27236 	struct sd_lun		*un;
27237 	struct uscsi_cmd	*com;
27238 	char			cdb[CDB_GROUP1];
27239 	int			rval;
27240 
27241 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27242 		return (ENXIO);
27243 	}
27244 
27245 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27246 	bzero(cdb, CDB_GROUP1);
27247 	cdb[0] = SCMD_PAUSE_RESUME;
27248 	switch (cmd) {
27249 	case CDROMRESUME:
27250 		cdb[8] = 1;
27251 		break;
27252 	case CDROMPAUSE:
27253 		cdb[8] = 0;
27254 		break;
27255 	default:
27256 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27257 		    " Command '%x' Not Supported\n", cmd);
27258 		rval = EINVAL;
27259 		goto done;
27260 	}
27261 
27262 	com->uscsi_cdb    = cdb;
27263 	com->uscsi_cdblen = CDB_GROUP1;
27264 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27265 
27266 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27267 	    SD_PATH_STANDARD);
27268 
27269 done:
27270 	kmem_free(com, sizeof (*com));
27271 	return (rval);
27272 }
27273 
27274 
27275 /*
27276  *    Function: sr_play_msf()
27277  *
27278  * Description: This routine is the driver entry point for handling CD-ROM
27279  *		ioctl requests to output the audio signals at the specified
27280  *		starting address and continue the audio play until the specified
27281  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27282  *		Frame (MSF) format.
27283  *
27284  *   Arguments: dev	- the device 'dev_t'
27285  *		data	- pointer to user provided audio msf structure,
27286  *		          specifying start/end addresses.
27287  *		flag	- this argument is a pass through to ddi_copyxxx()
27288  *		          directly from the mode argument of ioctl().
27289  *
27290  * Return Code: the code returned by sd_send_scsi_cmd()
27291  *		EFAULT if ddi_copyxxx() fails
27292  *		ENXIO if fail ddi_get_soft_state
27293  *		EINVAL if data pointer is NULL
27294  */
27295 
27296 static int
27297 sr_play_msf(dev_t dev, caddr_t data, int flag)
27298 {
27299 	struct sd_lun		*un;
27300 	struct uscsi_cmd	*com;
27301 	struct cdrom_msf	msf_struct;
27302 	struct cdrom_msf	*msf = &msf_struct;
27303 	char			cdb[CDB_GROUP1];
27304 	int			rval;
27305 
27306 	if (data == NULL) {
27307 		return (EINVAL);
27308 	}
27309 
27310 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27311 		return (ENXIO);
27312 	}
27313 
27314 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27315 		return (EFAULT);
27316 	}
27317 
27318 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27319 	bzero(cdb, CDB_GROUP1);
27320 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27321 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27322 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27323 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27324 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27325 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27326 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27327 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27328 	} else {
27329 		cdb[3] = msf->cdmsf_min0;
27330 		cdb[4] = msf->cdmsf_sec0;
27331 		cdb[5] = msf->cdmsf_frame0;
27332 		cdb[6] = msf->cdmsf_min1;
27333 		cdb[7] = msf->cdmsf_sec1;
27334 		cdb[8] = msf->cdmsf_frame1;
27335 	}
27336 	com->uscsi_cdb    = cdb;
27337 	com->uscsi_cdblen = CDB_GROUP1;
27338 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27339 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27340 	    SD_PATH_STANDARD);
27341 	kmem_free(com, sizeof (*com));
27342 	return (rval);
27343 }
27344 
27345 
27346 /*
27347  *    Function: sr_play_trkind()
27348  *
27349  * Description: This routine is the driver entry point for handling CD-ROM
27350  *		ioctl requests to output the audio signals at the specified
27351  *		starting address and continue the audio play until the specified
27352  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27353  *		format.
27354  *
27355  *   Arguments: dev	- the device 'dev_t'
27356  *		data	- pointer to user provided audio track/index structure,
27357  *		          specifying start/end addresses.
27358  *		flag	- this argument is a pass through to ddi_copyxxx()
27359  *		          directly from the mode argument of ioctl().
27360  *
27361  * Return Code: the code returned by sd_send_scsi_cmd()
27362  *		EFAULT if ddi_copyxxx() fails
27363  *		ENXIO if fail ddi_get_soft_state
27364  *		EINVAL if data pointer is NULL
27365  */
27366 
27367 static int
27368 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27369 {
27370 	struct cdrom_ti		ti_struct;
27371 	struct cdrom_ti		*ti = &ti_struct;
27372 	struct uscsi_cmd	*com = NULL;
27373 	char			cdb[CDB_GROUP1];
27374 	int			rval;
27375 
27376 	if (data == NULL) {
27377 		return (EINVAL);
27378 	}
27379 
27380 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27381 		return (EFAULT);
27382 	}
27383 
27384 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27385 	bzero(cdb, CDB_GROUP1);
27386 	cdb[0] = SCMD_PLAYAUDIO_TI;
27387 	cdb[4] = ti->cdti_trk0;
27388 	cdb[5] = ti->cdti_ind0;
27389 	cdb[7] = ti->cdti_trk1;
27390 	cdb[8] = ti->cdti_ind1;
27391 	com->uscsi_cdb    = cdb;
27392 	com->uscsi_cdblen = CDB_GROUP1;
27393 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27394 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27395 	    SD_PATH_STANDARD);
27396 	kmem_free(com, sizeof (*com));
27397 	return (rval);
27398 }
27399 
27400 
27401 /*
27402  *    Function: sr_read_all_subcodes()
27403  *
27404  * Description: This routine is the driver entry point for handling CD-ROM
27405  *		ioctl requests to return raw subcode data while the target is
27406  *		playing audio (CDROMSUBCODE).
27407  *
27408  *   Arguments: dev	- the device 'dev_t'
27409  *		data	- pointer to user provided cdrom subcode structure,
27410  *		          specifying the transfer length and address.
27411  *		flag	- this argument is a pass through to ddi_copyxxx()
27412  *		          directly from the mode argument of ioctl().
27413  *
27414  * Return Code: the code returned by sd_send_scsi_cmd()
27415  *		EFAULT if ddi_copyxxx() fails
27416  *		ENXIO if fail ddi_get_soft_state
27417  *		EINVAL if data pointer is NULL
27418  */
27419 
27420 static int
27421 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27422 {
27423 	struct sd_lun		*un = NULL;
27424 	struct uscsi_cmd	*com = NULL;
27425 	struct cdrom_subcode	*subcode = NULL;
27426 	int			rval;
27427 	size_t			buflen;
27428 	char			cdb[CDB_GROUP5];
27429 
27430 #ifdef _MULTI_DATAMODEL
27431 	/* To support ILP32 applications in an LP64 world */
27432 	struct cdrom_subcode32		cdrom_subcode32;
27433 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27434 #endif
27435 	if (data == NULL) {
27436 		return (EINVAL);
27437 	}
27438 
27439 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27440 		return (ENXIO);
27441 	}
27442 
27443 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27444 
27445 #ifdef _MULTI_DATAMODEL
27446 	switch (ddi_model_convert_from(flag & FMODELS)) {
27447 	case DDI_MODEL_ILP32:
27448 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27449 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27450 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27451 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27452 			return (EFAULT);
27453 		}
27454 		/* Convert the ILP32 uscsi data from the application to LP64 */
27455 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27456 		break;
27457 	case DDI_MODEL_NONE:
27458 		if (ddi_copyin(data, subcode,
27459 		    sizeof (struct cdrom_subcode), flag)) {
27460 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27461 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27462 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27463 			return (EFAULT);
27464 		}
27465 		break;
27466 	}
27467 #else /* ! _MULTI_DATAMODEL */
27468 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27469 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27470 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27471 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27472 		return (EFAULT);
27473 	}
27474 #endif /* _MULTI_DATAMODEL */
27475 
27476 	/*
27477 	 * Since MMC-2 expects max 3 bytes for length, check if the
27478 	 * length input is greater than 3 bytes
27479 	 */
27480 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27481 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27482 		    "sr_read_all_subcodes: "
27483 		    "cdrom transfer length too large: %d (limit %d)\n",
27484 		    subcode->cdsc_length, 0xFFFFFF);
27485 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27486 		return (EINVAL);
27487 	}
27488 
27489 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27490 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27491 	bzero(cdb, CDB_GROUP5);
27492 
27493 	if (un->un_f_mmc_cap == TRUE) {
27494 		cdb[0] = (char)SCMD_READ_CD;
27495 		cdb[2] = (char)0xff;
27496 		cdb[3] = (char)0xff;
27497 		cdb[4] = (char)0xff;
27498 		cdb[5] = (char)0xff;
27499 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27500 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27501 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27502 		cdb[10] = 1;
27503 	} else {
27504 		/*
27505 		 * Note: A vendor specific command (0xDF) is being used her to
27506 		 * request a read of all subcodes.
27507 		 */
27508 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27509 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27510 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27511 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27512 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27513 	}
27514 	com->uscsi_cdb	   = cdb;
27515 	com->uscsi_cdblen  = CDB_GROUP5;
27516 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27517 	com->uscsi_buflen  = buflen;
27518 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27519 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27520 	    SD_PATH_STANDARD);
27521 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27522 	kmem_free(com, sizeof (*com));
27523 	return (rval);
27524 }
27525 
27526 
27527 /*
27528  *    Function: sr_read_subchannel()
27529  *
27530  * Description: This routine is the driver entry point for handling CD-ROM
27531  *		ioctl requests to return the Q sub-channel data of the CD
27532  *		current position block. (CDROMSUBCHNL) The data includes the
27533  *		track number, index number, absolute CD-ROM address (LBA or MSF
27534  *		format per the user) , track relative CD-ROM address (LBA or MSF
27535  *		format per the user), control data and audio status.
27536  *
27537  *   Arguments: dev	- the device 'dev_t'
27538  *		data	- pointer to user provided cdrom sub-channel structure
27539  *		flag	- this argument is a pass through to ddi_copyxxx()
27540  *		          directly from the mode argument of ioctl().
27541  *
27542  * Return Code: the code returned by sd_send_scsi_cmd()
27543  *		EFAULT if ddi_copyxxx() fails
27544  *		ENXIO if fail ddi_get_soft_state
27545  *		EINVAL if data pointer is NULL
27546  */
27547 
27548 static int
27549 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27550 {
27551 	struct sd_lun		*un;
27552 	struct uscsi_cmd	*com;
27553 	struct cdrom_subchnl	subchanel;
27554 	struct cdrom_subchnl	*subchnl = &subchanel;
27555 	char			cdb[CDB_GROUP1];
27556 	caddr_t			buffer;
27557 	int			rval;
27558 
27559 	if (data == NULL) {
27560 		return (EINVAL);
27561 	}
27562 
27563 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27564 	    (un->un_state == SD_STATE_OFFLINE)) {
27565 		return (ENXIO);
27566 	}
27567 
27568 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27569 		return (EFAULT);
27570 	}
27571 
27572 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27573 	bzero(cdb, CDB_GROUP1);
27574 	cdb[0] = SCMD_READ_SUBCHANNEL;
27575 	/* Set the MSF bit based on the user requested address format */
27576 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27577 	/*
27578 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27579 	 * returned
27580 	 */
27581 	cdb[2] = 0x40;
27582 	/*
27583 	 * Set byte 3 to specify the return data format. A value of 0x01
27584 	 * indicates that the CD-ROM current position should be returned.
27585 	 */
27586 	cdb[3] = 0x01;
27587 	cdb[8] = 0x10;
27588 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27589 	com->uscsi_cdb	   = cdb;
27590 	com->uscsi_cdblen  = CDB_GROUP1;
27591 	com->uscsi_bufaddr = buffer;
27592 	com->uscsi_buflen  = 16;
27593 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27594 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27595 	    SD_PATH_STANDARD);
27596 	if (rval != 0) {
27597 		kmem_free(buffer, 16);
27598 		kmem_free(com, sizeof (*com));
27599 		return (rval);
27600 	}
27601 
27602 	/* Process the returned Q sub-channel data */
27603 	subchnl->cdsc_audiostatus = buffer[1];
27604 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
27605 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27606 	subchnl->cdsc_trk	= buffer[6];
27607 	subchnl->cdsc_ind	= buffer[7];
27608 	if (subchnl->cdsc_format & CDROM_LBA) {
27609 		subchnl->cdsc_absaddr.lba =
27610 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27611 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27612 		subchnl->cdsc_reladdr.lba =
27613 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27614 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27615 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27616 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27617 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27618 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27619 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27620 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27621 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27622 	} else {
27623 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27624 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27625 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27626 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27627 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27628 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27629 	}
27630 	kmem_free(buffer, 16);
27631 	kmem_free(com, sizeof (*com));
27632 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27633 	    != 0) {
27634 		return (EFAULT);
27635 	}
27636 	return (rval);
27637 }
27638 
27639 
27640 /*
27641  *    Function: sr_read_tocentry()
27642  *
27643  * Description: This routine is the driver entry point for handling CD-ROM
27644  *		ioctl requests to read from the Table of Contents (TOC)
27645  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27646  *		fields, the starting address (LBA or MSF format per the user)
27647  *		and the data mode if the user specified track is a data track.
27648  *
27649  *		Note: The READ HEADER (0x44) command used in this routine is
27650  *		obsolete per the SCSI MMC spec but still supported in the
27651  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27652  *		therefore the command is still implemented in this routine.
27653  *
27654  *   Arguments: dev	- the device 'dev_t'
27655  *		data	- pointer to user provided toc entry structure,
27656  *			  specifying the track # and the address format
27657  *			  (LBA or MSF).
27658  *		flag	- this argument is a pass through to ddi_copyxxx()
27659  *		          directly from the mode argument of ioctl().
27660  *
27661  * Return Code: the code returned by sd_send_scsi_cmd()
27662  *		EFAULT if ddi_copyxxx() fails
27663  *		ENXIO if fail ddi_get_soft_state
27664  *		EINVAL if data pointer is NULL
27665  */
27666 
27667 static int
27668 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27669 {
27670 	struct sd_lun		*un = NULL;
27671 	struct uscsi_cmd	*com;
27672 	struct cdrom_tocentry	toc_entry;
27673 	struct cdrom_tocentry	*entry = &toc_entry;
27674 	caddr_t			buffer;
27675 	int			rval;
27676 	char			cdb[CDB_GROUP1];
27677 
27678 	if (data == NULL) {
27679 		return (EINVAL);
27680 	}
27681 
27682 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27683 	    (un->un_state == SD_STATE_OFFLINE)) {
27684 		return (ENXIO);
27685 	}
27686 
27687 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27688 		return (EFAULT);
27689 	}
27690 
27691 	/* Validate the requested track and address format */
27692 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27693 		return (EINVAL);
27694 	}
27695 
27696 	if (entry->cdte_track == 0) {
27697 		return (EINVAL);
27698 	}
27699 
27700 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27701 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27702 	bzero(cdb, CDB_GROUP1);
27703 
27704 	cdb[0] = SCMD_READ_TOC;
27705 	/* Set the MSF bit based on the user requested address format  */
27706 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27707 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27708 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27709 	} else {
27710 		cdb[6] = entry->cdte_track;
27711 	}
27712 
27713 	/*
27714 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27715 	 * (4 byte TOC response header + 8 byte track descriptor)
27716 	 */
27717 	cdb[8] = 12;
27718 	com->uscsi_cdb	   = cdb;
27719 	com->uscsi_cdblen  = CDB_GROUP1;
27720 	com->uscsi_bufaddr = buffer;
27721 	com->uscsi_buflen  = 0x0C;
27722 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27723 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27724 	    SD_PATH_STANDARD);
27725 	if (rval != 0) {
27726 		kmem_free(buffer, 12);
27727 		kmem_free(com, sizeof (*com));
27728 		return (rval);
27729 	}
27730 
27731 	/* Process the toc entry */
27732 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27733 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27734 	if (entry->cdte_format & CDROM_LBA) {
27735 		entry->cdte_addr.lba =
27736 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27737 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27738 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27739 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27740 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27741 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27742 		/*
27743 		 * Send a READ TOC command using the LBA address format to get
27744 		 * the LBA for the track requested so it can be used in the
27745 		 * READ HEADER request
27746 		 *
27747 		 * Note: The MSF bit of the READ HEADER command specifies the
27748 		 * output format. The block address specified in that command
27749 		 * must be in LBA format.
27750 		 */
27751 		cdb[1] = 0;
27752 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27753 		    SD_PATH_STANDARD);
27754 		if (rval != 0) {
27755 			kmem_free(buffer, 12);
27756 			kmem_free(com, sizeof (*com));
27757 			return (rval);
27758 		}
27759 	} else {
27760 		entry->cdte_addr.msf.minute	= buffer[9];
27761 		entry->cdte_addr.msf.second	= buffer[10];
27762 		entry->cdte_addr.msf.frame	= buffer[11];
27763 		/*
27764 		 * Send a READ TOC command using the LBA address format to get
27765 		 * the LBA for the track requested so it can be used in the
27766 		 * READ HEADER request
27767 		 *
27768 		 * Note: The MSF bit of the READ HEADER command specifies the
27769 		 * output format. The block address specified in that command
27770 		 * must be in LBA format.
27771 		 */
27772 		cdb[1] = 0;
27773 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27774 		    SD_PATH_STANDARD);
27775 		if (rval != 0) {
27776 			kmem_free(buffer, 12);
27777 			kmem_free(com, sizeof (*com));
27778 			return (rval);
27779 		}
27780 	}
27781 
27782 	/*
27783 	 * Build and send the READ HEADER command to determine the data mode of
27784 	 * the user specified track.
27785 	 */
27786 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27787 	    (entry->cdte_track != CDROM_LEADOUT)) {
27788 		bzero(cdb, CDB_GROUP1);
27789 		cdb[0] = SCMD_READ_HEADER;
27790 		cdb[2] = buffer[8];
27791 		cdb[3] = buffer[9];
27792 		cdb[4] = buffer[10];
27793 		cdb[5] = buffer[11];
27794 		cdb[8] = 0x08;
27795 		com->uscsi_buflen = 0x08;
27796 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27797 		    SD_PATH_STANDARD);
27798 		if (rval == 0) {
27799 			entry->cdte_datamode = buffer[0];
27800 		} else {
27801 			/*
27802 			 * READ HEADER command failed, since this is
27803 			 * obsoleted in one spec, its better to return
27804 			 * -1 for an invlid track so that we can still
27805 			 * receive the rest of the TOC data.
27806 			 */
27807 			entry->cdte_datamode = (uchar_t)-1;
27808 		}
27809 	} else {
27810 		entry->cdte_datamode = (uchar_t)-1;
27811 	}
27812 
27813 	kmem_free(buffer, 12);
27814 	kmem_free(com, sizeof (*com));
27815 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27816 		return (EFAULT);
27817 
27818 	return (rval);
27819 }
27820 
27821 
27822 /*
27823  *    Function: sr_read_tochdr()
27824  *
27825  * Description: This routine is the driver entry point for handling CD-ROM
27826  * 		ioctl requests to read the Table of Contents (TOC) header
27827  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
27828  *		and ending track numbers
27829  *
27830  *   Arguments: dev	- the device 'dev_t'
27831  *		data	- pointer to user provided toc header structure,
27832  *			  specifying the starting and ending track numbers.
27833  *		flag	- this argument is a pass through to ddi_copyxxx()
27834  *			  directly from the mode argument of ioctl().
27835  *
27836  * Return Code: the code returned by sd_send_scsi_cmd()
27837  *		EFAULT if ddi_copyxxx() fails
27838  *		ENXIO if fail ddi_get_soft_state
27839  *		EINVAL if data pointer is NULL
27840  */
27841 
27842 static int
27843 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27844 {
27845 	struct sd_lun		*un;
27846 	struct uscsi_cmd	*com;
27847 	struct cdrom_tochdr	toc_header;
27848 	struct cdrom_tochdr	*hdr = &toc_header;
27849 	char			cdb[CDB_GROUP1];
27850 	int			rval;
27851 	caddr_t			buffer;
27852 
27853 	if (data == NULL) {
27854 		return (EINVAL);
27855 	}
27856 
27857 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27858 	    (un->un_state == SD_STATE_OFFLINE)) {
27859 		return (ENXIO);
27860 	}
27861 
27862 	buffer = kmem_zalloc(4, KM_SLEEP);
27863 	bzero(cdb, CDB_GROUP1);
27864 	cdb[0] = SCMD_READ_TOC;
27865 	/*
27866 	 * Specifying a track number of 0x00 in the READ TOC command indicates
27867 	 * that the TOC header should be returned
27868 	 */
27869 	cdb[6] = 0x00;
27870 	/*
27871 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27872 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27873 	 */
27874 	cdb[8] = 0x04;
27875 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27876 	com->uscsi_cdb	   = cdb;
27877 	com->uscsi_cdblen  = CDB_GROUP1;
27878 	com->uscsi_bufaddr = buffer;
27879 	com->uscsi_buflen  = 0x04;
27880 	com->uscsi_timeout = 300;
27881 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27882 
27883 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27884 	    SD_PATH_STANDARD);
27885 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27886 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
27887 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
27888 	} else {
27889 		hdr->cdth_trk0 = buffer[2];
27890 		hdr->cdth_trk1 = buffer[3];
27891 	}
27892 	kmem_free(buffer, 4);
27893 	kmem_free(com, sizeof (*com));
27894 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
27895 		return (EFAULT);
27896 	}
27897 	return (rval);
27898 }
27899 
27900 
27901 /*
27902  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
27903  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
27904  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
27905  * digital audio and extended architecture digital audio. These modes are
27906  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
27907  * MMC specs.
27908  *
27909  * In addition to support for the various data formats these routines also
27910  * include support for devices that implement only the direct access READ
27911  * commands (0x08, 0x28), devices that implement the READ_CD commands
27912  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
27913  * READ CDXA commands (0xD8, 0xDB)
27914  */
27915 
27916 /*
27917  *    Function: sr_read_mode1()
27918  *
27919  * Description: This routine is the driver entry point for handling CD-ROM
27920  *		ioctl read mode1 requests (CDROMREADMODE1).
27921  *
27922  *   Arguments: dev	- the device 'dev_t'
27923  *		data	- pointer to user provided cd read structure specifying
27924  *			  the lba buffer address and length.
27925  *		flag	- this argument is a pass through to ddi_copyxxx()
27926  *			  directly from the mode argument of ioctl().
27927  *
27928  * Return Code: the code returned by sd_send_scsi_cmd()
27929  *		EFAULT if ddi_copyxxx() fails
27930  *		ENXIO if fail ddi_get_soft_state
27931  *		EINVAL if data pointer is NULL
27932  */
27933 
27934 static int
27935 sr_read_mode1(dev_t dev, caddr_t data, int flag)
27936 {
27937 	struct sd_lun		*un;
27938 	struct cdrom_read	mode1_struct;
27939 	struct cdrom_read	*mode1 = &mode1_struct;
27940 	int			rval;
27941 	sd_ssc_t		*ssc;
27942 
27943 #ifdef _MULTI_DATAMODEL
27944 	/* To support ILP32 applications in an LP64 world */
27945 	struct cdrom_read32	cdrom_read32;
27946 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
27947 #endif /* _MULTI_DATAMODEL */
27948 
27949 	if (data == NULL) {
27950 		return (EINVAL);
27951 	}
27952 
27953 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27954 	    (un->un_state == SD_STATE_OFFLINE)) {
27955 		return (ENXIO);
27956 	}
27957 
27958 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27959 	    "sd_read_mode1: entry: un:0x%p\n", un);
27960 
27961 #ifdef _MULTI_DATAMODEL
27962 	switch (ddi_model_convert_from(flag & FMODELS)) {
27963 	case DDI_MODEL_ILP32:
27964 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27965 			return (EFAULT);
27966 		}
27967 		/* Convert the ILP32 uscsi data from the application to LP64 */
27968 		cdrom_read32tocdrom_read(cdrd32, mode1);
27969 		break;
27970 	case DDI_MODEL_NONE:
27971 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27972 			return (EFAULT);
27973 		}
27974 	}
27975 #else /* ! _MULTI_DATAMODEL */
27976 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27977 		return (EFAULT);
27978 	}
27979 #endif /* _MULTI_DATAMODEL */
27980 
27981 	ssc = sd_ssc_init(un);
27982 	rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
27983 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
27984 	sd_ssc_fini(ssc);
27985 
27986 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27987 	    "sd_read_mode1: exit: un:0x%p\n", un);
27988 
27989 	return (rval);
27990 }
27991 
27992 
27993 /*
27994  *    Function: sr_read_cd_mode2()
27995  *
27996  * Description: This routine is the driver entry point for handling CD-ROM
27997  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
27998  *		support the READ CD (0xBE) command or the 1st generation
27999  *		READ CD (0xD4) command.
28000  *
28001  *   Arguments: dev	- the device 'dev_t'
28002  *		data	- pointer to user provided cd read structure specifying
28003  *			  the lba buffer address and length.
28004  *		flag	- this argument is a pass through to ddi_copyxxx()
28005  *			  directly from the mode argument of ioctl().
28006  *
28007  * Return Code: the code returned by sd_send_scsi_cmd()
28008  *		EFAULT if ddi_copyxxx() fails
28009  *		ENXIO if fail ddi_get_soft_state
28010  *		EINVAL if data pointer is NULL
28011  */
28012 
28013 static int
28014 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28015 {
28016 	struct sd_lun		*un;
28017 	struct uscsi_cmd	*com;
28018 	struct cdrom_read	mode2_struct;
28019 	struct cdrom_read	*mode2 = &mode2_struct;
28020 	uchar_t			cdb[CDB_GROUP5];
28021 	int			nblocks;
28022 	int			rval;
28023 #ifdef _MULTI_DATAMODEL
28024 	/*  To support ILP32 applications in an LP64 world */
28025 	struct cdrom_read32	cdrom_read32;
28026 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28027 #endif /* _MULTI_DATAMODEL */
28028 
28029 	if (data == NULL) {
28030 		return (EINVAL);
28031 	}
28032 
28033 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28034 	    (un->un_state == SD_STATE_OFFLINE)) {
28035 		return (ENXIO);
28036 	}
28037 
28038 #ifdef _MULTI_DATAMODEL
28039 	switch (ddi_model_convert_from(flag & FMODELS)) {
28040 	case DDI_MODEL_ILP32:
28041 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28042 			return (EFAULT);
28043 		}
28044 		/* Convert the ILP32 uscsi data from the application to LP64 */
28045 		cdrom_read32tocdrom_read(cdrd32, mode2);
28046 		break;
28047 	case DDI_MODEL_NONE:
28048 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28049 			return (EFAULT);
28050 		}
28051 		break;
28052 	}
28053 
28054 #else /* ! _MULTI_DATAMODEL */
28055 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28056 		return (EFAULT);
28057 	}
28058 #endif /* _MULTI_DATAMODEL */
28059 
28060 	bzero(cdb, sizeof (cdb));
28061 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28062 		/* Read command supported by 1st generation atapi drives */
28063 		cdb[0] = SCMD_READ_CDD4;
28064 	} else {
28065 		/* Universal CD Access Command */
28066 		cdb[0] = SCMD_READ_CD;
28067 	}
28068 
28069 	/*
28070 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28071 	 */
28072 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28073 
28074 	/* set the start address */
28075 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28076 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28077 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28078 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28079 
28080 	/* set the transfer length */
28081 	nblocks = mode2->cdread_buflen / 2336;
28082 	cdb[6] = (uchar_t)(nblocks >> 16);
28083 	cdb[7] = (uchar_t)(nblocks >> 8);
28084 	cdb[8] = (uchar_t)nblocks;
28085 
28086 	/* set the filter bits */
28087 	cdb[9] = CDROM_READ_CD_USERDATA;
28088 
28089 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28090 	com->uscsi_cdb = (caddr_t)cdb;
28091 	com->uscsi_cdblen = sizeof (cdb);
28092 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28093 	com->uscsi_buflen = mode2->cdread_buflen;
28094 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28095 
28096 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28097 	    SD_PATH_STANDARD);
28098 	kmem_free(com, sizeof (*com));
28099 	return (rval);
28100 }
28101 
28102 
28103 /*
28104  *    Function: sr_read_mode2()
28105  *
28106  * Description: This routine is the driver entry point for handling CD-ROM
28107  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28108  *		do not support the READ CD (0xBE) command.
28109  *
28110  *   Arguments: dev	- the device 'dev_t'
28111  *		data	- pointer to user provided cd read structure specifying
28112  *			  the lba buffer address and length.
28113  *		flag	- this argument is a pass through to ddi_copyxxx()
28114  *			  directly from the mode argument of ioctl().
28115  *
28116  * Return Code: the code returned by sd_send_scsi_cmd()
28117  *		EFAULT if ddi_copyxxx() fails
28118  *		ENXIO if fail ddi_get_soft_state
28119  *		EINVAL if data pointer is NULL
28120  *		EIO if fail to reset block size
28121  *		EAGAIN if commands are in progress in the driver
28122  */
28123 
28124 static int
28125 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28126 {
28127 	struct sd_lun		*un;
28128 	struct cdrom_read	mode2_struct;
28129 	struct cdrom_read	*mode2 = &mode2_struct;
28130 	int			rval;
28131 	uint32_t		restore_blksize;
28132 	struct uscsi_cmd	*com;
28133 	uchar_t			cdb[CDB_GROUP0];
28134 	int			nblocks;
28135 
28136 #ifdef _MULTI_DATAMODEL
28137 	/* To support ILP32 applications in an LP64 world */
28138 	struct cdrom_read32	cdrom_read32;
28139 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28140 #endif /* _MULTI_DATAMODEL */
28141 
28142 	if (data == NULL) {
28143 		return (EINVAL);
28144 	}
28145 
28146 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28147 	    (un->un_state == SD_STATE_OFFLINE)) {
28148 		return (ENXIO);
28149 	}
28150 
28151 	/*
28152 	 * Because this routine will update the device and driver block size
28153 	 * being used we want to make sure there are no commands in progress.
28154 	 * If commands are in progress the user will have to try again.
28155 	 *
28156 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28157 	 * in sdioctl to protect commands from sdioctl through to the top of
28158 	 * sd_uscsi_strategy. See sdioctl for details.
28159 	 */
28160 	mutex_enter(SD_MUTEX(un));
28161 	if (un->un_ncmds_in_driver != 1) {
28162 		mutex_exit(SD_MUTEX(un));
28163 		return (EAGAIN);
28164 	}
28165 	mutex_exit(SD_MUTEX(un));
28166 
28167 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28168 	    "sd_read_mode2: entry: un:0x%p\n", un);
28169 
28170 #ifdef _MULTI_DATAMODEL
28171 	switch (ddi_model_convert_from(flag & FMODELS)) {
28172 	case DDI_MODEL_ILP32:
28173 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28174 			return (EFAULT);
28175 		}
28176 		/* Convert the ILP32 uscsi data from the application to LP64 */
28177 		cdrom_read32tocdrom_read(cdrd32, mode2);
28178 		break;
28179 	case DDI_MODEL_NONE:
28180 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28181 			return (EFAULT);
28182 		}
28183 		break;
28184 	}
28185 #else /* ! _MULTI_DATAMODEL */
28186 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28187 		return (EFAULT);
28188 	}
28189 #endif /* _MULTI_DATAMODEL */
28190 
28191 	/* Store the current target block size for restoration later */
28192 	restore_blksize = un->un_tgt_blocksize;
28193 
28194 	/* Change the device and soft state target block size to 2336 */
28195 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28196 		rval = EIO;
28197 		goto done;
28198 	}
28199 
28200 
28201 	bzero(cdb, sizeof (cdb));
28202 
28203 	/* set READ operation */
28204 	cdb[0] = SCMD_READ;
28205 
28206 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28207 	mode2->cdread_lba >>= 2;
28208 
28209 	/* set the start address */
28210 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28211 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28212 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28213 
28214 	/* set the transfer length */
28215 	nblocks = mode2->cdread_buflen / 2336;
28216 	cdb[4] = (uchar_t)nblocks & 0xFF;
28217 
28218 	/* build command */
28219 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28220 	com->uscsi_cdb = (caddr_t)cdb;
28221 	com->uscsi_cdblen = sizeof (cdb);
28222 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28223 	com->uscsi_buflen = mode2->cdread_buflen;
28224 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28225 
28226 	/*
28227 	 * Issue SCSI command with user space address for read buffer.
28228 	 *
28229 	 * This sends the command through main channel in the driver.
28230 	 *
28231 	 * Since this is accessed via an IOCTL call, we go through the
28232 	 * standard path, so that if the device was powered down, then
28233 	 * it would be 'awakened' to handle the command.
28234 	 */
28235 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28236 	    SD_PATH_STANDARD);
28237 
28238 	kmem_free(com, sizeof (*com));
28239 
28240 	/* Restore the device and soft state target block size */
28241 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28242 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28243 		    "can't do switch back to mode 1\n");
28244 		/*
28245 		 * If sd_send_scsi_READ succeeded we still need to report
28246 		 * an error because we failed to reset the block size
28247 		 */
28248 		if (rval == 0) {
28249 			rval = EIO;
28250 		}
28251 	}
28252 
28253 done:
28254 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28255 	    "sd_read_mode2: exit: un:0x%p\n", un);
28256 
28257 	return (rval);
28258 }
28259 
28260 
28261 /*
28262  *    Function: sr_sector_mode()
28263  *
28264  * Description: This utility function is used by sr_read_mode2 to set the target
28265  *		block size based on the user specified size. This is a legacy
28266  *		implementation based upon a vendor specific mode page
28267  *
28268  *   Arguments: dev	- the device 'dev_t'
28269  *		data	- flag indicating if block size is being set to 2336 or
28270  *			  512.
28271  *
28272  * Return Code: the code returned by sd_send_scsi_cmd()
28273  *		EFAULT if ddi_copyxxx() fails
28274  *		ENXIO if fail ddi_get_soft_state
28275  *		EINVAL if data pointer is NULL
28276  */
28277 
28278 static int
28279 sr_sector_mode(dev_t dev, uint32_t blksize)
28280 {
28281 	struct sd_lun	*un;
28282 	uchar_t		*sense;
28283 	uchar_t		*select;
28284 	int		rval;
28285 	sd_ssc_t	*ssc;
28286 
28287 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28288 	    (un->un_state == SD_STATE_OFFLINE)) {
28289 		return (ENXIO);
28290 	}
28291 
28292 	sense = kmem_zalloc(20, KM_SLEEP);
28293 
28294 	/* Note: This is a vendor specific mode page (0x81) */
28295 	ssc = sd_ssc_init(un);
28296 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28297 	    SD_PATH_STANDARD);
28298 	sd_ssc_fini(ssc);
28299 	if (rval != 0) {
28300 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28301 		    "sr_sector_mode: Mode Sense failed\n");
28302 		kmem_free(sense, 20);
28303 		return (rval);
28304 	}
28305 	select = kmem_zalloc(20, KM_SLEEP);
28306 	select[3] = 0x08;
28307 	select[10] = ((blksize >> 8) & 0xff);
28308 	select[11] = (blksize & 0xff);
28309 	select[12] = 0x01;
28310 	select[13] = 0x06;
28311 	select[14] = sense[14];
28312 	select[15] = sense[15];
28313 	if (blksize == SD_MODE2_BLKSIZE) {
28314 		select[14] |= 0x01;
28315 	}
28316 
28317 	ssc = sd_ssc_init(un);
28318 	rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28319 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28320 	sd_ssc_fini(ssc);
28321 	if (rval != 0) {
28322 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28323 		    "sr_sector_mode: Mode Select failed\n");
28324 	} else {
28325 		/*
28326 		 * Only update the softstate block size if we successfully
28327 		 * changed the device block mode.
28328 		 */
28329 		mutex_enter(SD_MUTEX(un));
28330 		sd_update_block_info(un, blksize, 0);
28331 		mutex_exit(SD_MUTEX(un));
28332 	}
28333 	kmem_free(sense, 20);
28334 	kmem_free(select, 20);
28335 	return (rval);
28336 }
28337 
28338 
28339 /*
28340  *    Function: sr_read_cdda()
28341  *
28342  * Description: This routine is the driver entry point for handling CD-ROM
28343  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28344  *		the target supports CDDA these requests are handled via a vendor
28345  *		specific command (0xD8) If the target does not support CDDA
28346  *		these requests are handled via the READ CD command (0xBE).
28347  *
28348  *   Arguments: dev	- the device 'dev_t'
28349  *		data	- pointer to user provided CD-DA structure specifying
28350  *			  the track starting address, transfer length, and
28351  *			  subcode options.
28352  *		flag	- this argument is a pass through to ddi_copyxxx()
28353  *			  directly from the mode argument of ioctl().
28354  *
28355  * Return Code: the code returned by sd_send_scsi_cmd()
28356  *		EFAULT if ddi_copyxxx() fails
28357  *		ENXIO if fail ddi_get_soft_state
28358  *		EINVAL if invalid arguments are provided
28359  *		ENOTTY
28360  */
28361 
28362 static int
28363 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28364 {
28365 	struct sd_lun			*un;
28366 	struct uscsi_cmd		*com;
28367 	struct cdrom_cdda		*cdda;
28368 	int				rval;
28369 	size_t				buflen;
28370 	char				cdb[CDB_GROUP5];
28371 
28372 #ifdef _MULTI_DATAMODEL
28373 	/* To support ILP32 applications in an LP64 world */
28374 	struct cdrom_cdda32	cdrom_cdda32;
28375 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28376 #endif /* _MULTI_DATAMODEL */
28377 
28378 	if (data == NULL) {
28379 		return (EINVAL);
28380 	}
28381 
28382 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28383 		return (ENXIO);
28384 	}
28385 
28386 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28387 
28388 #ifdef _MULTI_DATAMODEL
28389 	switch (ddi_model_convert_from(flag & FMODELS)) {
28390 	case DDI_MODEL_ILP32:
28391 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28392 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28393 			    "sr_read_cdda: ddi_copyin Failed\n");
28394 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28395 			return (EFAULT);
28396 		}
28397 		/* Convert the ILP32 uscsi data from the application to LP64 */
28398 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28399 		break;
28400 	case DDI_MODEL_NONE:
28401 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28402 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28403 			    "sr_read_cdda: ddi_copyin Failed\n");
28404 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28405 			return (EFAULT);
28406 		}
28407 		break;
28408 	}
28409 #else /* ! _MULTI_DATAMODEL */
28410 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28411 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28412 		    "sr_read_cdda: ddi_copyin Failed\n");
28413 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28414 		return (EFAULT);
28415 	}
28416 #endif /* _MULTI_DATAMODEL */
28417 
28418 	/*
28419 	 * Since MMC-2 expects max 3 bytes for length, check if the
28420 	 * length input is greater than 3 bytes
28421 	 */
28422 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28423 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28424 		    "cdrom transfer length too large: %d (limit %d)\n",
28425 		    cdda->cdda_length, 0xFFFFFF);
28426 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28427 		return (EINVAL);
28428 	}
28429 
28430 	switch (cdda->cdda_subcode) {
28431 	case CDROM_DA_NO_SUBCODE:
28432 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28433 		break;
28434 	case CDROM_DA_SUBQ:
28435 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28436 		break;
28437 	case CDROM_DA_ALL_SUBCODE:
28438 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28439 		break;
28440 	case CDROM_DA_SUBCODE_ONLY:
28441 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28442 		break;
28443 	default:
28444 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28445 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28446 		    cdda->cdda_subcode);
28447 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28448 		return (EINVAL);
28449 	}
28450 
28451 	/* Build and send the command */
28452 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28453 	bzero(cdb, CDB_GROUP5);
28454 
28455 	if (un->un_f_cfg_cdda == TRUE) {
28456 		cdb[0] = (char)SCMD_READ_CD;
28457 		cdb[1] = 0x04;
28458 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28459 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28460 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28461 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28462 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28463 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28464 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28465 		cdb[9] = 0x10;
28466 		switch (cdda->cdda_subcode) {
28467 		case CDROM_DA_NO_SUBCODE :
28468 			cdb[10] = 0x0;
28469 			break;
28470 		case CDROM_DA_SUBQ :
28471 			cdb[10] = 0x2;
28472 			break;
28473 		case CDROM_DA_ALL_SUBCODE :
28474 			cdb[10] = 0x1;
28475 			break;
28476 		case CDROM_DA_SUBCODE_ONLY :
28477 			/* FALLTHROUGH */
28478 		default :
28479 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28480 			kmem_free(com, sizeof (*com));
28481 			return (ENOTTY);
28482 		}
28483 	} else {
28484 		cdb[0] = (char)SCMD_READ_CDDA;
28485 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28486 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28487 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28488 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28489 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28490 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28491 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28492 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28493 		cdb[10] = cdda->cdda_subcode;
28494 	}
28495 
28496 	com->uscsi_cdb = cdb;
28497 	com->uscsi_cdblen = CDB_GROUP5;
28498 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28499 	com->uscsi_buflen = buflen;
28500 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28501 
28502 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28503 	    SD_PATH_STANDARD);
28504 
28505 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28506 	kmem_free(com, sizeof (*com));
28507 	return (rval);
28508 }
28509 
28510 
28511 /*
28512  *    Function: sr_read_cdxa()
28513  *
28514  * Description: This routine is the driver entry point for handling CD-ROM
28515  *		ioctl requests to return CD-XA (Extended Architecture) data.
28516  *		(CDROMCDXA).
28517  *
28518  *   Arguments: dev	- the device 'dev_t'
28519  *		data	- pointer to user provided CD-XA structure specifying
28520  *			  the data starting address, transfer length, and format
28521  *		flag	- this argument is a pass through to ddi_copyxxx()
28522  *			  directly from the mode argument of ioctl().
28523  *
28524  * Return Code: the code returned by sd_send_scsi_cmd()
28525  *		EFAULT if ddi_copyxxx() fails
28526  *		ENXIO if fail ddi_get_soft_state
28527  *		EINVAL if data pointer is NULL
28528  */
28529 
28530 static int
28531 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28532 {
28533 	struct sd_lun		*un;
28534 	struct uscsi_cmd	*com;
28535 	struct cdrom_cdxa	*cdxa;
28536 	int			rval;
28537 	size_t			buflen;
28538 	char			cdb[CDB_GROUP5];
28539 	uchar_t			read_flags;
28540 
28541 #ifdef _MULTI_DATAMODEL
28542 	/* To support ILP32 applications in an LP64 world */
28543 	struct cdrom_cdxa32		cdrom_cdxa32;
28544 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28545 #endif /* _MULTI_DATAMODEL */
28546 
28547 	if (data == NULL) {
28548 		return (EINVAL);
28549 	}
28550 
28551 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28552 		return (ENXIO);
28553 	}
28554 
28555 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28556 
28557 #ifdef _MULTI_DATAMODEL
28558 	switch (ddi_model_convert_from(flag & FMODELS)) {
28559 	case DDI_MODEL_ILP32:
28560 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28561 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28562 			return (EFAULT);
28563 		}
28564 		/*
28565 		 * Convert the ILP32 uscsi data from the
28566 		 * application to LP64 for internal use.
28567 		 */
28568 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28569 		break;
28570 	case DDI_MODEL_NONE:
28571 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28572 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28573 			return (EFAULT);
28574 		}
28575 		break;
28576 	}
28577 #else /* ! _MULTI_DATAMODEL */
28578 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28579 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28580 		return (EFAULT);
28581 	}
28582 #endif /* _MULTI_DATAMODEL */
28583 
28584 	/*
28585 	 * Since MMC-2 expects max 3 bytes for length, check if the
28586 	 * length input is greater than 3 bytes
28587 	 */
28588 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28589 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28590 		    "cdrom transfer length too large: %d (limit %d)\n",
28591 		    cdxa->cdxa_length, 0xFFFFFF);
28592 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28593 		return (EINVAL);
28594 	}
28595 
28596 	switch (cdxa->cdxa_format) {
28597 	case CDROM_XA_DATA:
28598 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28599 		read_flags = 0x10;
28600 		break;
28601 	case CDROM_XA_SECTOR_DATA:
28602 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28603 		read_flags = 0xf8;
28604 		break;
28605 	case CDROM_XA_DATA_W_ERROR:
28606 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28607 		read_flags = 0xfc;
28608 		break;
28609 	default:
28610 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28611 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28612 		    cdxa->cdxa_format);
28613 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28614 		return (EINVAL);
28615 	}
28616 
28617 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28618 	bzero(cdb, CDB_GROUP5);
28619 	if (un->un_f_mmc_cap == TRUE) {
28620 		cdb[0] = (char)SCMD_READ_CD;
28621 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28622 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28623 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28624 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28625 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28626 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28627 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28628 		cdb[9] = (char)read_flags;
28629 	} else {
28630 		/*
28631 		 * Note: A vendor specific command (0xDB) is being used her to
28632 		 * request a read of all subcodes.
28633 		 */
28634 		cdb[0] = (char)SCMD_READ_CDXA;
28635 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28636 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28637 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28638 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28639 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28640 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28641 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28642 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28643 		cdb[10] = cdxa->cdxa_format;
28644 	}
28645 	com->uscsi_cdb	   = cdb;
28646 	com->uscsi_cdblen  = CDB_GROUP5;
28647 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28648 	com->uscsi_buflen  = buflen;
28649 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28650 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28651 	    SD_PATH_STANDARD);
28652 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28653 	kmem_free(com, sizeof (*com));
28654 	return (rval);
28655 }
28656 
28657 
28658 /*
28659  *    Function: sr_eject()
28660  *
28661  * Description: This routine is the driver entry point for handling CD-ROM
28662  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28663  *
28664  *   Arguments: dev	- the device 'dev_t'
28665  *
28666  * Return Code: the code returned by sd_send_scsi_cmd()
28667  */
28668 
28669 static int
28670 sr_eject(dev_t dev)
28671 {
28672 	struct sd_lun	*un;
28673 	int		rval;
28674 	sd_ssc_t	*ssc;
28675 
28676 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28677 	    (un->un_state == SD_STATE_OFFLINE)) {
28678 		return (ENXIO);
28679 	}
28680 
28681 	/*
28682 	 * To prevent race conditions with the eject
28683 	 * command, keep track of an eject command as
28684 	 * it progresses. If we are already handling
28685 	 * an eject command in the driver for the given
28686 	 * unit and another request to eject is received
28687 	 * immediately return EAGAIN so we don't lose
28688 	 * the command if the current eject command fails.
28689 	 */
28690 	mutex_enter(SD_MUTEX(un));
28691 	if (un->un_f_ejecting == TRUE) {
28692 		mutex_exit(SD_MUTEX(un));
28693 		return (EAGAIN);
28694 	}
28695 	un->un_f_ejecting = TRUE;
28696 	mutex_exit(SD_MUTEX(un));
28697 
28698 	ssc = sd_ssc_init(un);
28699 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28700 	    SD_PATH_STANDARD);
28701 	sd_ssc_fini(ssc);
28702 
28703 	if (rval != 0) {
28704 		mutex_enter(SD_MUTEX(un));
28705 		un->un_f_ejecting = FALSE;
28706 		mutex_exit(SD_MUTEX(un));
28707 		return (rval);
28708 	}
28709 
28710 	ssc = sd_ssc_init(un);
28711 	rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28712 	    SD_TARGET_EJECT, SD_PATH_STANDARD);
28713 	sd_ssc_fini(ssc);
28714 
28715 	if (rval == 0) {
28716 		mutex_enter(SD_MUTEX(un));
28717 		sr_ejected(un);
28718 		un->un_mediastate = DKIO_EJECTED;
28719 		un->un_f_ejecting = FALSE;
28720 		cv_broadcast(&un->un_state_cv);
28721 		mutex_exit(SD_MUTEX(un));
28722 	} else {
28723 		mutex_enter(SD_MUTEX(un));
28724 		un->un_f_ejecting = FALSE;
28725 		mutex_exit(SD_MUTEX(un));
28726 	}
28727 	return (rval);
28728 }
28729 
28730 
28731 /*
28732  *    Function: sr_ejected()
28733  *
28734  * Description: This routine updates the soft state structure to invalidate the
28735  *		geometry information after the media has been ejected or a
28736  *		media eject has been detected.
28737  *
28738  *   Arguments: un - driver soft state (unit) structure
28739  */
28740 
28741 static void
28742 sr_ejected(struct sd_lun *un)
28743 {
28744 	struct sd_errstats *stp;
28745 
28746 	ASSERT(un != NULL);
28747 	ASSERT(mutex_owned(SD_MUTEX(un)));
28748 
28749 	un->un_f_blockcount_is_valid	= FALSE;
28750 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28751 	mutex_exit(SD_MUTEX(un));
28752 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28753 	mutex_enter(SD_MUTEX(un));
28754 
28755 	if (un->un_errstats != NULL) {
28756 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28757 		stp->sd_capacity.value.ui64 = 0;
28758 	}
28759 }
28760 
28761 
28762 /*
28763  *    Function: sr_check_wp()
28764  *
28765  * Description: This routine checks the write protection of a removable
28766  *      media disk and hotpluggable devices via the write protect bit of
28767  *      the Mode Page Header device specific field. Some devices choke
28768  *      on unsupported mode page. In order to workaround this issue,
28769  *      this routine has been implemented to use 0x3f mode page(request
28770  *      for all pages) for all device types.
28771  *
28772  *   Arguments: dev             - the device 'dev_t'
28773  *
28774  * Return Code: int indicating if the device is write protected (1) or not (0)
28775  *
28776  *     Context: Kernel thread.
28777  *
28778  */
28779 
28780 static int
28781 sr_check_wp(dev_t dev)
28782 {
28783 	struct sd_lun	*un;
28784 	uchar_t		device_specific;
28785 	uchar_t		*sense;
28786 	int		hdrlen;
28787 	int		rval = FALSE;
28788 	int		status;
28789 	sd_ssc_t	*ssc;
28790 
28791 	/*
28792 	 * Note: The return codes for this routine should be reworked to
28793 	 * properly handle the case of a NULL softstate.
28794 	 */
28795 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28796 		return (FALSE);
28797 	}
28798 
28799 	if (un->un_f_cfg_is_atapi == TRUE) {
28800 		/*
28801 		 * The mode page contents are not required; set the allocation
28802 		 * length for the mode page header only
28803 		 */
28804 		hdrlen = MODE_HEADER_LENGTH_GRP2;
28805 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28806 		ssc = sd_ssc_init(un);
28807 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28808 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28809 		sd_ssc_fini(ssc);
28810 		if (status != 0)
28811 			goto err_exit;
28812 		device_specific =
28813 		    ((struct mode_header_grp2 *)sense)->device_specific;
28814 	} else {
28815 		hdrlen = MODE_HEADER_LENGTH;
28816 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28817 		ssc = sd_ssc_init(un);
28818 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28819 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28820 		sd_ssc_fini(ssc);
28821 		if (status != 0)
28822 			goto err_exit;
28823 		device_specific =
28824 		    ((struct mode_header *)sense)->device_specific;
28825 	}
28826 
28827 
28828 	/*
28829 	 * Write protect mode sense failed; not all disks
28830 	 * understand this query. Return FALSE assuming that
28831 	 * these devices are not writable.
28832 	 */
28833 	if (device_specific & WRITE_PROTECT) {
28834 		rval = TRUE;
28835 	}
28836 
28837 err_exit:
28838 	kmem_free(sense, hdrlen);
28839 	return (rval);
28840 }
28841 
28842 /*
28843  *    Function: sr_volume_ctrl()
28844  *
28845  * Description: This routine is the driver entry point for handling CD-ROM
28846  *		audio output volume ioctl requests. (CDROMVOLCTRL)
28847  *
28848  *   Arguments: dev	- the device 'dev_t'
28849  *		data	- pointer to user audio volume control structure
28850  *		flag	- this argument is a pass through to ddi_copyxxx()
28851  *			  directly from the mode argument of ioctl().
28852  *
28853  * Return Code: the code returned by sd_send_scsi_cmd()
28854  *		EFAULT if ddi_copyxxx() fails
28855  *		ENXIO if fail ddi_get_soft_state
28856  *		EINVAL if data pointer is NULL
28857  *
28858  */
28859 
28860 static int
28861 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28862 {
28863 	struct sd_lun		*un;
28864 	struct cdrom_volctrl    volume;
28865 	struct cdrom_volctrl    *vol = &volume;
28866 	uchar_t			*sense_page;
28867 	uchar_t			*select_page;
28868 	uchar_t			*sense;
28869 	uchar_t			*select;
28870 	int			sense_buflen;
28871 	int			select_buflen;
28872 	int			rval;
28873 	sd_ssc_t		*ssc;
28874 
28875 	if (data == NULL) {
28876 		return (EINVAL);
28877 	}
28878 
28879 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28880 	    (un->un_state == SD_STATE_OFFLINE)) {
28881 		return (ENXIO);
28882 	}
28883 
28884 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
28885 		return (EFAULT);
28886 	}
28887 
28888 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28889 		struct mode_header_grp2		*sense_mhp;
28890 		struct mode_header_grp2		*select_mhp;
28891 		int				bd_len;
28892 
28893 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
28894 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
28895 		    MODEPAGE_AUDIO_CTRL_LEN;
28896 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28897 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28898 		ssc = sd_ssc_init(un);
28899 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
28900 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28901 		    SD_PATH_STANDARD);
28902 		sd_ssc_fini(ssc);
28903 
28904 		if (rval != 0) {
28905 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28906 			    "sr_volume_ctrl: Mode Sense Failed\n");
28907 			kmem_free(sense, sense_buflen);
28908 			kmem_free(select, select_buflen);
28909 			return (rval);
28910 		}
28911 		sense_mhp = (struct mode_header_grp2 *)sense;
28912 		select_mhp = (struct mode_header_grp2 *)select;
28913 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
28914 		    sense_mhp->bdesc_length_lo;
28915 		if (bd_len > MODE_BLK_DESC_LENGTH) {
28916 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28917 			    "sr_volume_ctrl: Mode Sense returned invalid "
28918 			    "block descriptor length\n");
28919 			kmem_free(sense, sense_buflen);
28920 			kmem_free(select, select_buflen);
28921 			return (EIO);
28922 		}
28923 		sense_page = (uchar_t *)
28924 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
28925 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
28926 		select_mhp->length_msb = 0;
28927 		select_mhp->length_lsb = 0;
28928 		select_mhp->bdesc_length_hi = 0;
28929 		select_mhp->bdesc_length_lo = 0;
28930 	} else {
28931 		struct mode_header		*sense_mhp, *select_mhp;
28932 
28933 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28934 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28935 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28936 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28937 		ssc = sd_ssc_init(un);
28938 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
28939 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28940 		    SD_PATH_STANDARD);
28941 		sd_ssc_fini(ssc);
28942 
28943 		if (rval != 0) {
28944 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28945 			    "sr_volume_ctrl: Mode Sense Failed\n");
28946 			kmem_free(sense, sense_buflen);
28947 			kmem_free(select, select_buflen);
28948 			return (rval);
28949 		}
28950 		sense_mhp  = (struct mode_header *)sense;
28951 		select_mhp = (struct mode_header *)select;
28952 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
28953 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28954 			    "sr_volume_ctrl: Mode Sense returned invalid "
28955 			    "block descriptor length\n");
28956 			kmem_free(sense, sense_buflen);
28957 			kmem_free(select, select_buflen);
28958 			return (EIO);
28959 		}
28960 		sense_page = (uchar_t *)
28961 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
28962 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
28963 		select_mhp->length = 0;
28964 		select_mhp->bdesc_length = 0;
28965 	}
28966 	/*
28967 	 * Note: An audio control data structure could be created and overlayed
28968 	 * on the following in place of the array indexing method implemented.
28969 	 */
28970 
28971 	/* Build the select data for the user volume data */
28972 	select_page[0] = MODEPAGE_AUDIO_CTRL;
28973 	select_page[1] = 0xE;
28974 	/* Set the immediate bit */
28975 	select_page[2] = 0x04;
28976 	/* Zero out reserved fields */
28977 	select_page[3] = 0x00;
28978 	select_page[4] = 0x00;
28979 	/* Return sense data for fields not to be modified */
28980 	select_page[5] = sense_page[5];
28981 	select_page[6] = sense_page[6];
28982 	select_page[7] = sense_page[7];
28983 	/* Set the user specified volume levels for channel 0 and 1 */
28984 	select_page[8] = 0x01;
28985 	select_page[9] = vol->channel0;
28986 	select_page[10] = 0x02;
28987 	select_page[11] = vol->channel1;
28988 	/* Channel 2 and 3 are currently unsupported so return the sense data */
28989 	select_page[12] = sense_page[12];
28990 	select_page[13] = sense_page[13];
28991 	select_page[14] = sense_page[14];
28992 	select_page[15] = sense_page[15];
28993 
28994 	ssc = sd_ssc_init(un);
28995 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28996 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
28997 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28998 	} else {
28999 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29000 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29001 	}
29002 	sd_ssc_fini(ssc);
29003 
29004 	kmem_free(sense, sense_buflen);
29005 	kmem_free(select, select_buflen);
29006 	return (rval);
29007 }
29008 
29009 
29010 /*
29011  *    Function: sr_read_sony_session_offset()
29012  *
29013  * Description: This routine is the driver entry point for handling CD-ROM
29014  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29015  *		The address of the first track in the last session of a
29016  *		multi-session CD-ROM is returned
29017  *
29018  *		Note: This routine uses a vendor specific key value in the
29019  *		command control field without implementing any vendor check here
29020  *		or in the ioctl routine.
29021  *
29022  *   Arguments: dev	- the device 'dev_t'
29023  *		data	- pointer to an int to hold the requested address
29024  *		flag	- this argument is a pass through to ddi_copyxxx()
29025  *			  directly from the mode argument of ioctl().
29026  *
29027  * Return Code: the code returned by sd_send_scsi_cmd()
29028  *		EFAULT if ddi_copyxxx() fails
29029  *		ENXIO if fail ddi_get_soft_state
29030  *		EINVAL if data pointer is NULL
29031  */
29032 
29033 static int
29034 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29035 {
29036 	struct sd_lun		*un;
29037 	struct uscsi_cmd	*com;
29038 	caddr_t			buffer;
29039 	char			cdb[CDB_GROUP1];
29040 	int			session_offset = 0;
29041 	int			rval;
29042 
29043 	if (data == NULL) {
29044 		return (EINVAL);
29045 	}
29046 
29047 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29048 	    (un->un_state == SD_STATE_OFFLINE)) {
29049 		return (ENXIO);
29050 	}
29051 
29052 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29053 	bzero(cdb, CDB_GROUP1);
29054 	cdb[0] = SCMD_READ_TOC;
29055 	/*
29056 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29057 	 * (4 byte TOC response header + 8 byte response data)
29058 	 */
29059 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29060 	/* Byte 9 is the control byte. A vendor specific value is used */
29061 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29062 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29063 	com->uscsi_cdb = cdb;
29064 	com->uscsi_cdblen = CDB_GROUP1;
29065 	com->uscsi_bufaddr = buffer;
29066 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29067 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29068 
29069 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29070 	    SD_PATH_STANDARD);
29071 	if (rval != 0) {
29072 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29073 		kmem_free(com, sizeof (*com));
29074 		return (rval);
29075 	}
29076 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29077 		session_offset =
29078 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29079 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29080 		/*
29081 		 * Offset returned offset in current lbasize block's. Convert to
29082 		 * 2k block's to return to the user
29083 		 */
29084 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29085 			session_offset >>= 2;
29086 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29087 			session_offset >>= 1;
29088 		}
29089 	}
29090 
29091 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29092 		rval = EFAULT;
29093 	}
29094 
29095 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29096 	kmem_free(com, sizeof (*com));
29097 	return (rval);
29098 }
29099 
29100 
29101 /*
29102  *    Function: sd_wm_cache_constructor()
29103  *
29104  * Description: Cache Constructor for the wmap cache for the read/modify/write
29105  * 		devices.
29106  *
29107  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29108  *		un	- sd_lun structure for the device.
29109  *		flag	- the km flags passed to constructor
29110  *
29111  * Return Code: 0 on success.
29112  *		-1 on failure.
29113  */
29114 
29115 /*ARGSUSED*/
29116 static int
29117 sd_wm_cache_constructor(void *wm, void *un, int flags)
29118 {
29119 	bzero(wm, sizeof (struct sd_w_map));
29120 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29121 	return (0);
29122 }
29123 
29124 
29125 /*
29126  *    Function: sd_wm_cache_destructor()
29127  *
29128  * Description: Cache destructor for the wmap cache for the read/modify/write
29129  * 		devices.
29130  *
29131  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29132  *		un	- sd_lun structure for the device.
29133  */
29134 /*ARGSUSED*/
29135 static void
29136 sd_wm_cache_destructor(void *wm, void *un)
29137 {
29138 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29139 }
29140 
29141 
29142 /*
29143  *    Function: sd_range_lock()
29144  *
29145  * Description: Lock the range of blocks specified as parameter to ensure
29146  *		that read, modify write is atomic and no other i/o writes
29147  *		to the same location. The range is specified in terms
29148  *		of start and end blocks. Block numbers are the actual
29149  *		media block numbers and not system.
29150  *
29151  *   Arguments: un	- sd_lun structure for the device.
29152  *		startb - The starting block number
29153  *		endb - The end block number
29154  *		typ - type of i/o - simple/read_modify_write
29155  *
29156  * Return Code: wm  - pointer to the wmap structure.
29157  *
29158  *     Context: This routine can sleep.
29159  */
29160 
29161 static struct sd_w_map *
29162 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29163 {
29164 	struct sd_w_map *wmp = NULL;
29165 	struct sd_w_map *sl_wmp = NULL;
29166 	struct sd_w_map *tmp_wmp;
29167 	wm_state state = SD_WM_CHK_LIST;
29168 
29169 
29170 	ASSERT(un != NULL);
29171 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29172 
29173 	mutex_enter(SD_MUTEX(un));
29174 
29175 	while (state != SD_WM_DONE) {
29176 
29177 		switch (state) {
29178 		case SD_WM_CHK_LIST:
29179 			/*
29180 			 * This is the starting state. Check the wmap list
29181 			 * to see if the range is currently available.
29182 			 */
29183 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29184 				/*
29185 				 * If this is a simple write and no rmw
29186 				 * i/o is pending then try to lock the
29187 				 * range as the range should be available.
29188 				 */
29189 				state = SD_WM_LOCK_RANGE;
29190 			} else {
29191 				tmp_wmp = sd_get_range(un, startb, endb);
29192 				if (tmp_wmp != NULL) {
29193 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29194 						/*
29195 						 * Should not keep onlist wmps
29196 						 * while waiting this macro
29197 						 * will also do wmp = NULL;
29198 						 */
29199 						FREE_ONLIST_WMAP(un, wmp);
29200 					}
29201 					/*
29202 					 * sl_wmp is the wmap on which wait
29203 					 * is done, since the tmp_wmp points
29204 					 * to the inuse wmap, set sl_wmp to
29205 					 * tmp_wmp and change the state to sleep
29206 					 */
29207 					sl_wmp = tmp_wmp;
29208 					state = SD_WM_WAIT_MAP;
29209 				} else {
29210 					state = SD_WM_LOCK_RANGE;
29211 				}
29212 
29213 			}
29214 			break;
29215 
29216 		case SD_WM_LOCK_RANGE:
29217 			ASSERT(un->un_wm_cache);
29218 			/*
29219 			 * The range need to be locked, try to get a wmap.
29220 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29221 			 * if possible as we will have to release the sd mutex
29222 			 * if we have to sleep.
29223 			 */
29224 			if (wmp == NULL)
29225 				wmp = kmem_cache_alloc(un->un_wm_cache,
29226 				    KM_NOSLEEP);
29227 			if (wmp == NULL) {
29228 				mutex_exit(SD_MUTEX(un));
29229 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29230 				    (sd_lun::un_wm_cache))
29231 				wmp = kmem_cache_alloc(un->un_wm_cache,
29232 				    KM_SLEEP);
29233 				mutex_enter(SD_MUTEX(un));
29234 				/*
29235 				 * we released the mutex so recheck and go to
29236 				 * check list state.
29237 				 */
29238 				state = SD_WM_CHK_LIST;
29239 			} else {
29240 				/*
29241 				 * We exit out of state machine since we
29242 				 * have the wmap. Do the housekeeping first.
29243 				 * place the wmap on the wmap list if it is not
29244 				 * on it already and then set the state to done.
29245 				 */
29246 				wmp->wm_start = startb;
29247 				wmp->wm_end = endb;
29248 				wmp->wm_flags = typ | SD_WM_BUSY;
29249 				if (typ & SD_WTYPE_RMW) {
29250 					un->un_rmw_count++;
29251 				}
29252 				/*
29253 				 * If not already on the list then link
29254 				 */
29255 				if (!ONLIST(un, wmp)) {
29256 					wmp->wm_next = un->un_wm;
29257 					wmp->wm_prev = NULL;
29258 					if (wmp->wm_next)
29259 						wmp->wm_next->wm_prev = wmp;
29260 					un->un_wm = wmp;
29261 				}
29262 				state = SD_WM_DONE;
29263 			}
29264 			break;
29265 
29266 		case SD_WM_WAIT_MAP:
29267 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29268 			/*
29269 			 * Wait is done on sl_wmp, which is set in the
29270 			 * check_list state.
29271 			 */
29272 			sl_wmp->wm_wanted_count++;
29273 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29274 			sl_wmp->wm_wanted_count--;
29275 			/*
29276 			 * We can reuse the memory from the completed sl_wmp
29277 			 * lock range for our new lock, but only if noone is
29278 			 * waiting for it.
29279 			 */
29280 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29281 			if (sl_wmp->wm_wanted_count == 0) {
29282 				if (wmp != NULL)
29283 					CHK_N_FREEWMP(un, wmp);
29284 				wmp = sl_wmp;
29285 			}
29286 			sl_wmp = NULL;
29287 			/*
29288 			 * After waking up, need to recheck for availability of
29289 			 * range.
29290 			 */
29291 			state = SD_WM_CHK_LIST;
29292 			break;
29293 
29294 		default:
29295 			panic("sd_range_lock: "
29296 			    "Unknown state %d in sd_range_lock", state);
29297 			/*NOTREACHED*/
29298 		} /* switch(state) */
29299 
29300 	} /* while(state != SD_WM_DONE) */
29301 
29302 	mutex_exit(SD_MUTEX(un));
29303 
29304 	ASSERT(wmp != NULL);
29305 
29306 	return (wmp);
29307 }
29308 
29309 
29310 /*
29311  *    Function: sd_get_range()
29312  *
29313  * Description: Find if there any overlapping I/O to this one
29314  *		Returns the write-map of 1st such I/O, NULL otherwise.
29315  *
29316  *   Arguments: un	- sd_lun structure for the device.
29317  *		startb - The starting block number
29318  *		endb - The end block number
29319  *
29320  * Return Code: wm  - pointer to the wmap structure.
29321  */
29322 
29323 static struct sd_w_map *
29324 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29325 {
29326 	struct sd_w_map *wmp;
29327 
29328 	ASSERT(un != NULL);
29329 
29330 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29331 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29332 			continue;
29333 		}
29334 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29335 			break;
29336 		}
29337 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29338 			break;
29339 		}
29340 	}
29341 
29342 	return (wmp);
29343 }
29344 
29345 
29346 /*
29347  *    Function: sd_free_inlist_wmap()
29348  *
29349  * Description: Unlink and free a write map struct.
29350  *
29351  *   Arguments: un      - sd_lun structure for the device.
29352  *		wmp	- sd_w_map which needs to be unlinked.
29353  */
29354 
29355 static void
29356 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29357 {
29358 	ASSERT(un != NULL);
29359 
29360 	if (un->un_wm == wmp) {
29361 		un->un_wm = wmp->wm_next;
29362 	} else {
29363 		wmp->wm_prev->wm_next = wmp->wm_next;
29364 	}
29365 
29366 	if (wmp->wm_next) {
29367 		wmp->wm_next->wm_prev = wmp->wm_prev;
29368 	}
29369 
29370 	wmp->wm_next = wmp->wm_prev = NULL;
29371 
29372 	kmem_cache_free(un->un_wm_cache, wmp);
29373 }
29374 
29375 
29376 /*
29377  *    Function: sd_range_unlock()
29378  *
29379  * Description: Unlock the range locked by wm.
29380  *		Free write map if nobody else is waiting on it.
29381  *
29382  *   Arguments: un      - sd_lun structure for the device.
29383  *              wmp     - sd_w_map which needs to be unlinked.
29384  */
29385 
29386 static void
29387 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29388 {
29389 	ASSERT(un != NULL);
29390 	ASSERT(wm != NULL);
29391 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29392 
29393 	mutex_enter(SD_MUTEX(un));
29394 
29395 	if (wm->wm_flags & SD_WTYPE_RMW) {
29396 		un->un_rmw_count--;
29397 	}
29398 
29399 	if (wm->wm_wanted_count) {
29400 		wm->wm_flags = 0;
29401 		/*
29402 		 * Broadcast that the wmap is available now.
29403 		 */
29404 		cv_broadcast(&wm->wm_avail);
29405 	} else {
29406 		/*
29407 		 * If no one is waiting on the map, it should be free'ed.
29408 		 */
29409 		sd_free_inlist_wmap(un, wm);
29410 	}
29411 
29412 	mutex_exit(SD_MUTEX(un));
29413 }
29414 
29415 
29416 /*
29417  *    Function: sd_read_modify_write_task
29418  *
29419  * Description: Called from a taskq thread to initiate the write phase of
29420  *		a read-modify-write request.  This is used for targets where
29421  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29422  *
29423  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29424  *
29425  *     Context: Called under taskq thread context.
29426  */
29427 
29428 static void
29429 sd_read_modify_write_task(void *arg)
29430 {
29431 	struct sd_mapblocksize_info	*bsp;
29432 	struct buf	*bp;
29433 	struct sd_xbuf	*xp;
29434 	struct sd_lun	*un;
29435 
29436 	bp = arg;	/* The bp is given in arg */
29437 	ASSERT(bp != NULL);
29438 
29439 	/* Get the pointer to the layer-private data struct */
29440 	xp = SD_GET_XBUF(bp);
29441 	ASSERT(xp != NULL);
29442 	bsp = xp->xb_private;
29443 	ASSERT(bsp != NULL);
29444 
29445 	un = SD_GET_UN(bp);
29446 	ASSERT(un != NULL);
29447 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29448 
29449 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29450 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29451 
29452 	/*
29453 	 * This is the write phase of a read-modify-write request, called
29454 	 * under the context of a taskq thread in response to the completion
29455 	 * of the read portion of the rmw request completing under interrupt
29456 	 * context. The write request must be sent from here down the iostart
29457 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29458 	 * we use the layer index saved in the layer-private data area.
29459 	 */
29460 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29461 
29462 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29463 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29464 }
29465 
29466 
29467 /*
29468  *    Function: sddump_do_read_of_rmw()
29469  *
29470  * Description: This routine will be called from sddump, If sddump is called
29471  *		with an I/O which not aligned on device blocksize boundary
29472  *		then the write has to be converted to read-modify-write.
29473  *		Do the read part here in order to keep sddump simple.
29474  *		Note - That the sd_mutex is held across the call to this
29475  *		routine.
29476  *
29477  *   Arguments: un	- sd_lun
29478  *		blkno	- block number in terms of media block size.
29479  *		nblk	- number of blocks.
29480  *		bpp	- pointer to pointer to the buf structure. On return
29481  *			from this function, *bpp points to the valid buffer
29482  *			to which the write has to be done.
29483  *
29484  * Return Code: 0 for success or errno-type return code
29485  */
29486 
29487 static int
29488 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29489 	struct buf **bpp)
29490 {
29491 	int err;
29492 	int i;
29493 	int rval;
29494 	struct buf *bp;
29495 	struct scsi_pkt *pkt = NULL;
29496 	uint32_t target_blocksize;
29497 
29498 	ASSERT(un != NULL);
29499 	ASSERT(mutex_owned(SD_MUTEX(un)));
29500 
29501 	target_blocksize = un->un_tgt_blocksize;
29502 
29503 	mutex_exit(SD_MUTEX(un));
29504 
29505 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29506 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29507 	if (bp == NULL) {
29508 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29509 		    "no resources for dumping; giving up");
29510 		err = ENOMEM;
29511 		goto done;
29512 	}
29513 
29514 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29515 	    blkno, nblk);
29516 	if (rval != 0) {
29517 		scsi_free_consistent_buf(bp);
29518 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29519 		    "no resources for dumping; giving up");
29520 		err = ENOMEM;
29521 		goto done;
29522 	}
29523 
29524 	pkt->pkt_flags |= FLAG_NOINTR;
29525 
29526 	err = EIO;
29527 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29528 
29529 		/*
29530 		 * Scsi_poll returns 0 (success) if the command completes and
29531 		 * the status block is STATUS_GOOD.  We should only check
29532 		 * errors if this condition is not true.  Even then we should
29533 		 * send our own request sense packet only if we have a check
29534 		 * condition and auto request sense has not been performed by
29535 		 * the hba.
29536 		 */
29537 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29538 
29539 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29540 			err = 0;
29541 			break;
29542 		}
29543 
29544 		/*
29545 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29546 		 * no need to read RQS data.
29547 		 */
29548 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29549 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29550 			    "Error while dumping state with rmw..."
29551 			    "Device is gone\n");
29552 			break;
29553 		}
29554 
29555 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29556 			SD_INFO(SD_LOG_DUMP, un,
29557 			    "sddump: read failed with CHECK, try # %d\n", i);
29558 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29559 				(void) sd_send_polled_RQS(un);
29560 			}
29561 
29562 			continue;
29563 		}
29564 
29565 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29566 			int reset_retval = 0;
29567 
29568 			SD_INFO(SD_LOG_DUMP, un,
29569 			    "sddump: read failed with BUSY, try # %d\n", i);
29570 
29571 			if (un->un_f_lun_reset_enabled == TRUE) {
29572 				reset_retval = scsi_reset(SD_ADDRESS(un),
29573 				    RESET_LUN);
29574 			}
29575 			if (reset_retval == 0) {
29576 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29577 			}
29578 			(void) sd_send_polled_RQS(un);
29579 
29580 		} else {
29581 			SD_INFO(SD_LOG_DUMP, un,
29582 			    "sddump: read failed with 0x%x, try # %d\n",
29583 			    SD_GET_PKT_STATUS(pkt), i);
29584 			mutex_enter(SD_MUTEX(un));
29585 			sd_reset_target(un, pkt);
29586 			mutex_exit(SD_MUTEX(un));
29587 		}
29588 
29589 		/*
29590 		 * If we are not getting anywhere with lun/target resets,
29591 		 * let's reset the bus.
29592 		 */
29593 		if (i > SD_NDUMP_RETRIES/2) {
29594 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29595 			(void) sd_send_polled_RQS(un);
29596 		}
29597 
29598 	}
29599 	scsi_destroy_pkt(pkt);
29600 
29601 	if (err != 0) {
29602 		scsi_free_consistent_buf(bp);
29603 		*bpp = NULL;
29604 	} else {
29605 		*bpp = bp;
29606 	}
29607 
29608 done:
29609 	mutex_enter(SD_MUTEX(un));
29610 	return (err);
29611 }
29612 
29613 
29614 /*
29615  *    Function: sd_failfast_flushq
29616  *
29617  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29618  *		in b_flags and move them onto the failfast queue, then kick
29619  *		off a thread to return all bp's on the failfast queue to
29620  *		their owners with an error set.
29621  *
29622  *   Arguments: un - pointer to the soft state struct for the instance.
29623  *
29624  *     Context: may execute in interrupt context.
29625  */
29626 
29627 static void
29628 sd_failfast_flushq(struct sd_lun *un)
29629 {
29630 	struct buf *bp;
29631 	struct buf *next_waitq_bp;
29632 	struct buf *prev_waitq_bp = NULL;
29633 
29634 	ASSERT(un != NULL);
29635 	ASSERT(mutex_owned(SD_MUTEX(un)));
29636 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29637 	ASSERT(un->un_failfast_bp == NULL);
29638 
29639 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29640 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29641 
29642 	/*
29643 	 * Check if we should flush all bufs when entering failfast state, or
29644 	 * just those with B_FAILFAST set.
29645 	 */
29646 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29647 		/*
29648 		 * Move *all* bp's on the wait queue to the failfast flush
29649 		 * queue, including those that do NOT have B_FAILFAST set.
29650 		 */
29651 		if (un->un_failfast_headp == NULL) {
29652 			ASSERT(un->un_failfast_tailp == NULL);
29653 			un->un_failfast_headp = un->un_waitq_headp;
29654 		} else {
29655 			ASSERT(un->un_failfast_tailp != NULL);
29656 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29657 		}
29658 
29659 		un->un_failfast_tailp = un->un_waitq_tailp;
29660 
29661 		/* update kstat for each bp moved out of the waitq */
29662 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29663 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29664 		}
29665 
29666 		/* empty the waitq */
29667 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29668 
29669 	} else {
29670 		/*
29671 		 * Go thru the wait queue, pick off all entries with
29672 		 * B_FAILFAST set, and move these onto the failfast queue.
29673 		 */
29674 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29675 			/*
29676 			 * Save the pointer to the next bp on the wait queue,
29677 			 * so we get to it on the next iteration of this loop.
29678 			 */
29679 			next_waitq_bp = bp->av_forw;
29680 
29681 			/*
29682 			 * If this bp from the wait queue does NOT have
29683 			 * B_FAILFAST set, just move on to the next element
29684 			 * in the wait queue. Note, this is the only place
29685 			 * where it is correct to set prev_waitq_bp.
29686 			 */
29687 			if ((bp->b_flags & B_FAILFAST) == 0) {
29688 				prev_waitq_bp = bp;
29689 				continue;
29690 			}
29691 
29692 			/*
29693 			 * Remove the bp from the wait queue.
29694 			 */
29695 			if (bp == un->un_waitq_headp) {
29696 				/* The bp is the first element of the waitq. */
29697 				un->un_waitq_headp = next_waitq_bp;
29698 				if (un->un_waitq_headp == NULL) {
29699 					/* The wait queue is now empty */
29700 					un->un_waitq_tailp = NULL;
29701 				}
29702 			} else {
29703 				/*
29704 				 * The bp is either somewhere in the middle
29705 				 * or at the end of the wait queue.
29706 				 */
29707 				ASSERT(un->un_waitq_headp != NULL);
29708 				ASSERT(prev_waitq_bp != NULL);
29709 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29710 				    == 0);
29711 				if (bp == un->un_waitq_tailp) {
29712 					/* bp is the last entry on the waitq. */
29713 					ASSERT(next_waitq_bp == NULL);
29714 					un->un_waitq_tailp = prev_waitq_bp;
29715 				}
29716 				prev_waitq_bp->av_forw = next_waitq_bp;
29717 			}
29718 			bp->av_forw = NULL;
29719 
29720 			/*
29721 			 * update kstat since the bp is moved out of
29722 			 * the waitq
29723 			 */
29724 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29725 
29726 			/*
29727 			 * Now put the bp onto the failfast queue.
29728 			 */
29729 			if (un->un_failfast_headp == NULL) {
29730 				/* failfast queue is currently empty */
29731 				ASSERT(un->un_failfast_tailp == NULL);
29732 				un->un_failfast_headp =
29733 				    un->un_failfast_tailp = bp;
29734 			} else {
29735 				/* Add the bp to the end of the failfast q */
29736 				ASSERT(un->un_failfast_tailp != NULL);
29737 				ASSERT(un->un_failfast_tailp->b_flags &
29738 				    B_FAILFAST);
29739 				un->un_failfast_tailp->av_forw = bp;
29740 				un->un_failfast_tailp = bp;
29741 			}
29742 		}
29743 	}
29744 
29745 	/*
29746 	 * Now return all bp's on the failfast queue to their owners.
29747 	 */
29748 	while ((bp = un->un_failfast_headp) != NULL) {
29749 
29750 		un->un_failfast_headp = bp->av_forw;
29751 		if (un->un_failfast_headp == NULL) {
29752 			un->un_failfast_tailp = NULL;
29753 		}
29754 
29755 		/*
29756 		 * We want to return the bp with a failure error code, but
29757 		 * we do not want a call to sd_start_cmds() to occur here,
29758 		 * so use sd_return_failed_command_no_restart() instead of
29759 		 * sd_return_failed_command().
29760 		 */
29761 		sd_return_failed_command_no_restart(un, bp, EIO);
29762 	}
29763 
29764 	/* Flush the xbuf queues if required. */
29765 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29766 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29767 	}
29768 
29769 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29770 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29771 }
29772 
29773 
29774 /*
29775  *    Function: sd_failfast_flushq_callback
29776  *
29777  * Description: Return TRUE if the given bp meets the criteria for failfast
29778  *		flushing. Used with ddi_xbuf_flushq(9F).
29779  *
29780  *   Arguments: bp - ptr to buf struct to be examined.
29781  *
29782  *     Context: Any
29783  */
29784 
29785 static int
29786 sd_failfast_flushq_callback(struct buf *bp)
29787 {
29788 	/*
29789 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29790 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29791 	 */
29792 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29793 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29794 }
29795 
29796 
29797 
29798 /*
29799  * Function: sd_setup_next_xfer
29800  *
29801  * Description: Prepare next I/O operation using DMA_PARTIAL
29802  *
29803  */
29804 
29805 static int
29806 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29807     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29808 {
29809 	ssize_t	num_blks_not_xfered;
29810 	daddr_t	strt_blk_num;
29811 	ssize_t	bytes_not_xfered;
29812 	int	rval;
29813 
29814 	ASSERT(pkt->pkt_resid == 0);
29815 
29816 	/*
29817 	 * Calculate next block number and amount to be transferred.
29818 	 *
29819 	 * How much data NOT transfered to the HBA yet.
29820 	 */
29821 	bytes_not_xfered = xp->xb_dma_resid;
29822 
29823 	/*
29824 	 * figure how many blocks NOT transfered to the HBA yet.
29825 	 */
29826 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29827 
29828 	/*
29829 	 * set starting block number to the end of what WAS transfered.
29830 	 */
29831 	strt_blk_num = xp->xb_blkno +
29832 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29833 
29834 	/*
29835 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29836 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29837 	 * the disk mutex here.
29838 	 */
29839 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
29840 	    strt_blk_num, num_blks_not_xfered);
29841 
29842 	if (rval == 0) {
29843 
29844 		/*
29845 		 * Success.
29846 		 *
29847 		 * Adjust things if there are still more blocks to be
29848 		 * transfered.
29849 		 */
29850 		xp->xb_dma_resid = pkt->pkt_resid;
29851 		pkt->pkt_resid = 0;
29852 
29853 		return (1);
29854 	}
29855 
29856 	/*
29857 	 * There's really only one possible return value from
29858 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29859 	 * returns NULL.
29860 	 */
29861 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29862 
29863 	bp->b_resid = bp->b_bcount;
29864 	bp->b_flags |= B_ERROR;
29865 
29866 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29867 	    "Error setting up next portion of DMA transfer\n");
29868 
29869 	return (0);
29870 }
29871 
29872 /*
29873  *    Function: sd_panic_for_res_conflict
29874  *
29875  * Description: Call panic with a string formatted with "Reservation Conflict"
29876  *		and a human readable identifier indicating the SD instance
29877  *		that experienced the reservation conflict.
29878  *
29879  *   Arguments: un - pointer to the soft state struct for the instance.
29880  *
29881  *     Context: may execute in interrupt context.
29882  */
29883 
29884 #define	SD_RESV_CONFLICT_FMT_LEN 40
29885 void
29886 sd_panic_for_res_conflict(struct sd_lun *un)
29887 {
29888 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
29889 	char path_str[MAXPATHLEN];
29890 
29891 	(void) snprintf(panic_str, sizeof (panic_str),
29892 	    "Reservation Conflict\nDisk: %s",
29893 	    ddi_pathname(SD_DEVINFO(un), path_str));
29894 
29895 	panic(panic_str);
29896 }
29897 
29898 /*
29899  * Note: The following sd_faultinjection_ioctl( ) routines implement
29900  * driver support for handling fault injection for error analysis
29901  * causing faults in multiple layers of the driver.
29902  *
29903  */
29904 
29905 #ifdef SD_FAULT_INJECTION
29906 static uint_t   sd_fault_injection_on = 0;
29907 
29908 /*
29909  *    Function: sd_faultinjection_ioctl()
29910  *
29911  * Description: This routine is the driver entry point for handling
29912  *              faultinjection ioctls to inject errors into the
29913  *              layer model
29914  *
29915  *   Arguments: cmd	- the ioctl cmd received
29916  *		arg	- the arguments from user and returns
29917  */
29918 
29919 static void
29920 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
29921 
29922 	uint_t i = 0;
29923 	uint_t rval;
29924 
29925 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
29926 
29927 	mutex_enter(SD_MUTEX(un));
29928 
29929 	switch (cmd) {
29930 	case SDIOCRUN:
29931 		/* Allow pushed faults to be injected */
29932 		SD_INFO(SD_LOG_SDTEST, un,
29933 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
29934 
29935 		sd_fault_injection_on = 1;
29936 
29937 		SD_INFO(SD_LOG_IOERR, un,
29938 		    "sd_faultinjection_ioctl: run finished\n");
29939 		break;
29940 
29941 	case SDIOCSTART:
29942 		/* Start Injection Session */
29943 		SD_INFO(SD_LOG_SDTEST, un,
29944 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
29945 
29946 		sd_fault_injection_on = 0;
29947 		un->sd_injection_mask = 0xFFFFFFFF;
29948 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29949 			un->sd_fi_fifo_pkt[i] = NULL;
29950 			un->sd_fi_fifo_xb[i] = NULL;
29951 			un->sd_fi_fifo_un[i] = NULL;
29952 			un->sd_fi_fifo_arq[i] = NULL;
29953 		}
29954 		un->sd_fi_fifo_start = 0;
29955 		un->sd_fi_fifo_end = 0;
29956 
29957 		mutex_enter(&(un->un_fi_mutex));
29958 		un->sd_fi_log[0] = '\0';
29959 		un->sd_fi_buf_len = 0;
29960 		mutex_exit(&(un->un_fi_mutex));
29961 
29962 		SD_INFO(SD_LOG_IOERR, un,
29963 		    "sd_faultinjection_ioctl: start finished\n");
29964 		break;
29965 
29966 	case SDIOCSTOP:
29967 		/* Stop Injection Session */
29968 		SD_INFO(SD_LOG_SDTEST, un,
29969 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
29970 		sd_fault_injection_on = 0;
29971 		un->sd_injection_mask = 0x0;
29972 
29973 		/* Empty stray or unuseds structs from fifo */
29974 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29975 			if (un->sd_fi_fifo_pkt[i] != NULL) {
29976 				kmem_free(un->sd_fi_fifo_pkt[i],
29977 				    sizeof (struct sd_fi_pkt));
29978 			}
29979 			if (un->sd_fi_fifo_xb[i] != NULL) {
29980 				kmem_free(un->sd_fi_fifo_xb[i],
29981 				    sizeof (struct sd_fi_xb));
29982 			}
29983 			if (un->sd_fi_fifo_un[i] != NULL) {
29984 				kmem_free(un->sd_fi_fifo_un[i],
29985 				    sizeof (struct sd_fi_un));
29986 			}
29987 			if (un->sd_fi_fifo_arq[i] != NULL) {
29988 				kmem_free(un->sd_fi_fifo_arq[i],
29989 				    sizeof (struct sd_fi_arq));
29990 			}
29991 			un->sd_fi_fifo_pkt[i] = NULL;
29992 			un->sd_fi_fifo_un[i] = NULL;
29993 			un->sd_fi_fifo_xb[i] = NULL;
29994 			un->sd_fi_fifo_arq[i] = NULL;
29995 		}
29996 		un->sd_fi_fifo_start = 0;
29997 		un->sd_fi_fifo_end = 0;
29998 
29999 		SD_INFO(SD_LOG_IOERR, un,
30000 		    "sd_faultinjection_ioctl: stop finished\n");
30001 		break;
30002 
30003 	case SDIOCINSERTPKT:
30004 		/* Store a packet struct to be pushed onto fifo */
30005 		SD_INFO(SD_LOG_SDTEST, un,
30006 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30007 
30008 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30009 
30010 		sd_fault_injection_on = 0;
30011 
30012 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30013 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30014 			kmem_free(un->sd_fi_fifo_pkt[i],
30015 			    sizeof (struct sd_fi_pkt));
30016 		}
30017 		if (arg != NULL) {
30018 			un->sd_fi_fifo_pkt[i] =
30019 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30020 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30021 				/* Alloc failed don't store anything */
30022 				break;
30023 			}
30024 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30025 			    sizeof (struct sd_fi_pkt), 0);
30026 			if (rval == -1) {
30027 				kmem_free(un->sd_fi_fifo_pkt[i],
30028 				    sizeof (struct sd_fi_pkt));
30029 				un->sd_fi_fifo_pkt[i] = NULL;
30030 			}
30031 		} else {
30032 			SD_INFO(SD_LOG_IOERR, un,
30033 			    "sd_faultinjection_ioctl: pkt null\n");
30034 		}
30035 		break;
30036 
30037 	case SDIOCINSERTXB:
30038 		/* Store a xb struct to be pushed onto fifo */
30039 		SD_INFO(SD_LOG_SDTEST, un,
30040 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30041 
30042 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30043 
30044 		sd_fault_injection_on = 0;
30045 
30046 		if (un->sd_fi_fifo_xb[i] != NULL) {
30047 			kmem_free(un->sd_fi_fifo_xb[i],
30048 			    sizeof (struct sd_fi_xb));
30049 			un->sd_fi_fifo_xb[i] = NULL;
30050 		}
30051 		if (arg != NULL) {
30052 			un->sd_fi_fifo_xb[i] =
30053 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30054 			if (un->sd_fi_fifo_xb[i] == NULL) {
30055 				/* Alloc failed don't store anything */
30056 				break;
30057 			}
30058 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30059 			    sizeof (struct sd_fi_xb), 0);
30060 
30061 			if (rval == -1) {
30062 				kmem_free(un->sd_fi_fifo_xb[i],
30063 				    sizeof (struct sd_fi_xb));
30064 				un->sd_fi_fifo_xb[i] = NULL;
30065 			}
30066 		} else {
30067 			SD_INFO(SD_LOG_IOERR, un,
30068 			    "sd_faultinjection_ioctl: xb null\n");
30069 		}
30070 		break;
30071 
30072 	case SDIOCINSERTUN:
30073 		/* Store a un struct to be pushed onto fifo */
30074 		SD_INFO(SD_LOG_SDTEST, un,
30075 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30076 
30077 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30078 
30079 		sd_fault_injection_on = 0;
30080 
30081 		if (un->sd_fi_fifo_un[i] != NULL) {
30082 			kmem_free(un->sd_fi_fifo_un[i],
30083 			    sizeof (struct sd_fi_un));
30084 			un->sd_fi_fifo_un[i] = NULL;
30085 		}
30086 		if (arg != NULL) {
30087 			un->sd_fi_fifo_un[i] =
30088 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30089 			if (un->sd_fi_fifo_un[i] == NULL) {
30090 				/* Alloc failed don't store anything */
30091 				break;
30092 			}
30093 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30094 			    sizeof (struct sd_fi_un), 0);
30095 			if (rval == -1) {
30096 				kmem_free(un->sd_fi_fifo_un[i],
30097 				    sizeof (struct sd_fi_un));
30098 				un->sd_fi_fifo_un[i] = NULL;
30099 			}
30100 
30101 		} else {
30102 			SD_INFO(SD_LOG_IOERR, un,
30103 			    "sd_faultinjection_ioctl: un null\n");
30104 		}
30105 
30106 		break;
30107 
30108 	case SDIOCINSERTARQ:
30109 		/* Store a arq struct to be pushed onto fifo */
30110 		SD_INFO(SD_LOG_SDTEST, un,
30111 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30112 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30113 
30114 		sd_fault_injection_on = 0;
30115 
30116 		if (un->sd_fi_fifo_arq[i] != NULL) {
30117 			kmem_free(un->sd_fi_fifo_arq[i],
30118 			    sizeof (struct sd_fi_arq));
30119 			un->sd_fi_fifo_arq[i] = NULL;
30120 		}
30121 		if (arg != NULL) {
30122 			un->sd_fi_fifo_arq[i] =
30123 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30124 			if (un->sd_fi_fifo_arq[i] == NULL) {
30125 				/* Alloc failed don't store anything */
30126 				break;
30127 			}
30128 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30129 			    sizeof (struct sd_fi_arq), 0);
30130 			if (rval == -1) {
30131 				kmem_free(un->sd_fi_fifo_arq[i],
30132 				    sizeof (struct sd_fi_arq));
30133 				un->sd_fi_fifo_arq[i] = NULL;
30134 			}
30135 
30136 		} else {
30137 			SD_INFO(SD_LOG_IOERR, un,
30138 			    "sd_faultinjection_ioctl: arq null\n");
30139 		}
30140 
30141 		break;
30142 
30143 	case SDIOCPUSH:
30144 		/* Push stored xb, pkt, un, and arq onto fifo */
30145 		sd_fault_injection_on = 0;
30146 
30147 		if (arg != NULL) {
30148 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30149 			if (rval != -1 &&
30150 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30151 				un->sd_fi_fifo_end += i;
30152 			}
30153 		} else {
30154 			SD_INFO(SD_LOG_IOERR, un,
30155 			    "sd_faultinjection_ioctl: push arg null\n");
30156 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30157 				un->sd_fi_fifo_end++;
30158 			}
30159 		}
30160 		SD_INFO(SD_LOG_IOERR, un,
30161 		    "sd_faultinjection_ioctl: push to end=%d\n",
30162 		    un->sd_fi_fifo_end);
30163 		break;
30164 
30165 	case SDIOCRETRIEVE:
30166 		/* Return buffer of log from Injection session */
30167 		SD_INFO(SD_LOG_SDTEST, un,
30168 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30169 
30170 		sd_fault_injection_on = 0;
30171 
30172 		mutex_enter(&(un->un_fi_mutex));
30173 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30174 		    un->sd_fi_buf_len+1, 0);
30175 		mutex_exit(&(un->un_fi_mutex));
30176 
30177 		if (rval == -1) {
30178 			/*
30179 			 * arg is possibly invalid setting
30180 			 * it to NULL for return
30181 			 */
30182 			arg = NULL;
30183 		}
30184 		break;
30185 	}
30186 
30187 	mutex_exit(SD_MUTEX(un));
30188 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30189 			    " exit\n");
30190 }
30191 
30192 
30193 /*
30194  *    Function: sd_injection_log()
30195  *
30196  * Description: This routine adds buff to the already existing injection log
30197  *              for retrieval via faultinjection_ioctl for use in fault
30198  *              detection and recovery
30199  *
30200  *   Arguments: buf - the string to add to the log
30201  */
30202 
30203 static void
30204 sd_injection_log(char *buf, struct sd_lun *un)
30205 {
30206 	uint_t len;
30207 
30208 	ASSERT(un != NULL);
30209 	ASSERT(buf != NULL);
30210 
30211 	mutex_enter(&(un->un_fi_mutex));
30212 
30213 	len = min(strlen(buf), 255);
30214 	/* Add logged value to Injection log to be returned later */
30215 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30216 		uint_t	offset = strlen((char *)un->sd_fi_log);
30217 		char *destp = (char *)un->sd_fi_log + offset;
30218 		int i;
30219 		for (i = 0; i < len; i++) {
30220 			*destp++ = *buf++;
30221 		}
30222 		un->sd_fi_buf_len += len;
30223 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30224 	}
30225 
30226 	mutex_exit(&(un->un_fi_mutex));
30227 }
30228 
30229 
30230 /*
30231  *    Function: sd_faultinjection()
30232  *
30233  * Description: This routine takes the pkt and changes its
30234  *		content based on error injection scenerio.
30235  *
30236  *   Arguments: pktp	- packet to be changed
30237  */
30238 
30239 static void
30240 sd_faultinjection(struct scsi_pkt *pktp)
30241 {
30242 	uint_t i;
30243 	struct sd_fi_pkt *fi_pkt;
30244 	struct sd_fi_xb *fi_xb;
30245 	struct sd_fi_un *fi_un;
30246 	struct sd_fi_arq *fi_arq;
30247 	struct buf *bp;
30248 	struct sd_xbuf *xb;
30249 	struct sd_lun *un;
30250 
30251 	ASSERT(pktp != NULL);
30252 
30253 	/* pull bp xb and un from pktp */
30254 	bp = (struct buf *)pktp->pkt_private;
30255 	xb = SD_GET_XBUF(bp);
30256 	un = SD_GET_UN(bp);
30257 
30258 	ASSERT(un != NULL);
30259 
30260 	mutex_enter(SD_MUTEX(un));
30261 
30262 	SD_TRACE(SD_LOG_SDTEST, un,
30263 	    "sd_faultinjection: entry Injection from sdintr\n");
30264 
30265 	/* if injection is off return */
30266 	if (sd_fault_injection_on == 0 ||
30267 	    un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30268 		mutex_exit(SD_MUTEX(un));
30269 		return;
30270 	}
30271 
30272 	SD_INFO(SD_LOG_SDTEST, un,
30273 	    "sd_faultinjection: is working for copying\n");
30274 
30275 	/* take next set off fifo */
30276 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30277 
30278 	fi_pkt = un->sd_fi_fifo_pkt[i];
30279 	fi_xb = un->sd_fi_fifo_xb[i];
30280 	fi_un = un->sd_fi_fifo_un[i];
30281 	fi_arq = un->sd_fi_fifo_arq[i];
30282 
30283 
30284 	/* set variables accordingly */
30285 	/* set pkt if it was on fifo */
30286 	if (fi_pkt != NULL) {
30287 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30288 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30289 		if (fi_pkt->pkt_cdbp != 0xff)
30290 			SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30291 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30292 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30293 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30294 
30295 	}
30296 	/* set xb if it was on fifo */
30297 	if (fi_xb != NULL) {
30298 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30299 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30300 		if (fi_xb->xb_retry_count != 0)
30301 			SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30302 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30303 		    "xb_victim_retry_count");
30304 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30305 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30306 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30307 
30308 		/* copy in block data from sense */
30309 		/*
30310 		 * if (fi_xb->xb_sense_data[0] != -1) {
30311 		 *	bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30312 		 *	SENSE_LENGTH);
30313 		 * }
30314 		 */
30315 		bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30316 
30317 		/* copy in extended sense codes */
30318 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30319 		    xb, es_code, "es_code");
30320 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30321 		    xb, es_key, "es_key");
30322 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30323 		    xb, es_add_code, "es_add_code");
30324 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30325 		    xb, es_qual_code, "es_qual_code");
30326 		struct scsi_extended_sense *esp;
30327 		esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30328 		esp->es_class = CLASS_EXTENDED_SENSE;
30329 	}
30330 
30331 	/* set un if it was on fifo */
30332 	if (fi_un != NULL) {
30333 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30334 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30335 		SD_CONDSET(un, un, un_reset_retry_count,
30336 		    "un_reset_retry_count");
30337 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30338 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30339 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30340 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30341 		    "un_f_allow_bus_device_reset");
30342 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30343 
30344 	}
30345 
30346 	/* copy in auto request sense if it was on fifo */
30347 	if (fi_arq != NULL) {
30348 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30349 	}
30350 
30351 	/* free structs */
30352 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30353 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30354 	}
30355 	if (un->sd_fi_fifo_xb[i] != NULL) {
30356 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30357 	}
30358 	if (un->sd_fi_fifo_un[i] != NULL) {
30359 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30360 	}
30361 	if (un->sd_fi_fifo_arq[i] != NULL) {
30362 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30363 	}
30364 
30365 	/*
30366 	 * kmem_free does not gurantee to set to NULL
30367 	 * since we uses these to determine if we set
30368 	 * values or not lets confirm they are always
30369 	 * NULL after free
30370 	 */
30371 	un->sd_fi_fifo_pkt[i] = NULL;
30372 	un->sd_fi_fifo_un[i] = NULL;
30373 	un->sd_fi_fifo_xb[i] = NULL;
30374 	un->sd_fi_fifo_arq[i] = NULL;
30375 
30376 	un->sd_fi_fifo_start++;
30377 
30378 	mutex_exit(SD_MUTEX(un));
30379 
30380 	SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30381 }
30382 
30383 #endif /* SD_FAULT_INJECTION */
30384 
30385 /*
30386  * This routine is invoked in sd_unit_attach(). Before calling it, the
30387  * properties in conf file should be processed already, and "hotpluggable"
30388  * property was processed also.
30389  *
30390  * The sd driver distinguishes 3 different type of devices: removable media,
30391  * non-removable media, and hotpluggable. Below the differences are defined:
30392  *
30393  * 1. Device ID
30394  *
30395  *     The device ID of a device is used to identify this device. Refer to
30396  *     ddi_devid_register(9F).
30397  *
30398  *     For a non-removable media disk device which can provide 0x80 or 0x83
30399  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30400  *     device ID is created to identify this device. For other non-removable
30401  *     media devices, a default device ID is created only if this device has
30402  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30403  *
30404  *     -------------------------------------------------------
30405  *     removable media   hotpluggable  | Can Have Device ID
30406  *     -------------------------------------------------------
30407  *         false             false     |     Yes
30408  *         false             true      |     Yes
30409  *         true                x       |     No
30410  *     ------------------------------------------------------
30411  *
30412  *
30413  * 2. SCSI group 4 commands
30414  *
30415  *     In SCSI specs, only some commands in group 4 command set can use
30416  *     8-byte addresses that can be used to access >2TB storage spaces.
30417  *     Other commands have no such capability. Without supporting group4,
30418  *     it is impossible to make full use of storage spaces of a disk with
30419  *     capacity larger than 2TB.
30420  *
30421  *     -----------------------------------------------
30422  *     removable media   hotpluggable   LP64  |  Group
30423  *     -----------------------------------------------
30424  *           false          false       false |   1
30425  *           false          false       true  |   4
30426  *           false          true        false |   1
30427  *           false          true        true  |   4
30428  *           true             x           x   |   5
30429  *     -----------------------------------------------
30430  *
30431  *
30432  * 3. Check for VTOC Label
30433  *
30434  *     If a direct-access disk has no EFI label, sd will check if it has a
30435  *     valid VTOC label. Now, sd also does that check for removable media
30436  *     and hotpluggable devices.
30437  *
30438  *     --------------------------------------------------------------
30439  *     Direct-Access   removable media    hotpluggable |  Check Label
30440  *     -------------------------------------------------------------
30441  *         false          false           false        |   No
30442  *         false          false           true         |   No
30443  *         false          true            false        |   Yes
30444  *         false          true            true         |   Yes
30445  *         true            x                x          |   Yes
30446  *     --------------------------------------------------------------
30447  *
30448  *
30449  * 4. Building default VTOC label
30450  *
30451  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30452  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30453  *     create default VTOC for them. Currently sd creates default VTOC label
30454  *     for all devices on x86 platform (VTOC_16), but only for removable
30455  *     media devices on SPARC (VTOC_8).
30456  *
30457  *     -----------------------------------------------------------
30458  *       removable media hotpluggable platform   |   Default Label
30459  *     -----------------------------------------------------------
30460  *             false          false    sparc     |     No
30461  *             false          true      x86      |     Yes
30462  *             false          true     sparc     |     Yes
30463  *             true             x        x       |     Yes
30464  *     ----------------------------------------------------------
30465  *
30466  *
30467  * 5. Supported blocksizes of target devices
30468  *
30469  *     Sd supports non-512-byte blocksize for removable media devices only.
30470  *     For other devices, only 512-byte blocksize is supported. This may be
30471  *     changed in near future because some RAID devices require non-512-byte
30472  *     blocksize
30473  *
30474  *     -----------------------------------------------------------
30475  *     removable media    hotpluggable    | non-512-byte blocksize
30476  *     -----------------------------------------------------------
30477  *           false          false         |   No
30478  *           false          true          |   No
30479  *           true             x           |   Yes
30480  *     -----------------------------------------------------------
30481  *
30482  *
30483  * 6. Automatic mount & unmount
30484  *
30485  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30486  *     if a device is removable media device. It return 1 for removable media
30487  *     devices, and 0 for others.
30488  *
30489  *     The automatic mounting subsystem should distinguish between the types
30490  *     of devices and apply automounting policies to each.
30491  *
30492  *
30493  * 7. fdisk partition management
30494  *
30495  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30496  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30497  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30498  *     fdisk partitions on both x86 and SPARC platform.
30499  *
30500  *     -----------------------------------------------------------
30501  *       platform   removable media  USB/1394  |  fdisk supported
30502  *     -----------------------------------------------------------
30503  *        x86         X               X        |       true
30504  *     ------------------------------------------------------------
30505  *        sparc       X               X        |       false
30506  *     ------------------------------------------------------------
30507  *
30508  *
30509  * 8. MBOOT/MBR
30510  *
30511  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30512  *     read/write mboot for removable media devices on sparc platform.
30513  *
30514  *     -----------------------------------------------------------
30515  *       platform   removable media  USB/1394  |  mboot supported
30516  *     -----------------------------------------------------------
30517  *        x86         X               X        |       true
30518  *     ------------------------------------------------------------
30519  *        sparc      false           false     |       false
30520  *        sparc      false           true      |       true
30521  *        sparc      true            false     |       true
30522  *        sparc      true            true      |       true
30523  *     ------------------------------------------------------------
30524  *
30525  *
30526  * 9.  error handling during opening device
30527  *
30528  *     If failed to open a disk device, an errno is returned. For some kinds
30529  *     of errors, different errno is returned depending on if this device is
30530  *     a removable media device. This brings USB/1394 hard disks in line with
30531  *     expected hard disk behavior. It is not expected that this breaks any
30532  *     application.
30533  *
30534  *     ------------------------------------------------------
30535  *       removable media    hotpluggable   |  errno
30536  *     ------------------------------------------------------
30537  *             false          false        |   EIO
30538  *             false          true         |   EIO
30539  *             true             x          |   ENXIO
30540  *     ------------------------------------------------------
30541  *
30542  *
30543  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30544  *
30545  *     These IOCTLs are applicable only to removable media devices.
30546  *
30547  *     -----------------------------------------------------------
30548  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30549  *     -----------------------------------------------------------
30550  *             false          false        |     No
30551  *             false          true         |     No
30552  *             true            x           |     Yes
30553  *     -----------------------------------------------------------
30554  *
30555  *
30556  * 12. Kstats for partitions
30557  *
30558  *     sd creates partition kstat for non-removable media devices. USB and
30559  *     Firewire hard disks now have partition kstats
30560  *
30561  *      ------------------------------------------------------
30562  *       removable media    hotpluggable   |   kstat
30563  *      ------------------------------------------------------
30564  *             false          false        |    Yes
30565  *             false          true         |    Yes
30566  *             true             x          |    No
30567  *       ------------------------------------------------------
30568  *
30569  *
30570  * 13. Removable media & hotpluggable properties
30571  *
30572  *     Sd driver creates a "removable-media" property for removable media
30573  *     devices. Parent nexus drivers create a "hotpluggable" property if
30574  *     it supports hotplugging.
30575  *
30576  *     ---------------------------------------------------------------------
30577  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30578  *     ---------------------------------------------------------------------
30579  *       false            false       |    No                   No
30580  *       false            true        |    No                   Yes
30581  *       true             false       |    Yes                  No
30582  *       true             true        |    Yes                  Yes
30583  *     ---------------------------------------------------------------------
30584  *
30585  *
30586  * 14. Power Management
30587  *
30588  *     sd only power manages removable media devices or devices that support
30589  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30590  *
30591  *     A parent nexus that supports hotplugging can also set "pm-capable"
30592  *     if the disk can be power managed.
30593  *
30594  *     ------------------------------------------------------------
30595  *       removable media hotpluggable pm-capable  |   power manage
30596  *     ------------------------------------------------------------
30597  *             false          false     false     |     No
30598  *             false          false     true      |     Yes
30599  *             false          true      false     |     No
30600  *             false          true      true      |     Yes
30601  *             true             x        x        |     Yes
30602  *     ------------------------------------------------------------
30603  *
30604  *      USB and firewire hard disks can now be power managed independently
30605  *      of the framebuffer
30606  *
30607  *
30608  * 15. Support for USB disks with capacity larger than 1TB
30609  *
30610  *     Currently, sd doesn't permit a fixed disk device with capacity
30611  *     larger than 1TB to be used in a 32-bit operating system environment.
30612  *     However, sd doesn't do that for removable media devices. Instead, it
30613  *     assumes that removable media devices cannot have a capacity larger
30614  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30615  *     supported, which can cause some unexpected results.
30616  *
30617  *     ---------------------------------------------------------------------
30618  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30619  *     ---------------------------------------------------------------------
30620  *             false          false  |   true         |     no
30621  *             false          true   |   true         |     no
30622  *             true           false  |   true         |     Yes
30623  *             true           true   |   true         |     Yes
30624  *     ---------------------------------------------------------------------
30625  *
30626  *
30627  * 16. Check write-protection at open time
30628  *
30629  *     When a removable media device is being opened for writing without NDELAY
30630  *     flag, sd will check if this device is writable. If attempting to open
30631  *     without NDELAY flag a write-protected device, this operation will abort.
30632  *
30633  *     ------------------------------------------------------------
30634  *       removable media    USB/1394   |   WP Check
30635  *     ------------------------------------------------------------
30636  *             false          false    |     No
30637  *             false          true     |     No
30638  *             true           false    |     Yes
30639  *             true           true     |     Yes
30640  *     ------------------------------------------------------------
30641  *
30642  *
30643  * 17. syslog when corrupted VTOC is encountered
30644  *
30645  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30646  *      for fixed SCSI disks.
30647  *     ------------------------------------------------------------
30648  *       removable media    USB/1394   |   print syslog
30649  *     ------------------------------------------------------------
30650  *             false          false    |     Yes
30651  *             false          true     |     No
30652  *             true           false    |     No
30653  *             true           true     |     No
30654  *     ------------------------------------------------------------
30655  */
30656 static void
30657 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30658 {
30659 	int	pm_cap;
30660 
30661 	ASSERT(un->un_sd);
30662 	ASSERT(un->un_sd->sd_inq);
30663 
30664 	/*
30665 	 * Enable SYNC CACHE support for all devices.
30666 	 */
30667 	un->un_f_sync_cache_supported = TRUE;
30668 
30669 	/*
30670 	 * Set the sync cache required flag to false.
30671 	 * This would ensure that there is no SYNC CACHE
30672 	 * sent when there are no writes
30673 	 */
30674 	un->un_f_sync_cache_required = FALSE;
30675 
30676 	if (un->un_sd->sd_inq->inq_rmb) {
30677 		/*
30678 		 * The media of this device is removable. And for this kind
30679 		 * of devices, it is possible to change medium after opening
30680 		 * devices. Thus we should support this operation.
30681 		 */
30682 		un->un_f_has_removable_media = TRUE;
30683 
30684 		/*
30685 		 * support non-512-byte blocksize of removable media devices
30686 		 */
30687 		un->un_f_non_devbsize_supported = TRUE;
30688 
30689 		/*
30690 		 * Assume that all removable media devices support DOOR_LOCK
30691 		 */
30692 		un->un_f_doorlock_supported = TRUE;
30693 
30694 		/*
30695 		 * For a removable media device, it is possible to be opened
30696 		 * with NDELAY flag when there is no media in drive, in this
30697 		 * case we don't care if device is writable. But if without
30698 		 * NDELAY flag, we need to check if media is write-protected.
30699 		 */
30700 		un->un_f_chk_wp_open = TRUE;
30701 
30702 		/*
30703 		 * need to start a SCSI watch thread to monitor media state,
30704 		 * when media is being inserted or ejected, notify syseventd.
30705 		 */
30706 		un->un_f_monitor_media_state = TRUE;
30707 
30708 		/*
30709 		 * Some devices don't support START_STOP_UNIT command.
30710 		 * Therefore, we'd better check if a device supports it
30711 		 * before sending it.
30712 		 */
30713 		un->un_f_check_start_stop = TRUE;
30714 
30715 		/*
30716 		 * support eject media ioctl:
30717 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
30718 		 */
30719 		un->un_f_eject_media_supported = TRUE;
30720 
30721 		/*
30722 		 * Because many removable-media devices don't support
30723 		 * LOG_SENSE, we couldn't use this command to check if
30724 		 * a removable media device support power-management.
30725 		 * We assume that they support power-management via
30726 		 * START_STOP_UNIT command and can be spun up and down
30727 		 * without limitations.
30728 		 */
30729 		un->un_f_pm_supported = TRUE;
30730 
30731 		/*
30732 		 * Need to create a zero length (Boolean) property
30733 		 * removable-media for the removable media devices.
30734 		 * Note that the return value of the property is not being
30735 		 * checked, since if unable to create the property
30736 		 * then do not want the attach to fail altogether. Consistent
30737 		 * with other property creation in attach.
30738 		 */
30739 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30740 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30741 
30742 	} else {
30743 		/*
30744 		 * create device ID for device
30745 		 */
30746 		un->un_f_devid_supported = TRUE;
30747 
30748 		/*
30749 		 * Spin up non-removable-media devices once it is attached
30750 		 */
30751 		un->un_f_attach_spinup = TRUE;
30752 
30753 		/*
30754 		 * According to SCSI specification, Sense data has two kinds of
30755 		 * format: fixed format, and descriptor format. At present, we
30756 		 * don't support descriptor format sense data for removable
30757 		 * media.
30758 		 */
30759 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30760 			un->un_f_descr_format_supported = TRUE;
30761 		}
30762 
30763 		/*
30764 		 * kstats are created only for non-removable media devices.
30765 		 *
30766 		 * Set this in sd.conf to 0 in order to disable kstats.  The
30767 		 * default is 1, so they are enabled by default.
30768 		 */
30769 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30770 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
30771 		    "enable-partition-kstats", 1));
30772 
30773 		/*
30774 		 * Check if HBA has set the "pm-capable" property.
30775 		 * If "pm-capable" exists and is non-zero then we can
30776 		 * power manage the device without checking the start/stop
30777 		 * cycle count log sense page.
30778 		 *
30779 		 * If "pm-capable" exists and is set to be false (0),
30780 		 * then we should not power manage the device.
30781 		 *
30782 		 * If "pm-capable" doesn't exist then pm_cap will
30783 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
30784 		 * sd will check the start/stop cycle count log sense page
30785 		 * and power manage the device if the cycle count limit has
30786 		 * not been exceeded.
30787 		 */
30788 		pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30789 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30790 		if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30791 			un->un_f_log_sense_supported = TRUE;
30792 			if (!un->un_f_power_condition_disabled &&
30793 			    SD_INQUIRY(un)->inq_ansi == 6) {
30794 				un->un_f_power_condition_supported = TRUE;
30795 			}
30796 		} else {
30797 			/*
30798 			 * pm-capable property exists.
30799 			 *
30800 			 * Convert "TRUE" values for pm_cap to
30801 			 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30802 			 * later. "TRUE" values are any values defined in
30803 			 * inquiry.h.
30804 			 */
30805 			if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30806 				un->un_f_log_sense_supported = FALSE;
30807 			} else {
30808 				/* SD_PM_CAPABLE_IS_TRUE case */
30809 				un->un_f_pm_supported = TRUE;
30810 				if (!un->un_f_power_condition_disabled &&
30811 				    SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30812 					un->un_f_power_condition_supported =
30813 					    TRUE;
30814 				}
30815 				if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30816 					un->un_f_log_sense_supported = TRUE;
30817 					un->un_f_pm_log_sense_smart =
30818 					    SD_PM_CAP_SMART_LOG(pm_cap);
30819 				}
30820 			}
30821 
30822 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
30823 			    "sd_unit_attach: un:0x%p pm-capable "
30824 			    "property set to %d.\n", un, un->un_f_pm_supported);
30825 		}
30826 	}
30827 
30828 	if (un->un_f_is_hotpluggable) {
30829 
30830 		/*
30831 		 * Have to watch hotpluggable devices as well, since
30832 		 * that's the only way for userland applications to
30833 		 * detect hot removal while device is busy/mounted.
30834 		 */
30835 		un->un_f_monitor_media_state = TRUE;
30836 
30837 		un->un_f_check_start_stop = TRUE;
30838 
30839 	}
30840 }
30841 
30842 /*
30843  * sd_tg_rdwr:
30844  * Provides rdwr access for cmlb via sd_tgops. The start_block is
30845  * in sys block size, req_length in bytes.
30846  *
30847  */
30848 static int
30849 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
30850     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
30851 {
30852 	struct sd_lun *un;
30853 	int path_flag = (int)(uintptr_t)tg_cookie;
30854 	char *dkl = NULL;
30855 	diskaddr_t real_addr = start_block;
30856 	diskaddr_t first_byte, end_block;
30857 
30858 	size_t	buffer_size = reqlength;
30859 	int rval = 0;
30860 	diskaddr_t	cap;
30861 	uint32_t	lbasize;
30862 	sd_ssc_t	*ssc;
30863 
30864 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30865 	if (un == NULL)
30866 		return (ENXIO);
30867 
30868 	if (cmd != TG_READ && cmd != TG_WRITE)
30869 		return (EINVAL);
30870 
30871 	ssc = sd_ssc_init(un);
30872 	mutex_enter(SD_MUTEX(un));
30873 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
30874 		mutex_exit(SD_MUTEX(un));
30875 		rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30876 		    &lbasize, path_flag);
30877 		if (rval != 0)
30878 			goto done1;
30879 		mutex_enter(SD_MUTEX(un));
30880 		sd_update_block_info(un, lbasize, cap);
30881 		if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
30882 			mutex_exit(SD_MUTEX(un));
30883 			rval = EIO;
30884 			goto done;
30885 		}
30886 	}
30887 
30888 	if (NOT_DEVBSIZE(un)) {
30889 		/*
30890 		 * sys_blocksize != tgt_blocksize, need to re-adjust
30891 		 * blkno and save the index to beginning of dk_label
30892 		 */
30893 		first_byte  = SD_SYSBLOCKS2BYTES(start_block);
30894 		real_addr = first_byte / un->un_tgt_blocksize;
30895 
30896 		end_block = (first_byte + reqlength +
30897 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
30898 
30899 		/* round up buffer size to multiple of target block size */
30900 		buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
30901 
30902 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
30903 		    "label_addr: 0x%x allocation size: 0x%x\n",
30904 		    real_addr, buffer_size);
30905 
30906 		if (((first_byte % un->un_tgt_blocksize) != 0) ||
30907 		    (reqlength % un->un_tgt_blocksize) != 0)
30908 			/* the request is not aligned */
30909 			dkl = kmem_zalloc(buffer_size, KM_SLEEP);
30910 	}
30911 
30912 	/*
30913 	 * The MMC standard allows READ CAPACITY to be
30914 	 * inaccurate by a bounded amount (in the interest of
30915 	 * response latency).  As a result, failed READs are
30916 	 * commonplace (due to the reading of metadata and not
30917 	 * data). Depending on the per-Vendor/drive Sense data,
30918 	 * the failed READ can cause many (unnecessary) retries.
30919 	 */
30920 
30921 	if (ISCD(un) && (cmd == TG_READ) &&
30922 	    (un->un_f_blockcount_is_valid == TRUE) &&
30923 	    ((start_block == (un->un_blockcount - 1))||
30924 	    (start_block == (un->un_blockcount - 2)))) {
30925 			path_flag = SD_PATH_DIRECT_PRIORITY;
30926 	}
30927 
30928 	mutex_exit(SD_MUTEX(un));
30929 	if (cmd == TG_READ) {
30930 		rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
30931 		    buffer_size, real_addr, path_flag);
30932 		if (dkl != NULL)
30933 			bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
30934 			    real_addr), bufaddr, reqlength);
30935 	} else {
30936 		if (dkl) {
30937 			rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
30938 			    real_addr, path_flag);
30939 			if (rval) {
30940 				goto done1;
30941 			}
30942 			bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
30943 			    real_addr), reqlength);
30944 		}
30945 		rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
30946 		    buffer_size, real_addr, path_flag);
30947 	}
30948 
30949 done1:
30950 	if (dkl != NULL)
30951 		kmem_free(dkl, buffer_size);
30952 
30953 	if (rval != 0) {
30954 		if (rval == EIO)
30955 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
30956 		else
30957 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
30958 	}
30959 done:
30960 	sd_ssc_fini(ssc);
30961 	return (rval);
30962 }
30963 
30964 
30965 static int
30966 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
30967 {
30968 
30969 	struct sd_lun *un;
30970 	diskaddr_t	cap;
30971 	uint32_t	lbasize;
30972 	int		path_flag = (int)(uintptr_t)tg_cookie;
30973 	int		ret = 0;
30974 
30975 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30976 	if (un == NULL)
30977 		return (ENXIO);
30978 
30979 	switch (cmd) {
30980 	case TG_GETPHYGEOM:
30981 	case TG_GETVIRTGEOM:
30982 	case TG_GETCAPACITY:
30983 	case TG_GETBLOCKSIZE:
30984 		mutex_enter(SD_MUTEX(un));
30985 
30986 		if ((un->un_f_blockcount_is_valid == TRUE) &&
30987 		    (un->un_f_tgt_blocksize_is_valid == TRUE)) {
30988 			cap = un->un_blockcount;
30989 			lbasize = un->un_tgt_blocksize;
30990 			mutex_exit(SD_MUTEX(un));
30991 		} else {
30992 			sd_ssc_t	*ssc;
30993 			mutex_exit(SD_MUTEX(un));
30994 			ssc = sd_ssc_init(un);
30995 			ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30996 			    &lbasize, path_flag);
30997 			if (ret != 0) {
30998 				if (ret == EIO)
30999 					sd_ssc_assessment(ssc,
31000 					    SD_FMT_STATUS_CHECK);
31001 				else
31002 					sd_ssc_assessment(ssc,
31003 					    SD_FMT_IGNORE);
31004 				sd_ssc_fini(ssc);
31005 				return (ret);
31006 			}
31007 			sd_ssc_fini(ssc);
31008 			mutex_enter(SD_MUTEX(un));
31009 			sd_update_block_info(un, lbasize, cap);
31010 			if ((un->un_f_blockcount_is_valid == FALSE) ||
31011 			    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31012 				mutex_exit(SD_MUTEX(un));
31013 				return (EIO);
31014 			}
31015 			mutex_exit(SD_MUTEX(un));
31016 		}
31017 
31018 		if (cmd == TG_GETCAPACITY) {
31019 			*(diskaddr_t *)arg = cap;
31020 			return (0);
31021 		}
31022 
31023 		if (cmd == TG_GETBLOCKSIZE) {
31024 			*(uint32_t *)arg = lbasize;
31025 			return (0);
31026 		}
31027 
31028 		if (cmd == TG_GETPHYGEOM)
31029 			ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31030 			    cap, lbasize, path_flag);
31031 		else
31032 			/* TG_GETVIRTGEOM */
31033 			ret = sd_get_virtual_geometry(un,
31034 			    (cmlb_geom_t *)arg, cap, lbasize);
31035 
31036 		return (ret);
31037 
31038 	case TG_GETATTR:
31039 		mutex_enter(SD_MUTEX(un));
31040 		((tg_attribute_t *)arg)->media_is_writable =
31041 		    un->un_f_mmc_writable_media;
31042 		((tg_attribute_t *)arg)->media_is_solid_state =
31043 		    un->un_f_is_solid_state;
31044 		mutex_exit(SD_MUTEX(un));
31045 		return (0);
31046 	default:
31047 		return (ENOTTY);
31048 
31049 	}
31050 }
31051 
31052 /*
31053  *    Function: sd_ssc_ereport_post
31054  *
31055  * Description: Will be called when SD driver need to post an ereport.
31056  *
31057  *    Context: Kernel thread or interrupt context.
31058  */
31059 static void
31060 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31061 {
31062 	int uscsi_path_instance = 0;
31063 	uchar_t	uscsi_pkt_reason;
31064 	uint32_t uscsi_pkt_state;
31065 	uint32_t uscsi_pkt_statistics;
31066 	uint64_t uscsi_ena;
31067 	uchar_t op_code;
31068 	uint8_t *sensep;
31069 	union scsi_cdb *cdbp;
31070 	uint_t cdblen = 0;
31071 	uint_t senlen = 0;
31072 	struct sd_lun *un;
31073 	dev_info_t *dip;
31074 	char *devid;
31075 	int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31076 	    SSC_FLAGS_INVALID_STATUS |
31077 	    SSC_FLAGS_INVALID_SENSE |
31078 	    SSC_FLAGS_INVALID_DATA;
31079 	char assessment[16];
31080 
31081 	ASSERT(ssc != NULL);
31082 	ASSERT(ssc->ssc_uscsi_cmd != NULL);
31083 	ASSERT(ssc->ssc_uscsi_info != NULL);
31084 
31085 	un = ssc->ssc_un;
31086 	ASSERT(un != NULL);
31087 
31088 	dip = un->un_sd->sd_dev;
31089 
31090 	/*
31091 	 * Get the devid:
31092 	 *	devid will only be passed to non-transport error reports.
31093 	 */
31094 	devid = DEVI(dip)->devi_devid_str;
31095 
31096 	/*
31097 	 * If we are syncing or dumping, the command will not be executed
31098 	 * so we bypass this situation.
31099 	 */
31100 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31101 	    (un->un_state == SD_STATE_DUMPING))
31102 		return;
31103 
31104 	uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31105 	uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31106 	uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31107 	uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31108 	uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31109 
31110 	sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31111 	cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31112 
31113 	/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31114 	if (cdbp == NULL) {
31115 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31116 		    "sd_ssc_ereport_post meet empty cdb\n");
31117 		return;
31118 	}
31119 
31120 	op_code = cdbp->scc_cmd;
31121 
31122 	cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31123 	senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31124 	    ssc->ssc_uscsi_cmd->uscsi_rqresid);
31125 
31126 	if (senlen > 0)
31127 		ASSERT(sensep != NULL);
31128 
31129 	/*
31130 	 * Initialize drv_assess to corresponding values.
31131 	 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31132 	 * on the sense-key returned back.
31133 	 */
31134 	switch (drv_assess) {
31135 		case SD_FM_DRV_RECOVERY:
31136 			(void) sprintf(assessment, "%s", "recovered");
31137 			break;
31138 		case SD_FM_DRV_RETRY:
31139 			(void) sprintf(assessment, "%s", "retry");
31140 			break;
31141 		case SD_FM_DRV_NOTICE:
31142 			(void) sprintf(assessment, "%s", "info");
31143 			break;
31144 		case SD_FM_DRV_FATAL:
31145 		default:
31146 			(void) sprintf(assessment, "%s", "unknown");
31147 	}
31148 	/*
31149 	 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31150 	 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31151 	 * driver-assessment will always be "recovered" here.
31152 	 */
31153 	if (drv_assess == SD_FM_DRV_RECOVERY) {
31154 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31155 		    "cmd.disk.recovered", uscsi_ena, devid, DDI_NOSLEEP,
31156 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31157 		    "driver-assessment", DATA_TYPE_STRING, assessment,
31158 		    "op-code", DATA_TYPE_UINT8, op_code,
31159 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31160 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31161 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31162 		    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31163 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31164 		    NULL);
31165 		return;
31166 	}
31167 
31168 	/*
31169 	 * If there is un-expected/un-decodable data, we should post
31170 	 * ereport.io.scsi.cmd.disk.dev.uderr.
31171 	 * driver-assessment will be set based on parameter drv_assess.
31172 	 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31173 	 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31174 	 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31175 	 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31176 	 */
31177 	if (ssc->ssc_flags & ssc_invalid_flags) {
31178 		if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31179 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31180 			    "cmd.disk.dev.uderr", uscsi_ena, devid, DDI_NOSLEEP,
31181 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31182 			    "driver-assessment", DATA_TYPE_STRING,
31183 			    drv_assess == SD_FM_DRV_FATAL ?
31184 			    "fail" : assessment,
31185 			    "op-code", DATA_TYPE_UINT8, op_code,
31186 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31187 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31188 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31189 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31190 			    "pkt-stats", DATA_TYPE_UINT32,
31191 			    uscsi_pkt_statistics,
31192 			    "stat-code", DATA_TYPE_UINT8,
31193 			    ssc->ssc_uscsi_cmd->uscsi_status,
31194 			    "un-decode-info", DATA_TYPE_STRING,
31195 			    ssc->ssc_info,
31196 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31197 			    senlen, sensep,
31198 			    NULL);
31199 		} else {
31200 			/*
31201 			 * For other type of invalid data, the
31202 			 * un-decode-value field would be empty because the
31203 			 * un-decodable content could be seen from upper
31204 			 * level payload or inside un-decode-info.
31205 			 */
31206 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31207 			    "cmd.disk.dev.uderr", uscsi_ena, devid, DDI_NOSLEEP,
31208 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31209 			    "driver-assessment", DATA_TYPE_STRING,
31210 			    drv_assess == SD_FM_DRV_FATAL ?
31211 			    "fail" : assessment,
31212 			    "op-code", DATA_TYPE_UINT8, op_code,
31213 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31214 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31215 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31216 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31217 			    "pkt-stats", DATA_TYPE_UINT32,
31218 			    uscsi_pkt_statistics,
31219 			    "stat-code", DATA_TYPE_UINT8,
31220 			    ssc->ssc_uscsi_cmd->uscsi_status,
31221 			    "un-decode-info", DATA_TYPE_STRING,
31222 			    ssc->ssc_info,
31223 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31224 			    0, NULL,
31225 			    NULL);
31226 		}
31227 		ssc->ssc_flags &= ~ssc_invalid_flags;
31228 		return;
31229 	}
31230 
31231 	if (uscsi_pkt_reason != CMD_CMPLT ||
31232 	    (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31233 		/*
31234 		 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31235 		 * set inside sd_start_cmds due to errors(bad packet or
31236 		 * fatal transport error), we should take it as a
31237 		 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31238 		 * driver-assessment will be set based on drv_assess.
31239 		 * We will set devid to NULL because it is a transport
31240 		 * error.
31241 		 */
31242 		if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31243 			ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31244 
31245 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31246 		    "cmd.disk.tran", uscsi_ena, NULL, DDI_NOSLEEP, FM_VERSION,
31247 		    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31248 		    "driver-assessment", DATA_TYPE_STRING,
31249 		    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31250 		    "op-code", DATA_TYPE_UINT8, op_code,
31251 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31252 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31253 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31254 		    "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31255 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31256 		    NULL);
31257 	} else {
31258 		/*
31259 		 * If we got here, we have a completed command, and we need
31260 		 * to further investigate the sense data to see what kind
31261 		 * of ereport we should post.
31262 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr
31263 		 * if sense-key == 0x3.
31264 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31265 		 * driver-assessment will be set based on the parameter
31266 		 * drv_assess.
31267 		 */
31268 		if (senlen > 0) {
31269 			/*
31270 			 * Here we have sense data available.
31271 			 */
31272 			uint8_t sense_key;
31273 			sense_key = scsi_sense_key(sensep);
31274 			if (sense_key == 0x3) {
31275 				/*
31276 				 * sense-key == 0x3(medium error),
31277 				 * driver-assessment should be "fatal" if
31278 				 * drv_assess is SD_FM_DRV_FATAL.
31279 				 */
31280 				scsi_fm_ereport_post(un->un_sd,
31281 				    uscsi_path_instance,
31282 				    "cmd.disk.dev.rqs.merr",
31283 				    uscsi_ena, devid, DDI_NOSLEEP, FM_VERSION,
31284 				    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31285 				    "driver-assessment",
31286 				    DATA_TYPE_STRING,
31287 				    drv_assess == SD_FM_DRV_FATAL ?
31288 				    "fatal" : assessment,
31289 				    "op-code",
31290 				    DATA_TYPE_UINT8, op_code,
31291 				    "cdb",
31292 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31293 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31294 				    "pkt-reason",
31295 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31296 				    "pkt-state",
31297 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31298 				    "pkt-stats",
31299 				    DATA_TYPE_UINT32,
31300 				    uscsi_pkt_statistics,
31301 				    "stat-code",
31302 				    DATA_TYPE_UINT8,
31303 				    ssc->ssc_uscsi_cmd->uscsi_status,
31304 				    "key",
31305 				    DATA_TYPE_UINT8,
31306 				    scsi_sense_key(sensep),
31307 				    "asc",
31308 				    DATA_TYPE_UINT8,
31309 				    scsi_sense_asc(sensep),
31310 				    "ascq",
31311 				    DATA_TYPE_UINT8,
31312 				    scsi_sense_ascq(sensep),
31313 				    "sense-data",
31314 				    DATA_TYPE_UINT8_ARRAY,
31315 				    senlen, sensep,
31316 				    "lba",
31317 				    DATA_TYPE_UINT64,
31318 				    ssc->ssc_uscsi_info->ui_lba,
31319 				    NULL);
31320 				} else {
31321 					/*
31322 					 * if sense-key == 0x4(hardware
31323 					 * error), driver-assessment should
31324 					 * be "fatal" if drv_assess is
31325 					 * SD_FM_DRV_FATAL.
31326 					 */
31327 					scsi_fm_ereport_post(un->un_sd,
31328 					    uscsi_path_instance,
31329 					    "cmd.disk.dev.rqs.derr",
31330 					    uscsi_ena, devid, DDI_NOSLEEP,
31331 					    FM_VERSION,
31332 					    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31333 					    "driver-assessment",
31334 					    DATA_TYPE_STRING,
31335 					    drv_assess == SD_FM_DRV_FATAL ?
31336 					    (sense_key == 0x4 ?
31337 					    "fatal" : "fail") : assessment,
31338 					    "op-code",
31339 					    DATA_TYPE_UINT8, op_code,
31340 					    "cdb",
31341 					    DATA_TYPE_UINT8_ARRAY, cdblen,
31342 					    ssc->ssc_uscsi_cmd->uscsi_cdb,
31343 					    "pkt-reason",
31344 					    DATA_TYPE_UINT8, uscsi_pkt_reason,
31345 					    "pkt-state",
31346 					    DATA_TYPE_UINT8, uscsi_pkt_state,
31347 					    "pkt-stats",
31348 					    DATA_TYPE_UINT32,
31349 					    uscsi_pkt_statistics,
31350 					    "stat-code",
31351 					    DATA_TYPE_UINT8,
31352 					    ssc->ssc_uscsi_cmd->uscsi_status,
31353 					    "key",
31354 					    DATA_TYPE_UINT8,
31355 					    scsi_sense_key(sensep),
31356 					    "asc",
31357 					    DATA_TYPE_UINT8,
31358 					    scsi_sense_asc(sensep),
31359 					    "ascq",
31360 					    DATA_TYPE_UINT8,
31361 					    scsi_sense_ascq(sensep),
31362 					    "sense-data",
31363 					    DATA_TYPE_UINT8_ARRAY,
31364 					    senlen, sensep,
31365 					    NULL);
31366 				}
31367 		} else {
31368 			/*
31369 			 * For stat_code == STATUS_GOOD, this is not a
31370 			 * hardware error.
31371 			 */
31372 			if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31373 				return;
31374 
31375 			/*
31376 			 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31377 			 * stat-code but with sense data unavailable.
31378 			 * driver-assessment will be set based on parameter
31379 			 * drv_assess.
31380 			 */
31381 			scsi_fm_ereport_post(un->un_sd,
31382 			    uscsi_path_instance, "cmd.disk.dev.serr", uscsi_ena,
31383 			    devid, DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8,
31384 			    FM_EREPORT_VERS0,
31385 			    "driver-assessment", DATA_TYPE_STRING,
31386 			    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31387 			    "op-code", DATA_TYPE_UINT8, op_code,
31388 			    "cdb",
31389 			    DATA_TYPE_UINT8_ARRAY,
31390 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31391 			    "pkt-reason",
31392 			    DATA_TYPE_UINT8, uscsi_pkt_reason,
31393 			    "pkt-state",
31394 			    DATA_TYPE_UINT8, uscsi_pkt_state,
31395 			    "pkt-stats",
31396 			    DATA_TYPE_UINT32, uscsi_pkt_statistics,
31397 			    "stat-code",
31398 			    DATA_TYPE_UINT8,
31399 			    ssc->ssc_uscsi_cmd->uscsi_status,
31400 			    NULL);
31401 		}
31402 	}
31403 }
31404 
31405 /*
31406  *     Function: sd_ssc_extract_info
31407  *
31408  * Description: Extract information available to help generate ereport.
31409  *
31410  *     Context: Kernel thread or interrupt context.
31411  */
31412 static void
31413 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31414     struct buf *bp, struct sd_xbuf *xp)
31415 {
31416 	size_t senlen = 0;
31417 	union scsi_cdb *cdbp;
31418 	int path_instance;
31419 	/*
31420 	 * Need scsi_cdb_size array to determine the cdb length.
31421 	 */
31422 	extern uchar_t	scsi_cdb_size[];
31423 
31424 	ASSERT(un != NULL);
31425 	ASSERT(pktp != NULL);
31426 	ASSERT(bp != NULL);
31427 	ASSERT(xp != NULL);
31428 	ASSERT(ssc != NULL);
31429 	ASSERT(mutex_owned(SD_MUTEX(un)));
31430 
31431 	/*
31432 	 * Transfer the cdb buffer pointer here.
31433 	 */
31434 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31435 
31436 	ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31437 	ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31438 
31439 	/*
31440 	 * Transfer the sense data buffer pointer if sense data is available,
31441 	 * calculate the sense data length first.
31442 	 */
31443 	if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31444 	    (xp->xb_sense_state & STATE_ARQ_DONE)) {
31445 		/*
31446 		 * For arq case, we will enter here.
31447 		 */
31448 		if (xp->xb_sense_state & STATE_XARQ_DONE) {
31449 			senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31450 		} else {
31451 			senlen = SENSE_LENGTH;
31452 		}
31453 	} else {
31454 		/*
31455 		 * For non-arq case, we will enter this branch.
31456 		 */
31457 		if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31458 		    (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31459 			senlen = SENSE_LENGTH - xp->xb_sense_resid;
31460 		}
31461 
31462 	}
31463 
31464 	ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31465 	ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31466 	ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31467 
31468 	ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31469 
31470 	/*
31471 	 * Only transfer path_instance when scsi_pkt was properly allocated.
31472 	 */
31473 	path_instance = pktp->pkt_path_instance;
31474 	if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31475 		ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31476 	else
31477 		ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31478 
31479 	/*
31480 	 * Copy in the other fields we may need when posting ereport.
31481 	 */
31482 	ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31483 	ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31484 	ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31485 	ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31486 
31487 	/*
31488 	 * For partially read/write command, we will not create ena
31489 	 * in case of a successful command be reconized as recovered.
31490 	 */
31491 	if ((pktp->pkt_reason == CMD_CMPLT) &&
31492 	    (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31493 	    (senlen == 0)) {
31494 		return;
31495 	}
31496 
31497 	/*
31498 	 * To associate ereports of a single command execution flow, we
31499 	 * need a shared ena for a specific command.
31500 	 */
31501 	if (xp->xb_ena == 0)
31502 		xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31503 	ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31504 }
31505 
31506 
31507 /*
31508  *     Function: sd_check_solid_state
31509  *
31510  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31511  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31512  *              RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the
31513  *              device is a solid state drive.
31514  *
31515  *     Context: Kernel thread or interrupt context.
31516  */
31517 
31518 static void
31519 sd_check_solid_state(sd_ssc_t *ssc)
31520 {
31521 	int		rval		= 0;
31522 	uchar_t		*inqb1		= NULL;
31523 	size_t		inqb1_len	= MAX_INQUIRY_SIZE;
31524 	size_t		inqb1_resid	= 0;
31525 	struct sd_lun	*un;
31526 
31527 	ASSERT(ssc != NULL);
31528 	un = ssc->ssc_un;
31529 	ASSERT(un != NULL);
31530 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31531 
31532 	mutex_enter(SD_MUTEX(un));
31533 	un->un_f_is_solid_state = FALSE;
31534 
31535 	if (ISCD(un)) {
31536 		mutex_exit(SD_MUTEX(un));
31537 		return;
31538 	}
31539 
31540 	if (sd_check_vpd_page_support(ssc) == 0 &&
31541 	    un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31542 		mutex_exit(SD_MUTEX(un));
31543 		/* collect page b1 data */
31544 		inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31545 
31546 		rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31547 		    0x01, 0xB1, &inqb1_resid);
31548 
31549 		if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31550 			SD_TRACE(SD_LOG_COMMON, un,
31551 			    "sd_check_solid_state: \
31552 			    successfully get VPD page: %x \
31553 			    PAGE LENGTH: %x BYTE 4: %x \
31554 			    BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31555 			    inqb1[5]);
31556 
31557 			mutex_enter(SD_MUTEX(un));
31558 			/*
31559 			 * Check the MEDIUM ROTATION RATE. If it is set
31560 			 * to 1, the device is a solid state drive.
31561 			 */
31562 			if (inqb1[4] == 0 && inqb1[5] == 1) {
31563 				un->un_f_is_solid_state = TRUE;
31564 			}
31565 			mutex_exit(SD_MUTEX(un));
31566 		} else if (rval != 0) {
31567 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31568 		}
31569 
31570 		kmem_free(inqb1, inqb1_len);
31571 	} else {
31572 		mutex_exit(SD_MUTEX(un));
31573 	}
31574 }
31575