xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 89b43686)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27  * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
28  */
29 /*
30  * Copyright 2011 cyril.galibern@opensvc.com
31  */
32 
33 /*
34  * SCSI disk target driver.
35  */
36 #include <sys/scsi/scsi.h>
37 #include <sys/dkbad.h>
38 #include <sys/dklabel.h>
39 #include <sys/dkio.h>
40 #include <sys/fdio.h>
41 #include <sys/cdio.h>
42 #include <sys/mhd.h>
43 #include <sys/vtoc.h>
44 #include <sys/dktp/fdisk.h>
45 #include <sys/kstat.h>
46 #include <sys/vtrace.h>
47 #include <sys/note.h>
48 #include <sys/thread.h>
49 #include <sys/proc.h>
50 #include <sys/efi_partition.h>
51 #include <sys/var.h>
52 #include <sys/aio_req.h>
53 
54 #ifdef __lock_lint
55 #define	_LP64
56 #define	__amd64
57 #endif
58 
59 #if (defined(__fibre))
60 /* Note: is there a leadville version of the following? */
61 #include <sys/fc4/fcal_linkapp.h>
62 #endif
63 #include <sys/taskq.h>
64 #include <sys/uuid.h>
65 #include <sys/byteorder.h>
66 #include <sys/sdt.h>
67 
68 #include "sd_xbuf.h"
69 
70 #include <sys/scsi/targets/sddef.h>
71 #include <sys/cmlb.h>
72 #include <sys/sysevent/eventdefs.h>
73 #include <sys/sysevent/dev.h>
74 
75 #include <sys/fm/protocol.h>
76 
77 /*
78  * Loadable module info.
79  */
80 #if (defined(__fibre))
81 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
82 #else /* !__fibre */
83 #define	SD_MODULE_NAME	"SCSI Disk Driver"
84 #endif /* !__fibre */
85 
86 /*
87  * Define the interconnect type, to allow the driver to distinguish
88  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
89  *
90  * This is really for backward compatibility. In the future, the driver
91  * should actually check the "interconnect-type" property as reported by
92  * the HBA; however at present this property is not defined by all HBAs,
93  * so we will use this #define (1) to permit the driver to run in
94  * backward-compatibility mode; and (2) to print a notification message
95  * if an FC HBA does not support the "interconnect-type" property.  The
96  * behavior of the driver will be to assume parallel SCSI behaviors unless
97  * the "interconnect-type" property is defined by the HBA **AND** has a
98  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
99  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
100  * Channel behaviors (as per the old ssd).  (Note that the
101  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
102  * will result in the driver assuming parallel SCSI behaviors.)
103  *
104  * (see common/sys/scsi/impl/services.h)
105  *
106  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
107  * since some FC HBAs may already support that, and there is some code in
108  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
109  * default would confuse that code, and besides things should work fine
110  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
111  * "interconnect_type" property.
112  *
113  */
114 #if (defined(__fibre))
115 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
116 #else
117 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
118 #endif
119 
120 /*
121  * The name of the driver, established from the module name in _init.
122  */
123 static	char *sd_label			= NULL;
124 
125 /*
126  * Driver name is unfortunately prefixed on some driver.conf properties.
127  */
128 #if (defined(__fibre))
129 #define	sd_max_xfer_size		ssd_max_xfer_size
130 #define	sd_config_list			ssd_config_list
131 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
132 static	char *sd_config_list		= "ssd-config-list";
133 #else
134 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
135 static	char *sd_config_list		= "sd-config-list";
136 #endif
137 
138 /*
139  * Driver global variables
140  */
141 
142 #if (defined(__fibre))
143 /*
144  * These #defines are to avoid namespace collisions that occur because this
145  * code is currently used to compile two separate driver modules: sd and ssd.
146  * All global variables need to be treated this way (even if declared static)
147  * in order to allow the debugger to resolve the names properly.
148  * It is anticipated that in the near future the ssd module will be obsoleted,
149  * at which time this namespace issue should go away.
150  */
151 #define	sd_state			ssd_state
152 #define	sd_io_time			ssd_io_time
153 #define	sd_failfast_enable		ssd_failfast_enable
154 #define	sd_ua_retry_count		ssd_ua_retry_count
155 #define	sd_report_pfa			ssd_report_pfa
156 #define	sd_max_throttle			ssd_max_throttle
157 #define	sd_min_throttle			ssd_min_throttle
158 #define	sd_rot_delay			ssd_rot_delay
159 
160 #define	sd_retry_on_reservation_conflict	\
161 					ssd_retry_on_reservation_conflict
162 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
163 #define	sd_resv_conflict_name		ssd_resv_conflict_name
164 
165 #define	sd_component_mask		ssd_component_mask
166 #define	sd_level_mask			ssd_level_mask
167 #define	sd_debug_un			ssd_debug_un
168 #define	sd_error_level			ssd_error_level
169 
170 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
171 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
172 
173 #define	sd_tr				ssd_tr
174 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
175 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
176 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
177 #define	sd_check_media_time		ssd_check_media_time
178 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
179 #define	sd_label_mutex			ssd_label_mutex
180 #define	sd_detach_mutex			ssd_detach_mutex
181 #define	sd_log_buf			ssd_log_buf
182 #define	sd_log_mutex			ssd_log_mutex
183 
184 #define	sd_disk_table			ssd_disk_table
185 #define	sd_disk_table_size		ssd_disk_table_size
186 #define	sd_sense_mutex			ssd_sense_mutex
187 #define	sd_cdbtab			ssd_cdbtab
188 
189 #define	sd_cb_ops			ssd_cb_ops
190 #define	sd_ops				ssd_ops
191 #define	sd_additional_codes		ssd_additional_codes
192 #define	sd_tgops			ssd_tgops
193 
194 #define	sd_minor_data			ssd_minor_data
195 #define	sd_minor_data_efi		ssd_minor_data_efi
196 
197 #define	sd_tq				ssd_tq
198 #define	sd_wmr_tq			ssd_wmr_tq
199 #define	sd_taskq_name			ssd_taskq_name
200 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
201 #define	sd_taskq_minalloc		ssd_taskq_minalloc
202 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
203 
204 #define	sd_dump_format_string		ssd_dump_format_string
205 
206 #define	sd_iostart_chain		ssd_iostart_chain
207 #define	sd_iodone_chain			ssd_iodone_chain
208 
209 #define	sd_pm_idletime			ssd_pm_idletime
210 
211 #define	sd_force_pm_supported		ssd_force_pm_supported
212 
213 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
214 
215 #define	sd_ssc_init			ssd_ssc_init
216 #define	sd_ssc_send			ssd_ssc_send
217 #define	sd_ssc_fini			ssd_ssc_fini
218 #define	sd_ssc_assessment		ssd_ssc_assessment
219 #define	sd_ssc_post			ssd_ssc_post
220 #define	sd_ssc_print			ssd_ssc_print
221 #define	sd_ssc_ereport_post		ssd_ssc_ereport_post
222 #define	sd_ssc_set_info			ssd_ssc_set_info
223 #define	sd_ssc_extract_info		ssd_ssc_extract_info
224 
225 #endif
226 
227 #ifdef	SDDEBUG
228 int	sd_force_pm_supported		= 0;
229 #endif	/* SDDEBUG */
230 
231 void *sd_state				= NULL;
232 int sd_io_time				= SD_IO_TIME;
233 int sd_failfast_enable			= 1;
234 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
235 int sd_report_pfa			= 1;
236 int sd_max_throttle			= SD_MAX_THROTTLE;
237 int sd_min_throttle			= SD_MIN_THROTTLE;
238 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
239 int sd_qfull_throttle_enable		= TRUE;
240 
241 int sd_retry_on_reservation_conflict	= 1;
242 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
243 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
244 
245 static int sd_dtype_optical_bind	= -1;
246 
247 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
248 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
249 
250 /*
251  * Global data for debug logging. To enable debug printing, sd_component_mask
252  * and sd_level_mask should be set to the desired bit patterns as outlined in
253  * sddef.h.
254  */
255 uint_t	sd_component_mask		= 0x0;
256 uint_t	sd_level_mask			= 0x0;
257 struct	sd_lun *sd_debug_un		= NULL;
258 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
259 
260 /* Note: these may go away in the future... */
261 static uint32_t	sd_xbuf_active_limit	= 512;
262 static uint32_t sd_xbuf_reserve_limit	= 16;
263 
264 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
265 
266 /*
267  * Timer value used to reset the throttle after it has been reduced
268  * (typically in response to TRAN_BUSY or STATUS_QFULL)
269  */
270 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
271 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
272 
273 /*
274  * Interval value associated with the media change scsi watch.
275  */
276 static int sd_check_media_time		= 3000000;
277 
278 /*
279  * Wait value used for in progress operations during a DDI_SUSPEND
280  */
281 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
282 
283 /*
284  * sd_label_mutex protects a static buffer used in the disk label
285  * component of the driver
286  */
287 static kmutex_t sd_label_mutex;
288 
289 /*
290  * sd_detach_mutex protects un_layer_count, un_detach_count, and
291  * un_opens_in_progress in the sd_lun structure.
292  */
293 static kmutex_t sd_detach_mutex;
294 
295 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
296 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
297 
298 /*
299  * Global buffer and mutex for debug logging
300  */
301 static char	sd_log_buf[1024];
302 static kmutex_t	sd_log_mutex;
303 
304 /*
305  * Structs and globals for recording attached lun information.
306  * This maintains a chain. Each node in the chain represents a SCSI controller.
307  * The structure records the number of luns attached to each target connected
308  * with the controller.
309  * For parallel scsi device only.
310  */
311 struct sd_scsi_hba_tgt_lun {
312 	struct sd_scsi_hba_tgt_lun	*next;
313 	dev_info_t			*pdip;
314 	int				nlun[NTARGETS_WIDE];
315 };
316 
317 /*
318  * Flag to indicate the lun is attached or detached
319  */
320 #define	SD_SCSI_LUN_ATTACH	0
321 #define	SD_SCSI_LUN_DETACH	1
322 
323 static kmutex_t	sd_scsi_target_lun_mutex;
324 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
325 
326 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
327     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
328 
329 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
330     sd_scsi_target_lun_head))
331 
332 /*
333  * "Smart" Probe Caching structs, globals, #defines, etc.
334  * For parallel scsi and non-self-identify device only.
335  */
336 
337 /*
338  * The following resources and routines are implemented to support
339  * "smart" probing, which caches the scsi_probe() results in an array,
340  * in order to help avoid long probe times.
341  */
342 struct sd_scsi_probe_cache {
343 	struct	sd_scsi_probe_cache	*next;
344 	dev_info_t	*pdip;
345 	int		cache[NTARGETS_WIDE];
346 };
347 
348 static kmutex_t	sd_scsi_probe_cache_mutex;
349 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
350 
351 /*
352  * Really we only need protection on the head of the linked list, but
353  * better safe than sorry.
354  */
355 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
356     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
357 
358 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
359     sd_scsi_probe_cache_head))
360 
361 /*
362  * Power attribute table
363  */
364 static sd_power_attr_ss sd_pwr_ss = {
365 	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
366 	{0, 100},
367 	{30, 0},
368 	{20000, 0}
369 };
370 
371 static sd_power_attr_pc sd_pwr_pc = {
372 	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
373 		"3=active", NULL },
374 	{0, 0, 0, 100},
375 	{90, 90, 20, 0},
376 	{15000, 15000, 1000, 0}
377 };
378 
379 /*
380  * Power level to power condition
381  */
382 static int sd_pl2pc[] = {
383 	SD_TARGET_START_VALID,
384 	SD_TARGET_STANDBY,
385 	SD_TARGET_IDLE,
386 	SD_TARGET_ACTIVE
387 };
388 
389 /*
390  * Vendor specific data name property declarations
391  */
392 
393 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
394 
395 static sd_tunables seagate_properties = {
396 	SEAGATE_THROTTLE_VALUE,
397 	0,
398 	0,
399 	0,
400 	0,
401 	0,
402 	0,
403 	0,
404 	0
405 };
406 
407 
408 static sd_tunables fujitsu_properties = {
409 	FUJITSU_THROTTLE_VALUE,
410 	0,
411 	0,
412 	0,
413 	0,
414 	0,
415 	0,
416 	0,
417 	0
418 };
419 
420 static sd_tunables ibm_properties = {
421 	IBM_THROTTLE_VALUE,
422 	0,
423 	0,
424 	0,
425 	0,
426 	0,
427 	0,
428 	0,
429 	0
430 };
431 
432 static sd_tunables purple_properties = {
433 	PURPLE_THROTTLE_VALUE,
434 	0,
435 	0,
436 	PURPLE_BUSY_RETRIES,
437 	PURPLE_RESET_RETRY_COUNT,
438 	PURPLE_RESERVE_RELEASE_TIME,
439 	0,
440 	0,
441 	0
442 };
443 
444 static sd_tunables sve_properties = {
445 	SVE_THROTTLE_VALUE,
446 	0,
447 	0,
448 	SVE_BUSY_RETRIES,
449 	SVE_RESET_RETRY_COUNT,
450 	SVE_RESERVE_RELEASE_TIME,
451 	SVE_MIN_THROTTLE_VALUE,
452 	SVE_DISKSORT_DISABLED_FLAG,
453 	0
454 };
455 
456 static sd_tunables maserati_properties = {
457 	0,
458 	0,
459 	0,
460 	0,
461 	0,
462 	0,
463 	0,
464 	MASERATI_DISKSORT_DISABLED_FLAG,
465 	MASERATI_LUN_RESET_ENABLED_FLAG
466 };
467 
468 static sd_tunables pirus_properties = {
469 	PIRUS_THROTTLE_VALUE,
470 	0,
471 	PIRUS_NRR_COUNT,
472 	PIRUS_BUSY_RETRIES,
473 	PIRUS_RESET_RETRY_COUNT,
474 	0,
475 	PIRUS_MIN_THROTTLE_VALUE,
476 	PIRUS_DISKSORT_DISABLED_FLAG,
477 	PIRUS_LUN_RESET_ENABLED_FLAG
478 };
479 
480 #endif
481 
482 #if (defined(__sparc) && !defined(__fibre)) || \
483 	(defined(__i386) || defined(__amd64))
484 
485 
486 static sd_tunables elite_properties = {
487 	ELITE_THROTTLE_VALUE,
488 	0,
489 	0,
490 	0,
491 	0,
492 	0,
493 	0,
494 	0,
495 	0
496 };
497 
498 static sd_tunables st31200n_properties = {
499 	ST31200N_THROTTLE_VALUE,
500 	0,
501 	0,
502 	0,
503 	0,
504 	0,
505 	0,
506 	0,
507 	0
508 };
509 
510 #endif /* Fibre or not */
511 
512 static sd_tunables lsi_properties_scsi = {
513 	LSI_THROTTLE_VALUE,
514 	0,
515 	LSI_NOTREADY_RETRIES,
516 	0,
517 	0,
518 	0,
519 	0,
520 	0,
521 	0
522 };
523 
524 static sd_tunables symbios_properties = {
525 	SYMBIOS_THROTTLE_VALUE,
526 	0,
527 	SYMBIOS_NOTREADY_RETRIES,
528 	0,
529 	0,
530 	0,
531 	0,
532 	0,
533 	0
534 };
535 
536 static sd_tunables lsi_properties = {
537 	0,
538 	0,
539 	LSI_NOTREADY_RETRIES,
540 	0,
541 	0,
542 	0,
543 	0,
544 	0,
545 	0
546 };
547 
548 static sd_tunables lsi_oem_properties = {
549 	0,
550 	0,
551 	LSI_OEM_NOTREADY_RETRIES,
552 	0,
553 	0,
554 	0,
555 	0,
556 	0,
557 	0,
558 	1
559 };
560 
561 
562 
563 #if (defined(SD_PROP_TST))
564 
565 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
566 #define	SD_TST_THROTTLE_VAL	16
567 #define	SD_TST_NOTREADY_VAL	12
568 #define	SD_TST_BUSY_VAL		60
569 #define	SD_TST_RST_RETRY_VAL	36
570 #define	SD_TST_RSV_REL_TIME	60
571 
572 static sd_tunables tst_properties = {
573 	SD_TST_THROTTLE_VAL,
574 	SD_TST_CTYPE_VAL,
575 	SD_TST_NOTREADY_VAL,
576 	SD_TST_BUSY_VAL,
577 	SD_TST_RST_RETRY_VAL,
578 	SD_TST_RSV_REL_TIME,
579 	0,
580 	0,
581 	0
582 };
583 #endif
584 
585 /* This is similar to the ANSI toupper implementation */
586 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
587 
588 /*
589  * Static Driver Configuration Table
590  *
591  * This is the table of disks which need throttle adjustment (or, perhaps
592  * something else as defined by the flags at a future time.)  device_id
593  * is a string consisting of concatenated vid (vendor), pid (product/model)
594  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
595  * the parts of the string are as defined by the sizes in the scsi_inquiry
596  * structure.  Device type is searched as far as the device_id string is
597  * defined.  Flags defines which values are to be set in the driver from the
598  * properties list.
599  *
600  * Entries below which begin and end with a "*" are a special case.
601  * These do not have a specific vendor, and the string which follows
602  * can appear anywhere in the 16 byte PID portion of the inquiry data.
603  *
604  * Entries below which begin and end with a " " (blank) are a special
605  * case. The comparison function will treat multiple consecutive blanks
606  * as equivalent to a single blank. For example, this causes a
607  * sd_disk_table entry of " NEC CDROM " to match a device's id string
608  * of  "NEC       CDROM".
609  *
610  * Note: The MD21 controller type has been obsoleted.
611  *	 ST318202F is a Legacy device
612  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
613  *	 made with an FC connection. The entries here are a legacy.
614  */
615 static sd_disk_config_t sd_disk_table[] = {
616 #if defined(__fibre) || defined(__i386) || defined(__amd64)
617 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
618 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
619 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
632 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
633 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
639 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
640 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
641 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
642 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
643 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
644 	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
645 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
646 	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
667 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
668 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
669 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
670 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
671 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
672 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
673 			SD_CONF_BSET_BSY_RETRY_COUNT|
674 			SD_CONF_BSET_RST_RETRIES|
675 			SD_CONF_BSET_RSV_REL_TIME,
676 		&purple_properties },
677 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
678 		SD_CONF_BSET_BSY_RETRY_COUNT|
679 		SD_CONF_BSET_RST_RETRIES|
680 		SD_CONF_BSET_RSV_REL_TIME|
681 		SD_CONF_BSET_MIN_THROTTLE|
682 		SD_CONF_BSET_DISKSORT_DISABLED,
683 		&sve_properties },
684 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
685 			SD_CONF_BSET_BSY_RETRY_COUNT|
686 			SD_CONF_BSET_RST_RETRIES|
687 			SD_CONF_BSET_RSV_REL_TIME,
688 		&purple_properties },
689 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
690 		SD_CONF_BSET_LUN_RESET_ENABLED,
691 		&maserati_properties },
692 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
693 		SD_CONF_BSET_NRR_COUNT|
694 		SD_CONF_BSET_BSY_RETRY_COUNT|
695 		SD_CONF_BSET_RST_RETRIES|
696 		SD_CONF_BSET_MIN_THROTTLE|
697 		SD_CONF_BSET_DISKSORT_DISABLED|
698 		SD_CONF_BSET_LUN_RESET_ENABLED,
699 		&pirus_properties },
700 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
701 		SD_CONF_BSET_NRR_COUNT|
702 		SD_CONF_BSET_BSY_RETRY_COUNT|
703 		SD_CONF_BSET_RST_RETRIES|
704 		SD_CONF_BSET_MIN_THROTTLE|
705 		SD_CONF_BSET_DISKSORT_DISABLED|
706 		SD_CONF_BSET_LUN_RESET_ENABLED,
707 		&pirus_properties },
708 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
709 		SD_CONF_BSET_NRR_COUNT|
710 		SD_CONF_BSET_BSY_RETRY_COUNT|
711 		SD_CONF_BSET_RST_RETRIES|
712 		SD_CONF_BSET_MIN_THROTTLE|
713 		SD_CONF_BSET_DISKSORT_DISABLED|
714 		SD_CONF_BSET_LUN_RESET_ENABLED,
715 		&pirus_properties },
716 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
717 		SD_CONF_BSET_NRR_COUNT|
718 		SD_CONF_BSET_BSY_RETRY_COUNT|
719 		SD_CONF_BSET_RST_RETRIES|
720 		SD_CONF_BSET_MIN_THROTTLE|
721 		SD_CONF_BSET_DISKSORT_DISABLED|
722 		SD_CONF_BSET_LUN_RESET_ENABLED,
723 		&pirus_properties },
724 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
725 		SD_CONF_BSET_NRR_COUNT|
726 		SD_CONF_BSET_BSY_RETRY_COUNT|
727 		SD_CONF_BSET_RST_RETRIES|
728 		SD_CONF_BSET_MIN_THROTTLE|
729 		SD_CONF_BSET_DISKSORT_DISABLED|
730 		SD_CONF_BSET_LUN_RESET_ENABLED,
731 		&pirus_properties },
732 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
733 		SD_CONF_BSET_NRR_COUNT|
734 		SD_CONF_BSET_BSY_RETRY_COUNT|
735 		SD_CONF_BSET_RST_RETRIES|
736 		SD_CONF_BSET_MIN_THROTTLE|
737 		SD_CONF_BSET_DISKSORT_DISABLED|
738 		SD_CONF_BSET_LUN_RESET_ENABLED,
739 		&pirus_properties },
740 	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
741 	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
742 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
743 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
744 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
747 #endif /* fibre or NON-sparc platforms */
748 #if ((defined(__sparc) && !defined(__fibre)) ||\
749 	(defined(__i386) || defined(__amd64)))
750 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
751 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
752 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
753 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
754 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
755 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
756 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
757 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
758 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
759 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
760 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
761 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
762 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
763 	    &symbios_properties },
764 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
765 	    &lsi_properties_scsi },
766 #if defined(__i386) || defined(__amd64)
767 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
768 				    | SD_CONF_BSET_READSUB_BCD
769 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
770 				    | SD_CONF_BSET_NO_READ_HEADER
771 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
772 
773 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
774 				    | SD_CONF_BSET_READSUB_BCD
775 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
776 				    | SD_CONF_BSET_NO_READ_HEADER
777 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
778 #endif /* __i386 || __amd64 */
779 #endif /* sparc NON-fibre or NON-sparc platforms */
780 
781 #if (defined(SD_PROP_TST))
782 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
783 				| SD_CONF_BSET_CTYPE
784 				| SD_CONF_BSET_NRR_COUNT
785 				| SD_CONF_BSET_FAB_DEVID
786 				| SD_CONF_BSET_NOCACHE
787 				| SD_CONF_BSET_BSY_RETRY_COUNT
788 				| SD_CONF_BSET_PLAYMSF_BCD
789 				| SD_CONF_BSET_READSUB_BCD
790 				| SD_CONF_BSET_READ_TOC_TRK_BCD
791 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
792 				| SD_CONF_BSET_NO_READ_HEADER
793 				| SD_CONF_BSET_READ_CD_XD4
794 				| SD_CONF_BSET_RST_RETRIES
795 				| SD_CONF_BSET_RSV_REL_TIME
796 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
797 #endif
798 };
799 
800 static const int sd_disk_table_size =
801 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
802 
803 /*
804  * Emulation mode disk drive VID/PID table
805  */
806 static char sd_flash_dev_table[][25] = {
807 	"ATA     MARVELL SD88SA02",
808 	"MARVELL SD88SA02",
809 	"TOSHIBA THNSNV05",
810 };
811 
812 static const int sd_flash_dev_table_size =
813 	sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
814 
815 #define	SD_INTERCONNECT_PARALLEL	0
816 #define	SD_INTERCONNECT_FABRIC		1
817 #define	SD_INTERCONNECT_FIBRE		2
818 #define	SD_INTERCONNECT_SSA		3
819 #define	SD_INTERCONNECT_SATA		4
820 #define	SD_INTERCONNECT_SAS		5
821 
822 #define	SD_IS_PARALLEL_SCSI(un)		\
823 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
824 #define	SD_IS_SERIAL(un)		\
825 	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
826 	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
827 
828 /*
829  * Definitions used by device id registration routines
830  */
831 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
832 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
833 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
834 
835 static kmutex_t sd_sense_mutex = {0};
836 
837 /*
838  * Macros for updates of the driver state
839  */
840 #define	New_state(un, s)        \
841 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
842 #define	Restore_state(un)	\
843 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
844 
845 static struct sd_cdbinfo sd_cdbtab[] = {
846 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
847 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
848 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
849 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
850 };
851 
852 /*
853  * Specifies the number of seconds that must have elapsed since the last
854  * cmd. has completed for a device to be declared idle to the PM framework.
855  */
856 static int sd_pm_idletime = 1;
857 
858 /*
859  * Internal function prototypes
860  */
861 
862 #if (defined(__fibre))
863 /*
864  * These #defines are to avoid namespace collisions that occur because this
865  * code is currently used to compile two separate driver modules: sd and ssd.
866  * All function names need to be treated this way (even if declared static)
867  * in order to allow the debugger to resolve the names properly.
868  * It is anticipated that in the near future the ssd module will be obsoleted,
869  * at which time this ugliness should go away.
870  */
871 #define	sd_log_trace			ssd_log_trace
872 #define	sd_log_info			ssd_log_info
873 #define	sd_log_err			ssd_log_err
874 #define	sdprobe				ssdprobe
875 #define	sdinfo				ssdinfo
876 #define	sd_prop_op			ssd_prop_op
877 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
878 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
879 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
880 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
881 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
882 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
883 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
884 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
885 #define	sd_spin_up_unit			ssd_spin_up_unit
886 #define	sd_enable_descr_sense		ssd_enable_descr_sense
887 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
888 #define	sd_set_mmc_caps			ssd_set_mmc_caps
889 #define	sd_read_unit_properties		ssd_read_unit_properties
890 #define	sd_process_sdconf_file		ssd_process_sdconf_file
891 #define	sd_process_sdconf_table		ssd_process_sdconf_table
892 #define	sd_sdconf_id_match		ssd_sdconf_id_match
893 #define	sd_blank_cmp			ssd_blank_cmp
894 #define	sd_chk_vers1_data		ssd_chk_vers1_data
895 #define	sd_set_vers1_properties		ssd_set_vers1_properties
896 #define	sd_check_solid_state		ssd_check_solid_state
897 #define	sd_check_emulation_mode		ssd_check_emulation_mode
898 
899 #define	sd_get_physical_geometry	ssd_get_physical_geometry
900 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
901 #define	sd_update_block_info		ssd_update_block_info
902 #define	sd_register_devid		ssd_register_devid
903 #define	sd_get_devid			ssd_get_devid
904 #define	sd_create_devid			ssd_create_devid
905 #define	sd_write_deviceid		ssd_write_deviceid
906 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
907 #define	sd_setup_pm			ssd_setup_pm
908 #define	sd_create_pm_components		ssd_create_pm_components
909 #define	sd_ddi_suspend			ssd_ddi_suspend
910 #define	sd_ddi_resume			ssd_ddi_resume
911 #define	sd_pm_state_change		ssd_pm_state_change
912 #define	sdpower				ssdpower
913 #define	sdattach			ssdattach
914 #define	sddetach			ssddetach
915 #define	sd_unit_attach			ssd_unit_attach
916 #define	sd_unit_detach			ssd_unit_detach
917 #define	sd_set_unit_attributes		ssd_set_unit_attributes
918 #define	sd_create_errstats		ssd_create_errstats
919 #define	sd_set_errstats			ssd_set_errstats
920 #define	sd_set_pstats			ssd_set_pstats
921 #define	sddump				ssddump
922 #define	sd_scsi_poll			ssd_scsi_poll
923 #define	sd_send_polled_RQS		ssd_send_polled_RQS
924 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
925 #define	sd_init_event_callbacks		ssd_init_event_callbacks
926 #define	sd_event_callback		ssd_event_callback
927 #define	sd_cache_control		ssd_cache_control
928 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
929 #define	sd_get_nv_sup			ssd_get_nv_sup
930 #define	sd_make_device			ssd_make_device
931 #define	sdopen				ssdopen
932 #define	sdclose				ssdclose
933 #define	sd_ready_and_valid		ssd_ready_and_valid
934 #define	sdmin				ssdmin
935 #define	sdread				ssdread
936 #define	sdwrite				ssdwrite
937 #define	sdaread				ssdaread
938 #define	sdawrite			ssdawrite
939 #define	sdstrategy			ssdstrategy
940 #define	sdioctl				ssdioctl
941 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
942 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
943 #define	sd_checksum_iostart		ssd_checksum_iostart
944 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
945 #define	sd_pm_iostart			ssd_pm_iostart
946 #define	sd_core_iostart			ssd_core_iostart
947 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
948 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
949 #define	sd_checksum_iodone		ssd_checksum_iodone
950 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
951 #define	sd_pm_iodone			ssd_pm_iodone
952 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
953 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
954 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
955 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
956 #define	sd_buf_iodone			ssd_buf_iodone
957 #define	sd_uscsi_strategy		ssd_uscsi_strategy
958 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
959 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
960 #define	sd_uscsi_iodone			ssd_uscsi_iodone
961 #define	sd_xbuf_strategy		ssd_xbuf_strategy
962 #define	sd_xbuf_init			ssd_xbuf_init
963 #define	sd_pm_entry			ssd_pm_entry
964 #define	sd_pm_exit			ssd_pm_exit
965 
966 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
967 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
968 
969 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
970 #define	sdintr				ssdintr
971 #define	sd_start_cmds			ssd_start_cmds
972 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
973 #define	sd_bioclone_alloc		ssd_bioclone_alloc
974 #define	sd_bioclone_free		ssd_bioclone_free
975 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
976 #define	sd_shadow_buf_free		ssd_shadow_buf_free
977 #define	sd_print_transport_rejected_message	\
978 					ssd_print_transport_rejected_message
979 #define	sd_retry_command		ssd_retry_command
980 #define	sd_set_retry_bp			ssd_set_retry_bp
981 #define	sd_send_request_sense_command	ssd_send_request_sense_command
982 #define	sd_start_retry_command		ssd_start_retry_command
983 #define	sd_start_direct_priority_command	\
984 					ssd_start_direct_priority_command
985 #define	sd_return_failed_command	ssd_return_failed_command
986 #define	sd_return_failed_command_no_restart	\
987 					ssd_return_failed_command_no_restart
988 #define	sd_return_command		ssd_return_command
989 #define	sd_sync_with_callback		ssd_sync_with_callback
990 #define	sdrunout			ssdrunout
991 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
992 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
993 #define	sd_reduce_throttle		ssd_reduce_throttle
994 #define	sd_restore_throttle		ssd_restore_throttle
995 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
996 #define	sd_init_cdb_limits		ssd_init_cdb_limits
997 #define	sd_pkt_status_good		ssd_pkt_status_good
998 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
999 #define	sd_pkt_status_busy		ssd_pkt_status_busy
1000 #define	sd_pkt_status_reservation_conflict	\
1001 					ssd_pkt_status_reservation_conflict
1002 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
1003 #define	sd_handle_request_sense		ssd_handle_request_sense
1004 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
1005 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
1006 #define	sd_validate_sense_data		ssd_validate_sense_data
1007 #define	sd_decode_sense			ssd_decode_sense
1008 #define	sd_print_sense_msg		ssd_print_sense_msg
1009 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
1010 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
1011 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
1012 #define	sd_sense_key_medium_or_hardware_error	\
1013 					ssd_sense_key_medium_or_hardware_error
1014 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1015 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1016 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1017 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1018 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1019 #define	sd_sense_key_default		ssd_sense_key_default
1020 #define	sd_print_retry_msg		ssd_print_retry_msg
1021 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1022 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1023 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1024 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1025 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1026 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1027 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1028 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1029 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1030 #define	sd_reset_target			ssd_reset_target
1031 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1032 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1033 #define	sd_taskq_create			ssd_taskq_create
1034 #define	sd_taskq_delete			ssd_taskq_delete
1035 #define	sd_target_change_task		ssd_target_change_task
1036 #define	sd_log_dev_status_event		ssd_log_dev_status_event
1037 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1038 #define	sd_log_eject_request_event	ssd_log_eject_request_event
1039 #define	sd_media_change_task		ssd_media_change_task
1040 #define	sd_handle_mchange		ssd_handle_mchange
1041 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1042 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1043 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1044 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1045 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1046 					sd_send_scsi_feature_GET_CONFIGURATION
1047 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1048 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1049 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1050 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1051 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1052 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1053 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1054 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1055 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1056 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1057 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1058 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1059 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1060 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1061 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1062 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1063 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1064 #define	sd_alloc_rqs			ssd_alloc_rqs
1065 #define	sd_free_rqs			ssd_free_rqs
1066 #define	sd_dump_memory			ssd_dump_memory
1067 #define	sd_get_media_info_com		ssd_get_media_info_com
1068 #define	sd_get_media_info		ssd_get_media_info
1069 #define	sd_get_media_info_ext		ssd_get_media_info_ext
1070 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1071 #define	sd_nvpair_str_decode		ssd_nvpair_str_decode
1072 #define	sd_strtok_r			ssd_strtok_r
1073 #define	sd_set_properties		ssd_set_properties
1074 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1075 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1076 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1077 #define	sd_check_mhd			ssd_check_mhd
1078 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1079 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1080 #define	sd_sname			ssd_sname
1081 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1082 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1083 #define	sd_take_ownership		ssd_take_ownership
1084 #define	sd_reserve_release		ssd_reserve_release
1085 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1086 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1087 #define	sd_persistent_reservation_in_read_keys	\
1088 					ssd_persistent_reservation_in_read_keys
1089 #define	sd_persistent_reservation_in_read_resv	\
1090 					ssd_persistent_reservation_in_read_resv
1091 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1092 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1093 #define	sd_mhdioc_release		ssd_mhdioc_release
1094 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1095 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1096 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1097 #define	sr_change_blkmode		ssr_change_blkmode
1098 #define	sr_change_speed			ssr_change_speed
1099 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1100 #define	sr_pause_resume			ssr_pause_resume
1101 #define	sr_play_msf			ssr_play_msf
1102 #define	sr_play_trkind			ssr_play_trkind
1103 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1104 #define	sr_read_subchannel		ssr_read_subchannel
1105 #define	sr_read_tocentry		ssr_read_tocentry
1106 #define	sr_read_tochdr			ssr_read_tochdr
1107 #define	sr_read_cdda			ssr_read_cdda
1108 #define	sr_read_cdxa			ssr_read_cdxa
1109 #define	sr_read_mode1			ssr_read_mode1
1110 #define	sr_read_mode2			ssr_read_mode2
1111 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1112 #define	sr_sector_mode			ssr_sector_mode
1113 #define	sr_eject			ssr_eject
1114 #define	sr_ejected			ssr_ejected
1115 #define	sr_check_wp			ssr_check_wp
1116 #define	sd_watch_request_submit		ssd_watch_request_submit
1117 #define	sd_check_media			ssd_check_media
1118 #define	sd_media_watch_cb		ssd_media_watch_cb
1119 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1120 #define	sr_volume_ctrl			ssr_volume_ctrl
1121 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1122 #define	sd_log_page_supported		ssd_log_page_supported
1123 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1124 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1125 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1126 #define	sd_range_lock			ssd_range_lock
1127 #define	sd_get_range			ssd_get_range
1128 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1129 #define	sd_range_unlock			ssd_range_unlock
1130 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1131 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1132 
1133 #define	sd_iostart_chain		ssd_iostart_chain
1134 #define	sd_iodone_chain			ssd_iodone_chain
1135 #define	sd_initpkt_map			ssd_initpkt_map
1136 #define	sd_destroypkt_map		ssd_destroypkt_map
1137 #define	sd_chain_type_map		ssd_chain_type_map
1138 #define	sd_chain_index_map		ssd_chain_index_map
1139 
1140 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1141 #define	sd_failfast_flushq		ssd_failfast_flushq
1142 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1143 
1144 #define	sd_is_lsi			ssd_is_lsi
1145 #define	sd_tg_rdwr			ssd_tg_rdwr
1146 #define	sd_tg_getinfo			ssd_tg_getinfo
1147 #define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
1148 
1149 #endif	/* #if (defined(__fibre)) */
1150 
1151 
1152 int _init(void);
1153 int _fini(void);
1154 int _info(struct modinfo *modinfop);
1155 
1156 /*PRINTFLIKE3*/
1157 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1158 /*PRINTFLIKE3*/
1159 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1160 /*PRINTFLIKE3*/
1161 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1162 
1163 static int sdprobe(dev_info_t *devi);
1164 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1165     void **result);
1166 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1167     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1168 
1169 /*
1170  * Smart probe for parallel scsi
1171  */
1172 static void sd_scsi_probe_cache_init(void);
1173 static void sd_scsi_probe_cache_fini(void);
1174 static void sd_scsi_clear_probe_cache(void);
1175 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1176 
1177 /*
1178  * Attached luns on target for parallel scsi
1179  */
1180 static void sd_scsi_target_lun_init(void);
1181 static void sd_scsi_target_lun_fini(void);
1182 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1183 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1184 
1185 static int	sd_spin_up_unit(sd_ssc_t *ssc);
1186 
1187 /*
1188  * Using sd_ssc_init to establish sd_ssc_t struct
1189  * Using sd_ssc_send to send uscsi internal command
1190  * Using sd_ssc_fini to free sd_ssc_t struct
1191  */
1192 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1193 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1194     int flag, enum uio_seg dataspace, int path_flag);
1195 static void sd_ssc_fini(sd_ssc_t *ssc);
1196 
1197 /*
1198  * Using sd_ssc_assessment to set correct type-of-assessment
1199  * Using sd_ssc_post to post ereport & system log
1200  *       sd_ssc_post will call sd_ssc_print to print system log
1201  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1202  */
1203 static void sd_ssc_assessment(sd_ssc_t *ssc,
1204     enum sd_type_assessment tp_assess);
1205 
1206 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1207 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1208 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1209     enum sd_driver_assessment drv_assess);
1210 
1211 /*
1212  * Using sd_ssc_set_info to mark an un-decodable-data error.
1213  * Using sd_ssc_extract_info to transfer information from internal
1214  *       data structures to sd_ssc_t.
1215  */
1216 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1217     const char *fmt, ...);
1218 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1219     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1220 
1221 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1222     enum uio_seg dataspace, int path_flag);
1223 
1224 #ifdef _LP64
1225 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1226 static void	sd_reenable_dsense_task(void *arg);
1227 #endif /* _LP64 */
1228 
1229 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1230 
1231 static void sd_read_unit_properties(struct sd_lun *un);
1232 static int  sd_process_sdconf_file(struct sd_lun *un);
1233 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1234 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1235 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1236 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1237     int *data_list, sd_tunables *values);
1238 static void sd_process_sdconf_table(struct sd_lun *un);
1239 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1240 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1241 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1242 	int list_len, char *dataname_ptr);
1243 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1244     sd_tunables *prop_list);
1245 
1246 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1247     int reservation_flag);
1248 static int  sd_get_devid(sd_ssc_t *ssc);
1249 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1250 static int  sd_write_deviceid(sd_ssc_t *ssc);
1251 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1252 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1253 
1254 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1255 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1256 
1257 static int  sd_ddi_suspend(dev_info_t *devi);
1258 static int  sd_ddi_resume(dev_info_t *devi);
1259 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1260 static int  sdpower(dev_info_t *devi, int component, int level);
1261 
1262 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1263 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1264 static int  sd_unit_attach(dev_info_t *devi);
1265 static int  sd_unit_detach(dev_info_t *devi);
1266 
1267 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1268 static void sd_create_errstats(struct sd_lun *un, int instance);
1269 static void sd_set_errstats(struct sd_lun *un);
1270 static void sd_set_pstats(struct sd_lun *un);
1271 
1272 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1273 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1274 static int  sd_send_polled_RQS(struct sd_lun *un);
1275 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1276 
1277 #if (defined(__fibre))
1278 /*
1279  * Event callbacks (photon)
1280  */
1281 static void sd_init_event_callbacks(struct sd_lun *un);
1282 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1283 #endif
1284 
1285 /*
1286  * Defines for sd_cache_control
1287  */
1288 
1289 #define	SD_CACHE_ENABLE		1
1290 #define	SD_CACHE_DISABLE	0
1291 #define	SD_CACHE_NOCHANGE	-1
1292 
1293 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1294 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1295 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1296 static dev_t sd_make_device(dev_info_t *devi);
1297 static void  sd_check_solid_state(sd_ssc_t *ssc);
1298 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1299 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1300 	uint64_t capacity);
1301 
1302 /*
1303  * Driver entry point functions.
1304  */
1305 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1306 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1307 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1308 
1309 static void sdmin(struct buf *bp);
1310 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1311 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1312 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1313 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1314 
1315 static int sdstrategy(struct buf *bp);
1316 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1317 
1318 /*
1319  * Function prototypes for layering functions in the iostart chain.
1320  */
1321 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1322 	struct buf *bp);
1323 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1324 	struct buf *bp);
1325 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1326 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1327 	struct buf *bp);
1328 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1329 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1330 
1331 /*
1332  * Function prototypes for layering functions in the iodone chain.
1333  */
1334 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1335 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1336 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1337 	struct buf *bp);
1338 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1339 	struct buf *bp);
1340 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1341 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1342 	struct buf *bp);
1343 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1344 
1345 /*
1346  * Prototypes for functions to support buf(9S) based IO.
1347  */
1348 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1349 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1350 static void sd_destroypkt_for_buf(struct buf *);
1351 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1352 	struct buf *bp, int flags,
1353 	int (*callback)(caddr_t), caddr_t callback_arg,
1354 	diskaddr_t lba, uint32_t blockcount);
1355 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1356 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1357 
1358 /*
1359  * Prototypes for functions to support USCSI IO.
1360  */
1361 static int sd_uscsi_strategy(struct buf *bp);
1362 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1363 static void sd_destroypkt_for_uscsi(struct buf *);
1364 
1365 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1366 	uchar_t chain_type, void *pktinfop);
1367 
1368 static int  sd_pm_entry(struct sd_lun *un);
1369 static void sd_pm_exit(struct sd_lun *un);
1370 
1371 static void sd_pm_idletimeout_handler(void *arg);
1372 
1373 /*
1374  * sd_core internal functions (used at the sd_core_io layer).
1375  */
1376 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1377 static void sdintr(struct scsi_pkt *pktp);
1378 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1379 
1380 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1381 	enum uio_seg dataspace, int path_flag);
1382 
1383 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1384 	daddr_t blkno, int (*func)(struct buf *));
1385 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1386 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1387 static void sd_bioclone_free(struct buf *bp);
1388 static void sd_shadow_buf_free(struct buf *bp);
1389 
1390 static void sd_print_transport_rejected_message(struct sd_lun *un,
1391 	struct sd_xbuf *xp, int code);
1392 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1393     void *arg, int code);
1394 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1395     void *arg, int code);
1396 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1397     void *arg, int code);
1398 
1399 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1400 	int retry_check_flag,
1401 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1402 		int c),
1403 	void *user_arg, int failure_code,  clock_t retry_delay,
1404 	void (*statp)(kstat_io_t *));
1405 
1406 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1407 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1408 
1409 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1410 	struct scsi_pkt *pktp);
1411 static void sd_start_retry_command(void *arg);
1412 static void sd_start_direct_priority_command(void *arg);
1413 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1414 	int errcode);
1415 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1416 	struct buf *bp, int errcode);
1417 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1418 static void sd_sync_with_callback(struct sd_lun *un);
1419 static int sdrunout(caddr_t arg);
1420 
1421 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1422 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1423 
1424 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1425 static void sd_restore_throttle(void *arg);
1426 
1427 static void sd_init_cdb_limits(struct sd_lun *un);
1428 
1429 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1430 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1431 
1432 /*
1433  * Error handling functions
1434  */
1435 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1436 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1437 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1438 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1439 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1440 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1441 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1442 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1443 
1444 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1445 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1446 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1447 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1448 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1449 	struct sd_xbuf *xp, size_t actual_len);
1450 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1451 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1452 
1453 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1454 	void *arg, int code);
1455 
1456 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1457 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1458 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1459 	uint8_t *sense_datap,
1460 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1461 static void sd_sense_key_not_ready(struct sd_lun *un,
1462 	uint8_t *sense_datap,
1463 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1464 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1465 	uint8_t *sense_datap,
1466 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1467 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1468 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1469 static void sd_sense_key_unit_attention(struct sd_lun *un,
1470 	uint8_t *sense_datap,
1471 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1472 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1473 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1474 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1475 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1476 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1477 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1478 static void sd_sense_key_default(struct sd_lun *un,
1479 	uint8_t *sense_datap,
1480 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1481 
1482 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1483 	void *arg, int flag);
1484 
1485 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1486 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1487 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1488 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1489 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1490 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1491 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1492 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1493 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1494 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1495 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1496 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1497 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1498 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1499 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1500 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1501 
1502 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1503 
1504 static void sd_start_stop_unit_callback(void *arg);
1505 static void sd_start_stop_unit_task(void *arg);
1506 
1507 static void sd_taskq_create(void);
1508 static void sd_taskq_delete(void);
1509 static void sd_target_change_task(void *arg);
1510 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1511 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1512 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1513 static void sd_media_change_task(void *arg);
1514 
1515 static int sd_handle_mchange(struct sd_lun *un);
1516 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1517 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1518 	uint32_t *lbap, int path_flag);
1519 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1520 	uint32_t *lbap, uint32_t *psp, int path_flag);
1521 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1522 	int flag, int path_flag);
1523 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1524 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1525 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1526 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1527 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1528 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1529 	uchar_t usr_cmd, uchar_t *usr_bufp);
1530 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1531 	struct dk_callback *dkc);
1532 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1533 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1534 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1535 	uchar_t *bufaddr, uint_t buflen, int path_flag);
1536 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1537 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1538 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1539 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1540 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1541 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1542 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1543 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1544 	size_t buflen, daddr_t start_block, int path_flag);
1545 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1546 	sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1547 	path_flag)
1548 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1549 	sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1550 	path_flag)
1551 
1552 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1553 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1554 	uint16_t param_ptr, int path_flag);
1555 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1556 	uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1557 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1558 
1559 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1560 static void sd_free_rqs(struct sd_lun *un);
1561 
1562 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1563 	uchar_t *data, int len, int fmt);
1564 static void sd_panic_for_res_conflict(struct sd_lun *un);
1565 
1566 /*
1567  * Disk Ioctl Function Prototypes
1568  */
1569 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1570 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1571 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1572 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1573 
1574 /*
1575  * Multi-host Ioctl Prototypes
1576  */
1577 static int sd_check_mhd(dev_t dev, int interval);
1578 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1579 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1580 static char *sd_sname(uchar_t status);
1581 static void sd_mhd_resvd_recover(void *arg);
1582 static void sd_resv_reclaim_thread();
1583 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1584 static int sd_reserve_release(dev_t dev, int cmd);
1585 static void sd_rmv_resv_reclaim_req(dev_t dev);
1586 static void sd_mhd_reset_notify_cb(caddr_t arg);
1587 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1588 	mhioc_inkeys_t *usrp, int flag);
1589 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1590 	mhioc_inresvs_t *usrp, int flag);
1591 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1592 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1593 static int sd_mhdioc_release(dev_t dev);
1594 static int sd_mhdioc_register_devid(dev_t dev);
1595 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1596 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1597 
1598 /*
1599  * SCSI removable prototypes
1600  */
1601 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1602 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1603 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1604 static int sr_pause_resume(dev_t dev, int mode);
1605 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1606 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1607 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1608 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1609 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1610 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1616 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1617 static int sr_eject(dev_t dev);
1618 static void sr_ejected(register struct sd_lun *un);
1619 static int sr_check_wp(dev_t dev);
1620 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1621 static int sd_check_media(dev_t dev, enum dkio_state state);
1622 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1623 static void sd_delayed_cv_broadcast(void *arg);
1624 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1625 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1626 
1627 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1628 
1629 /*
1630  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1631  */
1632 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1633 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1634 static void sd_wm_cache_destructor(void *wm, void *un);
1635 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1636 	daddr_t endb, ushort_t typ);
1637 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1638 	daddr_t endb);
1639 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1640 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1641 static void sd_read_modify_write_task(void * arg);
1642 static int
1643 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1644 	struct buf **bpp);
1645 
1646 
1647 /*
1648  * Function prototypes for failfast support.
1649  */
1650 static void sd_failfast_flushq(struct sd_lun *un);
1651 static int sd_failfast_flushq_callback(struct buf *bp);
1652 
1653 /*
1654  * Function prototypes to check for lsi devices
1655  */
1656 static void sd_is_lsi(struct sd_lun *un);
1657 
1658 /*
1659  * Function prototypes for partial DMA support
1660  */
1661 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1662 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1663 
1664 
1665 /* Function prototypes for cmlb */
1666 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1667     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1668 
1669 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1670 
1671 /*
1672  * For printing RMW warning message timely
1673  */
1674 static void sd_rmw_msg_print_handler(void *arg);
1675 
1676 /*
1677  * Constants for failfast support:
1678  *
1679  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1680  * failfast processing being performed.
1681  *
1682  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1683  * failfast processing on all bufs with B_FAILFAST set.
1684  */
1685 
1686 #define	SD_FAILFAST_INACTIVE		0
1687 #define	SD_FAILFAST_ACTIVE		1
1688 
1689 /*
1690  * Bitmask to control behavior of buf(9S) flushes when a transition to
1691  * the failfast state occurs. Optional bits include:
1692  *
1693  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1694  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1695  * be flushed.
1696  *
1697  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1698  * driver, in addition to the regular wait queue. This includes the xbuf
1699  * queues. When clear, only the driver's wait queue will be flushed.
1700  */
1701 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1702 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1703 
1704 /*
1705  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1706  * to flush all queues within the driver.
1707  */
1708 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1709 
1710 
1711 /*
1712  * SD Testing Fault Injection
1713  */
1714 #ifdef SD_FAULT_INJECTION
1715 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1716 static void sd_faultinjection(struct scsi_pkt *pktp);
1717 static void sd_injection_log(char *buf, struct sd_lun *un);
1718 #endif
1719 
1720 /*
1721  * Device driver ops vector
1722  */
1723 static struct cb_ops sd_cb_ops = {
1724 	sdopen,			/* open */
1725 	sdclose,		/* close */
1726 	sdstrategy,		/* strategy */
1727 	nodev,			/* print */
1728 	sddump,			/* dump */
1729 	sdread,			/* read */
1730 	sdwrite,		/* write */
1731 	sdioctl,		/* ioctl */
1732 	nodev,			/* devmap */
1733 	nodev,			/* mmap */
1734 	nodev,			/* segmap */
1735 	nochpoll,		/* poll */
1736 	sd_prop_op,		/* cb_prop_op */
1737 	0,			/* streamtab  */
1738 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1739 	CB_REV,			/* cb_rev */
1740 	sdaread, 		/* async I/O read entry point */
1741 	sdawrite		/* async I/O write entry point */
1742 };
1743 
1744 struct dev_ops sd_ops = {
1745 	DEVO_REV,		/* devo_rev, */
1746 	0,			/* refcnt  */
1747 	sdinfo,			/* info */
1748 	nulldev,		/* identify */
1749 	sdprobe,		/* probe */
1750 	sdattach,		/* attach */
1751 	sddetach,		/* detach */
1752 	nodev,			/* reset */
1753 	&sd_cb_ops,		/* driver operations */
1754 	NULL,			/* bus operations */
1755 	sdpower,		/* power */
1756 	ddi_quiesce_not_needed,		/* quiesce */
1757 };
1758 
1759 /*
1760  * This is the loadable module wrapper.
1761  */
1762 #include <sys/modctl.h>
1763 
1764 #ifndef XPV_HVM_DRIVER
1765 static struct modldrv modldrv = {
1766 	&mod_driverops,		/* Type of module. This one is a driver */
1767 	SD_MODULE_NAME,		/* Module name. */
1768 	&sd_ops			/* driver ops */
1769 };
1770 
1771 static struct modlinkage modlinkage = {
1772 	MODREV_1, &modldrv, NULL
1773 };
1774 
1775 #else /* XPV_HVM_DRIVER */
1776 static struct modlmisc modlmisc = {
1777 	&mod_miscops,		/* Type of module. This one is a misc */
1778 	"HVM " SD_MODULE_NAME,		/* Module name. */
1779 };
1780 
1781 static struct modlinkage modlinkage = {
1782 	MODREV_1, &modlmisc, NULL
1783 };
1784 
1785 #endif /* XPV_HVM_DRIVER */
1786 
1787 static cmlb_tg_ops_t sd_tgops = {
1788 	TG_DK_OPS_VERSION_1,
1789 	sd_tg_rdwr,
1790 	sd_tg_getinfo
1791 };
1792 
1793 static struct scsi_asq_key_strings sd_additional_codes[] = {
1794 	0x81, 0, "Logical Unit is Reserved",
1795 	0x85, 0, "Audio Address Not Valid",
1796 	0xb6, 0, "Media Load Mechanism Failed",
1797 	0xB9, 0, "Audio Play Operation Aborted",
1798 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1799 	0x53, 2, "Medium removal prevented",
1800 	0x6f, 0, "Authentication failed during key exchange",
1801 	0x6f, 1, "Key not present",
1802 	0x6f, 2, "Key not established",
1803 	0x6f, 3, "Read without proper authentication",
1804 	0x6f, 4, "Mismatched region to this logical unit",
1805 	0x6f, 5, "Region reset count error",
1806 	0xffff, 0x0, NULL
1807 };
1808 
1809 
1810 /*
1811  * Struct for passing printing information for sense data messages
1812  */
1813 struct sd_sense_info {
1814 	int	ssi_severity;
1815 	int	ssi_pfa_flag;
1816 };
1817 
1818 /*
1819  * Table of function pointers for iostart-side routines. Separate "chains"
1820  * of layered function calls are formed by placing the function pointers
1821  * sequentially in the desired order. Functions are called according to an
1822  * incrementing table index ordering. The last function in each chain must
1823  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1824  * in the sd_iodone_chain[] array.
1825  *
1826  * Note: It may seem more natural to organize both the iostart and iodone
1827  * functions together, into an array of structures (or some similar
1828  * organization) with a common index, rather than two separate arrays which
1829  * must be maintained in synchronization. The purpose of this division is
1830  * to achieve improved performance: individual arrays allows for more
1831  * effective cache line utilization on certain platforms.
1832  */
1833 
1834 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1835 
1836 
1837 static sd_chain_t sd_iostart_chain[] = {
1838 
1839 	/* Chain for buf IO for disk drive targets (PM enabled) */
1840 	sd_mapblockaddr_iostart,	/* Index: 0 */
1841 	sd_pm_iostart,			/* Index: 1 */
1842 	sd_core_iostart,		/* Index: 2 */
1843 
1844 	/* Chain for buf IO for disk drive targets (PM disabled) */
1845 	sd_mapblockaddr_iostart,	/* Index: 3 */
1846 	sd_core_iostart,		/* Index: 4 */
1847 
1848 	/*
1849 	 * Chain for buf IO for removable-media or large sector size
1850 	 * disk drive targets with RMW needed (PM enabled)
1851 	 */
1852 	sd_mapblockaddr_iostart,	/* Index: 5 */
1853 	sd_mapblocksize_iostart,	/* Index: 6 */
1854 	sd_pm_iostart,			/* Index: 7 */
1855 	sd_core_iostart,		/* Index: 8 */
1856 
1857 	/*
1858 	 * Chain for buf IO for removable-media or large sector size
1859 	 * disk drive targets with RMW needed (PM disabled)
1860 	 */
1861 	sd_mapblockaddr_iostart,	/* Index: 9 */
1862 	sd_mapblocksize_iostart,	/* Index: 10 */
1863 	sd_core_iostart,		/* Index: 11 */
1864 
1865 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1866 	sd_mapblockaddr_iostart,	/* Index: 12 */
1867 	sd_checksum_iostart,		/* Index: 13 */
1868 	sd_pm_iostart,			/* Index: 14 */
1869 	sd_core_iostart,		/* Index: 15 */
1870 
1871 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1872 	sd_mapblockaddr_iostart,	/* Index: 16 */
1873 	sd_checksum_iostart,		/* Index: 17 */
1874 	sd_core_iostart,		/* Index: 18 */
1875 
1876 	/* Chain for USCSI commands (all targets) */
1877 	sd_pm_iostart,			/* Index: 19 */
1878 	sd_core_iostart,		/* Index: 20 */
1879 
1880 	/* Chain for checksumming USCSI commands (all targets) */
1881 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1882 	sd_pm_iostart,			/* Index: 22 */
1883 	sd_core_iostart,		/* Index: 23 */
1884 
1885 	/* Chain for "direct" USCSI commands (all targets) */
1886 	sd_core_iostart,		/* Index: 24 */
1887 
1888 	/* Chain for "direct priority" USCSI commands (all targets) */
1889 	sd_core_iostart,		/* Index: 25 */
1890 
1891 	/*
1892 	 * Chain for buf IO for large sector size disk drive targets
1893 	 * with RMW needed with checksumming (PM enabled)
1894 	 */
1895 	sd_mapblockaddr_iostart,	/* Index: 26 */
1896 	sd_mapblocksize_iostart,	/* Index: 27 */
1897 	sd_checksum_iostart,		/* Index: 28 */
1898 	sd_pm_iostart,			/* Index: 29 */
1899 	sd_core_iostart,		/* Index: 30 */
1900 
1901 	/*
1902 	 * Chain for buf IO for large sector size disk drive targets
1903 	 * with RMW needed with checksumming (PM disabled)
1904 	 */
1905 	sd_mapblockaddr_iostart,	/* Index: 31 */
1906 	sd_mapblocksize_iostart,	/* Index: 32 */
1907 	sd_checksum_iostart,		/* Index: 33 */
1908 	sd_core_iostart,		/* Index: 34 */
1909 
1910 };
1911 
1912 /*
1913  * Macros to locate the first function of each iostart chain in the
1914  * sd_iostart_chain[] array. These are located by the index in the array.
1915  */
1916 #define	SD_CHAIN_DISK_IOSTART			0
1917 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1918 #define	SD_CHAIN_MSS_DISK_IOSTART		5
1919 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1920 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1921 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1922 #define	SD_CHAIN_CHKSUM_IOSTART			12
1923 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1924 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1925 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1926 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1927 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1928 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1929 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1930 
1931 
1932 /*
1933  * Table of function pointers for the iodone-side routines for the driver-
1934  * internal layering mechanism.  The calling sequence for iodone routines
1935  * uses a decrementing table index, so the last routine called in a chain
1936  * must be at the lowest array index location for that chain.  The last
1937  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1938  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1939  * of the functions in an iodone side chain must correspond to the ordering
1940  * of the iostart routines for that chain.  Note that there is no iodone
1941  * side routine that corresponds to sd_core_iostart(), so there is no
1942  * entry in the table for this.
1943  */
1944 
1945 static sd_chain_t sd_iodone_chain[] = {
1946 
1947 	/* Chain for buf IO for disk drive targets (PM enabled) */
1948 	sd_buf_iodone,			/* Index: 0 */
1949 	sd_mapblockaddr_iodone,		/* Index: 1 */
1950 	sd_pm_iodone,			/* Index: 2 */
1951 
1952 	/* Chain for buf IO for disk drive targets (PM disabled) */
1953 	sd_buf_iodone,			/* Index: 3 */
1954 	sd_mapblockaddr_iodone,		/* Index: 4 */
1955 
1956 	/*
1957 	 * Chain for buf IO for removable-media or large sector size
1958 	 * disk drive targets with RMW needed (PM enabled)
1959 	 */
1960 	sd_buf_iodone,			/* Index: 5 */
1961 	sd_mapblockaddr_iodone,		/* Index: 6 */
1962 	sd_mapblocksize_iodone,		/* Index: 7 */
1963 	sd_pm_iodone,			/* Index: 8 */
1964 
1965 	/*
1966 	 * Chain for buf IO for removable-media or large sector size
1967 	 * disk drive targets with RMW needed (PM disabled)
1968 	 */
1969 	sd_buf_iodone,			/* Index: 9 */
1970 	sd_mapblockaddr_iodone,		/* Index: 10 */
1971 	sd_mapblocksize_iodone,		/* Index: 11 */
1972 
1973 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1974 	sd_buf_iodone,			/* Index: 12 */
1975 	sd_mapblockaddr_iodone,		/* Index: 13 */
1976 	sd_checksum_iodone,		/* Index: 14 */
1977 	sd_pm_iodone,			/* Index: 15 */
1978 
1979 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1980 	sd_buf_iodone,			/* Index: 16 */
1981 	sd_mapblockaddr_iodone,		/* Index: 17 */
1982 	sd_checksum_iodone,		/* Index: 18 */
1983 
1984 	/* Chain for USCSI commands (non-checksum targets) */
1985 	sd_uscsi_iodone,		/* Index: 19 */
1986 	sd_pm_iodone,			/* Index: 20 */
1987 
1988 	/* Chain for USCSI commands (checksum targets) */
1989 	sd_uscsi_iodone,		/* Index: 21 */
1990 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1991 	sd_pm_iodone,			/* Index: 22 */
1992 
1993 	/* Chain for "direct" USCSI commands (all targets) */
1994 	sd_uscsi_iodone,		/* Index: 24 */
1995 
1996 	/* Chain for "direct priority" USCSI commands (all targets) */
1997 	sd_uscsi_iodone,		/* Index: 25 */
1998 
1999 	/*
2000 	 * Chain for buf IO for large sector size disk drive targets
2001 	 * with checksumming (PM enabled)
2002 	 */
2003 	sd_buf_iodone,			/* Index: 26 */
2004 	sd_mapblockaddr_iodone,		/* Index: 27 */
2005 	sd_mapblocksize_iodone,		/* Index: 28 */
2006 	sd_checksum_iodone,		/* Index: 29 */
2007 	sd_pm_iodone,			/* Index: 30 */
2008 
2009 	/*
2010 	 * Chain for buf IO for large sector size disk drive targets
2011 	 * with checksumming (PM disabled)
2012 	 */
2013 	sd_buf_iodone,			/* Index: 31 */
2014 	sd_mapblockaddr_iodone,		/* Index: 32 */
2015 	sd_mapblocksize_iodone,		/* Index: 33 */
2016 	sd_checksum_iodone,		/* Index: 34 */
2017 };
2018 
2019 
2020 /*
2021  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2022  * each iodone-side chain. These are located by the array index, but as the
2023  * iodone side functions are called in a decrementing-index order, the
2024  * highest index number in each chain must be specified (as these correspond
2025  * to the first function in the iodone chain that will be called by the core
2026  * at IO completion time).
2027  */
2028 
2029 #define	SD_CHAIN_DISK_IODONE			2
2030 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
2031 #define	SD_CHAIN_RMMEDIA_IODONE			8
2032 #define	SD_CHAIN_MSS_DISK_IODONE		8
2033 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2034 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2035 #define	SD_CHAIN_CHKSUM_IODONE			15
2036 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2037 #define	SD_CHAIN_USCSI_CMD_IODONE		20
2038 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2039 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
2040 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2041 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2042 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2043 
2044 
2045 
2046 /*
2047  * Array to map a layering chain index to the appropriate initpkt routine.
2048  * The redundant entries are present so that the index used for accessing
2049  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2050  * with this table as well.
2051  */
2052 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2053 
2054 static sd_initpkt_t	sd_initpkt_map[] = {
2055 
2056 	/* Chain for buf IO for disk drive targets (PM enabled) */
2057 	sd_initpkt_for_buf,		/* Index: 0 */
2058 	sd_initpkt_for_buf,		/* Index: 1 */
2059 	sd_initpkt_for_buf,		/* Index: 2 */
2060 
2061 	/* Chain for buf IO for disk drive targets (PM disabled) */
2062 	sd_initpkt_for_buf,		/* Index: 3 */
2063 	sd_initpkt_for_buf,		/* Index: 4 */
2064 
2065 	/*
2066 	 * Chain for buf IO for removable-media or large sector size
2067 	 * disk drive targets (PM enabled)
2068 	 */
2069 	sd_initpkt_for_buf,		/* Index: 5 */
2070 	sd_initpkt_for_buf,		/* Index: 6 */
2071 	sd_initpkt_for_buf,		/* Index: 7 */
2072 	sd_initpkt_for_buf,		/* Index: 8 */
2073 
2074 	/*
2075 	 * Chain for buf IO for removable-media or large sector size
2076 	 * disk drive targets (PM disabled)
2077 	 */
2078 	sd_initpkt_for_buf,		/* Index: 9 */
2079 	sd_initpkt_for_buf,		/* Index: 10 */
2080 	sd_initpkt_for_buf,		/* Index: 11 */
2081 
2082 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2083 	sd_initpkt_for_buf,		/* Index: 12 */
2084 	sd_initpkt_for_buf,		/* Index: 13 */
2085 	sd_initpkt_for_buf,		/* Index: 14 */
2086 	sd_initpkt_for_buf,		/* Index: 15 */
2087 
2088 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2089 	sd_initpkt_for_buf,		/* Index: 16 */
2090 	sd_initpkt_for_buf,		/* Index: 17 */
2091 	sd_initpkt_for_buf,		/* Index: 18 */
2092 
2093 	/* Chain for USCSI commands (non-checksum targets) */
2094 	sd_initpkt_for_uscsi,		/* Index: 19 */
2095 	sd_initpkt_for_uscsi,		/* Index: 20 */
2096 
2097 	/* Chain for USCSI commands (checksum targets) */
2098 	sd_initpkt_for_uscsi,		/* Index: 21 */
2099 	sd_initpkt_for_uscsi,		/* Index: 22 */
2100 	sd_initpkt_for_uscsi,		/* Index: 22 */
2101 
2102 	/* Chain for "direct" USCSI commands (all targets) */
2103 	sd_initpkt_for_uscsi,		/* Index: 24 */
2104 
2105 	/* Chain for "direct priority" USCSI commands (all targets) */
2106 	sd_initpkt_for_uscsi,		/* Index: 25 */
2107 
2108 	/*
2109 	 * Chain for buf IO for large sector size disk drive targets
2110 	 * with checksumming (PM enabled)
2111 	 */
2112 	sd_initpkt_for_buf,		/* Index: 26 */
2113 	sd_initpkt_for_buf,		/* Index: 27 */
2114 	sd_initpkt_for_buf,		/* Index: 28 */
2115 	sd_initpkt_for_buf,		/* Index: 29 */
2116 	sd_initpkt_for_buf,		/* Index: 30 */
2117 
2118 	/*
2119 	 * Chain for buf IO for large sector size disk drive targets
2120 	 * with checksumming (PM disabled)
2121 	 */
2122 	sd_initpkt_for_buf,		/* Index: 31 */
2123 	sd_initpkt_for_buf,		/* Index: 32 */
2124 	sd_initpkt_for_buf,		/* Index: 33 */
2125 	sd_initpkt_for_buf,		/* Index: 34 */
2126 };
2127 
2128 
2129 /*
2130  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2131  * The redundant entries are present so that the index used for accessing
2132  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2133  * with this table as well.
2134  */
2135 typedef void (*sd_destroypkt_t)(struct buf *);
2136 
2137 static sd_destroypkt_t	sd_destroypkt_map[] = {
2138 
2139 	/* Chain for buf IO for disk drive targets (PM enabled) */
2140 	sd_destroypkt_for_buf,		/* Index: 0 */
2141 	sd_destroypkt_for_buf,		/* Index: 1 */
2142 	sd_destroypkt_for_buf,		/* Index: 2 */
2143 
2144 	/* Chain for buf IO for disk drive targets (PM disabled) */
2145 	sd_destroypkt_for_buf,		/* Index: 3 */
2146 	sd_destroypkt_for_buf,		/* Index: 4 */
2147 
2148 	/*
2149 	 * Chain for buf IO for removable-media or large sector size
2150 	 * disk drive targets (PM enabled)
2151 	 */
2152 	sd_destroypkt_for_buf,		/* Index: 5 */
2153 	sd_destroypkt_for_buf,		/* Index: 6 */
2154 	sd_destroypkt_for_buf,		/* Index: 7 */
2155 	sd_destroypkt_for_buf,		/* Index: 8 */
2156 
2157 	/*
2158 	 * Chain for buf IO for removable-media or large sector size
2159 	 * disk drive targets (PM disabled)
2160 	 */
2161 	sd_destroypkt_for_buf,		/* Index: 9 */
2162 	sd_destroypkt_for_buf,		/* Index: 10 */
2163 	sd_destroypkt_for_buf,		/* Index: 11 */
2164 
2165 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2166 	sd_destroypkt_for_buf,		/* Index: 12 */
2167 	sd_destroypkt_for_buf,		/* Index: 13 */
2168 	sd_destroypkt_for_buf,		/* Index: 14 */
2169 	sd_destroypkt_for_buf,		/* Index: 15 */
2170 
2171 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2172 	sd_destroypkt_for_buf,		/* Index: 16 */
2173 	sd_destroypkt_for_buf,		/* Index: 17 */
2174 	sd_destroypkt_for_buf,		/* Index: 18 */
2175 
2176 	/* Chain for USCSI commands (non-checksum targets) */
2177 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2178 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2179 
2180 	/* Chain for USCSI commands (checksum targets) */
2181 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2182 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2183 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2184 
2185 	/* Chain for "direct" USCSI commands (all targets) */
2186 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2187 
2188 	/* Chain for "direct priority" USCSI commands (all targets) */
2189 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2190 
2191 	/*
2192 	 * Chain for buf IO for large sector size disk drive targets
2193 	 * with checksumming (PM disabled)
2194 	 */
2195 	sd_destroypkt_for_buf,		/* Index: 26 */
2196 	sd_destroypkt_for_buf,		/* Index: 27 */
2197 	sd_destroypkt_for_buf,		/* Index: 28 */
2198 	sd_destroypkt_for_buf,		/* Index: 29 */
2199 	sd_destroypkt_for_buf,		/* Index: 30 */
2200 
2201 	/*
2202 	 * Chain for buf IO for large sector size disk drive targets
2203 	 * with checksumming (PM enabled)
2204 	 */
2205 	sd_destroypkt_for_buf,		/* Index: 31 */
2206 	sd_destroypkt_for_buf,		/* Index: 32 */
2207 	sd_destroypkt_for_buf,		/* Index: 33 */
2208 	sd_destroypkt_for_buf,		/* Index: 34 */
2209 };
2210 
2211 
2212 
2213 /*
2214  * Array to map a layering chain index to the appropriate chain "type".
2215  * The chain type indicates a specific property/usage of the chain.
2216  * The redundant entries are present so that the index used for accessing
2217  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2218  * with this table as well.
2219  */
2220 
2221 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2222 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2223 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2224 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2225 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2226 						/* (for error recovery) */
2227 
2228 static int sd_chain_type_map[] = {
2229 
2230 	/* Chain for buf IO for disk drive targets (PM enabled) */
2231 	SD_CHAIN_BUFIO,			/* Index: 0 */
2232 	SD_CHAIN_BUFIO,			/* Index: 1 */
2233 	SD_CHAIN_BUFIO,			/* Index: 2 */
2234 
2235 	/* Chain for buf IO for disk drive targets (PM disabled) */
2236 	SD_CHAIN_BUFIO,			/* Index: 3 */
2237 	SD_CHAIN_BUFIO,			/* Index: 4 */
2238 
2239 	/*
2240 	 * Chain for buf IO for removable-media or large sector size
2241 	 * disk drive targets (PM enabled)
2242 	 */
2243 	SD_CHAIN_BUFIO,			/* Index: 5 */
2244 	SD_CHAIN_BUFIO,			/* Index: 6 */
2245 	SD_CHAIN_BUFIO,			/* Index: 7 */
2246 	SD_CHAIN_BUFIO,			/* Index: 8 */
2247 
2248 	/*
2249 	 * Chain for buf IO for removable-media or large sector size
2250 	 * disk drive targets (PM disabled)
2251 	 */
2252 	SD_CHAIN_BUFIO,			/* Index: 9 */
2253 	SD_CHAIN_BUFIO,			/* Index: 10 */
2254 	SD_CHAIN_BUFIO,			/* Index: 11 */
2255 
2256 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2257 	SD_CHAIN_BUFIO,			/* Index: 12 */
2258 	SD_CHAIN_BUFIO,			/* Index: 13 */
2259 	SD_CHAIN_BUFIO,			/* Index: 14 */
2260 	SD_CHAIN_BUFIO,			/* Index: 15 */
2261 
2262 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2263 	SD_CHAIN_BUFIO,			/* Index: 16 */
2264 	SD_CHAIN_BUFIO,			/* Index: 17 */
2265 	SD_CHAIN_BUFIO,			/* Index: 18 */
2266 
2267 	/* Chain for USCSI commands (non-checksum targets) */
2268 	SD_CHAIN_USCSI,			/* Index: 19 */
2269 	SD_CHAIN_USCSI,			/* Index: 20 */
2270 
2271 	/* Chain for USCSI commands (checksum targets) */
2272 	SD_CHAIN_USCSI,			/* Index: 21 */
2273 	SD_CHAIN_USCSI,			/* Index: 22 */
2274 	SD_CHAIN_USCSI,			/* Index: 23 */
2275 
2276 	/* Chain for "direct" USCSI commands (all targets) */
2277 	SD_CHAIN_DIRECT,		/* Index: 24 */
2278 
2279 	/* Chain for "direct priority" USCSI commands (all targets) */
2280 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2281 
2282 	/*
2283 	 * Chain for buf IO for large sector size disk drive targets
2284 	 * with checksumming (PM enabled)
2285 	 */
2286 	SD_CHAIN_BUFIO,			/* Index: 26 */
2287 	SD_CHAIN_BUFIO,			/* Index: 27 */
2288 	SD_CHAIN_BUFIO,			/* Index: 28 */
2289 	SD_CHAIN_BUFIO,			/* Index: 29 */
2290 	SD_CHAIN_BUFIO,			/* Index: 30 */
2291 
2292 	/*
2293 	 * Chain for buf IO for large sector size disk drive targets
2294 	 * with checksumming (PM disabled)
2295 	 */
2296 	SD_CHAIN_BUFIO,			/* Index: 31 */
2297 	SD_CHAIN_BUFIO,			/* Index: 32 */
2298 	SD_CHAIN_BUFIO,			/* Index: 33 */
2299 	SD_CHAIN_BUFIO,			/* Index: 34 */
2300 };
2301 
2302 
2303 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2304 #define	SD_IS_BUFIO(xp)			\
2305 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2306 
2307 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2308 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2309 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2310 
2311 
2312 
2313 /*
2314  * Struct, array, and macros to map a specific chain to the appropriate
2315  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2316  *
2317  * The sd_chain_index_map[] array is used at attach time to set the various
2318  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2319  * chain to be used with the instance. This allows different instances to use
2320  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2321  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2322  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2323  * dynamically & without the use of locking; and (2) a layer may update the
2324  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2325  * to allow for deferred processing of an IO within the same chain from a
2326  * different execution context.
2327  */
2328 
2329 struct sd_chain_index {
2330 	int	sci_iostart_index;
2331 	int	sci_iodone_index;
2332 };
2333 
2334 static struct sd_chain_index	sd_chain_index_map[] = {
2335 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2336 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2337 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2338 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2339 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2340 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2341 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2342 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2343 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2344 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2345 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2346 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2347 
2348 };
2349 
2350 
2351 /*
2352  * The following are indexes into the sd_chain_index_map[] array.
2353  */
2354 
2355 /* un->un_buf_chain_type must be set to one of these */
2356 #define	SD_CHAIN_INFO_DISK		0
2357 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2358 #define	SD_CHAIN_INFO_RMMEDIA		2
2359 #define	SD_CHAIN_INFO_MSS_DISK		2
2360 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2361 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2362 #define	SD_CHAIN_INFO_CHKSUM		4
2363 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2364 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2365 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2366 
2367 /* un->un_uscsi_chain_type must be set to one of these */
2368 #define	SD_CHAIN_INFO_USCSI_CMD		6
2369 /* USCSI with PM disabled is the same as DIRECT */
2370 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2371 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2372 
2373 /* un->un_direct_chain_type must be set to one of these */
2374 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2375 
2376 /* un->un_priority_chain_type must be set to one of these */
2377 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2378 
2379 /* size for devid inquiries */
2380 #define	MAX_INQUIRY_SIZE		0xF0
2381 
2382 /*
2383  * Macros used by functions to pass a given buf(9S) struct along to the
2384  * next function in the layering chain for further processing.
2385  *
2386  * In the following macros, passing more than three arguments to the called
2387  * routines causes the optimizer for the SPARC compiler to stop doing tail
2388  * call elimination which results in significant performance degradation.
2389  */
2390 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2391 	((*(sd_iostart_chain[index]))(index, un, bp))
2392 
2393 #define	SD_BEGIN_IODONE(index, un, bp)	\
2394 	((*(sd_iodone_chain[index]))(index, un, bp))
2395 
2396 #define	SD_NEXT_IOSTART(index, un, bp)				\
2397 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2398 
2399 #define	SD_NEXT_IODONE(index, un, bp)				\
2400 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2401 
2402 /*
2403  *    Function: _init
2404  *
2405  * Description: This is the driver _init(9E) entry point.
2406  *
2407  * Return Code: Returns the value from mod_install(9F) or
2408  *		ddi_soft_state_init(9F) as appropriate.
2409  *
2410  *     Context: Called when driver module loaded.
2411  */
2412 
2413 int
2414 _init(void)
2415 {
2416 	int	err;
2417 
2418 	/* establish driver name from module name */
2419 	sd_label = (char *)mod_modname(&modlinkage);
2420 
2421 #ifndef XPV_HVM_DRIVER
2422 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2423 	    SD_MAXUNIT);
2424 	if (err != 0) {
2425 		return (err);
2426 	}
2427 
2428 #else /* XPV_HVM_DRIVER */
2429 	/* Remove the leading "hvm_" from the module name */
2430 	ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
2431 	sd_label += strlen("hvm_");
2432 
2433 #endif /* XPV_HVM_DRIVER */
2434 
2435 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2436 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2437 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2438 
2439 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2440 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2441 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2442 
2443 	/*
2444 	 * it's ok to init here even for fibre device
2445 	 */
2446 	sd_scsi_probe_cache_init();
2447 
2448 	sd_scsi_target_lun_init();
2449 
2450 	/*
2451 	 * Creating taskq before mod_install ensures that all callers (threads)
2452 	 * that enter the module after a successful mod_install encounter
2453 	 * a valid taskq.
2454 	 */
2455 	sd_taskq_create();
2456 
2457 	err = mod_install(&modlinkage);
2458 	if (err != 0) {
2459 		/* delete taskq if install fails */
2460 		sd_taskq_delete();
2461 
2462 		mutex_destroy(&sd_detach_mutex);
2463 		mutex_destroy(&sd_log_mutex);
2464 		mutex_destroy(&sd_label_mutex);
2465 
2466 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2467 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2468 		cv_destroy(&sd_tr.srq_inprocess_cv);
2469 
2470 		sd_scsi_probe_cache_fini();
2471 
2472 		sd_scsi_target_lun_fini();
2473 
2474 #ifndef XPV_HVM_DRIVER
2475 		ddi_soft_state_fini(&sd_state);
2476 #endif /* !XPV_HVM_DRIVER */
2477 		return (err);
2478 	}
2479 
2480 	return (err);
2481 }
2482 
2483 
2484 /*
2485  *    Function: _fini
2486  *
2487  * Description: This is the driver _fini(9E) entry point.
2488  *
2489  * Return Code: Returns the value from mod_remove(9F)
2490  *
2491  *     Context: Called when driver module is unloaded.
2492  */
2493 
2494 int
2495 _fini(void)
2496 {
2497 	int err;
2498 
2499 	if ((err = mod_remove(&modlinkage)) != 0) {
2500 		return (err);
2501 	}
2502 
2503 	sd_taskq_delete();
2504 
2505 	mutex_destroy(&sd_detach_mutex);
2506 	mutex_destroy(&sd_log_mutex);
2507 	mutex_destroy(&sd_label_mutex);
2508 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2509 
2510 	sd_scsi_probe_cache_fini();
2511 
2512 	sd_scsi_target_lun_fini();
2513 
2514 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2515 	cv_destroy(&sd_tr.srq_inprocess_cv);
2516 
2517 #ifndef XPV_HVM_DRIVER
2518 	ddi_soft_state_fini(&sd_state);
2519 #endif /* !XPV_HVM_DRIVER */
2520 
2521 	return (err);
2522 }
2523 
2524 
2525 /*
2526  *    Function: _info
2527  *
2528  * Description: This is the driver _info(9E) entry point.
2529  *
2530  *   Arguments: modinfop - pointer to the driver modinfo structure
2531  *
2532  * Return Code: Returns the value from mod_info(9F).
2533  *
2534  *     Context: Kernel thread context
2535  */
2536 
2537 int
2538 _info(struct modinfo *modinfop)
2539 {
2540 	return (mod_info(&modlinkage, modinfop));
2541 }
2542 
2543 
2544 /*
2545  * The following routines implement the driver message logging facility.
2546  * They provide component- and level- based debug output filtering.
2547  * Output may also be restricted to messages for a single instance by
2548  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2549  * to NULL, then messages for all instances are printed.
2550  *
2551  * These routines have been cloned from each other due to the language
2552  * constraints of macros and variable argument list processing.
2553  */
2554 
2555 
2556 /*
2557  *    Function: sd_log_err
2558  *
2559  * Description: This routine is called by the SD_ERROR macro for debug
2560  *		logging of error conditions.
2561  *
2562  *   Arguments: comp - driver component being logged
2563  *		dev  - pointer to driver info structure
2564  *		fmt  - error string and format to be logged
2565  */
2566 
2567 static void
2568 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2569 {
2570 	va_list		ap;
2571 	dev_info_t	*dev;
2572 
2573 	ASSERT(un != NULL);
2574 	dev = SD_DEVINFO(un);
2575 	ASSERT(dev != NULL);
2576 
2577 	/*
2578 	 * Filter messages based on the global component and level masks.
2579 	 * Also print if un matches the value of sd_debug_un, or if
2580 	 * sd_debug_un is set to NULL.
2581 	 */
2582 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2583 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2584 		mutex_enter(&sd_log_mutex);
2585 		va_start(ap, fmt);
2586 		(void) vsprintf(sd_log_buf, fmt, ap);
2587 		va_end(ap);
2588 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2589 		mutex_exit(&sd_log_mutex);
2590 	}
2591 #ifdef SD_FAULT_INJECTION
2592 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2593 	if (un->sd_injection_mask & comp) {
2594 		mutex_enter(&sd_log_mutex);
2595 		va_start(ap, fmt);
2596 		(void) vsprintf(sd_log_buf, fmt, ap);
2597 		va_end(ap);
2598 		sd_injection_log(sd_log_buf, un);
2599 		mutex_exit(&sd_log_mutex);
2600 	}
2601 #endif
2602 }
2603 
2604 
2605 /*
2606  *    Function: sd_log_info
2607  *
2608  * Description: This routine is called by the SD_INFO macro for debug
2609  *		logging of general purpose informational conditions.
2610  *
2611  *   Arguments: comp - driver component being logged
2612  *		dev  - pointer to driver info structure
2613  *		fmt  - info string and format to be logged
2614  */
2615 
2616 static void
2617 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2618 {
2619 	va_list		ap;
2620 	dev_info_t	*dev;
2621 
2622 	ASSERT(un != NULL);
2623 	dev = SD_DEVINFO(un);
2624 	ASSERT(dev != NULL);
2625 
2626 	/*
2627 	 * Filter messages based on the global component and level masks.
2628 	 * Also print if un matches the value of sd_debug_un, or if
2629 	 * sd_debug_un is set to NULL.
2630 	 */
2631 	if ((sd_component_mask & component) &&
2632 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2633 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2634 		mutex_enter(&sd_log_mutex);
2635 		va_start(ap, fmt);
2636 		(void) vsprintf(sd_log_buf, fmt, ap);
2637 		va_end(ap);
2638 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2639 		mutex_exit(&sd_log_mutex);
2640 	}
2641 #ifdef SD_FAULT_INJECTION
2642 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2643 	if (un->sd_injection_mask & component) {
2644 		mutex_enter(&sd_log_mutex);
2645 		va_start(ap, fmt);
2646 		(void) vsprintf(sd_log_buf, fmt, ap);
2647 		va_end(ap);
2648 		sd_injection_log(sd_log_buf, un);
2649 		mutex_exit(&sd_log_mutex);
2650 	}
2651 #endif
2652 }
2653 
2654 
2655 /*
2656  *    Function: sd_log_trace
2657  *
2658  * Description: This routine is called by the SD_TRACE macro for debug
2659  *		logging of trace conditions (i.e. function entry/exit).
2660  *
2661  *   Arguments: comp - driver component being logged
2662  *		dev  - pointer to driver info structure
2663  *		fmt  - trace string and format to be logged
2664  */
2665 
2666 static void
2667 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2668 {
2669 	va_list		ap;
2670 	dev_info_t	*dev;
2671 
2672 	ASSERT(un != NULL);
2673 	dev = SD_DEVINFO(un);
2674 	ASSERT(dev != NULL);
2675 
2676 	/*
2677 	 * Filter messages based on the global component and level masks.
2678 	 * Also print if un matches the value of sd_debug_un, or if
2679 	 * sd_debug_un is set to NULL.
2680 	 */
2681 	if ((sd_component_mask & component) &&
2682 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2683 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2684 		mutex_enter(&sd_log_mutex);
2685 		va_start(ap, fmt);
2686 		(void) vsprintf(sd_log_buf, fmt, ap);
2687 		va_end(ap);
2688 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2689 		mutex_exit(&sd_log_mutex);
2690 	}
2691 #ifdef SD_FAULT_INJECTION
2692 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2693 	if (un->sd_injection_mask & component) {
2694 		mutex_enter(&sd_log_mutex);
2695 		va_start(ap, fmt);
2696 		(void) vsprintf(sd_log_buf, fmt, ap);
2697 		va_end(ap);
2698 		sd_injection_log(sd_log_buf, un);
2699 		mutex_exit(&sd_log_mutex);
2700 	}
2701 #endif
2702 }
2703 
2704 
2705 /*
2706  *    Function: sdprobe
2707  *
2708  * Description: This is the driver probe(9e) entry point function.
2709  *
2710  *   Arguments: devi - opaque device info handle
2711  *
2712  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2713  *              DDI_PROBE_FAILURE: If the probe failed.
2714  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2715  *				   but may be present in the future.
2716  */
2717 
2718 static int
2719 sdprobe(dev_info_t *devi)
2720 {
2721 	struct scsi_device	*devp;
2722 	int			rval;
2723 #ifndef XPV_HVM_DRIVER
2724 	int			instance = ddi_get_instance(devi);
2725 #endif /* !XPV_HVM_DRIVER */
2726 
2727 	/*
2728 	 * if it wasn't for pln, sdprobe could actually be nulldev
2729 	 * in the "__fibre" case.
2730 	 */
2731 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2732 		return (DDI_PROBE_DONTCARE);
2733 	}
2734 
2735 	devp = ddi_get_driver_private(devi);
2736 
2737 	if (devp == NULL) {
2738 		/* Ooops... nexus driver is mis-configured... */
2739 		return (DDI_PROBE_FAILURE);
2740 	}
2741 
2742 #ifndef XPV_HVM_DRIVER
2743 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2744 		return (DDI_PROBE_PARTIAL);
2745 	}
2746 #endif /* !XPV_HVM_DRIVER */
2747 
2748 	/*
2749 	 * Call the SCSA utility probe routine to see if we actually
2750 	 * have a target at this SCSI nexus.
2751 	 */
2752 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2753 	case SCSIPROBE_EXISTS:
2754 		switch (devp->sd_inq->inq_dtype) {
2755 		case DTYPE_DIRECT:
2756 			rval = DDI_PROBE_SUCCESS;
2757 			break;
2758 		case DTYPE_RODIRECT:
2759 			/* CDs etc. Can be removable media */
2760 			rval = DDI_PROBE_SUCCESS;
2761 			break;
2762 		case DTYPE_OPTICAL:
2763 			/*
2764 			 * Rewritable optical driver HP115AA
2765 			 * Can also be removable media
2766 			 */
2767 
2768 			/*
2769 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2770 			 * pre solaris 9 sparc sd behavior is required
2771 			 *
2772 			 * If first time through and sd_dtype_optical_bind
2773 			 * has not been set in /etc/system check properties
2774 			 */
2775 
2776 			if (sd_dtype_optical_bind  < 0) {
2777 				sd_dtype_optical_bind = ddi_prop_get_int
2778 				    (DDI_DEV_T_ANY, devi, 0,
2779 				    "optical-device-bind", 1);
2780 			}
2781 
2782 			if (sd_dtype_optical_bind == 0) {
2783 				rval = DDI_PROBE_FAILURE;
2784 			} else {
2785 				rval = DDI_PROBE_SUCCESS;
2786 			}
2787 			break;
2788 
2789 		case DTYPE_NOTPRESENT:
2790 		default:
2791 			rval = DDI_PROBE_FAILURE;
2792 			break;
2793 		}
2794 		break;
2795 	default:
2796 		rval = DDI_PROBE_PARTIAL;
2797 		break;
2798 	}
2799 
2800 	/*
2801 	 * This routine checks for resource allocation prior to freeing,
2802 	 * so it will take care of the "smart probing" case where a
2803 	 * scsi_probe() may or may not have been issued and will *not*
2804 	 * free previously-freed resources.
2805 	 */
2806 	scsi_unprobe(devp);
2807 	return (rval);
2808 }
2809 
2810 
2811 /*
2812  *    Function: sdinfo
2813  *
2814  * Description: This is the driver getinfo(9e) entry point function.
2815  * 		Given the device number, return the devinfo pointer from
2816  *		the scsi_device structure or the instance number
2817  *		associated with the dev_t.
2818  *
2819  *   Arguments: dip     - pointer to device info structure
2820  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2821  *			  DDI_INFO_DEVT2INSTANCE)
2822  *		arg     - driver dev_t
2823  *		resultp - user buffer for request response
2824  *
2825  * Return Code: DDI_SUCCESS
2826  *              DDI_FAILURE
2827  */
2828 /* ARGSUSED */
2829 static int
2830 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2831 {
2832 	struct sd_lun	*un;
2833 	dev_t		dev;
2834 	int		instance;
2835 	int		error;
2836 
2837 	switch (infocmd) {
2838 	case DDI_INFO_DEVT2DEVINFO:
2839 		dev = (dev_t)arg;
2840 		instance = SDUNIT(dev);
2841 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2842 			return (DDI_FAILURE);
2843 		}
2844 		*result = (void *) SD_DEVINFO(un);
2845 		error = DDI_SUCCESS;
2846 		break;
2847 	case DDI_INFO_DEVT2INSTANCE:
2848 		dev = (dev_t)arg;
2849 		instance = SDUNIT(dev);
2850 		*result = (void *)(uintptr_t)instance;
2851 		error = DDI_SUCCESS;
2852 		break;
2853 	default:
2854 		error = DDI_FAILURE;
2855 	}
2856 	return (error);
2857 }
2858 
2859 /*
2860  *    Function: sd_prop_op
2861  *
2862  * Description: This is the driver prop_op(9e) entry point function.
2863  *		Return the number of blocks for the partition in question
2864  *		or forward the request to the property facilities.
2865  *
2866  *   Arguments: dev       - device number
2867  *		dip       - pointer to device info structure
2868  *		prop_op   - property operator
2869  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2870  *		name      - pointer to property name
2871  *		valuep    - pointer or address of the user buffer
2872  *		lengthp   - property length
2873  *
2874  * Return Code: DDI_PROP_SUCCESS
2875  *              DDI_PROP_NOT_FOUND
2876  *              DDI_PROP_UNDEFINED
2877  *              DDI_PROP_NO_MEMORY
2878  *              DDI_PROP_BUF_TOO_SMALL
2879  */
2880 
2881 static int
2882 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2883 	char *name, caddr_t valuep, int *lengthp)
2884 {
2885 	struct sd_lun	*un;
2886 
2887 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2888 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2889 		    name, valuep, lengthp));
2890 
2891 	return (cmlb_prop_op(un->un_cmlbhandle,
2892 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2893 	    SDPART(dev), (void *)SD_PATH_DIRECT));
2894 }
2895 
2896 /*
2897  * The following functions are for smart probing:
2898  * sd_scsi_probe_cache_init()
2899  * sd_scsi_probe_cache_fini()
2900  * sd_scsi_clear_probe_cache()
2901  * sd_scsi_probe_with_cache()
2902  */
2903 
2904 /*
2905  *    Function: sd_scsi_probe_cache_init
2906  *
2907  * Description: Initializes the probe response cache mutex and head pointer.
2908  *
2909  *     Context: Kernel thread context
2910  */
2911 
2912 static void
2913 sd_scsi_probe_cache_init(void)
2914 {
2915 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2916 	sd_scsi_probe_cache_head = NULL;
2917 }
2918 
2919 
2920 /*
2921  *    Function: sd_scsi_probe_cache_fini
2922  *
2923  * Description: Frees all resources associated with the probe response cache.
2924  *
2925  *     Context: Kernel thread context
2926  */
2927 
2928 static void
2929 sd_scsi_probe_cache_fini(void)
2930 {
2931 	struct sd_scsi_probe_cache *cp;
2932 	struct sd_scsi_probe_cache *ncp;
2933 
2934 	/* Clean up our smart probing linked list */
2935 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2936 		ncp = cp->next;
2937 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2938 	}
2939 	sd_scsi_probe_cache_head = NULL;
2940 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2941 }
2942 
2943 
2944 /*
2945  *    Function: sd_scsi_clear_probe_cache
2946  *
2947  * Description: This routine clears the probe response cache. This is
2948  *		done when open() returns ENXIO so that when deferred
2949  *		attach is attempted (possibly after a device has been
2950  *		turned on) we will retry the probe. Since we don't know
2951  *		which target we failed to open, we just clear the
2952  *		entire cache.
2953  *
2954  *     Context: Kernel thread context
2955  */
2956 
2957 static void
2958 sd_scsi_clear_probe_cache(void)
2959 {
2960 	struct sd_scsi_probe_cache	*cp;
2961 	int				i;
2962 
2963 	mutex_enter(&sd_scsi_probe_cache_mutex);
2964 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2965 		/*
2966 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2967 		 * force probing to be performed the next time
2968 		 * sd_scsi_probe_with_cache is called.
2969 		 */
2970 		for (i = 0; i < NTARGETS_WIDE; i++) {
2971 			cp->cache[i] = SCSIPROBE_EXISTS;
2972 		}
2973 	}
2974 	mutex_exit(&sd_scsi_probe_cache_mutex);
2975 }
2976 
2977 
2978 /*
2979  *    Function: sd_scsi_probe_with_cache
2980  *
2981  * Description: This routine implements support for a scsi device probe
2982  *		with cache. The driver maintains a cache of the target
2983  *		responses to scsi probes. If we get no response from a
2984  *		target during a probe inquiry, we remember that, and we
2985  *		avoid additional calls to scsi_probe on non-zero LUNs
2986  *		on the same target until the cache is cleared. By doing
2987  *		so we avoid the 1/4 sec selection timeout for nonzero
2988  *		LUNs. lun0 of a target is always probed.
2989  *
2990  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2991  *              waitfunc - indicates what the allocator routines should
2992  *			   do when resources are not available. This value
2993  *			   is passed on to scsi_probe() when that routine
2994  *			   is called.
2995  *
2996  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2997  *		otherwise the value returned by scsi_probe(9F).
2998  *
2999  *     Context: Kernel thread context
3000  */
3001 
3002 static int
3003 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3004 {
3005 	struct sd_scsi_probe_cache	*cp;
3006 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
3007 	int		lun, tgt;
3008 
3009 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3010 	    SCSI_ADDR_PROP_LUN, 0);
3011 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3012 	    SCSI_ADDR_PROP_TARGET, -1);
3013 
3014 	/* Make sure caching enabled and target in range */
3015 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3016 		/* do it the old way (no cache) */
3017 		return (scsi_probe(devp, waitfn));
3018 	}
3019 
3020 	mutex_enter(&sd_scsi_probe_cache_mutex);
3021 
3022 	/* Find the cache for this scsi bus instance */
3023 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3024 		if (cp->pdip == pdip) {
3025 			break;
3026 		}
3027 	}
3028 
3029 	/* If we can't find a cache for this pdip, create one */
3030 	if (cp == NULL) {
3031 		int i;
3032 
3033 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3034 		    KM_SLEEP);
3035 		cp->pdip = pdip;
3036 		cp->next = sd_scsi_probe_cache_head;
3037 		sd_scsi_probe_cache_head = cp;
3038 		for (i = 0; i < NTARGETS_WIDE; i++) {
3039 			cp->cache[i] = SCSIPROBE_EXISTS;
3040 		}
3041 	}
3042 
3043 	mutex_exit(&sd_scsi_probe_cache_mutex);
3044 
3045 	/* Recompute the cache for this target if LUN zero */
3046 	if (lun == 0) {
3047 		cp->cache[tgt] = SCSIPROBE_EXISTS;
3048 	}
3049 
3050 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3051 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3052 		return (SCSIPROBE_NORESP);
3053 	}
3054 
3055 	/* Do the actual probe; save & return the result */
3056 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3057 }
3058 
3059 
3060 /*
3061  *    Function: sd_scsi_target_lun_init
3062  *
3063  * Description: Initializes the attached lun chain mutex and head pointer.
3064  *
3065  *     Context: Kernel thread context
3066  */
3067 
3068 static void
3069 sd_scsi_target_lun_init(void)
3070 {
3071 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3072 	sd_scsi_target_lun_head = NULL;
3073 }
3074 
3075 
3076 /*
3077  *    Function: sd_scsi_target_lun_fini
3078  *
3079  * Description: Frees all resources associated with the attached lun
3080  *              chain
3081  *
3082  *     Context: Kernel thread context
3083  */
3084 
3085 static void
3086 sd_scsi_target_lun_fini(void)
3087 {
3088 	struct sd_scsi_hba_tgt_lun	*cp;
3089 	struct sd_scsi_hba_tgt_lun	*ncp;
3090 
3091 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3092 		ncp = cp->next;
3093 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3094 	}
3095 	sd_scsi_target_lun_head = NULL;
3096 	mutex_destroy(&sd_scsi_target_lun_mutex);
3097 }
3098 
3099 
3100 /*
3101  *    Function: sd_scsi_get_target_lun_count
3102  *
3103  * Description: This routine will check in the attached lun chain to see
3104  * 		how many luns are attached on the required SCSI controller
3105  * 		and target. Currently, some capabilities like tagged queue
3106  *		are supported per target based by HBA. So all luns in a
3107  *		target have the same capabilities. Based on this assumption,
3108  * 		sd should only set these capabilities once per target. This
3109  *		function is called when sd needs to decide how many luns
3110  *		already attached on a target.
3111  *
3112  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3113  *			  controller device.
3114  *              target	- The target ID on the controller's SCSI bus.
3115  *
3116  * Return Code: The number of luns attached on the required target and
3117  *		controller.
3118  *		-1 if target ID is not in parallel SCSI scope or the given
3119  * 		dip is not in the chain.
3120  *
3121  *     Context: Kernel thread context
3122  */
3123 
3124 static int
3125 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3126 {
3127 	struct sd_scsi_hba_tgt_lun	*cp;
3128 
3129 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3130 		return (-1);
3131 	}
3132 
3133 	mutex_enter(&sd_scsi_target_lun_mutex);
3134 
3135 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3136 		if (cp->pdip == dip) {
3137 			break;
3138 		}
3139 	}
3140 
3141 	mutex_exit(&sd_scsi_target_lun_mutex);
3142 
3143 	if (cp == NULL) {
3144 		return (-1);
3145 	}
3146 
3147 	return (cp->nlun[target]);
3148 }
3149 
3150 
3151 /*
3152  *    Function: sd_scsi_update_lun_on_target
3153  *
3154  * Description: This routine is used to update the attached lun chain when a
3155  *		lun is attached or detached on a target.
3156  *
3157  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3158  *                        controller device.
3159  *              target  - The target ID on the controller's SCSI bus.
3160  *		flag	- Indicate the lun is attached or detached.
3161  *
3162  *     Context: Kernel thread context
3163  */
3164 
3165 static void
3166 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3167 {
3168 	struct sd_scsi_hba_tgt_lun	*cp;
3169 
3170 	mutex_enter(&sd_scsi_target_lun_mutex);
3171 
3172 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3173 		if (cp->pdip == dip) {
3174 			break;
3175 		}
3176 	}
3177 
3178 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3179 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3180 		    KM_SLEEP);
3181 		cp->pdip = dip;
3182 		cp->next = sd_scsi_target_lun_head;
3183 		sd_scsi_target_lun_head = cp;
3184 	}
3185 
3186 	mutex_exit(&sd_scsi_target_lun_mutex);
3187 
3188 	if (cp != NULL) {
3189 		if (flag == SD_SCSI_LUN_ATTACH) {
3190 			cp->nlun[target] ++;
3191 		} else {
3192 			cp->nlun[target] --;
3193 		}
3194 	}
3195 }
3196 
3197 
3198 /*
3199  *    Function: sd_spin_up_unit
3200  *
3201  * Description: Issues the following commands to spin-up the device:
3202  *		START STOP UNIT, and INQUIRY.
3203  *
3204  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3205  *                      structure for this target.
3206  *
3207  * Return Code: 0 - success
3208  *		EIO - failure
3209  *		EACCES - reservation conflict
3210  *
3211  *     Context: Kernel thread context
3212  */
3213 
3214 static int
3215 sd_spin_up_unit(sd_ssc_t *ssc)
3216 {
3217 	size_t	resid		= 0;
3218 	int	has_conflict	= FALSE;
3219 	uchar_t *bufaddr;
3220 	int 	status;
3221 	struct sd_lun	*un;
3222 
3223 	ASSERT(ssc != NULL);
3224 	un = ssc->ssc_un;
3225 	ASSERT(un != NULL);
3226 
3227 	/*
3228 	 * Send a throwaway START UNIT command.
3229 	 *
3230 	 * If we fail on this, we don't care presently what precisely
3231 	 * is wrong.  EMC's arrays will also fail this with a check
3232 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3233 	 * we don't want to fail the attach because it may become
3234 	 * "active" later.
3235 	 * We don't know if power condition is supported or not at
3236 	 * this stage, use START STOP bit.
3237 	 */
3238 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3239 	    SD_TARGET_START, SD_PATH_DIRECT);
3240 
3241 	if (status != 0) {
3242 		if (status == EACCES)
3243 			has_conflict = TRUE;
3244 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3245 	}
3246 
3247 	/*
3248 	 * Send another INQUIRY command to the target. This is necessary for
3249 	 * non-removable media direct access devices because their INQUIRY data
3250 	 * may not be fully qualified until they are spun up (perhaps via the
3251 	 * START command above).  Note: This seems to be needed for some
3252 	 * legacy devices only.) The INQUIRY command should succeed even if a
3253 	 * Reservation Conflict is present.
3254 	 */
3255 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3256 
3257 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3258 	    != 0) {
3259 		kmem_free(bufaddr, SUN_INQSIZE);
3260 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3261 		return (EIO);
3262 	}
3263 
3264 	/*
3265 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3266 	 * Note that this routine does not return a failure here even if the
3267 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3268 	 */
3269 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3270 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3271 	}
3272 
3273 	kmem_free(bufaddr, SUN_INQSIZE);
3274 
3275 	/* If we hit a reservation conflict above, tell the caller. */
3276 	if (has_conflict == TRUE) {
3277 		return (EACCES);
3278 	}
3279 
3280 	return (0);
3281 }
3282 
3283 #ifdef _LP64
3284 /*
3285  *    Function: sd_enable_descr_sense
3286  *
3287  * Description: This routine attempts to select descriptor sense format
3288  *		using the Control mode page.  Devices that support 64 bit
3289  *		LBAs (for >2TB luns) should also implement descriptor
3290  *		sense data so we will call this function whenever we see
3291  *		a lun larger than 2TB.  If for some reason the device
3292  *		supports 64 bit LBAs but doesn't support descriptor sense
3293  *		presumably the mode select will fail.  Everything will
3294  *		continue to work normally except that we will not get
3295  *		complete sense data for commands that fail with an LBA
3296  *		larger than 32 bits.
3297  *
3298  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3299  *                      structure for this target.
3300  *
3301  *     Context: Kernel thread context only
3302  */
3303 
3304 static void
3305 sd_enable_descr_sense(sd_ssc_t *ssc)
3306 {
3307 	uchar_t			*header;
3308 	struct mode_control_scsi3 *ctrl_bufp;
3309 	size_t			buflen;
3310 	size_t			bd_len;
3311 	int			status;
3312 	struct sd_lun		*un;
3313 
3314 	ASSERT(ssc != NULL);
3315 	un = ssc->ssc_un;
3316 	ASSERT(un != NULL);
3317 
3318 	/*
3319 	 * Read MODE SENSE page 0xA, Control Mode Page
3320 	 */
3321 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3322 	    sizeof (struct mode_control_scsi3);
3323 	header = kmem_zalloc(buflen, KM_SLEEP);
3324 
3325 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3326 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3327 
3328 	if (status != 0) {
3329 		SD_ERROR(SD_LOG_COMMON, un,
3330 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3331 		goto eds_exit;
3332 	}
3333 
3334 	/*
3335 	 * Determine size of Block Descriptors in order to locate
3336 	 * the mode page data. ATAPI devices return 0, SCSI devices
3337 	 * should return MODE_BLK_DESC_LENGTH.
3338 	 */
3339 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3340 
3341 	/* Clear the mode data length field for MODE SELECT */
3342 	((struct mode_header *)header)->length = 0;
3343 
3344 	ctrl_bufp = (struct mode_control_scsi3 *)
3345 	    (header + MODE_HEADER_LENGTH + bd_len);
3346 
3347 	/*
3348 	 * If the page length is smaller than the expected value,
3349 	 * the target device doesn't support D_SENSE. Bail out here.
3350 	 */
3351 	if (ctrl_bufp->mode_page.length <
3352 	    sizeof (struct mode_control_scsi3) - 2) {
3353 		SD_ERROR(SD_LOG_COMMON, un,
3354 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3355 		goto eds_exit;
3356 	}
3357 
3358 	/*
3359 	 * Clear PS bit for MODE SELECT
3360 	 */
3361 	ctrl_bufp->mode_page.ps = 0;
3362 
3363 	/*
3364 	 * Set D_SENSE to enable descriptor sense format.
3365 	 */
3366 	ctrl_bufp->d_sense = 1;
3367 
3368 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3369 
3370 	/*
3371 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3372 	 */
3373 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3374 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3375 
3376 	if (status != 0) {
3377 		SD_INFO(SD_LOG_COMMON, un,
3378 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3379 	} else {
3380 		kmem_free(header, buflen);
3381 		return;
3382 	}
3383 
3384 eds_exit:
3385 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3386 	kmem_free(header, buflen);
3387 }
3388 
3389 /*
3390  *    Function: sd_reenable_dsense_task
3391  *
3392  * Description: Re-enable descriptor sense after device or bus reset
3393  *
3394  *     Context: Executes in a taskq() thread context
3395  */
3396 static void
3397 sd_reenable_dsense_task(void *arg)
3398 {
3399 	struct	sd_lun	*un = arg;
3400 	sd_ssc_t	*ssc;
3401 
3402 	ASSERT(un != NULL);
3403 
3404 	ssc = sd_ssc_init(un);
3405 	sd_enable_descr_sense(ssc);
3406 	sd_ssc_fini(ssc);
3407 }
3408 #endif /* _LP64 */
3409 
3410 /*
3411  *    Function: sd_set_mmc_caps
3412  *
3413  * Description: This routine determines if the device is MMC compliant and if
3414  *		the device supports CDDA via a mode sense of the CDVD
3415  *		capabilities mode page. Also checks if the device is a
3416  *		dvdram writable device.
3417  *
3418  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3419  *                      structure for this target.
3420  *
3421  *     Context: Kernel thread context only
3422  */
3423 
3424 static void
3425 sd_set_mmc_caps(sd_ssc_t *ssc)
3426 {
3427 	struct mode_header_grp2		*sense_mhp;
3428 	uchar_t				*sense_page;
3429 	caddr_t				buf;
3430 	int				bd_len;
3431 	int				status;
3432 	struct uscsi_cmd		com;
3433 	int				rtn;
3434 	uchar_t				*out_data_rw, *out_data_hd;
3435 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3436 	uchar_t				*out_data_gesn;
3437 	int				gesn_len;
3438 	struct sd_lun			*un;
3439 
3440 	ASSERT(ssc != NULL);
3441 	un = ssc->ssc_un;
3442 	ASSERT(un != NULL);
3443 
3444 	/*
3445 	 * The flags which will be set in this function are - mmc compliant,
3446 	 * dvdram writable device, cdda support. Initialize them to FALSE
3447 	 * and if a capability is detected - it will be set to TRUE.
3448 	 */
3449 	un->un_f_mmc_cap = FALSE;
3450 	un->un_f_dvdram_writable_device = FALSE;
3451 	un->un_f_cfg_cdda = FALSE;
3452 
3453 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3454 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3455 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3456 
3457 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3458 
3459 	if (status != 0) {
3460 		/* command failed; just return */
3461 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3462 		return;
3463 	}
3464 	/*
3465 	 * If the mode sense request for the CDROM CAPABILITIES
3466 	 * page (0x2A) succeeds the device is assumed to be MMC.
3467 	 */
3468 	un->un_f_mmc_cap = TRUE;
3469 
3470 	/* See if GET STATUS EVENT NOTIFICATION is supported */
3471 	if (un->un_f_mmc_gesn_polling) {
3472 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3473 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3474 
3475 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3476 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3477 
3478 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3479 
3480 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3481 			un->un_f_mmc_gesn_polling = FALSE;
3482 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3483 			    "sd_set_mmc_caps: gesn not supported "
3484 			    "%d %x %x %x %x\n", rtn,
3485 			    out_data_gesn[0], out_data_gesn[1],
3486 			    out_data_gesn[2], out_data_gesn[3]);
3487 		}
3488 
3489 		kmem_free(out_data_gesn, gesn_len);
3490 	}
3491 
3492 	/* Get to the page data */
3493 	sense_mhp = (struct mode_header_grp2 *)buf;
3494 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3495 	    sense_mhp->bdesc_length_lo;
3496 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3497 		/*
3498 		 * We did not get back the expected block descriptor
3499 		 * length so we cannot determine if the device supports
3500 		 * CDDA. However, we still indicate the device is MMC
3501 		 * according to the successful response to the page
3502 		 * 0x2A mode sense request.
3503 		 */
3504 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3505 		    "sd_set_mmc_caps: Mode Sense returned "
3506 		    "invalid block descriptor length\n");
3507 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3508 		return;
3509 	}
3510 
3511 	/* See if read CDDA is supported */
3512 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3513 	    bd_len);
3514 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3515 
3516 	/* See if writing DVD RAM is supported. */
3517 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3518 	if (un->un_f_dvdram_writable_device == TRUE) {
3519 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3520 		return;
3521 	}
3522 
3523 	/*
3524 	 * If the device presents DVD or CD capabilities in the mode
3525 	 * page, we can return here since a RRD will not have
3526 	 * these capabilities.
3527 	 */
3528 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3529 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3530 		return;
3531 	}
3532 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3533 
3534 	/*
3535 	 * If un->un_f_dvdram_writable_device is still FALSE,
3536 	 * check for a Removable Rigid Disk (RRD).  A RRD
3537 	 * device is identified by the features RANDOM_WRITABLE and
3538 	 * HARDWARE_DEFECT_MANAGEMENT.
3539 	 */
3540 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3541 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3542 
3543 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3544 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3545 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3546 
3547 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3548 
3549 	if (rtn != 0) {
3550 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3551 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3552 		return;
3553 	}
3554 
3555 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3556 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3557 
3558 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3559 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3560 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3561 
3562 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3563 
3564 	if (rtn == 0) {
3565 		/*
3566 		 * We have good information, check for random writable
3567 		 * and hardware defect features.
3568 		 */
3569 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3570 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3571 			un->un_f_dvdram_writable_device = TRUE;
3572 		}
3573 	}
3574 
3575 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3576 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3577 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3578 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3579 }
3580 
3581 /*
3582  *    Function: sd_check_for_writable_cd
3583  *
3584  * Description: This routine determines if the media in the device is
3585  *		writable or not. It uses the get configuration command (0x46)
3586  *		to determine if the media is writable
3587  *
3588  *   Arguments: un - driver soft state (unit) structure
3589  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3590  *                           chain and the normal command waitq, or
3591  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3592  *                           "direct" chain and bypass the normal command
3593  *                           waitq.
3594  *
3595  *     Context: Never called at interrupt context.
3596  */
3597 
3598 static void
3599 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3600 {
3601 	struct uscsi_cmd		com;
3602 	uchar_t				*out_data;
3603 	uchar_t				*rqbuf;
3604 	int				rtn;
3605 	uchar_t				*out_data_rw, *out_data_hd;
3606 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3607 	struct mode_header_grp2		*sense_mhp;
3608 	uchar_t				*sense_page;
3609 	caddr_t				buf;
3610 	int				bd_len;
3611 	int				status;
3612 	struct sd_lun			*un;
3613 
3614 	ASSERT(ssc != NULL);
3615 	un = ssc->ssc_un;
3616 	ASSERT(un != NULL);
3617 	ASSERT(mutex_owned(SD_MUTEX(un)));
3618 
3619 	/*
3620 	 * Initialize the writable media to false, if configuration info.
3621 	 * tells us otherwise then only we will set it.
3622 	 */
3623 	un->un_f_mmc_writable_media = FALSE;
3624 	mutex_exit(SD_MUTEX(un));
3625 
3626 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3627 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3628 
3629 	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3630 	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
3631 
3632 	if (rtn != 0)
3633 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3634 
3635 	mutex_enter(SD_MUTEX(un));
3636 	if (rtn == 0) {
3637 		/*
3638 		 * We have good information, check for writable DVD.
3639 		 */
3640 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3641 			un->un_f_mmc_writable_media = TRUE;
3642 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3643 			kmem_free(rqbuf, SENSE_LENGTH);
3644 			return;
3645 		}
3646 	}
3647 
3648 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3649 	kmem_free(rqbuf, SENSE_LENGTH);
3650 
3651 	/*
3652 	 * Determine if this is a RRD type device.
3653 	 */
3654 	mutex_exit(SD_MUTEX(un));
3655 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3656 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3657 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3658 
3659 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3660 
3661 	mutex_enter(SD_MUTEX(un));
3662 	if (status != 0) {
3663 		/* command failed; just return */
3664 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3665 		return;
3666 	}
3667 
3668 	/* Get to the page data */
3669 	sense_mhp = (struct mode_header_grp2 *)buf;
3670 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3671 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3672 		/*
3673 		 * We did not get back the expected block descriptor length so
3674 		 * we cannot check the mode page.
3675 		 */
3676 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3677 		    "sd_check_for_writable_cd: Mode Sense returned "
3678 		    "invalid block descriptor length\n");
3679 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3680 		return;
3681 	}
3682 
3683 	/*
3684 	 * If the device presents DVD or CD capabilities in the mode
3685 	 * page, we can return here since a RRD device will not have
3686 	 * these capabilities.
3687 	 */
3688 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3689 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3690 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3691 		return;
3692 	}
3693 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3694 
3695 	/*
3696 	 * If un->un_f_mmc_writable_media is still FALSE,
3697 	 * check for RRD type media.  A RRD device is identified
3698 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3699 	 */
3700 	mutex_exit(SD_MUTEX(un));
3701 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3702 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3703 
3704 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3705 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3706 	    RANDOM_WRITABLE, path_flag);
3707 
3708 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3709 	if (rtn != 0) {
3710 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3711 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3712 		mutex_enter(SD_MUTEX(un));
3713 		return;
3714 	}
3715 
3716 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3717 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3718 
3719 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3720 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3721 	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
3722 
3723 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3724 	mutex_enter(SD_MUTEX(un));
3725 	if (rtn == 0) {
3726 		/*
3727 		 * We have good information, check for random writable
3728 		 * and hardware defect features as current.
3729 		 */
3730 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3731 		    (out_data_rw[10] & 0x1) &&
3732 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3733 		    (out_data_hd[10] & 0x1)) {
3734 			un->un_f_mmc_writable_media = TRUE;
3735 		}
3736 	}
3737 
3738 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3739 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3740 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3741 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3742 }
3743 
3744 /*
3745  *    Function: sd_read_unit_properties
3746  *
3747  * Description: The following implements a property lookup mechanism.
3748  *		Properties for particular disks (keyed on vendor, model
3749  *		and rev numbers) are sought in the sd.conf file via
3750  *		sd_process_sdconf_file(), and if not found there, are
3751  *		looked for in a list hardcoded in this driver via
3752  *		sd_process_sdconf_table() Once located the properties
3753  *		are used to update the driver unit structure.
3754  *
3755  *   Arguments: un - driver soft state (unit) structure
3756  */
3757 
3758 static void
3759 sd_read_unit_properties(struct sd_lun *un)
3760 {
3761 	/*
3762 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3763 	 * the "sd-config-list" property (from the sd.conf file) or if
3764 	 * there was not a match for the inquiry vid/pid. If this event
3765 	 * occurs the static driver configuration table is searched for
3766 	 * a match.
3767 	 */
3768 	ASSERT(un != NULL);
3769 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3770 		sd_process_sdconf_table(un);
3771 	}
3772 
3773 	/* check for LSI device */
3774 	sd_is_lsi(un);
3775 
3776 
3777 }
3778 
3779 
3780 /*
3781  *    Function: sd_process_sdconf_file
3782  *
3783  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3784  *		driver's config file (ie, sd.conf) and update the driver
3785  *		soft state structure accordingly.
3786  *
3787  *   Arguments: un - driver soft state (unit) structure
3788  *
3789  * Return Code: SD_SUCCESS - The properties were successfully set according
3790  *			     to the driver configuration file.
3791  *		SD_FAILURE - The driver config list was not obtained or
3792  *			     there was no vid/pid match. This indicates that
3793  *			     the static config table should be used.
3794  *
3795  * The config file has a property, "sd-config-list". Currently we support
3796  * two kinds of formats. For both formats, the value of this property
3797  * is a list of duplets:
3798  *
3799  *  sd-config-list=
3800  *	<duplet>,
3801  *	[,<duplet>]*;
3802  *
3803  * For the improved format, where
3804  *
3805  *     <duplet>:= "<vid+pid>","<tunable-list>"
3806  *
3807  * and
3808  *
3809  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3810  *     <tunable> =        <name> : <value>
3811  *
3812  * The <vid+pid> is the string that is returned by the target device on a
3813  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3814  * to apply to all target devices with the specified <vid+pid>.
3815  *
3816  * Each <tunable> is a "<name> : <value>" pair.
3817  *
3818  * For the old format, the structure of each duplet is as follows:
3819  *
3820  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3821  *
3822  * The first entry of the duplet is the device ID string (the concatenated
3823  * vid & pid; not to be confused with a device_id).  This is defined in
3824  * the same way as in the sd_disk_table.
3825  *
3826  * The second part of the duplet is a string that identifies a
3827  * data-property-name-list. The data-property-name-list is defined as
3828  * follows:
3829  *
3830  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3831  *
3832  * The syntax of <data-property-name> depends on the <version> field.
3833  *
3834  * If version = SD_CONF_VERSION_1 we have the following syntax:
3835  *
3836  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3837  *
3838  * where the prop0 value will be used to set prop0 if bit0 set in the
3839  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3840  *
3841  */
3842 
3843 static int
3844 sd_process_sdconf_file(struct sd_lun *un)
3845 {
3846 	char	**config_list = NULL;
3847 	uint_t	nelements;
3848 	char	*vidptr;
3849 	int	vidlen;
3850 	char	*dnlist_ptr;
3851 	char	*dataname_ptr;
3852 	char	*dataname_lasts;
3853 	int	*data_list = NULL;
3854 	uint_t	data_list_len;
3855 	int	rval = SD_FAILURE;
3856 	int	i;
3857 
3858 	ASSERT(un != NULL);
3859 
3860 	/* Obtain the configuration list associated with the .conf file */
3861 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3862 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3863 	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
3864 		return (SD_FAILURE);
3865 	}
3866 
3867 	/*
3868 	 * Compare vids in each duplet to the inquiry vid - if a match is
3869 	 * made, get the data value and update the soft state structure
3870 	 * accordingly.
3871 	 *
3872 	 * Each duplet should show as a pair of strings, return SD_FAILURE
3873 	 * otherwise.
3874 	 */
3875 	if (nelements & 1) {
3876 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3877 		    "sd-config-list should show as pairs of strings.\n");
3878 		if (config_list)
3879 			ddi_prop_free(config_list);
3880 		return (SD_FAILURE);
3881 	}
3882 
3883 	for (i = 0; i < nelements; i += 2) {
3884 		/*
3885 		 * Note: The assumption here is that each vid entry is on
3886 		 * a unique line from its associated duplet.
3887 		 */
3888 		vidptr = config_list[i];
3889 		vidlen = (int)strlen(vidptr);
3890 		if ((vidlen == 0) ||
3891 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3892 			continue;
3893 		}
3894 
3895 		/*
3896 		 * dnlist contains 1 or more blank separated
3897 		 * data-property-name entries
3898 		 */
3899 		dnlist_ptr = config_list[i + 1];
3900 
3901 		if (strchr(dnlist_ptr, ':') != NULL) {
3902 			/*
3903 			 * Decode the improved format sd-config-list.
3904 			 */
3905 			sd_nvpair_str_decode(un, dnlist_ptr);
3906 		} else {
3907 			/*
3908 			 * The old format sd-config-list, loop through all
3909 			 * data-property-name entries in the
3910 			 * data-property-name-list
3911 			 * setting the properties for each.
3912 			 */
3913 			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3914 			    &dataname_lasts); dataname_ptr != NULL;
3915 			    dataname_ptr = sd_strtok_r(NULL, " \t",
3916 			    &dataname_lasts)) {
3917 				int version;
3918 
3919 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3920 				    "sd_process_sdconf_file: disk:%s, "
3921 				    "data:%s\n", vidptr, dataname_ptr);
3922 
3923 				/* Get the data list */
3924 				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3925 				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3926 				    &data_list_len) != DDI_PROP_SUCCESS) {
3927 					SD_INFO(SD_LOG_ATTACH_DETACH, un,
3928 					    "sd_process_sdconf_file: data "
3929 					    "property (%s) has no value\n",
3930 					    dataname_ptr);
3931 					continue;
3932 				}
3933 
3934 				version = data_list[0];
3935 
3936 				if (version == SD_CONF_VERSION_1) {
3937 					sd_tunables values;
3938 
3939 					/* Set the properties */
3940 					if (sd_chk_vers1_data(un, data_list[1],
3941 					    &data_list[2], data_list_len,
3942 					    dataname_ptr) == SD_SUCCESS) {
3943 						sd_get_tunables_from_conf(un,
3944 						    data_list[1], &data_list[2],
3945 						    &values);
3946 						sd_set_vers1_properties(un,
3947 						    data_list[1], &values);
3948 						rval = SD_SUCCESS;
3949 					} else {
3950 						rval = SD_FAILURE;
3951 					}
3952 				} else {
3953 					scsi_log(SD_DEVINFO(un), sd_label,
3954 					    CE_WARN, "data property %s version "
3955 					    "0x%x is invalid.",
3956 					    dataname_ptr, version);
3957 					rval = SD_FAILURE;
3958 				}
3959 				if (data_list)
3960 					ddi_prop_free(data_list);
3961 			}
3962 		}
3963 	}
3964 
3965 	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
3966 	if (config_list) {
3967 		ddi_prop_free(config_list);
3968 	}
3969 
3970 	return (rval);
3971 }
3972 
3973 /*
3974  *    Function: sd_nvpair_str_decode()
3975  *
3976  * Description: Parse the improved format sd-config-list to get
3977  *    each entry of tunable, which includes a name-value pair.
3978  *    Then call sd_set_properties() to set the property.
3979  *
3980  *   Arguments: un - driver soft state (unit) structure
3981  *    nvpair_str - the tunable list
3982  */
3983 static void
3984 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3985 {
3986 	char	*nv, *name, *value, *token;
3987 	char	*nv_lasts, *v_lasts, *x_lasts;
3988 
3989 	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3990 	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3991 		token = sd_strtok_r(nv, ":", &v_lasts);
3992 		name  = sd_strtok_r(token, " \t", &x_lasts);
3993 		token = sd_strtok_r(NULL, ":", &v_lasts);
3994 		value = sd_strtok_r(token, " \t", &x_lasts);
3995 		if (name == NULL || value == NULL) {
3996 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3997 			    "sd_nvpair_str_decode: "
3998 			    "name or value is not valid!\n");
3999 		} else {
4000 			sd_set_properties(un, name, value);
4001 		}
4002 	}
4003 }
4004 
4005 /*
4006  *    Function: sd_strtok_r()
4007  *
4008  * Description: This function uses strpbrk and strspn to break
4009  *    string into tokens on sequentially subsequent calls. Return
4010  *    NULL when no non-separator characters remain. The first
4011  *    argument is NULL for subsequent calls.
4012  */
4013 static char *
4014 sd_strtok_r(char *string, const char *sepset, char **lasts)
4015 {
4016 	char	*q, *r;
4017 
4018 	/* First or subsequent call */
4019 	if (string == NULL)
4020 		string = *lasts;
4021 
4022 	if (string == NULL)
4023 		return (NULL);
4024 
4025 	/* Skip leading separators */
4026 	q = string + strspn(string, sepset);
4027 
4028 	if (*q == '\0')
4029 		return (NULL);
4030 
4031 	if ((r = strpbrk(q, sepset)) == NULL)
4032 		*lasts = NULL;
4033 	else {
4034 		*r = '\0';
4035 		*lasts = r + 1;
4036 	}
4037 	return (q);
4038 }
4039 
4040 /*
4041  *    Function: sd_set_properties()
4042  *
4043  * Description: Set device properties based on the improved
4044  *    format sd-config-list.
4045  *
4046  *   Arguments: un - driver soft state (unit) structure
4047  *    name  - supported tunable name
4048  *    value - tunable value
4049  */
4050 static void
4051 sd_set_properties(struct sd_lun *un, char *name, char *value)
4052 {
4053 	char	*endptr = NULL;
4054 	long	val = 0;
4055 
4056 	if (strcasecmp(name, "cache-nonvolatile") == 0) {
4057 		if (strcasecmp(value, "true") == 0) {
4058 			un->un_f_suppress_cache_flush = TRUE;
4059 		} else if (strcasecmp(value, "false") == 0) {
4060 			un->un_f_suppress_cache_flush = FALSE;
4061 		} else {
4062 			goto value_invalid;
4063 		}
4064 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4065 		    "suppress_cache_flush flag set to %d\n",
4066 		    un->un_f_suppress_cache_flush);
4067 		return;
4068 	}
4069 
4070 	if (strcasecmp(name, "controller-type") == 0) {
4071 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4072 			un->un_ctype = val;
4073 		} else {
4074 			goto value_invalid;
4075 		}
4076 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4077 		    "ctype set to %d\n", un->un_ctype);
4078 		return;
4079 	}
4080 
4081 	if (strcasecmp(name, "delay-busy") == 0) {
4082 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4083 			un->un_busy_timeout = drv_usectohz(val / 1000);
4084 		} else {
4085 			goto value_invalid;
4086 		}
4087 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4088 		    "busy_timeout set to %d\n", un->un_busy_timeout);
4089 		return;
4090 	}
4091 
4092 	if (strcasecmp(name, "disksort") == 0) {
4093 		if (strcasecmp(value, "true") == 0) {
4094 			un->un_f_disksort_disabled = FALSE;
4095 		} else if (strcasecmp(value, "false") == 0) {
4096 			un->un_f_disksort_disabled = TRUE;
4097 		} else {
4098 			goto value_invalid;
4099 		}
4100 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4101 		    "disksort disabled flag set to %d\n",
4102 		    un->un_f_disksort_disabled);
4103 		return;
4104 	}
4105 
4106 	if (strcasecmp(name, "power-condition") == 0) {
4107 		if (strcasecmp(value, "true") == 0) {
4108 			un->un_f_power_condition_disabled = FALSE;
4109 		} else if (strcasecmp(value, "false") == 0) {
4110 			un->un_f_power_condition_disabled = TRUE;
4111 		} else {
4112 			goto value_invalid;
4113 		}
4114 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4115 		    "power condition disabled flag set to %d\n",
4116 		    un->un_f_power_condition_disabled);
4117 		return;
4118 	}
4119 
4120 	if (strcasecmp(name, "timeout-releasereservation") == 0) {
4121 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4122 			un->un_reserve_release_time = val;
4123 		} else {
4124 			goto value_invalid;
4125 		}
4126 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4127 		    "reservation release timeout set to %d\n",
4128 		    un->un_reserve_release_time);
4129 		return;
4130 	}
4131 
4132 	if (strcasecmp(name, "reset-lun") == 0) {
4133 		if (strcasecmp(value, "true") == 0) {
4134 			un->un_f_lun_reset_enabled = TRUE;
4135 		} else if (strcasecmp(value, "false") == 0) {
4136 			un->un_f_lun_reset_enabled = FALSE;
4137 		} else {
4138 			goto value_invalid;
4139 		}
4140 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4141 		    "lun reset enabled flag set to %d\n",
4142 		    un->un_f_lun_reset_enabled);
4143 		return;
4144 	}
4145 
4146 	if (strcasecmp(name, "retries-busy") == 0) {
4147 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4148 			un->un_busy_retry_count = val;
4149 		} else {
4150 			goto value_invalid;
4151 		}
4152 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4153 		    "busy retry count set to %d\n", un->un_busy_retry_count);
4154 		return;
4155 	}
4156 
4157 	if (strcasecmp(name, "retries-timeout") == 0) {
4158 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4159 			un->un_retry_count = val;
4160 		} else {
4161 			goto value_invalid;
4162 		}
4163 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4164 		    "timeout retry count set to %d\n", un->un_retry_count);
4165 		return;
4166 	}
4167 
4168 	if (strcasecmp(name, "retries-notready") == 0) {
4169 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4170 			un->un_notready_retry_count = val;
4171 		} else {
4172 			goto value_invalid;
4173 		}
4174 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4175 		    "notready retry count set to %d\n",
4176 		    un->un_notready_retry_count);
4177 		return;
4178 	}
4179 
4180 	if (strcasecmp(name, "retries-reset") == 0) {
4181 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4182 			un->un_reset_retry_count = val;
4183 		} else {
4184 			goto value_invalid;
4185 		}
4186 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4187 		    "reset retry count set to %d\n",
4188 		    un->un_reset_retry_count);
4189 		return;
4190 	}
4191 
4192 	if (strcasecmp(name, "throttle-max") == 0) {
4193 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4194 			un->un_saved_throttle = un->un_throttle = val;
4195 		} else {
4196 			goto value_invalid;
4197 		}
4198 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4199 		    "throttle set to %d\n", un->un_throttle);
4200 	}
4201 
4202 	if (strcasecmp(name, "throttle-min") == 0) {
4203 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4204 			un->un_min_throttle = val;
4205 		} else {
4206 			goto value_invalid;
4207 		}
4208 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4209 		    "min throttle set to %d\n", un->un_min_throttle);
4210 	}
4211 
4212 	if (strcasecmp(name, "rmw-type") == 0) {
4213 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4214 			un->un_f_rmw_type = val;
4215 		} else {
4216 			goto value_invalid;
4217 		}
4218 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4219 		    "RMW type set to %d\n", un->un_f_rmw_type);
4220 	}
4221 
4222 	/*
4223 	 * Validate the throttle values.
4224 	 * If any of the numbers are invalid, set everything to defaults.
4225 	 */
4226 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4227 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4228 	    (un->un_min_throttle > un->un_throttle)) {
4229 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4230 		un->un_min_throttle = sd_min_throttle;
4231 	}
4232 
4233 	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4234 		if (strcasecmp(value, "true") == 0) {
4235 			un->un_f_mmc_gesn_polling = TRUE;
4236 		} else if (strcasecmp(value, "false") == 0) {
4237 			un->un_f_mmc_gesn_polling = FALSE;
4238 		} else {
4239 			goto value_invalid;
4240 		}
4241 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4242 		    "mmc-gesn-polling set to %d\n",
4243 		    un->un_f_mmc_gesn_polling);
4244 	}
4245 
4246 	return;
4247 
4248 value_invalid:
4249 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4250 	    "value of prop %s is invalid\n", name);
4251 }
4252 
4253 /*
4254  *    Function: sd_get_tunables_from_conf()
4255  *
4256  *
4257  *    This function reads the data list from the sd.conf file and pulls
4258  *    the values that can have numeric values as arguments and places
4259  *    the values in the appropriate sd_tunables member.
4260  *    Since the order of the data list members varies across platforms
4261  *    This function reads them from the data list in a platform specific
4262  *    order and places them into the correct sd_tunable member that is
4263  *    consistent across all platforms.
4264  */
4265 static void
4266 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4267     sd_tunables *values)
4268 {
4269 	int i;
4270 	int mask;
4271 
4272 	bzero(values, sizeof (sd_tunables));
4273 
4274 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4275 
4276 		mask = 1 << i;
4277 		if (mask > flags) {
4278 			break;
4279 		}
4280 
4281 		switch (mask & flags) {
4282 		case 0:	/* This mask bit not set in flags */
4283 			continue;
4284 		case SD_CONF_BSET_THROTTLE:
4285 			values->sdt_throttle = data_list[i];
4286 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4287 			    "sd_get_tunables_from_conf: throttle = %d\n",
4288 			    values->sdt_throttle);
4289 			break;
4290 		case SD_CONF_BSET_CTYPE:
4291 			values->sdt_ctype = data_list[i];
4292 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4293 			    "sd_get_tunables_from_conf: ctype = %d\n",
4294 			    values->sdt_ctype);
4295 			break;
4296 		case SD_CONF_BSET_NRR_COUNT:
4297 			values->sdt_not_rdy_retries = data_list[i];
4298 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4299 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4300 			    values->sdt_not_rdy_retries);
4301 			break;
4302 		case SD_CONF_BSET_BSY_RETRY_COUNT:
4303 			values->sdt_busy_retries = data_list[i];
4304 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4305 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
4306 			    values->sdt_busy_retries);
4307 			break;
4308 		case SD_CONF_BSET_RST_RETRIES:
4309 			values->sdt_reset_retries = data_list[i];
4310 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4311 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
4312 			    values->sdt_reset_retries);
4313 			break;
4314 		case SD_CONF_BSET_RSV_REL_TIME:
4315 			values->sdt_reserv_rel_time = data_list[i];
4316 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4317 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4318 			    values->sdt_reserv_rel_time);
4319 			break;
4320 		case SD_CONF_BSET_MIN_THROTTLE:
4321 			values->sdt_min_throttle = data_list[i];
4322 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4323 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
4324 			    values->sdt_min_throttle);
4325 			break;
4326 		case SD_CONF_BSET_DISKSORT_DISABLED:
4327 			values->sdt_disk_sort_dis = data_list[i];
4328 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4329 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4330 			    values->sdt_disk_sort_dis);
4331 			break;
4332 		case SD_CONF_BSET_LUN_RESET_ENABLED:
4333 			values->sdt_lun_reset_enable = data_list[i];
4334 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4335 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
4336 			    "\n", values->sdt_lun_reset_enable);
4337 			break;
4338 		case SD_CONF_BSET_CACHE_IS_NV:
4339 			values->sdt_suppress_cache_flush = data_list[i];
4340 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4341 			    "sd_get_tunables_from_conf: \
4342 			    suppress_cache_flush = %d"
4343 			    "\n", values->sdt_suppress_cache_flush);
4344 			break;
4345 		case SD_CONF_BSET_PC_DISABLED:
4346 			values->sdt_disk_sort_dis = data_list[i];
4347 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4348 			    "sd_get_tunables_from_conf: power_condition_dis = "
4349 			    "%d\n", values->sdt_power_condition_dis);
4350 			break;
4351 		}
4352 	}
4353 }
4354 
4355 /*
4356  *    Function: sd_process_sdconf_table
4357  *
4358  * Description: Search the static configuration table for a match on the
4359  *		inquiry vid/pid and update the driver soft state structure
4360  *		according to the table property values for the device.
4361  *
4362  *		The form of a configuration table entry is:
4363  *		  <vid+pid>,<flags>,<property-data>
4364  *		  "SEAGATE ST42400N",1,0x40000,
4365  *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4366  *
4367  *   Arguments: un - driver soft state (unit) structure
4368  */
4369 
4370 static void
4371 sd_process_sdconf_table(struct sd_lun *un)
4372 {
4373 	char	*id = NULL;
4374 	int	table_index;
4375 	int	idlen;
4376 
4377 	ASSERT(un != NULL);
4378 	for (table_index = 0; table_index < sd_disk_table_size;
4379 	    table_index++) {
4380 		id = sd_disk_table[table_index].device_id;
4381 		idlen = strlen(id);
4382 		if (idlen == 0) {
4383 			continue;
4384 		}
4385 
4386 		/*
4387 		 * The static configuration table currently does not
4388 		 * implement version 10 properties. Additionally,
4389 		 * multiple data-property-name entries are not
4390 		 * implemented in the static configuration table.
4391 		 */
4392 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4393 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4394 			    "sd_process_sdconf_table: disk %s\n", id);
4395 			sd_set_vers1_properties(un,
4396 			    sd_disk_table[table_index].flags,
4397 			    sd_disk_table[table_index].properties);
4398 			break;
4399 		}
4400 	}
4401 }
4402 
4403 
4404 /*
4405  *    Function: sd_sdconf_id_match
4406  *
4407  * Description: This local function implements a case sensitive vid/pid
4408  *		comparison as well as the boundary cases of wild card and
4409  *		multiple blanks.
4410  *
4411  *		Note: An implicit assumption made here is that the scsi
4412  *		inquiry structure will always keep the vid, pid and
4413  *		revision strings in consecutive sequence, so they can be
4414  *		read as a single string. If this assumption is not the
4415  *		case, a separate string, to be used for the check, needs
4416  *		to be built with these strings concatenated.
4417  *
4418  *   Arguments: un - driver soft state (unit) structure
4419  *		id - table or config file vid/pid
4420  *		idlen  - length of the vid/pid (bytes)
4421  *
4422  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4423  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4424  */
4425 
4426 static int
4427 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4428 {
4429 	struct scsi_inquiry	*sd_inq;
4430 	int 			rval = SD_SUCCESS;
4431 
4432 	ASSERT(un != NULL);
4433 	sd_inq = un->un_sd->sd_inq;
4434 	ASSERT(id != NULL);
4435 
4436 	/*
4437 	 * We use the inq_vid as a pointer to a buffer containing the
4438 	 * vid and pid and use the entire vid/pid length of the table
4439 	 * entry for the comparison. This works because the inq_pid
4440 	 * data member follows inq_vid in the scsi_inquiry structure.
4441 	 */
4442 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4443 		/*
4444 		 * The user id string is compared to the inquiry vid/pid
4445 		 * using a case insensitive comparison and ignoring
4446 		 * multiple spaces.
4447 		 */
4448 		rval = sd_blank_cmp(un, id, idlen);
4449 		if (rval != SD_SUCCESS) {
4450 			/*
4451 			 * User id strings that start and end with a "*"
4452 			 * are a special case. These do not have a
4453 			 * specific vendor, and the product string can
4454 			 * appear anywhere in the 16 byte PID portion of
4455 			 * the inquiry data. This is a simple strstr()
4456 			 * type search for the user id in the inquiry data.
4457 			 */
4458 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4459 				char	*pidptr = &id[1];
4460 				int	i;
4461 				int	j;
4462 				int	pidstrlen = idlen - 2;
4463 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
4464 				    pidstrlen;
4465 
4466 				if (j < 0) {
4467 					return (SD_FAILURE);
4468 				}
4469 				for (i = 0; i < j; i++) {
4470 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4471 					    pidptr, pidstrlen) == 0) {
4472 						rval = SD_SUCCESS;
4473 						break;
4474 					}
4475 				}
4476 			}
4477 		}
4478 	}
4479 	return (rval);
4480 }
4481 
4482 
4483 /*
4484  *    Function: sd_blank_cmp
4485  *
4486  * Description: If the id string starts and ends with a space, treat
4487  *		multiple consecutive spaces as equivalent to a single
4488  *		space. For example, this causes a sd_disk_table entry
4489  *		of " NEC CDROM " to match a device's id string of
4490  *		"NEC       CDROM".
4491  *
4492  *		Note: The success exit condition for this routine is if
4493  *		the pointer to the table entry is '\0' and the cnt of
4494  *		the inquiry length is zero. This will happen if the inquiry
4495  *		string returned by the device is padded with spaces to be
4496  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4497  *		SCSI spec states that the inquiry string is to be padded with
4498  *		spaces.
4499  *
4500  *   Arguments: un - driver soft state (unit) structure
4501  *		id - table or config file vid/pid
4502  *		idlen  - length of the vid/pid (bytes)
4503  *
4504  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4505  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4506  */
4507 
4508 static int
4509 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4510 {
4511 	char		*p1;
4512 	char		*p2;
4513 	int		cnt;
4514 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4515 	    sizeof (SD_INQUIRY(un)->inq_pid);
4516 
4517 	ASSERT(un != NULL);
4518 	p2 = un->un_sd->sd_inq->inq_vid;
4519 	ASSERT(id != NULL);
4520 	p1 = id;
4521 
4522 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4523 		/*
4524 		 * Note: string p1 is terminated by a NUL but string p2
4525 		 * isn't.  The end of p2 is determined by cnt.
4526 		 */
4527 		for (;;) {
4528 			/* skip over any extra blanks in both strings */
4529 			while ((*p1 != '\0') && (*p1 == ' ')) {
4530 				p1++;
4531 			}
4532 			while ((cnt != 0) && (*p2 == ' ')) {
4533 				p2++;
4534 				cnt--;
4535 			}
4536 
4537 			/* compare the two strings */
4538 			if ((cnt == 0) ||
4539 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4540 				break;
4541 			}
4542 			while ((cnt > 0) &&
4543 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4544 				p1++;
4545 				p2++;
4546 				cnt--;
4547 			}
4548 		}
4549 	}
4550 
4551 	/* return SD_SUCCESS if both strings match */
4552 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4553 }
4554 
4555 
4556 /*
4557  *    Function: sd_chk_vers1_data
4558  *
4559  * Description: Verify the version 1 device properties provided by the
4560  *		user via the configuration file
4561  *
4562  *   Arguments: un	     - driver soft state (unit) structure
4563  *		flags	     - integer mask indicating properties to be set
4564  *		prop_list    - integer list of property values
4565  *		list_len     - number of the elements
4566  *
4567  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4568  *		SD_FAILURE - Indicates the user provided data is invalid
4569  */
4570 
4571 static int
4572 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4573     int list_len, char *dataname_ptr)
4574 {
4575 	int i;
4576 	int mask = 1;
4577 	int index = 0;
4578 
4579 	ASSERT(un != NULL);
4580 
4581 	/* Check for a NULL property name and list */
4582 	if (dataname_ptr == NULL) {
4583 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4584 		    "sd_chk_vers1_data: NULL data property name.");
4585 		return (SD_FAILURE);
4586 	}
4587 	if (prop_list == NULL) {
4588 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4589 		    "sd_chk_vers1_data: %s NULL data property list.",
4590 		    dataname_ptr);
4591 		return (SD_FAILURE);
4592 	}
4593 
4594 	/* Display a warning if undefined bits are set in the flags */
4595 	if (flags & ~SD_CONF_BIT_MASK) {
4596 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4597 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4598 		    "Properties not set.",
4599 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4600 		return (SD_FAILURE);
4601 	}
4602 
4603 	/*
4604 	 * Verify the length of the list by identifying the highest bit set
4605 	 * in the flags and validating that the property list has a length
4606 	 * up to the index of this bit.
4607 	 */
4608 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4609 		if (flags & mask) {
4610 			index++;
4611 		}
4612 		mask = 1 << i;
4613 	}
4614 	if (list_len < (index + 2)) {
4615 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4616 		    "sd_chk_vers1_data: "
4617 		    "Data property list %s size is incorrect. "
4618 		    "Properties not set.", dataname_ptr);
4619 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4620 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4621 		return (SD_FAILURE);
4622 	}
4623 	return (SD_SUCCESS);
4624 }
4625 
4626 
4627 /*
4628  *    Function: sd_set_vers1_properties
4629  *
4630  * Description: Set version 1 device properties based on a property list
4631  *		retrieved from the driver configuration file or static
4632  *		configuration table. Version 1 properties have the format:
4633  *
4634  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4635  *
4636  *		where the prop0 value will be used to set prop0 if bit0
4637  *		is set in the flags
4638  *
4639  *   Arguments: un	     - driver soft state (unit) structure
4640  *		flags	     - integer mask indicating properties to be set
4641  *		prop_list    - integer list of property values
4642  */
4643 
4644 static void
4645 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4646 {
4647 	ASSERT(un != NULL);
4648 
4649 	/*
4650 	 * Set the flag to indicate cache is to be disabled. An attempt
4651 	 * to disable the cache via sd_cache_control() will be made
4652 	 * later during attach once the basic initialization is complete.
4653 	 */
4654 	if (flags & SD_CONF_BSET_NOCACHE) {
4655 		un->un_f_opt_disable_cache = TRUE;
4656 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4657 		    "sd_set_vers1_properties: caching disabled flag set\n");
4658 	}
4659 
4660 	/* CD-specific configuration parameters */
4661 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4662 		un->un_f_cfg_playmsf_bcd = TRUE;
4663 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4664 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4665 	}
4666 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4667 		un->un_f_cfg_readsub_bcd = TRUE;
4668 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4669 		    "sd_set_vers1_properties: readsub_bcd set\n");
4670 	}
4671 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4672 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4673 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4674 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4675 	}
4676 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4677 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4678 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4679 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4680 	}
4681 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4682 		un->un_f_cfg_no_read_header = TRUE;
4683 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4684 		    "sd_set_vers1_properties: no_read_header set\n");
4685 	}
4686 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4687 		un->un_f_cfg_read_cd_xd4 = TRUE;
4688 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4689 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4690 	}
4691 
4692 	/* Support for devices which do not have valid/unique serial numbers */
4693 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4694 		un->un_f_opt_fab_devid = TRUE;
4695 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4696 		    "sd_set_vers1_properties: fab_devid bit set\n");
4697 	}
4698 
4699 	/* Support for user throttle configuration */
4700 	if (flags & SD_CONF_BSET_THROTTLE) {
4701 		ASSERT(prop_list != NULL);
4702 		un->un_saved_throttle = un->un_throttle =
4703 		    prop_list->sdt_throttle;
4704 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4705 		    "sd_set_vers1_properties: throttle set to %d\n",
4706 		    prop_list->sdt_throttle);
4707 	}
4708 
4709 	/* Set the per disk retry count according to the conf file or table. */
4710 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4711 		ASSERT(prop_list != NULL);
4712 		if (prop_list->sdt_not_rdy_retries) {
4713 			un->un_notready_retry_count =
4714 			    prop_list->sdt_not_rdy_retries;
4715 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4716 			    "sd_set_vers1_properties: not ready retry count"
4717 			    " set to %d\n", un->un_notready_retry_count);
4718 		}
4719 	}
4720 
4721 	/* The controller type is reported for generic disk driver ioctls */
4722 	if (flags & SD_CONF_BSET_CTYPE) {
4723 		ASSERT(prop_list != NULL);
4724 		switch (prop_list->sdt_ctype) {
4725 		case CTYPE_CDROM:
4726 			un->un_ctype = prop_list->sdt_ctype;
4727 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4728 			    "sd_set_vers1_properties: ctype set to "
4729 			    "CTYPE_CDROM\n");
4730 			break;
4731 		case CTYPE_CCS:
4732 			un->un_ctype = prop_list->sdt_ctype;
4733 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4734 			    "sd_set_vers1_properties: ctype set to "
4735 			    "CTYPE_CCS\n");
4736 			break;
4737 		case CTYPE_ROD:		/* RW optical */
4738 			un->un_ctype = prop_list->sdt_ctype;
4739 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4740 			    "sd_set_vers1_properties: ctype set to "
4741 			    "CTYPE_ROD\n");
4742 			break;
4743 		default:
4744 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4745 			    "sd_set_vers1_properties: Could not set "
4746 			    "invalid ctype value (%d)",
4747 			    prop_list->sdt_ctype);
4748 		}
4749 	}
4750 
4751 	/* Purple failover timeout */
4752 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4753 		ASSERT(prop_list != NULL);
4754 		un->un_busy_retry_count =
4755 		    prop_list->sdt_busy_retries;
4756 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4757 		    "sd_set_vers1_properties: "
4758 		    "busy retry count set to %d\n",
4759 		    un->un_busy_retry_count);
4760 	}
4761 
4762 	/* Purple reset retry count */
4763 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4764 		ASSERT(prop_list != NULL);
4765 		un->un_reset_retry_count =
4766 		    prop_list->sdt_reset_retries;
4767 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4768 		    "sd_set_vers1_properties: "
4769 		    "reset retry count set to %d\n",
4770 		    un->un_reset_retry_count);
4771 	}
4772 
4773 	/* Purple reservation release timeout */
4774 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4775 		ASSERT(prop_list != NULL);
4776 		un->un_reserve_release_time =
4777 		    prop_list->sdt_reserv_rel_time;
4778 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4779 		    "sd_set_vers1_properties: "
4780 		    "reservation release timeout set to %d\n",
4781 		    un->un_reserve_release_time);
4782 	}
4783 
4784 	/*
4785 	 * Driver flag telling the driver to verify that no commands are pending
4786 	 * for a device before issuing a Test Unit Ready. This is a workaround
4787 	 * for a firmware bug in some Seagate eliteI drives.
4788 	 */
4789 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4790 		un->un_f_cfg_tur_check = TRUE;
4791 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4792 		    "sd_set_vers1_properties: tur queue check set\n");
4793 	}
4794 
4795 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4796 		un->un_min_throttle = prop_list->sdt_min_throttle;
4797 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4798 		    "sd_set_vers1_properties: min throttle set to %d\n",
4799 		    un->un_min_throttle);
4800 	}
4801 
4802 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4803 		un->un_f_disksort_disabled =
4804 		    (prop_list->sdt_disk_sort_dis != 0) ?
4805 		    TRUE : FALSE;
4806 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4807 		    "sd_set_vers1_properties: disksort disabled "
4808 		    "flag set to %d\n",
4809 		    prop_list->sdt_disk_sort_dis);
4810 	}
4811 
4812 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4813 		un->un_f_lun_reset_enabled =
4814 		    (prop_list->sdt_lun_reset_enable != 0) ?
4815 		    TRUE : FALSE;
4816 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4817 		    "sd_set_vers1_properties: lun reset enabled "
4818 		    "flag set to %d\n",
4819 		    prop_list->sdt_lun_reset_enable);
4820 	}
4821 
4822 	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4823 		un->un_f_suppress_cache_flush =
4824 		    (prop_list->sdt_suppress_cache_flush != 0) ?
4825 		    TRUE : FALSE;
4826 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4827 		    "sd_set_vers1_properties: suppress_cache_flush "
4828 		    "flag set to %d\n",
4829 		    prop_list->sdt_suppress_cache_flush);
4830 	}
4831 
4832 	if (flags & SD_CONF_BSET_PC_DISABLED) {
4833 		un->un_f_power_condition_disabled =
4834 		    (prop_list->sdt_power_condition_dis != 0) ?
4835 		    TRUE : FALSE;
4836 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4837 		    "sd_set_vers1_properties: power_condition_disabled "
4838 		    "flag set to %d\n",
4839 		    prop_list->sdt_power_condition_dis);
4840 	}
4841 
4842 	/*
4843 	 * Validate the throttle values.
4844 	 * If any of the numbers are invalid, set everything to defaults.
4845 	 */
4846 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4847 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4848 	    (un->un_min_throttle > un->un_throttle)) {
4849 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4850 		un->un_min_throttle = sd_min_throttle;
4851 	}
4852 }
4853 
4854 /*
4855  *   Function: sd_is_lsi()
4856  *
4857  *   Description: Check for lsi devices, step through the static device
4858  *	table to match vid/pid.
4859  *
4860  *   Args: un - ptr to sd_lun
4861  *
4862  *   Notes:  When creating new LSI property, need to add the new LSI property
4863  *		to this function.
4864  */
4865 static void
4866 sd_is_lsi(struct sd_lun *un)
4867 {
4868 	char	*id = NULL;
4869 	int	table_index;
4870 	int	idlen;
4871 	void	*prop;
4872 
4873 	ASSERT(un != NULL);
4874 	for (table_index = 0; table_index < sd_disk_table_size;
4875 	    table_index++) {
4876 		id = sd_disk_table[table_index].device_id;
4877 		idlen = strlen(id);
4878 		if (idlen == 0) {
4879 			continue;
4880 		}
4881 
4882 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4883 			prop = sd_disk_table[table_index].properties;
4884 			if (prop == &lsi_properties ||
4885 			    prop == &lsi_oem_properties ||
4886 			    prop == &lsi_properties_scsi ||
4887 			    prop == &symbios_properties) {
4888 				un->un_f_cfg_is_lsi = TRUE;
4889 			}
4890 			break;
4891 		}
4892 	}
4893 }
4894 
4895 /*
4896  *    Function: sd_get_physical_geometry
4897  *
4898  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4899  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4900  *		target, and use this information to initialize the physical
4901  *		geometry cache specified by pgeom_p.
4902  *
4903  *		MODE SENSE is an optional command, so failure in this case
4904  *		does not necessarily denote an error. We want to use the
4905  *		MODE SENSE commands to derive the physical geometry of the
4906  *		device, but if either command fails, the logical geometry is
4907  *		used as the fallback for disk label geometry in cmlb.
4908  *
4909  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4910  *		have already been initialized for the current target and
4911  *		that the current values be passed as args so that we don't
4912  *		end up ever trying to use -1 as a valid value. This could
4913  *		happen if either value is reset while we're not holding
4914  *		the mutex.
4915  *
4916  *   Arguments: un - driver soft state (unit) structure
4917  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4918  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4919  *			to use the USCSI "direct" chain and bypass the normal
4920  *			command waitq.
4921  *
4922  *     Context: Kernel thread only (can sleep).
4923  */
4924 
4925 static int
4926 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4927 	diskaddr_t capacity, int lbasize, int path_flag)
4928 {
4929 	struct	mode_format	*page3p;
4930 	struct	mode_geometry	*page4p;
4931 	struct	mode_header	*headerp;
4932 	int	sector_size;
4933 	int	nsect;
4934 	int	nhead;
4935 	int	ncyl;
4936 	int	intrlv;
4937 	int	spc;
4938 	diskaddr_t	modesense_capacity;
4939 	int	rpm;
4940 	int	bd_len;
4941 	int	mode_header_length;
4942 	uchar_t	*p3bufp;
4943 	uchar_t	*p4bufp;
4944 	int	cdbsize;
4945 	int 	ret = EIO;
4946 	sd_ssc_t *ssc;
4947 	int	status;
4948 
4949 	ASSERT(un != NULL);
4950 
4951 	if (lbasize == 0) {
4952 		if (ISCD(un)) {
4953 			lbasize = 2048;
4954 		} else {
4955 			lbasize = un->un_sys_blocksize;
4956 		}
4957 	}
4958 	pgeom_p->g_secsize = (unsigned short)lbasize;
4959 
4960 	/*
4961 	 * If the unit is a cd/dvd drive MODE SENSE page three
4962 	 * and MODE SENSE page four are reserved (see SBC spec
4963 	 * and MMC spec). To prevent soft errors just return
4964 	 * using the default LBA size.
4965 	 */
4966 	if (ISCD(un))
4967 		return (ret);
4968 
4969 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4970 
4971 	/*
4972 	 * Retrieve MODE SENSE page 3 - Format Device Page
4973 	 */
4974 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4975 	ssc = sd_ssc_init(un);
4976 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
4977 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
4978 	if (status != 0) {
4979 		SD_ERROR(SD_LOG_COMMON, un,
4980 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
4981 		goto page3_exit;
4982 	}
4983 
4984 	/*
4985 	 * Determine size of Block Descriptors in order to locate the mode
4986 	 * page data.  ATAPI devices return 0, SCSI devices should return
4987 	 * MODE_BLK_DESC_LENGTH.
4988 	 */
4989 	headerp = (struct mode_header *)p3bufp;
4990 	if (un->un_f_cfg_is_atapi == TRUE) {
4991 		struct mode_header_grp2 *mhp =
4992 		    (struct mode_header_grp2 *)headerp;
4993 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
4994 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4995 	} else {
4996 		mode_header_length = MODE_HEADER_LENGTH;
4997 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4998 	}
4999 
5000 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5001 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5002 		    "sd_get_physical_geometry: received unexpected bd_len "
5003 		    "of %d, page3\n", bd_len);
5004 		status = EIO;
5005 		goto page3_exit;
5006 	}
5007 
5008 	page3p = (struct mode_format *)
5009 	    ((caddr_t)headerp + mode_header_length + bd_len);
5010 
5011 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5012 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5013 		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
5014 		    "%d\n", page3p->mode_page.code);
5015 		status = EIO;
5016 		goto page3_exit;
5017 	}
5018 
5019 	/*
5020 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5021 	 * complete successfully; otherwise, revert to the logical geometry.
5022 	 * So, we need to save everything in temporary variables.
5023 	 */
5024 	sector_size = BE_16(page3p->data_bytes_sect);
5025 
5026 	/*
5027 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5028 	 */
5029 	if (sector_size == 0) {
5030 		sector_size = un->un_sys_blocksize;
5031 	} else {
5032 		sector_size &= ~(un->un_sys_blocksize - 1);
5033 	}
5034 
5035 	nsect  = BE_16(page3p->sect_track);
5036 	intrlv = BE_16(page3p->interleave);
5037 
5038 	SD_INFO(SD_LOG_COMMON, un,
5039 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5040 	SD_INFO(SD_LOG_COMMON, un,
5041 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5042 	    page3p->mode_page.code, nsect, sector_size);
5043 	SD_INFO(SD_LOG_COMMON, un,
5044 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5045 	    BE_16(page3p->track_skew),
5046 	    BE_16(page3p->cylinder_skew));
5047 
5048 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5049 
5050 	/*
5051 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5052 	 */
5053 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5054 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5055 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5056 	if (status != 0) {
5057 		SD_ERROR(SD_LOG_COMMON, un,
5058 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5059 		goto page4_exit;
5060 	}
5061 
5062 	/*
5063 	 * Determine size of Block Descriptors in order to locate the mode
5064 	 * page data.  ATAPI devices return 0, SCSI devices should return
5065 	 * MODE_BLK_DESC_LENGTH.
5066 	 */
5067 	headerp = (struct mode_header *)p4bufp;
5068 	if (un->un_f_cfg_is_atapi == TRUE) {
5069 		struct mode_header_grp2 *mhp =
5070 		    (struct mode_header_grp2 *)headerp;
5071 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5072 	} else {
5073 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5074 	}
5075 
5076 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5077 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5078 		    "sd_get_physical_geometry: received unexpected bd_len of "
5079 		    "%d, page4\n", bd_len);
5080 		status = EIO;
5081 		goto page4_exit;
5082 	}
5083 
5084 	page4p = (struct mode_geometry *)
5085 	    ((caddr_t)headerp + mode_header_length + bd_len);
5086 
5087 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5088 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5089 		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
5090 		    "%d\n", page4p->mode_page.code);
5091 		status = EIO;
5092 		goto page4_exit;
5093 	}
5094 
5095 	/*
5096 	 * Stash the data now, after we know that both commands completed.
5097 	 */
5098 
5099 
5100 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5101 	spc   = nhead * nsect;
5102 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5103 	rpm   = BE_16(page4p->rpm);
5104 
5105 	modesense_capacity = spc * ncyl;
5106 
5107 	SD_INFO(SD_LOG_COMMON, un,
5108 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5109 	SD_INFO(SD_LOG_COMMON, un,
5110 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5111 	SD_INFO(SD_LOG_COMMON, un,
5112 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5113 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5114 	    (void *)pgeom_p, capacity);
5115 
5116 	/*
5117 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5118 	 * the product of C * H * S returned by MODE SENSE >= that returned
5119 	 * by read capacity. This is an idiosyncrasy of the original x86
5120 	 * disk subsystem.
5121 	 */
5122 	if (modesense_capacity >= capacity) {
5123 		SD_INFO(SD_LOG_COMMON, un,
5124 		    "sd_get_physical_geometry: adjusting acyl; "
5125 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5126 		    (modesense_capacity - capacity + spc - 1) / spc);
5127 		if (sector_size != 0) {
5128 			/* 1243403: NEC D38x7 drives don't support sec size */
5129 			pgeom_p->g_secsize = (unsigned short)sector_size;
5130 		}
5131 		pgeom_p->g_nsect    = (unsigned short)nsect;
5132 		pgeom_p->g_nhead    = (unsigned short)nhead;
5133 		pgeom_p->g_capacity = capacity;
5134 		pgeom_p->g_acyl	    =
5135 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5136 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5137 	}
5138 
5139 	pgeom_p->g_rpm    = (unsigned short)rpm;
5140 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5141 	ret = 0;
5142 
5143 	SD_INFO(SD_LOG_COMMON, un,
5144 	    "sd_get_physical_geometry: mode sense geometry:\n");
5145 	SD_INFO(SD_LOG_COMMON, un,
5146 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5147 	    nsect, sector_size, intrlv);
5148 	SD_INFO(SD_LOG_COMMON, un,
5149 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5150 	    nhead, ncyl, rpm, modesense_capacity);
5151 	SD_INFO(SD_LOG_COMMON, un,
5152 	    "sd_get_physical_geometry: (cached)\n");
5153 	SD_INFO(SD_LOG_COMMON, un,
5154 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5155 	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5156 	    pgeom_p->g_nhead, pgeom_p->g_nsect);
5157 	SD_INFO(SD_LOG_COMMON, un,
5158 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5159 	    pgeom_p->g_secsize, pgeom_p->g_capacity,
5160 	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
5161 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5162 
5163 page4_exit:
5164 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5165 
5166 page3_exit:
5167 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5168 
5169 	if (status != 0) {
5170 		if (status == EIO) {
5171 			/*
5172 			 * Some disks do not support mode sense(6), we
5173 			 * should ignore this kind of error(sense key is
5174 			 * 0x5 - illegal request).
5175 			 */
5176 			uint8_t *sensep;
5177 			int senlen;
5178 
5179 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5180 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5181 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
5182 
5183 			if (senlen > 0 &&
5184 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5185 				sd_ssc_assessment(ssc,
5186 				    SD_FMT_IGNORE_COMPROMISE);
5187 			} else {
5188 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5189 			}
5190 		} else {
5191 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5192 		}
5193 	}
5194 	sd_ssc_fini(ssc);
5195 	return (ret);
5196 }
5197 
5198 /*
5199  *    Function: sd_get_virtual_geometry
5200  *
5201  * Description: Ask the controller to tell us about the target device.
5202  *
5203  *   Arguments: un - pointer to softstate
5204  *		capacity - disk capacity in #blocks
5205  *		lbasize - disk block size in bytes
5206  *
5207  *     Context: Kernel thread only
5208  */
5209 
5210 static int
5211 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5212     diskaddr_t capacity, int lbasize)
5213 {
5214 	uint_t	geombuf;
5215 	int	spc;
5216 
5217 	ASSERT(un != NULL);
5218 
5219 	/* Set sector size, and total number of sectors */
5220 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5221 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5222 
5223 	/* Let the HBA tell us its geometry */
5224 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5225 
5226 	/* A value of -1 indicates an undefined "geometry" property */
5227 	if (geombuf == (-1)) {
5228 		return (EINVAL);
5229 	}
5230 
5231 	/* Initialize the logical geometry cache. */
5232 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5233 	lgeom_p->g_nsect   = geombuf & 0xffff;
5234 	lgeom_p->g_secsize = un->un_sys_blocksize;
5235 
5236 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5237 
5238 	/*
5239 	 * Note: The driver originally converted the capacity value from
5240 	 * target blocks to system blocks. However, the capacity value passed
5241 	 * to this routine is already in terms of system blocks (this scaling
5242 	 * is done when the READ CAPACITY command is issued and processed).
5243 	 * This 'error' may have gone undetected because the usage of g_ncyl
5244 	 * (which is based upon g_capacity) is very limited within the driver
5245 	 */
5246 	lgeom_p->g_capacity = capacity;
5247 
5248 	/*
5249 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5250 	 * hba may return zero values if the device has been removed.
5251 	 */
5252 	if (spc == 0) {
5253 		lgeom_p->g_ncyl = 0;
5254 	} else {
5255 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5256 	}
5257 	lgeom_p->g_acyl = 0;
5258 
5259 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5260 	return (0);
5261 
5262 }
5263 /*
5264  *    Function: sd_update_block_info
5265  *
5266  * Description: Calculate a byte count to sector count bitshift value
5267  *		from sector size.
5268  *
5269  *   Arguments: un: unit struct.
5270  *		lbasize: new target sector size
5271  *		capacity: new target capacity, ie. block count
5272  *
5273  *     Context: Kernel thread context
5274  */
5275 
5276 static void
5277 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5278 {
5279 	if (lbasize != 0) {
5280 		un->un_tgt_blocksize = lbasize;
5281 		un->un_f_tgt_blocksize_is_valid = TRUE;
5282 		if (!un->un_f_has_removable_media) {
5283 			un->un_sys_blocksize = lbasize;
5284 		}
5285 	}
5286 
5287 	if (capacity != 0) {
5288 		un->un_blockcount		= capacity;
5289 		un->un_f_blockcount_is_valid	= TRUE;
5290 	}
5291 }
5292 
5293 
5294 /*
5295  *    Function: sd_register_devid
5296  *
5297  * Description: This routine will obtain the device id information from the
5298  *		target, obtain the serial number, and register the device
5299  *		id with the ddi framework.
5300  *
5301  *   Arguments: devi - the system's dev_info_t for the device.
5302  *		un - driver soft state (unit) structure
5303  *		reservation_flag - indicates if a reservation conflict
5304  *		occurred during attach
5305  *
5306  *     Context: Kernel Thread
5307  */
5308 static void
5309 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5310 {
5311 	int		rval		= 0;
5312 	uchar_t		*inq80		= NULL;
5313 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5314 	size_t		inq80_resid	= 0;
5315 	uchar_t		*inq83		= NULL;
5316 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5317 	size_t		inq83_resid	= 0;
5318 	int		dlen, len;
5319 	char		*sn;
5320 	struct sd_lun	*un;
5321 
5322 	ASSERT(ssc != NULL);
5323 	un = ssc->ssc_un;
5324 	ASSERT(un != NULL);
5325 	ASSERT(mutex_owned(SD_MUTEX(un)));
5326 	ASSERT((SD_DEVINFO(un)) == devi);
5327 
5328 
5329 	/*
5330 	 * We check the availability of the World Wide Name (0x83) and Unit
5331 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5332 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5333 	 * 0x83 is available, that is the best choice.  Our next choice is
5334 	 * 0x80.  If neither are available, we munge the devid from the device
5335 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5336 	 * to fabricate a devid for non-Sun qualified disks.
5337 	 */
5338 	if (sd_check_vpd_page_support(ssc) == 0) {
5339 		/* collect page 80 data if available */
5340 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5341 
5342 			mutex_exit(SD_MUTEX(un));
5343 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5344 
5345 			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5346 			    0x01, 0x80, &inq80_resid);
5347 
5348 			if (rval != 0) {
5349 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5350 				kmem_free(inq80, inq80_len);
5351 				inq80 = NULL;
5352 				inq80_len = 0;
5353 			} else if (ddi_prop_exists(
5354 			    DDI_DEV_T_NONE, SD_DEVINFO(un),
5355 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5356 			    INQUIRY_SERIAL_NO) == 0) {
5357 				/*
5358 				 * If we don't already have a serial number
5359 				 * property, do quick verify of data returned
5360 				 * and define property.
5361 				 */
5362 				dlen = inq80_len - inq80_resid;
5363 				len = (size_t)inq80[3];
5364 				if ((dlen >= 4) && ((len + 4) <= dlen)) {
5365 					/*
5366 					 * Ensure sn termination, skip leading
5367 					 * blanks, and create property
5368 					 * 'inquiry-serial-no'.
5369 					 */
5370 					sn = (char *)&inq80[4];
5371 					sn[len] = 0;
5372 					while (*sn && (*sn == ' '))
5373 						sn++;
5374 					if (*sn) {
5375 						(void) ddi_prop_update_string(
5376 						    DDI_DEV_T_NONE,
5377 						    SD_DEVINFO(un),
5378 						    INQUIRY_SERIAL_NO, sn);
5379 					}
5380 				}
5381 			}
5382 			mutex_enter(SD_MUTEX(un));
5383 		}
5384 
5385 		/* collect page 83 data if available */
5386 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5387 			mutex_exit(SD_MUTEX(un));
5388 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5389 
5390 			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5391 			    0x01, 0x83, &inq83_resid);
5392 
5393 			if (rval != 0) {
5394 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5395 				kmem_free(inq83, inq83_len);
5396 				inq83 = NULL;
5397 				inq83_len = 0;
5398 			}
5399 			mutex_enter(SD_MUTEX(un));
5400 		}
5401 	}
5402 
5403 	/*
5404 	 * If transport has already registered a devid for this target
5405 	 * then that takes precedence over the driver's determination
5406 	 * of the devid.
5407 	 *
5408 	 * NOTE: The reason this check is done here instead of at the beginning
5409 	 * of the function is to allow the code above to create the
5410 	 * 'inquiry-serial-no' property.
5411 	 */
5412 	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5413 		ASSERT(un->un_devid);
5414 		un->un_f_devid_transport_defined = TRUE;
5415 		goto cleanup; /* use devid registered by the transport */
5416 	}
5417 
5418 	/*
5419 	 * This is the case of antiquated Sun disk drives that have the
5420 	 * FAB_DEVID property set in the disk_table.  These drives
5421 	 * manage the devid's by storing them in last 2 available sectors
5422 	 * on the drive and have them fabricated by the ddi layer by calling
5423 	 * ddi_devid_init and passing the DEVID_FAB flag.
5424 	 */
5425 	if (un->un_f_opt_fab_devid == TRUE) {
5426 		/*
5427 		 * Depending on EINVAL isn't reliable, since a reserved disk
5428 		 * may result in invalid geometry, so check to make sure a
5429 		 * reservation conflict did not occur during attach.
5430 		 */
5431 		if ((sd_get_devid(ssc) == EINVAL) &&
5432 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5433 			/*
5434 			 * The devid is invalid AND there is no reservation
5435 			 * conflict.  Fabricate a new devid.
5436 			 */
5437 			(void) sd_create_devid(ssc);
5438 		}
5439 
5440 		/* Register the devid if it exists */
5441 		if (un->un_devid != NULL) {
5442 			(void) ddi_devid_register(SD_DEVINFO(un),
5443 			    un->un_devid);
5444 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5445 			    "sd_register_devid: Devid Fabricated\n");
5446 		}
5447 		goto cleanup;
5448 	}
5449 
5450 	/* encode best devid possible based on data available */
5451 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5452 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5453 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5454 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5455 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5456 
5457 		/* devid successfully encoded, register devid */
5458 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5459 
5460 	} else {
5461 		/*
5462 		 * Unable to encode a devid based on data available.
5463 		 * This is not a Sun qualified disk.  Older Sun disk
5464 		 * drives that have the SD_FAB_DEVID property
5465 		 * set in the disk_table and non Sun qualified
5466 		 * disks are treated in the same manner.  These
5467 		 * drives manage the devid's by storing them in
5468 		 * last 2 available sectors on the drive and
5469 		 * have them fabricated by the ddi layer by
5470 		 * calling ddi_devid_init and passing the
5471 		 * DEVID_FAB flag.
5472 		 * Create a fabricate devid only if there's no
5473 		 * fabricate devid existed.
5474 		 */
5475 		if (sd_get_devid(ssc) == EINVAL) {
5476 			(void) sd_create_devid(ssc);
5477 		}
5478 		un->un_f_opt_fab_devid = TRUE;
5479 
5480 		/* Register the devid if it exists */
5481 		if (un->un_devid != NULL) {
5482 			(void) ddi_devid_register(SD_DEVINFO(un),
5483 			    un->un_devid);
5484 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5485 			    "sd_register_devid: devid fabricated using "
5486 			    "ddi framework\n");
5487 		}
5488 	}
5489 
5490 cleanup:
5491 	/* clean up resources */
5492 	if (inq80 != NULL) {
5493 		kmem_free(inq80, inq80_len);
5494 	}
5495 	if (inq83 != NULL) {
5496 		kmem_free(inq83, inq83_len);
5497 	}
5498 }
5499 
5500 
5501 
5502 /*
5503  *    Function: sd_get_devid
5504  *
5505  * Description: This routine will return 0 if a valid device id has been
5506  *		obtained from the target and stored in the soft state. If a
5507  *		valid device id has not been previously read and stored, a
5508  *		read attempt will be made.
5509  *
5510  *   Arguments: un - driver soft state (unit) structure
5511  *
5512  * Return Code: 0 if we successfully get the device id
5513  *
5514  *     Context: Kernel Thread
5515  */
5516 
5517 static int
5518 sd_get_devid(sd_ssc_t *ssc)
5519 {
5520 	struct dk_devid		*dkdevid;
5521 	ddi_devid_t		tmpid;
5522 	uint_t			*ip;
5523 	size_t			sz;
5524 	diskaddr_t		blk;
5525 	int			status;
5526 	int			chksum;
5527 	int			i;
5528 	size_t			buffer_size;
5529 	struct sd_lun		*un;
5530 
5531 	ASSERT(ssc != NULL);
5532 	un = ssc->ssc_un;
5533 	ASSERT(un != NULL);
5534 	ASSERT(mutex_owned(SD_MUTEX(un)));
5535 
5536 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5537 	    un);
5538 
5539 	if (un->un_devid != NULL) {
5540 		return (0);
5541 	}
5542 
5543 	mutex_exit(SD_MUTEX(un));
5544 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5545 	    (void *)SD_PATH_DIRECT) != 0) {
5546 		mutex_enter(SD_MUTEX(un));
5547 		return (EINVAL);
5548 	}
5549 
5550 	/*
5551 	 * Read and verify device id, stored in the reserved cylinders at the
5552 	 * end of the disk. Backup label is on the odd sectors of the last
5553 	 * track of the last cylinder. Device id will be on track of the next
5554 	 * to last cylinder.
5555 	 */
5556 	mutex_enter(SD_MUTEX(un));
5557 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5558 	mutex_exit(SD_MUTEX(un));
5559 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5560 	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5561 	    SD_PATH_DIRECT);
5562 
5563 	if (status != 0) {
5564 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5565 		goto error;
5566 	}
5567 
5568 	/* Validate the revision */
5569 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5570 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5571 		status = EINVAL;
5572 		goto error;
5573 	}
5574 
5575 	/* Calculate the checksum */
5576 	chksum = 0;
5577 	ip = (uint_t *)dkdevid;
5578 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5579 	    i++) {
5580 		chksum ^= ip[i];
5581 	}
5582 
5583 	/* Compare the checksums */
5584 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5585 		status = EINVAL;
5586 		goto error;
5587 	}
5588 
5589 	/* Validate the device id */
5590 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5591 		status = EINVAL;
5592 		goto error;
5593 	}
5594 
5595 	/*
5596 	 * Store the device id in the driver soft state
5597 	 */
5598 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5599 	tmpid = kmem_alloc(sz, KM_SLEEP);
5600 
5601 	mutex_enter(SD_MUTEX(un));
5602 
5603 	un->un_devid = tmpid;
5604 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5605 
5606 	kmem_free(dkdevid, buffer_size);
5607 
5608 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5609 
5610 	return (status);
5611 error:
5612 	mutex_enter(SD_MUTEX(un));
5613 	kmem_free(dkdevid, buffer_size);
5614 	return (status);
5615 }
5616 
5617 
5618 /*
5619  *    Function: sd_create_devid
5620  *
5621  * Description: This routine will fabricate the device id and write it
5622  *		to the disk.
5623  *
5624  *   Arguments: un - driver soft state (unit) structure
5625  *
5626  * Return Code: value of the fabricated device id
5627  *
5628  *     Context: Kernel Thread
5629  */
5630 
5631 static ddi_devid_t
5632 sd_create_devid(sd_ssc_t *ssc)
5633 {
5634 	struct sd_lun	*un;
5635 
5636 	ASSERT(ssc != NULL);
5637 	un = ssc->ssc_un;
5638 	ASSERT(un != NULL);
5639 
5640 	/* Fabricate the devid */
5641 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5642 	    == DDI_FAILURE) {
5643 		return (NULL);
5644 	}
5645 
5646 	/* Write the devid to disk */
5647 	if (sd_write_deviceid(ssc) != 0) {
5648 		ddi_devid_free(un->un_devid);
5649 		un->un_devid = NULL;
5650 	}
5651 
5652 	return (un->un_devid);
5653 }
5654 
5655 
5656 /*
5657  *    Function: sd_write_deviceid
5658  *
5659  * Description: This routine will write the device id to the disk
5660  *		reserved sector.
5661  *
5662  *   Arguments: un - driver soft state (unit) structure
5663  *
5664  * Return Code: EINVAL
5665  *		value returned by sd_send_scsi_cmd
5666  *
5667  *     Context: Kernel Thread
5668  */
5669 
5670 static int
5671 sd_write_deviceid(sd_ssc_t *ssc)
5672 {
5673 	struct dk_devid		*dkdevid;
5674 	uchar_t			*buf;
5675 	diskaddr_t		blk;
5676 	uint_t			*ip, chksum;
5677 	int			status;
5678 	int			i;
5679 	struct sd_lun		*un;
5680 
5681 	ASSERT(ssc != NULL);
5682 	un = ssc->ssc_un;
5683 	ASSERT(un != NULL);
5684 	ASSERT(mutex_owned(SD_MUTEX(un)));
5685 
5686 	mutex_exit(SD_MUTEX(un));
5687 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5688 	    (void *)SD_PATH_DIRECT) != 0) {
5689 		mutex_enter(SD_MUTEX(un));
5690 		return (-1);
5691 	}
5692 
5693 
5694 	/* Allocate the buffer */
5695 	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5696 	dkdevid = (struct dk_devid *)buf;
5697 
5698 	/* Fill in the revision */
5699 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5700 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5701 
5702 	/* Copy in the device id */
5703 	mutex_enter(SD_MUTEX(un));
5704 	bcopy(un->un_devid, &dkdevid->dkd_devid,
5705 	    ddi_devid_sizeof(un->un_devid));
5706 	mutex_exit(SD_MUTEX(un));
5707 
5708 	/* Calculate the checksum */
5709 	chksum = 0;
5710 	ip = (uint_t *)dkdevid;
5711 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5712 	    i++) {
5713 		chksum ^= ip[i];
5714 	}
5715 
5716 	/* Fill-in checksum */
5717 	DKD_FORMCHKSUM(chksum, dkdevid);
5718 
5719 	/* Write the reserved sector */
5720 	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5721 	    SD_PATH_DIRECT);
5722 	if (status != 0)
5723 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5724 
5725 	kmem_free(buf, un->un_sys_blocksize);
5726 
5727 	mutex_enter(SD_MUTEX(un));
5728 	return (status);
5729 }
5730 
5731 
5732 /*
5733  *    Function: sd_check_vpd_page_support
5734  *
5735  * Description: This routine sends an inquiry command with the EVPD bit set and
5736  *		a page code of 0x00 to the device. It is used to determine which
5737  *		vital product pages are available to find the devid. We are
5738  *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5739  *		the device does not support that command.
5740  *
5741  *   Arguments: un  - driver soft state (unit) structure
5742  *
5743  * Return Code: 0 - success
5744  *		1 - check condition
5745  *
5746  *     Context: This routine can sleep.
5747  */
5748 
5749 static int
5750 sd_check_vpd_page_support(sd_ssc_t *ssc)
5751 {
5752 	uchar_t	*page_list	= NULL;
5753 	uchar_t	page_length	= 0xff;	/* Use max possible length */
5754 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
5755 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
5756 	int    	rval		= 0;
5757 	int	counter;
5758 	struct sd_lun		*un;
5759 
5760 	ASSERT(ssc != NULL);
5761 	un = ssc->ssc_un;
5762 	ASSERT(un != NULL);
5763 	ASSERT(mutex_owned(SD_MUTEX(un)));
5764 
5765 	mutex_exit(SD_MUTEX(un));
5766 
5767 	/*
5768 	 * We'll set the page length to the maximum to save figuring it out
5769 	 * with an additional call.
5770 	 */
5771 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
5772 
5773 	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5774 	    page_code, NULL);
5775 
5776 	if (rval != 0)
5777 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5778 
5779 	mutex_enter(SD_MUTEX(un));
5780 
5781 	/*
5782 	 * Now we must validate that the device accepted the command, as some
5783 	 * drives do not support it.  If the drive does support it, we will
5784 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
5785 	 * not, we return -1.
5786 	 */
5787 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5788 		/* Loop to find one of the 2 pages we need */
5789 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5790 
5791 		/*
5792 		 * Pages are returned in ascending order, and 0x83 is what we
5793 		 * are hoping for.
5794 		 */
5795 		while ((page_list[counter] <= 0xB1) &&
5796 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
5797 		    VPD_HEAD_OFFSET))) {
5798 			/*
5799 			 * Add 3 because page_list[3] is the number of
5800 			 * pages minus 3
5801 			 */
5802 
5803 			switch (page_list[counter]) {
5804 			case 0x00:
5805 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5806 				break;
5807 			case 0x80:
5808 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5809 				break;
5810 			case 0x81:
5811 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5812 				break;
5813 			case 0x82:
5814 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5815 				break;
5816 			case 0x83:
5817 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5818 				break;
5819 			case 0x86:
5820 				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5821 				break;
5822 			case 0xB1:
5823 				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5824 				break;
5825 			}
5826 			counter++;
5827 		}
5828 
5829 	} else {
5830 		rval = -1;
5831 
5832 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
5833 		    "sd_check_vpd_page_support: This drive does not implement "
5834 		    "VPD pages.\n");
5835 	}
5836 
5837 	kmem_free(page_list, page_length);
5838 
5839 	return (rval);
5840 }
5841 
5842 
5843 /*
5844  *    Function: sd_setup_pm
5845  *
5846  * Description: Initialize Power Management on the device
5847  *
5848  *     Context: Kernel Thread
5849  */
5850 
5851 static void
5852 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5853 {
5854 	uint_t		log_page_size;
5855 	uchar_t		*log_page_data;
5856 	int		rval = 0;
5857 	struct sd_lun	*un;
5858 
5859 	ASSERT(ssc != NULL);
5860 	un = ssc->ssc_un;
5861 	ASSERT(un != NULL);
5862 
5863 	/*
5864 	 * Since we are called from attach, holding a mutex for
5865 	 * un is unnecessary. Because some of the routines called
5866 	 * from here require SD_MUTEX to not be held, assert this
5867 	 * right up front.
5868 	 */
5869 	ASSERT(!mutex_owned(SD_MUTEX(un)));
5870 	/*
5871 	 * Since the sd device does not have the 'reg' property,
5872 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5873 	 * The following code is to tell cpr that this device
5874 	 * DOES need to be suspended and resumed.
5875 	 */
5876 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5877 	    "pm-hardware-state", "needs-suspend-resume");
5878 
5879 	/*
5880 	 * This complies with the new power management framework
5881 	 * for certain desktop machines. Create the pm_components
5882 	 * property as a string array property.
5883 	 * If un_f_pm_supported is TRUE, that means the disk
5884 	 * attached HBA has set the "pm-capable" property and
5885 	 * the value of this property is bigger than 0.
5886 	 */
5887 	if (un->un_f_pm_supported) {
5888 		/*
5889 		 * not all devices have a motor, try it first.
5890 		 * some devices may return ILLEGAL REQUEST, some
5891 		 * will hang
5892 		 * The following START_STOP_UNIT is used to check if target
5893 		 * device has a motor.
5894 		 */
5895 		un->un_f_start_stop_supported = TRUE;
5896 
5897 		if (un->un_f_power_condition_supported) {
5898 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5899 			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5900 			    SD_PATH_DIRECT);
5901 			if (rval != 0) {
5902 				un->un_f_power_condition_supported = FALSE;
5903 			}
5904 		}
5905 		if (!un->un_f_power_condition_supported) {
5906 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5907 			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5908 		}
5909 		if (rval != 0) {
5910 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5911 			un->un_f_start_stop_supported = FALSE;
5912 		}
5913 
5914 		/*
5915 		 * create pm properties anyways otherwise the parent can't
5916 		 * go to sleep
5917 		 */
5918 		un->un_f_pm_is_enabled = TRUE;
5919 		(void) sd_create_pm_components(devi, un);
5920 
5921 		/*
5922 		 * If it claims that log sense is supported, check it out.
5923 		 */
5924 		if (un->un_f_log_sense_supported) {
5925 			rval = sd_log_page_supported(ssc,
5926 			    START_STOP_CYCLE_PAGE);
5927 			if (rval == 1) {
5928 				/* Page found, use it. */
5929 				un->un_start_stop_cycle_page =
5930 				    START_STOP_CYCLE_PAGE;
5931 			} else {
5932 				/*
5933 				 * Page not found or log sense is not
5934 				 * supported.
5935 				 * Notice we do not check the old style
5936 				 * START_STOP_CYCLE_VU_PAGE because this
5937 				 * code path does not apply to old disks.
5938 				 */
5939 				un->un_f_log_sense_supported = FALSE;
5940 				un->un_f_pm_log_sense_smart = FALSE;
5941 			}
5942 		}
5943 
5944 		return;
5945 	}
5946 
5947 	/*
5948 	 * For the disk whose attached HBA has not set the "pm-capable"
5949 	 * property, check if it supports the power management.
5950 	 */
5951 	if (!un->un_f_log_sense_supported) {
5952 		un->un_power_level = SD_SPINDLE_ON;
5953 		un->un_f_pm_is_enabled = FALSE;
5954 		return;
5955 	}
5956 
5957 	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
5958 
5959 #ifdef	SDDEBUG
5960 	if (sd_force_pm_supported) {
5961 		/* Force a successful result */
5962 		rval = 1;
5963 	}
5964 #endif
5965 
5966 	/*
5967 	 * If the start-stop cycle counter log page is not supported
5968 	 * or if the pm-capable property is set to be false (0),
5969 	 * then we should not create the pm_components property.
5970 	 */
5971 	if (rval == -1) {
5972 		/*
5973 		 * Error.
5974 		 * Reading log sense failed, most likely this is
5975 		 * an older drive that does not support log sense.
5976 		 * If this fails auto-pm is not supported.
5977 		 */
5978 		un->un_power_level = SD_SPINDLE_ON;
5979 		un->un_f_pm_is_enabled = FALSE;
5980 
5981 	} else if (rval == 0) {
5982 		/*
5983 		 * Page not found.
5984 		 * The start stop cycle counter is implemented as page
5985 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
5986 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
5987 		 */
5988 		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
5989 			/*
5990 			 * Page found, use this one.
5991 			 */
5992 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
5993 			un->un_f_pm_is_enabled = TRUE;
5994 		} else {
5995 			/*
5996 			 * Error or page not found.
5997 			 * auto-pm is not supported for this device.
5998 			 */
5999 			un->un_power_level = SD_SPINDLE_ON;
6000 			un->un_f_pm_is_enabled = FALSE;
6001 		}
6002 	} else {
6003 		/*
6004 		 * Page found, use it.
6005 		 */
6006 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6007 		un->un_f_pm_is_enabled = TRUE;
6008 	}
6009 
6010 
6011 	if (un->un_f_pm_is_enabled == TRUE) {
6012 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6013 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6014 
6015 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6016 		    log_page_size, un->un_start_stop_cycle_page,
6017 		    0x01, 0, SD_PATH_DIRECT);
6018 
6019 		if (rval != 0) {
6020 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6021 		}
6022 
6023 #ifdef	SDDEBUG
6024 		if (sd_force_pm_supported) {
6025 			/* Force a successful result */
6026 			rval = 0;
6027 		}
6028 #endif
6029 
6030 		/*
6031 		 * If the Log sense for Page( Start/stop cycle counter page)
6032 		 * succeeds, then power management is supported and we can
6033 		 * enable auto-pm.
6034 		 */
6035 		if (rval == 0)  {
6036 			(void) sd_create_pm_components(devi, un);
6037 		} else {
6038 			un->un_power_level = SD_SPINDLE_ON;
6039 			un->un_f_pm_is_enabled = FALSE;
6040 		}
6041 
6042 		kmem_free(log_page_data, log_page_size);
6043 	}
6044 }
6045 
6046 
6047 /*
6048  *    Function: sd_create_pm_components
6049  *
6050  * Description: Initialize PM property.
6051  *
6052  *     Context: Kernel thread context
6053  */
6054 
6055 static void
6056 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6057 {
6058 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6059 
6060 	if (un->un_f_power_condition_supported) {
6061 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6062 		    "pm-components", sd_pwr_pc.pm_comp, 5)
6063 		    != DDI_PROP_SUCCESS) {
6064 			un->un_power_level = SD_SPINDLE_ACTIVE;
6065 			un->un_f_pm_is_enabled = FALSE;
6066 			return;
6067 		}
6068 	} else {
6069 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6070 		    "pm-components", sd_pwr_ss.pm_comp, 3)
6071 		    != DDI_PROP_SUCCESS) {
6072 			un->un_power_level = SD_SPINDLE_ON;
6073 			un->un_f_pm_is_enabled = FALSE;
6074 			return;
6075 		}
6076 	}
6077 	/*
6078 	 * When components are initially created they are idle,
6079 	 * power up any non-removables.
6080 	 * Note: the return value of pm_raise_power can't be used
6081 	 * for determining if PM should be enabled for this device.
6082 	 * Even if you check the return values and remove this
6083 	 * property created above, the PM framework will not honor the
6084 	 * change after the first call to pm_raise_power. Hence,
6085 	 * removal of that property does not help if pm_raise_power
6086 	 * fails. In the case of removable media, the start/stop
6087 	 * will fail if the media is not present.
6088 	 */
6089 	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6090 	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6091 		mutex_enter(SD_MUTEX(un));
6092 		un->un_power_level = SD_PM_STATE_ACTIVE(un);
6093 		mutex_enter(&un->un_pm_mutex);
6094 		/* Set to on and not busy. */
6095 		un->un_pm_count = 0;
6096 	} else {
6097 		mutex_enter(SD_MUTEX(un));
6098 		un->un_power_level = SD_PM_STATE_STOPPED(un);
6099 		mutex_enter(&un->un_pm_mutex);
6100 		/* Set to off. */
6101 		un->un_pm_count = -1;
6102 	}
6103 	mutex_exit(&un->un_pm_mutex);
6104 	mutex_exit(SD_MUTEX(un));
6105 }
6106 
6107 
6108 /*
6109  *    Function: sd_ddi_suspend
6110  *
6111  * Description: Performs system power-down operations. This includes
6112  *		setting the drive state to indicate its suspended so
6113  *		that no new commands will be accepted. Also, wait for
6114  *		all commands that are in transport or queued to a timer
6115  *		for retry to complete. All timeout threads are cancelled.
6116  *
6117  * Return Code: DDI_FAILURE or DDI_SUCCESS
6118  *
6119  *     Context: Kernel thread context
6120  */
6121 
6122 static int
6123 sd_ddi_suspend(dev_info_t *devi)
6124 {
6125 	struct	sd_lun	*un;
6126 	clock_t		wait_cmds_complete;
6127 
6128 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6129 	if (un == NULL) {
6130 		return (DDI_FAILURE);
6131 	}
6132 
6133 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6134 
6135 	mutex_enter(SD_MUTEX(un));
6136 
6137 	/* Return success if the device is already suspended. */
6138 	if (un->un_state == SD_STATE_SUSPENDED) {
6139 		mutex_exit(SD_MUTEX(un));
6140 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6141 		    "device already suspended, exiting\n");
6142 		return (DDI_SUCCESS);
6143 	}
6144 
6145 	/* Return failure if the device is being used by HA */
6146 	if (un->un_resvd_status &
6147 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6148 		mutex_exit(SD_MUTEX(un));
6149 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6150 		    "device in use by HA, exiting\n");
6151 		return (DDI_FAILURE);
6152 	}
6153 
6154 	/*
6155 	 * Return failure if the device is in a resource wait
6156 	 * or power changing state.
6157 	 */
6158 	if ((un->un_state == SD_STATE_RWAIT) ||
6159 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6160 		mutex_exit(SD_MUTEX(un));
6161 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6162 		    "device in resource wait state, exiting\n");
6163 		return (DDI_FAILURE);
6164 	}
6165 
6166 
6167 	un->un_save_state = un->un_last_state;
6168 	New_state(un, SD_STATE_SUSPENDED);
6169 
6170 	/*
6171 	 * Wait for all commands that are in transport or queued to a timer
6172 	 * for retry to complete.
6173 	 *
6174 	 * While waiting, no new commands will be accepted or sent because of
6175 	 * the new state we set above.
6176 	 *
6177 	 * Wait till current operation has completed. If we are in the resource
6178 	 * wait state (with an intr outstanding) then we need to wait till the
6179 	 * intr completes and starts the next cmd. We want to wait for
6180 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6181 	 */
6182 	wait_cmds_complete = ddi_get_lbolt() +
6183 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6184 
6185 	while (un->un_ncmds_in_transport != 0) {
6186 		/*
6187 		 * Fail if commands do not finish in the specified time.
6188 		 */
6189 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6190 		    wait_cmds_complete) == -1) {
6191 			/*
6192 			 * Undo the state changes made above. Everything
6193 			 * must go back to it's original value.
6194 			 */
6195 			Restore_state(un);
6196 			un->un_last_state = un->un_save_state;
6197 			/* Wake up any threads that might be waiting. */
6198 			cv_broadcast(&un->un_suspend_cv);
6199 			mutex_exit(SD_MUTEX(un));
6200 			SD_ERROR(SD_LOG_IO_PM, un,
6201 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6202 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6203 			return (DDI_FAILURE);
6204 		}
6205 	}
6206 
6207 	/*
6208 	 * Cancel SCSI watch thread and timeouts, if any are active
6209 	 */
6210 
6211 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6212 		opaque_t temp_token = un->un_swr_token;
6213 		mutex_exit(SD_MUTEX(un));
6214 		scsi_watch_suspend(temp_token);
6215 		mutex_enter(SD_MUTEX(un));
6216 	}
6217 
6218 	if (un->un_reset_throttle_timeid != NULL) {
6219 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6220 		un->un_reset_throttle_timeid = NULL;
6221 		mutex_exit(SD_MUTEX(un));
6222 		(void) untimeout(temp_id);
6223 		mutex_enter(SD_MUTEX(un));
6224 	}
6225 
6226 	if (un->un_dcvb_timeid != NULL) {
6227 		timeout_id_t temp_id = un->un_dcvb_timeid;
6228 		un->un_dcvb_timeid = NULL;
6229 		mutex_exit(SD_MUTEX(un));
6230 		(void) untimeout(temp_id);
6231 		mutex_enter(SD_MUTEX(un));
6232 	}
6233 
6234 	mutex_enter(&un->un_pm_mutex);
6235 	if (un->un_pm_timeid != NULL) {
6236 		timeout_id_t temp_id = un->un_pm_timeid;
6237 		un->un_pm_timeid = NULL;
6238 		mutex_exit(&un->un_pm_mutex);
6239 		mutex_exit(SD_MUTEX(un));
6240 		(void) untimeout(temp_id);
6241 		mutex_enter(SD_MUTEX(un));
6242 	} else {
6243 		mutex_exit(&un->un_pm_mutex);
6244 	}
6245 
6246 	if (un->un_rmw_msg_timeid != NULL) {
6247 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
6248 		un->un_rmw_msg_timeid = NULL;
6249 		mutex_exit(SD_MUTEX(un));
6250 		(void) untimeout(temp_id);
6251 		mutex_enter(SD_MUTEX(un));
6252 	}
6253 
6254 	if (un->un_retry_timeid != NULL) {
6255 		timeout_id_t temp_id = un->un_retry_timeid;
6256 		un->un_retry_timeid = NULL;
6257 		mutex_exit(SD_MUTEX(un));
6258 		(void) untimeout(temp_id);
6259 		mutex_enter(SD_MUTEX(un));
6260 
6261 		if (un->un_retry_bp != NULL) {
6262 			un->un_retry_bp->av_forw = un->un_waitq_headp;
6263 			un->un_waitq_headp = un->un_retry_bp;
6264 			if (un->un_waitq_tailp == NULL) {
6265 				un->un_waitq_tailp = un->un_retry_bp;
6266 			}
6267 			un->un_retry_bp = NULL;
6268 			un->un_retry_statp = NULL;
6269 		}
6270 	}
6271 
6272 	if (un->un_direct_priority_timeid != NULL) {
6273 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6274 		un->un_direct_priority_timeid = NULL;
6275 		mutex_exit(SD_MUTEX(un));
6276 		(void) untimeout(temp_id);
6277 		mutex_enter(SD_MUTEX(un));
6278 	}
6279 
6280 	if (un->un_f_is_fibre == TRUE) {
6281 		/*
6282 		 * Remove callbacks for insert and remove events
6283 		 */
6284 		if (un->un_insert_event != NULL) {
6285 			mutex_exit(SD_MUTEX(un));
6286 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6287 			mutex_enter(SD_MUTEX(un));
6288 			un->un_insert_event = NULL;
6289 		}
6290 
6291 		if (un->un_remove_event != NULL) {
6292 			mutex_exit(SD_MUTEX(un));
6293 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6294 			mutex_enter(SD_MUTEX(un));
6295 			un->un_remove_event = NULL;
6296 		}
6297 	}
6298 
6299 	mutex_exit(SD_MUTEX(un));
6300 
6301 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6302 
6303 	return (DDI_SUCCESS);
6304 }
6305 
6306 
6307 /*
6308  *    Function: sd_ddi_resume
6309  *
6310  * Description: Performs system power-up operations..
6311  *
6312  * Return Code: DDI_SUCCESS
6313  *		DDI_FAILURE
6314  *
6315  *     Context: Kernel thread context
6316  */
6317 
6318 static int
6319 sd_ddi_resume(dev_info_t *devi)
6320 {
6321 	struct	sd_lun	*un;
6322 
6323 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6324 	if (un == NULL) {
6325 		return (DDI_FAILURE);
6326 	}
6327 
6328 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6329 
6330 	mutex_enter(SD_MUTEX(un));
6331 	Restore_state(un);
6332 
6333 	/*
6334 	 * Restore the state which was saved to give the
6335 	 * the right state in un_last_state
6336 	 */
6337 	un->un_last_state = un->un_save_state;
6338 	/*
6339 	 * Note: throttle comes back at full.
6340 	 * Also note: this MUST be done before calling pm_raise_power
6341 	 * otherwise the system can get hung in biowait. The scenario where
6342 	 * this'll happen is under cpr suspend. Writing of the system
6343 	 * state goes through sddump, which writes 0 to un_throttle. If
6344 	 * writing the system state then fails, example if the partition is
6345 	 * too small, then cpr attempts a resume. If throttle isn't restored
6346 	 * from the saved value until after calling pm_raise_power then
6347 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6348 	 * in biowait.
6349 	 */
6350 	un->un_throttle = un->un_saved_throttle;
6351 
6352 	/*
6353 	 * The chance of failure is very rare as the only command done in power
6354 	 * entry point is START command when you transition from 0->1 or
6355 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6356 	 * which suspend was done. Ignore the return value as the resume should
6357 	 * not be failed. In the case of removable media the media need not be
6358 	 * inserted and hence there is a chance that raise power will fail with
6359 	 * media not present.
6360 	 */
6361 	if (un->un_f_attach_spinup) {
6362 		mutex_exit(SD_MUTEX(un));
6363 		(void) pm_raise_power(SD_DEVINFO(un), 0,
6364 		    SD_PM_STATE_ACTIVE(un));
6365 		mutex_enter(SD_MUTEX(un));
6366 	}
6367 
6368 	/*
6369 	 * Don't broadcast to the suspend cv and therefore possibly
6370 	 * start I/O until after power has been restored.
6371 	 */
6372 	cv_broadcast(&un->un_suspend_cv);
6373 	cv_broadcast(&un->un_state_cv);
6374 
6375 	/* restart thread */
6376 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6377 		scsi_watch_resume(un->un_swr_token);
6378 	}
6379 
6380 #if (defined(__fibre))
6381 	if (un->un_f_is_fibre == TRUE) {
6382 		/*
6383 		 * Add callbacks for insert and remove events
6384 		 */
6385 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6386 			sd_init_event_callbacks(un);
6387 		}
6388 	}
6389 #endif
6390 
6391 	/*
6392 	 * Transport any pending commands to the target.
6393 	 *
6394 	 * If this is a low-activity device commands in queue will have to wait
6395 	 * until new commands come in, which may take awhile. Also, we
6396 	 * specifically don't check un_ncmds_in_transport because we know that
6397 	 * there really are no commands in progress after the unit was
6398 	 * suspended and we could have reached the throttle level, been
6399 	 * suspended, and have no new commands coming in for awhile. Highly
6400 	 * unlikely, but so is the low-activity disk scenario.
6401 	 */
6402 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6403 
6404 	sd_start_cmds(un, NULL);
6405 	mutex_exit(SD_MUTEX(un));
6406 
6407 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6408 
6409 	return (DDI_SUCCESS);
6410 }
6411 
6412 
6413 /*
6414  *    Function: sd_pm_state_change
6415  *
6416  * Description: Change the driver power state.
6417  * 		Someone else is required to actually change the driver
6418  * 		power level.
6419  *
6420  *   Arguments: un - driver soft state (unit) structure
6421  *              level - the power level that is changed to
6422  *              flag - to decide how to change the power state
6423  *
6424  * Return Code: DDI_SUCCESS
6425  *
6426  *     Context: Kernel thread context
6427  */
6428 static int
6429 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6430 {
6431 	ASSERT(un != NULL);
6432 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6433 
6434 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6435 	mutex_enter(SD_MUTEX(un));
6436 
6437 	if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6438 		un->un_power_level = level;
6439 		ASSERT(!mutex_owned(&un->un_pm_mutex));
6440 		mutex_enter(&un->un_pm_mutex);
6441 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6442 			un->un_pm_count++;
6443 			ASSERT(un->un_pm_count == 0);
6444 		}
6445 		mutex_exit(&un->un_pm_mutex);
6446 	} else {
6447 		/*
6448 		 * Exit if power management is not enabled for this device,
6449 		 * or if the device is being used by HA.
6450 		 */
6451 		if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6452 		    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6453 			mutex_exit(SD_MUTEX(un));
6454 			SD_TRACE(SD_LOG_POWER, un,
6455 			    "sd_pm_state_change: exiting\n");
6456 			return (DDI_FAILURE);
6457 		}
6458 
6459 		SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6460 		    "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6461 
6462 		/*
6463 		 * See if the device is not busy, ie.:
6464 		 *    - we have no commands in the driver for this device
6465 		 *    - not waiting for resources
6466 		 */
6467 		if ((un->un_ncmds_in_driver == 0) &&
6468 		    (un->un_state != SD_STATE_RWAIT)) {
6469 			/*
6470 			 * The device is not busy, so it is OK to go to low
6471 			 * power state. Indicate low power, but rely on someone
6472 			 * else to actually change it.
6473 			 */
6474 			mutex_enter(&un->un_pm_mutex);
6475 			un->un_pm_count = -1;
6476 			mutex_exit(&un->un_pm_mutex);
6477 			un->un_power_level = level;
6478 		}
6479 	}
6480 
6481 	mutex_exit(SD_MUTEX(un));
6482 
6483 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6484 
6485 	return (DDI_SUCCESS);
6486 }
6487 
6488 
6489 /*
6490  *    Function: sd_pm_idletimeout_handler
6491  *
6492  * Description: A timer routine that's active only while a device is busy.
6493  *		The purpose is to extend slightly the pm framework's busy
6494  *		view of the device to prevent busy/idle thrashing for
6495  *		back-to-back commands. Do this by comparing the current time
6496  *		to the time at which the last command completed and when the
6497  *		difference is greater than sd_pm_idletime, call
6498  *		pm_idle_component. In addition to indicating idle to the pm
6499  *		framework, update the chain type to again use the internal pm
6500  *		layers of the driver.
6501  *
6502  *   Arguments: arg - driver soft state (unit) structure
6503  *
6504  *     Context: Executes in a timeout(9F) thread context
6505  */
6506 
6507 static void
6508 sd_pm_idletimeout_handler(void *arg)
6509 {
6510 	struct sd_lun *un = arg;
6511 
6512 	time_t	now;
6513 
6514 	mutex_enter(&sd_detach_mutex);
6515 	if (un->un_detach_count != 0) {
6516 		/* Abort if the instance is detaching */
6517 		mutex_exit(&sd_detach_mutex);
6518 		return;
6519 	}
6520 	mutex_exit(&sd_detach_mutex);
6521 
6522 	now = ddi_get_time();
6523 	/*
6524 	 * Grab both mutexes, in the proper order, since we're accessing
6525 	 * both PM and softstate variables.
6526 	 */
6527 	mutex_enter(SD_MUTEX(un));
6528 	mutex_enter(&un->un_pm_mutex);
6529 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
6530 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6531 		/*
6532 		 * Update the chain types.
6533 		 * This takes affect on the next new command received.
6534 		 */
6535 		if (un->un_f_non_devbsize_supported) {
6536 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6537 		} else {
6538 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6539 		}
6540 		un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6541 
6542 		SD_TRACE(SD_LOG_IO_PM, un,
6543 		    "sd_pm_idletimeout_handler: idling device\n");
6544 		(void) pm_idle_component(SD_DEVINFO(un), 0);
6545 		un->un_pm_idle_timeid = NULL;
6546 	} else {
6547 		un->un_pm_idle_timeid =
6548 		    timeout(sd_pm_idletimeout_handler, un,
6549 		    (drv_usectohz((clock_t)300000))); /* 300 ms. */
6550 	}
6551 	mutex_exit(&un->un_pm_mutex);
6552 	mutex_exit(SD_MUTEX(un));
6553 }
6554 
6555 
6556 /*
6557  *    Function: sd_pm_timeout_handler
6558  *
6559  * Description: Callback to tell framework we are idle.
6560  *
6561  *     Context: timeout(9f) thread context.
6562  */
6563 
6564 static void
6565 sd_pm_timeout_handler(void *arg)
6566 {
6567 	struct sd_lun *un = arg;
6568 
6569 	(void) pm_idle_component(SD_DEVINFO(un), 0);
6570 	mutex_enter(&un->un_pm_mutex);
6571 	un->un_pm_timeid = NULL;
6572 	mutex_exit(&un->un_pm_mutex);
6573 }
6574 
6575 
6576 /*
6577  *    Function: sdpower
6578  *
6579  * Description: PM entry point.
6580  *
6581  * Return Code: DDI_SUCCESS
6582  *		DDI_FAILURE
6583  *
6584  *     Context: Kernel thread context
6585  */
6586 
6587 static int
6588 sdpower(dev_info_t *devi, int component, int level)
6589 {
6590 	struct sd_lun	*un;
6591 	int		instance;
6592 	int		rval = DDI_SUCCESS;
6593 	uint_t		i, log_page_size, maxcycles, ncycles;
6594 	uchar_t		*log_page_data;
6595 	int		log_sense_page;
6596 	int		medium_present;
6597 	time_t		intvlp;
6598 	struct pm_trans_data	sd_pm_tran_data;
6599 	uchar_t		save_state;
6600 	int		sval;
6601 	uchar_t		state_before_pm;
6602 	int		got_semaphore_here;
6603 	sd_ssc_t	*ssc;
6604 	int	last_power_level;
6605 
6606 	instance = ddi_get_instance(devi);
6607 
6608 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6609 	    !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6610 		return (DDI_FAILURE);
6611 	}
6612 
6613 	ssc = sd_ssc_init(un);
6614 
6615 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6616 
6617 	/*
6618 	 * Must synchronize power down with close.
6619 	 * Attempt to decrement/acquire the open/close semaphore,
6620 	 * but do NOT wait on it. If it's not greater than zero,
6621 	 * ie. it can't be decremented without waiting, then
6622 	 * someone else, either open or close, already has it
6623 	 * and the try returns 0. Use that knowledge here to determine
6624 	 * if it's OK to change the device power level.
6625 	 * Also, only increment it on exit if it was decremented, ie. gotten,
6626 	 * here.
6627 	 */
6628 	got_semaphore_here = sema_tryp(&un->un_semoclose);
6629 
6630 	mutex_enter(SD_MUTEX(un));
6631 
6632 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6633 	    un->un_ncmds_in_driver);
6634 
6635 	/*
6636 	 * If un_ncmds_in_driver is non-zero it indicates commands are
6637 	 * already being processed in the driver, or if the semaphore was
6638 	 * not gotten here it indicates an open or close is being processed.
6639 	 * At the same time somebody is requesting to go to a lower power
6640 	 * that can't perform I/O, which can't happen, therefore we need to
6641 	 * return failure.
6642 	 */
6643 	if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6644 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6645 		mutex_exit(SD_MUTEX(un));
6646 
6647 		if (got_semaphore_here != 0) {
6648 			sema_v(&un->un_semoclose);
6649 		}
6650 		SD_TRACE(SD_LOG_IO_PM, un,
6651 		    "sdpower: exit, device has queued cmds.\n");
6652 
6653 		goto sdpower_failed;
6654 	}
6655 
6656 	/*
6657 	 * if it is OFFLINE that means the disk is completely dead
6658 	 * in our case we have to put the disk in on or off by sending commands
6659 	 * Of course that will fail anyway so return back here.
6660 	 *
6661 	 * Power changes to a device that's OFFLINE or SUSPENDED
6662 	 * are not allowed.
6663 	 */
6664 	if ((un->un_state == SD_STATE_OFFLINE) ||
6665 	    (un->un_state == SD_STATE_SUSPENDED)) {
6666 		mutex_exit(SD_MUTEX(un));
6667 
6668 		if (got_semaphore_here != 0) {
6669 			sema_v(&un->un_semoclose);
6670 		}
6671 		SD_TRACE(SD_LOG_IO_PM, un,
6672 		    "sdpower: exit, device is off-line.\n");
6673 
6674 		goto sdpower_failed;
6675 	}
6676 
6677 	/*
6678 	 * Change the device's state to indicate it's power level
6679 	 * is being changed. Do this to prevent a power off in the
6680 	 * middle of commands, which is especially bad on devices
6681 	 * that are really powered off instead of just spun down.
6682 	 */
6683 	state_before_pm = un->un_state;
6684 	un->un_state = SD_STATE_PM_CHANGING;
6685 
6686 	mutex_exit(SD_MUTEX(un));
6687 
6688 	/*
6689 	 * If log sense command is not supported, bypass the
6690 	 * following checking, otherwise, check the log sense
6691 	 * information for this device.
6692 	 */
6693 	if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6694 	    un->un_f_log_sense_supported) {
6695 		/*
6696 		 * Get the log sense information to understand whether the
6697 		 * the powercycle counts have gone beyond the threshhold.
6698 		 */
6699 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6700 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6701 
6702 		mutex_enter(SD_MUTEX(un));
6703 		log_sense_page = un->un_start_stop_cycle_page;
6704 		mutex_exit(SD_MUTEX(un));
6705 
6706 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6707 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6708 
6709 		if (rval != 0) {
6710 			if (rval == EIO)
6711 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6712 			else
6713 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6714 		}
6715 
6716 #ifdef	SDDEBUG
6717 		if (sd_force_pm_supported) {
6718 			/* Force a successful result */
6719 			rval = 0;
6720 		}
6721 #endif
6722 		if (rval != 0) {
6723 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6724 			    "Log Sense Failed\n");
6725 
6726 			kmem_free(log_page_data, log_page_size);
6727 			/* Cannot support power management on those drives */
6728 
6729 			if (got_semaphore_here != 0) {
6730 				sema_v(&un->un_semoclose);
6731 			}
6732 			/*
6733 			 * On exit put the state back to it's original value
6734 			 * and broadcast to anyone waiting for the power
6735 			 * change completion.
6736 			 */
6737 			mutex_enter(SD_MUTEX(un));
6738 			un->un_state = state_before_pm;
6739 			cv_broadcast(&un->un_suspend_cv);
6740 			mutex_exit(SD_MUTEX(un));
6741 			SD_TRACE(SD_LOG_IO_PM, un,
6742 			    "sdpower: exit, Log Sense Failed.\n");
6743 
6744 			goto sdpower_failed;
6745 		}
6746 
6747 		/*
6748 		 * From the page data - Convert the essential information to
6749 		 * pm_trans_data
6750 		 */
6751 		maxcycles =
6752 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6753 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6754 
6755 		ncycles =
6756 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6757 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
6758 
6759 		if (un->un_f_pm_log_sense_smart) {
6760 			sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6761 			sd_pm_tran_data.un.smart_count.consumed = ncycles;
6762 			sd_pm_tran_data.un.smart_count.flag = 0;
6763 			sd_pm_tran_data.format = DC_SMART_FORMAT;
6764 		} else {
6765 			sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6766 			sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6767 			for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6768 				sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6769 				    log_page_data[8+i];
6770 			}
6771 			sd_pm_tran_data.un.scsi_cycles.flag = 0;
6772 			sd_pm_tran_data.format = DC_SCSI_FORMAT;
6773 		}
6774 
6775 		kmem_free(log_page_data, log_page_size);
6776 
6777 		/*
6778 		 * Call pm_trans_check routine to get the Ok from
6779 		 * the global policy
6780 		 */
6781 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6782 #ifdef	SDDEBUG
6783 		if (sd_force_pm_supported) {
6784 			/* Force a successful result */
6785 			rval = 1;
6786 		}
6787 #endif
6788 		switch (rval) {
6789 		case 0:
6790 			/*
6791 			 * Not Ok to Power cycle or error in parameters passed
6792 			 * Would have given the advised time to consider power
6793 			 * cycle. Based on the new intvlp parameter we are
6794 			 * supposed to pretend we are busy so that pm framework
6795 			 * will never call our power entry point. Because of
6796 			 * that install a timeout handler and wait for the
6797 			 * recommended time to elapse so that power management
6798 			 * can be effective again.
6799 			 *
6800 			 * To effect this behavior, call pm_busy_component to
6801 			 * indicate to the framework this device is busy.
6802 			 * By not adjusting un_pm_count the rest of PM in
6803 			 * the driver will function normally, and independent
6804 			 * of this but because the framework is told the device
6805 			 * is busy it won't attempt powering down until it gets
6806 			 * a matching idle. The timeout handler sends this.
6807 			 * Note: sd_pm_entry can't be called here to do this
6808 			 * because sdpower may have been called as a result
6809 			 * of a call to pm_raise_power from within sd_pm_entry.
6810 			 *
6811 			 * If a timeout handler is already active then
6812 			 * don't install another.
6813 			 */
6814 			mutex_enter(&un->un_pm_mutex);
6815 			if (un->un_pm_timeid == NULL) {
6816 				un->un_pm_timeid =
6817 				    timeout(sd_pm_timeout_handler,
6818 				    un, intvlp * drv_usectohz(1000000));
6819 				mutex_exit(&un->un_pm_mutex);
6820 				(void) pm_busy_component(SD_DEVINFO(un), 0);
6821 			} else {
6822 				mutex_exit(&un->un_pm_mutex);
6823 			}
6824 			if (got_semaphore_here != 0) {
6825 				sema_v(&un->un_semoclose);
6826 			}
6827 			/*
6828 			 * On exit put the state back to it's original value
6829 			 * and broadcast to anyone waiting for the power
6830 			 * change completion.
6831 			 */
6832 			mutex_enter(SD_MUTEX(un));
6833 			un->un_state = state_before_pm;
6834 			cv_broadcast(&un->un_suspend_cv);
6835 			mutex_exit(SD_MUTEX(un));
6836 
6837 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6838 			    "trans check Failed, not ok to power cycle.\n");
6839 
6840 			goto sdpower_failed;
6841 		case -1:
6842 			if (got_semaphore_here != 0) {
6843 				sema_v(&un->un_semoclose);
6844 			}
6845 			/*
6846 			 * On exit put the state back to it's original value
6847 			 * and broadcast to anyone waiting for the power
6848 			 * change completion.
6849 			 */
6850 			mutex_enter(SD_MUTEX(un));
6851 			un->un_state = state_before_pm;
6852 			cv_broadcast(&un->un_suspend_cv);
6853 			mutex_exit(SD_MUTEX(un));
6854 			SD_TRACE(SD_LOG_IO_PM, un,
6855 			    "sdpower: exit, trans check command Failed.\n");
6856 
6857 			goto sdpower_failed;
6858 		}
6859 	}
6860 
6861 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6862 		/*
6863 		 * Save the last state... if the STOP FAILS we need it
6864 		 * for restoring
6865 		 */
6866 		mutex_enter(SD_MUTEX(un));
6867 		save_state = un->un_last_state;
6868 		last_power_level = un->un_power_level;
6869 		/*
6870 		 * There must not be any cmds. getting processed
6871 		 * in the driver when we get here. Power to the
6872 		 * device is potentially going off.
6873 		 */
6874 		ASSERT(un->un_ncmds_in_driver == 0);
6875 		mutex_exit(SD_MUTEX(un));
6876 
6877 		/*
6878 		 * For now PM suspend the device completely before spindle is
6879 		 * turned off
6880 		 */
6881 		if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6882 		    == DDI_FAILURE) {
6883 			if (got_semaphore_here != 0) {
6884 				sema_v(&un->un_semoclose);
6885 			}
6886 			/*
6887 			 * On exit put the state back to it's original value
6888 			 * and broadcast to anyone waiting for the power
6889 			 * change completion.
6890 			 */
6891 			mutex_enter(SD_MUTEX(un));
6892 			un->un_state = state_before_pm;
6893 			un->un_power_level = last_power_level;
6894 			cv_broadcast(&un->un_suspend_cv);
6895 			mutex_exit(SD_MUTEX(un));
6896 			SD_TRACE(SD_LOG_IO_PM, un,
6897 			    "sdpower: exit, PM suspend Failed.\n");
6898 
6899 			goto sdpower_failed;
6900 		}
6901 	}
6902 
6903 	/*
6904 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6905 	 * close, or strategy. Dump no long uses this routine, it uses it's
6906 	 * own code so it can be done in polled mode.
6907 	 */
6908 
6909 	medium_present = TRUE;
6910 
6911 	/*
6912 	 * When powering up, issue a TUR in case the device is at unit
6913 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
6914 	 * a deadlock on un_pm_busy_cv will occur.
6915 	 */
6916 	if (SD_PM_IS_IO_CAPABLE(un, level)) {
6917 		sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6918 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6919 		if (sval != 0)
6920 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6921 	}
6922 
6923 	if (un->un_f_power_condition_supported) {
6924 		char *pm_condition_name[] = {"STOPPED", "STANDBY",
6925 		    "IDLE", "ACTIVE"};
6926 		SD_TRACE(SD_LOG_IO_PM, un,
6927 		    "sdpower: sending \'%s\' power condition",
6928 		    pm_condition_name[level]);
6929 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6930 		    sd_pl2pc[level], SD_PATH_DIRECT);
6931 	} else {
6932 		SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6933 		    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6934 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6935 		    ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6936 		    SD_TARGET_STOP), SD_PATH_DIRECT);
6937 	}
6938 	if (sval != 0) {
6939 		if (sval == EIO)
6940 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6941 		else
6942 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6943 	}
6944 
6945 	/* Command failed, check for media present. */
6946 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
6947 		medium_present = FALSE;
6948 	}
6949 
6950 	/*
6951 	 * The conditions of interest here are:
6952 	 *   if a spindle off with media present fails,
6953 	 *	then restore the state and return an error.
6954 	 *   else if a spindle on fails,
6955 	 *	then return an error (there's no state to restore).
6956 	 * In all other cases we setup for the new state
6957 	 * and return success.
6958 	 */
6959 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6960 		if ((medium_present == TRUE) && (sval != 0)) {
6961 			/* The stop command from above failed */
6962 			rval = DDI_FAILURE;
6963 			/*
6964 			 * The stop command failed, and we have media
6965 			 * present. Put the level back by calling the
6966 			 * sd_pm_resume() and set the state back to
6967 			 * it's previous value.
6968 			 */
6969 			(void) sd_pm_state_change(un, last_power_level,
6970 			    SD_PM_STATE_ROLLBACK);
6971 			mutex_enter(SD_MUTEX(un));
6972 			un->un_last_state = save_state;
6973 			mutex_exit(SD_MUTEX(un));
6974 		} else if (un->un_f_monitor_media_state) {
6975 			/*
6976 			 * The stop command from above succeeded.
6977 			 * Terminate watch thread in case of removable media
6978 			 * devices going into low power state. This is as per
6979 			 * the requirements of pm framework, otherwise commands
6980 			 * will be generated for the device (through watch
6981 			 * thread), even when the device is in low power state.
6982 			 */
6983 			mutex_enter(SD_MUTEX(un));
6984 			un->un_f_watcht_stopped = FALSE;
6985 			if (un->un_swr_token != NULL) {
6986 				opaque_t temp_token = un->un_swr_token;
6987 				un->un_f_watcht_stopped = TRUE;
6988 				un->un_swr_token = NULL;
6989 				mutex_exit(SD_MUTEX(un));
6990 				(void) scsi_watch_request_terminate(temp_token,
6991 				    SCSI_WATCH_TERMINATE_ALL_WAIT);
6992 			} else {
6993 				mutex_exit(SD_MUTEX(un));
6994 			}
6995 		}
6996 	} else {
6997 		/*
6998 		 * The level requested is I/O capable.
6999 		 * Legacy behavior: return success on a failed spinup
7000 		 * if there is no media in the drive.
7001 		 * Do this by looking at medium_present here.
7002 		 */
7003 		if ((sval != 0) && medium_present) {
7004 			/* The start command from above failed */
7005 			rval = DDI_FAILURE;
7006 		} else {
7007 			/*
7008 			 * The start command from above succeeded
7009 			 * PM resume the devices now that we have
7010 			 * started the disks
7011 			 */
7012 			(void) sd_pm_state_change(un, level,
7013 			    SD_PM_STATE_CHANGE);
7014 
7015 			/*
7016 			 * Resume the watch thread since it was suspended
7017 			 * when the device went into low power mode.
7018 			 */
7019 			if (un->un_f_monitor_media_state) {
7020 				mutex_enter(SD_MUTEX(un));
7021 				if (un->un_f_watcht_stopped == TRUE) {
7022 					opaque_t temp_token;
7023 
7024 					un->un_f_watcht_stopped = FALSE;
7025 					mutex_exit(SD_MUTEX(un));
7026 					temp_token =
7027 					    sd_watch_request_submit(un);
7028 					mutex_enter(SD_MUTEX(un));
7029 					un->un_swr_token = temp_token;
7030 				}
7031 				mutex_exit(SD_MUTEX(un));
7032 			}
7033 		}
7034 	}
7035 
7036 	if (got_semaphore_here != 0) {
7037 		sema_v(&un->un_semoclose);
7038 	}
7039 	/*
7040 	 * On exit put the state back to it's original value
7041 	 * and broadcast to anyone waiting for the power
7042 	 * change completion.
7043 	 */
7044 	mutex_enter(SD_MUTEX(un));
7045 	un->un_state = state_before_pm;
7046 	cv_broadcast(&un->un_suspend_cv);
7047 	mutex_exit(SD_MUTEX(un));
7048 
7049 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7050 
7051 	sd_ssc_fini(ssc);
7052 	return (rval);
7053 
7054 sdpower_failed:
7055 
7056 	sd_ssc_fini(ssc);
7057 	return (DDI_FAILURE);
7058 }
7059 
7060 
7061 
7062 /*
7063  *    Function: sdattach
7064  *
7065  * Description: Driver's attach(9e) entry point function.
7066  *
7067  *   Arguments: devi - opaque device info handle
7068  *		cmd  - attach  type
7069  *
7070  * Return Code: DDI_SUCCESS
7071  *		DDI_FAILURE
7072  *
7073  *     Context: Kernel thread context
7074  */
7075 
7076 static int
7077 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7078 {
7079 	switch (cmd) {
7080 	case DDI_ATTACH:
7081 		return (sd_unit_attach(devi));
7082 	case DDI_RESUME:
7083 		return (sd_ddi_resume(devi));
7084 	default:
7085 		break;
7086 	}
7087 	return (DDI_FAILURE);
7088 }
7089 
7090 
7091 /*
7092  *    Function: sddetach
7093  *
7094  * Description: Driver's detach(9E) entry point function.
7095  *
7096  *   Arguments: devi - opaque device info handle
7097  *		cmd  - detach  type
7098  *
7099  * Return Code: DDI_SUCCESS
7100  *		DDI_FAILURE
7101  *
7102  *     Context: Kernel thread context
7103  */
7104 
7105 static int
7106 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7107 {
7108 	switch (cmd) {
7109 	case DDI_DETACH:
7110 		return (sd_unit_detach(devi));
7111 	case DDI_SUSPEND:
7112 		return (sd_ddi_suspend(devi));
7113 	default:
7114 		break;
7115 	}
7116 	return (DDI_FAILURE);
7117 }
7118 
7119 
7120 /*
7121  *     Function: sd_sync_with_callback
7122  *
7123  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7124  *		 state while the callback routine is active.
7125  *
7126  *    Arguments: un: softstate structure for the instance
7127  *
7128  *	Context: Kernel thread context
7129  */
7130 
7131 static void
7132 sd_sync_with_callback(struct sd_lun *un)
7133 {
7134 	ASSERT(un != NULL);
7135 
7136 	mutex_enter(SD_MUTEX(un));
7137 
7138 	ASSERT(un->un_in_callback >= 0);
7139 
7140 	while (un->un_in_callback > 0) {
7141 		mutex_exit(SD_MUTEX(un));
7142 		delay(2);
7143 		mutex_enter(SD_MUTEX(un));
7144 	}
7145 
7146 	mutex_exit(SD_MUTEX(un));
7147 }
7148 
7149 /*
7150  *    Function: sd_unit_attach
7151  *
7152  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7153  *		the soft state structure for the device and performs
7154  *		all necessary structure and device initializations.
7155  *
7156  *   Arguments: devi: the system's dev_info_t for the device.
7157  *
7158  * Return Code: DDI_SUCCESS if attach is successful.
7159  *		DDI_FAILURE if any part of the attach fails.
7160  *
7161  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7162  *		Kernel thread context only.  Can sleep.
7163  */
7164 
7165 static int
7166 sd_unit_attach(dev_info_t *devi)
7167 {
7168 	struct	scsi_device	*devp;
7169 	struct	sd_lun		*un;
7170 	char			*variantp;
7171 	char			name_str[48];
7172 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7173 	int	instance;
7174 	int	rval;
7175 	int	wc_enabled;
7176 	int	tgt;
7177 	uint64_t	capacity;
7178 	uint_t		lbasize = 0;
7179 	dev_info_t	*pdip = ddi_get_parent(devi);
7180 	int		offbyone = 0;
7181 	int		geom_label_valid = 0;
7182 	sd_ssc_t	*ssc;
7183 	int		status;
7184 	struct sd_fm_internal	*sfip = NULL;
7185 	int		max_xfer_size;
7186 
7187 	/*
7188 	 * Retrieve the target driver's private data area. This was set
7189 	 * up by the HBA.
7190 	 */
7191 	devp = ddi_get_driver_private(devi);
7192 
7193 	/*
7194 	 * Retrieve the target ID of the device.
7195 	 */
7196 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7197 	    SCSI_ADDR_PROP_TARGET, -1);
7198 
7199 	/*
7200 	 * Since we have no idea what state things were left in by the last
7201 	 * user of the device, set up some 'default' settings, ie. turn 'em
7202 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7203 	 * Do this before the scsi_probe, which sends an inquiry.
7204 	 * This is a fix for bug (4430280).
7205 	 * Of special importance is wide-xfer. The drive could have been left
7206 	 * in wide transfer mode by the last driver to communicate with it,
7207 	 * this includes us. If that's the case, and if the following is not
7208 	 * setup properly or we don't re-negotiate with the drive prior to
7209 	 * transferring data to/from the drive, it causes bus parity errors,
7210 	 * data overruns, and unexpected interrupts. This first occurred when
7211 	 * the fix for bug (4378686) was made.
7212 	 */
7213 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7214 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7215 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7216 
7217 	/*
7218 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7219 	 * on a target. Setting it per lun instance actually sets the
7220 	 * capability of this target, which affects those luns already
7221 	 * attached on the same target. So during attach, we can only disable
7222 	 * this capability only when no other lun has been attached on this
7223 	 * target. By doing this, we assume a target has the same tagged-qing
7224 	 * capability for every lun. The condition can be removed when HBA
7225 	 * is changed to support per lun based tagged-qing capability.
7226 	 */
7227 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7228 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7229 	}
7230 
7231 	/*
7232 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7233 	 * This call will allocate and fill in the scsi_inquiry structure
7234 	 * and point the sd_inq member of the scsi_device structure to it.
7235 	 * If the attach succeeds, then this memory will not be de-allocated
7236 	 * (via scsi_unprobe()) until the instance is detached.
7237 	 */
7238 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7239 		goto probe_failed;
7240 	}
7241 
7242 	/*
7243 	 * Check the device type as specified in the inquiry data and
7244 	 * claim it if it is of a type that we support.
7245 	 */
7246 	switch (devp->sd_inq->inq_dtype) {
7247 	case DTYPE_DIRECT:
7248 		break;
7249 	case DTYPE_RODIRECT:
7250 		break;
7251 	case DTYPE_OPTICAL:
7252 		break;
7253 	case DTYPE_NOTPRESENT:
7254 	default:
7255 		/* Unsupported device type; fail the attach. */
7256 		goto probe_failed;
7257 	}
7258 
7259 	/*
7260 	 * Allocate the soft state structure for this unit.
7261 	 *
7262 	 * We rely upon this memory being set to all zeroes by
7263 	 * ddi_soft_state_zalloc().  We assume that any member of the
7264 	 * soft state structure that is not explicitly initialized by
7265 	 * this routine will have a value of zero.
7266 	 */
7267 	instance = ddi_get_instance(devp->sd_dev);
7268 #ifndef XPV_HVM_DRIVER
7269 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7270 		goto probe_failed;
7271 	}
7272 #endif /* !XPV_HVM_DRIVER */
7273 
7274 	/*
7275 	 * Retrieve a pointer to the newly-allocated soft state.
7276 	 *
7277 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7278 	 * was successful, unless something has gone horribly wrong and the
7279 	 * ddi's soft state internals are corrupt (in which case it is
7280 	 * probably better to halt here than just fail the attach....)
7281 	 */
7282 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7283 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7284 		    instance);
7285 		/*NOTREACHED*/
7286 	}
7287 
7288 	/*
7289 	 * Link the back ptr of the driver soft state to the scsi_device
7290 	 * struct for this lun.
7291 	 * Save a pointer to the softstate in the driver-private area of
7292 	 * the scsi_device struct.
7293 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7294 	 * we first set un->un_sd below.
7295 	 */
7296 	un->un_sd = devp;
7297 	devp->sd_private = (opaque_t)un;
7298 
7299 	/*
7300 	 * The following must be after devp is stored in the soft state struct.
7301 	 */
7302 #ifdef SDDEBUG
7303 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7304 	    "%s_unit_attach: un:0x%p instance:%d\n",
7305 	    ddi_driver_name(devi), un, instance);
7306 #endif
7307 
7308 	/*
7309 	 * Set up the device type and node type (for the minor nodes).
7310 	 * By default we assume that the device can at least support the
7311 	 * Common Command Set. Call it a CD-ROM if it reports itself
7312 	 * as a RODIRECT device.
7313 	 */
7314 	switch (devp->sd_inq->inq_dtype) {
7315 	case DTYPE_RODIRECT:
7316 		un->un_node_type = DDI_NT_CD_CHAN;
7317 		un->un_ctype	 = CTYPE_CDROM;
7318 		break;
7319 	case DTYPE_OPTICAL:
7320 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7321 		un->un_ctype	 = CTYPE_ROD;
7322 		break;
7323 	default:
7324 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7325 		un->un_ctype	 = CTYPE_CCS;
7326 		break;
7327 	}
7328 
7329 	/*
7330 	 * Try to read the interconnect type from the HBA.
7331 	 *
7332 	 * Note: This driver is currently compiled as two binaries, a parallel
7333 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7334 	 * differences are determined at compile time. In the future a single
7335 	 * binary will be provided and the interconnect type will be used to
7336 	 * differentiate between fibre and parallel scsi behaviors. At that time
7337 	 * it will be necessary for all fibre channel HBAs to support this
7338 	 * property.
7339 	 *
7340 	 * set un_f_is_fiber to TRUE ( default fiber )
7341 	 */
7342 	un->un_f_is_fibre = TRUE;
7343 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7344 	case INTERCONNECT_SSA:
7345 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7346 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7347 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7348 		break;
7349 	case INTERCONNECT_PARALLEL:
7350 		un->un_f_is_fibre = FALSE;
7351 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7352 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7353 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7354 		break;
7355 	case INTERCONNECT_SAS:
7356 		un->un_f_is_fibre = FALSE;
7357 		un->un_interconnect_type = SD_INTERCONNECT_SAS;
7358 		un->un_node_type = DDI_NT_BLOCK_SAS;
7359 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7360 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7361 		break;
7362 	case INTERCONNECT_SATA:
7363 		un->un_f_is_fibre = FALSE;
7364 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
7365 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7366 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7367 		break;
7368 	case INTERCONNECT_FIBRE:
7369 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7370 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7371 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7372 		break;
7373 	case INTERCONNECT_FABRIC:
7374 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7375 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7376 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7377 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7378 		break;
7379 	default:
7380 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7381 		/*
7382 		 * The HBA does not support the "interconnect-type" property
7383 		 * (or did not provide a recognized type).
7384 		 *
7385 		 * Note: This will be obsoleted when a single fibre channel
7386 		 * and parallel scsi driver is delivered. In the meantime the
7387 		 * interconnect type will be set to the platform default.If that
7388 		 * type is not parallel SCSI, it means that we should be
7389 		 * assuming "ssd" semantics. However, here this also means that
7390 		 * the FC HBA is not supporting the "interconnect-type" property
7391 		 * like we expect it to, so log this occurrence.
7392 		 */
7393 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7394 		if (!SD_IS_PARALLEL_SCSI(un)) {
7395 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7396 			    "sd_unit_attach: un:0x%p Assuming "
7397 			    "INTERCONNECT_FIBRE\n", un);
7398 		} else {
7399 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7400 			    "sd_unit_attach: un:0x%p Assuming "
7401 			    "INTERCONNECT_PARALLEL\n", un);
7402 			un->un_f_is_fibre = FALSE;
7403 		}
7404 #else
7405 		/*
7406 		 * Note: This source will be implemented when a single fibre
7407 		 * channel and parallel scsi driver is delivered. The default
7408 		 * will be to assume that if a device does not support the
7409 		 * "interconnect-type" property it is a parallel SCSI HBA and
7410 		 * we will set the interconnect type for parallel scsi.
7411 		 */
7412 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7413 		un->un_f_is_fibre = FALSE;
7414 #endif
7415 		break;
7416 	}
7417 
7418 	if (un->un_f_is_fibre == TRUE) {
7419 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7420 		    SCSI_VERSION_3) {
7421 			switch (un->un_interconnect_type) {
7422 			case SD_INTERCONNECT_FIBRE:
7423 			case SD_INTERCONNECT_SSA:
7424 				un->un_node_type = DDI_NT_BLOCK_WWN;
7425 				break;
7426 			default:
7427 				break;
7428 			}
7429 		}
7430 	}
7431 
7432 	/*
7433 	 * Initialize the Request Sense command for the target
7434 	 */
7435 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7436 		goto alloc_rqs_failed;
7437 	}
7438 
7439 	/*
7440 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7441 	 * with separate binary for sd and ssd.
7442 	 *
7443 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7444 	 * The hardcoded values will go away when Sparc uses 1 binary
7445 	 * for sd and ssd.  This hardcoded values need to match
7446 	 * SD_RETRY_COUNT in sddef.h
7447 	 * The value used is base on interconnect type.
7448 	 * fibre = 3, parallel = 5
7449 	 */
7450 #if defined(__i386) || defined(__amd64)
7451 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7452 #else
7453 	un->un_retry_count = SD_RETRY_COUNT;
7454 #endif
7455 
7456 	/*
7457 	 * Set the per disk retry count to the default number of retries
7458 	 * for disks and CDROMs. This value can be overridden by the
7459 	 * disk property list or an entry in sd.conf.
7460 	 */
7461 	un->un_notready_retry_count =
7462 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7463 	    : DISK_NOT_READY_RETRY_COUNT(un);
7464 
7465 	/*
7466 	 * Set the busy retry count to the default value of un_retry_count.
7467 	 * This can be overridden by entries in sd.conf or the device
7468 	 * config table.
7469 	 */
7470 	un->un_busy_retry_count = un->un_retry_count;
7471 
7472 	/*
7473 	 * Init the reset threshold for retries.  This number determines
7474 	 * how many retries must be performed before a reset can be issued
7475 	 * (for certain error conditions). This can be overridden by entries
7476 	 * in sd.conf or the device config table.
7477 	 */
7478 	un->un_reset_retry_count = (un->un_retry_count / 2);
7479 
7480 	/*
7481 	 * Set the victim_retry_count to the default un_retry_count
7482 	 */
7483 	un->un_victim_retry_count = (2 * un->un_retry_count);
7484 
7485 	/*
7486 	 * Set the reservation release timeout to the default value of
7487 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7488 	 * device config table.
7489 	 */
7490 	un->un_reserve_release_time = 5;
7491 
7492 	/*
7493 	 * Set up the default maximum transfer size. Note that this may
7494 	 * get updated later in the attach, when setting up default wide
7495 	 * operations for disks.
7496 	 */
7497 #if defined(__i386) || defined(__amd64)
7498 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7499 	un->un_partial_dma_supported = 1;
7500 #else
7501 	un->un_max_xfer_size = (uint_t)maxphys;
7502 #endif
7503 
7504 	/*
7505 	 * Get "allow bus device reset" property (defaults to "enabled" if
7506 	 * the property was not defined). This is to disable bus resets for
7507 	 * certain kinds of error recovery. Note: In the future when a run-time
7508 	 * fibre check is available the soft state flag should default to
7509 	 * enabled.
7510 	 */
7511 	if (un->un_f_is_fibre == TRUE) {
7512 		un->un_f_allow_bus_device_reset = TRUE;
7513 	} else {
7514 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7515 		    "allow-bus-device-reset", 1) != 0) {
7516 			un->un_f_allow_bus_device_reset = TRUE;
7517 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7518 			    "sd_unit_attach: un:0x%p Bus device reset "
7519 			    "enabled\n", un);
7520 		} else {
7521 			un->un_f_allow_bus_device_reset = FALSE;
7522 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7523 			    "sd_unit_attach: un:0x%p Bus device reset "
7524 			    "disabled\n", un);
7525 		}
7526 	}
7527 
7528 	/*
7529 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7530 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7531 	 *
7532 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7533 	 * property. The new "variant" property with a value of "atapi" has been
7534 	 * introduced so that future 'variants' of standard SCSI behavior (like
7535 	 * atapi) could be specified by the underlying HBA drivers by supplying
7536 	 * a new value for the "variant" property, instead of having to define a
7537 	 * new property.
7538 	 */
7539 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7540 		un->un_f_cfg_is_atapi = TRUE;
7541 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7542 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7543 	}
7544 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7545 	    &variantp) == DDI_PROP_SUCCESS) {
7546 		if (strcmp(variantp, "atapi") == 0) {
7547 			un->un_f_cfg_is_atapi = TRUE;
7548 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7549 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7550 		}
7551 		ddi_prop_free(variantp);
7552 	}
7553 
7554 	un->un_cmd_timeout	= SD_IO_TIME;
7555 
7556 	un->un_busy_timeout  = SD_BSY_TIMEOUT;
7557 
7558 	/* Info on current states, statuses, etc. (Updated frequently) */
7559 	un->un_state		= SD_STATE_NORMAL;
7560 	un->un_last_state	= SD_STATE_NORMAL;
7561 
7562 	/* Control & status info for command throttling */
7563 	un->un_throttle		= sd_max_throttle;
7564 	un->un_saved_throttle	= sd_max_throttle;
7565 	un->un_min_throttle	= sd_min_throttle;
7566 
7567 	if (un->un_f_is_fibre == TRUE) {
7568 		un->un_f_use_adaptive_throttle = TRUE;
7569 	} else {
7570 		un->un_f_use_adaptive_throttle = FALSE;
7571 	}
7572 
7573 	/* Removable media support. */
7574 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7575 	un->un_mediastate		= DKIO_NONE;
7576 	un->un_specified_mediastate	= DKIO_NONE;
7577 
7578 	/* CVs for suspend/resume (PM or DR) */
7579 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7580 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7581 
7582 	/* Power management support. */
7583 	un->un_power_level = SD_SPINDLE_UNINIT;
7584 
7585 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7586 	un->un_f_wcc_inprog = 0;
7587 
7588 	/*
7589 	 * The open/close semaphore is used to serialize threads executing
7590 	 * in the driver's open & close entry point routines for a given
7591 	 * instance.
7592 	 */
7593 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7594 
7595 	/*
7596 	 * The conf file entry and softstate variable is a forceful override,
7597 	 * meaning a non-zero value must be entered to change the default.
7598 	 */
7599 	un->un_f_disksort_disabled = FALSE;
7600 	un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7601 	un->un_f_enable_rmw = FALSE;
7602 
7603 	/*
7604 	 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7605 	 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7606 	 */
7607 	un->un_f_mmc_gesn_polling = TRUE;
7608 
7609 	/*
7610 	 * Retrieve the properties from the static driver table or the driver
7611 	 * configuration file (.conf) for this unit and update the soft state
7612 	 * for the device as needed for the indicated properties.
7613 	 * Note: the property configuration needs to occur here as some of the
7614 	 * following routines may have dependencies on soft state flags set
7615 	 * as part of the driver property configuration.
7616 	 */
7617 	sd_read_unit_properties(un);
7618 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7619 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7620 
7621 	/*
7622 	 * Only if a device has "hotpluggable" property, it is
7623 	 * treated as hotpluggable device. Otherwise, it is
7624 	 * regarded as non-hotpluggable one.
7625 	 */
7626 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7627 	    -1) != -1) {
7628 		un->un_f_is_hotpluggable = TRUE;
7629 	}
7630 
7631 	/*
7632 	 * set unit's attributes(flags) according to "hotpluggable" and
7633 	 * RMB bit in INQUIRY data.
7634 	 */
7635 	sd_set_unit_attributes(un, devi);
7636 
7637 	/*
7638 	 * By default, we mark the capacity, lbasize, and geometry
7639 	 * as invalid. Only if we successfully read a valid capacity
7640 	 * will we update the un_blockcount and un_tgt_blocksize with the
7641 	 * valid values (the geometry will be validated later).
7642 	 */
7643 	un->un_f_blockcount_is_valid	= FALSE;
7644 	un->un_f_tgt_blocksize_is_valid	= FALSE;
7645 
7646 	/*
7647 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7648 	 * otherwise.
7649 	 */
7650 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7651 	un->un_blockcount = 0;
7652 
7653 	/*
7654 	 * physical sector size default to DEV_BSIZE currently.
7655 	 */
7656 	un->un_phy_blocksize = DEV_BSIZE;
7657 
7658 	/*
7659 	 * Set up the per-instance info needed to determine the correct
7660 	 * CDBs and other info for issuing commands to the target.
7661 	 */
7662 	sd_init_cdb_limits(un);
7663 
7664 	/*
7665 	 * Set up the IO chains to use, based upon the target type.
7666 	 */
7667 	if (un->un_f_non_devbsize_supported) {
7668 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7669 	} else {
7670 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7671 	}
7672 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7673 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7674 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7675 
7676 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7677 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7678 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7679 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7680 
7681 
7682 	if (ISCD(un)) {
7683 		un->un_additional_codes = sd_additional_codes;
7684 	} else {
7685 		un->un_additional_codes = NULL;
7686 	}
7687 
7688 	/*
7689 	 * Create the kstats here so they can be available for attach-time
7690 	 * routines that send commands to the unit (either polled or via
7691 	 * sd_send_scsi_cmd).
7692 	 *
7693 	 * Note: This is a critical sequence that needs to be maintained:
7694 	 *	1) Instantiate the kstats here, before any routines using the
7695 	 *	   iopath (i.e. sd_send_scsi_cmd).
7696 	 *	2) Instantiate and initialize the partition stats
7697 	 *	   (sd_set_pstats).
7698 	 *	3) Initialize the error stats (sd_set_errstats), following
7699 	 *	   sd_validate_geometry(),sd_register_devid(),
7700 	 *	   and sd_cache_control().
7701 	 */
7702 
7703 	un->un_stats = kstat_create(sd_label, instance,
7704 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7705 	if (un->un_stats != NULL) {
7706 		un->un_stats->ks_lock = SD_MUTEX(un);
7707 		kstat_install(un->un_stats);
7708 	}
7709 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7710 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
7711 
7712 	sd_create_errstats(un, instance);
7713 	if (un->un_errstats == NULL) {
7714 		goto create_errstats_failed;
7715 	}
7716 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7717 	    "sd_unit_attach: un:0x%p errstats created\n", un);
7718 
7719 	/*
7720 	 * The following if/else code was relocated here from below as part
7721 	 * of the fix for bug (4430280). However with the default setup added
7722 	 * on entry to this routine, it's no longer absolutely necessary for
7723 	 * this to be before the call to sd_spin_up_unit.
7724 	 */
7725 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7726 		int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7727 		    (devp->sd_inq->inq_ansi == 5)) &&
7728 		    devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7729 
7730 		/*
7731 		 * If tagged queueing is supported by the target
7732 		 * and by the host adapter then we will enable it
7733 		 */
7734 		un->un_tagflags = 0;
7735 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7736 		    (un->un_f_arq_enabled == TRUE)) {
7737 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7738 			    1, 1) == 1) {
7739 				un->un_tagflags = FLAG_STAG;
7740 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7741 				    "sd_unit_attach: un:0x%p tag queueing "
7742 				    "enabled\n", un);
7743 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
7744 			    "untagged-qing", 0) == 1) {
7745 				un->un_f_opt_queueing = TRUE;
7746 				un->un_saved_throttle = un->un_throttle =
7747 				    min(un->un_throttle, 3);
7748 			} else {
7749 				un->un_f_opt_queueing = FALSE;
7750 				un->un_saved_throttle = un->un_throttle = 1;
7751 			}
7752 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7753 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
7754 			/* The Host Adapter supports internal queueing. */
7755 			un->un_f_opt_queueing = TRUE;
7756 			un->un_saved_throttle = un->un_throttle =
7757 			    min(un->un_throttle, 3);
7758 		} else {
7759 			un->un_f_opt_queueing = FALSE;
7760 			un->un_saved_throttle = un->un_throttle = 1;
7761 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7762 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
7763 		}
7764 
7765 		/*
7766 		 * Enable large transfers for SATA/SAS drives
7767 		 */
7768 		if (SD_IS_SERIAL(un)) {
7769 			un->un_max_xfer_size =
7770 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7771 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7772 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7773 			    "sd_unit_attach: un:0x%p max transfer "
7774 			    "size=0x%x\n", un, un->un_max_xfer_size);
7775 
7776 		}
7777 
7778 		/* Setup or tear down default wide operations for disks */
7779 
7780 		/*
7781 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7782 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
7783 		 * system and be set to different values. In the future this
7784 		 * code may need to be updated when the ssd module is
7785 		 * obsoleted and removed from the system. (4299588)
7786 		 */
7787 		if (SD_IS_PARALLEL_SCSI(un) &&
7788 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7789 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7790 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7791 			    1, 1) == 1) {
7792 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7793 				    "sd_unit_attach: un:0x%p Wide Transfer "
7794 				    "enabled\n", un);
7795 			}
7796 
7797 			/*
7798 			 * If tagged queuing has also been enabled, then
7799 			 * enable large xfers
7800 			 */
7801 			if (un->un_saved_throttle == sd_max_throttle) {
7802 				un->un_max_xfer_size =
7803 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7804 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7805 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7806 				    "sd_unit_attach: un:0x%p max transfer "
7807 				    "size=0x%x\n", un, un->un_max_xfer_size);
7808 			}
7809 		} else {
7810 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7811 			    0, 1) == 1) {
7812 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7813 				    "sd_unit_attach: un:0x%p "
7814 				    "Wide Transfer disabled\n", un);
7815 			}
7816 		}
7817 	} else {
7818 		un->un_tagflags = FLAG_STAG;
7819 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7820 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7821 	}
7822 
7823 	/*
7824 	 * If this target supports LUN reset, try to enable it.
7825 	 */
7826 	if (un->un_f_lun_reset_enabled) {
7827 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7828 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7829 			    "un:0x%p lun_reset capability set\n", un);
7830 		} else {
7831 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7832 			    "un:0x%p lun-reset capability not set\n", un);
7833 		}
7834 	}
7835 
7836 	/*
7837 	 * Adjust the maximum transfer size. This is to fix
7838 	 * the problem of partial DMA support on SPARC. Some
7839 	 * HBA driver, like aac, has very small dma_attr_maxxfer
7840 	 * size, which requires partial DMA support on SPARC.
7841 	 * In the future the SPARC pci nexus driver may solve
7842 	 * the problem instead of this fix.
7843 	 */
7844 	max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7845 	if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7846 		/* We need DMA partial even on sparc to ensure sddump() works */
7847 		un->un_max_xfer_size = max_xfer_size;
7848 		if (un->un_partial_dma_supported == 0)
7849 			un->un_partial_dma_supported = 1;
7850 	}
7851 	if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7852 	    DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7853 		if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7854 		    un->un_max_xfer_size) == 1) {
7855 			un->un_buf_breakup_supported = 1;
7856 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7857 			    "un:0x%p Buf breakup enabled\n", un);
7858 		}
7859 	}
7860 
7861 	/*
7862 	 * Set PKT_DMA_PARTIAL flag.
7863 	 */
7864 	if (un->un_partial_dma_supported == 1) {
7865 		un->un_pkt_flags = PKT_DMA_PARTIAL;
7866 	} else {
7867 		un->un_pkt_flags = 0;
7868 	}
7869 
7870 	/* Initialize sd_ssc_t for internal uscsi commands */
7871 	ssc = sd_ssc_init(un);
7872 	scsi_fm_init(devp);
7873 
7874 	/*
7875 	 * Allocate memory for SCSI FMA stuffs.
7876 	 */
7877 	un->un_fm_private =
7878 	    kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7879 	sfip = (struct sd_fm_internal *)un->un_fm_private;
7880 	sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7881 	sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7882 	sfip->fm_ssc.ssc_un = un;
7883 
7884 	if (ISCD(un) ||
7885 	    un->un_f_has_removable_media ||
7886 	    devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7887 		/*
7888 		 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7889 		 * Their log are unchanged.
7890 		 */
7891 		sfip->fm_log_level = SD_FM_LOG_NSUP;
7892 	} else {
7893 		/*
7894 		 * If enter here, it should be non-CDROM and FM-capable
7895 		 * device, and it will not keep the old scsi_log as before
7896 		 * in /var/adm/messages. However, the property
7897 		 * "fm-scsi-log" will control whether the FM telemetry will
7898 		 * be logged in /var/adm/messages.
7899 		 */
7900 		int fm_scsi_log;
7901 		fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7902 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7903 
7904 		if (fm_scsi_log)
7905 			sfip->fm_log_level = SD_FM_LOG_EREPORT;
7906 		else
7907 			sfip->fm_log_level = SD_FM_LOG_SILENT;
7908 	}
7909 
7910 	/*
7911 	 * At this point in the attach, we have enough info in the
7912 	 * soft state to be able to issue commands to the target.
7913 	 *
7914 	 * All command paths used below MUST issue their commands as
7915 	 * SD_PATH_DIRECT. This is important as intermediate layers
7916 	 * are not all initialized yet (such as PM).
7917 	 */
7918 
7919 	/*
7920 	 * Send a TEST UNIT READY command to the device. This should clear
7921 	 * any outstanding UNIT ATTENTION that may be present.
7922 	 *
7923 	 * Note: Don't check for success, just track if there is a reservation,
7924 	 * this is a throw away command to clear any unit attentions.
7925 	 *
7926 	 * Note: This MUST be the first command issued to the target during
7927 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
7928 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7929 	 * with attempts at spinning up a device with no media.
7930 	 */
7931 	status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7932 	if (status != 0) {
7933 		if (status == EACCES)
7934 			reservation_flag = SD_TARGET_IS_RESERVED;
7935 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7936 	}
7937 
7938 	/*
7939 	 * If the device is NOT a removable media device, attempt to spin
7940 	 * it up (using the START_STOP_UNIT command) and read its capacity
7941 	 * (using the READ CAPACITY command).  Note, however, that either
7942 	 * of these could fail and in some cases we would continue with
7943 	 * the attach despite the failure (see below).
7944 	 */
7945 	if (un->un_f_descr_format_supported) {
7946 
7947 		switch (sd_spin_up_unit(ssc)) {
7948 		case 0:
7949 			/*
7950 			 * Spin-up was successful; now try to read the
7951 			 * capacity.  If successful then save the results
7952 			 * and mark the capacity & lbasize as valid.
7953 			 */
7954 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7955 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
7956 
7957 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
7958 			    &lbasize, SD_PATH_DIRECT);
7959 
7960 			switch (status) {
7961 			case 0: {
7962 				if (capacity > DK_MAX_BLOCKS) {
7963 #ifdef _LP64
7964 					if ((capacity + 1) >
7965 					    SD_GROUP1_MAX_ADDRESS) {
7966 						/*
7967 						 * Enable descriptor format
7968 						 * sense data so that we can
7969 						 * get 64 bit sense data
7970 						 * fields.
7971 						 */
7972 						sd_enable_descr_sense(ssc);
7973 					}
7974 #else
7975 					/* 32-bit kernels can't handle this */
7976 					scsi_log(SD_DEVINFO(un),
7977 					    sd_label, CE_WARN,
7978 					    "disk has %llu blocks, which "
7979 					    "is too large for a 32-bit "
7980 					    "kernel", capacity);
7981 
7982 #if defined(__i386) || defined(__amd64)
7983 					/*
7984 					 * 1TB disk was treated as (1T - 512)B
7985 					 * in the past, so that it might have
7986 					 * valid VTOC and solaris partitions,
7987 					 * we have to allow it to continue to
7988 					 * work.
7989 					 */
7990 					if (capacity -1 > DK_MAX_BLOCKS)
7991 #endif
7992 					goto spinup_failed;
7993 #endif
7994 				}
7995 
7996 				/*
7997 				 * Here it's not necessary to check the case:
7998 				 * the capacity of the device is bigger than
7999 				 * what the max hba cdb can support. Because
8000 				 * sd_send_scsi_READ_CAPACITY will retrieve
8001 				 * the capacity by sending USCSI command, which
8002 				 * is constrained by the max hba cdb. Actually,
8003 				 * sd_send_scsi_READ_CAPACITY will return
8004 				 * EINVAL when using bigger cdb than required
8005 				 * cdb length. Will handle this case in
8006 				 * "case EINVAL".
8007 				 */
8008 
8009 				/*
8010 				 * The following relies on
8011 				 * sd_send_scsi_READ_CAPACITY never
8012 				 * returning 0 for capacity and/or lbasize.
8013 				 */
8014 				sd_update_block_info(un, lbasize, capacity);
8015 
8016 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8017 				    "sd_unit_attach: un:0x%p capacity = %ld "
8018 				    "blocks; lbasize= %ld.\n", un,
8019 				    un->un_blockcount, un->un_tgt_blocksize);
8020 
8021 				break;
8022 			}
8023 			case EINVAL:
8024 				/*
8025 				 * In the case where the max-cdb-length property
8026 				 * is smaller than the required CDB length for
8027 				 * a SCSI device, a target driver can fail to
8028 				 * attach to that device.
8029 				 */
8030 				scsi_log(SD_DEVINFO(un),
8031 				    sd_label, CE_WARN,
8032 				    "disk capacity is too large "
8033 				    "for current cdb length");
8034 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8035 
8036 				goto spinup_failed;
8037 			case EACCES:
8038 				/*
8039 				 * Should never get here if the spin-up
8040 				 * succeeded, but code it in anyway.
8041 				 * From here, just continue with the attach...
8042 				 */
8043 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8044 				    "sd_unit_attach: un:0x%p "
8045 				    "sd_send_scsi_READ_CAPACITY "
8046 				    "returned reservation conflict\n", un);
8047 				reservation_flag = SD_TARGET_IS_RESERVED;
8048 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8049 				break;
8050 			default:
8051 				/*
8052 				 * Likewise, should never get here if the
8053 				 * spin-up succeeded. Just continue with
8054 				 * the attach...
8055 				 */
8056 				if (status == EIO)
8057 					sd_ssc_assessment(ssc,
8058 					    SD_FMT_STATUS_CHECK);
8059 				else
8060 					sd_ssc_assessment(ssc,
8061 					    SD_FMT_IGNORE);
8062 				break;
8063 			}
8064 			break;
8065 		case EACCES:
8066 			/*
8067 			 * Device is reserved by another host.  In this case
8068 			 * we could not spin it up or read the capacity, but
8069 			 * we continue with the attach anyway.
8070 			 */
8071 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8072 			    "sd_unit_attach: un:0x%p spin-up reservation "
8073 			    "conflict.\n", un);
8074 			reservation_flag = SD_TARGET_IS_RESERVED;
8075 			break;
8076 		default:
8077 			/* Fail the attach if the spin-up failed. */
8078 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8079 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8080 			goto spinup_failed;
8081 		}
8082 
8083 	}
8084 
8085 	/*
8086 	 * Check to see if this is a MMC drive
8087 	 */
8088 	if (ISCD(un)) {
8089 		sd_set_mmc_caps(ssc);
8090 	}
8091 
8092 	/*
8093 	 * Add a zero-length attribute to tell the world we support
8094 	 * kernel ioctls (for layered drivers)
8095 	 */
8096 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8097 	    DDI_KERNEL_IOCTL, NULL, 0);
8098 
8099 	/*
8100 	 * Add a boolean property to tell the world we support
8101 	 * the B_FAILFAST flag (for layered drivers)
8102 	 */
8103 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8104 	    "ddi-failfast-supported", NULL, 0);
8105 
8106 	/*
8107 	 * Initialize power management
8108 	 */
8109 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8110 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8111 	sd_setup_pm(ssc, devi);
8112 	if (un->un_f_pm_is_enabled == FALSE) {
8113 		/*
8114 		 * For performance, point to a jump table that does
8115 		 * not include pm.
8116 		 * The direct and priority chains don't change with PM.
8117 		 *
8118 		 * Note: this is currently done based on individual device
8119 		 * capabilities. When an interface for determining system
8120 		 * power enabled state becomes available, or when additional
8121 		 * layers are added to the command chain, these values will
8122 		 * have to be re-evaluated for correctness.
8123 		 */
8124 		if (un->un_f_non_devbsize_supported) {
8125 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8126 		} else {
8127 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8128 		}
8129 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8130 	}
8131 
8132 	/*
8133 	 * This property is set to 0 by HA software to avoid retries
8134 	 * on a reserved disk. (The preferred property name is
8135 	 * "retry-on-reservation-conflict") (1189689)
8136 	 *
8137 	 * Note: The use of a global here can have unintended consequences. A
8138 	 * per instance variable is preferable to match the capabilities of
8139 	 * different underlying hba's (4402600)
8140 	 */
8141 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8142 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8143 	    sd_retry_on_reservation_conflict);
8144 	if (sd_retry_on_reservation_conflict != 0) {
8145 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8146 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8147 		    sd_retry_on_reservation_conflict);
8148 	}
8149 
8150 	/* Set up options for QFULL handling. */
8151 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8152 	    "qfull-retries", -1)) != -1) {
8153 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8154 		    rval, 1);
8155 	}
8156 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8157 	    "qfull-retry-interval", -1)) != -1) {
8158 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8159 		    rval, 1);
8160 	}
8161 
8162 	/*
8163 	 * This just prints a message that announces the existence of the
8164 	 * device. The message is always printed in the system logfile, but
8165 	 * only appears on the console if the system is booted with the
8166 	 * -v (verbose) argument.
8167 	 */
8168 	ddi_report_dev(devi);
8169 
8170 	un->un_mediastate = DKIO_NONE;
8171 
8172 	/*
8173 	 * Check if this is a SSD(Solid State Drive).
8174 	 */
8175 	sd_check_solid_state(ssc);
8176 
8177 	/*
8178 	 * Check whether the drive is in emulation mode.
8179 	 */
8180 	sd_check_emulation_mode(ssc);
8181 
8182 	cmlb_alloc_handle(&un->un_cmlbhandle);
8183 
8184 #if defined(__i386) || defined(__amd64)
8185 	/*
8186 	 * On x86, compensate for off-by-1 legacy error
8187 	 */
8188 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8189 	    (lbasize == un->un_sys_blocksize))
8190 		offbyone = CMLB_OFF_BY_ONE;
8191 #endif
8192 
8193 	if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8194 	    VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8195 	    VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8196 	    un->un_node_type, offbyone, un->un_cmlbhandle,
8197 	    (void *)SD_PATH_DIRECT) != 0) {
8198 		goto cmlb_attach_failed;
8199 	}
8200 
8201 
8202 	/*
8203 	 * Read and validate the device's geometry (ie, disk label)
8204 	 * A new unformatted drive will not have a valid geometry, but
8205 	 * the driver needs to successfully attach to this device so
8206 	 * the drive can be formatted via ioctls.
8207 	 */
8208 	geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8209 	    (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8210 
8211 	mutex_enter(SD_MUTEX(un));
8212 
8213 	/*
8214 	 * Read and initialize the devid for the unit.
8215 	 */
8216 	if (un->un_f_devid_supported) {
8217 		sd_register_devid(ssc, devi, reservation_flag);
8218 	}
8219 	mutex_exit(SD_MUTEX(un));
8220 
8221 #if (defined(__fibre))
8222 	/*
8223 	 * Register callbacks for fibre only.  You can't do this solely
8224 	 * on the basis of the devid_type because this is hba specific.
8225 	 * We need to query our hba capabilities to find out whether to
8226 	 * register or not.
8227 	 */
8228 	if (un->un_f_is_fibre) {
8229 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8230 			sd_init_event_callbacks(un);
8231 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8232 			    "sd_unit_attach: un:0x%p event callbacks inserted",
8233 			    un);
8234 		}
8235 	}
8236 #endif
8237 
8238 	if (un->un_f_opt_disable_cache == TRUE) {
8239 		/*
8240 		 * Disable both read cache and write cache.  This is
8241 		 * the historic behavior of the keywords in the config file.
8242 		 */
8243 		if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8244 		    0) {
8245 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8246 			    "sd_unit_attach: un:0x%p Could not disable "
8247 			    "caching", un);
8248 			goto devid_failed;
8249 		}
8250 	}
8251 
8252 	/*
8253 	 * Check the value of the WCE bit now and
8254 	 * set un_f_write_cache_enabled accordingly.
8255 	 */
8256 	(void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8257 	mutex_enter(SD_MUTEX(un));
8258 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8259 	mutex_exit(SD_MUTEX(un));
8260 
8261 	if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8262 	    un->un_tgt_blocksize != DEV_BSIZE) ||
8263 	    un->un_f_enable_rmw) {
8264 		if (!(un->un_wm_cache)) {
8265 			(void) snprintf(name_str, sizeof (name_str),
8266 			    "%s%d_cache",
8267 			    ddi_driver_name(SD_DEVINFO(un)),
8268 			    ddi_get_instance(SD_DEVINFO(un)));
8269 			un->un_wm_cache = kmem_cache_create(
8270 			    name_str, sizeof (struct sd_w_map),
8271 			    8, sd_wm_cache_constructor,
8272 			    sd_wm_cache_destructor, NULL,
8273 			    (void *)un, NULL, 0);
8274 			if (!(un->un_wm_cache)) {
8275 				goto wm_cache_failed;
8276 			}
8277 		}
8278 	}
8279 
8280 	/*
8281 	 * Check the value of the NV_SUP bit and set
8282 	 * un_f_suppress_cache_flush accordingly.
8283 	 */
8284 	sd_get_nv_sup(ssc);
8285 
8286 	/*
8287 	 * Find out what type of reservation this disk supports.
8288 	 */
8289 	status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8290 
8291 	switch (status) {
8292 	case 0:
8293 		/*
8294 		 * SCSI-3 reservations are supported.
8295 		 */
8296 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8297 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8298 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8299 		break;
8300 	case ENOTSUP:
8301 		/*
8302 		 * The PERSISTENT RESERVE IN command would not be recognized by
8303 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8304 		 */
8305 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8306 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8307 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8308 
8309 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8310 		break;
8311 	default:
8312 		/*
8313 		 * default to SCSI-3 reservations
8314 		 */
8315 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8316 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8317 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8318 
8319 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8320 		break;
8321 	}
8322 
8323 	/*
8324 	 * Set the pstat and error stat values here, so data obtained during the
8325 	 * previous attach-time routines is available.
8326 	 *
8327 	 * Note: This is a critical sequence that needs to be maintained:
8328 	 *	1) Instantiate the kstats before any routines using the iopath
8329 	 *	   (i.e. sd_send_scsi_cmd).
8330 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8331 	 *	   stats (sd_set_pstats)here, following
8332 	 *	   cmlb_validate_geometry(), sd_register_devid(), and
8333 	 *	   sd_cache_control().
8334 	 */
8335 
8336 	if (un->un_f_pkstats_enabled && geom_label_valid) {
8337 		sd_set_pstats(un);
8338 		SD_TRACE(SD_LOG_IO_PARTITION, un,
8339 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8340 	}
8341 
8342 	sd_set_errstats(un);
8343 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8344 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8345 
8346 
8347 	/*
8348 	 * After successfully attaching an instance, we record the information
8349 	 * of how many luns have been attached on the relative target and
8350 	 * controller for parallel SCSI. This information is used when sd tries
8351 	 * to set the tagged queuing capability in HBA.
8352 	 */
8353 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8354 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8355 	}
8356 
8357 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8358 	    "sd_unit_attach: un:0x%p exit success\n", un);
8359 
8360 	/* Uninitialize sd_ssc_t pointer */
8361 	sd_ssc_fini(ssc);
8362 
8363 	return (DDI_SUCCESS);
8364 
8365 	/*
8366 	 * An error occurred during the attach; clean up & return failure.
8367 	 */
8368 wm_cache_failed:
8369 devid_failed:
8370 
8371 setup_pm_failed:
8372 	ddi_remove_minor_node(devi, NULL);
8373 
8374 cmlb_attach_failed:
8375 	/*
8376 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8377 	 */
8378 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8379 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8380 
8381 	/*
8382 	 * Refer to the comments of setting tagged-qing in the beginning of
8383 	 * sd_unit_attach. We can only disable tagged queuing when there is
8384 	 * no lun attached on the target.
8385 	 */
8386 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8387 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8388 	}
8389 
8390 	if (un->un_f_is_fibre == FALSE) {
8391 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8392 	}
8393 
8394 spinup_failed:
8395 
8396 	/* Uninitialize sd_ssc_t pointer */
8397 	sd_ssc_fini(ssc);
8398 
8399 	mutex_enter(SD_MUTEX(un));
8400 
8401 	/* Deallocate SCSI FMA memory spaces */
8402 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8403 
8404 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8405 	if (un->un_direct_priority_timeid != NULL) {
8406 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8407 		un->un_direct_priority_timeid = NULL;
8408 		mutex_exit(SD_MUTEX(un));
8409 		(void) untimeout(temp_id);
8410 		mutex_enter(SD_MUTEX(un));
8411 	}
8412 
8413 	/* Cancel any pending start/stop timeouts */
8414 	if (un->un_startstop_timeid != NULL) {
8415 		timeout_id_t temp_id = un->un_startstop_timeid;
8416 		un->un_startstop_timeid = NULL;
8417 		mutex_exit(SD_MUTEX(un));
8418 		(void) untimeout(temp_id);
8419 		mutex_enter(SD_MUTEX(un));
8420 	}
8421 
8422 	/* Cancel any pending reset-throttle timeouts */
8423 	if (un->un_reset_throttle_timeid != NULL) {
8424 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8425 		un->un_reset_throttle_timeid = NULL;
8426 		mutex_exit(SD_MUTEX(un));
8427 		(void) untimeout(temp_id);
8428 		mutex_enter(SD_MUTEX(un));
8429 	}
8430 
8431 	/* Cancel rmw warning message timeouts */
8432 	if (un->un_rmw_msg_timeid != NULL) {
8433 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8434 		un->un_rmw_msg_timeid = NULL;
8435 		mutex_exit(SD_MUTEX(un));
8436 		(void) untimeout(temp_id);
8437 		mutex_enter(SD_MUTEX(un));
8438 	}
8439 
8440 	/* Cancel any pending retry timeouts */
8441 	if (un->un_retry_timeid != NULL) {
8442 		timeout_id_t temp_id = un->un_retry_timeid;
8443 		un->un_retry_timeid = NULL;
8444 		mutex_exit(SD_MUTEX(un));
8445 		(void) untimeout(temp_id);
8446 		mutex_enter(SD_MUTEX(un));
8447 	}
8448 
8449 	/* Cancel any pending delayed cv broadcast timeouts */
8450 	if (un->un_dcvb_timeid != NULL) {
8451 		timeout_id_t temp_id = un->un_dcvb_timeid;
8452 		un->un_dcvb_timeid = NULL;
8453 		mutex_exit(SD_MUTEX(un));
8454 		(void) untimeout(temp_id);
8455 		mutex_enter(SD_MUTEX(un));
8456 	}
8457 
8458 	mutex_exit(SD_MUTEX(un));
8459 
8460 	/* There should not be any in-progress I/O so ASSERT this check */
8461 	ASSERT(un->un_ncmds_in_transport == 0);
8462 	ASSERT(un->un_ncmds_in_driver == 0);
8463 
8464 	/* Do not free the softstate if the callback routine is active */
8465 	sd_sync_with_callback(un);
8466 
8467 	/*
8468 	 * Partition stats apparently are not used with removables. These would
8469 	 * not have been created during attach, so no need to clean them up...
8470 	 */
8471 	if (un->un_errstats != NULL) {
8472 		kstat_delete(un->un_errstats);
8473 		un->un_errstats = NULL;
8474 	}
8475 
8476 create_errstats_failed:
8477 
8478 	if (un->un_stats != NULL) {
8479 		kstat_delete(un->un_stats);
8480 		un->un_stats = NULL;
8481 	}
8482 
8483 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8484 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8485 
8486 	ddi_prop_remove_all(devi);
8487 	sema_destroy(&un->un_semoclose);
8488 	cv_destroy(&un->un_state_cv);
8489 
8490 getrbuf_failed:
8491 
8492 	sd_free_rqs(un);
8493 
8494 alloc_rqs_failed:
8495 
8496 	devp->sd_private = NULL;
8497 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8498 
8499 get_softstate_failed:
8500 	/*
8501 	 * Note: the man pages are unclear as to whether or not doing a
8502 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8503 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8504 	 * ddi_get_soft_state() fails.  The implication seems to be
8505 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8506 	 */
8507 #ifndef XPV_HVM_DRIVER
8508 	ddi_soft_state_free(sd_state, instance);
8509 #endif /* !XPV_HVM_DRIVER */
8510 
8511 probe_failed:
8512 	scsi_unprobe(devp);
8513 
8514 	return (DDI_FAILURE);
8515 }
8516 
8517 
8518 /*
8519  *    Function: sd_unit_detach
8520  *
8521  * Description: Performs DDI_DETACH processing for sddetach().
8522  *
8523  * Return Code: DDI_SUCCESS
8524  *		DDI_FAILURE
8525  *
8526  *     Context: Kernel thread context
8527  */
8528 
8529 static int
8530 sd_unit_detach(dev_info_t *devi)
8531 {
8532 	struct scsi_device	*devp;
8533 	struct sd_lun		*un;
8534 	int			i;
8535 	int			tgt;
8536 	dev_t			dev;
8537 	dev_info_t		*pdip = ddi_get_parent(devi);
8538 #ifndef XPV_HVM_DRIVER
8539 	int			instance = ddi_get_instance(devi);
8540 #endif /* !XPV_HVM_DRIVER */
8541 
8542 	mutex_enter(&sd_detach_mutex);
8543 
8544 	/*
8545 	 * Fail the detach for any of the following:
8546 	 *  - Unable to get the sd_lun struct for the instance
8547 	 *  - A layered driver has an outstanding open on the instance
8548 	 *  - Another thread is already detaching this instance
8549 	 *  - Another thread is currently performing an open
8550 	 */
8551 	devp = ddi_get_driver_private(devi);
8552 	if ((devp == NULL) ||
8553 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8554 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8555 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8556 		mutex_exit(&sd_detach_mutex);
8557 		return (DDI_FAILURE);
8558 	}
8559 
8560 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8561 
8562 	/*
8563 	 * Mark this instance as currently in a detach, to inhibit any
8564 	 * opens from a layered driver.
8565 	 */
8566 	un->un_detach_count++;
8567 	mutex_exit(&sd_detach_mutex);
8568 
8569 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8570 	    SCSI_ADDR_PROP_TARGET, -1);
8571 
8572 	dev = sd_make_device(SD_DEVINFO(un));
8573 
8574 #ifndef lint
8575 	_NOTE(COMPETING_THREADS_NOW);
8576 #endif
8577 
8578 	mutex_enter(SD_MUTEX(un));
8579 
8580 	/*
8581 	 * Fail the detach if there are any outstanding layered
8582 	 * opens on this device.
8583 	 */
8584 	for (i = 0; i < NDKMAP; i++) {
8585 		if (un->un_ocmap.lyropen[i] != 0) {
8586 			goto err_notclosed;
8587 		}
8588 	}
8589 
8590 	/*
8591 	 * Verify there are NO outstanding commands issued to this device.
8592 	 * ie, un_ncmds_in_transport == 0.
8593 	 * It's possible to have outstanding commands through the physio
8594 	 * code path, even though everything's closed.
8595 	 */
8596 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8597 	    (un->un_direct_priority_timeid != NULL) ||
8598 	    (un->un_state == SD_STATE_RWAIT)) {
8599 		mutex_exit(SD_MUTEX(un));
8600 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8601 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8602 		goto err_stillbusy;
8603 	}
8604 
8605 	/*
8606 	 * If we have the device reserved, release the reservation.
8607 	 */
8608 	if ((un->un_resvd_status & SD_RESERVE) &&
8609 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8610 		mutex_exit(SD_MUTEX(un));
8611 		/*
8612 		 * Note: sd_reserve_release sends a command to the device
8613 		 * via the sd_ioctlcmd() path, and can sleep.
8614 		 */
8615 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8616 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8617 			    "sd_dr_detach: Cannot release reservation \n");
8618 		}
8619 	} else {
8620 		mutex_exit(SD_MUTEX(un));
8621 	}
8622 
8623 	/*
8624 	 * Untimeout any reserve recover, throttle reset, restart unit
8625 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8626 	 * from getting nulled by their callback functions.
8627 	 */
8628 	mutex_enter(SD_MUTEX(un));
8629 	if (un->un_resvd_timeid != NULL) {
8630 		timeout_id_t temp_id = un->un_resvd_timeid;
8631 		un->un_resvd_timeid = NULL;
8632 		mutex_exit(SD_MUTEX(un));
8633 		(void) untimeout(temp_id);
8634 		mutex_enter(SD_MUTEX(un));
8635 	}
8636 
8637 	if (un->un_reset_throttle_timeid != NULL) {
8638 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8639 		un->un_reset_throttle_timeid = NULL;
8640 		mutex_exit(SD_MUTEX(un));
8641 		(void) untimeout(temp_id);
8642 		mutex_enter(SD_MUTEX(un));
8643 	}
8644 
8645 	if (un->un_startstop_timeid != NULL) {
8646 		timeout_id_t temp_id = un->un_startstop_timeid;
8647 		un->un_startstop_timeid = NULL;
8648 		mutex_exit(SD_MUTEX(un));
8649 		(void) untimeout(temp_id);
8650 		mutex_enter(SD_MUTEX(un));
8651 	}
8652 
8653 	if (un->un_rmw_msg_timeid != NULL) {
8654 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8655 		un->un_rmw_msg_timeid = NULL;
8656 		mutex_exit(SD_MUTEX(un));
8657 		(void) untimeout(temp_id);
8658 		mutex_enter(SD_MUTEX(un));
8659 	}
8660 
8661 	if (un->un_dcvb_timeid != NULL) {
8662 		timeout_id_t temp_id = un->un_dcvb_timeid;
8663 		un->un_dcvb_timeid = NULL;
8664 		mutex_exit(SD_MUTEX(un));
8665 		(void) untimeout(temp_id);
8666 	} else {
8667 		mutex_exit(SD_MUTEX(un));
8668 	}
8669 
8670 	/* Remove any pending reservation reclaim requests for this device */
8671 	sd_rmv_resv_reclaim_req(dev);
8672 
8673 	mutex_enter(SD_MUTEX(un));
8674 
8675 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8676 	if (un->un_direct_priority_timeid != NULL) {
8677 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8678 		un->un_direct_priority_timeid = NULL;
8679 		mutex_exit(SD_MUTEX(un));
8680 		(void) untimeout(temp_id);
8681 		mutex_enter(SD_MUTEX(un));
8682 	}
8683 
8684 	/* Cancel any active multi-host disk watch thread requests */
8685 	if (un->un_mhd_token != NULL) {
8686 		mutex_exit(SD_MUTEX(un));
8687 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8688 		if (scsi_watch_request_terminate(un->un_mhd_token,
8689 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8690 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8691 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8692 			/*
8693 			 * Note: We are returning here after having removed
8694 			 * some driver timeouts above. This is consistent with
8695 			 * the legacy implementation but perhaps the watch
8696 			 * terminate call should be made with the wait flag set.
8697 			 */
8698 			goto err_stillbusy;
8699 		}
8700 		mutex_enter(SD_MUTEX(un));
8701 		un->un_mhd_token = NULL;
8702 	}
8703 
8704 	if (un->un_swr_token != NULL) {
8705 		mutex_exit(SD_MUTEX(un));
8706 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8707 		if (scsi_watch_request_terminate(un->un_swr_token,
8708 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8709 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8710 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8711 			/*
8712 			 * Note: We are returning here after having removed
8713 			 * some driver timeouts above. This is consistent with
8714 			 * the legacy implementation but perhaps the watch
8715 			 * terminate call should be made with the wait flag set.
8716 			 */
8717 			goto err_stillbusy;
8718 		}
8719 		mutex_enter(SD_MUTEX(un));
8720 		un->un_swr_token = NULL;
8721 	}
8722 
8723 	mutex_exit(SD_MUTEX(un));
8724 
8725 	/*
8726 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8727 	 * if we have not registered one.
8728 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8729 	 */
8730 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8731 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8732 
8733 	/*
8734 	 * protect the timeout pointers from getting nulled by
8735 	 * their callback functions during the cancellation process.
8736 	 * In such a scenario untimeout can be invoked with a null value.
8737 	 */
8738 	_NOTE(NO_COMPETING_THREADS_NOW);
8739 
8740 	mutex_enter(&un->un_pm_mutex);
8741 	if (un->un_pm_idle_timeid != NULL) {
8742 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8743 		un->un_pm_idle_timeid = NULL;
8744 		mutex_exit(&un->un_pm_mutex);
8745 
8746 		/*
8747 		 * Timeout is active; cancel it.
8748 		 * Note that it'll never be active on a device
8749 		 * that does not support PM therefore we don't
8750 		 * have to check before calling pm_idle_component.
8751 		 */
8752 		(void) untimeout(temp_id);
8753 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8754 		mutex_enter(&un->un_pm_mutex);
8755 	}
8756 
8757 	/*
8758 	 * Check whether there is already a timeout scheduled for power
8759 	 * management. If yes then don't lower the power here, that's.
8760 	 * the timeout handler's job.
8761 	 */
8762 	if (un->un_pm_timeid != NULL) {
8763 		timeout_id_t temp_id = un->un_pm_timeid;
8764 		un->un_pm_timeid = NULL;
8765 		mutex_exit(&un->un_pm_mutex);
8766 		/*
8767 		 * Timeout is active; cancel it.
8768 		 * Note that it'll never be active on a device
8769 		 * that does not support PM therefore we don't
8770 		 * have to check before calling pm_idle_component.
8771 		 */
8772 		(void) untimeout(temp_id);
8773 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8774 
8775 	} else {
8776 		mutex_exit(&un->un_pm_mutex);
8777 		if ((un->un_f_pm_is_enabled == TRUE) &&
8778 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8779 		    != DDI_SUCCESS)) {
8780 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8781 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8782 			/*
8783 			 * Fix for bug: 4297749, item # 13
8784 			 * The above test now includes a check to see if PM is
8785 			 * supported by this device before call
8786 			 * pm_lower_power().
8787 			 * Note, the following is not dead code. The call to
8788 			 * pm_lower_power above will generate a call back into
8789 			 * our sdpower routine which might result in a timeout
8790 			 * handler getting activated. Therefore the following
8791 			 * code is valid and necessary.
8792 			 */
8793 			mutex_enter(&un->un_pm_mutex);
8794 			if (un->un_pm_timeid != NULL) {
8795 				timeout_id_t temp_id = un->un_pm_timeid;
8796 				un->un_pm_timeid = NULL;
8797 				mutex_exit(&un->un_pm_mutex);
8798 				(void) untimeout(temp_id);
8799 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8800 			} else {
8801 				mutex_exit(&un->un_pm_mutex);
8802 			}
8803 		}
8804 	}
8805 
8806 	/*
8807 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8808 	 * Relocated here from above to be after the call to
8809 	 * pm_lower_power, which was getting errors.
8810 	 */
8811 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8812 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8813 
8814 	/*
8815 	 * Currently, tagged queuing is supported per target based by HBA.
8816 	 * Setting this per lun instance actually sets the capability of this
8817 	 * target in HBA, which affects those luns already attached on the
8818 	 * same target. So during detach, we can only disable this capability
8819 	 * only when this is the only lun left on this target. By doing
8820 	 * this, we assume a target has the same tagged queuing capability
8821 	 * for every lun. The condition can be removed when HBA is changed to
8822 	 * support per lun based tagged queuing capability.
8823 	 */
8824 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8825 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8826 	}
8827 
8828 	if (un->un_f_is_fibre == FALSE) {
8829 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8830 	}
8831 
8832 	/*
8833 	 * Remove any event callbacks, fibre only
8834 	 */
8835 	if (un->un_f_is_fibre == TRUE) {
8836 		if ((un->un_insert_event != NULL) &&
8837 		    (ddi_remove_event_handler(un->un_insert_cb_id) !=
8838 		    DDI_SUCCESS)) {
8839 			/*
8840 			 * Note: We are returning here after having done
8841 			 * substantial cleanup above. This is consistent
8842 			 * with the legacy implementation but this may not
8843 			 * be the right thing to do.
8844 			 */
8845 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8846 			    "sd_dr_detach: Cannot cancel insert event\n");
8847 			goto err_remove_event;
8848 		}
8849 		un->un_insert_event = NULL;
8850 
8851 		if ((un->un_remove_event != NULL) &&
8852 		    (ddi_remove_event_handler(un->un_remove_cb_id) !=
8853 		    DDI_SUCCESS)) {
8854 			/*
8855 			 * Note: We are returning here after having done
8856 			 * substantial cleanup above. This is consistent
8857 			 * with the legacy implementation but this may not
8858 			 * be the right thing to do.
8859 			 */
8860 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8861 			    "sd_dr_detach: Cannot cancel remove event\n");
8862 			goto err_remove_event;
8863 		}
8864 		un->un_remove_event = NULL;
8865 	}
8866 
8867 	/* Do not free the softstate if the callback routine is active */
8868 	sd_sync_with_callback(un);
8869 
8870 	cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8871 	cmlb_free_handle(&un->un_cmlbhandle);
8872 
8873 	/*
8874 	 * Hold the detach mutex here, to make sure that no other threads ever
8875 	 * can access a (partially) freed soft state structure.
8876 	 */
8877 	mutex_enter(&sd_detach_mutex);
8878 
8879 	/*
8880 	 * Clean up the soft state struct.
8881 	 * Cleanup is done in reverse order of allocs/inits.
8882 	 * At this point there should be no competing threads anymore.
8883 	 */
8884 
8885 	scsi_fm_fini(devp);
8886 
8887 	/*
8888 	 * Deallocate memory for SCSI FMA.
8889 	 */
8890 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8891 
8892 	/*
8893 	 * Unregister and free device id if it was not registered
8894 	 * by the transport.
8895 	 */
8896 	if (un->un_f_devid_transport_defined == FALSE)
8897 		ddi_devid_unregister(devi);
8898 
8899 	/*
8900 	 * free the devid structure if allocated before (by ddi_devid_init()
8901 	 * or ddi_devid_get()).
8902 	 */
8903 	if (un->un_devid) {
8904 		ddi_devid_free(un->un_devid);
8905 		un->un_devid = NULL;
8906 	}
8907 
8908 	/*
8909 	 * Destroy wmap cache if it exists.
8910 	 */
8911 	if (un->un_wm_cache != NULL) {
8912 		kmem_cache_destroy(un->un_wm_cache);
8913 		un->un_wm_cache = NULL;
8914 	}
8915 
8916 	/*
8917 	 * kstat cleanup is done in detach for all device types (4363169).
8918 	 * We do not want to fail detach if the device kstats are not deleted
8919 	 * since there is a confusion about the devo_refcnt for the device.
8920 	 * We just delete the kstats and let detach complete successfully.
8921 	 */
8922 	if (un->un_stats != NULL) {
8923 		kstat_delete(un->un_stats);
8924 		un->un_stats = NULL;
8925 	}
8926 	if (un->un_errstats != NULL) {
8927 		kstat_delete(un->un_errstats);
8928 		un->un_errstats = NULL;
8929 	}
8930 
8931 	/* Remove partition stats */
8932 	if (un->un_f_pkstats_enabled) {
8933 		for (i = 0; i < NSDMAP; i++) {
8934 			if (un->un_pstats[i] != NULL) {
8935 				kstat_delete(un->un_pstats[i]);
8936 				un->un_pstats[i] = NULL;
8937 			}
8938 		}
8939 	}
8940 
8941 	/* Remove xbuf registration */
8942 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8943 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8944 
8945 	/* Remove driver properties */
8946 	ddi_prop_remove_all(devi);
8947 
8948 	mutex_destroy(&un->un_pm_mutex);
8949 	cv_destroy(&un->un_pm_busy_cv);
8950 
8951 	cv_destroy(&un->un_wcc_cv);
8952 
8953 	/* Open/close semaphore */
8954 	sema_destroy(&un->un_semoclose);
8955 
8956 	/* Removable media condvar. */
8957 	cv_destroy(&un->un_state_cv);
8958 
8959 	/* Suspend/resume condvar. */
8960 	cv_destroy(&un->un_suspend_cv);
8961 	cv_destroy(&un->un_disk_busy_cv);
8962 
8963 	sd_free_rqs(un);
8964 
8965 	/* Free up soft state */
8966 	devp->sd_private = NULL;
8967 
8968 	bzero(un, sizeof (struct sd_lun));
8969 #ifndef XPV_HVM_DRIVER
8970 	ddi_soft_state_free(sd_state, instance);
8971 #endif /* !XPV_HVM_DRIVER */
8972 
8973 	mutex_exit(&sd_detach_mutex);
8974 
8975 	/* This frees up the INQUIRY data associated with the device. */
8976 	scsi_unprobe(devp);
8977 
8978 	/*
8979 	 * After successfully detaching an instance, we update the information
8980 	 * of how many luns have been attached in the relative target and
8981 	 * controller for parallel SCSI. This information is used when sd tries
8982 	 * to set the tagged queuing capability in HBA.
8983 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
8984 	 * check if the device is parallel SCSI. However, we don't need to
8985 	 * check here because we've already checked during attach. No device
8986 	 * that is not parallel SCSI is in the chain.
8987 	 */
8988 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8989 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
8990 	}
8991 
8992 	return (DDI_SUCCESS);
8993 
8994 err_notclosed:
8995 	mutex_exit(SD_MUTEX(un));
8996 
8997 err_stillbusy:
8998 	_NOTE(NO_COMPETING_THREADS_NOW);
8999 
9000 err_remove_event:
9001 	mutex_enter(&sd_detach_mutex);
9002 	un->un_detach_count--;
9003 	mutex_exit(&sd_detach_mutex);
9004 
9005 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9006 	return (DDI_FAILURE);
9007 }
9008 
9009 
9010 /*
9011  *    Function: sd_create_errstats
9012  *
9013  * Description: This routine instantiates the device error stats.
9014  *
9015  *		Note: During attach the stats are instantiated first so they are
9016  *		available for attach-time routines that utilize the driver
9017  *		iopath to send commands to the device. The stats are initialized
9018  *		separately so data obtained during some attach-time routines is
9019  *		available. (4362483)
9020  *
9021  *   Arguments: un - driver soft state (unit) structure
9022  *		instance - driver instance
9023  *
9024  *     Context: Kernel thread context
9025  */
9026 
9027 static void
9028 sd_create_errstats(struct sd_lun *un, int instance)
9029 {
9030 	struct	sd_errstats	*stp;
9031 	char	kstatmodule_err[KSTAT_STRLEN];
9032 	char	kstatname[KSTAT_STRLEN];
9033 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9034 
9035 	ASSERT(un != NULL);
9036 
9037 	if (un->un_errstats != NULL) {
9038 		return;
9039 	}
9040 
9041 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9042 	    "%serr", sd_label);
9043 	(void) snprintf(kstatname, sizeof (kstatname),
9044 	    "%s%d,err", sd_label, instance);
9045 
9046 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9047 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9048 
9049 	if (un->un_errstats == NULL) {
9050 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9051 		    "sd_create_errstats: Failed kstat_create\n");
9052 		return;
9053 	}
9054 
9055 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9056 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9057 	    KSTAT_DATA_UINT32);
9058 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9059 	    KSTAT_DATA_UINT32);
9060 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9061 	    KSTAT_DATA_UINT32);
9062 	kstat_named_init(&stp->sd_vid,		"Vendor",
9063 	    KSTAT_DATA_CHAR);
9064 	kstat_named_init(&stp->sd_pid,		"Product",
9065 	    KSTAT_DATA_CHAR);
9066 	kstat_named_init(&stp->sd_revision,	"Revision",
9067 	    KSTAT_DATA_CHAR);
9068 	kstat_named_init(&stp->sd_serial,	"Serial No",
9069 	    KSTAT_DATA_CHAR);
9070 	kstat_named_init(&stp->sd_capacity,	"Size",
9071 	    KSTAT_DATA_ULONGLONG);
9072 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9073 	    KSTAT_DATA_UINT32);
9074 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9075 	    KSTAT_DATA_UINT32);
9076 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9077 	    KSTAT_DATA_UINT32);
9078 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9079 	    KSTAT_DATA_UINT32);
9080 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9081 	    KSTAT_DATA_UINT32);
9082 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9083 	    KSTAT_DATA_UINT32);
9084 
9085 	un->un_errstats->ks_private = un;
9086 	un->un_errstats->ks_update  = nulldev;
9087 
9088 	kstat_install(un->un_errstats);
9089 }
9090 
9091 
9092 /*
9093  *    Function: sd_set_errstats
9094  *
9095  * Description: This routine sets the value of the vendor id, product id,
9096  *		revision, serial number, and capacity device error stats.
9097  *
9098  *		Note: During attach the stats are instantiated first so they are
9099  *		available for attach-time routines that utilize the driver
9100  *		iopath to send commands to the device. The stats are initialized
9101  *		separately so data obtained during some attach-time routines is
9102  *		available. (4362483)
9103  *
9104  *   Arguments: un - driver soft state (unit) structure
9105  *
9106  *     Context: Kernel thread context
9107  */
9108 
9109 static void
9110 sd_set_errstats(struct sd_lun *un)
9111 {
9112 	struct	sd_errstats	*stp;
9113 	char 			*sn;
9114 
9115 	ASSERT(un != NULL);
9116 	ASSERT(un->un_errstats != NULL);
9117 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9118 	ASSERT(stp != NULL);
9119 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9120 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9121 	(void) strncpy(stp->sd_revision.value.c,
9122 	    un->un_sd->sd_inq->inq_revision, 4);
9123 
9124 	/*
9125 	 * All the errstats are persistent across detach/attach,
9126 	 * so reset all the errstats here in case of the hot
9127 	 * replacement of disk drives, except for not changed
9128 	 * Sun qualified drives.
9129 	 */
9130 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9131 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9132 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9133 		stp->sd_softerrs.value.ui32 = 0;
9134 		stp->sd_harderrs.value.ui32 = 0;
9135 		stp->sd_transerrs.value.ui32 = 0;
9136 		stp->sd_rq_media_err.value.ui32 = 0;
9137 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9138 		stp->sd_rq_nodev_err.value.ui32 = 0;
9139 		stp->sd_rq_recov_err.value.ui32 = 0;
9140 		stp->sd_rq_illrq_err.value.ui32 = 0;
9141 		stp->sd_rq_pfa_err.value.ui32 = 0;
9142 	}
9143 
9144 	/*
9145 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9146 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9147 	 * (4376302))
9148 	 */
9149 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9150 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9151 		    sizeof (SD_INQUIRY(un)->inq_serial));
9152 	} else {
9153 		/*
9154 		 * Set the "Serial No" kstat for non-Sun qualified drives
9155 		 */
9156 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9157 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9158 		    INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9159 			(void) strlcpy(stp->sd_serial.value.c, sn,
9160 			    sizeof (stp->sd_serial.value.c));
9161 			ddi_prop_free(sn);
9162 		}
9163 	}
9164 
9165 	if (un->un_f_blockcount_is_valid != TRUE) {
9166 		/*
9167 		 * Set capacity error stat to 0 for no media. This ensures
9168 		 * a valid capacity is displayed in response to 'iostat -E'
9169 		 * when no media is present in the device.
9170 		 */
9171 		stp->sd_capacity.value.ui64 = 0;
9172 	} else {
9173 		/*
9174 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9175 		 * capacity.
9176 		 *
9177 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9178 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9179 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9180 		 */
9181 		stp->sd_capacity.value.ui64 = (uint64_t)
9182 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9183 	}
9184 }
9185 
9186 
9187 /*
9188  *    Function: sd_set_pstats
9189  *
9190  * Description: This routine instantiates and initializes the partition
9191  *              stats for each partition with more than zero blocks.
9192  *		(4363169)
9193  *
9194  *   Arguments: un - driver soft state (unit) structure
9195  *
9196  *     Context: Kernel thread context
9197  */
9198 
9199 static void
9200 sd_set_pstats(struct sd_lun *un)
9201 {
9202 	char	kstatname[KSTAT_STRLEN];
9203 	int	instance;
9204 	int	i;
9205 	diskaddr_t	nblks = 0;
9206 	char	*partname = NULL;
9207 
9208 	ASSERT(un != NULL);
9209 
9210 	instance = ddi_get_instance(SD_DEVINFO(un));
9211 
9212 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9213 	for (i = 0; i < NSDMAP; i++) {
9214 
9215 		if (cmlb_partinfo(un->un_cmlbhandle, i,
9216 		    &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9217 			continue;
9218 		mutex_enter(SD_MUTEX(un));
9219 
9220 		if ((un->un_pstats[i] == NULL) &&
9221 		    (nblks != 0)) {
9222 
9223 			(void) snprintf(kstatname, sizeof (kstatname),
9224 			    "%s%d,%s", sd_label, instance,
9225 			    partname);
9226 
9227 			un->un_pstats[i] = kstat_create(sd_label,
9228 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9229 			    1, KSTAT_FLAG_PERSISTENT);
9230 			if (un->un_pstats[i] != NULL) {
9231 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9232 				kstat_install(un->un_pstats[i]);
9233 			}
9234 		}
9235 		mutex_exit(SD_MUTEX(un));
9236 	}
9237 }
9238 
9239 
9240 #if (defined(__fibre))
9241 /*
9242  *    Function: sd_init_event_callbacks
9243  *
9244  * Description: This routine initializes the insertion and removal event
9245  *		callbacks. (fibre only)
9246  *
9247  *   Arguments: un - driver soft state (unit) structure
9248  *
9249  *     Context: Kernel thread context
9250  */
9251 
9252 static void
9253 sd_init_event_callbacks(struct sd_lun *un)
9254 {
9255 	ASSERT(un != NULL);
9256 
9257 	if ((un->un_insert_event == NULL) &&
9258 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9259 	    &un->un_insert_event) == DDI_SUCCESS)) {
9260 		/*
9261 		 * Add the callback for an insertion event
9262 		 */
9263 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9264 		    un->un_insert_event, sd_event_callback, (void *)un,
9265 		    &(un->un_insert_cb_id));
9266 	}
9267 
9268 	if ((un->un_remove_event == NULL) &&
9269 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9270 	    &un->un_remove_event) == DDI_SUCCESS)) {
9271 		/*
9272 		 * Add the callback for a removal event
9273 		 */
9274 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9275 		    un->un_remove_event, sd_event_callback, (void *)un,
9276 		    &(un->un_remove_cb_id));
9277 	}
9278 }
9279 
9280 
9281 /*
9282  *    Function: sd_event_callback
9283  *
9284  * Description: This routine handles insert/remove events (photon). The
9285  *		state is changed to OFFLINE which can be used to supress
9286  *		error msgs. (fibre only)
9287  *
9288  *   Arguments: un - driver soft state (unit) structure
9289  *
9290  *     Context: Callout thread context
9291  */
9292 /* ARGSUSED */
9293 static void
9294 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9295     void *bus_impldata)
9296 {
9297 	struct sd_lun *un = (struct sd_lun *)arg;
9298 
9299 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9300 	if (event == un->un_insert_event) {
9301 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9302 		mutex_enter(SD_MUTEX(un));
9303 		if (un->un_state == SD_STATE_OFFLINE) {
9304 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9305 				un->un_state = un->un_last_state;
9306 			} else {
9307 				/*
9308 				 * We have gone through SUSPEND/RESUME while
9309 				 * we were offline. Restore the last state
9310 				 */
9311 				un->un_state = un->un_save_state;
9312 			}
9313 		}
9314 		mutex_exit(SD_MUTEX(un));
9315 
9316 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9317 	} else if (event == un->un_remove_event) {
9318 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9319 		mutex_enter(SD_MUTEX(un));
9320 		/*
9321 		 * We need to handle an event callback that occurs during
9322 		 * the suspend operation, since we don't prevent it.
9323 		 */
9324 		if (un->un_state != SD_STATE_OFFLINE) {
9325 			if (un->un_state != SD_STATE_SUSPENDED) {
9326 				New_state(un, SD_STATE_OFFLINE);
9327 			} else {
9328 				un->un_last_state = SD_STATE_OFFLINE;
9329 			}
9330 		}
9331 		mutex_exit(SD_MUTEX(un));
9332 	} else {
9333 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9334 		    "!Unknown event\n");
9335 	}
9336 
9337 }
9338 #endif
9339 
9340 /*
9341  *    Function: sd_cache_control()
9342  *
9343  * Description: This routine is the driver entry point for setting
9344  *		read and write caching by modifying the WCE (write cache
9345  *		enable) and RCD (read cache disable) bits of mode
9346  *		page 8 (MODEPAGE_CACHING).
9347  *
9348  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
9349  *                      structure for this target.
9350  *		rcd_flag - flag for controlling the read cache
9351  *		wce_flag - flag for controlling the write cache
9352  *
9353  * Return Code: EIO
9354  *		code returned by sd_send_scsi_MODE_SENSE and
9355  *		sd_send_scsi_MODE_SELECT
9356  *
9357  *     Context: Kernel Thread
9358  */
9359 
9360 static int
9361 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9362 {
9363 	struct mode_caching	*mode_caching_page;
9364 	uchar_t			*header;
9365 	size_t			buflen;
9366 	int			hdrlen;
9367 	int			bd_len;
9368 	int			rval = 0;
9369 	struct mode_header_grp2	*mhp;
9370 	struct sd_lun		*un;
9371 	int			status;
9372 
9373 	ASSERT(ssc != NULL);
9374 	un = ssc->ssc_un;
9375 	ASSERT(un != NULL);
9376 
9377 	/*
9378 	 * Do a test unit ready, otherwise a mode sense may not work if this
9379 	 * is the first command sent to the device after boot.
9380 	 */
9381 	status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9382 	if (status != 0)
9383 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9384 
9385 	if (un->un_f_cfg_is_atapi == TRUE) {
9386 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9387 	} else {
9388 		hdrlen = MODE_HEADER_LENGTH;
9389 	}
9390 
9391 	/*
9392 	 * Allocate memory for the retrieved mode page and its headers.  Set
9393 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
9394 	 * we get all of the mode sense data otherwise, the mode select
9395 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
9396 	 */
9397 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9398 	    sizeof (struct mode_cache_scsi3);
9399 
9400 	header = kmem_zalloc(buflen, KM_SLEEP);
9401 
9402 	/* Get the information from the device. */
9403 	if (un->un_f_cfg_is_atapi == TRUE) {
9404 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9405 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9406 	} else {
9407 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9408 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9409 	}
9410 
9411 	if (rval != 0) {
9412 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9413 		    "sd_cache_control: Mode Sense Failed\n");
9414 		goto mode_sense_failed;
9415 	}
9416 
9417 	/*
9418 	 * Determine size of Block Descriptors in order to locate
9419 	 * the mode page data. ATAPI devices return 0, SCSI devices
9420 	 * should return MODE_BLK_DESC_LENGTH.
9421 	 */
9422 	if (un->un_f_cfg_is_atapi == TRUE) {
9423 		mhp	= (struct mode_header_grp2 *)header;
9424 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9425 	} else {
9426 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9427 	}
9428 
9429 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9430 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9431 		    "sd_cache_control: Mode Sense returned invalid block "
9432 		    "descriptor length\n");
9433 		rval = EIO;
9434 		goto mode_sense_failed;
9435 	}
9436 
9437 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9438 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9439 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9440 		    "sd_cache_control: Mode Sense caching page code mismatch "
9441 		    "%d\n", mode_caching_page->mode_page.code);
9442 		rval = EIO;
9443 		goto mode_sense_failed;
9444 	}
9445 
9446 	/* Check the relevant bits on successful mode sense. */
9447 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9448 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9449 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9450 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9451 
9452 		size_t sbuflen;
9453 		uchar_t save_pg;
9454 
9455 		/*
9456 		 * Construct select buffer length based on the
9457 		 * length of the sense data returned.
9458 		 */
9459 		sbuflen =  hdrlen + bd_len +
9460 		    sizeof (struct mode_page) +
9461 		    (int)mode_caching_page->mode_page.length;
9462 
9463 		/*
9464 		 * Set the caching bits as requested.
9465 		 */
9466 		if (rcd_flag == SD_CACHE_ENABLE)
9467 			mode_caching_page->rcd = 0;
9468 		else if (rcd_flag == SD_CACHE_DISABLE)
9469 			mode_caching_page->rcd = 1;
9470 
9471 		if (wce_flag == SD_CACHE_ENABLE)
9472 			mode_caching_page->wce = 1;
9473 		else if (wce_flag == SD_CACHE_DISABLE)
9474 			mode_caching_page->wce = 0;
9475 
9476 		/*
9477 		 * Save the page if the mode sense says the
9478 		 * drive supports it.
9479 		 */
9480 		save_pg = mode_caching_page->mode_page.ps ?
9481 		    SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9482 
9483 		/* Clear reserved bits before mode select. */
9484 		mode_caching_page->mode_page.ps = 0;
9485 
9486 		/*
9487 		 * Clear out mode header for mode select.
9488 		 * The rest of the retrieved page will be reused.
9489 		 */
9490 		bzero(header, hdrlen);
9491 
9492 		if (un->un_f_cfg_is_atapi == TRUE) {
9493 			mhp = (struct mode_header_grp2 *)header;
9494 			mhp->bdesc_length_hi = bd_len >> 8;
9495 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9496 		} else {
9497 			((struct mode_header *)header)->bdesc_length = bd_len;
9498 		}
9499 
9500 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9501 
9502 		/* Issue mode select to change the cache settings */
9503 		if (un->un_f_cfg_is_atapi == TRUE) {
9504 			rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header,
9505 			    sbuflen, save_pg, SD_PATH_DIRECT);
9506 		} else {
9507 			rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
9508 			    sbuflen, save_pg, SD_PATH_DIRECT);
9509 		}
9510 
9511 	}
9512 
9513 
9514 mode_sense_failed:
9515 
9516 	kmem_free(header, buflen);
9517 
9518 	if (rval != 0) {
9519 		if (rval == EIO)
9520 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9521 		else
9522 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9523 	}
9524 	return (rval);
9525 }
9526 
9527 
9528 /*
9529  *    Function: sd_get_write_cache_enabled()
9530  *
9531  * Description: This routine is the driver entry point for determining if
9532  *		write caching is enabled.  It examines the WCE (write cache
9533  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
9534  *
9535  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
9536  *                      structure for this target.
9537  *		is_enabled - pointer to int where write cache enabled state
9538  *		is returned (non-zero -> write cache enabled)
9539  *
9540  *
9541  * Return Code: EIO
9542  *		code returned by sd_send_scsi_MODE_SENSE
9543  *
9544  *     Context: Kernel Thread
9545  *
9546  * NOTE: If ioctl is added to disable write cache, this sequence should
9547  * be followed so that no locking is required for accesses to
9548  * un->un_f_write_cache_enabled:
9549  * 	do mode select to clear wce
9550  * 	do synchronize cache to flush cache
9551  * 	set un->un_f_write_cache_enabled = FALSE
9552  *
9553  * Conversely, an ioctl to enable the write cache should be done
9554  * in this order:
9555  * 	set un->un_f_write_cache_enabled = TRUE
9556  * 	do mode select to set wce
9557  */
9558 
9559 static int
9560 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9561 {
9562 	struct mode_caching	*mode_caching_page;
9563 	uchar_t			*header;
9564 	size_t			buflen;
9565 	int			hdrlen;
9566 	int			bd_len;
9567 	int			rval = 0;
9568 	struct sd_lun		*un;
9569 	int			status;
9570 
9571 	ASSERT(ssc != NULL);
9572 	un = ssc->ssc_un;
9573 	ASSERT(un != NULL);
9574 	ASSERT(is_enabled != NULL);
9575 
9576 	/* in case of error, flag as enabled */
9577 	*is_enabled = TRUE;
9578 
9579 	/*
9580 	 * Do a test unit ready, otherwise a mode sense may not work if this
9581 	 * is the first command sent to the device after boot.
9582 	 */
9583 	status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9584 
9585 	if (status != 0)
9586 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9587 
9588 	if (un->un_f_cfg_is_atapi == TRUE) {
9589 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9590 	} else {
9591 		hdrlen = MODE_HEADER_LENGTH;
9592 	}
9593 
9594 	/*
9595 	 * Allocate memory for the retrieved mode page and its headers.  Set
9596 	 * a pointer to the page itself.
9597 	 */
9598 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9599 	header = kmem_zalloc(buflen, KM_SLEEP);
9600 
9601 	/* Get the information from the device. */
9602 	if (un->un_f_cfg_is_atapi == TRUE) {
9603 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9604 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9605 	} else {
9606 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9607 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9608 	}
9609 
9610 	if (rval != 0) {
9611 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9612 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
9613 		goto mode_sense_failed;
9614 	}
9615 
9616 	/*
9617 	 * Determine size of Block Descriptors in order to locate
9618 	 * the mode page data. ATAPI devices return 0, SCSI devices
9619 	 * should return MODE_BLK_DESC_LENGTH.
9620 	 */
9621 	if (un->un_f_cfg_is_atapi == TRUE) {
9622 		struct mode_header_grp2	*mhp;
9623 		mhp	= (struct mode_header_grp2 *)header;
9624 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9625 	} else {
9626 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9627 	}
9628 
9629 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9630 		/* FMA should make upset complain here */
9631 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9632 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
9633 		    "block descriptor length\n");
9634 		rval = EIO;
9635 		goto mode_sense_failed;
9636 	}
9637 
9638 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9639 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9640 		/* FMA could make upset complain here */
9641 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9642 		    "sd_get_write_cache_enabled: Mode Sense caching page "
9643 		    "code mismatch %d\n", mode_caching_page->mode_page.code);
9644 		rval = EIO;
9645 		goto mode_sense_failed;
9646 	}
9647 	*is_enabled = mode_caching_page->wce;
9648 
9649 mode_sense_failed:
9650 	if (rval == 0) {
9651 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9652 	} else if (rval == EIO) {
9653 		/*
9654 		 * Some disks do not support mode sense(6), we
9655 		 * should ignore this kind of error(sense key is
9656 		 * 0x5 - illegal request).
9657 		 */
9658 		uint8_t *sensep;
9659 		int senlen;
9660 
9661 		sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9662 		senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9663 		    ssc->ssc_uscsi_cmd->uscsi_rqresid);
9664 
9665 		if (senlen > 0 &&
9666 		    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9667 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9668 		} else {
9669 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9670 		}
9671 	} else {
9672 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9673 	}
9674 	kmem_free(header, buflen);
9675 	return (rval);
9676 }
9677 
9678 /*
9679  *    Function: sd_get_nv_sup()
9680  *
9681  * Description: This routine is the driver entry point for
9682  * determining whether non-volatile cache is supported. This
9683  * determination process works as follows:
9684  *
9685  * 1. sd first queries sd.conf on whether
9686  * suppress_cache_flush bit is set for this device.
9687  *
9688  * 2. if not there, then queries the internal disk table.
9689  *
9690  * 3. if either sd.conf or internal disk table specifies
9691  * cache flush be suppressed, we don't bother checking
9692  * NV_SUP bit.
9693  *
9694  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9695  * the optional INQUIRY VPD page 0x86. If the device
9696  * supports VPD page 0x86, sd examines the NV_SUP
9697  * (non-volatile cache support) bit in the INQUIRY VPD page
9698  * 0x86:
9699  *   o If NV_SUP bit is set, sd assumes the device has a
9700  *   non-volatile cache and set the
9701  *   un_f_sync_nv_supported to TRUE.
9702  *   o Otherwise cache is not non-volatile,
9703  *   un_f_sync_nv_supported is set to FALSE.
9704  *
9705  * Arguments: un - driver soft state (unit) structure
9706  *
9707  * Return Code:
9708  *
9709  *     Context: Kernel Thread
9710  */
9711 
9712 static void
9713 sd_get_nv_sup(sd_ssc_t *ssc)
9714 {
9715 	int		rval		= 0;
9716 	uchar_t		*inq86		= NULL;
9717 	size_t		inq86_len	= MAX_INQUIRY_SIZE;
9718 	size_t		inq86_resid	= 0;
9719 	struct		dk_callback *dkc;
9720 	struct sd_lun	*un;
9721 
9722 	ASSERT(ssc != NULL);
9723 	un = ssc->ssc_un;
9724 	ASSERT(un != NULL);
9725 
9726 	mutex_enter(SD_MUTEX(un));
9727 
9728 	/*
9729 	 * Be conservative on the device's support of
9730 	 * SYNC_NV bit: un_f_sync_nv_supported is
9731 	 * initialized to be false.
9732 	 */
9733 	un->un_f_sync_nv_supported = FALSE;
9734 
9735 	/*
9736 	 * If either sd.conf or internal disk table
9737 	 * specifies cache flush be suppressed, then
9738 	 * we don't bother checking NV_SUP bit.
9739 	 */
9740 	if (un->un_f_suppress_cache_flush == TRUE) {
9741 		mutex_exit(SD_MUTEX(un));
9742 		return;
9743 	}
9744 
9745 	if (sd_check_vpd_page_support(ssc) == 0 &&
9746 	    un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9747 		mutex_exit(SD_MUTEX(un));
9748 		/* collect page 86 data if available */
9749 		inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9750 
9751 		rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9752 		    0x01, 0x86, &inq86_resid);
9753 
9754 		if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9755 			SD_TRACE(SD_LOG_COMMON, un,
9756 			    "sd_get_nv_sup: \
9757 			    successfully get VPD page: %x \
9758 			    PAGE LENGTH: %x BYTE 6: %x\n",
9759 			    inq86[1], inq86[3], inq86[6]);
9760 
9761 			mutex_enter(SD_MUTEX(un));
9762 			/*
9763 			 * check the value of NV_SUP bit: only if the device
9764 			 * reports NV_SUP bit to be 1, the
9765 			 * un_f_sync_nv_supported bit will be set to true.
9766 			 */
9767 			if (inq86[6] & SD_VPD_NV_SUP) {
9768 				un->un_f_sync_nv_supported = TRUE;
9769 			}
9770 			mutex_exit(SD_MUTEX(un));
9771 		} else if (rval != 0) {
9772 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9773 		}
9774 
9775 		kmem_free(inq86, inq86_len);
9776 	} else {
9777 		mutex_exit(SD_MUTEX(un));
9778 	}
9779 
9780 	/*
9781 	 * Send a SYNC CACHE command to check whether
9782 	 * SYNC_NV bit is supported. This command should have
9783 	 * un_f_sync_nv_supported set to correct value.
9784 	 */
9785 	mutex_enter(SD_MUTEX(un));
9786 	if (un->un_f_sync_nv_supported) {
9787 		mutex_exit(SD_MUTEX(un));
9788 		dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9789 		dkc->dkc_flag = FLUSH_VOLATILE;
9790 		(void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9791 
9792 		/*
9793 		 * Send a TEST UNIT READY command to the device. This should
9794 		 * clear any outstanding UNIT ATTENTION that may be present.
9795 		 */
9796 		rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9797 		if (rval != 0)
9798 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9799 
9800 		kmem_free(dkc, sizeof (struct dk_callback));
9801 	} else {
9802 		mutex_exit(SD_MUTEX(un));
9803 	}
9804 
9805 	SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9806 	    un_f_suppress_cache_flush is set to %d\n",
9807 	    un->un_f_suppress_cache_flush);
9808 }
9809 
9810 /*
9811  *    Function: sd_make_device
9812  *
9813  * Description: Utility routine to return the Solaris device number from
9814  *		the data in the device's dev_info structure.
9815  *
9816  * Return Code: The Solaris device number
9817  *
9818  *     Context: Any
9819  */
9820 
9821 static dev_t
9822 sd_make_device(dev_info_t *devi)
9823 {
9824 	return (makedevice(ddi_driver_major(devi),
9825 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9826 }
9827 
9828 
9829 /*
9830  *    Function: sd_pm_entry
9831  *
9832  * Description: Called at the start of a new command to manage power
9833  *		and busy status of a device. This includes determining whether
9834  *		the current power state of the device is sufficient for
9835  *		performing the command or whether it must be changed.
9836  *		The PM framework is notified appropriately.
9837  *		Only with a return status of DDI_SUCCESS will the
9838  *		component be busy to the framework.
9839  *
9840  *		All callers of sd_pm_entry must check the return status
9841  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9842  *		of DDI_FAILURE indicates the device failed to power up.
9843  *		In this case un_pm_count has been adjusted so the result
9844  *		on exit is still powered down, ie. count is less than 0.
9845  *		Calling sd_pm_exit with this count value hits an ASSERT.
9846  *
9847  * Return Code: DDI_SUCCESS or DDI_FAILURE
9848  *
9849  *     Context: Kernel thread context.
9850  */
9851 
9852 static int
9853 sd_pm_entry(struct sd_lun *un)
9854 {
9855 	int return_status = DDI_SUCCESS;
9856 
9857 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9858 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9859 
9860 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9861 
9862 	if (un->un_f_pm_is_enabled == FALSE) {
9863 		SD_TRACE(SD_LOG_IO_PM, un,
9864 		    "sd_pm_entry: exiting, PM not enabled\n");
9865 		return (return_status);
9866 	}
9867 
9868 	/*
9869 	 * Just increment a counter if PM is enabled. On the transition from
9870 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9871 	 * the count with each IO and mark the device as idle when the count
9872 	 * hits 0.
9873 	 *
9874 	 * If the count is less than 0 the device is powered down. If a powered
9875 	 * down device is successfully powered up then the count must be
9876 	 * incremented to reflect the power up. Note that it'll get incremented
9877 	 * a second time to become busy.
9878 	 *
9879 	 * Because the following has the potential to change the device state
9880 	 * and must release the un_pm_mutex to do so, only one thread can be
9881 	 * allowed through at a time.
9882 	 */
9883 
9884 	mutex_enter(&un->un_pm_mutex);
9885 	while (un->un_pm_busy == TRUE) {
9886 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9887 	}
9888 	un->un_pm_busy = TRUE;
9889 
9890 	if (un->un_pm_count < 1) {
9891 
9892 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9893 
9894 		/*
9895 		 * Indicate we are now busy so the framework won't attempt to
9896 		 * power down the device. This call will only fail if either
9897 		 * we passed a bad component number or the device has no
9898 		 * components. Neither of these should ever happen.
9899 		 */
9900 		mutex_exit(&un->un_pm_mutex);
9901 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9902 		ASSERT(return_status == DDI_SUCCESS);
9903 
9904 		mutex_enter(&un->un_pm_mutex);
9905 
9906 		if (un->un_pm_count < 0) {
9907 			mutex_exit(&un->un_pm_mutex);
9908 
9909 			SD_TRACE(SD_LOG_IO_PM, un,
9910 			    "sd_pm_entry: power up component\n");
9911 
9912 			/*
9913 			 * pm_raise_power will cause sdpower to be called
9914 			 * which brings the device power level to the
9915 			 * desired state, If successful, un_pm_count and
9916 			 * un_power_level will be updated appropriately.
9917 			 */
9918 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9919 			    SD_PM_STATE_ACTIVE(un));
9920 
9921 			mutex_enter(&un->un_pm_mutex);
9922 
9923 			if (return_status != DDI_SUCCESS) {
9924 				/*
9925 				 * Power up failed.
9926 				 * Idle the device and adjust the count
9927 				 * so the result on exit is that we're
9928 				 * still powered down, ie. count is less than 0.
9929 				 */
9930 				SD_TRACE(SD_LOG_IO_PM, un,
9931 				    "sd_pm_entry: power up failed,"
9932 				    " idle the component\n");
9933 
9934 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9935 				un->un_pm_count--;
9936 			} else {
9937 				/*
9938 				 * Device is powered up, verify the
9939 				 * count is non-negative.
9940 				 * This is debug only.
9941 				 */
9942 				ASSERT(un->un_pm_count == 0);
9943 			}
9944 		}
9945 
9946 		if (return_status == DDI_SUCCESS) {
9947 			/*
9948 			 * For performance, now that the device has been tagged
9949 			 * as busy, and it's known to be powered up, update the
9950 			 * chain types to use jump tables that do not include
9951 			 * pm. This significantly lowers the overhead and
9952 			 * therefore improves performance.
9953 			 */
9954 
9955 			mutex_exit(&un->un_pm_mutex);
9956 			mutex_enter(SD_MUTEX(un));
9957 			SD_TRACE(SD_LOG_IO_PM, un,
9958 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
9959 			    un->un_uscsi_chain_type);
9960 
9961 			if (un->un_f_non_devbsize_supported) {
9962 				un->un_buf_chain_type =
9963 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
9964 			} else {
9965 				un->un_buf_chain_type =
9966 				    SD_CHAIN_INFO_DISK_NO_PM;
9967 			}
9968 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9969 
9970 			SD_TRACE(SD_LOG_IO_PM, un,
9971 			    "             changed  uscsi_chain_type to   %d\n",
9972 			    un->un_uscsi_chain_type);
9973 			mutex_exit(SD_MUTEX(un));
9974 			mutex_enter(&un->un_pm_mutex);
9975 
9976 			if (un->un_pm_idle_timeid == NULL) {
9977 				/* 300 ms. */
9978 				un->un_pm_idle_timeid =
9979 				    timeout(sd_pm_idletimeout_handler, un,
9980 				    (drv_usectohz((clock_t)300000)));
9981 				/*
9982 				 * Include an extra call to busy which keeps the
9983 				 * device busy with-respect-to the PM layer
9984 				 * until the timer fires, at which time it'll
9985 				 * get the extra idle call.
9986 				 */
9987 				(void) pm_busy_component(SD_DEVINFO(un), 0);
9988 			}
9989 		}
9990 	}
9991 	un->un_pm_busy = FALSE;
9992 	/* Next... */
9993 	cv_signal(&un->un_pm_busy_cv);
9994 
9995 	un->un_pm_count++;
9996 
9997 	SD_TRACE(SD_LOG_IO_PM, un,
9998 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9999 
10000 	mutex_exit(&un->un_pm_mutex);
10001 
10002 	return (return_status);
10003 }
10004 
10005 
10006 /*
10007  *    Function: sd_pm_exit
10008  *
10009  * Description: Called at the completion of a command to manage busy
10010  *		status for the device. If the device becomes idle the
10011  *		PM framework is notified.
10012  *
10013  *     Context: Kernel thread context
10014  */
10015 
10016 static void
10017 sd_pm_exit(struct sd_lun *un)
10018 {
10019 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10020 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10021 
10022 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10023 
10024 	/*
10025 	 * After attach the following flag is only read, so don't
10026 	 * take the penalty of acquiring a mutex for it.
10027 	 */
10028 	if (un->un_f_pm_is_enabled == TRUE) {
10029 
10030 		mutex_enter(&un->un_pm_mutex);
10031 		un->un_pm_count--;
10032 
10033 		SD_TRACE(SD_LOG_IO_PM, un,
10034 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10035 
10036 		ASSERT(un->un_pm_count >= 0);
10037 		if (un->un_pm_count == 0) {
10038 			mutex_exit(&un->un_pm_mutex);
10039 
10040 			SD_TRACE(SD_LOG_IO_PM, un,
10041 			    "sd_pm_exit: idle component\n");
10042 
10043 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10044 
10045 		} else {
10046 			mutex_exit(&un->un_pm_mutex);
10047 		}
10048 	}
10049 
10050 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10051 }
10052 
10053 
10054 /*
10055  *    Function: sdopen
10056  *
10057  * Description: Driver's open(9e) entry point function.
10058  *
10059  *   Arguments: dev_i   - pointer to device number
10060  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10061  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10062  *		cred_p  - user credential pointer
10063  *
10064  * Return Code: EINVAL
10065  *		ENXIO
10066  *		EIO
10067  *		EROFS
10068  *		EBUSY
10069  *
10070  *     Context: Kernel thread context
10071  */
10072 /* ARGSUSED */
10073 static int
10074 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10075 {
10076 	struct sd_lun	*un;
10077 	int		nodelay;
10078 	int		part;
10079 	uint64_t	partmask;
10080 	int		instance;
10081 	dev_t		dev;
10082 	int		rval = EIO;
10083 	diskaddr_t	nblks = 0;
10084 	diskaddr_t	label_cap;
10085 
10086 	/* Validate the open type */
10087 	if (otyp >= OTYPCNT) {
10088 		return (EINVAL);
10089 	}
10090 
10091 	dev = *dev_p;
10092 	instance = SDUNIT(dev);
10093 	mutex_enter(&sd_detach_mutex);
10094 
10095 	/*
10096 	 * Fail the open if there is no softstate for the instance, or
10097 	 * if another thread somewhere is trying to detach the instance.
10098 	 */
10099 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10100 	    (un->un_detach_count != 0)) {
10101 		mutex_exit(&sd_detach_mutex);
10102 		/*
10103 		 * The probe cache only needs to be cleared when open (9e) fails
10104 		 * with ENXIO (4238046).
10105 		 */
10106 		/*
10107 		 * un-conditionally clearing probe cache is ok with
10108 		 * separate sd/ssd binaries
10109 		 * x86 platform can be an issue with both parallel
10110 		 * and fibre in 1 binary
10111 		 */
10112 		sd_scsi_clear_probe_cache();
10113 		return (ENXIO);
10114 	}
10115 
10116 	/*
10117 	 * The un_layer_count is to prevent another thread in specfs from
10118 	 * trying to detach the instance, which can happen when we are
10119 	 * called from a higher-layer driver instead of thru specfs.
10120 	 * This will not be needed when DDI provides a layered driver
10121 	 * interface that allows specfs to know that an instance is in
10122 	 * use by a layered driver & should not be detached.
10123 	 *
10124 	 * Note: the semantics for layered driver opens are exactly one
10125 	 * close for every open.
10126 	 */
10127 	if (otyp == OTYP_LYR) {
10128 		un->un_layer_count++;
10129 	}
10130 
10131 	/*
10132 	 * Keep a count of the current # of opens in progress. This is because
10133 	 * some layered drivers try to call us as a regular open. This can
10134 	 * cause problems that we cannot prevent, however by keeping this count
10135 	 * we can at least keep our open and detach routines from racing against
10136 	 * each other under such conditions.
10137 	 */
10138 	un->un_opens_in_progress++;
10139 	mutex_exit(&sd_detach_mutex);
10140 
10141 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10142 	part	 = SDPART(dev);
10143 	partmask = 1 << part;
10144 
10145 	/*
10146 	 * We use a semaphore here in order to serialize
10147 	 * open and close requests on the device.
10148 	 */
10149 	sema_p(&un->un_semoclose);
10150 
10151 	mutex_enter(SD_MUTEX(un));
10152 
10153 	/*
10154 	 * All device accesses go thru sdstrategy() where we check
10155 	 * on suspend status but there could be a scsi_poll command,
10156 	 * which bypasses sdstrategy(), so we need to check pm
10157 	 * status.
10158 	 */
10159 
10160 	if (!nodelay) {
10161 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10162 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10163 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10164 		}
10165 
10166 		mutex_exit(SD_MUTEX(un));
10167 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10168 			rval = EIO;
10169 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10170 			    "sdopen: sd_pm_entry failed\n");
10171 			goto open_failed_with_pm;
10172 		}
10173 		mutex_enter(SD_MUTEX(un));
10174 	}
10175 
10176 	/* check for previous exclusive open */
10177 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10178 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10179 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10180 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10181 
10182 	if (un->un_exclopen & (partmask)) {
10183 		goto excl_open_fail;
10184 	}
10185 
10186 	if (flag & FEXCL) {
10187 		int i;
10188 		if (un->un_ocmap.lyropen[part]) {
10189 			goto excl_open_fail;
10190 		}
10191 		for (i = 0; i < (OTYPCNT - 1); i++) {
10192 			if (un->un_ocmap.regopen[i] & (partmask)) {
10193 				goto excl_open_fail;
10194 			}
10195 		}
10196 	}
10197 
10198 	/*
10199 	 * Check the write permission if this is a removable media device,
10200 	 * NDELAY has not been set, and writable permission is requested.
10201 	 *
10202 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10203 	 * attempt will fail with EIO as part of the I/O processing. This is a
10204 	 * more permissive implementation that allows the open to succeed and
10205 	 * WRITE attempts to fail when appropriate.
10206 	 */
10207 	if (un->un_f_chk_wp_open) {
10208 		if ((flag & FWRITE) && (!nodelay)) {
10209 			mutex_exit(SD_MUTEX(un));
10210 			/*
10211 			 * Defer the check for write permission on writable
10212 			 * DVD drive till sdstrategy and will not fail open even
10213 			 * if FWRITE is set as the device can be writable
10214 			 * depending upon the media and the media can change
10215 			 * after the call to open().
10216 			 */
10217 			if (un->un_f_dvdram_writable_device == FALSE) {
10218 				if (ISCD(un) || sr_check_wp(dev)) {
10219 				rval = EROFS;
10220 				mutex_enter(SD_MUTEX(un));
10221 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10222 				    "write to cd or write protected media\n");
10223 				goto open_fail;
10224 				}
10225 			}
10226 			mutex_enter(SD_MUTEX(un));
10227 		}
10228 	}
10229 
10230 	/*
10231 	 * If opening in NDELAY/NONBLOCK mode, just return.
10232 	 * Check if disk is ready and has a valid geometry later.
10233 	 */
10234 	if (!nodelay) {
10235 		sd_ssc_t	*ssc;
10236 
10237 		mutex_exit(SD_MUTEX(un));
10238 		ssc = sd_ssc_init(un);
10239 		rval = sd_ready_and_valid(ssc, part);
10240 		sd_ssc_fini(ssc);
10241 		mutex_enter(SD_MUTEX(un));
10242 		/*
10243 		 * Fail if device is not ready or if the number of disk
10244 		 * blocks is zero or negative for non CD devices.
10245 		 */
10246 
10247 		nblks = 0;
10248 
10249 		if (rval == SD_READY_VALID && (!ISCD(un))) {
10250 			/* if cmlb_partinfo fails, nblks remains 0 */
10251 			mutex_exit(SD_MUTEX(un));
10252 			(void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10253 			    NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10254 			mutex_enter(SD_MUTEX(un));
10255 		}
10256 
10257 		if ((rval != SD_READY_VALID) ||
10258 		    (!ISCD(un) && nblks <= 0)) {
10259 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10260 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10261 			    "device not ready or invalid disk block value\n");
10262 			goto open_fail;
10263 		}
10264 #if defined(__i386) || defined(__amd64)
10265 	} else {
10266 		uchar_t *cp;
10267 		/*
10268 		 * x86 requires special nodelay handling, so that p0 is
10269 		 * always defined and accessible.
10270 		 * Invalidate geometry only if device is not already open.
10271 		 */
10272 		cp = &un->un_ocmap.chkd[0];
10273 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10274 			if (*cp != (uchar_t)0) {
10275 				break;
10276 			}
10277 			cp++;
10278 		}
10279 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10280 			mutex_exit(SD_MUTEX(un));
10281 			cmlb_invalidate(un->un_cmlbhandle,
10282 			    (void *)SD_PATH_DIRECT);
10283 			mutex_enter(SD_MUTEX(un));
10284 		}
10285 
10286 #endif
10287 	}
10288 
10289 	if (otyp == OTYP_LYR) {
10290 		un->un_ocmap.lyropen[part]++;
10291 	} else {
10292 		un->un_ocmap.regopen[otyp] |= partmask;
10293 	}
10294 
10295 	/* Set up open and exclusive open flags */
10296 	if (flag & FEXCL) {
10297 		un->un_exclopen |= (partmask);
10298 	}
10299 
10300 	/*
10301 	 * If the lun is EFI labeled and lun capacity is greater than the
10302 	 * capacity contained in the label, log a sys-event to notify the
10303 	 * interested module.
10304 	 * To avoid an infinite loop of logging sys-event, we only log the
10305 	 * event when the lun is not opened in NDELAY mode. The event handler
10306 	 * should open the lun in NDELAY mode.
10307 	 */
10308 	if (!nodelay) {
10309 		mutex_exit(SD_MUTEX(un));
10310 		if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10311 		    (void*)SD_PATH_DIRECT) == 0) {
10312 			mutex_enter(SD_MUTEX(un));
10313 			if (un->un_f_blockcount_is_valid &&
10314 			    un->un_blockcount > label_cap &&
10315 			    un->un_f_expnevent == B_FALSE) {
10316 				un->un_f_expnevent = B_TRUE;
10317 				mutex_exit(SD_MUTEX(un));
10318 				sd_log_lun_expansion_event(un,
10319 				    (nodelay ? KM_NOSLEEP : KM_SLEEP));
10320 				mutex_enter(SD_MUTEX(un));
10321 			}
10322 		} else {
10323 			mutex_enter(SD_MUTEX(un));
10324 		}
10325 	}
10326 
10327 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10328 	    "open of part %d type %d\n", part, otyp);
10329 
10330 	mutex_exit(SD_MUTEX(un));
10331 	if (!nodelay) {
10332 		sd_pm_exit(un);
10333 	}
10334 
10335 	sema_v(&un->un_semoclose);
10336 
10337 	mutex_enter(&sd_detach_mutex);
10338 	un->un_opens_in_progress--;
10339 	mutex_exit(&sd_detach_mutex);
10340 
10341 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10342 	return (DDI_SUCCESS);
10343 
10344 excl_open_fail:
10345 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10346 	rval = EBUSY;
10347 
10348 open_fail:
10349 	mutex_exit(SD_MUTEX(un));
10350 
10351 	/*
10352 	 * On a failed open we must exit the pm management.
10353 	 */
10354 	if (!nodelay) {
10355 		sd_pm_exit(un);
10356 	}
10357 open_failed_with_pm:
10358 	sema_v(&un->un_semoclose);
10359 
10360 	mutex_enter(&sd_detach_mutex);
10361 	un->un_opens_in_progress--;
10362 	if (otyp == OTYP_LYR) {
10363 		un->un_layer_count--;
10364 	}
10365 	mutex_exit(&sd_detach_mutex);
10366 
10367 	return (rval);
10368 }
10369 
10370 
10371 /*
10372  *    Function: sdclose
10373  *
10374  * Description: Driver's close(9e) entry point function.
10375  *
10376  *   Arguments: dev    - device number
10377  *		flag   - file status flag, informational only
10378  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10379  *		cred_p - user credential pointer
10380  *
10381  * Return Code: ENXIO
10382  *
10383  *     Context: Kernel thread context
10384  */
10385 /* ARGSUSED */
10386 static int
10387 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10388 {
10389 	struct sd_lun	*un;
10390 	uchar_t		*cp;
10391 	int		part;
10392 	int		nodelay;
10393 	int		rval = 0;
10394 
10395 	/* Validate the open type */
10396 	if (otyp >= OTYPCNT) {
10397 		return (ENXIO);
10398 	}
10399 
10400 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10401 		return (ENXIO);
10402 	}
10403 
10404 	part = SDPART(dev);
10405 	nodelay = flag & (FNDELAY | FNONBLOCK);
10406 
10407 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10408 	    "sdclose: close of part %d type %d\n", part, otyp);
10409 
10410 	/*
10411 	 * We use a semaphore here in order to serialize
10412 	 * open and close requests on the device.
10413 	 */
10414 	sema_p(&un->un_semoclose);
10415 
10416 	mutex_enter(SD_MUTEX(un));
10417 
10418 	/* Don't proceed if power is being changed. */
10419 	while (un->un_state == SD_STATE_PM_CHANGING) {
10420 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10421 	}
10422 
10423 	if (un->un_exclopen & (1 << part)) {
10424 		un->un_exclopen &= ~(1 << part);
10425 	}
10426 
10427 	/* Update the open partition map */
10428 	if (otyp == OTYP_LYR) {
10429 		un->un_ocmap.lyropen[part] -= 1;
10430 	} else {
10431 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10432 	}
10433 
10434 	cp = &un->un_ocmap.chkd[0];
10435 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10436 		if (*cp != NULL) {
10437 			break;
10438 		}
10439 		cp++;
10440 	}
10441 
10442 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10443 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10444 
10445 		/*
10446 		 * We avoid persistance upon the last close, and set
10447 		 * the throttle back to the maximum.
10448 		 */
10449 		un->un_throttle = un->un_saved_throttle;
10450 
10451 		if (un->un_state == SD_STATE_OFFLINE) {
10452 			if (un->un_f_is_fibre == FALSE) {
10453 				scsi_log(SD_DEVINFO(un), sd_label,
10454 				    CE_WARN, "offline\n");
10455 			}
10456 			mutex_exit(SD_MUTEX(un));
10457 			cmlb_invalidate(un->un_cmlbhandle,
10458 			    (void *)SD_PATH_DIRECT);
10459 			mutex_enter(SD_MUTEX(un));
10460 
10461 		} else {
10462 			/*
10463 			 * Flush any outstanding writes in NVRAM cache.
10464 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10465 			 * cmd, it may not work for non-Pluto devices.
10466 			 * SYNCHRONIZE CACHE is not required for removables,
10467 			 * except DVD-RAM drives.
10468 			 *
10469 			 * Also note: because SYNCHRONIZE CACHE is currently
10470 			 * the only command issued here that requires the
10471 			 * drive be powered up, only do the power up before
10472 			 * sending the Sync Cache command. If additional
10473 			 * commands are added which require a powered up
10474 			 * drive, the following sequence may have to change.
10475 			 *
10476 			 * And finally, note that parallel SCSI on SPARC
10477 			 * only issues a Sync Cache to DVD-RAM, a newly
10478 			 * supported device.
10479 			 */
10480 #if defined(__i386) || defined(__amd64)
10481 			if ((un->un_f_sync_cache_supported &&
10482 			    un->un_f_sync_cache_required) ||
10483 			    un->un_f_dvdram_writable_device == TRUE) {
10484 #else
10485 			if (un->un_f_dvdram_writable_device == TRUE) {
10486 #endif
10487 				mutex_exit(SD_MUTEX(un));
10488 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10489 					rval =
10490 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10491 					    NULL);
10492 					/* ignore error if not supported */
10493 					if (rval == ENOTSUP) {
10494 						rval = 0;
10495 					} else if (rval != 0) {
10496 						rval = EIO;
10497 					}
10498 					sd_pm_exit(un);
10499 				} else {
10500 					rval = EIO;
10501 				}
10502 				mutex_enter(SD_MUTEX(un));
10503 			}
10504 
10505 			/*
10506 			 * For devices which supports DOOR_LOCK, send an ALLOW
10507 			 * MEDIA REMOVAL command, but don't get upset if it
10508 			 * fails. We need to raise the power of the drive before
10509 			 * we can call sd_send_scsi_DOORLOCK()
10510 			 */
10511 			if (un->un_f_doorlock_supported) {
10512 				mutex_exit(SD_MUTEX(un));
10513 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10514 					sd_ssc_t	*ssc;
10515 
10516 					ssc = sd_ssc_init(un);
10517 					rval = sd_send_scsi_DOORLOCK(ssc,
10518 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10519 					if (rval != 0)
10520 						sd_ssc_assessment(ssc,
10521 						    SD_FMT_IGNORE);
10522 					sd_ssc_fini(ssc);
10523 
10524 					sd_pm_exit(un);
10525 					if (ISCD(un) && (rval != 0) &&
10526 					    (nodelay != 0)) {
10527 						rval = ENXIO;
10528 					}
10529 				} else {
10530 					rval = EIO;
10531 				}
10532 				mutex_enter(SD_MUTEX(un));
10533 			}
10534 
10535 			/*
10536 			 * If a device has removable media, invalidate all
10537 			 * parameters related to media, such as geometry,
10538 			 * blocksize, and blockcount.
10539 			 */
10540 			if (un->un_f_has_removable_media) {
10541 				sr_ejected(un);
10542 			}
10543 
10544 			/*
10545 			 * Destroy the cache (if it exists) which was
10546 			 * allocated for the write maps since this is
10547 			 * the last close for this media.
10548 			 */
10549 			if (un->un_wm_cache) {
10550 				/*
10551 				 * Check if there are pending commands.
10552 				 * and if there are give a warning and
10553 				 * do not destroy the cache.
10554 				 */
10555 				if (un->un_ncmds_in_driver > 0) {
10556 					scsi_log(SD_DEVINFO(un),
10557 					    sd_label, CE_WARN,
10558 					    "Unable to clean up memory "
10559 					    "because of pending I/O\n");
10560 				} else {
10561 					kmem_cache_destroy(
10562 					    un->un_wm_cache);
10563 					un->un_wm_cache = NULL;
10564 				}
10565 			}
10566 		}
10567 	}
10568 
10569 	mutex_exit(SD_MUTEX(un));
10570 	sema_v(&un->un_semoclose);
10571 
10572 	if (otyp == OTYP_LYR) {
10573 		mutex_enter(&sd_detach_mutex);
10574 		/*
10575 		 * The detach routine may run when the layer count
10576 		 * drops to zero.
10577 		 */
10578 		un->un_layer_count--;
10579 		mutex_exit(&sd_detach_mutex);
10580 	}
10581 
10582 	return (rval);
10583 }
10584 
10585 
10586 /*
10587  *    Function: sd_ready_and_valid
10588  *
10589  * Description: Test if device is ready and has a valid geometry.
10590  *
10591  *   Arguments: ssc - sd_ssc_t will contain un
10592  *		un  - driver soft state (unit) structure
10593  *
10594  * Return Code: SD_READY_VALID		ready and valid label
10595  *		SD_NOT_READY_VALID	not ready, no label
10596  *		SD_RESERVED_BY_OTHERS	reservation conflict
10597  *
10598  *     Context: Never called at interrupt context.
10599  */
10600 
10601 static int
10602 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10603 {
10604 	struct sd_errstats	*stp;
10605 	uint64_t		capacity;
10606 	uint_t			lbasize;
10607 	int			rval = SD_READY_VALID;
10608 	char			name_str[48];
10609 	boolean_t		is_valid;
10610 	struct sd_lun		*un;
10611 	int			status;
10612 
10613 	ASSERT(ssc != NULL);
10614 	un = ssc->ssc_un;
10615 	ASSERT(un != NULL);
10616 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10617 
10618 	mutex_enter(SD_MUTEX(un));
10619 	/*
10620 	 * If a device has removable media, we must check if media is
10621 	 * ready when checking if this device is ready and valid.
10622 	 */
10623 	if (un->un_f_has_removable_media) {
10624 		mutex_exit(SD_MUTEX(un));
10625 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10626 
10627 		if (status != 0) {
10628 			rval = SD_NOT_READY_VALID;
10629 			mutex_enter(SD_MUTEX(un));
10630 
10631 			/* Ignore all failed status for removalbe media */
10632 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10633 
10634 			goto done;
10635 		}
10636 
10637 		is_valid = SD_IS_VALID_LABEL(un);
10638 		mutex_enter(SD_MUTEX(un));
10639 		if (!is_valid ||
10640 		    (un->un_f_blockcount_is_valid == FALSE) ||
10641 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10642 
10643 			/* capacity has to be read every open. */
10644 			mutex_exit(SD_MUTEX(un));
10645 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10646 			    &lbasize, SD_PATH_DIRECT);
10647 
10648 			if (status != 0) {
10649 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10650 
10651 				cmlb_invalidate(un->un_cmlbhandle,
10652 				    (void *)SD_PATH_DIRECT);
10653 				mutex_enter(SD_MUTEX(un));
10654 				rval = SD_NOT_READY_VALID;
10655 
10656 				goto done;
10657 			} else {
10658 				mutex_enter(SD_MUTEX(un));
10659 				sd_update_block_info(un, lbasize, capacity);
10660 			}
10661 		}
10662 
10663 		/*
10664 		 * Check if the media in the device is writable or not.
10665 		 */
10666 		if (!is_valid && ISCD(un)) {
10667 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10668 		}
10669 
10670 	} else {
10671 		/*
10672 		 * Do a test unit ready to clear any unit attention from non-cd
10673 		 * devices.
10674 		 */
10675 		mutex_exit(SD_MUTEX(un));
10676 
10677 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10678 		if (status != 0) {
10679 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10680 		}
10681 
10682 		mutex_enter(SD_MUTEX(un));
10683 	}
10684 
10685 
10686 	/*
10687 	 * If this is a non 512 block device, allocate space for
10688 	 * the wmap cache. This is being done here since every time
10689 	 * a media is changed this routine will be called and the
10690 	 * block size is a function of media rather than device.
10691 	 */
10692 	if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10693 	    un->un_f_non_devbsize_supported) &&
10694 	    un->un_tgt_blocksize != DEV_BSIZE) ||
10695 	    un->un_f_enable_rmw) {
10696 		if (!(un->un_wm_cache)) {
10697 			(void) snprintf(name_str, sizeof (name_str),
10698 			    "%s%d_cache",
10699 			    ddi_driver_name(SD_DEVINFO(un)),
10700 			    ddi_get_instance(SD_DEVINFO(un)));
10701 			un->un_wm_cache = kmem_cache_create(
10702 			    name_str, sizeof (struct sd_w_map),
10703 			    8, sd_wm_cache_constructor,
10704 			    sd_wm_cache_destructor, NULL,
10705 			    (void *)un, NULL, 0);
10706 			if (!(un->un_wm_cache)) {
10707 				rval = ENOMEM;
10708 				goto done;
10709 			}
10710 		}
10711 	}
10712 
10713 	if (un->un_state == SD_STATE_NORMAL) {
10714 		/*
10715 		 * If the target is not yet ready here (defined by a TUR
10716 		 * failure), invalidate the geometry and print an 'offline'
10717 		 * message. This is a legacy message, as the state of the
10718 		 * target is not actually changed to SD_STATE_OFFLINE.
10719 		 *
10720 		 * If the TUR fails for EACCES (Reservation Conflict),
10721 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
10722 		 * reservation conflict. If the TUR fails for other
10723 		 * reasons, SD_NOT_READY_VALID will be returned.
10724 		 */
10725 		int err;
10726 
10727 		mutex_exit(SD_MUTEX(un));
10728 		err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10729 		mutex_enter(SD_MUTEX(un));
10730 
10731 		if (err != 0) {
10732 			mutex_exit(SD_MUTEX(un));
10733 			cmlb_invalidate(un->un_cmlbhandle,
10734 			    (void *)SD_PATH_DIRECT);
10735 			mutex_enter(SD_MUTEX(un));
10736 			if (err == EACCES) {
10737 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10738 				    "reservation conflict\n");
10739 				rval = SD_RESERVED_BY_OTHERS;
10740 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10741 			} else {
10742 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10743 				    "drive offline\n");
10744 				rval = SD_NOT_READY_VALID;
10745 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10746 			}
10747 			goto done;
10748 		}
10749 	}
10750 
10751 	if (un->un_f_format_in_progress == FALSE) {
10752 		mutex_exit(SD_MUTEX(un));
10753 
10754 		(void) cmlb_validate(un->un_cmlbhandle, 0,
10755 		    (void *)SD_PATH_DIRECT);
10756 		if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10757 		    NULL, (void *) SD_PATH_DIRECT) != 0) {
10758 			rval = SD_NOT_READY_VALID;
10759 			mutex_enter(SD_MUTEX(un));
10760 
10761 			goto done;
10762 		}
10763 		if (un->un_f_pkstats_enabled) {
10764 			sd_set_pstats(un);
10765 			SD_TRACE(SD_LOG_IO_PARTITION, un,
10766 			    "sd_ready_and_valid: un:0x%p pstats created and "
10767 			    "set\n", un);
10768 		}
10769 		mutex_enter(SD_MUTEX(un));
10770 	}
10771 
10772 	/*
10773 	 * If this device supports DOOR_LOCK command, try and send
10774 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10775 	 * if it fails. For a CD, however, it is an error
10776 	 */
10777 	if (un->un_f_doorlock_supported) {
10778 		mutex_exit(SD_MUTEX(un));
10779 		status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10780 		    SD_PATH_DIRECT);
10781 
10782 		if ((status != 0) && ISCD(un)) {
10783 			rval = SD_NOT_READY_VALID;
10784 			mutex_enter(SD_MUTEX(un));
10785 
10786 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10787 
10788 			goto done;
10789 		} else if (status != 0)
10790 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10791 		mutex_enter(SD_MUTEX(un));
10792 	}
10793 
10794 	/* The state has changed, inform the media watch routines */
10795 	un->un_mediastate = DKIO_INSERTED;
10796 	cv_broadcast(&un->un_state_cv);
10797 	rval = SD_READY_VALID;
10798 
10799 done:
10800 
10801 	/*
10802 	 * Initialize the capacity kstat value, if no media previously
10803 	 * (capacity kstat is 0) and a media has been inserted
10804 	 * (un_blockcount > 0).
10805 	 */
10806 	if (un->un_errstats != NULL) {
10807 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10808 		if ((stp->sd_capacity.value.ui64 == 0) &&
10809 		    (un->un_f_blockcount_is_valid == TRUE)) {
10810 			stp->sd_capacity.value.ui64 =
10811 			    (uint64_t)((uint64_t)un->un_blockcount *
10812 			    un->un_sys_blocksize);
10813 		}
10814 	}
10815 
10816 	mutex_exit(SD_MUTEX(un));
10817 	return (rval);
10818 }
10819 
10820 
10821 /*
10822  *    Function: sdmin
10823  *
10824  * Description: Routine to limit the size of a data transfer. Used in
10825  *		conjunction with physio(9F).
10826  *
10827  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10828  *
10829  *     Context: Kernel thread context.
10830  */
10831 
10832 static void
10833 sdmin(struct buf *bp)
10834 {
10835 	struct sd_lun	*un;
10836 	int		instance;
10837 
10838 	instance = SDUNIT(bp->b_edev);
10839 
10840 	un = ddi_get_soft_state(sd_state, instance);
10841 	ASSERT(un != NULL);
10842 
10843 	/*
10844 	 * We depend on buf breakup to restrict
10845 	 * IO size if it is enabled.
10846 	 */
10847 	if (un->un_buf_breakup_supported) {
10848 		return;
10849 	}
10850 
10851 	if (bp->b_bcount > un->un_max_xfer_size) {
10852 		bp->b_bcount = un->un_max_xfer_size;
10853 	}
10854 }
10855 
10856 
10857 /*
10858  *    Function: sdread
10859  *
10860  * Description: Driver's read(9e) entry point function.
10861  *
10862  *   Arguments: dev   - device number
10863  *		uio   - structure pointer describing where data is to be stored
10864  *			in user's space
10865  *		cred_p  - user credential pointer
10866  *
10867  * Return Code: ENXIO
10868  *		EIO
10869  *		EINVAL
10870  *		value returned by physio
10871  *
10872  *     Context: Kernel thread context.
10873  */
10874 /* ARGSUSED */
10875 static int
10876 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10877 {
10878 	struct sd_lun	*un = NULL;
10879 	int		secmask;
10880 	int		err = 0;
10881 	sd_ssc_t	*ssc;
10882 
10883 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10884 		return (ENXIO);
10885 	}
10886 
10887 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10888 
10889 
10890 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10891 		mutex_enter(SD_MUTEX(un));
10892 		/*
10893 		 * Because the call to sd_ready_and_valid will issue I/O we
10894 		 * must wait here if either the device is suspended or
10895 		 * if it's power level is changing.
10896 		 */
10897 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10898 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10899 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10900 		}
10901 		un->un_ncmds_in_driver++;
10902 		mutex_exit(SD_MUTEX(un));
10903 
10904 		/* Initialize sd_ssc_t for internal uscsi commands */
10905 		ssc = sd_ssc_init(un);
10906 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10907 			err = EIO;
10908 		} else {
10909 			err = 0;
10910 		}
10911 		sd_ssc_fini(ssc);
10912 
10913 		mutex_enter(SD_MUTEX(un));
10914 		un->un_ncmds_in_driver--;
10915 		ASSERT(un->un_ncmds_in_driver >= 0);
10916 		mutex_exit(SD_MUTEX(un));
10917 		if (err != 0)
10918 			return (err);
10919 	}
10920 
10921 	/*
10922 	 * Read requests are restricted to multiples of the system block size.
10923 	 */
10924 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10925 	    !un->un_f_enable_rmw)
10926 		secmask = un->un_tgt_blocksize - 1;
10927 	else
10928 		secmask = DEV_BSIZE - 1;
10929 
10930 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10931 		SD_ERROR(SD_LOG_READ_WRITE, un,
10932 		    "sdread: file offset not modulo %d\n",
10933 		    secmask + 1);
10934 		err = EINVAL;
10935 	} else if (uio->uio_iov->iov_len & (secmask)) {
10936 		SD_ERROR(SD_LOG_READ_WRITE, un,
10937 		    "sdread: transfer length not modulo %d\n",
10938 		    secmask + 1);
10939 		err = EINVAL;
10940 	} else {
10941 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10942 	}
10943 
10944 	return (err);
10945 }
10946 
10947 
10948 /*
10949  *    Function: sdwrite
10950  *
10951  * Description: Driver's write(9e) entry point function.
10952  *
10953  *   Arguments: dev   - device number
10954  *		uio   - structure pointer describing where data is stored in
10955  *			user's space
10956  *		cred_p  - user credential pointer
10957  *
10958  * Return Code: ENXIO
10959  *		EIO
10960  *		EINVAL
10961  *		value returned by physio
10962  *
10963  *     Context: Kernel thread context.
10964  */
10965 /* ARGSUSED */
10966 static int
10967 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10968 {
10969 	struct sd_lun	*un = NULL;
10970 	int		secmask;
10971 	int		err = 0;
10972 	sd_ssc_t	*ssc;
10973 
10974 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10975 		return (ENXIO);
10976 	}
10977 
10978 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10979 
10980 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10981 		mutex_enter(SD_MUTEX(un));
10982 		/*
10983 		 * Because the call to sd_ready_and_valid will issue I/O we
10984 		 * must wait here if either the device is suspended or
10985 		 * if it's power level is changing.
10986 		 */
10987 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10988 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10989 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10990 		}
10991 		un->un_ncmds_in_driver++;
10992 		mutex_exit(SD_MUTEX(un));
10993 
10994 		/* Initialize sd_ssc_t for internal uscsi commands */
10995 		ssc = sd_ssc_init(un);
10996 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10997 			err = EIO;
10998 		} else {
10999 			err = 0;
11000 		}
11001 		sd_ssc_fini(ssc);
11002 
11003 		mutex_enter(SD_MUTEX(un));
11004 		un->un_ncmds_in_driver--;
11005 		ASSERT(un->un_ncmds_in_driver >= 0);
11006 		mutex_exit(SD_MUTEX(un));
11007 		if (err != 0)
11008 			return (err);
11009 	}
11010 
11011 	/*
11012 	 * Write requests are restricted to multiples of the system block size.
11013 	 */
11014 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11015 	    !un->un_f_enable_rmw)
11016 		secmask = un->un_tgt_blocksize - 1;
11017 	else
11018 		secmask = DEV_BSIZE - 1;
11019 
11020 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11021 		SD_ERROR(SD_LOG_READ_WRITE, un,
11022 		    "sdwrite: file offset not modulo %d\n",
11023 		    secmask + 1);
11024 		err = EINVAL;
11025 	} else if (uio->uio_iov->iov_len & (secmask)) {
11026 		SD_ERROR(SD_LOG_READ_WRITE, un,
11027 		    "sdwrite: transfer length not modulo %d\n",
11028 		    secmask + 1);
11029 		err = EINVAL;
11030 	} else {
11031 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11032 	}
11033 
11034 	return (err);
11035 }
11036 
11037 
11038 /*
11039  *    Function: sdaread
11040  *
11041  * Description: Driver's aread(9e) entry point function.
11042  *
11043  *   Arguments: dev   - device number
11044  *		aio   - structure pointer describing where data is to be stored
11045  *		cred_p  - user credential pointer
11046  *
11047  * Return Code: ENXIO
11048  *		EIO
11049  *		EINVAL
11050  *		value returned by aphysio
11051  *
11052  *     Context: Kernel thread context.
11053  */
11054 /* ARGSUSED */
11055 static int
11056 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11057 {
11058 	struct sd_lun	*un = NULL;
11059 	struct uio	*uio = aio->aio_uio;
11060 	int		secmask;
11061 	int		err = 0;
11062 	sd_ssc_t	*ssc;
11063 
11064 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11065 		return (ENXIO);
11066 	}
11067 
11068 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11069 
11070 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11071 		mutex_enter(SD_MUTEX(un));
11072 		/*
11073 		 * Because the call to sd_ready_and_valid will issue I/O we
11074 		 * must wait here if either the device is suspended or
11075 		 * if it's power level is changing.
11076 		 */
11077 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11078 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11079 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11080 		}
11081 		un->un_ncmds_in_driver++;
11082 		mutex_exit(SD_MUTEX(un));
11083 
11084 		/* Initialize sd_ssc_t for internal uscsi commands */
11085 		ssc = sd_ssc_init(un);
11086 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11087 			err = EIO;
11088 		} else {
11089 			err = 0;
11090 		}
11091 		sd_ssc_fini(ssc);
11092 
11093 		mutex_enter(SD_MUTEX(un));
11094 		un->un_ncmds_in_driver--;
11095 		ASSERT(un->un_ncmds_in_driver >= 0);
11096 		mutex_exit(SD_MUTEX(un));
11097 		if (err != 0)
11098 			return (err);
11099 	}
11100 
11101 	/*
11102 	 * Read requests are restricted to multiples of the system block size.
11103 	 */
11104 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11105 	    !un->un_f_enable_rmw)
11106 		secmask = un->un_tgt_blocksize - 1;
11107 	else
11108 		secmask = DEV_BSIZE - 1;
11109 
11110 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11111 		SD_ERROR(SD_LOG_READ_WRITE, un,
11112 		    "sdaread: file offset not modulo %d\n",
11113 		    secmask + 1);
11114 		err = EINVAL;
11115 	} else if (uio->uio_iov->iov_len & (secmask)) {
11116 		SD_ERROR(SD_LOG_READ_WRITE, un,
11117 		    "sdaread: transfer length not modulo %d\n",
11118 		    secmask + 1);
11119 		err = EINVAL;
11120 	} else {
11121 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11122 	}
11123 
11124 	return (err);
11125 }
11126 
11127 
11128 /*
11129  *    Function: sdawrite
11130  *
11131  * Description: Driver's awrite(9e) entry point function.
11132  *
11133  *   Arguments: dev   - device number
11134  *		aio   - structure pointer describing where data is stored
11135  *		cred_p  - user credential pointer
11136  *
11137  * Return Code: ENXIO
11138  *		EIO
11139  *		EINVAL
11140  *		value returned by aphysio
11141  *
11142  *     Context: Kernel thread context.
11143  */
11144 /* ARGSUSED */
11145 static int
11146 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11147 {
11148 	struct sd_lun	*un = NULL;
11149 	struct uio	*uio = aio->aio_uio;
11150 	int		secmask;
11151 	int		err = 0;
11152 	sd_ssc_t	*ssc;
11153 
11154 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11155 		return (ENXIO);
11156 	}
11157 
11158 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11159 
11160 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11161 		mutex_enter(SD_MUTEX(un));
11162 		/*
11163 		 * Because the call to sd_ready_and_valid will issue I/O we
11164 		 * must wait here if either the device is suspended or
11165 		 * if it's power level is changing.
11166 		 */
11167 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11168 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11169 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11170 		}
11171 		un->un_ncmds_in_driver++;
11172 		mutex_exit(SD_MUTEX(un));
11173 
11174 		/* Initialize sd_ssc_t for internal uscsi commands */
11175 		ssc = sd_ssc_init(un);
11176 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11177 			err = EIO;
11178 		} else {
11179 			err = 0;
11180 		}
11181 		sd_ssc_fini(ssc);
11182 
11183 		mutex_enter(SD_MUTEX(un));
11184 		un->un_ncmds_in_driver--;
11185 		ASSERT(un->un_ncmds_in_driver >= 0);
11186 		mutex_exit(SD_MUTEX(un));
11187 		if (err != 0)
11188 			return (err);
11189 	}
11190 
11191 	/*
11192 	 * Write requests are restricted to multiples of the system block size.
11193 	 */
11194 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11195 	    !un->un_f_enable_rmw)
11196 		secmask = un->un_tgt_blocksize - 1;
11197 	else
11198 		secmask = DEV_BSIZE - 1;
11199 
11200 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11201 		SD_ERROR(SD_LOG_READ_WRITE, un,
11202 		    "sdawrite: file offset not modulo %d\n",
11203 		    secmask + 1);
11204 		err = EINVAL;
11205 	} else if (uio->uio_iov->iov_len & (secmask)) {
11206 		SD_ERROR(SD_LOG_READ_WRITE, un,
11207 		    "sdawrite: transfer length not modulo %d\n",
11208 		    secmask + 1);
11209 		err = EINVAL;
11210 	} else {
11211 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11212 	}
11213 
11214 	return (err);
11215 }
11216 
11217 
11218 
11219 
11220 
11221 /*
11222  * Driver IO processing follows the following sequence:
11223  *
11224  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11225  *         |                |                     ^
11226  *         v                v                     |
11227  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11228  *         |                |                     |                   |
11229  *         v                |                     |                   |
11230  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11231  *         |                |                     ^                   ^
11232  *         v                v                     |                   |
11233  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11234  *         |                |                     |                   |
11235  *     +---+                |                     +------------+      +-------+
11236  *     |                    |                                  |              |
11237  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11238  *     |                    v                                  |              |
11239  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11240  *     |                    |                                  ^              |
11241  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11242  *     |                    v                                  |              |
11243  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11244  *     |                    |                                  ^              |
11245  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11246  *     |                    v                                  |              |
11247  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11248  *     |                    |                                  ^              |
11249  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11250  *     |                    v                                  |              |
11251  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11252  *     |                    |                                  ^              |
11253  *     |                    |                                  |              |
11254  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11255  *                          |                           ^
11256  *                          v                           |
11257  *                   sd_core_iostart()                  |
11258  *                          |                           |
11259  *                          |                           +------>(*destroypkt)()
11260  *                          +-> sd_start_cmds() <-+     |           |
11261  *                          |                     |     |           v
11262  *                          |                     |     |  scsi_destroy_pkt(9F)
11263  *                          |                     |     |
11264  *                          +->(*initpkt)()       +- sdintr()
11265  *                          |  |                        |  |
11266  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11267  *                          |  +-> scsi_setup_cdb(9F)   |
11268  *                          |                           |
11269  *                          +--> scsi_transport(9F)     |
11270  *                                     |                |
11271  *                                     +----> SCSA ---->+
11272  *
11273  *
11274  * This code is based upon the following presumptions:
11275  *
11276  *   - iostart and iodone functions operate on buf(9S) structures. These
11277  *     functions perform the necessary operations on the buf(9S) and pass
11278  *     them along to the next function in the chain by using the macros
11279  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11280  *     (for iodone side functions).
11281  *
11282  *   - The iostart side functions may sleep. The iodone side functions
11283  *     are called under interrupt context and may NOT sleep. Therefore
11284  *     iodone side functions also may not call iostart side functions.
11285  *     (NOTE: iostart side functions should NOT sleep for memory, as
11286  *     this could result in deadlock.)
11287  *
11288  *   - An iostart side function may call its corresponding iodone side
11289  *     function directly (if necessary).
11290  *
11291  *   - In the event of an error, an iostart side function can return a buf(9S)
11292  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11293  *     b_error in the usual way of course).
11294  *
11295  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11296  *     requests to the iostart side functions.  The iostart side functions in
11297  *     this case would be called under the context of a taskq thread, so it's
11298  *     OK for them to block/sleep/spin in this case.
11299  *
11300  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11301  *     pass them along to the next function in the chain.  The corresponding
11302  *     iodone side functions must coalesce the "shadow" bufs and return
11303  *     the "original" buf to the next higher layer.
11304  *
11305  *   - The b_private field of the buf(9S) struct holds a pointer to
11306  *     an sd_xbuf struct, which contains information needed to
11307  *     construct the scsi_pkt for the command.
11308  *
11309  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11310  *     layer must acquire & release the SD_MUTEX(un) as needed.
11311  */
11312 
11313 
11314 /*
11315  * Create taskq for all targets in the system. This is created at
11316  * _init(9E) and destroyed at _fini(9E).
11317  *
11318  * Note: here we set the minalloc to a reasonably high number to ensure that
11319  * we will have an adequate supply of task entries available at interrupt time.
11320  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11321  * sd_create_taskq().  Since we do not want to sleep for allocations at
11322  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11323  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11324  * requests any one instant in time.
11325  */
11326 #define	SD_TASKQ_NUMTHREADS	8
11327 #define	SD_TASKQ_MINALLOC	256
11328 #define	SD_TASKQ_MAXALLOC	256
11329 
11330 static taskq_t	*sd_tq = NULL;
11331 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11332 
11333 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11334 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11335 
11336 /*
11337  * The following task queue is being created for the write part of
11338  * read-modify-write of non-512 block size devices.
11339  * Limit the number of threads to 1 for now. This number has been chosen
11340  * considering the fact that it applies only to dvd ram drives/MO drives
11341  * currently. Performance for which is not main criteria at this stage.
11342  * Note: It needs to be explored if we can use a single taskq in future
11343  */
11344 #define	SD_WMR_TASKQ_NUMTHREADS	1
11345 static taskq_t	*sd_wmr_tq = NULL;
11346 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11347 
11348 /*
11349  *    Function: sd_taskq_create
11350  *
11351  * Description: Create taskq thread(s) and preallocate task entries
11352  *
11353  * Return Code: Returns a pointer to the allocated taskq_t.
11354  *
11355  *     Context: Can sleep. Requires blockable context.
11356  *
11357  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11358  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11359  *		- taskq_create() will block for memory, also it will panic
11360  *		  if it cannot create the requested number of threads.
11361  *		- Currently taskq_create() creates threads that cannot be
11362  *		  swapped.
11363  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11364  *		  supply of taskq entries at interrupt time (ie, so that we
11365  *		  do not have to sleep for memory)
11366  */
11367 
11368 static void
11369 sd_taskq_create(void)
11370 {
11371 	char	taskq_name[TASKQ_NAMELEN];
11372 
11373 	ASSERT(sd_tq == NULL);
11374 	ASSERT(sd_wmr_tq == NULL);
11375 
11376 	(void) snprintf(taskq_name, sizeof (taskq_name),
11377 	    "%s_drv_taskq", sd_label);
11378 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11379 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11380 	    TASKQ_PREPOPULATE));
11381 
11382 	(void) snprintf(taskq_name, sizeof (taskq_name),
11383 	    "%s_rmw_taskq", sd_label);
11384 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11385 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11386 	    TASKQ_PREPOPULATE));
11387 }
11388 
11389 
11390 /*
11391  *    Function: sd_taskq_delete
11392  *
11393  * Description: Complementary cleanup routine for sd_taskq_create().
11394  *
11395  *     Context: Kernel thread context.
11396  */
11397 
11398 static void
11399 sd_taskq_delete(void)
11400 {
11401 	ASSERT(sd_tq != NULL);
11402 	ASSERT(sd_wmr_tq != NULL);
11403 	taskq_destroy(sd_tq);
11404 	taskq_destroy(sd_wmr_tq);
11405 	sd_tq = NULL;
11406 	sd_wmr_tq = NULL;
11407 }
11408 
11409 
11410 /*
11411  *    Function: sdstrategy
11412  *
11413  * Description: Driver's strategy (9E) entry point function.
11414  *
11415  *   Arguments: bp - pointer to buf(9S)
11416  *
11417  * Return Code: Always returns zero
11418  *
11419  *     Context: Kernel thread context.
11420  */
11421 
11422 static int
11423 sdstrategy(struct buf *bp)
11424 {
11425 	struct sd_lun *un;
11426 
11427 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11428 	if (un == NULL) {
11429 		bioerror(bp, EIO);
11430 		bp->b_resid = bp->b_bcount;
11431 		biodone(bp);
11432 		return (0);
11433 	}
11434 
11435 	/* As was done in the past, fail new cmds. if state is dumping. */
11436 	if (un->un_state == SD_STATE_DUMPING) {
11437 		bioerror(bp, ENXIO);
11438 		bp->b_resid = bp->b_bcount;
11439 		biodone(bp);
11440 		return (0);
11441 	}
11442 
11443 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11444 
11445 	/*
11446 	 * Commands may sneak in while we released the mutex in
11447 	 * DDI_SUSPEND, we should block new commands. However, old
11448 	 * commands that are still in the driver at this point should
11449 	 * still be allowed to drain.
11450 	 */
11451 	mutex_enter(SD_MUTEX(un));
11452 	/*
11453 	 * Must wait here if either the device is suspended or
11454 	 * if it's power level is changing.
11455 	 */
11456 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11457 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11458 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11459 	}
11460 
11461 	un->un_ncmds_in_driver++;
11462 
11463 	/*
11464 	 * atapi: Since we are running the CD for now in PIO mode we need to
11465 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11466 	 * the HBA's init_pkt routine.
11467 	 */
11468 	if (un->un_f_cfg_is_atapi == TRUE) {
11469 		mutex_exit(SD_MUTEX(un));
11470 		bp_mapin(bp);
11471 		mutex_enter(SD_MUTEX(un));
11472 	}
11473 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11474 	    un->un_ncmds_in_driver);
11475 
11476 	if (bp->b_flags & B_WRITE)
11477 		un->un_f_sync_cache_required = TRUE;
11478 
11479 	mutex_exit(SD_MUTEX(un));
11480 
11481 	/*
11482 	 * This will (eventually) allocate the sd_xbuf area and
11483 	 * call sd_xbuf_strategy().  We just want to return the
11484 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11485 	 * imized tail call which saves us a stack frame.
11486 	 */
11487 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11488 }
11489 
11490 
11491 /*
11492  *    Function: sd_xbuf_strategy
11493  *
11494  * Description: Function for initiating IO operations via the
11495  *		ddi_xbuf_qstrategy() mechanism.
11496  *
11497  *     Context: Kernel thread context.
11498  */
11499 
11500 static void
11501 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11502 {
11503 	struct sd_lun *un = arg;
11504 
11505 	ASSERT(bp != NULL);
11506 	ASSERT(xp != NULL);
11507 	ASSERT(un != NULL);
11508 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11509 
11510 	/*
11511 	 * Initialize the fields in the xbuf and save a pointer to the
11512 	 * xbuf in bp->b_private.
11513 	 */
11514 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11515 
11516 	/* Send the buf down the iostart chain */
11517 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11518 }
11519 
11520 
11521 /*
11522  *    Function: sd_xbuf_init
11523  *
11524  * Description: Prepare the given sd_xbuf struct for use.
11525  *
11526  *   Arguments: un - ptr to softstate
11527  *		bp - ptr to associated buf(9S)
11528  *		xp - ptr to associated sd_xbuf
11529  *		chain_type - IO chain type to use:
11530  *			SD_CHAIN_NULL
11531  *			SD_CHAIN_BUFIO
11532  *			SD_CHAIN_USCSI
11533  *			SD_CHAIN_DIRECT
11534  *			SD_CHAIN_DIRECT_PRIORITY
11535  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11536  *			initialization; may be NULL if none.
11537  *
11538  *     Context: Kernel thread context
11539  */
11540 
11541 static void
11542 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11543 	uchar_t chain_type, void *pktinfop)
11544 {
11545 	int index;
11546 
11547 	ASSERT(un != NULL);
11548 	ASSERT(bp != NULL);
11549 	ASSERT(xp != NULL);
11550 
11551 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11552 	    bp, chain_type);
11553 
11554 	xp->xb_un	= un;
11555 	xp->xb_pktp	= NULL;
11556 	xp->xb_pktinfo	= pktinfop;
11557 	xp->xb_private	= bp->b_private;
11558 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11559 
11560 	/*
11561 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11562 	 * upon the specified chain type to use.
11563 	 */
11564 	switch (chain_type) {
11565 	case SD_CHAIN_NULL:
11566 		/*
11567 		 * Fall thru to just use the values for the buf type, even
11568 		 * tho for the NULL chain these values will never be used.
11569 		 */
11570 		/* FALLTHRU */
11571 	case SD_CHAIN_BUFIO:
11572 		index = un->un_buf_chain_type;
11573 		if ((!un->un_f_has_removable_media) &&
11574 		    (un->un_tgt_blocksize != 0) &&
11575 		    (un->un_tgt_blocksize != DEV_BSIZE ||
11576 		    un->un_f_enable_rmw)) {
11577 			int secmask = 0, blknomask = 0;
11578 			if (un->un_f_enable_rmw) {
11579 				blknomask =
11580 				    (un->un_phy_blocksize / DEV_BSIZE) - 1;
11581 				secmask = un->un_phy_blocksize - 1;
11582 			} else {
11583 				blknomask =
11584 				    (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11585 				secmask = un->un_tgt_blocksize - 1;
11586 			}
11587 
11588 			if ((bp->b_lblkno & (blknomask)) ||
11589 			    (bp->b_bcount & (secmask))) {
11590 				if ((un->un_f_rmw_type !=
11591 				    SD_RMW_TYPE_RETURN_ERROR) ||
11592 				    un->un_f_enable_rmw) {
11593 					if (un->un_f_pm_is_enabled == FALSE)
11594 						index =
11595 						    SD_CHAIN_INFO_MSS_DSK_NO_PM;
11596 					else
11597 						index =
11598 						    SD_CHAIN_INFO_MSS_DISK;
11599 				}
11600 			}
11601 		}
11602 		break;
11603 	case SD_CHAIN_USCSI:
11604 		index = un->un_uscsi_chain_type;
11605 		break;
11606 	case SD_CHAIN_DIRECT:
11607 		index = un->un_direct_chain_type;
11608 		break;
11609 	case SD_CHAIN_DIRECT_PRIORITY:
11610 		index = un->un_priority_chain_type;
11611 		break;
11612 	default:
11613 		/* We're really broken if we ever get here... */
11614 		panic("sd_xbuf_init: illegal chain type!");
11615 		/*NOTREACHED*/
11616 	}
11617 
11618 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11619 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11620 
11621 	/*
11622 	 * It might be a bit easier to simply bzero the entire xbuf above,
11623 	 * but it turns out that since we init a fair number of members anyway,
11624 	 * we save a fair number cycles by doing explicit assignment of zero.
11625 	 */
11626 	xp->xb_pkt_flags	= 0;
11627 	xp->xb_dma_resid	= 0;
11628 	xp->xb_retry_count	= 0;
11629 	xp->xb_victim_retry_count = 0;
11630 	xp->xb_ua_retry_count	= 0;
11631 	xp->xb_nr_retry_count	= 0;
11632 	xp->xb_sense_bp		= NULL;
11633 	xp->xb_sense_status	= 0;
11634 	xp->xb_sense_state	= 0;
11635 	xp->xb_sense_resid	= 0;
11636 	xp->xb_ena		= 0;
11637 
11638 	bp->b_private	= xp;
11639 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11640 	bp->b_resid	= 0;
11641 	bp->av_forw	= NULL;
11642 	bp->av_back	= NULL;
11643 	bioerror(bp, 0);
11644 
11645 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11646 }
11647 
11648 
11649 /*
11650  *    Function: sd_uscsi_strategy
11651  *
11652  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11653  *
11654  *   Arguments: bp - buf struct ptr
11655  *
11656  * Return Code: Always returns 0
11657  *
11658  *     Context: Kernel thread context
11659  */
11660 
11661 static int
11662 sd_uscsi_strategy(struct buf *bp)
11663 {
11664 	struct sd_lun		*un;
11665 	struct sd_uscsi_info	*uip;
11666 	struct sd_xbuf		*xp;
11667 	uchar_t			chain_type;
11668 	uchar_t			cmd;
11669 
11670 	ASSERT(bp != NULL);
11671 
11672 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11673 	if (un == NULL) {
11674 		bioerror(bp, EIO);
11675 		bp->b_resid = bp->b_bcount;
11676 		biodone(bp);
11677 		return (0);
11678 	}
11679 
11680 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11681 
11682 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11683 
11684 	/*
11685 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11686 	 */
11687 	ASSERT(bp->b_private != NULL);
11688 	uip = (struct sd_uscsi_info *)bp->b_private;
11689 	cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11690 
11691 	mutex_enter(SD_MUTEX(un));
11692 	/*
11693 	 * atapi: Since we are running the CD for now in PIO mode we need to
11694 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11695 	 * the HBA's init_pkt routine.
11696 	 */
11697 	if (un->un_f_cfg_is_atapi == TRUE) {
11698 		mutex_exit(SD_MUTEX(un));
11699 		bp_mapin(bp);
11700 		mutex_enter(SD_MUTEX(un));
11701 	}
11702 	un->un_ncmds_in_driver++;
11703 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11704 	    un->un_ncmds_in_driver);
11705 
11706 	if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11707 	    (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11708 		un->un_f_sync_cache_required = TRUE;
11709 
11710 	mutex_exit(SD_MUTEX(un));
11711 
11712 	switch (uip->ui_flags) {
11713 	case SD_PATH_DIRECT:
11714 		chain_type = SD_CHAIN_DIRECT;
11715 		break;
11716 	case SD_PATH_DIRECT_PRIORITY:
11717 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11718 		break;
11719 	default:
11720 		chain_type = SD_CHAIN_USCSI;
11721 		break;
11722 	}
11723 
11724 	/*
11725 	 * We may allocate extra buf for external USCSI commands. If the
11726 	 * application asks for bigger than 20-byte sense data via USCSI,
11727 	 * SCSA layer will allocate 252 bytes sense buf for that command.
11728 	 */
11729 	if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11730 	    SENSE_LENGTH) {
11731 		xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11732 		    MAX_SENSE_LENGTH, KM_SLEEP);
11733 	} else {
11734 		xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11735 	}
11736 
11737 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11738 
11739 	/* Use the index obtained within xbuf_init */
11740 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11741 
11742 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11743 
11744 	return (0);
11745 }
11746 
11747 /*
11748  *    Function: sd_send_scsi_cmd
11749  *
11750  * Description: Runs a USCSI command for user (when called thru sdioctl),
11751  *		or for the driver
11752  *
11753  *   Arguments: dev - the dev_t for the device
11754  *		incmd - ptr to a valid uscsi_cmd struct
11755  *		flag - bit flag, indicating open settings, 32/64 bit type
11756  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11757  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11758  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11759  *			to use the USCSI "direct" chain and bypass the normal
11760  *			command waitq.
11761  *
11762  * Return Code: 0 -  successful completion of the given command
11763  *		EIO - scsi_uscsi_handle_command() failed
11764  *		ENXIO  - soft state not found for specified dev
11765  *		EINVAL
11766  *		EFAULT - copyin/copyout error
11767  *		return code of scsi_uscsi_handle_command():
11768  *			EIO
11769  *			ENXIO
11770  *			EACCES
11771  *
11772  *     Context: Waits for command to complete. Can sleep.
11773  */
11774 
11775 static int
11776 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11777 	enum uio_seg dataspace, int path_flag)
11778 {
11779 	struct sd_lun	*un;
11780 	sd_ssc_t	*ssc;
11781 	int		rval;
11782 
11783 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11784 	if (un == NULL) {
11785 		return (ENXIO);
11786 	}
11787 
11788 	/*
11789 	 * Using sd_ssc_send to handle uscsi cmd
11790 	 */
11791 	ssc = sd_ssc_init(un);
11792 	rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11793 	sd_ssc_fini(ssc);
11794 
11795 	return (rval);
11796 }
11797 
11798 /*
11799  *    Function: sd_ssc_init
11800  *
11801  * Description: Uscsi end-user call this function to initialize necessary
11802  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11803  *
11804  *              The return value of sd_send_scsi_cmd will be treated as a
11805  *              fault in various conditions. Even it is not Zero, some
11806  *              callers may ignore the return value. That is to say, we can
11807  *              not make an accurate assessment in sdintr, since if a
11808  *              command is failed in sdintr it does not mean the caller of
11809  *              sd_send_scsi_cmd will treat it as a real failure.
11810  *
11811  *              To avoid printing too many error logs for a failed uscsi
11812  *              packet that the caller may not treat it as a failure, the
11813  *              sd will keep silent for handling all uscsi commands.
11814  *
11815  *              During detach->attach and attach-open, for some types of
11816  *              problems, the driver should be providing information about
11817  *              the problem encountered. Device use USCSI_SILENT, which
11818  *              suppresses all driver information. The result is that no
11819  *              information about the problem is available. Being
11820  *              completely silent during this time is inappropriate. The
11821  *              driver needs a more selective filter than USCSI_SILENT, so
11822  *              that information related to faults is provided.
11823  *
11824  *              To make the accurate accessment, the caller  of
11825  *              sd_send_scsi_USCSI_CMD should take the ownership and
11826  *              get necessary information to print error messages.
11827  *
11828  *              If we want to print necessary info of uscsi command, we need to
11829  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11830  *              assessment. We use sd_ssc_init to alloc necessary
11831  *              structs for sending an uscsi command and we are also
11832  *              responsible for free the memory by calling
11833  *              sd_ssc_fini.
11834  *
11835  *              The calling secquences will look like:
11836  *              sd_ssc_init->
11837  *
11838  *                  ...
11839  *
11840  *                  sd_send_scsi_USCSI_CMD->
11841  *                      sd_ssc_send-> - - - sdintr
11842  *                  ...
11843  *
11844  *                  if we think the return value should be treated as a
11845  *                  failure, we make the accessment here and print out
11846  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11847  *
11848  *                  ...
11849  *
11850  *              sd_ssc_fini
11851  *
11852  *
11853  *   Arguments: un - pointer to driver soft state (unit) structure for this
11854  *                   target.
11855  *
11856  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11857  *                         uscsi_cmd and sd_uscsi_info.
11858  *                  NULL - if can not alloc memory for sd_ssc_t struct
11859  *
11860  *     Context: Kernel Thread.
11861  */
11862 static sd_ssc_t *
11863 sd_ssc_init(struct sd_lun *un)
11864 {
11865 	sd_ssc_t		*ssc;
11866 	struct uscsi_cmd	*ucmdp;
11867 	struct sd_uscsi_info	*uip;
11868 
11869 	ASSERT(un != NULL);
11870 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11871 
11872 	/*
11873 	 * Allocate sd_ssc_t structure
11874 	 */
11875 	ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11876 
11877 	/*
11878 	 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11879 	 */
11880 	ucmdp = scsi_uscsi_alloc();
11881 
11882 	/*
11883 	 * Allocate sd_uscsi_info structure
11884 	 */
11885 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11886 
11887 	ssc->ssc_uscsi_cmd = ucmdp;
11888 	ssc->ssc_uscsi_info = uip;
11889 	ssc->ssc_un = un;
11890 
11891 	return (ssc);
11892 }
11893 
11894 /*
11895  * Function: sd_ssc_fini
11896  *
11897  * Description: To free sd_ssc_t and it's hanging off
11898  *
11899  * Arguments: ssc - struct pointer of sd_ssc_t.
11900  */
11901 static void
11902 sd_ssc_fini(sd_ssc_t *ssc)
11903 {
11904 	scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11905 
11906 	if (ssc->ssc_uscsi_info != NULL) {
11907 		kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11908 		ssc->ssc_uscsi_info = NULL;
11909 	}
11910 
11911 	kmem_free(ssc, sizeof (sd_ssc_t));
11912 	ssc = NULL;
11913 }
11914 
11915 /*
11916  * Function: sd_ssc_send
11917  *
11918  * Description: Runs a USCSI command for user when called through sdioctl,
11919  *              or for the driver.
11920  *
11921  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11922  *                    sd_uscsi_info in.
11923  *		incmd - ptr to a valid uscsi_cmd struct
11924  *		flag - bit flag, indicating open settings, 32/64 bit type
11925  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11926  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11927  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11928  *			to use the USCSI "direct" chain and bypass the normal
11929  *			command waitq.
11930  *
11931  * Return Code: 0 -  successful completion of the given command
11932  *		EIO - scsi_uscsi_handle_command() failed
11933  *		ENXIO  - soft state not found for specified dev
11934  *		ECANCELED - command cancelled due to low power
11935  *		EINVAL
11936  *		EFAULT - copyin/copyout error
11937  *		return code of scsi_uscsi_handle_command():
11938  *			EIO
11939  *			ENXIO
11940  *			EACCES
11941  *
11942  *     Context: Kernel Thread;
11943  *              Waits for command to complete. Can sleep.
11944  */
11945 static int
11946 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11947 	enum uio_seg dataspace, int path_flag)
11948 {
11949 	struct sd_uscsi_info	*uip;
11950 	struct uscsi_cmd	*uscmd;
11951 	struct sd_lun		*un;
11952 	dev_t			dev;
11953 
11954 	int	format = 0;
11955 	int	rval;
11956 
11957 	ASSERT(ssc != NULL);
11958 	un = ssc->ssc_un;
11959 	ASSERT(un != NULL);
11960 	uscmd = ssc->ssc_uscsi_cmd;
11961 	ASSERT(uscmd != NULL);
11962 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11963 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
11964 		/*
11965 		 * If enter here, it indicates that the previous uscsi
11966 		 * command has not been processed by sd_ssc_assessment.
11967 		 * This is violating our rules of FMA telemetry processing.
11968 		 * We should print out this message and the last undisposed
11969 		 * uscsi command.
11970 		 */
11971 		if (uscmd->uscsi_cdb != NULL) {
11972 			SD_INFO(SD_LOG_SDTEST, un,
11973 			    "sd_ssc_send is missing the alternative "
11974 			    "sd_ssc_assessment when running command 0x%x.\n",
11975 			    uscmd->uscsi_cdb[0]);
11976 		}
11977 		/*
11978 		 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
11979 		 * the initial status.
11980 		 */
11981 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
11982 	}
11983 
11984 	/*
11985 	 * We need to make sure sd_ssc_send will have sd_ssc_assessment
11986 	 * followed to avoid missing FMA telemetries.
11987 	 */
11988 	ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
11989 
11990 	/*
11991 	 * if USCSI_PMFAILFAST is set and un is in low power, fail the
11992 	 * command immediately.
11993 	 */
11994 	mutex_enter(SD_MUTEX(un));
11995 	mutex_enter(&un->un_pm_mutex);
11996 	if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
11997 	    SD_DEVICE_IS_IN_LOW_POWER(un)) {
11998 		SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
11999 		    "un:0x%p is in low power\n", un);
12000 		mutex_exit(&un->un_pm_mutex);
12001 		mutex_exit(SD_MUTEX(un));
12002 		return (ECANCELED);
12003 	}
12004 	mutex_exit(&un->un_pm_mutex);
12005 	mutex_exit(SD_MUTEX(un));
12006 
12007 #ifdef SDDEBUG
12008 	switch (dataspace) {
12009 	case UIO_USERSPACE:
12010 		SD_TRACE(SD_LOG_IO, un,
12011 		    "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12012 		break;
12013 	case UIO_SYSSPACE:
12014 		SD_TRACE(SD_LOG_IO, un,
12015 		    "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12016 		break;
12017 	default:
12018 		SD_TRACE(SD_LOG_IO, un,
12019 		    "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12020 		break;
12021 	}
12022 #endif
12023 
12024 	rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12025 	    SD_ADDRESS(un), &uscmd);
12026 	if (rval != 0) {
12027 		SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12028 		    "scsi_uscsi_alloc_and_copyin failed\n", un);
12029 		return (rval);
12030 	}
12031 
12032 	if ((uscmd->uscsi_cdb != NULL) &&
12033 	    (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12034 		mutex_enter(SD_MUTEX(un));
12035 		un->un_f_format_in_progress = TRUE;
12036 		mutex_exit(SD_MUTEX(un));
12037 		format = 1;
12038 	}
12039 
12040 	/*
12041 	 * Allocate an sd_uscsi_info struct and fill it with the info
12042 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12043 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12044 	 * since we allocate the buf here in this function, we do not
12045 	 * need to preserve the prior contents of b_private.
12046 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12047 	 */
12048 	uip = ssc->ssc_uscsi_info;
12049 	uip->ui_flags = path_flag;
12050 	uip->ui_cmdp = uscmd;
12051 
12052 	/*
12053 	 * Commands sent with priority are intended for error recovery
12054 	 * situations, and do not have retries performed.
12055 	 */
12056 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12057 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12058 	}
12059 	uscmd->uscsi_flags &= ~USCSI_NOINTR;
12060 
12061 	dev = SD_GET_DEV(un);
12062 	rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12063 	    sd_uscsi_strategy, NULL, uip);
12064 
12065 	/*
12066 	 * mark ssc_flags right after handle_cmd to make sure
12067 	 * the uscsi has been sent
12068 	 */
12069 	ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12070 
12071 #ifdef SDDEBUG
12072 	SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12073 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12074 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12075 	if (uscmd->uscsi_bufaddr != NULL) {
12076 		SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12077 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12078 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12079 		if (dataspace == UIO_SYSSPACE) {
12080 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12081 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12082 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12083 		}
12084 	}
12085 #endif
12086 
12087 	if (format == 1) {
12088 		mutex_enter(SD_MUTEX(un));
12089 		un->un_f_format_in_progress = FALSE;
12090 		mutex_exit(SD_MUTEX(un));
12091 	}
12092 
12093 	(void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12094 
12095 	return (rval);
12096 }
12097 
12098 /*
12099  *     Function: sd_ssc_print
12100  *
12101  * Description: Print information available to the console.
12102  *
12103  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12104  *                    sd_uscsi_info in.
12105  *            sd_severity - log level.
12106  *     Context: Kernel thread or interrupt context.
12107  */
12108 static void
12109 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12110 {
12111 	struct uscsi_cmd	*ucmdp;
12112 	struct scsi_device	*devp;
12113 	dev_info_t 		*devinfo;
12114 	uchar_t			*sensep;
12115 	int			senlen;
12116 	union scsi_cdb		*cdbp;
12117 	uchar_t			com;
12118 	extern struct scsi_key_strings scsi_cmds[];
12119 
12120 	ASSERT(ssc != NULL);
12121 	ASSERT(ssc->ssc_un != NULL);
12122 
12123 	if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12124 		return;
12125 	ucmdp = ssc->ssc_uscsi_cmd;
12126 	devp = SD_SCSI_DEVP(ssc->ssc_un);
12127 	devinfo = SD_DEVINFO(ssc->ssc_un);
12128 	ASSERT(ucmdp != NULL);
12129 	ASSERT(devp != NULL);
12130 	ASSERT(devinfo != NULL);
12131 	sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12132 	senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12133 	cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12134 
12135 	/* In certain case (like DOORLOCK), the cdb could be NULL. */
12136 	if (cdbp == NULL)
12137 		return;
12138 	/* We don't print log if no sense data available. */
12139 	if (senlen == 0)
12140 		sensep = NULL;
12141 	com = cdbp->scc_cmd;
12142 	scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12143 	    scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12144 }
12145 
12146 /*
12147  *     Function: sd_ssc_assessment
12148  *
12149  * Description: We use this function to make an assessment at the point
12150  *              where SD driver may encounter a potential error.
12151  *
12152  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12153  *                  sd_uscsi_info in.
12154  *            tp_assess - a hint of strategy for ereport posting.
12155  *            Possible values of tp_assess include:
12156  *                SD_FMT_IGNORE - we don't post any ereport because we're
12157  *                sure that it is ok to ignore the underlying problems.
12158  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12159  *                but it might be not correct to ignore the underlying hardware
12160  *                error.
12161  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12162  *                payload driver-assessment of value "fail" or
12163  *                "fatal"(depending on what information we have here). This
12164  *                assessment value is usually set when SD driver think there
12165  *                is a potential error occurred(Typically, when return value
12166  *                of the SCSI command is EIO).
12167  *                SD_FMT_STANDARD - we will post an ereport with the payload
12168  *                driver-assessment of value "info". This assessment value is
12169  *                set when the SCSI command returned successfully and with
12170  *                sense data sent back.
12171  *
12172  *     Context: Kernel thread.
12173  */
12174 static void
12175 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12176 {
12177 	int senlen = 0;
12178 	struct uscsi_cmd *ucmdp = NULL;
12179 	struct sd_lun *un;
12180 
12181 	ASSERT(ssc != NULL);
12182 	un = ssc->ssc_un;
12183 	ASSERT(un != NULL);
12184 	ucmdp = ssc->ssc_uscsi_cmd;
12185 	ASSERT(ucmdp != NULL);
12186 
12187 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12188 		ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12189 	} else {
12190 		/*
12191 		 * If enter here, it indicates that we have a wrong
12192 		 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12193 		 * both of which should be called in a pair in case of
12194 		 * loss of FMA telemetries.
12195 		 */
12196 		if (ucmdp->uscsi_cdb != NULL) {
12197 			SD_INFO(SD_LOG_SDTEST, un,
12198 			    "sd_ssc_assessment is missing the "
12199 			    "alternative sd_ssc_send when running 0x%x, "
12200 			    "or there are superfluous sd_ssc_assessment for "
12201 			    "the same sd_ssc_send.\n",
12202 			    ucmdp->uscsi_cdb[0]);
12203 		}
12204 		/*
12205 		 * Set the ssc_flags to the initial value to avoid passing
12206 		 * down dirty flags to the following sd_ssc_send function.
12207 		 */
12208 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12209 		return;
12210 	}
12211 
12212 	/*
12213 	 * Only handle an issued command which is waiting for assessment.
12214 	 * A command which is not issued will not have
12215 	 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12216 	 */
12217 	if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12218 		sd_ssc_print(ssc, SCSI_ERR_INFO);
12219 		return;
12220 	} else {
12221 		/*
12222 		 * For an issued command, we should clear this flag in
12223 		 * order to make the sd_ssc_t structure be used off
12224 		 * multiple uscsi commands.
12225 		 */
12226 		ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12227 	}
12228 
12229 	/*
12230 	 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12231 	 * commands here. And we should clear the ssc_flags before return.
12232 	 */
12233 	if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12234 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12235 		return;
12236 	}
12237 
12238 	switch (tp_assess) {
12239 	case SD_FMT_IGNORE:
12240 	case SD_FMT_IGNORE_COMPROMISE:
12241 		break;
12242 	case SD_FMT_STATUS_CHECK:
12243 		/*
12244 		 * For a failed command(including the succeeded command
12245 		 * with invalid data sent back).
12246 		 */
12247 		sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12248 		break;
12249 	case SD_FMT_STANDARD:
12250 		/*
12251 		 * Always for the succeeded commands probably with sense
12252 		 * data sent back.
12253 		 * Limitation:
12254 		 *	We can only handle a succeeded command with sense
12255 		 *	data sent back when auto-request-sense is enabled.
12256 		 */
12257 		senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12258 		    ssc->ssc_uscsi_cmd->uscsi_rqresid;
12259 		if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12260 		    (un->un_f_arq_enabled == TRUE) &&
12261 		    senlen > 0 &&
12262 		    ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12263 			sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12264 		}
12265 		break;
12266 	default:
12267 		/*
12268 		 * Should not have other type of assessment.
12269 		 */
12270 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12271 		    "sd_ssc_assessment got wrong "
12272 		    "sd_type_assessment %d.\n", tp_assess);
12273 		break;
12274 	}
12275 	/*
12276 	 * Clear up the ssc_flags before return.
12277 	 */
12278 	ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12279 }
12280 
12281 /*
12282  *    Function: sd_ssc_post
12283  *
12284  * Description: 1. read the driver property to get fm-scsi-log flag.
12285  *              2. print log if fm_log_capable is non-zero.
12286  *              3. call sd_ssc_ereport_post to post ereport if possible.
12287  *
12288  *    Context: May be called from kernel thread or interrupt context.
12289  */
12290 static void
12291 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12292 {
12293 	struct sd_lun	*un;
12294 	int		sd_severity;
12295 
12296 	ASSERT(ssc != NULL);
12297 	un = ssc->ssc_un;
12298 	ASSERT(un != NULL);
12299 
12300 	/*
12301 	 * We may enter here from sd_ssc_assessment(for USCSI command) or
12302 	 * by directly called from sdintr context.
12303 	 * We don't handle a non-disk drive(CD-ROM, removable media).
12304 	 * Clear the ssc_flags before return in case we've set
12305 	 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12306 	 * driver.
12307 	 */
12308 	if (ISCD(un) || un->un_f_has_removable_media) {
12309 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12310 		return;
12311 	}
12312 
12313 	switch (sd_assess) {
12314 		case SD_FM_DRV_FATAL:
12315 			sd_severity = SCSI_ERR_FATAL;
12316 			break;
12317 		case SD_FM_DRV_RECOVERY:
12318 			sd_severity = SCSI_ERR_RECOVERED;
12319 			break;
12320 		case SD_FM_DRV_RETRY:
12321 			sd_severity = SCSI_ERR_RETRYABLE;
12322 			break;
12323 		case SD_FM_DRV_NOTICE:
12324 			sd_severity = SCSI_ERR_INFO;
12325 			break;
12326 		default:
12327 			sd_severity = SCSI_ERR_UNKNOWN;
12328 	}
12329 	/* print log */
12330 	sd_ssc_print(ssc, sd_severity);
12331 
12332 	/* always post ereport */
12333 	sd_ssc_ereport_post(ssc, sd_assess);
12334 }
12335 
12336 /*
12337  *    Function: sd_ssc_set_info
12338  *
12339  * Description: Mark ssc_flags and set ssc_info which would be the
12340  *              payload of uderr ereport. This function will cause
12341  *              sd_ssc_ereport_post to post uderr ereport only.
12342  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12343  *              the function will also call SD_ERROR or scsi_log for a
12344  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12345  *
12346  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12347  *                  sd_uscsi_info in.
12348  *            ssc_flags - indicate the sub-category of a uderr.
12349  *            comp - this argument is meaningful only when
12350  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12351  *                   values include:
12352  *                   > 0, SD_ERROR is used with comp as the driver logging
12353  *                   component;
12354  *                   = 0, scsi-log is used to log error telemetries;
12355  *                   < 0, no log available for this telemetry.
12356  *
12357  *    Context: Kernel thread or interrupt context
12358  */
12359 static void
12360 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12361 {
12362 	va_list	ap;
12363 
12364 	ASSERT(ssc != NULL);
12365 	ASSERT(ssc->ssc_un != NULL);
12366 
12367 	ssc->ssc_flags |= ssc_flags;
12368 	va_start(ap, fmt);
12369 	(void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12370 	va_end(ap);
12371 
12372 	/*
12373 	 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12374 	 * with invalid data sent back. For non-uscsi command, the
12375 	 * following code will be bypassed.
12376 	 */
12377 	if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12378 		if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12379 			/*
12380 			 * If the error belong to certain component and we
12381 			 * do not want it to show up on the console, we
12382 			 * will use SD_ERROR, otherwise scsi_log is
12383 			 * preferred.
12384 			 */
12385 			if (comp > 0) {
12386 				SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12387 			} else if (comp == 0) {
12388 				scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12389 				    CE_WARN, ssc->ssc_info);
12390 			}
12391 		}
12392 	}
12393 }
12394 
12395 /*
12396  *    Function: sd_buf_iodone
12397  *
12398  * Description: Frees the sd_xbuf & returns the buf to its originator.
12399  *
12400  *     Context: May be called from interrupt context.
12401  */
12402 /* ARGSUSED */
12403 static void
12404 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12405 {
12406 	struct sd_xbuf *xp;
12407 
12408 	ASSERT(un != NULL);
12409 	ASSERT(bp != NULL);
12410 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12411 
12412 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12413 
12414 	xp = SD_GET_XBUF(bp);
12415 	ASSERT(xp != NULL);
12416 
12417 	/* xbuf is gone after this */
12418 	if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12419 		mutex_enter(SD_MUTEX(un));
12420 
12421 		/*
12422 		 * Grab time when the cmd completed.
12423 		 * This is used for determining if the system has been
12424 		 * idle long enough to make it idle to the PM framework.
12425 		 * This is for lowering the overhead, and therefore improving
12426 		 * performance per I/O operation.
12427 		 */
12428 		un->un_pm_idle_time = ddi_get_time();
12429 
12430 		un->un_ncmds_in_driver--;
12431 		ASSERT(un->un_ncmds_in_driver >= 0);
12432 		SD_INFO(SD_LOG_IO, un,
12433 		    "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12434 		    un->un_ncmds_in_driver);
12435 
12436 		mutex_exit(SD_MUTEX(un));
12437 	}
12438 
12439 	biodone(bp);				/* bp is gone after this */
12440 
12441 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12442 }
12443 
12444 
12445 /*
12446  *    Function: sd_uscsi_iodone
12447  *
12448  * Description: Frees the sd_xbuf & returns the buf to its originator.
12449  *
12450  *     Context: May be called from interrupt context.
12451  */
12452 /* ARGSUSED */
12453 static void
12454 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12455 {
12456 	struct sd_xbuf *xp;
12457 
12458 	ASSERT(un != NULL);
12459 	ASSERT(bp != NULL);
12460 
12461 	xp = SD_GET_XBUF(bp);
12462 	ASSERT(xp != NULL);
12463 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12464 
12465 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12466 
12467 	bp->b_private = xp->xb_private;
12468 
12469 	mutex_enter(SD_MUTEX(un));
12470 
12471 	/*
12472 	 * Grab time when the cmd completed.
12473 	 * This is used for determining if the system has been
12474 	 * idle long enough to make it idle to the PM framework.
12475 	 * This is for lowering the overhead, and therefore improving
12476 	 * performance per I/O operation.
12477 	 */
12478 	un->un_pm_idle_time = ddi_get_time();
12479 
12480 	un->un_ncmds_in_driver--;
12481 	ASSERT(un->un_ncmds_in_driver >= 0);
12482 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12483 	    un->un_ncmds_in_driver);
12484 
12485 	mutex_exit(SD_MUTEX(un));
12486 
12487 	if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12488 	    SENSE_LENGTH) {
12489 		kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12490 		    MAX_SENSE_LENGTH);
12491 	} else {
12492 		kmem_free(xp, sizeof (struct sd_xbuf));
12493 	}
12494 
12495 	biodone(bp);
12496 
12497 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12498 }
12499 
12500 
12501 /*
12502  *    Function: sd_mapblockaddr_iostart
12503  *
12504  * Description: Verify request lies within the partition limits for
12505  *		the indicated minor device.  Issue "overrun" buf if
12506  *		request would exceed partition range.  Converts
12507  *		partition-relative block address to absolute.
12508  *
12509  *              Upon exit of this function:
12510  *              1.I/O is aligned
12511  *                 xp->xb_blkno represents the absolute sector address
12512  *              2.I/O is misaligned
12513  *                 xp->xb_blkno represents the absolute logical block address
12514  *                 based on DEV_BSIZE. The logical block address will be
12515  *                 converted to physical sector address in sd_mapblocksize_\
12516  *                 iostart.
12517  *              3.I/O is misaligned but is aligned in "overrun" buf
12518  *                 xp->xb_blkno represents the absolute logical block address
12519  *                 based on DEV_BSIZE. The logical block address will be
12520  *                 converted to physical sector address in sd_mapblocksize_\
12521  *                 iostart. But no RMW will be issued in this case.
12522  *
12523  *     Context: Can sleep
12524  *
12525  *      Issues: This follows what the old code did, in terms of accessing
12526  *		some of the partition info in the unit struct without holding
12527  *		the mutext.  This is a general issue, if the partition info
12528  *		can be altered while IO is in progress... as soon as we send
12529  *		a buf, its partitioning can be invalid before it gets to the
12530  *		device.  Probably the right fix is to move partitioning out
12531  *		of the driver entirely.
12532  */
12533 
12534 static void
12535 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12536 {
12537 	diskaddr_t	nblocks;	/* #blocks in the given partition */
12538 	daddr_t	blocknum;	/* Block number specified by the buf */
12539 	size_t	requested_nblocks;
12540 	size_t	available_nblocks;
12541 	int	partition;
12542 	diskaddr_t	partition_offset;
12543 	struct sd_xbuf *xp;
12544 	int secmask = 0, blknomask = 0;
12545 	ushort_t is_aligned = TRUE;
12546 
12547 	ASSERT(un != NULL);
12548 	ASSERT(bp != NULL);
12549 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12550 
12551 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12552 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12553 
12554 	xp = SD_GET_XBUF(bp);
12555 	ASSERT(xp != NULL);
12556 
12557 	/*
12558 	 * If the geometry is not indicated as valid, attempt to access
12559 	 * the unit & verify the geometry/label. This can be the case for
12560 	 * removable-media devices, of if the device was opened in
12561 	 * NDELAY/NONBLOCK mode.
12562 	 */
12563 	partition = SDPART(bp->b_edev);
12564 
12565 	if (!SD_IS_VALID_LABEL(un)) {
12566 		sd_ssc_t *ssc;
12567 		/*
12568 		 * Initialize sd_ssc_t for internal uscsi commands
12569 		 * In case of potential porformance issue, we need
12570 		 * to alloc memory only if there is invalid label
12571 		 */
12572 		ssc = sd_ssc_init(un);
12573 
12574 		if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12575 			/*
12576 			 * For removable devices it is possible to start an
12577 			 * I/O without a media by opening the device in nodelay
12578 			 * mode. Also for writable CDs there can be many
12579 			 * scenarios where there is no geometry yet but volume
12580 			 * manager is trying to issue a read() just because
12581 			 * it can see TOC on the CD. So do not print a message
12582 			 * for removables.
12583 			 */
12584 			if (!un->un_f_has_removable_media) {
12585 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12586 				    "i/o to invalid geometry\n");
12587 			}
12588 			bioerror(bp, EIO);
12589 			bp->b_resid = bp->b_bcount;
12590 			SD_BEGIN_IODONE(index, un, bp);
12591 
12592 			sd_ssc_fini(ssc);
12593 			return;
12594 		}
12595 		sd_ssc_fini(ssc);
12596 	}
12597 
12598 	nblocks = 0;
12599 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
12600 	    &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12601 
12602 	if (un->un_f_enable_rmw) {
12603 		blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12604 		secmask = un->un_phy_blocksize - 1;
12605 	} else {
12606 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12607 		secmask = un->un_tgt_blocksize - 1;
12608 	}
12609 
12610 	if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12611 		is_aligned = FALSE;
12612 	}
12613 
12614 	if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12615 		/*
12616 		 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12617 		 * Convert the logical block number to target's physical sector
12618 		 * number.
12619 		 */
12620 		if (is_aligned) {
12621 			xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12622 		} else {
12623 			switch (un->un_f_rmw_type) {
12624 			case SD_RMW_TYPE_RETURN_ERROR:
12625 				if (un->un_f_enable_rmw)
12626 					break;
12627 				else {
12628 					bp->b_flags |= B_ERROR;
12629 					goto error_exit;
12630 				}
12631 
12632 			case SD_RMW_TYPE_DEFAULT:
12633 				mutex_enter(SD_MUTEX(un));
12634 				if (!un->un_f_enable_rmw &&
12635 				    un->un_rmw_msg_timeid == NULL) {
12636 					scsi_log(SD_DEVINFO(un), sd_label,
12637 					    CE_WARN, "I/O request is not "
12638 					    "aligned with %d disk sector size. "
12639 					    "It is handled through Read Modify "
12640 					    "Write but the performance is "
12641 					    "very low.\n",
12642 					    un->un_tgt_blocksize);
12643 					un->un_rmw_msg_timeid =
12644 					    timeout(sd_rmw_msg_print_handler,
12645 					    un, SD_RMW_MSG_PRINT_TIMEOUT);
12646 				} else {
12647 					un->un_rmw_incre_count ++;
12648 				}
12649 				mutex_exit(SD_MUTEX(un));
12650 				break;
12651 
12652 			case SD_RMW_TYPE_NO_WARNING:
12653 			default:
12654 				break;
12655 			}
12656 
12657 			nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12658 			partition_offset = SD_TGT2SYSBLOCK(un,
12659 			    partition_offset);
12660 		}
12661 	}
12662 
12663 	/*
12664 	 * blocknum is the starting block number of the request. At this
12665 	 * point it is still relative to the start of the minor device.
12666 	 */
12667 	blocknum = xp->xb_blkno;
12668 
12669 	/*
12670 	 * Legacy: If the starting block number is one past the last block
12671 	 * in the partition, do not set B_ERROR in the buf.
12672 	 */
12673 	if (blocknum == nblocks)  {
12674 		goto error_exit;
12675 	}
12676 
12677 	/*
12678 	 * Confirm that the first block of the request lies within the
12679 	 * partition limits. Also the requested number of bytes must be
12680 	 * a multiple of the system block size.
12681 	 */
12682 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12683 	    ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12684 		bp->b_flags |= B_ERROR;
12685 		goto error_exit;
12686 	}
12687 
12688 	/*
12689 	 * If the requsted # blocks exceeds the available # blocks, that
12690 	 * is an overrun of the partition.
12691 	 */
12692 	if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12693 		requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12694 	} else {
12695 		requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12696 	}
12697 
12698 	available_nblocks = (size_t)(nblocks - blocknum);
12699 	ASSERT(nblocks >= blocknum);
12700 
12701 	if (requested_nblocks > available_nblocks) {
12702 		size_t resid;
12703 
12704 		/*
12705 		 * Allocate an "overrun" buf to allow the request to proceed
12706 		 * for the amount of space available in the partition. The
12707 		 * amount not transferred will be added into the b_resid
12708 		 * when the operation is complete. The overrun buf
12709 		 * replaces the original buf here, and the original buf
12710 		 * is saved inside the overrun buf, for later use.
12711 		 */
12712 		if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12713 			resid = SD_TGTBLOCKS2BYTES(un,
12714 			    (offset_t)(requested_nblocks - available_nblocks));
12715 		} else {
12716 			resid = SD_SYSBLOCKS2BYTES(
12717 			    (offset_t)(requested_nblocks - available_nblocks));
12718 		}
12719 
12720 		size_t count = bp->b_bcount - resid;
12721 		/*
12722 		 * Note: count is an unsigned entity thus it'll NEVER
12723 		 * be less than 0 so ASSERT the original values are
12724 		 * correct.
12725 		 */
12726 		ASSERT(bp->b_bcount >= resid);
12727 
12728 		bp = sd_bioclone_alloc(bp, count, blocknum,
12729 		    (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12730 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12731 		ASSERT(xp != NULL);
12732 	}
12733 
12734 	/* At this point there should be no residual for this buf. */
12735 	ASSERT(bp->b_resid == 0);
12736 
12737 	/* Convert the block number to an absolute address. */
12738 	xp->xb_blkno += partition_offset;
12739 
12740 	SD_NEXT_IOSTART(index, un, bp);
12741 
12742 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12743 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12744 
12745 	return;
12746 
12747 error_exit:
12748 	bp->b_resid = bp->b_bcount;
12749 	SD_BEGIN_IODONE(index, un, bp);
12750 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12751 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12752 }
12753 
12754 
12755 /*
12756  *    Function: sd_mapblockaddr_iodone
12757  *
12758  * Description: Completion-side processing for partition management.
12759  *
12760  *     Context: May be called under interrupt context
12761  */
12762 
12763 static void
12764 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12765 {
12766 	/* int	partition; */	/* Not used, see below. */
12767 	ASSERT(un != NULL);
12768 	ASSERT(bp != NULL);
12769 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12770 
12771 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12772 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12773 
12774 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12775 		/*
12776 		 * We have an "overrun" buf to deal with...
12777 		 */
12778 		struct sd_xbuf	*xp;
12779 		struct buf	*obp;	/* ptr to the original buf */
12780 
12781 		xp = SD_GET_XBUF(bp);
12782 		ASSERT(xp != NULL);
12783 
12784 		/* Retrieve the pointer to the original buf */
12785 		obp = (struct buf *)xp->xb_private;
12786 		ASSERT(obp != NULL);
12787 
12788 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12789 		bioerror(obp, bp->b_error);
12790 
12791 		sd_bioclone_free(bp);
12792 
12793 		/*
12794 		 * Get back the original buf.
12795 		 * Note that since the restoration of xb_blkno below
12796 		 * was removed, the sd_xbuf is not needed.
12797 		 */
12798 		bp = obp;
12799 		/*
12800 		 * xp = SD_GET_XBUF(bp);
12801 		 * ASSERT(xp != NULL);
12802 		 */
12803 	}
12804 
12805 	/*
12806 	 * Convert sd->xb_blkno back to a minor-device relative value.
12807 	 * Note: this has been commented out, as it is not needed in the
12808 	 * current implementation of the driver (ie, since this function
12809 	 * is at the top of the layering chains, so the info will be
12810 	 * discarded) and it is in the "hot" IO path.
12811 	 *
12812 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12813 	 * xp->xb_blkno -= un->un_offset[partition];
12814 	 */
12815 
12816 	SD_NEXT_IODONE(index, un, bp);
12817 
12818 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12819 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12820 }
12821 
12822 
12823 /*
12824  *    Function: sd_mapblocksize_iostart
12825  *
12826  * Description: Convert between system block size (un->un_sys_blocksize)
12827  *		and target block size (un->un_tgt_blocksize).
12828  *
12829  *     Context: Can sleep to allocate resources.
12830  *
12831  * Assumptions: A higher layer has already performed any partition validation,
12832  *		and converted the xp->xb_blkno to an absolute value relative
12833  *		to the start of the device.
12834  *
12835  *		It is also assumed that the higher layer has implemented
12836  *		an "overrun" mechanism for the case where the request would
12837  *		read/write beyond the end of a partition.  In this case we
12838  *		assume (and ASSERT) that bp->b_resid == 0.
12839  *
12840  *		Note: The implementation for this routine assumes the target
12841  *		block size remains constant between allocation and transport.
12842  */
12843 
12844 static void
12845 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12846 {
12847 	struct sd_mapblocksize_info	*bsp;
12848 	struct sd_xbuf			*xp;
12849 	offset_t first_byte;
12850 	daddr_t	start_block, end_block;
12851 	daddr_t	request_bytes;
12852 	ushort_t is_aligned = FALSE;
12853 
12854 	ASSERT(un != NULL);
12855 	ASSERT(bp != NULL);
12856 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12857 	ASSERT(bp->b_resid == 0);
12858 
12859 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12860 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12861 
12862 	/*
12863 	 * For a non-writable CD, a write request is an error
12864 	 */
12865 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12866 	    (un->un_f_mmc_writable_media == FALSE)) {
12867 		bioerror(bp, EIO);
12868 		bp->b_resid = bp->b_bcount;
12869 		SD_BEGIN_IODONE(index, un, bp);
12870 		return;
12871 	}
12872 
12873 	/*
12874 	 * We do not need a shadow buf if the device is using
12875 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12876 	 * In this case there is no layer-private data block allocated.
12877 	 */
12878 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12879 	    (bp->b_bcount == 0)) {
12880 		goto done;
12881 	}
12882 
12883 #if defined(__i386) || defined(__amd64)
12884 	/* We do not support non-block-aligned transfers for ROD devices */
12885 	ASSERT(!ISROD(un));
12886 #endif
12887 
12888 	xp = SD_GET_XBUF(bp);
12889 	ASSERT(xp != NULL);
12890 
12891 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12892 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12893 	    un->un_tgt_blocksize, DEV_BSIZE);
12894 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12895 	    "request start block:0x%x\n", xp->xb_blkno);
12896 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12897 	    "request len:0x%x\n", bp->b_bcount);
12898 
12899 	/*
12900 	 * Allocate the layer-private data area for the mapblocksize layer.
12901 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12902 	 * struct to store the pointer to their layer-private data block, but
12903 	 * each layer also has the responsibility of restoring the prior
12904 	 * contents of xb_private before returning the buf/xbuf to the
12905 	 * higher layer that sent it.
12906 	 *
12907 	 * Here we save the prior contents of xp->xb_private into the
12908 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12909 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12910 	 * the layer-private area and returning the buf/xbuf to the layer
12911 	 * that sent it.
12912 	 *
12913 	 * Note that here we use kmem_zalloc for the allocation as there are
12914 	 * parts of the mapblocksize code that expect certain fields to be
12915 	 * zero unless explicitly set to a required value.
12916 	 */
12917 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12918 	bsp->mbs_oprivate = xp->xb_private;
12919 	xp->xb_private = bsp;
12920 
12921 	/*
12922 	 * This treats the data on the disk (target) as an array of bytes.
12923 	 * first_byte is the byte offset, from the beginning of the device,
12924 	 * to the location of the request. This is converted from a
12925 	 * un->un_sys_blocksize block address to a byte offset, and then back
12926 	 * to a block address based upon a un->un_tgt_blocksize block size.
12927 	 *
12928 	 * xp->xb_blkno should be absolute upon entry into this function,
12929 	 * but, but it is based upon partitions that use the "system"
12930 	 * block size. It must be adjusted to reflect the block size of
12931 	 * the target.
12932 	 *
12933 	 * Note that end_block is actually the block that follows the last
12934 	 * block of the request, but that's what is needed for the computation.
12935 	 */
12936 	first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12937 	if (un->un_f_enable_rmw) {
12938 		start_block = xp->xb_blkno =
12939 		    (first_byte / un->un_phy_blocksize) *
12940 		    (un->un_phy_blocksize / DEV_BSIZE);
12941 		end_block   = ((first_byte + bp->b_bcount +
12942 		    un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12943 		    (un->un_phy_blocksize / DEV_BSIZE);
12944 	} else {
12945 		start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12946 		end_block   = (first_byte + bp->b_bcount +
12947 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12948 	}
12949 
12950 	/* request_bytes is rounded up to a multiple of the target block size */
12951 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12952 
12953 	/*
12954 	 * See if the starting address of the request and the request
12955 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12956 	 * then we do not need to allocate a shadow buf to handle the request.
12957 	 */
12958 	if (un->un_f_enable_rmw) {
12959 		if (((first_byte % un->un_phy_blocksize) == 0) &&
12960 		    ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
12961 			is_aligned = TRUE;
12962 		}
12963 	} else {
12964 		if (((first_byte % un->un_tgt_blocksize) == 0) &&
12965 		    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12966 			is_aligned = TRUE;
12967 		}
12968 	}
12969 
12970 	if ((bp->b_flags & B_READ) == 0) {
12971 		/*
12972 		 * Lock the range for a write operation. An aligned request is
12973 		 * considered a simple write; otherwise the request must be a
12974 		 * read-modify-write.
12975 		 */
12976 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12977 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12978 	}
12979 
12980 	/*
12981 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12982 	 * where the READ command is generated for a read-modify-write. (The
12983 	 * write phase is deferred until after the read completes.)
12984 	 */
12985 	if (is_aligned == FALSE) {
12986 
12987 		struct sd_mapblocksize_info	*shadow_bsp;
12988 		struct sd_xbuf	*shadow_xp;
12989 		struct buf	*shadow_bp;
12990 
12991 		/*
12992 		 * Allocate the shadow buf and it associated xbuf. Note that
12993 		 * after this call the xb_blkno value in both the original
12994 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12995 		 * same: absolute relative to the start of the device, and
12996 		 * adjusted for the target block size. The b_blkno in the
12997 		 * shadow buf will also be set to this value. We should never
12998 		 * change b_blkno in the original bp however.
12999 		 *
13000 		 * Note also that the shadow buf will always need to be a
13001 		 * READ command, regardless of whether the incoming command
13002 		 * is a READ or a WRITE.
13003 		 */
13004 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13005 		    xp->xb_blkno,
13006 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
13007 
13008 		shadow_xp = SD_GET_XBUF(shadow_bp);
13009 
13010 		/*
13011 		 * Allocate the layer-private data for the shadow buf.
13012 		 * (No need to preserve xb_private in the shadow xbuf.)
13013 		 */
13014 		shadow_xp->xb_private = shadow_bsp =
13015 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13016 
13017 		/*
13018 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13019 		 * to figure out where the start of the user data is (based upon
13020 		 * the system block size) in the data returned by the READ
13021 		 * command (which will be based upon the target blocksize). Note
13022 		 * that this is only really used if the request is unaligned.
13023 		 */
13024 		if (un->un_f_enable_rmw) {
13025 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13026 			    ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13027 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13028 			    (bsp->mbs_copy_offset < un->un_phy_blocksize));
13029 		} else {
13030 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13031 			    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13032 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13033 			    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13034 		}
13035 
13036 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13037 
13038 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13039 
13040 		/* Transfer the wmap (if any) to the shadow buf */
13041 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13042 		bsp->mbs_wmp = NULL;
13043 
13044 		/*
13045 		 * The shadow buf goes on from here in place of the
13046 		 * original buf.
13047 		 */
13048 		shadow_bsp->mbs_orig_bp = bp;
13049 		bp = shadow_bp;
13050 	}
13051 
13052 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13053 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13054 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13055 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13056 	    request_bytes);
13057 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13058 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13059 
13060 done:
13061 	SD_NEXT_IOSTART(index, un, bp);
13062 
13063 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13064 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13065 }
13066 
13067 
13068 /*
13069  *    Function: sd_mapblocksize_iodone
13070  *
13071  * Description: Completion side processing for block-size mapping.
13072  *
13073  *     Context: May be called under interrupt context
13074  */
13075 
13076 static void
13077 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13078 {
13079 	struct sd_mapblocksize_info	*bsp;
13080 	struct sd_xbuf	*xp;
13081 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
13082 	struct buf	*orig_bp;	/* ptr to the original buf */
13083 	offset_t	shadow_end;
13084 	offset_t	request_end;
13085 	offset_t	shadow_start;
13086 	ssize_t		copy_offset;
13087 	size_t		copy_length;
13088 	size_t		shortfall;
13089 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
13090 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
13091 
13092 	ASSERT(un != NULL);
13093 	ASSERT(bp != NULL);
13094 
13095 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13096 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13097 
13098 	/*
13099 	 * There is no shadow buf or layer-private data if the target is
13100 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
13101 	 */
13102 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13103 	    (bp->b_bcount == 0)) {
13104 		goto exit;
13105 	}
13106 
13107 	xp = SD_GET_XBUF(bp);
13108 	ASSERT(xp != NULL);
13109 
13110 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
13111 	bsp = xp->xb_private;
13112 
13113 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13114 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13115 
13116 	if (is_write) {
13117 		/*
13118 		 * For a WRITE request we must free up the block range that
13119 		 * we have locked up.  This holds regardless of whether this is
13120 		 * an aligned write request or a read-modify-write request.
13121 		 */
13122 		sd_range_unlock(un, bsp->mbs_wmp);
13123 		bsp->mbs_wmp = NULL;
13124 	}
13125 
13126 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13127 		/*
13128 		 * An aligned read or write command will have no shadow buf;
13129 		 * there is not much else to do with it.
13130 		 */
13131 		goto done;
13132 	}
13133 
13134 	orig_bp = bsp->mbs_orig_bp;
13135 	ASSERT(orig_bp != NULL);
13136 	orig_xp = SD_GET_XBUF(orig_bp);
13137 	ASSERT(orig_xp != NULL);
13138 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13139 
13140 	if (!is_write && has_wmap) {
13141 		/*
13142 		 * A READ with a wmap means this is the READ phase of a
13143 		 * read-modify-write. If an error occurred on the READ then
13144 		 * we do not proceed with the WRITE phase or copy any data.
13145 		 * Just release the write maps and return with an error.
13146 		 */
13147 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13148 			orig_bp->b_resid = orig_bp->b_bcount;
13149 			bioerror(orig_bp, bp->b_error);
13150 			sd_range_unlock(un, bsp->mbs_wmp);
13151 			goto freebuf_done;
13152 		}
13153 	}
13154 
13155 	/*
13156 	 * Here is where we set up to copy the data from the shadow buf
13157 	 * into the space associated with the original buf.
13158 	 *
13159 	 * To deal with the conversion between block sizes, these
13160 	 * computations treat the data as an array of bytes, with the
13161 	 * first byte (byte 0) corresponding to the first byte in the
13162 	 * first block on the disk.
13163 	 */
13164 
13165 	/*
13166 	 * shadow_start and shadow_len indicate the location and size of
13167 	 * the data returned with the shadow IO request.
13168 	 */
13169 	if (un->un_f_enable_rmw) {
13170 		shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13171 	} else {
13172 		shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13173 	}
13174 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13175 
13176 	/*
13177 	 * copy_offset gives the offset (in bytes) from the start of the first
13178 	 * block of the READ request to the beginning of the data.  We retrieve
13179 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13180 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13181 	 * data to be copied (in bytes).
13182 	 */
13183 	copy_offset  = bsp->mbs_copy_offset;
13184 	if (un->un_f_enable_rmw) {
13185 		ASSERT((copy_offset >= 0) &&
13186 		    (copy_offset < un->un_phy_blocksize));
13187 	} else {
13188 		ASSERT((copy_offset >= 0) &&
13189 		    (copy_offset < un->un_tgt_blocksize));
13190 	}
13191 
13192 	copy_length  = orig_bp->b_bcount;
13193 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13194 
13195 	/*
13196 	 * Set up the resid and error fields of orig_bp as appropriate.
13197 	 */
13198 	if (shadow_end >= request_end) {
13199 		/* We got all the requested data; set resid to zero */
13200 		orig_bp->b_resid = 0;
13201 	} else {
13202 		/*
13203 		 * We failed to get enough data to fully satisfy the original
13204 		 * request. Just copy back whatever data we got and set
13205 		 * up the residual and error code as required.
13206 		 *
13207 		 * 'shortfall' is the amount by which the data received with the
13208 		 * shadow buf has "fallen short" of the requested amount.
13209 		 */
13210 		shortfall = (size_t)(request_end - shadow_end);
13211 
13212 		if (shortfall > orig_bp->b_bcount) {
13213 			/*
13214 			 * We did not get enough data to even partially
13215 			 * fulfill the original request.  The residual is
13216 			 * equal to the amount requested.
13217 			 */
13218 			orig_bp->b_resid = orig_bp->b_bcount;
13219 		} else {
13220 			/*
13221 			 * We did not get all the data that we requested
13222 			 * from the device, but we will try to return what
13223 			 * portion we did get.
13224 			 */
13225 			orig_bp->b_resid = shortfall;
13226 		}
13227 		ASSERT(copy_length >= orig_bp->b_resid);
13228 		copy_length  -= orig_bp->b_resid;
13229 	}
13230 
13231 	/* Propagate the error code from the shadow buf to the original buf */
13232 	bioerror(orig_bp, bp->b_error);
13233 
13234 	if (is_write) {
13235 		goto freebuf_done;	/* No data copying for a WRITE */
13236 	}
13237 
13238 	if (has_wmap) {
13239 		/*
13240 		 * This is a READ command from the READ phase of a
13241 		 * read-modify-write request. We have to copy the data given
13242 		 * by the user OVER the data returned by the READ command,
13243 		 * then convert the command from a READ to a WRITE and send
13244 		 * it back to the target.
13245 		 */
13246 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13247 		    copy_length);
13248 
13249 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
13250 
13251 		/*
13252 		 * Dispatch the WRITE command to the taskq thread, which
13253 		 * will in turn send the command to the target. When the
13254 		 * WRITE command completes, we (sd_mapblocksize_iodone())
13255 		 * will get called again as part of the iodone chain
13256 		 * processing for it. Note that we will still be dealing
13257 		 * with the shadow buf at that point.
13258 		 */
13259 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13260 		    KM_NOSLEEP) != 0) {
13261 			/*
13262 			 * Dispatch was successful so we are done. Return
13263 			 * without going any higher up the iodone chain. Do
13264 			 * not free up any layer-private data until after the
13265 			 * WRITE completes.
13266 			 */
13267 			return;
13268 		}
13269 
13270 		/*
13271 		 * Dispatch of the WRITE command failed; set up the error
13272 		 * condition and send this IO back up the iodone chain.
13273 		 */
13274 		bioerror(orig_bp, EIO);
13275 		orig_bp->b_resid = orig_bp->b_bcount;
13276 
13277 	} else {
13278 		/*
13279 		 * This is a regular READ request (ie, not a RMW). Copy the
13280 		 * data from the shadow buf into the original buf. The
13281 		 * copy_offset compensates for any "misalignment" between the
13282 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13283 		 * original buf (with its un->un_sys_blocksize blocks).
13284 		 */
13285 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13286 		    copy_length);
13287 	}
13288 
13289 freebuf_done:
13290 
13291 	/*
13292 	 * At this point we still have both the shadow buf AND the original
13293 	 * buf to deal with, as well as the layer-private data area in each.
13294 	 * Local variables are as follows:
13295 	 *
13296 	 * bp -- points to shadow buf
13297 	 * xp -- points to xbuf of shadow buf
13298 	 * bsp -- points to layer-private data area of shadow buf
13299 	 * orig_bp -- points to original buf
13300 	 *
13301 	 * First free the shadow buf and its associated xbuf, then free the
13302 	 * layer-private data area from the shadow buf. There is no need to
13303 	 * restore xb_private in the shadow xbuf.
13304 	 */
13305 	sd_shadow_buf_free(bp);
13306 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13307 
13308 	/*
13309 	 * Now update the local variables to point to the original buf, xbuf,
13310 	 * and layer-private area.
13311 	 */
13312 	bp = orig_bp;
13313 	xp = SD_GET_XBUF(bp);
13314 	ASSERT(xp != NULL);
13315 	ASSERT(xp == orig_xp);
13316 	bsp = xp->xb_private;
13317 	ASSERT(bsp != NULL);
13318 
13319 done:
13320 	/*
13321 	 * Restore xb_private to whatever it was set to by the next higher
13322 	 * layer in the chain, then free the layer-private data area.
13323 	 */
13324 	xp->xb_private = bsp->mbs_oprivate;
13325 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13326 
13327 exit:
13328 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13329 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13330 
13331 	SD_NEXT_IODONE(index, un, bp);
13332 }
13333 
13334 
13335 /*
13336  *    Function: sd_checksum_iostart
13337  *
13338  * Description: A stub function for a layer that's currently not used.
13339  *		For now just a placeholder.
13340  *
13341  *     Context: Kernel thread context
13342  */
13343 
13344 static void
13345 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13346 {
13347 	ASSERT(un != NULL);
13348 	ASSERT(bp != NULL);
13349 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13350 	SD_NEXT_IOSTART(index, un, bp);
13351 }
13352 
13353 
13354 /*
13355  *    Function: sd_checksum_iodone
13356  *
13357  * Description: A stub function for a layer that's currently not used.
13358  *		For now just a placeholder.
13359  *
13360  *     Context: May be called under interrupt context
13361  */
13362 
13363 static void
13364 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13365 {
13366 	ASSERT(un != NULL);
13367 	ASSERT(bp != NULL);
13368 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13369 	SD_NEXT_IODONE(index, un, bp);
13370 }
13371 
13372 
13373 /*
13374  *    Function: sd_checksum_uscsi_iostart
13375  *
13376  * Description: A stub function for a layer that's currently not used.
13377  *		For now just a placeholder.
13378  *
13379  *     Context: Kernel thread context
13380  */
13381 
13382 static void
13383 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13384 {
13385 	ASSERT(un != NULL);
13386 	ASSERT(bp != NULL);
13387 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13388 	SD_NEXT_IOSTART(index, un, bp);
13389 }
13390 
13391 
13392 /*
13393  *    Function: sd_checksum_uscsi_iodone
13394  *
13395  * Description: A stub function for a layer that's currently not used.
13396  *		For now just a placeholder.
13397  *
13398  *     Context: May be called under interrupt context
13399  */
13400 
13401 static void
13402 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13403 {
13404 	ASSERT(un != NULL);
13405 	ASSERT(bp != NULL);
13406 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13407 	SD_NEXT_IODONE(index, un, bp);
13408 }
13409 
13410 
13411 /*
13412  *    Function: sd_pm_iostart
13413  *
13414  * Description: iostart-side routine for Power mangement.
13415  *
13416  *     Context: Kernel thread context
13417  */
13418 
13419 static void
13420 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13421 {
13422 	ASSERT(un != NULL);
13423 	ASSERT(bp != NULL);
13424 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13425 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13426 
13427 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13428 
13429 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13430 		/*
13431 		 * Set up to return the failed buf back up the 'iodone'
13432 		 * side of the calling chain.
13433 		 */
13434 		bioerror(bp, EIO);
13435 		bp->b_resid = bp->b_bcount;
13436 
13437 		SD_BEGIN_IODONE(index, un, bp);
13438 
13439 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13440 		return;
13441 	}
13442 
13443 	SD_NEXT_IOSTART(index, un, bp);
13444 
13445 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13446 }
13447 
13448 
13449 /*
13450  *    Function: sd_pm_iodone
13451  *
13452  * Description: iodone-side routine for power mangement.
13453  *
13454  *     Context: may be called from interrupt context
13455  */
13456 
13457 static void
13458 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13459 {
13460 	ASSERT(un != NULL);
13461 	ASSERT(bp != NULL);
13462 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13463 
13464 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13465 
13466 	/*
13467 	 * After attach the following flag is only read, so don't
13468 	 * take the penalty of acquiring a mutex for it.
13469 	 */
13470 	if (un->un_f_pm_is_enabled == TRUE) {
13471 		sd_pm_exit(un);
13472 	}
13473 
13474 	SD_NEXT_IODONE(index, un, bp);
13475 
13476 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13477 }
13478 
13479 
13480 /*
13481  *    Function: sd_core_iostart
13482  *
13483  * Description: Primary driver function for enqueuing buf(9S) structs from
13484  *		the system and initiating IO to the target device
13485  *
13486  *     Context: Kernel thread context. Can sleep.
13487  *
13488  * Assumptions:  - The given xp->xb_blkno is absolute
13489  *		   (ie, relative to the start of the device).
13490  *		 - The IO is to be done using the native blocksize of
13491  *		   the device, as specified in un->un_tgt_blocksize.
13492  */
13493 /* ARGSUSED */
13494 static void
13495 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13496 {
13497 	struct sd_xbuf *xp;
13498 
13499 	ASSERT(un != NULL);
13500 	ASSERT(bp != NULL);
13501 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13502 	ASSERT(bp->b_resid == 0);
13503 
13504 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13505 
13506 	xp = SD_GET_XBUF(bp);
13507 	ASSERT(xp != NULL);
13508 
13509 	mutex_enter(SD_MUTEX(un));
13510 
13511 	/*
13512 	 * If we are currently in the failfast state, fail any new IO
13513 	 * that has B_FAILFAST set, then return.
13514 	 */
13515 	if ((bp->b_flags & B_FAILFAST) &&
13516 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13517 		mutex_exit(SD_MUTEX(un));
13518 		bioerror(bp, EIO);
13519 		bp->b_resid = bp->b_bcount;
13520 		SD_BEGIN_IODONE(index, un, bp);
13521 		return;
13522 	}
13523 
13524 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13525 		/*
13526 		 * Priority command -- transport it immediately.
13527 		 *
13528 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13529 		 * because all direct priority commands should be associated
13530 		 * with error recovery actions which we don't want to retry.
13531 		 */
13532 		sd_start_cmds(un, bp);
13533 	} else {
13534 		/*
13535 		 * Normal command -- add it to the wait queue, then start
13536 		 * transporting commands from the wait queue.
13537 		 */
13538 		sd_add_buf_to_waitq(un, bp);
13539 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13540 		sd_start_cmds(un, NULL);
13541 	}
13542 
13543 	mutex_exit(SD_MUTEX(un));
13544 
13545 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13546 }
13547 
13548 
13549 /*
13550  *    Function: sd_init_cdb_limits
13551  *
13552  * Description: This is to handle scsi_pkt initialization differences
13553  *		between the driver platforms.
13554  *
13555  *		Legacy behaviors:
13556  *
13557  *		If the block number or the sector count exceeds the
13558  *		capabilities of a Group 0 command, shift over to a
13559  *		Group 1 command. We don't blindly use Group 1
13560  *		commands because a) some drives (CDC Wren IVs) get a
13561  *		bit confused, and b) there is probably a fair amount
13562  *		of speed difference for a target to receive and decode
13563  *		a 10 byte command instead of a 6 byte command.
13564  *
13565  *		The xfer time difference of 6 vs 10 byte CDBs is
13566  *		still significant so this code is still worthwhile.
13567  *		10 byte CDBs are very inefficient with the fas HBA driver
13568  *		and older disks. Each CDB byte took 1 usec with some
13569  *		popular disks.
13570  *
13571  *     Context: Must be called at attach time
13572  */
13573 
13574 static void
13575 sd_init_cdb_limits(struct sd_lun *un)
13576 {
13577 	int hba_cdb_limit;
13578 
13579 	/*
13580 	 * Use CDB_GROUP1 commands for most devices except for
13581 	 * parallel SCSI fixed drives in which case we get better
13582 	 * performance using CDB_GROUP0 commands (where applicable).
13583 	 */
13584 	un->un_mincdb = SD_CDB_GROUP1;
13585 #if !defined(__fibre)
13586 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13587 	    !un->un_f_has_removable_media) {
13588 		un->un_mincdb = SD_CDB_GROUP0;
13589 	}
13590 #endif
13591 
13592 	/*
13593 	 * Try to read the max-cdb-length supported by HBA.
13594 	 */
13595 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13596 	if (0 >= un->un_max_hba_cdb) {
13597 		un->un_max_hba_cdb = CDB_GROUP4;
13598 		hba_cdb_limit = SD_CDB_GROUP4;
13599 	} else if (0 < un->un_max_hba_cdb &&
13600 	    un->un_max_hba_cdb < CDB_GROUP1) {
13601 		hba_cdb_limit = SD_CDB_GROUP0;
13602 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13603 	    un->un_max_hba_cdb < CDB_GROUP5) {
13604 		hba_cdb_limit = SD_CDB_GROUP1;
13605 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13606 	    un->un_max_hba_cdb < CDB_GROUP4) {
13607 		hba_cdb_limit = SD_CDB_GROUP5;
13608 	} else {
13609 		hba_cdb_limit = SD_CDB_GROUP4;
13610 	}
13611 
13612 	/*
13613 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13614 	 * commands for fixed disks unless we are building for a 32 bit
13615 	 * kernel.
13616 	 */
13617 #ifdef _LP64
13618 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13619 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13620 #else
13621 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13622 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13623 #endif
13624 
13625 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13626 	    ? sizeof (struct scsi_arq_status) : 1);
13627 	un->un_cmd_timeout = (ushort_t)sd_io_time;
13628 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13629 }
13630 
13631 
13632 /*
13633  *    Function: sd_initpkt_for_buf
13634  *
13635  * Description: Allocate and initialize for transport a scsi_pkt struct,
13636  *		based upon the info specified in the given buf struct.
13637  *
13638  *		Assumes the xb_blkno in the request is absolute (ie,
13639  *		relative to the start of the device (NOT partition!).
13640  *		Also assumes that the request is using the native block
13641  *		size of the device (as returned by the READ CAPACITY
13642  *		command).
13643  *
13644  * Return Code: SD_PKT_ALLOC_SUCCESS
13645  *		SD_PKT_ALLOC_FAILURE
13646  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13647  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13648  *
13649  *     Context: Kernel thread and may be called from software interrupt context
13650  *		as part of a sdrunout callback. This function may not block or
13651  *		call routines that block
13652  */
13653 
13654 static int
13655 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13656 {
13657 	struct sd_xbuf	*xp;
13658 	struct scsi_pkt *pktp = NULL;
13659 	struct sd_lun	*un;
13660 	size_t		blockcount;
13661 	daddr_t		startblock;
13662 	int		rval;
13663 	int		cmd_flags;
13664 
13665 	ASSERT(bp != NULL);
13666 	ASSERT(pktpp != NULL);
13667 	xp = SD_GET_XBUF(bp);
13668 	ASSERT(xp != NULL);
13669 	un = SD_GET_UN(bp);
13670 	ASSERT(un != NULL);
13671 	ASSERT(mutex_owned(SD_MUTEX(un)));
13672 	ASSERT(bp->b_resid == 0);
13673 
13674 	SD_TRACE(SD_LOG_IO_CORE, un,
13675 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13676 
13677 	mutex_exit(SD_MUTEX(un));
13678 
13679 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13680 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13681 		/*
13682 		 * Already have a scsi_pkt -- just need DMA resources.
13683 		 * We must recompute the CDB in case the mapping returns
13684 		 * a nonzero pkt_resid.
13685 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13686 		 * that is being retried, the unmap/remap of the DMA resouces
13687 		 * will result in the entire transfer starting over again
13688 		 * from the very first block.
13689 		 */
13690 		ASSERT(xp->xb_pktp != NULL);
13691 		pktp = xp->xb_pktp;
13692 	} else {
13693 		pktp = NULL;
13694 	}
13695 #endif /* __i386 || __amd64 */
13696 
13697 	startblock = xp->xb_blkno;	/* Absolute block num. */
13698 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13699 
13700 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13701 
13702 	/*
13703 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13704 	 * call scsi_init_pkt, and build the CDB.
13705 	 */
13706 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13707 	    cmd_flags, sdrunout, (caddr_t)un,
13708 	    startblock, blockcount);
13709 
13710 	if (rval == 0) {
13711 		/*
13712 		 * Success.
13713 		 *
13714 		 * If partial DMA is being used and required for this transfer.
13715 		 * set it up here.
13716 		 */
13717 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13718 		    (pktp->pkt_resid != 0)) {
13719 
13720 			/*
13721 			 * Save the CDB length and pkt_resid for the
13722 			 * next xfer
13723 			 */
13724 			xp->xb_dma_resid = pktp->pkt_resid;
13725 
13726 			/* rezero resid */
13727 			pktp->pkt_resid = 0;
13728 
13729 		} else {
13730 			xp->xb_dma_resid = 0;
13731 		}
13732 
13733 		pktp->pkt_flags = un->un_tagflags;
13734 		pktp->pkt_time  = un->un_cmd_timeout;
13735 		pktp->pkt_comp  = sdintr;
13736 
13737 		pktp->pkt_private = bp;
13738 		*pktpp = pktp;
13739 
13740 		SD_TRACE(SD_LOG_IO_CORE, un,
13741 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13742 
13743 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13744 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13745 #endif
13746 
13747 		mutex_enter(SD_MUTEX(un));
13748 		return (SD_PKT_ALLOC_SUCCESS);
13749 
13750 	}
13751 
13752 	/*
13753 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13754 	 * from sd_setup_rw_pkt.
13755 	 */
13756 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13757 
13758 	if (rval == SD_PKT_ALLOC_FAILURE) {
13759 		*pktpp = NULL;
13760 		/*
13761 		 * Set the driver state to RWAIT to indicate the driver
13762 		 * is waiting on resource allocations. The driver will not
13763 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13764 		 */
13765 		mutex_enter(SD_MUTEX(un));
13766 		New_state(un, SD_STATE_RWAIT);
13767 
13768 		SD_ERROR(SD_LOG_IO_CORE, un,
13769 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13770 
13771 		if ((bp->b_flags & B_ERROR) != 0) {
13772 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13773 		}
13774 		return (SD_PKT_ALLOC_FAILURE);
13775 	} else {
13776 		/*
13777 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13778 		 *
13779 		 * This should never happen.  Maybe someone messed with the
13780 		 * kernel's minphys?
13781 		 */
13782 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13783 		    "Request rejected: too large for CDB: "
13784 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13785 		SD_ERROR(SD_LOG_IO_CORE, un,
13786 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13787 		mutex_enter(SD_MUTEX(un));
13788 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13789 
13790 	}
13791 }
13792 
13793 
13794 /*
13795  *    Function: sd_destroypkt_for_buf
13796  *
13797  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13798  *
13799  *     Context: Kernel thread or interrupt context
13800  */
13801 
13802 static void
13803 sd_destroypkt_for_buf(struct buf *bp)
13804 {
13805 	ASSERT(bp != NULL);
13806 	ASSERT(SD_GET_UN(bp) != NULL);
13807 
13808 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13809 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13810 
13811 	ASSERT(SD_GET_PKTP(bp) != NULL);
13812 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13813 
13814 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13815 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13816 }
13817 
13818 /*
13819  *    Function: sd_setup_rw_pkt
13820  *
13821  * Description: Determines appropriate CDB group for the requested LBA
13822  *		and transfer length, calls scsi_init_pkt, and builds
13823  *		the CDB.  Do not use for partial DMA transfers except
13824  *		for the initial transfer since the CDB size must
13825  *		remain constant.
13826  *
13827  *     Context: Kernel thread and may be called from software interrupt
13828  *		context as part of a sdrunout callback. This function may not
13829  *		block or call routines that block
13830  */
13831 
13832 
13833 int
13834 sd_setup_rw_pkt(struct sd_lun *un,
13835     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13836     int (*callback)(caddr_t), caddr_t callback_arg,
13837     diskaddr_t lba, uint32_t blockcount)
13838 {
13839 	struct scsi_pkt *return_pktp;
13840 	union scsi_cdb *cdbp;
13841 	struct sd_cdbinfo *cp = NULL;
13842 	int i;
13843 
13844 	/*
13845 	 * See which size CDB to use, based upon the request.
13846 	 */
13847 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13848 
13849 		/*
13850 		 * Check lba and block count against sd_cdbtab limits.
13851 		 * In the partial DMA case, we have to use the same size
13852 		 * CDB for all the transfers.  Check lba + blockcount
13853 		 * against the max LBA so we know that segment of the
13854 		 * transfer can use the CDB we select.
13855 		 */
13856 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13857 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13858 
13859 			/*
13860 			 * The command will fit into the CDB type
13861 			 * specified by sd_cdbtab[i].
13862 			 */
13863 			cp = sd_cdbtab + i;
13864 
13865 			/*
13866 			 * Call scsi_init_pkt so we can fill in the
13867 			 * CDB.
13868 			 */
13869 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13870 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13871 			    flags, callback, callback_arg);
13872 
13873 			if (return_pktp != NULL) {
13874 
13875 				/*
13876 				 * Return new value of pkt
13877 				 */
13878 				*pktpp = return_pktp;
13879 
13880 				/*
13881 				 * To be safe, zero the CDB insuring there is
13882 				 * no leftover data from a previous command.
13883 				 */
13884 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13885 
13886 				/*
13887 				 * Handle partial DMA mapping
13888 				 */
13889 				if (return_pktp->pkt_resid != 0) {
13890 
13891 					/*
13892 					 * Not going to xfer as many blocks as
13893 					 * originally expected
13894 					 */
13895 					blockcount -=
13896 					    SD_BYTES2TGTBLOCKS(un,
13897 					    return_pktp->pkt_resid);
13898 				}
13899 
13900 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13901 
13902 				/*
13903 				 * Set command byte based on the CDB
13904 				 * type we matched.
13905 				 */
13906 				cdbp->scc_cmd = cp->sc_grpmask |
13907 				    ((bp->b_flags & B_READ) ?
13908 				    SCMD_READ : SCMD_WRITE);
13909 
13910 				SD_FILL_SCSI1_LUN(un, return_pktp);
13911 
13912 				/*
13913 				 * Fill in LBA and length
13914 				 */
13915 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13916 				    (cp->sc_grpcode == CDB_GROUP4) ||
13917 				    (cp->sc_grpcode == CDB_GROUP0) ||
13918 				    (cp->sc_grpcode == CDB_GROUP5));
13919 
13920 				if (cp->sc_grpcode == CDB_GROUP1) {
13921 					FORMG1ADDR(cdbp, lba);
13922 					FORMG1COUNT(cdbp, blockcount);
13923 					return (0);
13924 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13925 					FORMG4LONGADDR(cdbp, lba);
13926 					FORMG4COUNT(cdbp, blockcount);
13927 					return (0);
13928 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13929 					FORMG0ADDR(cdbp, lba);
13930 					FORMG0COUNT(cdbp, blockcount);
13931 					return (0);
13932 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13933 					FORMG5ADDR(cdbp, lba);
13934 					FORMG5COUNT(cdbp, blockcount);
13935 					return (0);
13936 				}
13937 
13938 				/*
13939 				 * It should be impossible to not match one
13940 				 * of the CDB types above, so we should never
13941 				 * reach this point.  Set the CDB command byte
13942 				 * to test-unit-ready to avoid writing
13943 				 * to somewhere we don't intend.
13944 				 */
13945 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13946 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13947 			} else {
13948 				/*
13949 				 * Couldn't get scsi_pkt
13950 				 */
13951 				return (SD_PKT_ALLOC_FAILURE);
13952 			}
13953 		}
13954 	}
13955 
13956 	/*
13957 	 * None of the available CDB types were suitable.  This really
13958 	 * should never happen:  on a 64 bit system we support
13959 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13960 	 * and on a 32 bit system we will refuse to bind to a device
13961 	 * larger than 2TB so addresses will never be larger than 32 bits.
13962 	 */
13963 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13964 }
13965 
13966 /*
13967  *    Function: sd_setup_next_rw_pkt
13968  *
13969  * Description: Setup packet for partial DMA transfers, except for the
13970  * 		initial transfer.  sd_setup_rw_pkt should be used for
13971  *		the initial transfer.
13972  *
13973  *     Context: Kernel thread and may be called from interrupt context.
13974  */
13975 
13976 int
13977 sd_setup_next_rw_pkt(struct sd_lun *un,
13978     struct scsi_pkt *pktp, struct buf *bp,
13979     diskaddr_t lba, uint32_t blockcount)
13980 {
13981 	uchar_t com;
13982 	union scsi_cdb *cdbp;
13983 	uchar_t cdb_group_id;
13984 
13985 	ASSERT(pktp != NULL);
13986 	ASSERT(pktp->pkt_cdbp != NULL);
13987 
13988 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13989 	com = cdbp->scc_cmd;
13990 	cdb_group_id = CDB_GROUPID(com);
13991 
13992 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13993 	    (cdb_group_id == CDB_GROUPID_1) ||
13994 	    (cdb_group_id == CDB_GROUPID_4) ||
13995 	    (cdb_group_id == CDB_GROUPID_5));
13996 
13997 	/*
13998 	 * Move pkt to the next portion of the xfer.
13999 	 * func is NULL_FUNC so we do not have to release
14000 	 * the disk mutex here.
14001 	 */
14002 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
14003 	    NULL_FUNC, NULL) == pktp) {
14004 		/* Success.  Handle partial DMA */
14005 		if (pktp->pkt_resid != 0) {
14006 			blockcount -=
14007 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14008 		}
14009 
14010 		cdbp->scc_cmd = com;
14011 		SD_FILL_SCSI1_LUN(un, pktp);
14012 		if (cdb_group_id == CDB_GROUPID_1) {
14013 			FORMG1ADDR(cdbp, lba);
14014 			FORMG1COUNT(cdbp, blockcount);
14015 			return (0);
14016 		} else if (cdb_group_id == CDB_GROUPID_4) {
14017 			FORMG4LONGADDR(cdbp, lba);
14018 			FORMG4COUNT(cdbp, blockcount);
14019 			return (0);
14020 		} else if (cdb_group_id == CDB_GROUPID_0) {
14021 			FORMG0ADDR(cdbp, lba);
14022 			FORMG0COUNT(cdbp, blockcount);
14023 			return (0);
14024 		} else if (cdb_group_id == CDB_GROUPID_5) {
14025 			FORMG5ADDR(cdbp, lba);
14026 			FORMG5COUNT(cdbp, blockcount);
14027 			return (0);
14028 		}
14029 
14030 		/* Unreachable */
14031 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14032 	}
14033 
14034 	/*
14035 	 * Error setting up next portion of cmd transfer.
14036 	 * Something is definitely very wrong and this
14037 	 * should not happen.
14038 	 */
14039 	return (SD_PKT_ALLOC_FAILURE);
14040 }
14041 
14042 /*
14043  *    Function: sd_initpkt_for_uscsi
14044  *
14045  * Description: Allocate and initialize for transport a scsi_pkt struct,
14046  *		based upon the info specified in the given uscsi_cmd struct.
14047  *
14048  * Return Code: SD_PKT_ALLOC_SUCCESS
14049  *		SD_PKT_ALLOC_FAILURE
14050  *		SD_PKT_ALLOC_FAILURE_NO_DMA
14051  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14052  *
14053  *     Context: Kernel thread and may be called from software interrupt context
14054  *		as part of a sdrunout callback. This function may not block or
14055  *		call routines that block
14056  */
14057 
14058 static int
14059 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14060 {
14061 	struct uscsi_cmd *uscmd;
14062 	struct sd_xbuf	*xp;
14063 	struct scsi_pkt	*pktp;
14064 	struct sd_lun	*un;
14065 	uint32_t	flags = 0;
14066 
14067 	ASSERT(bp != NULL);
14068 	ASSERT(pktpp != NULL);
14069 	xp = SD_GET_XBUF(bp);
14070 	ASSERT(xp != NULL);
14071 	un = SD_GET_UN(bp);
14072 	ASSERT(un != NULL);
14073 	ASSERT(mutex_owned(SD_MUTEX(un)));
14074 
14075 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14076 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14077 	ASSERT(uscmd != NULL);
14078 
14079 	SD_TRACE(SD_LOG_IO_CORE, un,
14080 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14081 
14082 	/*
14083 	 * Allocate the scsi_pkt for the command.
14084 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14085 	 *	 during scsi_init_pkt time and will continue to use the
14086 	 *	 same path as long as the same scsi_pkt is used without
14087 	 *	 intervening scsi_dma_free(). Since uscsi command does
14088 	 *	 not call scsi_dmafree() before retry failed command, it
14089 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14090 	 *	 set such that scsi_vhci can use other available path for
14091 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
14092 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
14093 	 */
14094 	if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14095 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14096 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14097 		    ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14098 		    - sizeof (struct scsi_extended_sense)), 0,
14099 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14100 		    sdrunout, (caddr_t)un);
14101 	} else {
14102 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14103 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14104 		    sizeof (struct scsi_arq_status), 0,
14105 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14106 		    sdrunout, (caddr_t)un);
14107 	}
14108 
14109 	if (pktp == NULL) {
14110 		*pktpp = NULL;
14111 		/*
14112 		 * Set the driver state to RWAIT to indicate the driver
14113 		 * is waiting on resource allocations. The driver will not
14114 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
14115 		 */
14116 		New_state(un, SD_STATE_RWAIT);
14117 
14118 		SD_ERROR(SD_LOG_IO_CORE, un,
14119 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14120 
14121 		if ((bp->b_flags & B_ERROR) != 0) {
14122 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14123 		}
14124 		return (SD_PKT_ALLOC_FAILURE);
14125 	}
14126 
14127 	/*
14128 	 * We do not do DMA breakup for USCSI commands, so return failure
14129 	 * here if all the needed DMA resources were not allocated.
14130 	 */
14131 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14132 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14133 		scsi_destroy_pkt(pktp);
14134 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14135 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14136 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14137 	}
14138 
14139 	/* Init the cdb from the given uscsi struct */
14140 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14141 	    uscmd->uscsi_cdb[0], 0, 0, 0);
14142 
14143 	SD_FILL_SCSI1_LUN(un, pktp);
14144 
14145 	/*
14146 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
14147 	 * for listing of the supported flags.
14148 	 */
14149 
14150 	if (uscmd->uscsi_flags & USCSI_SILENT) {
14151 		flags |= FLAG_SILENT;
14152 	}
14153 
14154 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14155 		flags |= FLAG_DIAGNOSE;
14156 	}
14157 
14158 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14159 		flags |= FLAG_ISOLATE;
14160 	}
14161 
14162 	if (un->un_f_is_fibre == FALSE) {
14163 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14164 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14165 		}
14166 	}
14167 
14168 	/*
14169 	 * Set the pkt flags here so we save time later.
14170 	 * Note: These flags are NOT in the uscsi man page!!!
14171 	 */
14172 	if (uscmd->uscsi_flags & USCSI_HEAD) {
14173 		flags |= FLAG_HEAD;
14174 	}
14175 
14176 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
14177 		flags |= FLAG_NOINTR;
14178 	}
14179 
14180 	/*
14181 	 * For tagged queueing, things get a bit complicated.
14182 	 * Check first for head of queue and last for ordered queue.
14183 	 * If neither head nor order, use the default driver tag flags.
14184 	 */
14185 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14186 		if (uscmd->uscsi_flags & USCSI_HTAG) {
14187 			flags |= FLAG_HTAG;
14188 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
14189 			flags |= FLAG_OTAG;
14190 		} else {
14191 			flags |= un->un_tagflags & FLAG_TAGMASK;
14192 		}
14193 	}
14194 
14195 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
14196 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14197 	}
14198 
14199 	pktp->pkt_flags = flags;
14200 
14201 	/* Transfer uscsi information to scsi_pkt */
14202 	(void) scsi_uscsi_pktinit(uscmd, pktp);
14203 
14204 	/* Copy the caller's CDB into the pkt... */
14205 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14206 
14207 	if (uscmd->uscsi_timeout == 0) {
14208 		pktp->pkt_time = un->un_uscsi_timeout;
14209 	} else {
14210 		pktp->pkt_time = uscmd->uscsi_timeout;
14211 	}
14212 
14213 	/* need it later to identify USCSI request in sdintr */
14214 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
14215 
14216 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
14217 
14218 	pktp->pkt_private = bp;
14219 	pktp->pkt_comp = sdintr;
14220 	*pktpp = pktp;
14221 
14222 	SD_TRACE(SD_LOG_IO_CORE, un,
14223 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14224 
14225 	return (SD_PKT_ALLOC_SUCCESS);
14226 }
14227 
14228 
14229 /*
14230  *    Function: sd_destroypkt_for_uscsi
14231  *
14232  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14233  *		IOs.. Also saves relevant info into the associated uscsi_cmd
14234  *		struct.
14235  *
14236  *     Context: May be called under interrupt context
14237  */
14238 
14239 static void
14240 sd_destroypkt_for_uscsi(struct buf *bp)
14241 {
14242 	struct uscsi_cmd *uscmd;
14243 	struct sd_xbuf	*xp;
14244 	struct scsi_pkt	*pktp;
14245 	struct sd_lun	*un;
14246 	struct sd_uscsi_info *suip;
14247 
14248 	ASSERT(bp != NULL);
14249 	xp = SD_GET_XBUF(bp);
14250 	ASSERT(xp != NULL);
14251 	un = SD_GET_UN(bp);
14252 	ASSERT(un != NULL);
14253 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14254 	pktp = SD_GET_PKTP(bp);
14255 	ASSERT(pktp != NULL);
14256 
14257 	SD_TRACE(SD_LOG_IO_CORE, un,
14258 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14259 
14260 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14261 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14262 	ASSERT(uscmd != NULL);
14263 
14264 	/* Save the status and the residual into the uscsi_cmd struct */
14265 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14266 	uscmd->uscsi_resid  = bp->b_resid;
14267 
14268 	/* Transfer scsi_pkt information to uscsi */
14269 	(void) scsi_uscsi_pktfini(pktp, uscmd);
14270 
14271 	/*
14272 	 * If enabled, copy any saved sense data into the area specified
14273 	 * by the uscsi command.
14274 	 */
14275 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14276 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14277 		/*
14278 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14279 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14280 		 */
14281 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14282 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14283 		if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14284 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14285 			    MAX_SENSE_LENGTH);
14286 		} else {
14287 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14288 			    SENSE_LENGTH);
14289 		}
14290 	}
14291 	/*
14292 	 * The following assignments are for SCSI FMA.
14293 	 */
14294 	ASSERT(xp->xb_private != NULL);
14295 	suip = (struct sd_uscsi_info *)xp->xb_private;
14296 	suip->ui_pkt_reason = pktp->pkt_reason;
14297 	suip->ui_pkt_state = pktp->pkt_state;
14298 	suip->ui_pkt_statistics = pktp->pkt_statistics;
14299 	suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14300 
14301 	/* We are done with the scsi_pkt; free it now */
14302 	ASSERT(SD_GET_PKTP(bp) != NULL);
14303 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14304 
14305 	SD_TRACE(SD_LOG_IO_CORE, un,
14306 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14307 }
14308 
14309 
14310 /*
14311  *    Function: sd_bioclone_alloc
14312  *
14313  * Description: Allocate a buf(9S) and init it as per the given buf
14314  *		and the various arguments.  The associated sd_xbuf
14315  *		struct is (nearly) duplicated.  The struct buf *bp
14316  *		argument is saved in new_xp->xb_private.
14317  *
14318  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14319  *		datalen - size of data area for the shadow bp
14320  *		blkno - starting LBA
14321  *		func - function pointer for b_iodone in the shadow buf. (May
14322  *			be NULL if none.)
14323  *
14324  * Return Code: Pointer to allocates buf(9S) struct
14325  *
14326  *     Context: Can sleep.
14327  */
14328 
14329 static struct buf *
14330 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14331 	daddr_t blkno, int (*func)(struct buf *))
14332 {
14333 	struct	sd_lun	*un;
14334 	struct	sd_xbuf	*xp;
14335 	struct	sd_xbuf	*new_xp;
14336 	struct	buf	*new_bp;
14337 
14338 	ASSERT(bp != NULL);
14339 	xp = SD_GET_XBUF(bp);
14340 	ASSERT(xp != NULL);
14341 	un = SD_GET_UN(bp);
14342 	ASSERT(un != NULL);
14343 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14344 
14345 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14346 	    NULL, KM_SLEEP);
14347 
14348 	new_bp->b_lblkno	= blkno;
14349 
14350 	/*
14351 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14352 	 * original xbuf into it.
14353 	 */
14354 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14355 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14356 
14357 	/*
14358 	 * The given bp is automatically saved in the xb_private member
14359 	 * of the new xbuf.  Callers are allowed to depend on this.
14360 	 */
14361 	new_xp->xb_private = bp;
14362 
14363 	new_bp->b_private  = new_xp;
14364 
14365 	return (new_bp);
14366 }
14367 
14368 /*
14369  *    Function: sd_shadow_buf_alloc
14370  *
14371  * Description: Allocate a buf(9S) and init it as per the given buf
14372  *		and the various arguments.  The associated sd_xbuf
14373  *		struct is (nearly) duplicated.  The struct buf *bp
14374  *		argument is saved in new_xp->xb_private.
14375  *
14376  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14377  *		datalen - size of data area for the shadow bp
14378  *		bflags - B_READ or B_WRITE (pseudo flag)
14379  *		blkno - starting LBA
14380  *		func - function pointer for b_iodone in the shadow buf. (May
14381  *			be NULL if none.)
14382  *
14383  * Return Code: Pointer to allocates buf(9S) struct
14384  *
14385  *     Context: Can sleep.
14386  */
14387 
14388 static struct buf *
14389 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14390 	daddr_t blkno, int (*func)(struct buf *))
14391 {
14392 	struct	sd_lun	*un;
14393 	struct	sd_xbuf	*xp;
14394 	struct	sd_xbuf	*new_xp;
14395 	struct	buf	*new_bp;
14396 
14397 	ASSERT(bp != NULL);
14398 	xp = SD_GET_XBUF(bp);
14399 	ASSERT(xp != NULL);
14400 	un = SD_GET_UN(bp);
14401 	ASSERT(un != NULL);
14402 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14403 
14404 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14405 		bp_mapin(bp);
14406 	}
14407 
14408 	bflags &= (B_READ | B_WRITE);
14409 #if defined(__i386) || defined(__amd64)
14410 	new_bp = getrbuf(KM_SLEEP);
14411 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14412 	new_bp->b_bcount = datalen;
14413 	new_bp->b_flags = bflags |
14414 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14415 #else
14416 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14417 	    datalen, bflags, SLEEP_FUNC, NULL);
14418 #endif
14419 	new_bp->av_forw	= NULL;
14420 	new_bp->av_back	= NULL;
14421 	new_bp->b_dev	= bp->b_dev;
14422 	new_bp->b_blkno	= blkno;
14423 	new_bp->b_iodone = func;
14424 	new_bp->b_edev	= bp->b_edev;
14425 	new_bp->b_resid	= 0;
14426 
14427 	/* We need to preserve the B_FAILFAST flag */
14428 	if (bp->b_flags & B_FAILFAST) {
14429 		new_bp->b_flags |= B_FAILFAST;
14430 	}
14431 
14432 	/*
14433 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14434 	 * original xbuf into it.
14435 	 */
14436 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14437 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14438 
14439 	/* Need later to copy data between the shadow buf & original buf! */
14440 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14441 
14442 	/*
14443 	 * The given bp is automatically saved in the xb_private member
14444 	 * of the new xbuf.  Callers are allowed to depend on this.
14445 	 */
14446 	new_xp->xb_private = bp;
14447 
14448 	new_bp->b_private  = new_xp;
14449 
14450 	return (new_bp);
14451 }
14452 
14453 /*
14454  *    Function: sd_bioclone_free
14455  *
14456  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14457  *		in the larger than partition operation.
14458  *
14459  *     Context: May be called under interrupt context
14460  */
14461 
14462 static void
14463 sd_bioclone_free(struct buf *bp)
14464 {
14465 	struct sd_xbuf	*xp;
14466 
14467 	ASSERT(bp != NULL);
14468 	xp = SD_GET_XBUF(bp);
14469 	ASSERT(xp != NULL);
14470 
14471 	/*
14472 	 * Call bp_mapout() before freeing the buf,  in case a lower
14473 	 * layer or HBA  had done a bp_mapin().  we must do this here
14474 	 * as we are the "originator" of the shadow buf.
14475 	 */
14476 	bp_mapout(bp);
14477 
14478 	/*
14479 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14480 	 * never gets confused by a stale value in this field. (Just a little
14481 	 * extra defensiveness here.)
14482 	 */
14483 	bp->b_iodone = NULL;
14484 
14485 	freerbuf(bp);
14486 
14487 	kmem_free(xp, sizeof (struct sd_xbuf));
14488 }
14489 
14490 /*
14491  *    Function: sd_shadow_buf_free
14492  *
14493  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14494  *
14495  *     Context: May be called under interrupt context
14496  */
14497 
14498 static void
14499 sd_shadow_buf_free(struct buf *bp)
14500 {
14501 	struct sd_xbuf	*xp;
14502 
14503 	ASSERT(bp != NULL);
14504 	xp = SD_GET_XBUF(bp);
14505 	ASSERT(xp != NULL);
14506 
14507 #if defined(__sparc)
14508 	/*
14509 	 * Call bp_mapout() before freeing the buf,  in case a lower
14510 	 * layer or HBA  had done a bp_mapin().  we must do this here
14511 	 * as we are the "originator" of the shadow buf.
14512 	 */
14513 	bp_mapout(bp);
14514 #endif
14515 
14516 	/*
14517 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14518 	 * never gets confused by a stale value in this field. (Just a little
14519 	 * extra defensiveness here.)
14520 	 */
14521 	bp->b_iodone = NULL;
14522 
14523 #if defined(__i386) || defined(__amd64)
14524 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14525 	freerbuf(bp);
14526 #else
14527 	scsi_free_consistent_buf(bp);
14528 #endif
14529 
14530 	kmem_free(xp, sizeof (struct sd_xbuf));
14531 }
14532 
14533 
14534 /*
14535  *    Function: sd_print_transport_rejected_message
14536  *
14537  * Description: This implements the ludicrously complex rules for printing
14538  *		a "transport rejected" message.  This is to address the
14539  *		specific problem of having a flood of this error message
14540  *		produced when a failover occurs.
14541  *
14542  *     Context: Any.
14543  */
14544 
14545 static void
14546 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14547 	int code)
14548 {
14549 	ASSERT(un != NULL);
14550 	ASSERT(mutex_owned(SD_MUTEX(un)));
14551 	ASSERT(xp != NULL);
14552 
14553 	/*
14554 	 * Print the "transport rejected" message under the following
14555 	 * conditions:
14556 	 *
14557 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14558 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14559 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14560 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14561 	 *   scsi_transport(9F) (which indicates that the target might have
14562 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14563 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14564 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14565 	 *   from scsi_transport().
14566 	 *
14567 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14568 	 * the preceeding cases in order for the message to be printed.
14569 	 */
14570 	if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14571 	    (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14572 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14573 		    (code != TRAN_FATAL_ERROR) ||
14574 		    (un->un_tran_fatal_count == 1)) {
14575 			switch (code) {
14576 			case TRAN_BADPKT:
14577 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14578 				    "transport rejected bad packet\n");
14579 				break;
14580 			case TRAN_FATAL_ERROR:
14581 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14582 				    "transport rejected fatal error\n");
14583 				break;
14584 			default:
14585 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14586 				    "transport rejected (%d)\n", code);
14587 				break;
14588 			}
14589 		}
14590 	}
14591 }
14592 
14593 
14594 /*
14595  *    Function: sd_add_buf_to_waitq
14596  *
14597  * Description: Add the given buf(9S) struct to the wait queue for the
14598  *		instance.  If sorting is enabled, then the buf is added
14599  *		to the queue via an elevator sort algorithm (a la
14600  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14601  *		If sorting is not enabled, then the buf is just added
14602  *		to the end of the wait queue.
14603  *
14604  * Return Code: void
14605  *
14606  *     Context: Does not sleep/block, therefore technically can be called
14607  *		from any context.  However if sorting is enabled then the
14608  *		execution time is indeterminate, and may take long if
14609  *		the wait queue grows large.
14610  */
14611 
14612 static void
14613 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14614 {
14615 	struct buf *ap;
14616 
14617 	ASSERT(bp != NULL);
14618 	ASSERT(un != NULL);
14619 	ASSERT(mutex_owned(SD_MUTEX(un)));
14620 
14621 	/* If the queue is empty, add the buf as the only entry & return. */
14622 	if (un->un_waitq_headp == NULL) {
14623 		ASSERT(un->un_waitq_tailp == NULL);
14624 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14625 		bp->av_forw = NULL;
14626 		return;
14627 	}
14628 
14629 	ASSERT(un->un_waitq_tailp != NULL);
14630 
14631 	/*
14632 	 * If sorting is disabled, just add the buf to the tail end of
14633 	 * the wait queue and return.
14634 	 */
14635 	if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14636 		un->un_waitq_tailp->av_forw = bp;
14637 		un->un_waitq_tailp = bp;
14638 		bp->av_forw = NULL;
14639 		return;
14640 	}
14641 
14642 	/*
14643 	 * Sort thru the list of requests currently on the wait queue
14644 	 * and add the new buf request at the appropriate position.
14645 	 *
14646 	 * The un->un_waitq_headp is an activity chain pointer on which
14647 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14648 	 * first queue holds those requests which are positioned after
14649 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14650 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14651 	 * Thus we implement a one way scan, retracting after reaching
14652 	 * the end of the drive to the first request on the second
14653 	 * queue, at which time it becomes the first queue.
14654 	 * A one-way scan is natural because of the way UNIX read-ahead
14655 	 * blocks are allocated.
14656 	 *
14657 	 * If we lie after the first request, then we must locate the
14658 	 * second request list and add ourselves to it.
14659 	 */
14660 	ap = un->un_waitq_headp;
14661 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14662 		while (ap->av_forw != NULL) {
14663 			/*
14664 			 * Look for an "inversion" in the (normally
14665 			 * ascending) block numbers. This indicates
14666 			 * the start of the second request list.
14667 			 */
14668 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14669 				/*
14670 				 * Search the second request list for the
14671 				 * first request at a larger block number.
14672 				 * We go before that; however if there is
14673 				 * no such request, we go at the end.
14674 				 */
14675 				do {
14676 					if (SD_GET_BLKNO(bp) <
14677 					    SD_GET_BLKNO(ap->av_forw)) {
14678 						goto insert;
14679 					}
14680 					ap = ap->av_forw;
14681 				} while (ap->av_forw != NULL);
14682 				goto insert;		/* after last */
14683 			}
14684 			ap = ap->av_forw;
14685 		}
14686 
14687 		/*
14688 		 * No inversions... we will go after the last, and
14689 		 * be the first request in the second request list.
14690 		 */
14691 		goto insert;
14692 	}
14693 
14694 	/*
14695 	 * Request is at/after the current request...
14696 	 * sort in the first request list.
14697 	 */
14698 	while (ap->av_forw != NULL) {
14699 		/*
14700 		 * We want to go after the current request (1) if
14701 		 * there is an inversion after it (i.e. it is the end
14702 		 * of the first request list), or (2) if the next
14703 		 * request is a larger block no. than our request.
14704 		 */
14705 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14706 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14707 			goto insert;
14708 		}
14709 		ap = ap->av_forw;
14710 	}
14711 
14712 	/*
14713 	 * Neither a second list nor a larger request, therefore
14714 	 * we go at the end of the first list (which is the same
14715 	 * as the end of the whole schebang).
14716 	 */
14717 insert:
14718 	bp->av_forw = ap->av_forw;
14719 	ap->av_forw = bp;
14720 
14721 	/*
14722 	 * If we inserted onto the tail end of the waitq, make sure the
14723 	 * tail pointer is updated.
14724 	 */
14725 	if (ap == un->un_waitq_tailp) {
14726 		un->un_waitq_tailp = bp;
14727 	}
14728 }
14729 
14730 
14731 /*
14732  *    Function: sd_start_cmds
14733  *
14734  * Description: Remove and transport cmds from the driver queues.
14735  *
14736  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14737  *
14738  *		immed_bp - ptr to a buf to be transported immediately. Only
14739  *		the immed_bp is transported; bufs on the waitq are not
14740  *		processed and the un_retry_bp is not checked.  If immed_bp is
14741  *		NULL, then normal queue processing is performed.
14742  *
14743  *     Context: May be called from kernel thread context, interrupt context,
14744  *		or runout callback context. This function may not block or
14745  *		call routines that block.
14746  */
14747 
14748 static void
14749 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14750 {
14751 	struct	sd_xbuf	*xp;
14752 	struct	buf	*bp;
14753 	void	(*statp)(kstat_io_t *);
14754 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14755 	void	(*saved_statp)(kstat_io_t *);
14756 #endif
14757 	int	rval;
14758 	struct sd_fm_internal *sfip = NULL;
14759 
14760 	ASSERT(un != NULL);
14761 	ASSERT(mutex_owned(SD_MUTEX(un)));
14762 	ASSERT(un->un_ncmds_in_transport >= 0);
14763 	ASSERT(un->un_throttle >= 0);
14764 
14765 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14766 
14767 	do {
14768 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14769 		saved_statp = NULL;
14770 #endif
14771 
14772 		/*
14773 		 * If we are syncing or dumping, fail the command to
14774 		 * avoid recursively calling back into scsi_transport().
14775 		 * The dump I/O itself uses a separate code path so this
14776 		 * only prevents non-dump I/O from being sent while dumping.
14777 		 * File system sync takes place before dumping begins.
14778 		 * During panic, filesystem I/O is allowed provided
14779 		 * un_in_callback is <= 1.  This is to prevent recursion
14780 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14781 		 * sd_start_cmds and so on.  See panic.c for more information
14782 		 * about the states the system can be in during panic.
14783 		 */
14784 		if ((un->un_state == SD_STATE_DUMPING) ||
14785 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14786 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14787 			    "sd_start_cmds: panicking\n");
14788 			goto exit;
14789 		}
14790 
14791 		if ((bp = immed_bp) != NULL) {
14792 			/*
14793 			 * We have a bp that must be transported immediately.
14794 			 * It's OK to transport the immed_bp here without doing
14795 			 * the throttle limit check because the immed_bp is
14796 			 * always used in a retry/recovery case. This means
14797 			 * that we know we are not at the throttle limit by
14798 			 * virtue of the fact that to get here we must have
14799 			 * already gotten a command back via sdintr(). This also
14800 			 * relies on (1) the command on un_retry_bp preventing
14801 			 * further commands from the waitq from being issued;
14802 			 * and (2) the code in sd_retry_command checking the
14803 			 * throttle limit before issuing a delayed or immediate
14804 			 * retry. This holds even if the throttle limit is
14805 			 * currently ratcheted down from its maximum value.
14806 			 */
14807 			statp = kstat_runq_enter;
14808 			if (bp == un->un_retry_bp) {
14809 				ASSERT((un->un_retry_statp == NULL) ||
14810 				    (un->un_retry_statp == kstat_waitq_enter) ||
14811 				    (un->un_retry_statp ==
14812 				    kstat_runq_back_to_waitq));
14813 				/*
14814 				 * If the waitq kstat was incremented when
14815 				 * sd_set_retry_bp() queued this bp for a retry,
14816 				 * then we must set up statp so that the waitq
14817 				 * count will get decremented correctly below.
14818 				 * Also we must clear un->un_retry_statp to
14819 				 * ensure that we do not act on a stale value
14820 				 * in this field.
14821 				 */
14822 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14823 				    (un->un_retry_statp ==
14824 				    kstat_runq_back_to_waitq)) {
14825 					statp = kstat_waitq_to_runq;
14826 				}
14827 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14828 				saved_statp = un->un_retry_statp;
14829 #endif
14830 				un->un_retry_statp = NULL;
14831 
14832 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14833 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14834 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14835 				    un, un->un_retry_bp, un->un_throttle,
14836 				    un->un_ncmds_in_transport);
14837 			} else {
14838 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14839 				    "processing priority bp:0x%p\n", bp);
14840 			}
14841 
14842 		} else if ((bp = un->un_waitq_headp) != NULL) {
14843 			/*
14844 			 * A command on the waitq is ready to go, but do not
14845 			 * send it if:
14846 			 *
14847 			 * (1) the throttle limit has been reached, or
14848 			 * (2) a retry is pending, or
14849 			 * (3) a START_STOP_UNIT callback pending, or
14850 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14851 			 *	command is pending.
14852 			 *
14853 			 * For all of these conditions, IO processing will
14854 			 * restart after the condition is cleared.
14855 			 */
14856 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14857 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14858 				    "sd_start_cmds: exiting, "
14859 				    "throttle limit reached!\n");
14860 				goto exit;
14861 			}
14862 			if (un->un_retry_bp != NULL) {
14863 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14864 				    "sd_start_cmds: exiting, retry pending!\n");
14865 				goto exit;
14866 			}
14867 			if (un->un_startstop_timeid != NULL) {
14868 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14869 				    "sd_start_cmds: exiting, "
14870 				    "START_STOP pending!\n");
14871 				goto exit;
14872 			}
14873 			if (un->un_direct_priority_timeid != NULL) {
14874 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14875 				    "sd_start_cmds: exiting, "
14876 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14877 				goto exit;
14878 			}
14879 
14880 			/* Dequeue the command */
14881 			un->un_waitq_headp = bp->av_forw;
14882 			if (un->un_waitq_headp == NULL) {
14883 				un->un_waitq_tailp = NULL;
14884 			}
14885 			bp->av_forw = NULL;
14886 			statp = kstat_waitq_to_runq;
14887 			SD_TRACE(SD_LOG_IO_CORE, un,
14888 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14889 
14890 		} else {
14891 			/* No work to do so bail out now */
14892 			SD_TRACE(SD_LOG_IO_CORE, un,
14893 			    "sd_start_cmds: no more work, exiting!\n");
14894 			goto exit;
14895 		}
14896 
14897 		/*
14898 		 * Reset the state to normal. This is the mechanism by which
14899 		 * the state transitions from either SD_STATE_RWAIT or
14900 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14901 		 * If state is SD_STATE_PM_CHANGING then this command is
14902 		 * part of the device power control and the state must
14903 		 * not be put back to normal. Doing so would would
14904 		 * allow new commands to proceed when they shouldn't,
14905 		 * the device may be going off.
14906 		 */
14907 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14908 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14909 			New_state(un, SD_STATE_NORMAL);
14910 		}
14911 
14912 		xp = SD_GET_XBUF(bp);
14913 		ASSERT(xp != NULL);
14914 
14915 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14916 		/*
14917 		 * Allocate the scsi_pkt if we need one, or attach DMA
14918 		 * resources if we have a scsi_pkt that needs them. The
14919 		 * latter should only occur for commands that are being
14920 		 * retried.
14921 		 */
14922 		if ((xp->xb_pktp == NULL) ||
14923 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14924 #else
14925 		if (xp->xb_pktp == NULL) {
14926 #endif
14927 			/*
14928 			 * There is no scsi_pkt allocated for this buf. Call
14929 			 * the initpkt function to allocate & init one.
14930 			 *
14931 			 * The scsi_init_pkt runout callback functionality is
14932 			 * implemented as follows:
14933 			 *
14934 			 * 1) The initpkt function always calls
14935 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14936 			 *    callback routine.
14937 			 * 2) A successful packet allocation is initialized and
14938 			 *    the I/O is transported.
14939 			 * 3) The I/O associated with an allocation resource
14940 			 *    failure is left on its queue to be retried via
14941 			 *    runout or the next I/O.
14942 			 * 4) The I/O associated with a DMA error is removed
14943 			 *    from the queue and failed with EIO. Processing of
14944 			 *    the transport queues is also halted to be
14945 			 *    restarted via runout or the next I/O.
14946 			 * 5) The I/O associated with a CDB size or packet
14947 			 *    size error is removed from the queue and failed
14948 			 *    with EIO. Processing of the transport queues is
14949 			 *    continued.
14950 			 *
14951 			 * Note: there is no interface for canceling a runout
14952 			 * callback. To prevent the driver from detaching or
14953 			 * suspending while a runout is pending the driver
14954 			 * state is set to SD_STATE_RWAIT
14955 			 *
14956 			 * Note: using the scsi_init_pkt callback facility can
14957 			 * result in an I/O request persisting at the head of
14958 			 * the list which cannot be satisfied even after
14959 			 * multiple retries. In the future the driver may
14960 			 * implement some kind of maximum runout count before
14961 			 * failing an I/O.
14962 			 *
14963 			 * Note: the use of funcp below may seem superfluous,
14964 			 * but it helps warlock figure out the correct
14965 			 * initpkt function calls (see [s]sd.wlcmd).
14966 			 */
14967 			struct scsi_pkt	*pktp;
14968 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14969 
14970 			ASSERT(bp != un->un_rqs_bp);
14971 
14972 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14973 			switch ((*funcp)(bp, &pktp)) {
14974 			case  SD_PKT_ALLOC_SUCCESS:
14975 				xp->xb_pktp = pktp;
14976 				SD_TRACE(SD_LOG_IO_CORE, un,
14977 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14978 				    pktp);
14979 				goto got_pkt;
14980 
14981 			case SD_PKT_ALLOC_FAILURE:
14982 				/*
14983 				 * Temporary (hopefully) resource depletion.
14984 				 * Since retries and RQS commands always have a
14985 				 * scsi_pkt allocated, these cases should never
14986 				 * get here. So the only cases this needs to
14987 				 * handle is a bp from the waitq (which we put
14988 				 * back onto the waitq for sdrunout), or a bp
14989 				 * sent as an immed_bp (which we just fail).
14990 				 */
14991 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14992 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14993 
14994 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14995 
14996 				if (bp == immed_bp) {
14997 					/*
14998 					 * If SD_XB_DMA_FREED is clear, then
14999 					 * this is a failure to allocate a
15000 					 * scsi_pkt, and we must fail the
15001 					 * command.
15002 					 */
15003 					if ((xp->xb_pkt_flags &
15004 					    SD_XB_DMA_FREED) == 0) {
15005 						break;
15006 					}
15007 
15008 					/*
15009 					 * If this immediate command is NOT our
15010 					 * un_retry_bp, then we must fail it.
15011 					 */
15012 					if (bp != un->un_retry_bp) {
15013 						break;
15014 					}
15015 
15016 					/*
15017 					 * We get here if this cmd is our
15018 					 * un_retry_bp that was DMAFREED, but
15019 					 * scsi_init_pkt() failed to reallocate
15020 					 * DMA resources when we attempted to
15021 					 * retry it. This can happen when an
15022 					 * mpxio failover is in progress, but
15023 					 * we don't want to just fail the
15024 					 * command in this case.
15025 					 *
15026 					 * Use timeout(9F) to restart it after
15027 					 * a 100ms delay.  We don't want to
15028 					 * let sdrunout() restart it, because
15029 					 * sdrunout() is just supposed to start
15030 					 * commands that are sitting on the
15031 					 * wait queue.  The un_retry_bp stays
15032 					 * set until the command completes, but
15033 					 * sdrunout can be called many times
15034 					 * before that happens.  Since sdrunout
15035 					 * cannot tell if the un_retry_bp is
15036 					 * already in the transport, it could
15037 					 * end up calling scsi_transport() for
15038 					 * the un_retry_bp multiple times.
15039 					 *
15040 					 * Also: don't schedule the callback
15041 					 * if some other callback is already
15042 					 * pending.
15043 					 */
15044 					if (un->un_retry_statp == NULL) {
15045 						/*
15046 						 * restore the kstat pointer to
15047 						 * keep kstat counts coherent
15048 						 * when we do retry the command.
15049 						 */
15050 						un->un_retry_statp =
15051 						    saved_statp;
15052 					}
15053 
15054 					if ((un->un_startstop_timeid == NULL) &&
15055 					    (un->un_retry_timeid == NULL) &&
15056 					    (un->un_direct_priority_timeid ==
15057 					    NULL)) {
15058 
15059 						un->un_retry_timeid =
15060 						    timeout(
15061 						    sd_start_retry_command,
15062 						    un, SD_RESTART_TIMEOUT);
15063 					}
15064 					goto exit;
15065 				}
15066 
15067 #else
15068 				if (bp == immed_bp) {
15069 					break;	/* Just fail the command */
15070 				}
15071 #endif
15072 
15073 				/* Add the buf back to the head of the waitq */
15074 				bp->av_forw = un->un_waitq_headp;
15075 				un->un_waitq_headp = bp;
15076 				if (un->un_waitq_tailp == NULL) {
15077 					un->un_waitq_tailp = bp;
15078 				}
15079 				goto exit;
15080 
15081 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
15082 				/*
15083 				 * HBA DMA resource failure. Fail the command
15084 				 * and continue processing of the queues.
15085 				 */
15086 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15087 				    "sd_start_cmds: "
15088 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15089 				break;
15090 
15091 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15092 				/*
15093 				 * Note:x86: Partial DMA mapping not supported
15094 				 * for USCSI commands, and all the needed DMA
15095 				 * resources were not allocated.
15096 				 */
15097 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15098 				    "sd_start_cmds: "
15099 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15100 				break;
15101 
15102 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15103 				/*
15104 				 * Note:x86: Request cannot fit into CDB based
15105 				 * on lba and len.
15106 				 */
15107 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15108 				    "sd_start_cmds: "
15109 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15110 				break;
15111 
15112 			default:
15113 				/* Should NEVER get here! */
15114 				panic("scsi_initpkt error");
15115 				/*NOTREACHED*/
15116 			}
15117 
15118 			/*
15119 			 * Fatal error in allocating a scsi_pkt for this buf.
15120 			 * Update kstats & return the buf with an error code.
15121 			 * We must use sd_return_failed_command_no_restart() to
15122 			 * avoid a recursive call back into sd_start_cmds().
15123 			 * However this also means that we must keep processing
15124 			 * the waitq here in order to avoid stalling.
15125 			 */
15126 			if (statp == kstat_waitq_to_runq) {
15127 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15128 			}
15129 			sd_return_failed_command_no_restart(un, bp, EIO);
15130 			if (bp == immed_bp) {
15131 				/* immed_bp is gone by now, so clear this */
15132 				immed_bp = NULL;
15133 			}
15134 			continue;
15135 		}
15136 got_pkt:
15137 		if (bp == immed_bp) {
15138 			/* goto the head of the class.... */
15139 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15140 		}
15141 
15142 		un->un_ncmds_in_transport++;
15143 		SD_UPDATE_KSTATS(un, statp, bp);
15144 
15145 		/*
15146 		 * Call scsi_transport() to send the command to the target.
15147 		 * According to SCSA architecture, we must drop the mutex here
15148 		 * before calling scsi_transport() in order to avoid deadlock.
15149 		 * Note that the scsi_pkt's completion routine can be executed
15150 		 * (from interrupt context) even before the call to
15151 		 * scsi_transport() returns.
15152 		 */
15153 		SD_TRACE(SD_LOG_IO_CORE, un,
15154 		    "sd_start_cmds: calling scsi_transport()\n");
15155 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15156 
15157 		mutex_exit(SD_MUTEX(un));
15158 		rval = scsi_transport(xp->xb_pktp);
15159 		mutex_enter(SD_MUTEX(un));
15160 
15161 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15162 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
15163 
15164 		switch (rval) {
15165 		case TRAN_ACCEPT:
15166 			/* Clear this with every pkt accepted by the HBA */
15167 			un->un_tran_fatal_count = 0;
15168 			break;	/* Success; try the next cmd (if any) */
15169 
15170 		case TRAN_BUSY:
15171 			un->un_ncmds_in_transport--;
15172 			ASSERT(un->un_ncmds_in_transport >= 0);
15173 
15174 			/*
15175 			 * Don't retry request sense, the sense data
15176 			 * is lost when another request is sent.
15177 			 * Free up the rqs buf and retry
15178 			 * the original failed cmd.  Update kstat.
15179 			 */
15180 			if (bp == un->un_rqs_bp) {
15181 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15182 				bp = sd_mark_rqs_idle(un, xp);
15183 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15184 				    NULL, NULL, EIO, un->un_busy_timeout / 500,
15185 				    kstat_waitq_enter);
15186 				goto exit;
15187 			}
15188 
15189 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15190 			/*
15191 			 * Free the DMA resources for the  scsi_pkt. This will
15192 			 * allow mpxio to select another path the next time
15193 			 * we call scsi_transport() with this scsi_pkt.
15194 			 * See sdintr() for the rationalization behind this.
15195 			 */
15196 			if ((un->un_f_is_fibre == TRUE) &&
15197 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15198 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15199 				scsi_dmafree(xp->xb_pktp);
15200 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15201 			}
15202 #endif
15203 
15204 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15205 				/*
15206 				 * Commands that are SD_PATH_DIRECT_PRIORITY
15207 				 * are for error recovery situations. These do
15208 				 * not use the normal command waitq, so if they
15209 				 * get a TRAN_BUSY we cannot put them back onto
15210 				 * the waitq for later retry. One possible
15211 				 * problem is that there could already be some
15212 				 * other command on un_retry_bp that is waiting
15213 				 * for this one to complete, so we would be
15214 				 * deadlocked if we put this command back onto
15215 				 * the waitq for later retry (since un_retry_bp
15216 				 * must complete before the driver gets back to
15217 				 * commands on the waitq).
15218 				 *
15219 				 * To avoid deadlock we must schedule a callback
15220 				 * that will restart this command after a set
15221 				 * interval.  This should keep retrying for as
15222 				 * long as the underlying transport keeps
15223 				 * returning TRAN_BUSY (just like for other
15224 				 * commands).  Use the same timeout interval as
15225 				 * for the ordinary TRAN_BUSY retry.
15226 				 */
15227 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15228 				    "sd_start_cmds: scsi_transport() returned "
15229 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15230 
15231 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15232 				un->un_direct_priority_timeid =
15233 				    timeout(sd_start_direct_priority_command,
15234 				    bp, un->un_busy_timeout / 500);
15235 
15236 				goto exit;
15237 			}
15238 
15239 			/*
15240 			 * For TRAN_BUSY, we want to reduce the throttle value,
15241 			 * unless we are retrying a command.
15242 			 */
15243 			if (bp != un->un_retry_bp) {
15244 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15245 			}
15246 
15247 			/*
15248 			 * Set up the bp to be tried again 10 ms later.
15249 			 * Note:x86: Is there a timeout value in the sd_lun
15250 			 * for this condition?
15251 			 */
15252 			sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15253 			    kstat_runq_back_to_waitq);
15254 			goto exit;
15255 
15256 		case TRAN_FATAL_ERROR:
15257 			un->un_tran_fatal_count++;
15258 			/* FALLTHRU */
15259 
15260 		case TRAN_BADPKT:
15261 		default:
15262 			un->un_ncmds_in_transport--;
15263 			ASSERT(un->un_ncmds_in_transport >= 0);
15264 
15265 			/*
15266 			 * If this is our REQUEST SENSE command with a
15267 			 * transport error, we must get back the pointers
15268 			 * to the original buf, and mark the REQUEST
15269 			 * SENSE command as "available".
15270 			 */
15271 			if (bp == un->un_rqs_bp) {
15272 				bp = sd_mark_rqs_idle(un, xp);
15273 				xp = SD_GET_XBUF(bp);
15274 			} else {
15275 				/*
15276 				 * Legacy behavior: do not update transport
15277 				 * error count for request sense commands.
15278 				 */
15279 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
15280 			}
15281 
15282 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15283 			sd_print_transport_rejected_message(un, xp, rval);
15284 
15285 			/*
15286 			 * This command will be terminated by SD driver due
15287 			 * to a fatal transport error. We should post
15288 			 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15289 			 * of "fail" for any command to indicate this
15290 			 * situation.
15291 			 */
15292 			if (xp->xb_ena > 0) {
15293 				ASSERT(un->un_fm_private != NULL);
15294 				sfip = un->un_fm_private;
15295 				sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15296 				sd_ssc_extract_info(&sfip->fm_ssc, un,
15297 				    xp->xb_pktp, bp, xp);
15298 				sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15299 			}
15300 
15301 			/*
15302 			 * We must use sd_return_failed_command_no_restart() to
15303 			 * avoid a recursive call back into sd_start_cmds().
15304 			 * However this also means that we must keep processing
15305 			 * the waitq here in order to avoid stalling.
15306 			 */
15307 			sd_return_failed_command_no_restart(un, bp, EIO);
15308 
15309 			/*
15310 			 * Notify any threads waiting in sd_ddi_suspend() that
15311 			 * a command completion has occurred.
15312 			 */
15313 			if (un->un_state == SD_STATE_SUSPENDED) {
15314 				cv_broadcast(&un->un_disk_busy_cv);
15315 			}
15316 
15317 			if (bp == immed_bp) {
15318 				/* immed_bp is gone by now, so clear this */
15319 				immed_bp = NULL;
15320 			}
15321 			break;
15322 		}
15323 
15324 	} while (immed_bp == NULL);
15325 
15326 exit:
15327 	ASSERT(mutex_owned(SD_MUTEX(un)));
15328 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15329 }
15330 
15331 
15332 /*
15333  *    Function: sd_return_command
15334  *
15335  * Description: Returns a command to its originator (with or without an
15336  *		error).  Also starts commands waiting to be transported
15337  *		to the target.
15338  *
15339  *     Context: May be called from interrupt, kernel, or timeout context
15340  */
15341 
15342 static void
15343 sd_return_command(struct sd_lun *un, struct buf *bp)
15344 {
15345 	struct sd_xbuf *xp;
15346 	struct scsi_pkt *pktp;
15347 	struct sd_fm_internal *sfip;
15348 
15349 	ASSERT(bp != NULL);
15350 	ASSERT(un != NULL);
15351 	ASSERT(mutex_owned(SD_MUTEX(un)));
15352 	ASSERT(bp != un->un_rqs_bp);
15353 	xp = SD_GET_XBUF(bp);
15354 	ASSERT(xp != NULL);
15355 
15356 	pktp = SD_GET_PKTP(bp);
15357 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15358 	ASSERT(sfip != NULL);
15359 
15360 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15361 
15362 	/*
15363 	 * Note: check for the "sdrestart failed" case.
15364 	 */
15365 	if ((un->un_partial_dma_supported == 1) &&
15366 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15367 	    (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15368 	    (xp->xb_pktp->pkt_resid == 0)) {
15369 
15370 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15371 			/*
15372 			 * Successfully set up next portion of cmd
15373 			 * transfer, try sending it
15374 			 */
15375 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15376 			    NULL, NULL, 0, (clock_t)0, NULL);
15377 			sd_start_cmds(un, NULL);
15378 			return;	/* Note:x86: need a return here? */
15379 		}
15380 	}
15381 
15382 	/*
15383 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15384 	 * can happen if upon being re-tried the failfast bp either
15385 	 * succeeded or encountered another error (possibly even a different
15386 	 * error than the one that precipitated the failfast state, but in
15387 	 * that case it would have had to exhaust retries as well). Regardless,
15388 	 * this should not occur whenever the instance is in the active
15389 	 * failfast state.
15390 	 */
15391 	if (bp == un->un_failfast_bp) {
15392 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15393 		un->un_failfast_bp = NULL;
15394 	}
15395 
15396 	/*
15397 	 * Clear the failfast state upon successful completion of ANY cmd.
15398 	 */
15399 	if (bp->b_error == 0) {
15400 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15401 		/*
15402 		 * If this is a successful command, but used to be retried,
15403 		 * we will take it as a recovered command and post an
15404 		 * ereport with driver-assessment of "recovered".
15405 		 */
15406 		if (xp->xb_ena > 0) {
15407 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15408 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15409 		}
15410 	} else {
15411 		/*
15412 		 * If this is a failed non-USCSI command we will post an
15413 		 * ereport with driver-assessment set accordingly("fail" or
15414 		 * "fatal").
15415 		 */
15416 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15417 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15418 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15419 		}
15420 	}
15421 
15422 	/*
15423 	 * This is used if the command was retried one or more times. Show that
15424 	 * we are done with it, and allow processing of the waitq to resume.
15425 	 */
15426 	if (bp == un->un_retry_bp) {
15427 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15428 		    "sd_return_command: un:0x%p: "
15429 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15430 		un->un_retry_bp = NULL;
15431 		un->un_retry_statp = NULL;
15432 	}
15433 
15434 	SD_UPDATE_RDWR_STATS(un, bp);
15435 	SD_UPDATE_PARTITION_STATS(un, bp);
15436 
15437 	switch (un->un_state) {
15438 	case SD_STATE_SUSPENDED:
15439 		/*
15440 		 * Notify any threads waiting in sd_ddi_suspend() that
15441 		 * a command completion has occurred.
15442 		 */
15443 		cv_broadcast(&un->un_disk_busy_cv);
15444 		break;
15445 	default:
15446 		sd_start_cmds(un, NULL);
15447 		break;
15448 	}
15449 
15450 	/* Return this command up the iodone chain to its originator. */
15451 	mutex_exit(SD_MUTEX(un));
15452 
15453 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15454 	xp->xb_pktp = NULL;
15455 
15456 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15457 
15458 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15459 	mutex_enter(SD_MUTEX(un));
15460 
15461 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15462 }
15463 
15464 
15465 /*
15466  *    Function: sd_return_failed_command
15467  *
15468  * Description: Command completion when an error occurred.
15469  *
15470  *     Context: May be called from interrupt context
15471  */
15472 
15473 static void
15474 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15475 {
15476 	ASSERT(bp != NULL);
15477 	ASSERT(un != NULL);
15478 	ASSERT(mutex_owned(SD_MUTEX(un)));
15479 
15480 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15481 	    "sd_return_failed_command: entry\n");
15482 
15483 	/*
15484 	 * b_resid could already be nonzero due to a partial data
15485 	 * transfer, so do not change it here.
15486 	 */
15487 	SD_BIOERROR(bp, errcode);
15488 
15489 	sd_return_command(un, bp);
15490 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15491 	    "sd_return_failed_command: exit\n");
15492 }
15493 
15494 
15495 /*
15496  *    Function: sd_return_failed_command_no_restart
15497  *
15498  * Description: Same as sd_return_failed_command, but ensures that no
15499  *		call back into sd_start_cmds will be issued.
15500  *
15501  *     Context: May be called from interrupt context
15502  */
15503 
15504 static void
15505 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15506 	int errcode)
15507 {
15508 	struct sd_xbuf *xp;
15509 
15510 	ASSERT(bp != NULL);
15511 	ASSERT(un != NULL);
15512 	ASSERT(mutex_owned(SD_MUTEX(un)));
15513 	xp = SD_GET_XBUF(bp);
15514 	ASSERT(xp != NULL);
15515 	ASSERT(errcode != 0);
15516 
15517 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15518 	    "sd_return_failed_command_no_restart: entry\n");
15519 
15520 	/*
15521 	 * b_resid could already be nonzero due to a partial data
15522 	 * transfer, so do not change it here.
15523 	 */
15524 	SD_BIOERROR(bp, errcode);
15525 
15526 	/*
15527 	 * If this is the failfast bp, clear it. This can happen if the
15528 	 * failfast bp encounterd a fatal error when we attempted to
15529 	 * re-try it (such as a scsi_transport(9F) failure).  However
15530 	 * we should NOT be in an active failfast state if the failfast
15531 	 * bp is not NULL.
15532 	 */
15533 	if (bp == un->un_failfast_bp) {
15534 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15535 		un->un_failfast_bp = NULL;
15536 	}
15537 
15538 	if (bp == un->un_retry_bp) {
15539 		/*
15540 		 * This command was retried one or more times. Show that we are
15541 		 * done with it, and allow processing of the waitq to resume.
15542 		 */
15543 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15544 		    "sd_return_failed_command_no_restart: "
15545 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15546 		un->un_retry_bp = NULL;
15547 		un->un_retry_statp = NULL;
15548 	}
15549 
15550 	SD_UPDATE_RDWR_STATS(un, bp);
15551 	SD_UPDATE_PARTITION_STATS(un, bp);
15552 
15553 	mutex_exit(SD_MUTEX(un));
15554 
15555 	if (xp->xb_pktp != NULL) {
15556 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15557 		xp->xb_pktp = NULL;
15558 	}
15559 
15560 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15561 
15562 	mutex_enter(SD_MUTEX(un));
15563 
15564 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15565 	    "sd_return_failed_command_no_restart: exit\n");
15566 }
15567 
15568 
15569 /*
15570  *    Function: sd_retry_command
15571  *
15572  * Description: queue up a command for retry, or (optionally) fail it
15573  *		if retry counts are exhausted.
15574  *
15575  *   Arguments: un - Pointer to the sd_lun struct for the target.
15576  *
15577  *		bp - Pointer to the buf for the command to be retried.
15578  *
15579  *		retry_check_flag - Flag to see which (if any) of the retry
15580  *		   counts should be decremented/checked. If the indicated
15581  *		   retry count is exhausted, then the command will not be
15582  *		   retried; it will be failed instead. This should use a
15583  *		   value equal to one of the following:
15584  *
15585  *			SD_RETRIES_NOCHECK
15586  *			SD_RESD_RETRIES_STANDARD
15587  *			SD_RETRIES_VICTIM
15588  *
15589  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15590  *		   if the check should be made to see of FLAG_ISOLATE is set
15591  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15592  *		   not retried, it is simply failed.
15593  *
15594  *		user_funcp - Ptr to function to call before dispatching the
15595  *		   command. May be NULL if no action needs to be performed.
15596  *		   (Primarily intended for printing messages.)
15597  *
15598  *		user_arg - Optional argument to be passed along to
15599  *		   the user_funcp call.
15600  *
15601  *		failure_code - errno return code to set in the bp if the
15602  *		   command is going to be failed.
15603  *
15604  *		retry_delay - Retry delay interval in (clock_t) units. May
15605  *		   be zero which indicates that the retry should be retried
15606  *		   immediately (ie, without an intervening delay).
15607  *
15608  *		statp - Ptr to kstat function to be updated if the command
15609  *		   is queued for a delayed retry. May be NULL if no kstat
15610  *		   update is desired.
15611  *
15612  *     Context: May be called from interrupt context.
15613  */
15614 
15615 static void
15616 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15617 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15618 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15619 	void (*statp)(kstat_io_t *))
15620 {
15621 	struct sd_xbuf	*xp;
15622 	struct scsi_pkt	*pktp;
15623 	struct sd_fm_internal *sfip;
15624 
15625 	ASSERT(un != NULL);
15626 	ASSERT(mutex_owned(SD_MUTEX(un)));
15627 	ASSERT(bp != NULL);
15628 	xp = SD_GET_XBUF(bp);
15629 	ASSERT(xp != NULL);
15630 	pktp = SD_GET_PKTP(bp);
15631 	ASSERT(pktp != NULL);
15632 
15633 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15634 	ASSERT(sfip != NULL);
15635 
15636 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15637 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15638 
15639 	/*
15640 	 * If we are syncing or dumping, fail the command to avoid
15641 	 * recursively calling back into scsi_transport().
15642 	 */
15643 	if (ddi_in_panic()) {
15644 		goto fail_command_no_log;
15645 	}
15646 
15647 	/*
15648 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15649 	 * log an error and fail the command.
15650 	 */
15651 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15652 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15653 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15654 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15655 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15656 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15657 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15658 		goto fail_command;
15659 	}
15660 
15661 	/*
15662 	 * If we are suspended, then put the command onto head of the
15663 	 * wait queue since we don't want to start more commands, and
15664 	 * clear the un_retry_bp. Next time when we are resumed, will
15665 	 * handle the command in the wait queue.
15666 	 */
15667 	switch (un->un_state) {
15668 	case SD_STATE_SUSPENDED:
15669 	case SD_STATE_DUMPING:
15670 		bp->av_forw = un->un_waitq_headp;
15671 		un->un_waitq_headp = bp;
15672 		if (un->un_waitq_tailp == NULL) {
15673 			un->un_waitq_tailp = bp;
15674 		}
15675 		if (bp == un->un_retry_bp) {
15676 			un->un_retry_bp = NULL;
15677 			un->un_retry_statp = NULL;
15678 		}
15679 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15680 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15681 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15682 		return;
15683 	default:
15684 		break;
15685 	}
15686 
15687 	/*
15688 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15689 	 * is set; if it is then we do not want to retry the command.
15690 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15691 	 */
15692 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15693 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15694 			goto fail_command;
15695 		}
15696 	}
15697 
15698 
15699 	/*
15700 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15701 	 * command timeout or a selection timeout has occurred. This means
15702 	 * that we were unable to establish an kind of communication with
15703 	 * the target, and subsequent retries and/or commands are likely
15704 	 * to encounter similar results and take a long time to complete.
15705 	 *
15706 	 * If this is a failfast error condition, we need to update the
15707 	 * failfast state, even if this bp does not have B_FAILFAST set.
15708 	 */
15709 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15710 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15711 			ASSERT(un->un_failfast_bp == NULL);
15712 			/*
15713 			 * If we are already in the active failfast state, and
15714 			 * another failfast error condition has been detected,
15715 			 * then fail this command if it has B_FAILFAST set.
15716 			 * If B_FAILFAST is clear, then maintain the legacy
15717 			 * behavior of retrying heroically, even tho this will
15718 			 * take a lot more time to fail the command.
15719 			 */
15720 			if (bp->b_flags & B_FAILFAST) {
15721 				goto fail_command;
15722 			}
15723 		} else {
15724 			/*
15725 			 * We're not in the active failfast state, but we
15726 			 * have a failfast error condition, so we must begin
15727 			 * transition to the next state. We do this regardless
15728 			 * of whether or not this bp has B_FAILFAST set.
15729 			 */
15730 			if (un->un_failfast_bp == NULL) {
15731 				/*
15732 				 * This is the first bp to meet a failfast
15733 				 * condition so save it on un_failfast_bp &
15734 				 * do normal retry processing. Do not enter
15735 				 * active failfast state yet. This marks
15736 				 * entry into the "failfast pending" state.
15737 				 */
15738 				un->un_failfast_bp = bp;
15739 
15740 			} else if (un->un_failfast_bp == bp) {
15741 				/*
15742 				 * This is the second time *this* bp has
15743 				 * encountered a failfast error condition,
15744 				 * so enter active failfast state & flush
15745 				 * queues as appropriate.
15746 				 */
15747 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15748 				un->un_failfast_bp = NULL;
15749 				sd_failfast_flushq(un);
15750 
15751 				/*
15752 				 * Fail this bp now if B_FAILFAST set;
15753 				 * otherwise continue with retries. (It would
15754 				 * be pretty ironic if this bp succeeded on a
15755 				 * subsequent retry after we just flushed all
15756 				 * the queues).
15757 				 */
15758 				if (bp->b_flags & B_FAILFAST) {
15759 					goto fail_command;
15760 				}
15761 
15762 #if !defined(lint) && !defined(__lint)
15763 			} else {
15764 				/*
15765 				 * If neither of the preceeding conditionals
15766 				 * was true, it means that there is some
15767 				 * *other* bp that has met an inital failfast
15768 				 * condition and is currently either being
15769 				 * retried or is waiting to be retried. In
15770 				 * that case we should perform normal retry
15771 				 * processing on *this* bp, since there is a
15772 				 * chance that the current failfast condition
15773 				 * is transient and recoverable. If that does
15774 				 * not turn out to be the case, then retries
15775 				 * will be cleared when the wait queue is
15776 				 * flushed anyway.
15777 				 */
15778 #endif
15779 			}
15780 		}
15781 	} else {
15782 		/*
15783 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15784 		 * likely were able to at least establish some level of
15785 		 * communication with the target and subsequent commands
15786 		 * and/or retries are likely to get through to the target,
15787 		 * In this case we want to be aggressive about clearing
15788 		 * the failfast state. Note that this does not affect
15789 		 * the "failfast pending" condition.
15790 		 */
15791 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15792 	}
15793 
15794 
15795 	/*
15796 	 * Check the specified retry count to see if we can still do
15797 	 * any retries with this pkt before we should fail it.
15798 	 */
15799 	switch (retry_check_flag & SD_RETRIES_MASK) {
15800 	case SD_RETRIES_VICTIM:
15801 		/*
15802 		 * Check the victim retry count. If exhausted, then fall
15803 		 * thru & check against the standard retry count.
15804 		 */
15805 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15806 			/* Increment count & proceed with the retry */
15807 			xp->xb_victim_retry_count++;
15808 			break;
15809 		}
15810 		/* Victim retries exhausted, fall back to std. retries... */
15811 		/* FALLTHRU */
15812 
15813 	case SD_RETRIES_STANDARD:
15814 		if (xp->xb_retry_count >= un->un_retry_count) {
15815 			/* Retries exhausted, fail the command */
15816 			SD_TRACE(SD_LOG_IO_CORE, un,
15817 			    "sd_retry_command: retries exhausted!\n");
15818 			/*
15819 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15820 			 * commands with nonzero pkt_resid.
15821 			 */
15822 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15823 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15824 			    (pktp->pkt_resid != 0)) {
15825 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15826 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15827 					SD_UPDATE_B_RESID(bp, pktp);
15828 				}
15829 			}
15830 			goto fail_command;
15831 		}
15832 		xp->xb_retry_count++;
15833 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15834 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15835 		break;
15836 
15837 	case SD_RETRIES_UA:
15838 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15839 			/* Retries exhausted, fail the command */
15840 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15841 			    "Unit Attention retries exhausted. "
15842 			    "Check the target.\n");
15843 			goto fail_command;
15844 		}
15845 		xp->xb_ua_retry_count++;
15846 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15847 		    "sd_retry_command: retry count:%d\n",
15848 		    xp->xb_ua_retry_count);
15849 		break;
15850 
15851 	case SD_RETRIES_BUSY:
15852 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15853 			/* Retries exhausted, fail the command */
15854 			SD_TRACE(SD_LOG_IO_CORE, un,
15855 			    "sd_retry_command: retries exhausted!\n");
15856 			goto fail_command;
15857 		}
15858 		xp->xb_retry_count++;
15859 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15860 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15861 		break;
15862 
15863 	case SD_RETRIES_NOCHECK:
15864 	default:
15865 		/* No retry count to check. Just proceed with the retry */
15866 		break;
15867 	}
15868 
15869 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15870 
15871 	/*
15872 	 * If this is a non-USCSI command being retried
15873 	 * during execution last time, we should post an ereport with
15874 	 * driver-assessment of the value "retry".
15875 	 * For partial DMA, request sense and STATUS_QFULL, there are no
15876 	 * hardware errors, we bypass ereport posting.
15877 	 */
15878 	if (failure_code != 0) {
15879 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15880 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15881 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15882 		}
15883 	}
15884 
15885 	/*
15886 	 * If we were given a zero timeout, we must attempt to retry the
15887 	 * command immediately (ie, without a delay).
15888 	 */
15889 	if (retry_delay == 0) {
15890 		/*
15891 		 * Check some limiting conditions to see if we can actually
15892 		 * do the immediate retry.  If we cannot, then we must
15893 		 * fall back to queueing up a delayed retry.
15894 		 */
15895 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15896 			/*
15897 			 * We are at the throttle limit for the target,
15898 			 * fall back to delayed retry.
15899 			 */
15900 			retry_delay = un->un_busy_timeout;
15901 			statp = kstat_waitq_enter;
15902 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15903 			    "sd_retry_command: immed. retry hit "
15904 			    "throttle!\n");
15905 		} else {
15906 			/*
15907 			 * We're clear to proceed with the immediate retry.
15908 			 * First call the user-provided function (if any)
15909 			 */
15910 			if (user_funcp != NULL) {
15911 				(*user_funcp)(un, bp, user_arg,
15912 				    SD_IMMEDIATE_RETRY_ISSUED);
15913 #ifdef __lock_lint
15914 				sd_print_incomplete_msg(un, bp, user_arg,
15915 				    SD_IMMEDIATE_RETRY_ISSUED);
15916 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15917 				    SD_IMMEDIATE_RETRY_ISSUED);
15918 				sd_print_sense_failed_msg(un, bp, user_arg,
15919 				    SD_IMMEDIATE_RETRY_ISSUED);
15920 #endif
15921 			}
15922 
15923 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15924 			    "sd_retry_command: issuing immediate retry\n");
15925 
15926 			/*
15927 			 * Call sd_start_cmds() to transport the command to
15928 			 * the target.
15929 			 */
15930 			sd_start_cmds(un, bp);
15931 
15932 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15933 			    "sd_retry_command exit\n");
15934 			return;
15935 		}
15936 	}
15937 
15938 	/*
15939 	 * Set up to retry the command after a delay.
15940 	 * First call the user-provided function (if any)
15941 	 */
15942 	if (user_funcp != NULL) {
15943 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15944 	}
15945 
15946 	sd_set_retry_bp(un, bp, retry_delay, statp);
15947 
15948 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15949 	return;
15950 
15951 fail_command:
15952 
15953 	if (user_funcp != NULL) {
15954 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15955 	}
15956 
15957 fail_command_no_log:
15958 
15959 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15960 	    "sd_retry_command: returning failed command\n");
15961 
15962 	sd_return_failed_command(un, bp, failure_code);
15963 
15964 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15965 }
15966 
15967 
15968 /*
15969  *    Function: sd_set_retry_bp
15970  *
15971  * Description: Set up the given bp for retry.
15972  *
15973  *   Arguments: un - ptr to associated softstate
15974  *		bp - ptr to buf(9S) for the command
15975  *		retry_delay - time interval before issuing retry (may be 0)
15976  *		statp - optional pointer to kstat function
15977  *
15978  *     Context: May be called under interrupt context
15979  */
15980 
15981 static void
15982 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15983 	void (*statp)(kstat_io_t *))
15984 {
15985 	ASSERT(un != NULL);
15986 	ASSERT(mutex_owned(SD_MUTEX(un)));
15987 	ASSERT(bp != NULL);
15988 
15989 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15990 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15991 
15992 	/*
15993 	 * Indicate that the command is being retried. This will not allow any
15994 	 * other commands on the wait queue to be transported to the target
15995 	 * until this command has been completed (success or failure). The
15996 	 * "retry command" is not transported to the target until the given
15997 	 * time delay expires, unless the user specified a 0 retry_delay.
15998 	 *
15999 	 * Note: the timeout(9F) callback routine is what actually calls
16000 	 * sd_start_cmds() to transport the command, with the exception of a
16001 	 * zero retry_delay. The only current implementor of a zero retry delay
16002 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
16003 	 */
16004 	if (un->un_retry_bp == NULL) {
16005 		ASSERT(un->un_retry_statp == NULL);
16006 		un->un_retry_bp = bp;
16007 
16008 		/*
16009 		 * If the user has not specified a delay the command should
16010 		 * be queued and no timeout should be scheduled.
16011 		 */
16012 		if (retry_delay == 0) {
16013 			/*
16014 			 * Save the kstat pointer that will be used in the
16015 			 * call to SD_UPDATE_KSTATS() below, so that
16016 			 * sd_start_cmds() can correctly decrement the waitq
16017 			 * count when it is time to transport this command.
16018 			 */
16019 			un->un_retry_statp = statp;
16020 			goto done;
16021 		}
16022 	}
16023 
16024 	if (un->un_retry_bp == bp) {
16025 		/*
16026 		 * Save the kstat pointer that will be used in the call to
16027 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16028 		 * correctly decrement the waitq count when it is time to
16029 		 * transport this command.
16030 		 */
16031 		un->un_retry_statp = statp;
16032 
16033 		/*
16034 		 * Schedule a timeout if:
16035 		 *   1) The user has specified a delay.
16036 		 *   2) There is not a START_STOP_UNIT callback pending.
16037 		 *
16038 		 * If no delay has been specified, then it is up to the caller
16039 		 * to ensure that IO processing continues without stalling.
16040 		 * Effectively, this means that the caller will issue the
16041 		 * required call to sd_start_cmds(). The START_STOP_UNIT
16042 		 * callback does this after the START STOP UNIT command has
16043 		 * completed. In either of these cases we should not schedule
16044 		 * a timeout callback here.  Also don't schedule the timeout if
16045 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16046 		 */
16047 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16048 		    (un->un_direct_priority_timeid == NULL)) {
16049 			un->un_retry_timeid =
16050 			    timeout(sd_start_retry_command, un, retry_delay);
16051 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16052 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
16053 			    " bp:0x%p un_retry_timeid:0x%p\n",
16054 			    un, bp, un->un_retry_timeid);
16055 		}
16056 	} else {
16057 		/*
16058 		 * We only get in here if there is already another command
16059 		 * waiting to be retried.  In this case, we just put the
16060 		 * given command onto the wait queue, so it can be transported
16061 		 * after the current retry command has completed.
16062 		 *
16063 		 * Also we have to make sure that if the command at the head
16064 		 * of the wait queue is the un_failfast_bp, that we do not
16065 		 * put ahead of it any other commands that are to be retried.
16066 		 */
16067 		if ((un->un_failfast_bp != NULL) &&
16068 		    (un->un_failfast_bp == un->un_waitq_headp)) {
16069 			/*
16070 			 * Enqueue this command AFTER the first command on
16071 			 * the wait queue (which is also un_failfast_bp).
16072 			 */
16073 			bp->av_forw = un->un_waitq_headp->av_forw;
16074 			un->un_waitq_headp->av_forw = bp;
16075 			if (un->un_waitq_headp == un->un_waitq_tailp) {
16076 				un->un_waitq_tailp = bp;
16077 			}
16078 		} else {
16079 			/* Enqueue this command at the head of the waitq. */
16080 			bp->av_forw = un->un_waitq_headp;
16081 			un->un_waitq_headp = bp;
16082 			if (un->un_waitq_tailp == NULL) {
16083 				un->un_waitq_tailp = bp;
16084 			}
16085 		}
16086 
16087 		if (statp == NULL) {
16088 			statp = kstat_waitq_enter;
16089 		}
16090 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16091 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16092 	}
16093 
16094 done:
16095 	if (statp != NULL) {
16096 		SD_UPDATE_KSTATS(un, statp, bp);
16097 	}
16098 
16099 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16100 	    "sd_set_retry_bp: exit un:0x%p\n", un);
16101 }
16102 
16103 
16104 /*
16105  *    Function: sd_start_retry_command
16106  *
16107  * Description: Start the command that has been waiting on the target's
16108  *		retry queue.  Called from timeout(9F) context after the
16109  *		retry delay interval has expired.
16110  *
16111  *   Arguments: arg - pointer to associated softstate for the device.
16112  *
16113  *     Context: timeout(9F) thread context.  May not sleep.
16114  */
16115 
16116 static void
16117 sd_start_retry_command(void *arg)
16118 {
16119 	struct sd_lun *un = arg;
16120 
16121 	ASSERT(un != NULL);
16122 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16123 
16124 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16125 	    "sd_start_retry_command: entry\n");
16126 
16127 	mutex_enter(SD_MUTEX(un));
16128 
16129 	un->un_retry_timeid = NULL;
16130 
16131 	if (un->un_retry_bp != NULL) {
16132 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16133 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16134 		    un, un->un_retry_bp);
16135 		sd_start_cmds(un, un->un_retry_bp);
16136 	}
16137 
16138 	mutex_exit(SD_MUTEX(un));
16139 
16140 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16141 	    "sd_start_retry_command: exit\n");
16142 }
16143 
16144 /*
16145  *    Function: sd_rmw_msg_print_handler
16146  *
16147  * Description: If RMW mode is enabled and warning message is triggered
16148  *              print I/O count during a fixed interval.
16149  *
16150  *   Arguments: arg - pointer to associated softstate for the device.
16151  *
16152  *     Context: timeout(9F) thread context. May not sleep.
16153  */
16154 static void
16155 sd_rmw_msg_print_handler(void *arg)
16156 {
16157 	struct sd_lun *un = arg;
16158 
16159 	ASSERT(un != NULL);
16160 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16161 
16162 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16163 	    "sd_rmw_msg_print_handler: entry\n");
16164 
16165 	mutex_enter(SD_MUTEX(un));
16166 
16167 	if (un->un_rmw_incre_count > 0) {
16168 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16169 		    "%"PRIu64" I/O requests are not aligned with %d disk "
16170 		    "sector size in %ld seconds. They are handled through "
16171 		    "Read Modify Write but the performance is very low!\n",
16172 		    un->un_rmw_incre_count, un->un_tgt_blocksize,
16173 		    drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16174 		un->un_rmw_incre_count = 0;
16175 		un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16176 		    un, SD_RMW_MSG_PRINT_TIMEOUT);
16177 	} else {
16178 		un->un_rmw_msg_timeid = NULL;
16179 	}
16180 
16181 	mutex_exit(SD_MUTEX(un));
16182 
16183 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16184 	    "sd_rmw_msg_print_handler: exit\n");
16185 }
16186 
16187 /*
16188  *    Function: sd_start_direct_priority_command
16189  *
16190  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16191  *		received TRAN_BUSY when we called scsi_transport() to send it
16192  *		to the underlying HBA. This function is called from timeout(9F)
16193  *		context after the delay interval has expired.
16194  *
16195  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16196  *
16197  *     Context: timeout(9F) thread context.  May not sleep.
16198  */
16199 
16200 static void
16201 sd_start_direct_priority_command(void *arg)
16202 {
16203 	struct buf	*priority_bp = arg;
16204 	struct sd_lun	*un;
16205 
16206 	ASSERT(priority_bp != NULL);
16207 	un = SD_GET_UN(priority_bp);
16208 	ASSERT(un != NULL);
16209 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16210 
16211 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16212 	    "sd_start_direct_priority_command: entry\n");
16213 
16214 	mutex_enter(SD_MUTEX(un));
16215 	un->un_direct_priority_timeid = NULL;
16216 	sd_start_cmds(un, priority_bp);
16217 	mutex_exit(SD_MUTEX(un));
16218 
16219 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16220 	    "sd_start_direct_priority_command: exit\n");
16221 }
16222 
16223 
16224 /*
16225  *    Function: sd_send_request_sense_command
16226  *
16227  * Description: Sends a REQUEST SENSE command to the target
16228  *
16229  *     Context: May be called from interrupt context.
16230  */
16231 
16232 static void
16233 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16234 	struct scsi_pkt *pktp)
16235 {
16236 	ASSERT(bp != NULL);
16237 	ASSERT(un != NULL);
16238 	ASSERT(mutex_owned(SD_MUTEX(un)));
16239 
16240 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16241 	    "entry: buf:0x%p\n", bp);
16242 
16243 	/*
16244 	 * If we are syncing or dumping, then fail the command to avoid a
16245 	 * recursive callback into scsi_transport(). Also fail the command
16246 	 * if we are suspended (legacy behavior).
16247 	 */
16248 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16249 	    (un->un_state == SD_STATE_DUMPING)) {
16250 		sd_return_failed_command(un, bp, EIO);
16251 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16252 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
16253 		return;
16254 	}
16255 
16256 	/*
16257 	 * Retry the failed command and don't issue the request sense if:
16258 	 *    1) the sense buf is busy
16259 	 *    2) we have 1 or more outstanding commands on the target
16260 	 *    (the sense data will be cleared or invalidated any way)
16261 	 *
16262 	 * Note: There could be an issue with not checking a retry limit here,
16263 	 * the problem is determining which retry limit to check.
16264 	 */
16265 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16266 		/* Don't retry if the command is flagged as non-retryable */
16267 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16268 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16269 			    NULL, NULL, 0, un->un_busy_timeout,
16270 			    kstat_waitq_enter);
16271 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16272 			    "sd_send_request_sense_command: "
16273 			    "at full throttle, retrying exit\n");
16274 		} else {
16275 			sd_return_failed_command(un, bp, EIO);
16276 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16277 			    "sd_send_request_sense_command: "
16278 			    "at full throttle, non-retryable exit\n");
16279 		}
16280 		return;
16281 	}
16282 
16283 	sd_mark_rqs_busy(un, bp);
16284 	sd_start_cmds(un, un->un_rqs_bp);
16285 
16286 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16287 	    "sd_send_request_sense_command: exit\n");
16288 }
16289 
16290 
16291 /*
16292  *    Function: sd_mark_rqs_busy
16293  *
16294  * Description: Indicate that the request sense bp for this instance is
16295  *		in use.
16296  *
16297  *     Context: May be called under interrupt context
16298  */
16299 
16300 static void
16301 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16302 {
16303 	struct sd_xbuf	*sense_xp;
16304 
16305 	ASSERT(un != NULL);
16306 	ASSERT(bp != NULL);
16307 	ASSERT(mutex_owned(SD_MUTEX(un)));
16308 	ASSERT(un->un_sense_isbusy == 0);
16309 
16310 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16311 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16312 
16313 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16314 	ASSERT(sense_xp != NULL);
16315 
16316 	SD_INFO(SD_LOG_IO, un,
16317 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16318 
16319 	ASSERT(sense_xp->xb_pktp != NULL);
16320 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16321 	    == (FLAG_SENSING | FLAG_HEAD));
16322 
16323 	un->un_sense_isbusy = 1;
16324 	un->un_rqs_bp->b_resid = 0;
16325 	sense_xp->xb_pktp->pkt_resid  = 0;
16326 	sense_xp->xb_pktp->pkt_reason = 0;
16327 
16328 	/* So we can get back the bp at interrupt time! */
16329 	sense_xp->xb_sense_bp = bp;
16330 
16331 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16332 
16333 	/*
16334 	 * Mark this buf as awaiting sense data. (This is already set in
16335 	 * the pkt_flags for the RQS packet.)
16336 	 */
16337 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16338 
16339 	/* Request sense down same path */
16340 	if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16341 	    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16342 		sense_xp->xb_pktp->pkt_path_instance =
16343 		    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16344 
16345 	sense_xp->xb_retry_count	= 0;
16346 	sense_xp->xb_victim_retry_count = 0;
16347 	sense_xp->xb_ua_retry_count	= 0;
16348 	sense_xp->xb_nr_retry_count 	= 0;
16349 	sense_xp->xb_dma_resid  = 0;
16350 
16351 	/* Clean up the fields for auto-request sense */
16352 	sense_xp->xb_sense_status = 0;
16353 	sense_xp->xb_sense_state  = 0;
16354 	sense_xp->xb_sense_resid  = 0;
16355 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16356 
16357 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16358 }
16359 
16360 
16361 /*
16362  *    Function: sd_mark_rqs_idle
16363  *
16364  * Description: SD_MUTEX must be held continuously through this routine
16365  *		to prevent reuse of the rqs struct before the caller can
16366  *		complete it's processing.
16367  *
16368  * Return Code: Pointer to the RQS buf
16369  *
16370  *     Context: May be called under interrupt context
16371  */
16372 
16373 static struct buf *
16374 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16375 {
16376 	struct buf *bp;
16377 	ASSERT(un != NULL);
16378 	ASSERT(sense_xp != NULL);
16379 	ASSERT(mutex_owned(SD_MUTEX(un)));
16380 	ASSERT(un->un_sense_isbusy != 0);
16381 
16382 	un->un_sense_isbusy = 0;
16383 	bp = sense_xp->xb_sense_bp;
16384 	sense_xp->xb_sense_bp = NULL;
16385 
16386 	/* This pkt is no longer interested in getting sense data */
16387 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16388 
16389 	return (bp);
16390 }
16391 
16392 
16393 
16394 /*
16395  *    Function: sd_alloc_rqs
16396  *
16397  * Description: Set up the unit to receive auto request sense data
16398  *
16399  * Return Code: DDI_SUCCESS or DDI_FAILURE
16400  *
16401  *     Context: Called under attach(9E) context
16402  */
16403 
16404 static int
16405 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16406 {
16407 	struct sd_xbuf *xp;
16408 
16409 	ASSERT(un != NULL);
16410 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16411 	ASSERT(un->un_rqs_bp == NULL);
16412 	ASSERT(un->un_rqs_pktp == NULL);
16413 
16414 	/*
16415 	 * First allocate the required buf and scsi_pkt structs, then set up
16416 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16417 	 */
16418 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16419 	    MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16420 	if (un->un_rqs_bp == NULL) {
16421 		return (DDI_FAILURE);
16422 	}
16423 
16424 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16425 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16426 
16427 	if (un->un_rqs_pktp == NULL) {
16428 		sd_free_rqs(un);
16429 		return (DDI_FAILURE);
16430 	}
16431 
16432 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16433 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16434 	    SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16435 
16436 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16437 
16438 	/* Set up the other needed members in the ARQ scsi_pkt. */
16439 	un->un_rqs_pktp->pkt_comp   = sdintr;
16440 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16441 	un->un_rqs_pktp->pkt_flags |=
16442 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16443 
16444 	/*
16445 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16446 	 * provide any intpkt, destroypkt routines as we take care of
16447 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16448 	 */
16449 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16450 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16451 	xp->xb_pktp = un->un_rqs_pktp;
16452 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16453 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16454 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16455 
16456 	/*
16457 	 * Save the pointer to the request sense private bp so it can
16458 	 * be retrieved in sdintr.
16459 	 */
16460 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16461 	ASSERT(un->un_rqs_bp->b_private == xp);
16462 
16463 	/*
16464 	 * See if the HBA supports auto-request sense for the specified
16465 	 * target/lun. If it does, then try to enable it (if not already
16466 	 * enabled).
16467 	 *
16468 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16469 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16470 	 * return success.  However, in both of these cases ARQ is always
16471 	 * enabled and scsi_ifgetcap will always return true. The best approach
16472 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16473 	 *
16474 	 * The 3rd case is the HBA (adp) always return enabled on
16475 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16476 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16477 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16478 	 */
16479 
16480 	if (un->un_f_is_fibre == TRUE) {
16481 		un->un_f_arq_enabled = TRUE;
16482 	} else {
16483 #if defined(__i386) || defined(__amd64)
16484 		/*
16485 		 * Circumvent the Adaptec bug, remove this code when
16486 		 * the bug is fixed
16487 		 */
16488 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16489 #endif
16490 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16491 		case 0:
16492 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16493 			    "sd_alloc_rqs: HBA supports ARQ\n");
16494 			/*
16495 			 * ARQ is supported by this HBA but currently is not
16496 			 * enabled. Attempt to enable it and if successful then
16497 			 * mark this instance as ARQ enabled.
16498 			 */
16499 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16500 			    == 1) {
16501 				/* Successfully enabled ARQ in the HBA */
16502 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16503 				    "sd_alloc_rqs: ARQ enabled\n");
16504 				un->un_f_arq_enabled = TRUE;
16505 			} else {
16506 				/* Could not enable ARQ in the HBA */
16507 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16508 				    "sd_alloc_rqs: failed ARQ enable\n");
16509 				un->un_f_arq_enabled = FALSE;
16510 			}
16511 			break;
16512 		case 1:
16513 			/*
16514 			 * ARQ is supported by this HBA and is already enabled.
16515 			 * Just mark ARQ as enabled for this instance.
16516 			 */
16517 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16518 			    "sd_alloc_rqs: ARQ already enabled\n");
16519 			un->un_f_arq_enabled = TRUE;
16520 			break;
16521 		default:
16522 			/*
16523 			 * ARQ is not supported by this HBA; disable it for this
16524 			 * instance.
16525 			 */
16526 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16527 			    "sd_alloc_rqs: HBA does not support ARQ\n");
16528 			un->un_f_arq_enabled = FALSE;
16529 			break;
16530 		}
16531 	}
16532 
16533 	return (DDI_SUCCESS);
16534 }
16535 
16536 
16537 /*
16538  *    Function: sd_free_rqs
16539  *
16540  * Description: Cleanup for the pre-instance RQS command.
16541  *
16542  *     Context: Kernel thread context
16543  */
16544 
16545 static void
16546 sd_free_rqs(struct sd_lun *un)
16547 {
16548 	ASSERT(un != NULL);
16549 
16550 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16551 
16552 	/*
16553 	 * If consistent memory is bound to a scsi_pkt, the pkt
16554 	 * has to be destroyed *before* freeing the consistent memory.
16555 	 * Don't change the sequence of this operations.
16556 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16557 	 * after it was freed in scsi_free_consistent_buf().
16558 	 */
16559 	if (un->un_rqs_pktp != NULL) {
16560 		scsi_destroy_pkt(un->un_rqs_pktp);
16561 		un->un_rqs_pktp = NULL;
16562 	}
16563 
16564 	if (un->un_rqs_bp != NULL) {
16565 		struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16566 		if (xp != NULL) {
16567 			kmem_free(xp, sizeof (struct sd_xbuf));
16568 		}
16569 		scsi_free_consistent_buf(un->un_rqs_bp);
16570 		un->un_rqs_bp = NULL;
16571 	}
16572 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16573 }
16574 
16575 
16576 
16577 /*
16578  *    Function: sd_reduce_throttle
16579  *
16580  * Description: Reduces the maximum # of outstanding commands on a
16581  *		target to the current number of outstanding commands.
16582  *		Queues a tiemout(9F) callback to restore the limit
16583  *		after a specified interval has elapsed.
16584  *		Typically used when we get a TRAN_BUSY return code
16585  *		back from scsi_transport().
16586  *
16587  *   Arguments: un - ptr to the sd_lun softstate struct
16588  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16589  *
16590  *     Context: May be called from interrupt context
16591  */
16592 
16593 static void
16594 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16595 {
16596 	ASSERT(un != NULL);
16597 	ASSERT(mutex_owned(SD_MUTEX(un)));
16598 	ASSERT(un->un_ncmds_in_transport >= 0);
16599 
16600 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16601 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16602 	    un, un->un_throttle, un->un_ncmds_in_transport);
16603 
16604 	if (un->un_throttle > 1) {
16605 		if (un->un_f_use_adaptive_throttle == TRUE) {
16606 			switch (throttle_type) {
16607 			case SD_THROTTLE_TRAN_BUSY:
16608 				if (un->un_busy_throttle == 0) {
16609 					un->un_busy_throttle = un->un_throttle;
16610 				}
16611 				break;
16612 			case SD_THROTTLE_QFULL:
16613 				un->un_busy_throttle = 0;
16614 				break;
16615 			default:
16616 				ASSERT(FALSE);
16617 			}
16618 
16619 			if (un->un_ncmds_in_transport > 0) {
16620 				un->un_throttle = un->un_ncmds_in_transport;
16621 			}
16622 
16623 		} else {
16624 			if (un->un_ncmds_in_transport == 0) {
16625 				un->un_throttle = 1;
16626 			} else {
16627 				un->un_throttle = un->un_ncmds_in_transport;
16628 			}
16629 		}
16630 	}
16631 
16632 	/* Reschedule the timeout if none is currently active */
16633 	if (un->un_reset_throttle_timeid == NULL) {
16634 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16635 		    un, SD_THROTTLE_RESET_INTERVAL);
16636 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16637 		    "sd_reduce_throttle: timeout scheduled!\n");
16638 	}
16639 
16640 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16641 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16642 }
16643 
16644 
16645 
16646 /*
16647  *    Function: sd_restore_throttle
16648  *
16649  * Description: Callback function for timeout(9F).  Resets the current
16650  *		value of un->un_throttle to its default.
16651  *
16652  *   Arguments: arg - pointer to associated softstate for the device.
16653  *
16654  *     Context: May be called from interrupt context
16655  */
16656 
16657 static void
16658 sd_restore_throttle(void *arg)
16659 {
16660 	struct sd_lun	*un = arg;
16661 
16662 	ASSERT(un != NULL);
16663 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16664 
16665 	mutex_enter(SD_MUTEX(un));
16666 
16667 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16668 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16669 
16670 	un->un_reset_throttle_timeid = NULL;
16671 
16672 	if (un->un_f_use_adaptive_throttle == TRUE) {
16673 		/*
16674 		 * If un_busy_throttle is nonzero, then it contains the
16675 		 * value that un_throttle was when we got a TRAN_BUSY back
16676 		 * from scsi_transport(). We want to revert back to this
16677 		 * value.
16678 		 *
16679 		 * In the QFULL case, the throttle limit will incrementally
16680 		 * increase until it reaches max throttle.
16681 		 */
16682 		if (un->un_busy_throttle > 0) {
16683 			un->un_throttle = un->un_busy_throttle;
16684 			un->un_busy_throttle = 0;
16685 		} else {
16686 			/*
16687 			 * increase throttle by 10% open gate slowly, schedule
16688 			 * another restore if saved throttle has not been
16689 			 * reached
16690 			 */
16691 			short throttle;
16692 			if (sd_qfull_throttle_enable) {
16693 				throttle = un->un_throttle +
16694 				    max((un->un_throttle / 10), 1);
16695 				un->un_throttle =
16696 				    (throttle < un->un_saved_throttle) ?
16697 				    throttle : un->un_saved_throttle;
16698 				if (un->un_throttle < un->un_saved_throttle) {
16699 					un->un_reset_throttle_timeid =
16700 					    timeout(sd_restore_throttle,
16701 					    un,
16702 					    SD_QFULL_THROTTLE_RESET_INTERVAL);
16703 				}
16704 			}
16705 		}
16706 
16707 		/*
16708 		 * If un_throttle has fallen below the low-water mark, we
16709 		 * restore the maximum value here (and allow it to ratchet
16710 		 * down again if necessary).
16711 		 */
16712 		if (un->un_throttle < un->un_min_throttle) {
16713 			un->un_throttle = un->un_saved_throttle;
16714 		}
16715 	} else {
16716 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16717 		    "restoring limit from 0x%x to 0x%x\n",
16718 		    un->un_throttle, un->un_saved_throttle);
16719 		un->un_throttle = un->un_saved_throttle;
16720 	}
16721 
16722 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16723 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16724 
16725 	sd_start_cmds(un, NULL);
16726 
16727 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16728 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16729 	    un, un->un_throttle);
16730 
16731 	mutex_exit(SD_MUTEX(un));
16732 
16733 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16734 }
16735 
16736 /*
16737  *    Function: sdrunout
16738  *
16739  * Description: Callback routine for scsi_init_pkt when a resource allocation
16740  *		fails.
16741  *
16742  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16743  *		soft state instance.
16744  *
16745  * Return Code: The scsi_init_pkt routine allows for the callback function to
16746  *		return a 0 indicating the callback should be rescheduled or a 1
16747  *		indicating not to reschedule. This routine always returns 1
16748  *		because the driver always provides a callback function to
16749  *		scsi_init_pkt. This results in a callback always being scheduled
16750  *		(via the scsi_init_pkt callback implementation) if a resource
16751  *		failure occurs.
16752  *
16753  *     Context: This callback function may not block or call routines that block
16754  *
16755  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16756  *		request persisting at the head of the list which cannot be
16757  *		satisfied even after multiple retries. In the future the driver
16758  *		may implement some time of maximum runout count before failing
16759  *		an I/O.
16760  */
16761 
16762 static int
16763 sdrunout(caddr_t arg)
16764 {
16765 	struct sd_lun	*un = (struct sd_lun *)arg;
16766 
16767 	ASSERT(un != NULL);
16768 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16769 
16770 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16771 
16772 	mutex_enter(SD_MUTEX(un));
16773 	sd_start_cmds(un, NULL);
16774 	mutex_exit(SD_MUTEX(un));
16775 	/*
16776 	 * This callback routine always returns 1 (i.e. do not reschedule)
16777 	 * because we always specify sdrunout as the callback handler for
16778 	 * scsi_init_pkt inside the call to sd_start_cmds.
16779 	 */
16780 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16781 	return (1);
16782 }
16783 
16784 
16785 /*
16786  *    Function: sdintr
16787  *
16788  * Description: Completion callback routine for scsi_pkt(9S) structs
16789  *		sent to the HBA driver via scsi_transport(9F).
16790  *
16791  *     Context: Interrupt context
16792  */
16793 
16794 static void
16795 sdintr(struct scsi_pkt *pktp)
16796 {
16797 	struct buf	*bp;
16798 	struct sd_xbuf	*xp;
16799 	struct sd_lun	*un;
16800 	size_t		actual_len;
16801 	sd_ssc_t	*sscp;
16802 
16803 	ASSERT(pktp != NULL);
16804 	bp = (struct buf *)pktp->pkt_private;
16805 	ASSERT(bp != NULL);
16806 	xp = SD_GET_XBUF(bp);
16807 	ASSERT(xp != NULL);
16808 	ASSERT(xp->xb_pktp != NULL);
16809 	un = SD_GET_UN(bp);
16810 	ASSERT(un != NULL);
16811 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16812 
16813 #ifdef SD_FAULT_INJECTION
16814 
16815 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16816 	/* SD FaultInjection */
16817 	sd_faultinjection(pktp);
16818 
16819 #endif /* SD_FAULT_INJECTION */
16820 
16821 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16822 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16823 
16824 	mutex_enter(SD_MUTEX(un));
16825 
16826 	ASSERT(un->un_fm_private != NULL);
16827 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16828 	ASSERT(sscp != NULL);
16829 
16830 	/* Reduce the count of the #commands currently in transport */
16831 	un->un_ncmds_in_transport--;
16832 	ASSERT(un->un_ncmds_in_transport >= 0);
16833 
16834 	/* Increment counter to indicate that the callback routine is active */
16835 	un->un_in_callback++;
16836 
16837 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16838 
16839 #ifdef	SDDEBUG
16840 	if (bp == un->un_retry_bp) {
16841 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16842 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16843 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16844 	}
16845 #endif
16846 
16847 	/*
16848 	 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16849 	 * state if needed.
16850 	 */
16851 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16852 		/* Prevent multiple console messages for the same failure. */
16853 		if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16854 			un->un_last_pkt_reason = CMD_DEV_GONE;
16855 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16856 			    "Command failed to complete...Device is gone\n");
16857 		}
16858 		if (un->un_mediastate != DKIO_DEV_GONE) {
16859 			un->un_mediastate = DKIO_DEV_GONE;
16860 			cv_broadcast(&un->un_state_cv);
16861 		}
16862 		/*
16863 		 * If the command happens to be the REQUEST SENSE command,
16864 		 * free up the rqs buf and fail the original command.
16865 		 */
16866 		if (bp == un->un_rqs_bp) {
16867 			bp = sd_mark_rqs_idle(un, xp);
16868 		}
16869 		sd_return_failed_command(un, bp, EIO);
16870 		goto exit;
16871 	}
16872 
16873 	if (pktp->pkt_state & STATE_XARQ_DONE) {
16874 		SD_TRACE(SD_LOG_COMMON, un,
16875 		    "sdintr: extra sense data received. pkt=%p\n", pktp);
16876 	}
16877 
16878 	/*
16879 	 * First see if the pkt has auto-request sense data with it....
16880 	 * Look at the packet state first so we don't take a performance
16881 	 * hit looking at the arq enabled flag unless absolutely necessary.
16882 	 */
16883 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16884 	    (un->un_f_arq_enabled == TRUE)) {
16885 		/*
16886 		 * The HBA did an auto request sense for this command so check
16887 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16888 		 * driver command that should not be retried.
16889 		 */
16890 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16891 			/*
16892 			 * Save the relevant sense info into the xp for the
16893 			 * original cmd.
16894 			 */
16895 			struct scsi_arq_status *asp;
16896 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16897 			xp->xb_sense_status =
16898 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16899 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16900 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16901 			if (pktp->pkt_state & STATE_XARQ_DONE) {
16902 				actual_len = MAX_SENSE_LENGTH -
16903 				    xp->xb_sense_resid;
16904 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16905 				    MAX_SENSE_LENGTH);
16906 			} else {
16907 				if (xp->xb_sense_resid > SENSE_LENGTH) {
16908 					actual_len = MAX_SENSE_LENGTH -
16909 					    xp->xb_sense_resid;
16910 				} else {
16911 					actual_len = SENSE_LENGTH -
16912 					    xp->xb_sense_resid;
16913 				}
16914 				if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16915 					if ((((struct uscsi_cmd *)
16916 					    (xp->xb_pktinfo))->uscsi_rqlen) >
16917 					    actual_len) {
16918 						xp->xb_sense_resid =
16919 						    (((struct uscsi_cmd *)
16920 						    (xp->xb_pktinfo))->
16921 						    uscsi_rqlen) - actual_len;
16922 					} else {
16923 						xp->xb_sense_resid = 0;
16924 					}
16925 				}
16926 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16927 				    SENSE_LENGTH);
16928 			}
16929 
16930 			/* fail the command */
16931 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16932 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16933 			sd_return_failed_command(un, bp, EIO);
16934 			goto exit;
16935 		}
16936 
16937 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16938 		/*
16939 		 * We want to either retry or fail this command, so free
16940 		 * the DMA resources here.  If we retry the command then
16941 		 * the DMA resources will be reallocated in sd_start_cmds().
16942 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16943 		 * causes the *entire* transfer to start over again from the
16944 		 * beginning of the request, even for PARTIAL chunks that
16945 		 * have already transferred successfully.
16946 		 */
16947 		if ((un->un_f_is_fibre == TRUE) &&
16948 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16949 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16950 			scsi_dmafree(pktp);
16951 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16952 		}
16953 #endif
16954 
16955 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16956 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16957 
16958 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16959 		goto exit;
16960 	}
16961 
16962 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16963 	if (pktp->pkt_flags & FLAG_SENSING)  {
16964 		/* This pktp is from the unit's REQUEST_SENSE command */
16965 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16966 		    "sdintr: sd_handle_request_sense\n");
16967 		sd_handle_request_sense(un, bp, xp, pktp);
16968 		goto exit;
16969 	}
16970 
16971 	/*
16972 	 * Check to see if the command successfully completed as requested;
16973 	 * this is the most common case (and also the hot performance path).
16974 	 *
16975 	 * Requirements for successful completion are:
16976 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16977 	 * In addition:
16978 	 * - A residual of zero indicates successful completion no matter what
16979 	 *   the command is.
16980 	 * - If the residual is not zero and the command is not a read or
16981 	 *   write, then it's still defined as successful completion. In other
16982 	 *   words, if the command is a read or write the residual must be
16983 	 *   zero for successful completion.
16984 	 * - If the residual is not zero and the command is a read or
16985 	 *   write, and it's a USCSICMD, then it's still defined as
16986 	 *   successful completion.
16987 	 */
16988 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16989 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16990 
16991 		/*
16992 		 * Since this command is returned with a good status, we
16993 		 * can reset the count for Sonoma failover.
16994 		 */
16995 		un->un_sonoma_failure_count = 0;
16996 
16997 		/*
16998 		 * Return all USCSI commands on good status
16999 		 */
17000 		if (pktp->pkt_resid == 0) {
17001 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17002 			    "sdintr: returning command for resid == 0\n");
17003 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17004 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17005 			SD_UPDATE_B_RESID(bp, pktp);
17006 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17007 			    "sdintr: returning command for resid != 0\n");
17008 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17009 			SD_UPDATE_B_RESID(bp, pktp);
17010 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17011 			    "sdintr: returning uscsi command\n");
17012 		} else {
17013 			goto not_successful;
17014 		}
17015 		sd_return_command(un, bp);
17016 
17017 		/*
17018 		 * Decrement counter to indicate that the callback routine
17019 		 * is done.
17020 		 */
17021 		un->un_in_callback--;
17022 		ASSERT(un->un_in_callback >= 0);
17023 		mutex_exit(SD_MUTEX(un));
17024 
17025 		return;
17026 	}
17027 
17028 not_successful:
17029 
17030 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
17031 	/*
17032 	 * The following is based upon knowledge of the underlying transport
17033 	 * and its use of DMA resources.  This code should be removed when
17034 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17035 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17036 	 * and sd_start_cmds().
17037 	 *
17038 	 * Free any DMA resources associated with this command if there
17039 	 * is a chance it could be retried or enqueued for later retry.
17040 	 * If we keep the DMA binding then mpxio cannot reissue the
17041 	 * command on another path whenever a path failure occurs.
17042 	 *
17043 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17044 	 * causes the *entire* transfer to start over again from the
17045 	 * beginning of the request, even for PARTIAL chunks that
17046 	 * have already transferred successfully.
17047 	 *
17048 	 * This is only done for non-uscsi commands (and also skipped for the
17049 	 * driver's internal RQS command). Also just do this for Fibre Channel
17050 	 * devices as these are the only ones that support mpxio.
17051 	 */
17052 	if ((un->un_f_is_fibre == TRUE) &&
17053 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17054 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17055 		scsi_dmafree(pktp);
17056 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17057 	}
17058 #endif
17059 
17060 	/*
17061 	 * The command did not successfully complete as requested so check
17062 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17063 	 * driver command that should not be retried so just return. If
17064 	 * FLAG_DIAGNOSE is not set the error will be processed below.
17065 	 */
17066 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17067 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17068 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17069 		/*
17070 		 * Issue a request sense if a check condition caused the error
17071 		 * (we handle the auto request sense case above), otherwise
17072 		 * just fail the command.
17073 		 */
17074 		if ((pktp->pkt_reason == CMD_CMPLT) &&
17075 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17076 			sd_send_request_sense_command(un, bp, pktp);
17077 		} else {
17078 			sd_return_failed_command(un, bp, EIO);
17079 		}
17080 		goto exit;
17081 	}
17082 
17083 	/*
17084 	 * The command did not successfully complete as requested so process
17085 	 * the error, retry, and/or attempt recovery.
17086 	 */
17087 	switch (pktp->pkt_reason) {
17088 	case CMD_CMPLT:
17089 		switch (SD_GET_PKT_STATUS(pktp)) {
17090 		case STATUS_GOOD:
17091 			/*
17092 			 * The command completed successfully with a non-zero
17093 			 * residual
17094 			 */
17095 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17096 			    "sdintr: STATUS_GOOD \n");
17097 			sd_pkt_status_good(un, bp, xp, pktp);
17098 			break;
17099 
17100 		case STATUS_CHECK:
17101 		case STATUS_TERMINATED:
17102 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17103 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17104 			sd_pkt_status_check_condition(un, bp, xp, pktp);
17105 			break;
17106 
17107 		case STATUS_BUSY:
17108 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17109 			    "sdintr: STATUS_BUSY\n");
17110 			sd_pkt_status_busy(un, bp, xp, pktp);
17111 			break;
17112 
17113 		case STATUS_RESERVATION_CONFLICT:
17114 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17115 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
17116 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17117 			break;
17118 
17119 		case STATUS_QFULL:
17120 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17121 			    "sdintr: STATUS_QFULL\n");
17122 			sd_pkt_status_qfull(un, bp, xp, pktp);
17123 			break;
17124 
17125 		case STATUS_MET:
17126 		case STATUS_INTERMEDIATE:
17127 		case STATUS_SCSI2:
17128 		case STATUS_INTERMEDIATE_MET:
17129 		case STATUS_ACA_ACTIVE:
17130 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17131 			    "Unexpected SCSI status received: 0x%x\n",
17132 			    SD_GET_PKT_STATUS(pktp));
17133 			/*
17134 			 * Mark the ssc_flags when detected invalid status
17135 			 * code for non-USCSI command.
17136 			 */
17137 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17138 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17139 				    0, "stat-code");
17140 			}
17141 			sd_return_failed_command(un, bp, EIO);
17142 			break;
17143 
17144 		default:
17145 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17146 			    "Invalid SCSI status received: 0x%x\n",
17147 			    SD_GET_PKT_STATUS(pktp));
17148 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17149 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17150 				    0, "stat-code");
17151 			}
17152 			sd_return_failed_command(un, bp, EIO);
17153 			break;
17154 
17155 		}
17156 		break;
17157 
17158 	case CMD_INCOMPLETE:
17159 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17160 		    "sdintr:  CMD_INCOMPLETE\n");
17161 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17162 		break;
17163 	case CMD_TRAN_ERR:
17164 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17165 		    "sdintr: CMD_TRAN_ERR\n");
17166 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17167 		break;
17168 	case CMD_RESET:
17169 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17170 		    "sdintr: CMD_RESET \n");
17171 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17172 		break;
17173 	case CMD_ABORTED:
17174 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17175 		    "sdintr: CMD_ABORTED \n");
17176 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17177 		break;
17178 	case CMD_TIMEOUT:
17179 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17180 		    "sdintr: CMD_TIMEOUT\n");
17181 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17182 		break;
17183 	case CMD_UNX_BUS_FREE:
17184 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17185 		    "sdintr: CMD_UNX_BUS_FREE \n");
17186 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17187 		break;
17188 	case CMD_TAG_REJECT:
17189 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17190 		    "sdintr: CMD_TAG_REJECT\n");
17191 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17192 		break;
17193 	default:
17194 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17195 		    "sdintr: default\n");
17196 		/*
17197 		 * Mark the ssc_flags for detecting invliad pkt_reason.
17198 		 */
17199 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17200 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17201 			    0, "pkt-reason");
17202 		}
17203 		sd_pkt_reason_default(un, bp, xp, pktp);
17204 		break;
17205 	}
17206 
17207 exit:
17208 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17209 
17210 	/* Decrement counter to indicate that the callback routine is done. */
17211 	un->un_in_callback--;
17212 	ASSERT(un->un_in_callback >= 0);
17213 
17214 	/*
17215 	 * At this point, the pkt has been dispatched, ie, it is either
17216 	 * being re-tried or has been returned to its caller and should
17217 	 * not be referenced.
17218 	 */
17219 
17220 	mutex_exit(SD_MUTEX(un));
17221 }
17222 
17223 
17224 /*
17225  *    Function: sd_print_incomplete_msg
17226  *
17227  * Description: Prints the error message for a CMD_INCOMPLETE error.
17228  *
17229  *   Arguments: un - ptr to associated softstate for the device.
17230  *		bp - ptr to the buf(9S) for the command.
17231  *		arg - message string ptr
17232  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17233  *			or SD_NO_RETRY_ISSUED.
17234  *
17235  *     Context: May be called under interrupt context
17236  */
17237 
17238 static void
17239 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17240 {
17241 	struct scsi_pkt	*pktp;
17242 	char	*msgp;
17243 	char	*cmdp = arg;
17244 
17245 	ASSERT(un != NULL);
17246 	ASSERT(mutex_owned(SD_MUTEX(un)));
17247 	ASSERT(bp != NULL);
17248 	ASSERT(arg != NULL);
17249 	pktp = SD_GET_PKTP(bp);
17250 	ASSERT(pktp != NULL);
17251 
17252 	switch (code) {
17253 	case SD_DELAYED_RETRY_ISSUED:
17254 	case SD_IMMEDIATE_RETRY_ISSUED:
17255 		msgp = "retrying";
17256 		break;
17257 	case SD_NO_RETRY_ISSUED:
17258 	default:
17259 		msgp = "giving up";
17260 		break;
17261 	}
17262 
17263 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17264 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17265 		    "incomplete %s- %s\n", cmdp, msgp);
17266 	}
17267 }
17268 
17269 
17270 
17271 /*
17272  *    Function: sd_pkt_status_good
17273  *
17274  * Description: Processing for a STATUS_GOOD code in pkt_status.
17275  *
17276  *     Context: May be called under interrupt context
17277  */
17278 
17279 static void
17280 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17281 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17282 {
17283 	char	*cmdp;
17284 
17285 	ASSERT(un != NULL);
17286 	ASSERT(mutex_owned(SD_MUTEX(un)));
17287 	ASSERT(bp != NULL);
17288 	ASSERT(xp != NULL);
17289 	ASSERT(pktp != NULL);
17290 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17291 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17292 	ASSERT(pktp->pkt_resid != 0);
17293 
17294 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17295 
17296 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17297 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17298 	case SCMD_READ:
17299 		cmdp = "read";
17300 		break;
17301 	case SCMD_WRITE:
17302 		cmdp = "write";
17303 		break;
17304 	default:
17305 		SD_UPDATE_B_RESID(bp, pktp);
17306 		sd_return_command(un, bp);
17307 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17308 		return;
17309 	}
17310 
17311 	/*
17312 	 * See if we can retry the read/write, preferrably immediately.
17313 	 * If retries are exhaused, then sd_retry_command() will update
17314 	 * the b_resid count.
17315 	 */
17316 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17317 	    cmdp, EIO, (clock_t)0, NULL);
17318 
17319 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17320 }
17321 
17322 
17323 
17324 
17325 
17326 /*
17327  *    Function: sd_handle_request_sense
17328  *
17329  * Description: Processing for non-auto Request Sense command.
17330  *
17331  *   Arguments: un - ptr to associated softstate
17332  *		sense_bp - ptr to buf(9S) for the RQS command
17333  *		sense_xp - ptr to the sd_xbuf for the RQS command
17334  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17335  *
17336  *     Context: May be called under interrupt context
17337  */
17338 
17339 static void
17340 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17341 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17342 {
17343 	struct buf	*cmd_bp;	/* buf for the original command */
17344 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17345 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17346 	size_t		actual_len;	/* actual sense data length */
17347 
17348 	ASSERT(un != NULL);
17349 	ASSERT(mutex_owned(SD_MUTEX(un)));
17350 	ASSERT(sense_bp != NULL);
17351 	ASSERT(sense_xp != NULL);
17352 	ASSERT(sense_pktp != NULL);
17353 
17354 	/*
17355 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17356 	 * RQS command and not the original command.
17357 	 */
17358 	ASSERT(sense_pktp == un->un_rqs_pktp);
17359 	ASSERT(sense_bp   == un->un_rqs_bp);
17360 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17361 	    (FLAG_SENSING | FLAG_HEAD));
17362 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17363 	    FLAG_SENSING) == FLAG_SENSING);
17364 
17365 	/* These are the bp, xp, and pktp for the original command */
17366 	cmd_bp = sense_xp->xb_sense_bp;
17367 	cmd_xp = SD_GET_XBUF(cmd_bp);
17368 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17369 
17370 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17371 		/*
17372 		 * The REQUEST SENSE command failed.  Release the REQUEST
17373 		 * SENSE command for re-use, get back the bp for the original
17374 		 * command, and attempt to re-try the original command if
17375 		 * FLAG_DIAGNOSE is not set in the original packet.
17376 		 */
17377 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17378 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17379 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17380 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17381 			    NULL, NULL, EIO, (clock_t)0, NULL);
17382 			return;
17383 		}
17384 	}
17385 
17386 	/*
17387 	 * Save the relevant sense info into the xp for the original cmd.
17388 	 *
17389 	 * Note: if the request sense failed the state info will be zero
17390 	 * as set in sd_mark_rqs_busy()
17391 	 */
17392 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17393 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17394 	actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17395 	if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17396 	    (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17397 	    SENSE_LENGTH)) {
17398 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17399 		    MAX_SENSE_LENGTH);
17400 		cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17401 	} else {
17402 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17403 		    SENSE_LENGTH);
17404 		if (actual_len < SENSE_LENGTH) {
17405 			cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17406 		} else {
17407 			cmd_xp->xb_sense_resid = 0;
17408 		}
17409 	}
17410 
17411 	/*
17412 	 *  Free up the RQS command....
17413 	 *  NOTE:
17414 	 *	Must do this BEFORE calling sd_validate_sense_data!
17415 	 *	sd_validate_sense_data may return the original command in
17416 	 *	which case the pkt will be freed and the flags can no
17417 	 *	longer be touched.
17418 	 *	SD_MUTEX is held through this process until the command
17419 	 *	is dispatched based upon the sense data, so there are
17420 	 *	no race conditions.
17421 	 */
17422 	(void) sd_mark_rqs_idle(un, sense_xp);
17423 
17424 	/*
17425 	 * For a retryable command see if we have valid sense data, if so then
17426 	 * turn it over to sd_decode_sense() to figure out the right course of
17427 	 * action. Just fail a non-retryable command.
17428 	 */
17429 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17430 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17431 		    SD_SENSE_DATA_IS_VALID) {
17432 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17433 		}
17434 	} else {
17435 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17436 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17437 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17438 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17439 		sd_return_failed_command(un, cmd_bp, EIO);
17440 	}
17441 }
17442 
17443 
17444 
17445 
17446 /*
17447  *    Function: sd_handle_auto_request_sense
17448  *
17449  * Description: Processing for auto-request sense information.
17450  *
17451  *   Arguments: un - ptr to associated softstate
17452  *		bp - ptr to buf(9S) for the command
17453  *		xp - ptr to the sd_xbuf for the command
17454  *		pktp - ptr to the scsi_pkt(9S) for the command
17455  *
17456  *     Context: May be called under interrupt context
17457  */
17458 
17459 static void
17460 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17461 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17462 {
17463 	struct scsi_arq_status *asp;
17464 	size_t actual_len;
17465 
17466 	ASSERT(un != NULL);
17467 	ASSERT(mutex_owned(SD_MUTEX(un)));
17468 	ASSERT(bp != NULL);
17469 	ASSERT(xp != NULL);
17470 	ASSERT(pktp != NULL);
17471 	ASSERT(pktp != un->un_rqs_pktp);
17472 	ASSERT(bp   != un->un_rqs_bp);
17473 
17474 	/*
17475 	 * For auto-request sense, we get a scsi_arq_status back from
17476 	 * the HBA, with the sense data in the sts_sensedata member.
17477 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17478 	 */
17479 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17480 
17481 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17482 		/*
17483 		 * The auto REQUEST SENSE failed; see if we can re-try
17484 		 * the original command.
17485 		 */
17486 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17487 		    "auto request sense failed (reason=%s)\n",
17488 		    scsi_rname(asp->sts_rqpkt_reason));
17489 
17490 		sd_reset_target(un, pktp);
17491 
17492 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17493 		    NULL, NULL, EIO, (clock_t)0, NULL);
17494 		return;
17495 	}
17496 
17497 	/* Save the relevant sense info into the xp for the original cmd. */
17498 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17499 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17500 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17501 	if (xp->xb_sense_state & STATE_XARQ_DONE) {
17502 		actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17503 		bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17504 		    MAX_SENSE_LENGTH);
17505 	} else {
17506 		if (xp->xb_sense_resid > SENSE_LENGTH) {
17507 			actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17508 		} else {
17509 			actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17510 		}
17511 		if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17512 			if ((((struct uscsi_cmd *)
17513 			    (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17514 				xp->xb_sense_resid = (((struct uscsi_cmd *)
17515 				    (xp->xb_pktinfo))->uscsi_rqlen) -
17516 				    actual_len;
17517 			} else {
17518 				xp->xb_sense_resid = 0;
17519 			}
17520 		}
17521 		bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17522 	}
17523 
17524 	/*
17525 	 * See if we have valid sense data, if so then turn it over to
17526 	 * sd_decode_sense() to figure out the right course of action.
17527 	 */
17528 	if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17529 	    SD_SENSE_DATA_IS_VALID) {
17530 		sd_decode_sense(un, bp, xp, pktp);
17531 	}
17532 }
17533 
17534 
17535 /*
17536  *    Function: sd_print_sense_failed_msg
17537  *
17538  * Description: Print log message when RQS has failed.
17539  *
17540  *   Arguments: un - ptr to associated softstate
17541  *		bp - ptr to buf(9S) for the command
17542  *		arg - generic message string ptr
17543  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17544  *			or SD_NO_RETRY_ISSUED
17545  *
17546  *     Context: May be called from interrupt context
17547  */
17548 
17549 static void
17550 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17551 	int code)
17552 {
17553 	char	*msgp = arg;
17554 
17555 	ASSERT(un != NULL);
17556 	ASSERT(mutex_owned(SD_MUTEX(un)));
17557 	ASSERT(bp != NULL);
17558 
17559 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17560 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17561 	}
17562 }
17563 
17564 
17565 /*
17566  *    Function: sd_validate_sense_data
17567  *
17568  * Description: Check the given sense data for validity.
17569  *		If the sense data is not valid, the command will
17570  *		be either failed or retried!
17571  *
17572  * Return Code: SD_SENSE_DATA_IS_INVALID
17573  *		SD_SENSE_DATA_IS_VALID
17574  *
17575  *     Context: May be called from interrupt context
17576  */
17577 
17578 static int
17579 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17580 	size_t actual_len)
17581 {
17582 	struct scsi_extended_sense *esp;
17583 	struct	scsi_pkt *pktp;
17584 	char	*msgp = NULL;
17585 	sd_ssc_t *sscp;
17586 
17587 	ASSERT(un != NULL);
17588 	ASSERT(mutex_owned(SD_MUTEX(un)));
17589 	ASSERT(bp != NULL);
17590 	ASSERT(bp != un->un_rqs_bp);
17591 	ASSERT(xp != NULL);
17592 	ASSERT(un->un_fm_private != NULL);
17593 
17594 	pktp = SD_GET_PKTP(bp);
17595 	ASSERT(pktp != NULL);
17596 
17597 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17598 	ASSERT(sscp != NULL);
17599 
17600 	/*
17601 	 * Check the status of the RQS command (auto or manual).
17602 	 */
17603 	switch (xp->xb_sense_status & STATUS_MASK) {
17604 	case STATUS_GOOD:
17605 		break;
17606 
17607 	case STATUS_RESERVATION_CONFLICT:
17608 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17609 		return (SD_SENSE_DATA_IS_INVALID);
17610 
17611 	case STATUS_BUSY:
17612 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17613 		    "Busy Status on REQUEST SENSE\n");
17614 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17615 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17616 		return (SD_SENSE_DATA_IS_INVALID);
17617 
17618 	case STATUS_QFULL:
17619 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17620 		    "QFULL Status on REQUEST SENSE\n");
17621 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17622 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17623 		return (SD_SENSE_DATA_IS_INVALID);
17624 
17625 	case STATUS_CHECK:
17626 	case STATUS_TERMINATED:
17627 		msgp = "Check Condition on REQUEST SENSE\n";
17628 		goto sense_failed;
17629 
17630 	default:
17631 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17632 		goto sense_failed;
17633 	}
17634 
17635 	/*
17636 	 * See if we got the minimum required amount of sense data.
17637 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17638 	 * or less.
17639 	 */
17640 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17641 	    (actual_len == 0)) {
17642 		msgp = "Request Sense couldn't get sense data\n";
17643 		goto sense_failed;
17644 	}
17645 
17646 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17647 		msgp = "Not enough sense information\n";
17648 		/* Mark the ssc_flags for detecting invalid sense data */
17649 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17650 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17651 			    "sense-data");
17652 		}
17653 		goto sense_failed;
17654 	}
17655 
17656 	/*
17657 	 * We require the extended sense data
17658 	 */
17659 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17660 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17661 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17662 			static char tmp[8];
17663 			static char buf[148];
17664 			char *p = (char *)(xp->xb_sense_data);
17665 			int i;
17666 
17667 			mutex_enter(&sd_sense_mutex);
17668 			(void) strcpy(buf, "undecodable sense information:");
17669 			for (i = 0; i < actual_len; i++) {
17670 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17671 				(void) strcpy(&buf[strlen(buf)], tmp);
17672 			}
17673 			i = strlen(buf);
17674 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17675 
17676 			if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17677 				scsi_log(SD_DEVINFO(un), sd_label,
17678 				    CE_WARN, buf);
17679 			}
17680 			mutex_exit(&sd_sense_mutex);
17681 		}
17682 
17683 		/* Mark the ssc_flags for detecting invalid sense data */
17684 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17685 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17686 			    "sense-data");
17687 		}
17688 
17689 		/* Note: Legacy behavior, fail the command with no retry */
17690 		sd_return_failed_command(un, bp, EIO);
17691 		return (SD_SENSE_DATA_IS_INVALID);
17692 	}
17693 
17694 	/*
17695 	 * Check that es_code is valid (es_class concatenated with es_code
17696 	 * make up the "response code" field.  es_class will always be 7, so
17697 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17698 	 * format.
17699 	 */
17700 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17701 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17702 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17703 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17704 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17705 		/* Mark the ssc_flags for detecting invalid sense data */
17706 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17707 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17708 			    "sense-data");
17709 		}
17710 		goto sense_failed;
17711 	}
17712 
17713 	return (SD_SENSE_DATA_IS_VALID);
17714 
17715 sense_failed:
17716 	/*
17717 	 * If the request sense failed (for whatever reason), attempt
17718 	 * to retry the original command.
17719 	 */
17720 #if defined(__i386) || defined(__amd64)
17721 	/*
17722 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17723 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17724 	 * for both SCSI/FC.
17725 	 * The SD_RETRY_DELAY value need to be adjusted here
17726 	 * when SD_RETRY_DELAY change in sddef.h
17727 	 */
17728 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17729 	    sd_print_sense_failed_msg, msgp, EIO,
17730 	    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17731 #else
17732 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17733 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17734 #endif
17735 
17736 	return (SD_SENSE_DATA_IS_INVALID);
17737 }
17738 
17739 /*
17740  *    Function: sd_decode_sense
17741  *
17742  * Description: Take recovery action(s) when SCSI Sense Data is received.
17743  *
17744  *     Context: Interrupt context.
17745  */
17746 
17747 static void
17748 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17749 	struct scsi_pkt *pktp)
17750 {
17751 	uint8_t sense_key;
17752 
17753 	ASSERT(un != NULL);
17754 	ASSERT(mutex_owned(SD_MUTEX(un)));
17755 	ASSERT(bp != NULL);
17756 	ASSERT(bp != un->un_rqs_bp);
17757 	ASSERT(xp != NULL);
17758 	ASSERT(pktp != NULL);
17759 
17760 	sense_key = scsi_sense_key(xp->xb_sense_data);
17761 
17762 	switch (sense_key) {
17763 	case KEY_NO_SENSE:
17764 		sd_sense_key_no_sense(un, bp, xp, pktp);
17765 		break;
17766 	case KEY_RECOVERABLE_ERROR:
17767 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17768 		    bp, xp, pktp);
17769 		break;
17770 	case KEY_NOT_READY:
17771 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17772 		    bp, xp, pktp);
17773 		break;
17774 	case KEY_MEDIUM_ERROR:
17775 	case KEY_HARDWARE_ERROR:
17776 		sd_sense_key_medium_or_hardware_error(un,
17777 		    xp->xb_sense_data, bp, xp, pktp);
17778 		break;
17779 	case KEY_ILLEGAL_REQUEST:
17780 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17781 		break;
17782 	case KEY_UNIT_ATTENTION:
17783 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17784 		    bp, xp, pktp);
17785 		break;
17786 	case KEY_WRITE_PROTECT:
17787 	case KEY_VOLUME_OVERFLOW:
17788 	case KEY_MISCOMPARE:
17789 		sd_sense_key_fail_command(un, bp, xp, pktp);
17790 		break;
17791 	case KEY_BLANK_CHECK:
17792 		sd_sense_key_blank_check(un, bp, xp, pktp);
17793 		break;
17794 	case KEY_ABORTED_COMMAND:
17795 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17796 		break;
17797 	case KEY_VENDOR_UNIQUE:
17798 	case KEY_COPY_ABORTED:
17799 	case KEY_EQUAL:
17800 	case KEY_RESERVED:
17801 	default:
17802 		sd_sense_key_default(un, xp->xb_sense_data,
17803 		    bp, xp, pktp);
17804 		break;
17805 	}
17806 }
17807 
17808 
17809 /*
17810  *    Function: sd_dump_memory
17811  *
17812  * Description: Debug logging routine to print the contents of a user provided
17813  *		buffer. The output of the buffer is broken up into 256 byte
17814  *		segments due to a size constraint of the scsi_log.
17815  *		implementation.
17816  *
17817  *   Arguments: un - ptr to softstate
17818  *		comp - component mask
17819  *		title - "title" string to preceed data when printed
17820  *		data - ptr to data block to be printed
17821  *		len - size of data block to be printed
17822  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17823  *
17824  *     Context: May be called from interrupt context
17825  */
17826 
17827 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17828 
17829 static char *sd_dump_format_string[] = {
17830 		" 0x%02x",
17831 		" %c"
17832 };
17833 
17834 static void
17835 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17836     int len, int fmt)
17837 {
17838 	int	i, j;
17839 	int	avail_count;
17840 	int	start_offset;
17841 	int	end_offset;
17842 	size_t	entry_len;
17843 	char	*bufp;
17844 	char	*local_buf;
17845 	char	*format_string;
17846 
17847 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17848 
17849 	/*
17850 	 * In the debug version of the driver, this function is called from a
17851 	 * number of places which are NOPs in the release driver.
17852 	 * The debug driver therefore has additional methods of filtering
17853 	 * debug output.
17854 	 */
17855 #ifdef SDDEBUG
17856 	/*
17857 	 * In the debug version of the driver we can reduce the amount of debug
17858 	 * messages by setting sd_error_level to something other than
17859 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17860 	 * sd_component_mask.
17861 	 */
17862 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17863 	    (sd_error_level != SCSI_ERR_ALL)) {
17864 		return;
17865 	}
17866 	if (((sd_component_mask & comp) == 0) ||
17867 	    (sd_error_level != SCSI_ERR_ALL)) {
17868 		return;
17869 	}
17870 #else
17871 	if (sd_error_level != SCSI_ERR_ALL) {
17872 		return;
17873 	}
17874 #endif
17875 
17876 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17877 	bufp = local_buf;
17878 	/*
17879 	 * Available length is the length of local_buf[], minus the
17880 	 * length of the title string, minus one for the ":", minus
17881 	 * one for the newline, minus one for the NULL terminator.
17882 	 * This gives the #bytes available for holding the printed
17883 	 * values from the given data buffer.
17884 	 */
17885 	if (fmt == SD_LOG_HEX) {
17886 		format_string = sd_dump_format_string[0];
17887 	} else /* SD_LOG_CHAR */ {
17888 		format_string = sd_dump_format_string[1];
17889 	}
17890 	/*
17891 	 * Available count is the number of elements from the given
17892 	 * data buffer that we can fit into the available length.
17893 	 * This is based upon the size of the format string used.
17894 	 * Make one entry and find it's size.
17895 	 */
17896 	(void) sprintf(bufp, format_string, data[0]);
17897 	entry_len = strlen(bufp);
17898 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17899 
17900 	j = 0;
17901 	while (j < len) {
17902 		bufp = local_buf;
17903 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17904 		start_offset = j;
17905 
17906 		end_offset = start_offset + avail_count;
17907 
17908 		(void) sprintf(bufp, "%s:", title);
17909 		bufp += strlen(bufp);
17910 		for (i = start_offset; ((i < end_offset) && (j < len));
17911 		    i++, j++) {
17912 			(void) sprintf(bufp, format_string, data[i]);
17913 			bufp += entry_len;
17914 		}
17915 		(void) sprintf(bufp, "\n");
17916 
17917 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17918 	}
17919 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17920 }
17921 
17922 /*
17923  *    Function: sd_print_sense_msg
17924  *
17925  * Description: Log a message based upon the given sense data.
17926  *
17927  *   Arguments: un - ptr to associated softstate
17928  *		bp - ptr to buf(9S) for the command
17929  *		arg - ptr to associate sd_sense_info struct
17930  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17931  *			or SD_NO_RETRY_ISSUED
17932  *
17933  *     Context: May be called from interrupt context
17934  */
17935 
17936 static void
17937 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17938 {
17939 	struct sd_xbuf	*xp;
17940 	struct scsi_pkt	*pktp;
17941 	uint8_t *sensep;
17942 	daddr_t request_blkno;
17943 	diskaddr_t err_blkno;
17944 	int severity;
17945 	int pfa_flag;
17946 	extern struct scsi_key_strings scsi_cmds[];
17947 
17948 	ASSERT(un != NULL);
17949 	ASSERT(mutex_owned(SD_MUTEX(un)));
17950 	ASSERT(bp != NULL);
17951 	xp = SD_GET_XBUF(bp);
17952 	ASSERT(xp != NULL);
17953 	pktp = SD_GET_PKTP(bp);
17954 	ASSERT(pktp != NULL);
17955 	ASSERT(arg != NULL);
17956 
17957 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17958 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17959 
17960 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17961 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17962 		severity = SCSI_ERR_RETRYABLE;
17963 	}
17964 
17965 	/* Use absolute block number for the request block number */
17966 	request_blkno = xp->xb_blkno;
17967 
17968 	/*
17969 	 * Now try to get the error block number from the sense data
17970 	 */
17971 	sensep = xp->xb_sense_data;
17972 
17973 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
17974 	    (uint64_t *)&err_blkno)) {
17975 		/*
17976 		 * We retrieved the error block number from the information
17977 		 * portion of the sense data.
17978 		 *
17979 		 * For USCSI commands we are better off using the error
17980 		 * block no. as the requested block no. (This is the best
17981 		 * we can estimate.)
17982 		 */
17983 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17984 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17985 			request_blkno = err_blkno;
17986 		}
17987 	} else {
17988 		/*
17989 		 * Without the es_valid bit set (for fixed format) or an
17990 		 * information descriptor (for descriptor format) we cannot
17991 		 * be certain of the error blkno, so just use the
17992 		 * request_blkno.
17993 		 */
17994 		err_blkno = (diskaddr_t)request_blkno;
17995 	}
17996 
17997 	/*
17998 	 * The following will log the buffer contents for the release driver
17999 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
18000 	 * level is set to verbose.
18001 	 */
18002 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
18003 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18004 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18005 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18006 
18007 	if (pfa_flag == FALSE) {
18008 		/* This is normally only set for USCSI */
18009 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18010 			return;
18011 		}
18012 
18013 		if ((SD_IS_BUFIO(xp) == TRUE) &&
18014 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18015 		    (severity < sd_error_level))) {
18016 			return;
18017 		}
18018 	}
18019 	/*
18020 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
18021 	 */
18022 	if ((SD_IS_LSI(un)) &&
18023 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18024 	    (scsi_sense_asc(sensep) == 0x94) &&
18025 	    (scsi_sense_ascq(sensep) == 0x01)) {
18026 		un->un_sonoma_failure_count++;
18027 		if (un->un_sonoma_failure_count > 1) {
18028 			return;
18029 		}
18030 	}
18031 
18032 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18033 	    ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18034 	    (pktp->pkt_resid == 0))) {
18035 		scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18036 		    request_blkno, err_blkno, scsi_cmds,
18037 		    (struct scsi_extended_sense *)sensep,
18038 		    un->un_additional_codes, NULL);
18039 	}
18040 }
18041 
18042 /*
18043  *    Function: sd_sense_key_no_sense
18044  *
18045  * Description: Recovery action when sense data was not received.
18046  *
18047  *     Context: May be called from interrupt context
18048  */
18049 
18050 static void
18051 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
18052 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18053 {
18054 	struct sd_sense_info	si;
18055 
18056 	ASSERT(un != NULL);
18057 	ASSERT(mutex_owned(SD_MUTEX(un)));
18058 	ASSERT(bp != NULL);
18059 	ASSERT(xp != NULL);
18060 	ASSERT(pktp != NULL);
18061 
18062 	si.ssi_severity = SCSI_ERR_FATAL;
18063 	si.ssi_pfa_flag = FALSE;
18064 
18065 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
18066 
18067 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18068 	    &si, EIO, (clock_t)0, NULL);
18069 }
18070 
18071 
18072 /*
18073  *    Function: sd_sense_key_recoverable_error
18074  *
18075  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18076  *
18077  *     Context: May be called from interrupt context
18078  */
18079 
18080 static void
18081 sd_sense_key_recoverable_error(struct sd_lun *un,
18082 	uint8_t *sense_datap,
18083 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18084 {
18085 	struct sd_sense_info	si;
18086 	uint8_t asc = scsi_sense_asc(sense_datap);
18087 
18088 	ASSERT(un != NULL);
18089 	ASSERT(mutex_owned(SD_MUTEX(un)));
18090 	ASSERT(bp != NULL);
18091 	ASSERT(xp != NULL);
18092 	ASSERT(pktp != NULL);
18093 
18094 	/*
18095 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18096 	 */
18097 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18098 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18099 		si.ssi_severity = SCSI_ERR_INFO;
18100 		si.ssi_pfa_flag = TRUE;
18101 	} else {
18102 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
18103 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18104 		si.ssi_severity = SCSI_ERR_RECOVERED;
18105 		si.ssi_pfa_flag = FALSE;
18106 	}
18107 
18108 	if (pktp->pkt_resid == 0) {
18109 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18110 		sd_return_command(un, bp);
18111 		return;
18112 	}
18113 
18114 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18115 	    &si, EIO, (clock_t)0, NULL);
18116 }
18117 
18118 
18119 
18120 
18121 /*
18122  *    Function: sd_sense_key_not_ready
18123  *
18124  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18125  *
18126  *     Context: May be called from interrupt context
18127  */
18128 
18129 static void
18130 sd_sense_key_not_ready(struct sd_lun *un,
18131 	uint8_t *sense_datap,
18132 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18133 {
18134 	struct sd_sense_info	si;
18135 	uint8_t asc = scsi_sense_asc(sense_datap);
18136 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18137 
18138 	ASSERT(un != NULL);
18139 	ASSERT(mutex_owned(SD_MUTEX(un)));
18140 	ASSERT(bp != NULL);
18141 	ASSERT(xp != NULL);
18142 	ASSERT(pktp != NULL);
18143 
18144 	si.ssi_severity = SCSI_ERR_FATAL;
18145 	si.ssi_pfa_flag = FALSE;
18146 
18147 	/*
18148 	 * Update error stats after first NOT READY error. Disks may have
18149 	 * been powered down and may need to be restarted.  For CDROMs,
18150 	 * report NOT READY errors only if media is present.
18151 	 */
18152 	if ((ISCD(un) && (asc == 0x3A)) ||
18153 	    (xp->xb_nr_retry_count > 0)) {
18154 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18155 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18156 	}
18157 
18158 	/*
18159 	 * Just fail if the "not ready" retry limit has been reached.
18160 	 */
18161 	if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18162 		/* Special check for error message printing for removables. */
18163 		if (un->un_f_has_removable_media && (asc == 0x04) &&
18164 		    (ascq >= 0x04)) {
18165 			si.ssi_severity = SCSI_ERR_ALL;
18166 		}
18167 		goto fail_command;
18168 	}
18169 
18170 	/*
18171 	 * Check the ASC and ASCQ in the sense data as needed, to determine
18172 	 * what to do.
18173 	 */
18174 	switch (asc) {
18175 	case 0x04:	/* LOGICAL UNIT NOT READY */
18176 		/*
18177 		 * disk drives that don't spin up result in a very long delay
18178 		 * in format without warning messages. We will log a message
18179 		 * if the error level is set to verbose.
18180 		 */
18181 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18182 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18183 			    "logical unit not ready, resetting disk\n");
18184 		}
18185 
18186 		/*
18187 		 * There are different requirements for CDROMs and disks for
18188 		 * the number of retries.  If a CD-ROM is giving this, it is
18189 		 * probably reading TOC and is in the process of getting
18190 		 * ready, so we should keep on trying for a long time to make
18191 		 * sure that all types of media are taken in account (for
18192 		 * some media the drive takes a long time to read TOC).  For
18193 		 * disks we do not want to retry this too many times as this
18194 		 * can cause a long hang in format when the drive refuses to
18195 		 * spin up (a very common failure).
18196 		 */
18197 		switch (ascq) {
18198 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18199 			/*
18200 			 * Disk drives frequently refuse to spin up which
18201 			 * results in a very long hang in format without
18202 			 * warning messages.
18203 			 *
18204 			 * Note: This code preserves the legacy behavior of
18205 			 * comparing xb_nr_retry_count against zero for fibre
18206 			 * channel targets instead of comparing against the
18207 			 * un_reset_retry_count value.  The reason for this
18208 			 * discrepancy has been so utterly lost beneath the
18209 			 * Sands of Time that even Indiana Jones could not
18210 			 * find it.
18211 			 */
18212 			if (un->un_f_is_fibre == TRUE) {
18213 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18214 				    (xp->xb_nr_retry_count > 0)) &&
18215 				    (un->un_startstop_timeid == NULL)) {
18216 					scsi_log(SD_DEVINFO(un), sd_label,
18217 					    CE_WARN, "logical unit not ready, "
18218 					    "resetting disk\n");
18219 					sd_reset_target(un, pktp);
18220 				}
18221 			} else {
18222 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18223 				    (xp->xb_nr_retry_count >
18224 				    un->un_reset_retry_count)) &&
18225 				    (un->un_startstop_timeid == NULL)) {
18226 					scsi_log(SD_DEVINFO(un), sd_label,
18227 					    CE_WARN, "logical unit not ready, "
18228 					    "resetting disk\n");
18229 					sd_reset_target(un, pktp);
18230 				}
18231 			}
18232 			break;
18233 
18234 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18235 			/*
18236 			 * If the target is in the process of becoming
18237 			 * ready, just proceed with the retry. This can
18238 			 * happen with CD-ROMs that take a long time to
18239 			 * read TOC after a power cycle or reset.
18240 			 */
18241 			goto do_retry;
18242 
18243 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18244 			break;
18245 
18246 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18247 			/*
18248 			 * Retries cannot help here so just fail right away.
18249 			 */
18250 			goto fail_command;
18251 
18252 		case 0x88:
18253 			/*
18254 			 * Vendor-unique code for T3/T4: it indicates a
18255 			 * path problem in a mutipathed config, but as far as
18256 			 * the target driver is concerned it equates to a fatal
18257 			 * error, so we should just fail the command right away
18258 			 * (without printing anything to the console). If this
18259 			 * is not a T3/T4, fall thru to the default recovery
18260 			 * action.
18261 			 * T3/T4 is FC only, don't need to check is_fibre
18262 			 */
18263 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
18264 				sd_return_failed_command(un, bp, EIO);
18265 				return;
18266 			}
18267 			/* FALLTHRU */
18268 
18269 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18270 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18271 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18272 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18273 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18274 		default:    /* Possible future codes in SCSI spec? */
18275 			/*
18276 			 * For removable-media devices, do not retry if
18277 			 * ASCQ > 2 as these result mostly from USCSI commands
18278 			 * on MMC devices issued to check status of an
18279 			 * operation initiated in immediate mode.  Also for
18280 			 * ASCQ >= 4 do not print console messages as these
18281 			 * mainly represent a user-initiated operation
18282 			 * instead of a system failure.
18283 			 */
18284 			if (un->un_f_has_removable_media) {
18285 				si.ssi_severity = SCSI_ERR_ALL;
18286 				goto fail_command;
18287 			}
18288 			break;
18289 		}
18290 
18291 		/*
18292 		 * As part of our recovery attempt for the NOT READY
18293 		 * condition, we issue a START STOP UNIT command. However
18294 		 * we want to wait for a short delay before attempting this
18295 		 * as there may still be more commands coming back from the
18296 		 * target with the check condition. To do this we use
18297 		 * timeout(9F) to call sd_start_stop_unit_callback() after
18298 		 * the delay interval expires. (sd_start_stop_unit_callback()
18299 		 * dispatches sd_start_stop_unit_task(), which will issue
18300 		 * the actual START STOP UNIT command. The delay interval
18301 		 * is one-half of the delay that we will use to retry the
18302 		 * command that generated the NOT READY condition.
18303 		 *
18304 		 * Note that we could just dispatch sd_start_stop_unit_task()
18305 		 * from here and allow it to sleep for the delay interval,
18306 		 * but then we would be tying up the taskq thread
18307 		 * uncesessarily for the duration of the delay.
18308 		 *
18309 		 * Do not issue the START STOP UNIT if the current command
18310 		 * is already a START STOP UNIT.
18311 		 */
18312 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18313 			break;
18314 		}
18315 
18316 		/*
18317 		 * Do not schedule the timeout if one is already pending.
18318 		 */
18319 		if (un->un_startstop_timeid != NULL) {
18320 			SD_INFO(SD_LOG_ERROR, un,
18321 			    "sd_sense_key_not_ready: restart already issued to"
18322 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18323 			    ddi_get_instance(SD_DEVINFO(un)));
18324 			break;
18325 		}
18326 
18327 		/*
18328 		 * Schedule the START STOP UNIT command, then queue the command
18329 		 * for a retry.
18330 		 *
18331 		 * Note: A timeout is not scheduled for this retry because we
18332 		 * want the retry to be serial with the START_STOP_UNIT. The
18333 		 * retry will be started when the START_STOP_UNIT is completed
18334 		 * in sd_start_stop_unit_task.
18335 		 */
18336 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18337 		    un, un->un_busy_timeout / 2);
18338 		xp->xb_nr_retry_count++;
18339 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18340 		return;
18341 
18342 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18343 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18344 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18345 			    "unit does not respond to selection\n");
18346 		}
18347 		break;
18348 
18349 	case 0x3A:	/* MEDIUM NOT PRESENT */
18350 		if (sd_error_level >= SCSI_ERR_FATAL) {
18351 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18352 			    "Caddy not inserted in drive\n");
18353 		}
18354 
18355 		sr_ejected(un);
18356 		un->un_mediastate = DKIO_EJECTED;
18357 		/* The state has changed, inform the media watch routines */
18358 		cv_broadcast(&un->un_state_cv);
18359 		/* Just fail if no media is present in the drive. */
18360 		goto fail_command;
18361 
18362 	default:
18363 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18364 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18365 			    "Unit not Ready. Additional sense code 0x%x\n",
18366 			    asc);
18367 		}
18368 		break;
18369 	}
18370 
18371 do_retry:
18372 
18373 	/*
18374 	 * Retry the command, as some targets may report NOT READY for
18375 	 * several seconds after being reset.
18376 	 */
18377 	xp->xb_nr_retry_count++;
18378 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18379 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18380 	    &si, EIO, un->un_busy_timeout, NULL);
18381 
18382 	return;
18383 
18384 fail_command:
18385 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18386 	sd_return_failed_command(un, bp, EIO);
18387 }
18388 
18389 
18390 
18391 /*
18392  *    Function: sd_sense_key_medium_or_hardware_error
18393  *
18394  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18395  *		sense key.
18396  *
18397  *     Context: May be called from interrupt context
18398  */
18399 
18400 static void
18401 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18402 	uint8_t *sense_datap,
18403 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18404 {
18405 	struct sd_sense_info	si;
18406 	uint8_t sense_key = scsi_sense_key(sense_datap);
18407 	uint8_t asc = scsi_sense_asc(sense_datap);
18408 
18409 	ASSERT(un != NULL);
18410 	ASSERT(mutex_owned(SD_MUTEX(un)));
18411 	ASSERT(bp != NULL);
18412 	ASSERT(xp != NULL);
18413 	ASSERT(pktp != NULL);
18414 
18415 	si.ssi_severity = SCSI_ERR_FATAL;
18416 	si.ssi_pfa_flag = FALSE;
18417 
18418 	if (sense_key == KEY_MEDIUM_ERROR) {
18419 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18420 	}
18421 
18422 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18423 
18424 	if ((un->un_reset_retry_count != 0) &&
18425 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18426 		mutex_exit(SD_MUTEX(un));
18427 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18428 		if (un->un_f_allow_bus_device_reset == TRUE) {
18429 
18430 			boolean_t try_resetting_target = B_TRUE;
18431 
18432 			/*
18433 			 * We need to be able to handle specific ASC when we are
18434 			 * handling a KEY_HARDWARE_ERROR. In particular
18435 			 * taking the default action of resetting the target may
18436 			 * not be the appropriate way to attempt recovery.
18437 			 * Resetting a target because of a single LUN failure
18438 			 * victimizes all LUNs on that target.
18439 			 *
18440 			 * This is true for the LSI arrays, if an LSI
18441 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18442 			 * should trust it.
18443 			 */
18444 
18445 			if (sense_key == KEY_HARDWARE_ERROR) {
18446 				switch (asc) {
18447 				case 0x84:
18448 					if (SD_IS_LSI(un)) {
18449 						try_resetting_target = B_FALSE;
18450 					}
18451 					break;
18452 				default:
18453 					break;
18454 				}
18455 			}
18456 
18457 			if (try_resetting_target == B_TRUE) {
18458 				int reset_retval = 0;
18459 				if (un->un_f_lun_reset_enabled == TRUE) {
18460 					SD_TRACE(SD_LOG_IO_CORE, un,
18461 					    "sd_sense_key_medium_or_hardware_"
18462 					    "error: issuing RESET_LUN\n");
18463 					reset_retval =
18464 					    scsi_reset(SD_ADDRESS(un),
18465 					    RESET_LUN);
18466 				}
18467 				if (reset_retval == 0) {
18468 					SD_TRACE(SD_LOG_IO_CORE, un,
18469 					    "sd_sense_key_medium_or_hardware_"
18470 					    "error: issuing RESET_TARGET\n");
18471 					(void) scsi_reset(SD_ADDRESS(un),
18472 					    RESET_TARGET);
18473 				}
18474 			}
18475 		}
18476 		mutex_enter(SD_MUTEX(un));
18477 	}
18478 
18479 	/*
18480 	 * This really ought to be a fatal error, but we will retry anyway
18481 	 * as some drives report this as a spurious error.
18482 	 */
18483 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18484 	    &si, EIO, (clock_t)0, NULL);
18485 }
18486 
18487 
18488 
18489 /*
18490  *    Function: sd_sense_key_illegal_request
18491  *
18492  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18493  *
18494  *     Context: May be called from interrupt context
18495  */
18496 
18497 static void
18498 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18499 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18500 {
18501 	struct sd_sense_info	si;
18502 
18503 	ASSERT(un != NULL);
18504 	ASSERT(mutex_owned(SD_MUTEX(un)));
18505 	ASSERT(bp != NULL);
18506 	ASSERT(xp != NULL);
18507 	ASSERT(pktp != NULL);
18508 
18509 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18510 
18511 	si.ssi_severity = SCSI_ERR_INFO;
18512 	si.ssi_pfa_flag = FALSE;
18513 
18514 	/* Pointless to retry if the target thinks it's an illegal request */
18515 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18516 	sd_return_failed_command(un, bp, EIO);
18517 }
18518 
18519 
18520 
18521 
18522 /*
18523  *    Function: sd_sense_key_unit_attention
18524  *
18525  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18526  *
18527  *     Context: May be called from interrupt context
18528  */
18529 
18530 static void
18531 sd_sense_key_unit_attention(struct sd_lun *un,
18532 	uint8_t *sense_datap,
18533 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18534 {
18535 	/*
18536 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18537 	 * like Sonoma can return UNIT ATTENTION close to a minute
18538 	 * under certain conditions.
18539 	 */
18540 	int	retry_check_flag = SD_RETRIES_UA;
18541 	boolean_t	kstat_updated = B_FALSE;
18542 	struct	sd_sense_info		si;
18543 	uint8_t asc = scsi_sense_asc(sense_datap);
18544 	uint8_t	ascq = scsi_sense_ascq(sense_datap);
18545 
18546 	ASSERT(un != NULL);
18547 	ASSERT(mutex_owned(SD_MUTEX(un)));
18548 	ASSERT(bp != NULL);
18549 	ASSERT(xp != NULL);
18550 	ASSERT(pktp != NULL);
18551 
18552 	si.ssi_severity = SCSI_ERR_INFO;
18553 	si.ssi_pfa_flag = FALSE;
18554 
18555 
18556 	switch (asc) {
18557 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18558 		if (sd_report_pfa != 0) {
18559 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18560 			si.ssi_pfa_flag = TRUE;
18561 			retry_check_flag = SD_RETRIES_STANDARD;
18562 			goto do_retry;
18563 		}
18564 
18565 		break;
18566 
18567 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18568 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18569 			un->un_resvd_status |=
18570 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18571 		}
18572 #ifdef _LP64
18573 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18574 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18575 			    un, KM_NOSLEEP) == 0) {
18576 				/*
18577 				 * If we can't dispatch the task we'll just
18578 				 * live without descriptor sense.  We can
18579 				 * try again on the next "unit attention"
18580 				 */
18581 				SD_ERROR(SD_LOG_ERROR, un,
18582 				    "sd_sense_key_unit_attention: "
18583 				    "Could not dispatch "
18584 				    "sd_reenable_dsense_task\n");
18585 			}
18586 		}
18587 #endif /* _LP64 */
18588 		/* FALLTHRU */
18589 
18590 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18591 		if (!un->un_f_has_removable_media) {
18592 			break;
18593 		}
18594 
18595 		/*
18596 		 * When we get a unit attention from a removable-media device,
18597 		 * it may be in a state that will take a long time to recover
18598 		 * (e.g., from a reset).  Since we are executing in interrupt
18599 		 * context here, we cannot wait around for the device to come
18600 		 * back. So hand this command off to sd_media_change_task()
18601 		 * for deferred processing under taskq thread context. (Note
18602 		 * that the command still may be failed if a problem is
18603 		 * encountered at a later time.)
18604 		 */
18605 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18606 		    KM_NOSLEEP) == 0) {
18607 			/*
18608 			 * Cannot dispatch the request so fail the command.
18609 			 */
18610 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18611 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18612 			si.ssi_severity = SCSI_ERR_FATAL;
18613 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18614 			sd_return_failed_command(un, bp, EIO);
18615 		}
18616 
18617 		/*
18618 		 * If failed to dispatch sd_media_change_task(), we already
18619 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18620 		 * we should update kstat later if it encounters an error. So,
18621 		 * we update kstat_updated flag here.
18622 		 */
18623 		kstat_updated = B_TRUE;
18624 
18625 		/*
18626 		 * Either the command has been successfully dispatched to a
18627 		 * task Q for retrying, or the dispatch failed. In either case
18628 		 * do NOT retry again by calling sd_retry_command. This sets up
18629 		 * two retries of the same command and when one completes and
18630 		 * frees the resources the other will access freed memory,
18631 		 * a bad thing.
18632 		 */
18633 		return;
18634 
18635 	default:
18636 		break;
18637 	}
18638 
18639 	/*
18640 	 * ASC  ASCQ
18641 	 *  2A   09	Capacity data has changed
18642 	 *  2A   01	Mode parameters changed
18643 	 *  3F   0E	Reported luns data has changed
18644 	 * Arrays that support logical unit expansion should report
18645 	 * capacity changes(2Ah/09). Mode parameters changed and
18646 	 * reported luns data has changed are the approximation.
18647 	 */
18648 	if (((asc == 0x2a) && (ascq == 0x09)) ||
18649 	    ((asc == 0x2a) && (ascq == 0x01)) ||
18650 	    ((asc == 0x3f) && (ascq == 0x0e))) {
18651 		if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18652 		    KM_NOSLEEP) == 0) {
18653 			SD_ERROR(SD_LOG_ERROR, un,
18654 			    "sd_sense_key_unit_attention: "
18655 			    "Could not dispatch sd_target_change_task\n");
18656 		}
18657 	}
18658 
18659 	/*
18660 	 * Update kstat if we haven't done that.
18661 	 */
18662 	if (!kstat_updated) {
18663 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18664 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18665 	}
18666 
18667 do_retry:
18668 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18669 	    EIO, SD_UA_RETRY_DELAY, NULL);
18670 }
18671 
18672 
18673 
18674 /*
18675  *    Function: sd_sense_key_fail_command
18676  *
18677  * Description: Use to fail a command when we don't like the sense key that
18678  *		was returned.
18679  *
18680  *     Context: May be called from interrupt context
18681  */
18682 
18683 static void
18684 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18685 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18686 {
18687 	struct sd_sense_info	si;
18688 
18689 	ASSERT(un != NULL);
18690 	ASSERT(mutex_owned(SD_MUTEX(un)));
18691 	ASSERT(bp != NULL);
18692 	ASSERT(xp != NULL);
18693 	ASSERT(pktp != NULL);
18694 
18695 	si.ssi_severity = SCSI_ERR_FATAL;
18696 	si.ssi_pfa_flag = FALSE;
18697 
18698 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18699 	sd_return_failed_command(un, bp, EIO);
18700 }
18701 
18702 
18703 
18704 /*
18705  *    Function: sd_sense_key_blank_check
18706  *
18707  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18708  *		Has no monetary connotation.
18709  *
18710  *     Context: May be called from interrupt context
18711  */
18712 
18713 static void
18714 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18715 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18716 {
18717 	struct sd_sense_info	si;
18718 
18719 	ASSERT(un != NULL);
18720 	ASSERT(mutex_owned(SD_MUTEX(un)));
18721 	ASSERT(bp != NULL);
18722 	ASSERT(xp != NULL);
18723 	ASSERT(pktp != NULL);
18724 
18725 	/*
18726 	 * Blank check is not fatal for removable devices, therefore
18727 	 * it does not require a console message.
18728 	 */
18729 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18730 	    SCSI_ERR_FATAL;
18731 	si.ssi_pfa_flag = FALSE;
18732 
18733 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18734 	sd_return_failed_command(un, bp, EIO);
18735 }
18736 
18737 
18738 
18739 
18740 /*
18741  *    Function: sd_sense_key_aborted_command
18742  *
18743  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18744  *
18745  *     Context: May be called from interrupt context
18746  */
18747 
18748 static void
18749 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18750 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18751 {
18752 	struct sd_sense_info	si;
18753 
18754 	ASSERT(un != NULL);
18755 	ASSERT(mutex_owned(SD_MUTEX(un)));
18756 	ASSERT(bp != NULL);
18757 	ASSERT(xp != NULL);
18758 	ASSERT(pktp != NULL);
18759 
18760 	si.ssi_severity = SCSI_ERR_FATAL;
18761 	si.ssi_pfa_flag = FALSE;
18762 
18763 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18764 
18765 	/*
18766 	 * This really ought to be a fatal error, but we will retry anyway
18767 	 * as some drives report this as a spurious error.
18768 	 */
18769 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18770 	    &si, EIO, drv_usectohz(100000), NULL);
18771 }
18772 
18773 
18774 
18775 /*
18776  *    Function: sd_sense_key_default
18777  *
18778  * Description: Default recovery action for several SCSI sense keys (basically
18779  *		attempts a retry).
18780  *
18781  *     Context: May be called from interrupt context
18782  */
18783 
18784 static void
18785 sd_sense_key_default(struct sd_lun *un,
18786 	uint8_t *sense_datap,
18787 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18788 {
18789 	struct sd_sense_info	si;
18790 	uint8_t sense_key = scsi_sense_key(sense_datap);
18791 
18792 	ASSERT(un != NULL);
18793 	ASSERT(mutex_owned(SD_MUTEX(un)));
18794 	ASSERT(bp != NULL);
18795 	ASSERT(xp != NULL);
18796 	ASSERT(pktp != NULL);
18797 
18798 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18799 
18800 	/*
18801 	 * Undecoded sense key.	Attempt retries and hope that will fix
18802 	 * the problem.  Otherwise, we're dead.
18803 	 */
18804 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18805 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18806 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18807 	}
18808 
18809 	si.ssi_severity = SCSI_ERR_FATAL;
18810 	si.ssi_pfa_flag = FALSE;
18811 
18812 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18813 	    &si, EIO, (clock_t)0, NULL);
18814 }
18815 
18816 
18817 
18818 /*
18819  *    Function: sd_print_retry_msg
18820  *
18821  * Description: Print a message indicating the retry action being taken.
18822  *
18823  *   Arguments: un - ptr to associated softstate
18824  *		bp - ptr to buf(9S) for the command
18825  *		arg - not used.
18826  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18827  *			or SD_NO_RETRY_ISSUED
18828  *
18829  *     Context: May be called from interrupt context
18830  */
18831 /* ARGSUSED */
18832 static void
18833 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18834 {
18835 	struct sd_xbuf	*xp;
18836 	struct scsi_pkt *pktp;
18837 	char *reasonp;
18838 	char *msgp;
18839 
18840 	ASSERT(un != NULL);
18841 	ASSERT(mutex_owned(SD_MUTEX(un)));
18842 	ASSERT(bp != NULL);
18843 	pktp = SD_GET_PKTP(bp);
18844 	ASSERT(pktp != NULL);
18845 	xp = SD_GET_XBUF(bp);
18846 	ASSERT(xp != NULL);
18847 
18848 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18849 	mutex_enter(&un->un_pm_mutex);
18850 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18851 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18852 	    (pktp->pkt_flags & FLAG_SILENT)) {
18853 		mutex_exit(&un->un_pm_mutex);
18854 		goto update_pkt_reason;
18855 	}
18856 	mutex_exit(&un->un_pm_mutex);
18857 
18858 	/*
18859 	 * Suppress messages if they are all the same pkt_reason; with
18860 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18861 	 * If we are in panic, then suppress the retry messages.
18862 	 */
18863 	switch (flag) {
18864 	case SD_NO_RETRY_ISSUED:
18865 		msgp = "giving up";
18866 		break;
18867 	case SD_IMMEDIATE_RETRY_ISSUED:
18868 	case SD_DELAYED_RETRY_ISSUED:
18869 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18870 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18871 		    (sd_error_level != SCSI_ERR_ALL))) {
18872 			return;
18873 		}
18874 		msgp = "retrying command";
18875 		break;
18876 	default:
18877 		goto update_pkt_reason;
18878 	}
18879 
18880 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18881 	    scsi_rname(pktp->pkt_reason));
18882 
18883 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18884 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18885 		    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18886 	}
18887 
18888 update_pkt_reason:
18889 	/*
18890 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18891 	 * This is to prevent multiple console messages for the same failure
18892 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18893 	 * when the command is retried successfully because there still may be
18894 	 * more commands coming back with the same value of pktp->pkt_reason.
18895 	 */
18896 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18897 		un->un_last_pkt_reason = pktp->pkt_reason;
18898 	}
18899 }
18900 
18901 
18902 /*
18903  *    Function: sd_print_cmd_incomplete_msg
18904  *
18905  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18906  *
18907  *   Arguments: un - ptr to associated softstate
18908  *		bp - ptr to buf(9S) for the command
18909  *		arg - passed to sd_print_retry_msg()
18910  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18911  *			or SD_NO_RETRY_ISSUED
18912  *
18913  *     Context: May be called from interrupt context
18914  */
18915 
18916 static void
18917 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18918 	int code)
18919 {
18920 	dev_info_t	*dip;
18921 
18922 	ASSERT(un != NULL);
18923 	ASSERT(mutex_owned(SD_MUTEX(un)));
18924 	ASSERT(bp != NULL);
18925 
18926 	switch (code) {
18927 	case SD_NO_RETRY_ISSUED:
18928 		/* Command was failed. Someone turned off this target? */
18929 		if (un->un_state != SD_STATE_OFFLINE) {
18930 			/*
18931 			 * Suppress message if we are detaching and
18932 			 * device has been disconnected
18933 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18934 			 * private interface and not part of the DDI
18935 			 */
18936 			dip = un->un_sd->sd_dev;
18937 			if (!(DEVI_IS_DETACHING(dip) &&
18938 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18939 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18940 				"disk not responding to selection\n");
18941 			}
18942 			New_state(un, SD_STATE_OFFLINE);
18943 		}
18944 		break;
18945 
18946 	case SD_DELAYED_RETRY_ISSUED:
18947 	case SD_IMMEDIATE_RETRY_ISSUED:
18948 	default:
18949 		/* Command was successfully queued for retry */
18950 		sd_print_retry_msg(un, bp, arg, code);
18951 		break;
18952 	}
18953 }
18954 
18955 
18956 /*
18957  *    Function: sd_pkt_reason_cmd_incomplete
18958  *
18959  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18960  *
18961  *     Context: May be called from interrupt context
18962  */
18963 
18964 static void
18965 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18966 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18967 {
18968 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18969 
18970 	ASSERT(un != NULL);
18971 	ASSERT(mutex_owned(SD_MUTEX(un)));
18972 	ASSERT(bp != NULL);
18973 	ASSERT(xp != NULL);
18974 	ASSERT(pktp != NULL);
18975 
18976 	/* Do not do a reset if selection did not complete */
18977 	/* Note: Should this not just check the bit? */
18978 	if (pktp->pkt_state != STATE_GOT_BUS) {
18979 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18980 		sd_reset_target(un, pktp);
18981 	}
18982 
18983 	/*
18984 	 * If the target was not successfully selected, then set
18985 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18986 	 * with the target, and further retries and/or commands are
18987 	 * likely to take a long time.
18988 	 */
18989 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18990 		flag |= SD_RETRIES_FAILFAST;
18991 	}
18992 
18993 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18994 
18995 	sd_retry_command(un, bp, flag,
18996 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18997 }
18998 
18999 
19000 
19001 /*
19002  *    Function: sd_pkt_reason_cmd_tran_err
19003  *
19004  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19005  *
19006  *     Context: May be called from interrupt context
19007  */
19008 
19009 static void
19010 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19011 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19012 {
19013 	ASSERT(un != NULL);
19014 	ASSERT(mutex_owned(SD_MUTEX(un)));
19015 	ASSERT(bp != NULL);
19016 	ASSERT(xp != NULL);
19017 	ASSERT(pktp != NULL);
19018 
19019 	/*
19020 	 * Do not reset if we got a parity error, or if
19021 	 * selection did not complete.
19022 	 */
19023 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19024 	/* Note: Should this not just check the bit for pkt_state? */
19025 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19026 	    (pktp->pkt_state != STATE_GOT_BUS)) {
19027 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19028 		sd_reset_target(un, pktp);
19029 	}
19030 
19031 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19032 
19033 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19034 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19035 }
19036 
19037 
19038 
19039 /*
19040  *    Function: sd_pkt_reason_cmd_reset
19041  *
19042  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19043  *
19044  *     Context: May be called from interrupt context
19045  */
19046 
19047 static void
19048 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
19049 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19050 {
19051 	ASSERT(un != NULL);
19052 	ASSERT(mutex_owned(SD_MUTEX(un)));
19053 	ASSERT(bp != NULL);
19054 	ASSERT(xp != NULL);
19055 	ASSERT(pktp != NULL);
19056 
19057 	/* The target may still be running the command, so try to reset. */
19058 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19059 	sd_reset_target(un, pktp);
19060 
19061 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19062 
19063 	/*
19064 	 * If pkt_reason is CMD_RESET chances are that this pkt got
19065 	 * reset because another target on this bus caused it. The target
19066 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19067 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19068 	 */
19069 
19070 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19071 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19072 }
19073 
19074 
19075 
19076 
19077 /*
19078  *    Function: sd_pkt_reason_cmd_aborted
19079  *
19080  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19081  *
19082  *     Context: May be called from interrupt context
19083  */
19084 
19085 static void
19086 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
19087 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19088 {
19089 	ASSERT(un != NULL);
19090 	ASSERT(mutex_owned(SD_MUTEX(un)));
19091 	ASSERT(bp != NULL);
19092 	ASSERT(xp != NULL);
19093 	ASSERT(pktp != NULL);
19094 
19095 	/* The target may still be running the command, so try to reset. */
19096 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19097 	sd_reset_target(un, pktp);
19098 
19099 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19100 
19101 	/*
19102 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19103 	 * aborted because another target on this bus caused it. The target
19104 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19105 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19106 	 */
19107 
19108 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19109 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19110 }
19111 
19112 
19113 
19114 /*
19115  *    Function: sd_pkt_reason_cmd_timeout
19116  *
19117  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19118  *
19119  *     Context: May be called from interrupt context
19120  */
19121 
19122 static void
19123 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19124 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19125 {
19126 	ASSERT(un != NULL);
19127 	ASSERT(mutex_owned(SD_MUTEX(un)));
19128 	ASSERT(bp != NULL);
19129 	ASSERT(xp != NULL);
19130 	ASSERT(pktp != NULL);
19131 
19132 
19133 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19134 	sd_reset_target(un, pktp);
19135 
19136 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19137 
19138 	/*
19139 	 * A command timeout indicates that we could not establish
19140 	 * communication with the target, so set SD_RETRIES_FAILFAST
19141 	 * as further retries/commands are likely to take a long time.
19142 	 */
19143 	sd_retry_command(un, bp,
19144 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19145 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19146 }
19147 
19148 
19149 
19150 /*
19151  *    Function: sd_pkt_reason_cmd_unx_bus_free
19152  *
19153  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19154  *
19155  *     Context: May be called from interrupt context
19156  */
19157 
19158 static void
19159 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19160 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19161 {
19162 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19163 
19164 	ASSERT(un != NULL);
19165 	ASSERT(mutex_owned(SD_MUTEX(un)));
19166 	ASSERT(bp != NULL);
19167 	ASSERT(xp != NULL);
19168 	ASSERT(pktp != NULL);
19169 
19170 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19171 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19172 
19173 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19174 	    sd_print_retry_msg : NULL;
19175 
19176 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19177 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19178 }
19179 
19180 
19181 /*
19182  *    Function: sd_pkt_reason_cmd_tag_reject
19183  *
19184  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19185  *
19186  *     Context: May be called from interrupt context
19187  */
19188 
19189 static void
19190 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19191 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19192 {
19193 	ASSERT(un != NULL);
19194 	ASSERT(mutex_owned(SD_MUTEX(un)));
19195 	ASSERT(bp != NULL);
19196 	ASSERT(xp != NULL);
19197 	ASSERT(pktp != NULL);
19198 
19199 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19200 	pktp->pkt_flags = 0;
19201 	un->un_tagflags = 0;
19202 	if (un->un_f_opt_queueing == TRUE) {
19203 		un->un_throttle = min(un->un_throttle, 3);
19204 	} else {
19205 		un->un_throttle = 1;
19206 	}
19207 	mutex_exit(SD_MUTEX(un));
19208 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19209 	mutex_enter(SD_MUTEX(un));
19210 
19211 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19212 
19213 	/* Legacy behavior not to check retry counts here. */
19214 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19215 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19216 }
19217 
19218 
19219 /*
19220  *    Function: sd_pkt_reason_default
19221  *
19222  * Description: Default recovery actions for SCSA pkt_reason values that
19223  *		do not have more explicit recovery actions.
19224  *
19225  *     Context: May be called from interrupt context
19226  */
19227 
19228 static void
19229 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19230 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19231 {
19232 	ASSERT(un != NULL);
19233 	ASSERT(mutex_owned(SD_MUTEX(un)));
19234 	ASSERT(bp != NULL);
19235 	ASSERT(xp != NULL);
19236 	ASSERT(pktp != NULL);
19237 
19238 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19239 	sd_reset_target(un, pktp);
19240 
19241 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19242 
19243 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19244 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19245 }
19246 
19247 
19248 
19249 /*
19250  *    Function: sd_pkt_status_check_condition
19251  *
19252  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19253  *
19254  *     Context: May be called from interrupt context
19255  */
19256 
19257 static void
19258 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19259 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19260 {
19261 	ASSERT(un != NULL);
19262 	ASSERT(mutex_owned(SD_MUTEX(un)));
19263 	ASSERT(bp != NULL);
19264 	ASSERT(xp != NULL);
19265 	ASSERT(pktp != NULL);
19266 
19267 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19268 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
19269 
19270 	/*
19271 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19272 	 * command will be retried after the request sense). Otherwise, retry
19273 	 * the command. Note: we are issuing the request sense even though the
19274 	 * retry limit may have been reached for the failed command.
19275 	 */
19276 	if (un->un_f_arq_enabled == FALSE) {
19277 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19278 		    "no ARQ, sending request sense command\n");
19279 		sd_send_request_sense_command(un, bp, pktp);
19280 	} else {
19281 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19282 		    "ARQ,retrying request sense command\n");
19283 #if defined(__i386) || defined(__amd64)
19284 		/*
19285 		 * The SD_RETRY_DELAY value need to be adjusted here
19286 		 * when SD_RETRY_DELAY change in sddef.h
19287 		 */
19288 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19289 		    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19290 		    NULL);
19291 #else
19292 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19293 		    EIO, SD_RETRY_DELAY, NULL);
19294 #endif
19295 	}
19296 
19297 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19298 }
19299 
19300 
19301 /*
19302  *    Function: sd_pkt_status_busy
19303  *
19304  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19305  *
19306  *     Context: May be called from interrupt context
19307  */
19308 
19309 static void
19310 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19311 	struct scsi_pkt *pktp)
19312 {
19313 	ASSERT(un != NULL);
19314 	ASSERT(mutex_owned(SD_MUTEX(un)));
19315 	ASSERT(bp != NULL);
19316 	ASSERT(xp != NULL);
19317 	ASSERT(pktp != NULL);
19318 
19319 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19320 	    "sd_pkt_status_busy: entry\n");
19321 
19322 	/* If retries are exhausted, just fail the command. */
19323 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
19324 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19325 		    "device busy too long\n");
19326 		sd_return_failed_command(un, bp, EIO);
19327 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19328 		    "sd_pkt_status_busy: exit\n");
19329 		return;
19330 	}
19331 	xp->xb_retry_count++;
19332 
19333 	/*
19334 	 * Try to reset the target. However, we do not want to perform
19335 	 * more than one reset if the device continues to fail. The reset
19336 	 * will be performed when the retry count reaches the reset
19337 	 * threshold.  This threshold should be set such that at least
19338 	 * one retry is issued before the reset is performed.
19339 	 */
19340 	if (xp->xb_retry_count ==
19341 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19342 		int rval = 0;
19343 		mutex_exit(SD_MUTEX(un));
19344 		if (un->un_f_allow_bus_device_reset == TRUE) {
19345 			/*
19346 			 * First try to reset the LUN; if we cannot then
19347 			 * try to reset the target.
19348 			 */
19349 			if (un->un_f_lun_reset_enabled == TRUE) {
19350 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19351 				    "sd_pkt_status_busy: RESET_LUN\n");
19352 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19353 			}
19354 			if (rval == 0) {
19355 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19356 				    "sd_pkt_status_busy: RESET_TARGET\n");
19357 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19358 			}
19359 		}
19360 		if (rval == 0) {
19361 			/*
19362 			 * If the RESET_LUN and/or RESET_TARGET failed,
19363 			 * try RESET_ALL
19364 			 */
19365 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19366 			    "sd_pkt_status_busy: RESET_ALL\n");
19367 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19368 		}
19369 		mutex_enter(SD_MUTEX(un));
19370 		if (rval == 0) {
19371 			/*
19372 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19373 			 * At this point we give up & fail the command.
19374 			 */
19375 			sd_return_failed_command(un, bp, EIO);
19376 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19377 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19378 			return;
19379 		}
19380 	}
19381 
19382 	/*
19383 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19384 	 * we have already checked the retry counts above.
19385 	 */
19386 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19387 	    EIO, un->un_busy_timeout, NULL);
19388 
19389 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19390 	    "sd_pkt_status_busy: exit\n");
19391 }
19392 
19393 
19394 /*
19395  *    Function: sd_pkt_status_reservation_conflict
19396  *
19397  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19398  *		command status.
19399  *
19400  *     Context: May be called from interrupt context
19401  */
19402 
19403 static void
19404 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19405 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19406 {
19407 	ASSERT(un != NULL);
19408 	ASSERT(mutex_owned(SD_MUTEX(un)));
19409 	ASSERT(bp != NULL);
19410 	ASSERT(xp != NULL);
19411 	ASSERT(pktp != NULL);
19412 
19413 	/*
19414 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19415 	 * conflict could be due to various reasons like incorrect keys, not
19416 	 * registered or not reserved etc. So, we return EACCES to the caller.
19417 	 */
19418 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19419 		int cmd = SD_GET_PKT_OPCODE(pktp);
19420 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19421 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19422 			sd_return_failed_command(un, bp, EACCES);
19423 			return;
19424 		}
19425 	}
19426 
19427 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19428 
19429 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19430 		if (sd_failfast_enable != 0) {
19431 			/* By definition, we must panic here.... */
19432 			sd_panic_for_res_conflict(un);
19433 			/*NOTREACHED*/
19434 		}
19435 		SD_ERROR(SD_LOG_IO, un,
19436 		    "sd_handle_resv_conflict: Disk Reserved\n");
19437 		sd_return_failed_command(un, bp, EACCES);
19438 		return;
19439 	}
19440 
19441 	/*
19442 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19443 	 * property is set (default is 1). Retries will not succeed
19444 	 * on a disk reserved by another initiator. HA systems
19445 	 * may reset this via sd.conf to avoid these retries.
19446 	 *
19447 	 * Note: The legacy return code for this failure is EIO, however EACCES
19448 	 * seems more appropriate for a reservation conflict.
19449 	 */
19450 	if (sd_retry_on_reservation_conflict == 0) {
19451 		SD_ERROR(SD_LOG_IO, un,
19452 		    "sd_handle_resv_conflict: Device Reserved\n");
19453 		sd_return_failed_command(un, bp, EIO);
19454 		return;
19455 	}
19456 
19457 	/*
19458 	 * Retry the command if we can.
19459 	 *
19460 	 * Note: The legacy return code for this failure is EIO, however EACCES
19461 	 * seems more appropriate for a reservation conflict.
19462 	 */
19463 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19464 	    (clock_t)2, NULL);
19465 }
19466 
19467 
19468 
19469 /*
19470  *    Function: sd_pkt_status_qfull
19471  *
19472  * Description: Handle a QUEUE FULL condition from the target.  This can
19473  *		occur if the HBA does not handle the queue full condition.
19474  *		(Basically this means third-party HBAs as Sun HBAs will
19475  *		handle the queue full condition.)  Note that if there are
19476  *		some commands already in the transport, then the queue full
19477  *		has occurred because the queue for this nexus is actually
19478  *		full. If there are no commands in the transport, then the
19479  *		queue full is resulting from some other initiator or lun
19480  *		consuming all the resources at the target.
19481  *
19482  *     Context: May be called from interrupt context
19483  */
19484 
19485 static void
19486 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19487 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19488 {
19489 	ASSERT(un != NULL);
19490 	ASSERT(mutex_owned(SD_MUTEX(un)));
19491 	ASSERT(bp != NULL);
19492 	ASSERT(xp != NULL);
19493 	ASSERT(pktp != NULL);
19494 
19495 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19496 	    "sd_pkt_status_qfull: entry\n");
19497 
19498 	/*
19499 	 * Just lower the QFULL throttle and retry the command.  Note that
19500 	 * we do not limit the number of retries here.
19501 	 */
19502 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19503 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19504 	    SD_RESTART_TIMEOUT, NULL);
19505 
19506 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19507 	    "sd_pkt_status_qfull: exit\n");
19508 }
19509 
19510 
19511 /*
19512  *    Function: sd_reset_target
19513  *
19514  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19515  *		RESET_TARGET, or RESET_ALL.
19516  *
19517  *     Context: May be called under interrupt context.
19518  */
19519 
19520 static void
19521 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19522 {
19523 	int rval = 0;
19524 
19525 	ASSERT(un != NULL);
19526 	ASSERT(mutex_owned(SD_MUTEX(un)));
19527 	ASSERT(pktp != NULL);
19528 
19529 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19530 
19531 	/*
19532 	 * No need to reset if the transport layer has already done so.
19533 	 */
19534 	if ((pktp->pkt_statistics &
19535 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19536 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19537 		    "sd_reset_target: no reset\n");
19538 		return;
19539 	}
19540 
19541 	mutex_exit(SD_MUTEX(un));
19542 
19543 	if (un->un_f_allow_bus_device_reset == TRUE) {
19544 		if (un->un_f_lun_reset_enabled == TRUE) {
19545 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19546 			    "sd_reset_target: RESET_LUN\n");
19547 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19548 		}
19549 		if (rval == 0) {
19550 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19551 			    "sd_reset_target: RESET_TARGET\n");
19552 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19553 		}
19554 	}
19555 
19556 	if (rval == 0) {
19557 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19558 		    "sd_reset_target: RESET_ALL\n");
19559 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19560 	}
19561 
19562 	mutex_enter(SD_MUTEX(un));
19563 
19564 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19565 }
19566 
19567 /*
19568  *    Function: sd_target_change_task
19569  *
19570  * Description: Handle dynamic target change
19571  *
19572  *     Context: Executes in a taskq() thread context
19573  */
19574 static void
19575 sd_target_change_task(void *arg)
19576 {
19577 	struct sd_lun		*un = arg;
19578 	uint64_t		capacity;
19579 	diskaddr_t		label_cap;
19580 	uint_t			lbasize;
19581 	sd_ssc_t		*ssc;
19582 
19583 	ASSERT(un != NULL);
19584 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19585 
19586 	if ((un->un_f_blockcount_is_valid == FALSE) ||
19587 	    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19588 		return;
19589 	}
19590 
19591 	ssc = sd_ssc_init(un);
19592 
19593 	if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19594 	    &lbasize, SD_PATH_DIRECT) != 0) {
19595 		SD_ERROR(SD_LOG_ERROR, un,
19596 		    "sd_target_change_task: fail to read capacity\n");
19597 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19598 		goto task_exit;
19599 	}
19600 
19601 	mutex_enter(SD_MUTEX(un));
19602 	if (capacity <= un->un_blockcount) {
19603 		mutex_exit(SD_MUTEX(un));
19604 		goto task_exit;
19605 	}
19606 
19607 	sd_update_block_info(un, lbasize, capacity);
19608 	mutex_exit(SD_MUTEX(un));
19609 
19610 	/*
19611 	 * If lun is EFI labeled and lun capacity is greater than the
19612 	 * capacity contained in the label, log a sys event.
19613 	 */
19614 	if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19615 	    (void*)SD_PATH_DIRECT) == 0) {
19616 		mutex_enter(SD_MUTEX(un));
19617 		if (un->un_f_blockcount_is_valid &&
19618 		    un->un_blockcount > label_cap) {
19619 			mutex_exit(SD_MUTEX(un));
19620 			sd_log_lun_expansion_event(un, KM_SLEEP);
19621 		} else {
19622 			mutex_exit(SD_MUTEX(un));
19623 		}
19624 	}
19625 
19626 task_exit:
19627 	sd_ssc_fini(ssc);
19628 }
19629 
19630 
19631 /*
19632  *    Function: sd_log_dev_status_event
19633  *
19634  * Description: Log EC_dev_status sysevent
19635  *
19636  *     Context: Never called from interrupt context
19637  */
19638 static void
19639 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19640 {
19641 	int err;
19642 	char			*path;
19643 	nvlist_t		*attr_list;
19644 
19645 	/* Allocate and build sysevent attribute list */
19646 	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19647 	if (err != 0) {
19648 		SD_ERROR(SD_LOG_ERROR, un,
19649 		    "sd_log_dev_status_event: fail to allocate space\n");
19650 		return;
19651 	}
19652 
19653 	path = kmem_alloc(MAXPATHLEN, km_flag);
19654 	if (path == NULL) {
19655 		nvlist_free(attr_list);
19656 		SD_ERROR(SD_LOG_ERROR, un,
19657 		    "sd_log_dev_status_event: fail to allocate space\n");
19658 		return;
19659 	}
19660 	/*
19661 	 * Add path attribute to identify the lun.
19662 	 * We are using minor node 'a' as the sysevent attribute.
19663 	 */
19664 	(void) snprintf(path, MAXPATHLEN, "/devices");
19665 	(void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19666 	(void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19667 	    ":a");
19668 
19669 	err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19670 	if (err != 0) {
19671 		nvlist_free(attr_list);
19672 		kmem_free(path, MAXPATHLEN);
19673 		SD_ERROR(SD_LOG_ERROR, un,
19674 		    "sd_log_dev_status_event: fail to add attribute\n");
19675 		return;
19676 	}
19677 
19678 	/* Log dynamic lun expansion sysevent */
19679 	err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19680 	    esc, attr_list, NULL, km_flag);
19681 	if (err != DDI_SUCCESS) {
19682 		SD_ERROR(SD_LOG_ERROR, un,
19683 		    "sd_log_dev_status_event: fail to log sysevent\n");
19684 	}
19685 
19686 	nvlist_free(attr_list);
19687 	kmem_free(path, MAXPATHLEN);
19688 }
19689 
19690 
19691 /*
19692  *    Function: sd_log_lun_expansion_event
19693  *
19694  * Description: Log lun expansion sys event
19695  *
19696  *     Context: Never called from interrupt context
19697  */
19698 static void
19699 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19700 {
19701 	sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19702 }
19703 
19704 
19705 /*
19706  *    Function: sd_log_eject_request_event
19707  *
19708  * Description: Log eject request sysevent
19709  *
19710  *     Context: Never called from interrupt context
19711  */
19712 static void
19713 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19714 {
19715 	sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19716 }
19717 
19718 
19719 /*
19720  *    Function: sd_media_change_task
19721  *
19722  * Description: Recovery action for CDROM to become available.
19723  *
19724  *     Context: Executes in a taskq() thread context
19725  */
19726 
19727 static void
19728 sd_media_change_task(void *arg)
19729 {
19730 	struct	scsi_pkt	*pktp = arg;
19731 	struct	sd_lun		*un;
19732 	struct	buf		*bp;
19733 	struct	sd_xbuf		*xp;
19734 	int	err		= 0;
19735 	int	retry_count	= 0;
19736 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19737 	struct	sd_sense_info	si;
19738 
19739 	ASSERT(pktp != NULL);
19740 	bp = (struct buf *)pktp->pkt_private;
19741 	ASSERT(bp != NULL);
19742 	xp = SD_GET_XBUF(bp);
19743 	ASSERT(xp != NULL);
19744 	un = SD_GET_UN(bp);
19745 	ASSERT(un != NULL);
19746 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19747 	ASSERT(un->un_f_monitor_media_state);
19748 
19749 	si.ssi_severity = SCSI_ERR_INFO;
19750 	si.ssi_pfa_flag = FALSE;
19751 
19752 	/*
19753 	 * When a reset is issued on a CDROM, it takes a long time to
19754 	 * recover. First few attempts to read capacity and other things
19755 	 * related to handling unit attention fail (with a ASC 0x4 and
19756 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19757 	 * to limit the retries in other cases of genuine failures like
19758 	 * no media in drive.
19759 	 */
19760 	while (retry_count++ < retry_limit) {
19761 		if ((err = sd_handle_mchange(un)) == 0) {
19762 			break;
19763 		}
19764 		if (err == EAGAIN) {
19765 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19766 		}
19767 		/* Sleep for 0.5 sec. & try again */
19768 		delay(drv_usectohz(500000));
19769 	}
19770 
19771 	/*
19772 	 * Dispatch (retry or fail) the original command here,
19773 	 * along with appropriate console messages....
19774 	 *
19775 	 * Must grab the mutex before calling sd_retry_command,
19776 	 * sd_print_sense_msg and sd_return_failed_command.
19777 	 */
19778 	mutex_enter(SD_MUTEX(un));
19779 	if (err != SD_CMD_SUCCESS) {
19780 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19781 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19782 		si.ssi_severity = SCSI_ERR_FATAL;
19783 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19784 		sd_return_failed_command(un, bp, EIO);
19785 	} else {
19786 		sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19787 		    &si, EIO, (clock_t)0, NULL);
19788 	}
19789 	mutex_exit(SD_MUTEX(un));
19790 }
19791 
19792 
19793 
19794 /*
19795  *    Function: sd_handle_mchange
19796  *
19797  * Description: Perform geometry validation & other recovery when CDROM
19798  *		has been removed from drive.
19799  *
19800  * Return Code: 0 for success
19801  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19802  *		sd_send_scsi_READ_CAPACITY()
19803  *
19804  *     Context: Executes in a taskq() thread context
19805  */
19806 
19807 static int
19808 sd_handle_mchange(struct sd_lun *un)
19809 {
19810 	uint64_t	capacity;
19811 	uint32_t	lbasize;
19812 	int		rval;
19813 	sd_ssc_t	*ssc;
19814 
19815 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19816 	ASSERT(un->un_f_monitor_media_state);
19817 
19818 	ssc = sd_ssc_init(un);
19819 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19820 	    SD_PATH_DIRECT_PRIORITY);
19821 
19822 	if (rval != 0)
19823 		goto failed;
19824 
19825 	mutex_enter(SD_MUTEX(un));
19826 	sd_update_block_info(un, lbasize, capacity);
19827 
19828 	if (un->un_errstats != NULL) {
19829 		struct	sd_errstats *stp =
19830 		    (struct sd_errstats *)un->un_errstats->ks_data;
19831 		stp->sd_capacity.value.ui64 = (uint64_t)
19832 		    ((uint64_t)un->un_blockcount *
19833 		    (uint64_t)un->un_tgt_blocksize);
19834 	}
19835 
19836 	/*
19837 	 * Check if the media in the device is writable or not
19838 	 */
19839 	if (ISCD(un)) {
19840 		sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19841 	}
19842 
19843 	/*
19844 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19845 	 * valid geometry.
19846 	 */
19847 	mutex_exit(SD_MUTEX(un));
19848 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19849 
19850 
19851 	if (cmlb_validate(un->un_cmlbhandle, 0,
19852 	    (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19853 		sd_ssc_fini(ssc);
19854 		return (EIO);
19855 	} else {
19856 		if (un->un_f_pkstats_enabled) {
19857 			sd_set_pstats(un);
19858 			SD_TRACE(SD_LOG_IO_PARTITION, un,
19859 			    "sd_handle_mchange: un:0x%p pstats created and "
19860 			    "set\n", un);
19861 		}
19862 	}
19863 
19864 	/*
19865 	 * Try to lock the door
19866 	 */
19867 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19868 	    SD_PATH_DIRECT_PRIORITY);
19869 failed:
19870 	if (rval != 0)
19871 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19872 	sd_ssc_fini(ssc);
19873 	return (rval);
19874 }
19875 
19876 
19877 /*
19878  *    Function: sd_send_scsi_DOORLOCK
19879  *
19880  * Description: Issue the scsi DOOR LOCK command
19881  *
19882  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19883  *                      structure for this target.
19884  *		flag  - SD_REMOVAL_ALLOW
19885  *			SD_REMOVAL_PREVENT
19886  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19887  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19888  *			to use the USCSI "direct" chain and bypass the normal
19889  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19890  *			command is issued as part of an error recovery action.
19891  *
19892  * Return Code: 0   - Success
19893  *		errno return code from sd_ssc_send()
19894  *
19895  *     Context: Can sleep.
19896  */
19897 
19898 static int
19899 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19900 {
19901 	struct scsi_extended_sense	sense_buf;
19902 	union scsi_cdb		cdb;
19903 	struct uscsi_cmd	ucmd_buf;
19904 	int			status;
19905 	struct sd_lun		*un;
19906 
19907 	ASSERT(ssc != NULL);
19908 	un = ssc->ssc_un;
19909 	ASSERT(un != NULL);
19910 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19911 
19912 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19913 
19914 	/* already determined doorlock is not supported, fake success */
19915 	if (un->un_f_doorlock_supported == FALSE) {
19916 		return (0);
19917 	}
19918 
19919 	/*
19920 	 * If we are ejecting and see an SD_REMOVAL_PREVENT
19921 	 * ignore the command so we can complete the eject
19922 	 * operation.
19923 	 */
19924 	if (flag == SD_REMOVAL_PREVENT) {
19925 		mutex_enter(SD_MUTEX(un));
19926 		if (un->un_f_ejecting == TRUE) {
19927 			mutex_exit(SD_MUTEX(un));
19928 			return (EAGAIN);
19929 		}
19930 		mutex_exit(SD_MUTEX(un));
19931 	}
19932 
19933 	bzero(&cdb, sizeof (cdb));
19934 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19935 
19936 	cdb.scc_cmd = SCMD_DOORLOCK;
19937 	cdb.cdb_opaque[4] = (uchar_t)flag;
19938 
19939 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19940 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19941 	ucmd_buf.uscsi_bufaddr	= NULL;
19942 	ucmd_buf.uscsi_buflen	= 0;
19943 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19944 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19945 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19946 	ucmd_buf.uscsi_timeout	= 15;
19947 
19948 	SD_TRACE(SD_LOG_IO, un,
19949 	    "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
19950 
19951 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
19952 	    UIO_SYSSPACE, path_flag);
19953 
19954 	if (status == 0)
19955 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
19956 
19957 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19958 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19959 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
19960 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19961 
19962 		/* fake success and skip subsequent doorlock commands */
19963 		un->un_f_doorlock_supported = FALSE;
19964 		return (0);
19965 	}
19966 
19967 	return (status);
19968 }
19969 
19970 /*
19971  *    Function: sd_send_scsi_READ_CAPACITY
19972  *
19973  * Description: This routine uses the scsi READ CAPACITY command to determine
19974  *		the device capacity in number of blocks and the device native
19975  *		block size. If this function returns a failure, then the
19976  *		values in *capp and *lbap are undefined.  If the capacity
19977  *		returned is 0xffffffff then the lun is too large for a
19978  *		normal READ CAPACITY command and the results of a
19979  *		READ CAPACITY 16 will be used instead.
19980  *
19981  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
19982  *		capp - ptr to unsigned 64-bit variable to receive the
19983  *			capacity value from the command.
19984  *		lbap - ptr to unsigned 32-bit varaible to receive the
19985  *			block size value from the command
19986  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19987  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19988  *			to use the USCSI "direct" chain and bypass the normal
19989  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19990  *			command is issued as part of an error recovery action.
19991  *
19992  * Return Code: 0   - Success
19993  *		EIO - IO error
19994  *		EACCES - Reservation conflict detected
19995  *		EAGAIN - Device is becoming ready
19996  *		errno return code from sd_ssc_send()
19997  *
19998  *     Context: Can sleep.  Blocks until command completes.
19999  */
20000 
20001 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
20002 
20003 static int
20004 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20005 	int path_flag)
20006 {
20007 	struct	scsi_extended_sense	sense_buf;
20008 	struct	uscsi_cmd	ucmd_buf;
20009 	union	scsi_cdb	cdb;
20010 	uint32_t		*capacity_buf;
20011 	uint64_t		capacity;
20012 	uint32_t		lbasize;
20013 	uint32_t		pbsize;
20014 	int			status;
20015 	struct sd_lun		*un;
20016 
20017 	ASSERT(ssc != NULL);
20018 
20019 	un = ssc->ssc_un;
20020 	ASSERT(un != NULL);
20021 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20022 	ASSERT(capp != NULL);
20023 	ASSERT(lbap != NULL);
20024 
20025 	SD_TRACE(SD_LOG_IO, un,
20026 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20027 
20028 	/*
20029 	 * First send a READ_CAPACITY command to the target.
20030 	 * (This command is mandatory under SCSI-2.)
20031 	 *
20032 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
20033 	 * Medium Indicator bit is cleared.  The address field must be
20034 	 * zero if the PMI bit is zero.
20035 	 */
20036 	bzero(&cdb, sizeof (cdb));
20037 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20038 
20039 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20040 
20041 	cdb.scc_cmd = SCMD_READ_CAPACITY;
20042 
20043 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20044 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20045 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
20046 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
20047 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20048 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20049 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20050 	ucmd_buf.uscsi_timeout	= 60;
20051 
20052 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20053 	    UIO_SYSSPACE, path_flag);
20054 
20055 	switch (status) {
20056 	case 0:
20057 		/* Return failure if we did not get valid capacity data. */
20058 		if (ucmd_buf.uscsi_resid != 0) {
20059 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20060 			    "sd_send_scsi_READ_CAPACITY received invalid "
20061 			    "capacity data");
20062 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20063 			return (EIO);
20064 		}
20065 		/*
20066 		 * Read capacity and block size from the READ CAPACITY 10 data.
20067 		 * This data may be adjusted later due to device specific
20068 		 * issues.
20069 		 *
20070 		 * According to the SCSI spec, the READ CAPACITY 10
20071 		 * command returns the following:
20072 		 *
20073 		 *  bytes 0-3: Maximum logical block address available.
20074 		 *		(MSB in byte:0 & LSB in byte:3)
20075 		 *
20076 		 *  bytes 4-7: Block length in bytes
20077 		 *		(MSB in byte:4 & LSB in byte:7)
20078 		 *
20079 		 */
20080 		capacity = BE_32(capacity_buf[0]);
20081 		lbasize = BE_32(capacity_buf[1]);
20082 
20083 		/*
20084 		 * Done with capacity_buf
20085 		 */
20086 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20087 
20088 		/*
20089 		 * if the reported capacity is set to all 0xf's, then
20090 		 * this disk is too large and requires SBC-2 commands.
20091 		 * Reissue the request using READ CAPACITY 16.
20092 		 */
20093 		if (capacity == 0xffffffff) {
20094 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20095 			status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20096 			    &lbasize, &pbsize, path_flag);
20097 			if (status != 0) {
20098 				return (status);
20099 			} else {
20100 				goto rc16_done;
20101 			}
20102 		}
20103 		break;	/* Success! */
20104 	case EIO:
20105 		switch (ucmd_buf.uscsi_status) {
20106 		case STATUS_RESERVATION_CONFLICT:
20107 			status = EACCES;
20108 			break;
20109 		case STATUS_CHECK:
20110 			/*
20111 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20112 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20113 			 */
20114 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20115 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20116 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20117 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20118 				return (EAGAIN);
20119 			}
20120 			break;
20121 		default:
20122 			break;
20123 		}
20124 		/* FALLTHRU */
20125 	default:
20126 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20127 		return (status);
20128 	}
20129 
20130 	/*
20131 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20132 	 * (2352 and 0 are common) so for these devices always force the value
20133 	 * to 2048 as required by the ATAPI specs.
20134 	 */
20135 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20136 		lbasize = 2048;
20137 	}
20138 
20139 	/*
20140 	 * Get the maximum LBA value from the READ CAPACITY data.
20141 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20142 	 * was cleared when issuing the command. This means that the LBA
20143 	 * returned from the device is the LBA of the last logical block
20144 	 * on the logical unit.  The actual logical block count will be
20145 	 * this value plus one.
20146 	 */
20147 	capacity += 1;
20148 
20149 	/*
20150 	 * Currently, for removable media, the capacity is saved in terms
20151 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20152 	 */
20153 	if (un->un_f_has_removable_media)
20154 		capacity *= (lbasize / un->un_sys_blocksize);
20155 
20156 rc16_done:
20157 
20158 	/*
20159 	 * Copy the values from the READ CAPACITY command into the space
20160 	 * provided by the caller.
20161 	 */
20162 	*capp = capacity;
20163 	*lbap = lbasize;
20164 
20165 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20166 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20167 
20168 	/*
20169 	 * Both the lbasize and capacity from the device must be nonzero,
20170 	 * otherwise we assume that the values are not valid and return
20171 	 * failure to the caller. (4203735)
20172 	 */
20173 	if ((capacity == 0) || (lbasize == 0)) {
20174 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20175 		    "sd_send_scsi_READ_CAPACITY received invalid value "
20176 		    "capacity %llu lbasize %d", capacity, lbasize);
20177 		return (EIO);
20178 	}
20179 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20180 	return (0);
20181 }
20182 
20183 /*
20184  *    Function: sd_send_scsi_READ_CAPACITY_16
20185  *
20186  * Description: This routine uses the scsi READ CAPACITY 16 command to
20187  *		determine the device capacity in number of blocks and the
20188  *		device native block size.  If this function returns a failure,
20189  *		then the values in *capp and *lbap are undefined.
20190  *		This routine should be called by sd_send_scsi_READ_CAPACITY
20191  *              which will apply any device specific adjustments to capacity
20192  *              and lbasize. One exception is it is also called by
20193  *              sd_get_media_info_ext. In that function, there is no need to
20194  *              adjust the capacity and lbasize.
20195  *
20196  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20197  *		capp - ptr to unsigned 64-bit variable to receive the
20198  *			capacity value from the command.
20199  *		lbap - ptr to unsigned 32-bit varaible to receive the
20200  *			block size value from the command
20201  *              psp  - ptr to unsigned 32-bit variable to receive the
20202  *                      physical block size value from the command
20203  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20204  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20205  *			to use the USCSI "direct" chain and bypass the normal
20206  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
20207  *			this command is issued as part of an error recovery
20208  *			action.
20209  *
20210  * Return Code: 0   - Success
20211  *		EIO - IO error
20212  *		EACCES - Reservation conflict detected
20213  *		EAGAIN - Device is becoming ready
20214  *		errno return code from sd_ssc_send()
20215  *
20216  *     Context: Can sleep.  Blocks until command completes.
20217  */
20218 
20219 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
20220 
20221 static int
20222 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20223 	uint32_t *lbap, uint32_t *psp, int path_flag)
20224 {
20225 	struct	scsi_extended_sense	sense_buf;
20226 	struct	uscsi_cmd	ucmd_buf;
20227 	union	scsi_cdb	cdb;
20228 	uint64_t		*capacity16_buf;
20229 	uint64_t		capacity;
20230 	uint32_t		lbasize;
20231 	uint32_t		pbsize;
20232 	uint32_t		lbpb_exp;
20233 	int			status;
20234 	struct sd_lun		*un;
20235 
20236 	ASSERT(ssc != NULL);
20237 
20238 	un = ssc->ssc_un;
20239 	ASSERT(un != NULL);
20240 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20241 	ASSERT(capp != NULL);
20242 	ASSERT(lbap != NULL);
20243 
20244 	SD_TRACE(SD_LOG_IO, un,
20245 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20246 
20247 	/*
20248 	 * First send a READ_CAPACITY_16 command to the target.
20249 	 *
20250 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20251 	 * Medium Indicator bit is cleared.  The address field must be
20252 	 * zero if the PMI bit is zero.
20253 	 */
20254 	bzero(&cdb, sizeof (cdb));
20255 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20256 
20257 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20258 
20259 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20260 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
20261 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
20262 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
20263 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20264 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20265 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20266 	ucmd_buf.uscsi_timeout	= 60;
20267 
20268 	/*
20269 	 * Read Capacity (16) is a Service Action In command.  One
20270 	 * command byte (0x9E) is overloaded for multiple operations,
20271 	 * with the second CDB byte specifying the desired operation
20272 	 */
20273 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20274 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20275 
20276 	/*
20277 	 * Fill in allocation length field
20278 	 */
20279 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20280 
20281 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20282 	    UIO_SYSSPACE, path_flag);
20283 
20284 	switch (status) {
20285 	case 0:
20286 		/* Return failure if we did not get valid capacity data. */
20287 		if (ucmd_buf.uscsi_resid > 20) {
20288 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20289 			    "sd_send_scsi_READ_CAPACITY_16 received invalid "
20290 			    "capacity data");
20291 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20292 			return (EIO);
20293 		}
20294 
20295 		/*
20296 		 * Read capacity and block size from the READ CAPACITY 16 data.
20297 		 * This data may be adjusted later due to device specific
20298 		 * issues.
20299 		 *
20300 		 * According to the SCSI spec, the READ CAPACITY 16
20301 		 * command returns the following:
20302 		 *
20303 		 *  bytes 0-7: Maximum logical block address available.
20304 		 *		(MSB in byte:0 & LSB in byte:7)
20305 		 *
20306 		 *  bytes 8-11: Block length in bytes
20307 		 *		(MSB in byte:8 & LSB in byte:11)
20308 		 *
20309 		 *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20310 		 */
20311 		capacity = BE_64(capacity16_buf[0]);
20312 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20313 		lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20314 
20315 		pbsize = lbasize << lbpb_exp;
20316 
20317 		/*
20318 		 * Done with capacity16_buf
20319 		 */
20320 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20321 
20322 		/*
20323 		 * if the reported capacity is set to all 0xf's, then
20324 		 * this disk is too large.  This could only happen with
20325 		 * a device that supports LBAs larger than 64 bits which
20326 		 * are not defined by any current T10 standards.
20327 		 */
20328 		if (capacity == 0xffffffffffffffff) {
20329 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20330 			    "disk is too large");
20331 			return (EIO);
20332 		}
20333 		break;	/* Success! */
20334 	case EIO:
20335 		switch (ucmd_buf.uscsi_status) {
20336 		case STATUS_RESERVATION_CONFLICT:
20337 			status = EACCES;
20338 			break;
20339 		case STATUS_CHECK:
20340 			/*
20341 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20342 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20343 			 */
20344 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20345 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20346 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20347 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20348 				return (EAGAIN);
20349 			}
20350 			break;
20351 		default:
20352 			break;
20353 		}
20354 		/* FALLTHRU */
20355 	default:
20356 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20357 		return (status);
20358 	}
20359 
20360 	/*
20361 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20362 	 * (2352 and 0 are common) so for these devices always force the value
20363 	 * to 2048 as required by the ATAPI specs.
20364 	 */
20365 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20366 		lbasize = 2048;
20367 	}
20368 
20369 	/*
20370 	 * Get the maximum LBA value from the READ CAPACITY 16 data.
20371 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20372 	 * was cleared when issuing the command. This means that the LBA
20373 	 * returned from the device is the LBA of the last logical block
20374 	 * on the logical unit.  The actual logical block count will be
20375 	 * this value plus one.
20376 	 */
20377 	capacity += 1;
20378 
20379 	/*
20380 	 * Currently, for removable media, the capacity is saved in terms
20381 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20382 	 */
20383 	if (un->un_f_has_removable_media)
20384 		capacity *= (lbasize / un->un_sys_blocksize);
20385 
20386 	*capp = capacity;
20387 	*lbap = lbasize;
20388 	*psp = pbsize;
20389 
20390 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20391 	    "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20392 	    capacity, lbasize, pbsize);
20393 
20394 	if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20395 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20396 		    "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20397 		    "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20398 		return (EIO);
20399 	}
20400 
20401 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20402 	return (0);
20403 }
20404 
20405 
20406 /*
20407  *    Function: sd_send_scsi_START_STOP_UNIT
20408  *
20409  * Description: Issue a scsi START STOP UNIT command to the target.
20410  *
20411  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20412  *                       structure for this target.
20413  *      pc_flag - SD_POWER_CONDITION
20414  *                SD_START_STOP
20415  *		flag  - SD_TARGET_START
20416  *			SD_TARGET_STOP
20417  *			SD_TARGET_EJECT
20418  *			SD_TARGET_CLOSE
20419  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20420  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20421  *			to use the USCSI "direct" chain and bypass the normal
20422  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20423  *			command is issued as part of an error recovery action.
20424  *
20425  * Return Code: 0   - Success
20426  *		EIO - IO error
20427  *		EACCES - Reservation conflict detected
20428  *		ENXIO  - Not Ready, medium not present
20429  *		errno return code from sd_ssc_send()
20430  *
20431  *     Context: Can sleep.
20432  */
20433 
20434 static int
20435 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20436     int path_flag)
20437 {
20438 	struct	scsi_extended_sense	sense_buf;
20439 	union scsi_cdb		cdb;
20440 	struct uscsi_cmd	ucmd_buf;
20441 	int			status;
20442 	struct sd_lun		*un;
20443 
20444 	ASSERT(ssc != NULL);
20445 	un = ssc->ssc_un;
20446 	ASSERT(un != NULL);
20447 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20448 
20449 	SD_TRACE(SD_LOG_IO, un,
20450 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20451 
20452 	if (un->un_f_check_start_stop &&
20453 	    (pc_flag == SD_START_STOP) &&
20454 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20455 	    (un->un_f_start_stop_supported != TRUE)) {
20456 		return (0);
20457 	}
20458 
20459 	/*
20460 	 * If we are performing an eject operation and
20461 	 * we receive any command other than SD_TARGET_EJECT
20462 	 * we should immediately return.
20463 	 */
20464 	if (flag != SD_TARGET_EJECT) {
20465 		mutex_enter(SD_MUTEX(un));
20466 		if (un->un_f_ejecting == TRUE) {
20467 			mutex_exit(SD_MUTEX(un));
20468 			return (EAGAIN);
20469 		}
20470 		mutex_exit(SD_MUTEX(un));
20471 	}
20472 
20473 	bzero(&cdb, sizeof (cdb));
20474 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20475 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20476 
20477 	cdb.scc_cmd = SCMD_START_STOP;
20478 	cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20479 	    (uchar_t)(flag << 4) : (uchar_t)flag;
20480 
20481 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20482 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20483 	ucmd_buf.uscsi_bufaddr	= NULL;
20484 	ucmd_buf.uscsi_buflen	= 0;
20485 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20486 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20487 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20488 	ucmd_buf.uscsi_timeout	= 200;
20489 
20490 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20491 	    UIO_SYSSPACE, path_flag);
20492 
20493 	switch (status) {
20494 	case 0:
20495 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20496 		break;	/* Success! */
20497 	case EIO:
20498 		switch (ucmd_buf.uscsi_status) {
20499 		case STATUS_RESERVATION_CONFLICT:
20500 			status = EACCES;
20501 			break;
20502 		case STATUS_CHECK:
20503 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20504 				switch (scsi_sense_key(
20505 				    (uint8_t *)&sense_buf)) {
20506 				case KEY_ILLEGAL_REQUEST:
20507 					status = ENOTSUP;
20508 					break;
20509 				case KEY_NOT_READY:
20510 					if (scsi_sense_asc(
20511 					    (uint8_t *)&sense_buf)
20512 					    == 0x3A) {
20513 						status = ENXIO;
20514 					}
20515 					break;
20516 				default:
20517 					break;
20518 				}
20519 			}
20520 			break;
20521 		default:
20522 			break;
20523 		}
20524 		break;
20525 	default:
20526 		break;
20527 	}
20528 
20529 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20530 
20531 	return (status);
20532 }
20533 
20534 
20535 /*
20536  *    Function: sd_start_stop_unit_callback
20537  *
20538  * Description: timeout(9F) callback to begin recovery process for a
20539  *		device that has spun down.
20540  *
20541  *   Arguments: arg - pointer to associated softstate struct.
20542  *
20543  *     Context: Executes in a timeout(9F) thread context
20544  */
20545 
20546 static void
20547 sd_start_stop_unit_callback(void *arg)
20548 {
20549 	struct sd_lun	*un = arg;
20550 	ASSERT(un != NULL);
20551 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20552 
20553 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20554 
20555 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20556 }
20557 
20558 
20559 /*
20560  *    Function: sd_start_stop_unit_task
20561  *
20562  * Description: Recovery procedure when a drive is spun down.
20563  *
20564  *   Arguments: arg - pointer to associated softstate struct.
20565  *
20566  *     Context: Executes in a taskq() thread context
20567  */
20568 
20569 static void
20570 sd_start_stop_unit_task(void *arg)
20571 {
20572 	struct sd_lun	*un = arg;
20573 	sd_ssc_t	*ssc;
20574 	int		power_level;
20575 	int		rval;
20576 
20577 	ASSERT(un != NULL);
20578 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20579 
20580 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20581 
20582 	/*
20583 	 * Some unformatted drives report not ready error, no need to
20584 	 * restart if format has been initiated.
20585 	 */
20586 	mutex_enter(SD_MUTEX(un));
20587 	if (un->un_f_format_in_progress == TRUE) {
20588 		mutex_exit(SD_MUTEX(un));
20589 		return;
20590 	}
20591 	mutex_exit(SD_MUTEX(un));
20592 
20593 	ssc = sd_ssc_init(un);
20594 	/*
20595 	 * When a START STOP command is issued from here, it is part of a
20596 	 * failure recovery operation and must be issued before any other
20597 	 * commands, including any pending retries. Thus it must be sent
20598 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20599 	 * succeeds or not, we will start I/O after the attempt.
20600 	 * If power condition is supported and the current power level
20601 	 * is capable of performing I/O, we should set the power condition
20602 	 * to that level. Otherwise, set the power condition to ACTIVE.
20603 	 */
20604 	if (un->un_f_power_condition_supported) {
20605 		mutex_enter(SD_MUTEX(un));
20606 		ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20607 		power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20608 		    > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20609 		mutex_exit(SD_MUTEX(un));
20610 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20611 		    sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20612 	} else {
20613 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20614 		    SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20615 	}
20616 
20617 	if (rval != 0)
20618 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20619 	sd_ssc_fini(ssc);
20620 	/*
20621 	 * The above call blocks until the START_STOP_UNIT command completes.
20622 	 * Now that it has completed, we must re-try the original IO that
20623 	 * received the NOT READY condition in the first place. There are
20624 	 * three possible conditions here:
20625 	 *
20626 	 *  (1) The original IO is on un_retry_bp.
20627 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
20628 	 *	is NULL.
20629 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
20630 	 *	points to some other, unrelated bp.
20631 	 *
20632 	 * For each case, we must call sd_start_cmds() with un_retry_bp
20633 	 * as the argument. If un_retry_bp is NULL, this will initiate
20634 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
20635 	 * then this will process the bp on un_retry_bp. That may or may not
20636 	 * be the original IO, but that does not matter: the important thing
20637 	 * is to keep the IO processing going at this point.
20638 	 *
20639 	 * Note: This is a very specific error recovery sequence associated
20640 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20641 	 * serialize the I/O with completion of the spin-up.
20642 	 */
20643 	mutex_enter(SD_MUTEX(un));
20644 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20645 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20646 	    un, un->un_retry_bp);
20647 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20648 	sd_start_cmds(un, un->un_retry_bp);
20649 	mutex_exit(SD_MUTEX(un));
20650 
20651 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20652 }
20653 
20654 
20655 /*
20656  *    Function: sd_send_scsi_INQUIRY
20657  *
20658  * Description: Issue the scsi INQUIRY command.
20659  *
20660  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20661  *                      structure for this target.
20662  *		bufaddr
20663  *		buflen
20664  *		evpd
20665  *		page_code
20666  *		page_length
20667  *
20668  * Return Code: 0   - Success
20669  *		errno return code from sd_ssc_send()
20670  *
20671  *     Context: Can sleep. Does not return until command is completed.
20672  */
20673 
20674 static int
20675 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20676 	uchar_t evpd, uchar_t page_code, size_t *residp)
20677 {
20678 	union scsi_cdb		cdb;
20679 	struct uscsi_cmd	ucmd_buf;
20680 	int			status;
20681 	struct sd_lun		*un;
20682 
20683 	ASSERT(ssc != NULL);
20684 	un = ssc->ssc_un;
20685 	ASSERT(un != NULL);
20686 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20687 	ASSERT(bufaddr != NULL);
20688 
20689 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20690 
20691 	bzero(&cdb, sizeof (cdb));
20692 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20693 	bzero(bufaddr, buflen);
20694 
20695 	cdb.scc_cmd = SCMD_INQUIRY;
20696 	cdb.cdb_opaque[1] = evpd;
20697 	cdb.cdb_opaque[2] = page_code;
20698 	FORMG0COUNT(&cdb, buflen);
20699 
20700 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20701 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20702 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20703 	ucmd_buf.uscsi_buflen	= buflen;
20704 	ucmd_buf.uscsi_rqbuf	= NULL;
20705 	ucmd_buf.uscsi_rqlen	= 0;
20706 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20707 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20708 
20709 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20710 	    UIO_SYSSPACE, SD_PATH_DIRECT);
20711 
20712 	/*
20713 	 * Only handle status == 0, the upper-level caller
20714 	 * will put different assessment based on the context.
20715 	 */
20716 	if (status == 0)
20717 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20718 
20719 	if ((status == 0) && (residp != NULL)) {
20720 		*residp = ucmd_buf.uscsi_resid;
20721 	}
20722 
20723 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20724 
20725 	return (status);
20726 }
20727 
20728 
20729 /*
20730  *    Function: sd_send_scsi_TEST_UNIT_READY
20731  *
20732  * Description: Issue the scsi TEST UNIT READY command.
20733  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20734  *		prevent retrying failed commands. Use this when the intent
20735  *		is either to check for device readiness, to clear a Unit
20736  *		Attention, or to clear any outstanding sense data.
20737  *		However under specific conditions the expected behavior
20738  *		is for retries to bring a device ready, so use the flag
20739  *		with caution.
20740  *
20741  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20742  *                      structure for this target.
20743  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20744  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20745  *			0: dont check for media present, do retries on cmd.
20746  *
20747  * Return Code: 0   - Success
20748  *		EIO - IO error
20749  *		EACCES - Reservation conflict detected
20750  *		ENXIO  - Not Ready, medium not present
20751  *		errno return code from sd_ssc_send()
20752  *
20753  *     Context: Can sleep. Does not return until command is completed.
20754  */
20755 
20756 static int
20757 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20758 {
20759 	struct	scsi_extended_sense	sense_buf;
20760 	union scsi_cdb		cdb;
20761 	struct uscsi_cmd	ucmd_buf;
20762 	int			status;
20763 	struct sd_lun		*un;
20764 
20765 	ASSERT(ssc != NULL);
20766 	un = ssc->ssc_un;
20767 	ASSERT(un != NULL);
20768 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20769 
20770 	SD_TRACE(SD_LOG_IO, un,
20771 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20772 
20773 	/*
20774 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20775 	 * timeouts when they receive a TUR and the queue is not empty. Check
20776 	 * the configuration flag set during attach (indicating the drive has
20777 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20778 	 * TUR. If there are
20779 	 * pending commands return success, this is a bit arbitrary but is ok
20780 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20781 	 * configurations.
20782 	 */
20783 	if (un->un_f_cfg_tur_check == TRUE) {
20784 		mutex_enter(SD_MUTEX(un));
20785 		if (un->un_ncmds_in_transport != 0) {
20786 			mutex_exit(SD_MUTEX(un));
20787 			return (0);
20788 		}
20789 		mutex_exit(SD_MUTEX(un));
20790 	}
20791 
20792 	bzero(&cdb, sizeof (cdb));
20793 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20794 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20795 
20796 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20797 
20798 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20799 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20800 	ucmd_buf.uscsi_bufaddr	= NULL;
20801 	ucmd_buf.uscsi_buflen	= 0;
20802 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20803 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20804 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20805 
20806 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20807 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20808 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20809 	}
20810 	ucmd_buf.uscsi_timeout	= 60;
20811 
20812 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20813 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20814 	    SD_PATH_STANDARD));
20815 
20816 	switch (status) {
20817 	case 0:
20818 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20819 		break;	/* Success! */
20820 	case EIO:
20821 		switch (ucmd_buf.uscsi_status) {
20822 		case STATUS_RESERVATION_CONFLICT:
20823 			status = EACCES;
20824 			break;
20825 		case STATUS_CHECK:
20826 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20827 				break;
20828 			}
20829 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20830 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20831 			    KEY_NOT_READY) &&
20832 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20833 				status = ENXIO;
20834 			}
20835 			break;
20836 		default:
20837 			break;
20838 		}
20839 		break;
20840 	default:
20841 		break;
20842 	}
20843 
20844 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20845 
20846 	return (status);
20847 }
20848 
20849 /*
20850  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20851  *
20852  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20853  *
20854  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20855  *                      structure for this target.
20856  *
20857  * Return Code: 0   - Success
20858  *		EACCES
20859  *		ENOTSUP
20860  *		errno return code from sd_ssc_send()
20861  *
20862  *     Context: Can sleep. Does not return until command is completed.
20863  */
20864 
20865 static int
20866 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t  usr_cmd,
20867 	uint16_t data_len, uchar_t *data_bufp)
20868 {
20869 	struct scsi_extended_sense	sense_buf;
20870 	union scsi_cdb		cdb;
20871 	struct uscsi_cmd	ucmd_buf;
20872 	int			status;
20873 	int			no_caller_buf = FALSE;
20874 	struct sd_lun		*un;
20875 
20876 	ASSERT(ssc != NULL);
20877 	un = ssc->ssc_un;
20878 	ASSERT(un != NULL);
20879 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20880 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20881 
20882 	SD_TRACE(SD_LOG_IO, un,
20883 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20884 
20885 	bzero(&cdb, sizeof (cdb));
20886 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20887 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20888 	if (data_bufp == NULL) {
20889 		/* Allocate a default buf if the caller did not give one */
20890 		ASSERT(data_len == 0);
20891 		data_len  = MHIOC_RESV_KEY_SIZE;
20892 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20893 		no_caller_buf = TRUE;
20894 	}
20895 
20896 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20897 	cdb.cdb_opaque[1] = usr_cmd;
20898 	FORMG1COUNT(&cdb, data_len);
20899 
20900 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20901 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20902 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20903 	ucmd_buf.uscsi_buflen	= data_len;
20904 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20905 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20906 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20907 	ucmd_buf.uscsi_timeout	= 60;
20908 
20909 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20910 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20911 
20912 	switch (status) {
20913 	case 0:
20914 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20915 
20916 		break;	/* Success! */
20917 	case EIO:
20918 		switch (ucmd_buf.uscsi_status) {
20919 		case STATUS_RESERVATION_CONFLICT:
20920 			status = EACCES;
20921 			break;
20922 		case STATUS_CHECK:
20923 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20924 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20925 			    KEY_ILLEGAL_REQUEST)) {
20926 				status = ENOTSUP;
20927 			}
20928 			break;
20929 		default:
20930 			break;
20931 		}
20932 		break;
20933 	default:
20934 		break;
20935 	}
20936 
20937 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20938 
20939 	if (no_caller_buf == TRUE) {
20940 		kmem_free(data_bufp, data_len);
20941 	}
20942 
20943 	return (status);
20944 }
20945 
20946 
20947 /*
20948  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
20949  *
20950  * Description: This routine is the driver entry point for handling CD-ROM
20951  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
20952  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
20953  *		device.
20954  *
20955  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
20956  *                      for the target.
20957  *		usr_cmd SCSI-3 reservation facility command (one of
20958  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
20959  *			SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
20960  *		usr_bufp - user provided pointer register, reserve descriptor or
20961  *			preempt and abort structure (mhioc_register_t,
20962  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
20963  *
20964  * Return Code: 0   - Success
20965  *		EACCES
20966  *		ENOTSUP
20967  *		errno return code from sd_ssc_send()
20968  *
20969  *     Context: Can sleep. Does not return until command is completed.
20970  */
20971 
20972 static int
20973 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
20974 	uchar_t	*usr_bufp)
20975 {
20976 	struct scsi_extended_sense	sense_buf;
20977 	union scsi_cdb		cdb;
20978 	struct uscsi_cmd	ucmd_buf;
20979 	int			status;
20980 	uchar_t			data_len = sizeof (sd_prout_t);
20981 	sd_prout_t		*prp;
20982 	struct sd_lun		*un;
20983 
20984 	ASSERT(ssc != NULL);
20985 	un = ssc->ssc_un;
20986 	ASSERT(un != NULL);
20987 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20988 	ASSERT(data_len == 24);	/* required by scsi spec */
20989 
20990 	SD_TRACE(SD_LOG_IO, un,
20991 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
20992 
20993 	if (usr_bufp == NULL) {
20994 		return (EINVAL);
20995 	}
20996 
20997 	bzero(&cdb, sizeof (cdb));
20998 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20999 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21000 	prp = kmem_zalloc(data_len, KM_SLEEP);
21001 
21002 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21003 	cdb.cdb_opaque[1] = usr_cmd;
21004 	FORMG1COUNT(&cdb, data_len);
21005 
21006 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21007 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21008 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
21009 	ucmd_buf.uscsi_buflen	= data_len;
21010 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21011 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21012 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21013 	ucmd_buf.uscsi_timeout	= 60;
21014 
21015 	switch (usr_cmd) {
21016 	case SD_SCSI3_REGISTER: {
21017 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21018 
21019 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21020 		bcopy(ptr->newkey.key, prp->service_key,
21021 		    MHIOC_RESV_KEY_SIZE);
21022 		prp->aptpl = ptr->aptpl;
21023 		break;
21024 	}
21025 	case SD_SCSI3_CLEAR: {
21026 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21027 
21028 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21029 		break;
21030 	}
21031 	case SD_SCSI3_RESERVE:
21032 	case SD_SCSI3_RELEASE: {
21033 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21034 
21035 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21036 		prp->scope_address = BE_32(ptr->scope_specific_addr);
21037 		cdb.cdb_opaque[2] = ptr->type;
21038 		break;
21039 	}
21040 	case SD_SCSI3_PREEMPTANDABORT: {
21041 		mhioc_preemptandabort_t *ptr =
21042 		    (mhioc_preemptandabort_t *)usr_bufp;
21043 
21044 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21045 		bcopy(ptr->victim_key.key, prp->service_key,
21046 		    MHIOC_RESV_KEY_SIZE);
21047 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21048 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
21049 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
21050 		break;
21051 	}
21052 	case SD_SCSI3_REGISTERANDIGNOREKEY:
21053 	{
21054 		mhioc_registerandignorekey_t *ptr;
21055 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21056 		bcopy(ptr->newkey.key,
21057 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
21058 		prp->aptpl = ptr->aptpl;
21059 		break;
21060 	}
21061 	default:
21062 		ASSERT(FALSE);
21063 		break;
21064 	}
21065 
21066 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21067 	    UIO_SYSSPACE, SD_PATH_STANDARD);
21068 
21069 	switch (status) {
21070 	case 0:
21071 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21072 		break;	/* Success! */
21073 	case EIO:
21074 		switch (ucmd_buf.uscsi_status) {
21075 		case STATUS_RESERVATION_CONFLICT:
21076 			status = EACCES;
21077 			break;
21078 		case STATUS_CHECK:
21079 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21080 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21081 			    KEY_ILLEGAL_REQUEST)) {
21082 				status = ENOTSUP;
21083 			}
21084 			break;
21085 		default:
21086 			break;
21087 		}
21088 		break;
21089 	default:
21090 		break;
21091 	}
21092 
21093 	kmem_free(prp, data_len);
21094 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21095 	return (status);
21096 }
21097 
21098 
21099 /*
21100  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
21101  *
21102  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21103  *
21104  *   Arguments: un - pointer to the target's soft state struct
21105  *              dkc - pointer to the callback structure
21106  *
21107  * Return Code: 0 - success
21108  *		errno-type error code
21109  *
21110  *     Context: kernel thread context only.
21111  *
21112  *  _______________________________________________________________
21113  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
21114  * |FLUSH_VOLATILE|              | operation                       |
21115  * |______________|______________|_________________________________|
21116  * | 0            | NULL         | Synchronous flush on both       |
21117  * |              |              | volatile and non-volatile cache |
21118  * |______________|______________|_________________________________|
21119  * | 1            | NULL         | Synchronous flush on volatile   |
21120  * |              |              | cache; disk drivers may suppress|
21121  * |              |              | flush if disk table indicates   |
21122  * |              |              | non-volatile cache              |
21123  * |______________|______________|_________________________________|
21124  * | 0            | !NULL        | Asynchronous flush on both      |
21125  * |              |              | volatile and non-volatile cache;|
21126  * |______________|______________|_________________________________|
21127  * | 1            | !NULL        | Asynchronous flush on volatile  |
21128  * |              |              | cache; disk drivers may suppress|
21129  * |              |              | flush if disk table indicates   |
21130  * |              |              | non-volatile cache              |
21131  * |______________|______________|_________________________________|
21132  *
21133  */
21134 
21135 static int
21136 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21137 {
21138 	struct sd_uscsi_info	*uip;
21139 	struct uscsi_cmd	*uscmd;
21140 	union scsi_cdb		*cdb;
21141 	struct buf		*bp;
21142 	int			rval = 0;
21143 	int			is_async;
21144 
21145 	SD_TRACE(SD_LOG_IO, un,
21146 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21147 
21148 	ASSERT(un != NULL);
21149 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21150 
21151 	if (dkc == NULL || dkc->dkc_callback == NULL) {
21152 		is_async = FALSE;
21153 	} else {
21154 		is_async = TRUE;
21155 	}
21156 
21157 	mutex_enter(SD_MUTEX(un));
21158 	/* check whether cache flush should be suppressed */
21159 	if (un->un_f_suppress_cache_flush == TRUE) {
21160 		mutex_exit(SD_MUTEX(un));
21161 		/*
21162 		 * suppress the cache flush if the device is told to do
21163 		 * so by sd.conf or disk table
21164 		 */
21165 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21166 		    skip the cache flush since suppress_cache_flush is %d!\n",
21167 		    un->un_f_suppress_cache_flush);
21168 
21169 		if (is_async == TRUE) {
21170 			/* invoke callback for asynchronous flush */
21171 			(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21172 		}
21173 		return (rval);
21174 	}
21175 	mutex_exit(SD_MUTEX(un));
21176 
21177 	/*
21178 	 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21179 	 * set properly
21180 	 */
21181 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21182 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21183 
21184 	mutex_enter(SD_MUTEX(un));
21185 	if (dkc != NULL && un->un_f_sync_nv_supported &&
21186 	    (dkc->dkc_flag & FLUSH_VOLATILE)) {
21187 		/*
21188 		 * if the device supports SYNC_NV bit, turn on
21189 		 * the SYNC_NV bit to only flush volatile cache
21190 		 */
21191 		cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21192 	}
21193 	mutex_exit(SD_MUTEX(un));
21194 
21195 	/*
21196 	 * First get some memory for the uscsi_cmd struct and cdb
21197 	 * and initialize for SYNCHRONIZE_CACHE cmd.
21198 	 */
21199 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21200 	uscmd->uscsi_cdblen = CDB_GROUP1;
21201 	uscmd->uscsi_cdb = (caddr_t)cdb;
21202 	uscmd->uscsi_bufaddr = NULL;
21203 	uscmd->uscsi_buflen = 0;
21204 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21205 	uscmd->uscsi_rqlen = SENSE_LENGTH;
21206 	uscmd->uscsi_rqresid = SENSE_LENGTH;
21207 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21208 	uscmd->uscsi_timeout = sd_io_time;
21209 
21210 	/*
21211 	 * Allocate an sd_uscsi_info struct and fill it with the info
21212 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21213 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21214 	 * since we allocate the buf here in this function, we do not
21215 	 * need to preserve the prior contents of b_private.
21216 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21217 	 */
21218 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21219 	uip->ui_flags = SD_PATH_DIRECT;
21220 	uip->ui_cmdp  = uscmd;
21221 
21222 	bp = getrbuf(KM_SLEEP);
21223 	bp->b_private = uip;
21224 
21225 	/*
21226 	 * Setup buffer to carry uscsi request.
21227 	 */
21228 	bp->b_flags  = B_BUSY;
21229 	bp->b_bcount = 0;
21230 	bp->b_blkno  = 0;
21231 
21232 	if (is_async == TRUE) {
21233 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21234 		uip->ui_dkc = *dkc;
21235 	}
21236 
21237 	bp->b_edev = SD_GET_DEV(un);
21238 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
21239 
21240 	/*
21241 	 * Unset un_f_sync_cache_required flag
21242 	 */
21243 	mutex_enter(SD_MUTEX(un));
21244 	un->un_f_sync_cache_required = FALSE;
21245 	mutex_exit(SD_MUTEX(un));
21246 
21247 	(void) sd_uscsi_strategy(bp);
21248 
21249 	/*
21250 	 * If synchronous request, wait for completion
21251 	 * If async just return and let b_iodone callback
21252 	 * cleanup.
21253 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
21254 	 * but it was also incremented in sd_uscsi_strategy(), so
21255 	 * we should be ok.
21256 	 */
21257 	if (is_async == FALSE) {
21258 		(void) biowait(bp);
21259 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21260 	}
21261 
21262 	return (rval);
21263 }
21264 
21265 
21266 static int
21267 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21268 {
21269 	struct sd_uscsi_info *uip;
21270 	struct uscsi_cmd *uscmd;
21271 	uint8_t *sense_buf;
21272 	struct sd_lun *un;
21273 	int status;
21274 	union scsi_cdb *cdb;
21275 
21276 	uip = (struct sd_uscsi_info *)(bp->b_private);
21277 	ASSERT(uip != NULL);
21278 
21279 	uscmd = uip->ui_cmdp;
21280 	ASSERT(uscmd != NULL);
21281 
21282 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21283 	ASSERT(sense_buf != NULL);
21284 
21285 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21286 	ASSERT(un != NULL);
21287 
21288 	cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21289 
21290 	status = geterror(bp);
21291 	switch (status) {
21292 	case 0:
21293 		break;	/* Success! */
21294 	case EIO:
21295 		switch (uscmd->uscsi_status) {
21296 		case STATUS_RESERVATION_CONFLICT:
21297 			/* Ignore reservation conflict */
21298 			status = 0;
21299 			goto done;
21300 
21301 		case STATUS_CHECK:
21302 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21303 			    (scsi_sense_key(sense_buf) ==
21304 			    KEY_ILLEGAL_REQUEST)) {
21305 				/* Ignore Illegal Request error */
21306 				if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21307 					mutex_enter(SD_MUTEX(un));
21308 					un->un_f_sync_nv_supported = FALSE;
21309 					mutex_exit(SD_MUTEX(un));
21310 					status = 0;
21311 					SD_TRACE(SD_LOG_IO, un,
21312 					    "un_f_sync_nv_supported \
21313 					    is set to false.\n");
21314 					goto done;
21315 				}
21316 
21317 				mutex_enter(SD_MUTEX(un));
21318 				un->un_f_sync_cache_supported = FALSE;
21319 				mutex_exit(SD_MUTEX(un));
21320 				SD_TRACE(SD_LOG_IO, un,
21321 				    "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21322 				    un_f_sync_cache_supported set to false \
21323 				    with asc = %x, ascq = %x\n",
21324 				    scsi_sense_asc(sense_buf),
21325 				    scsi_sense_ascq(sense_buf));
21326 				status = ENOTSUP;
21327 				goto done;
21328 			}
21329 			break;
21330 		default:
21331 			break;
21332 		}
21333 		/* FALLTHRU */
21334 	default:
21335 		/*
21336 		 * Turn on the un_f_sync_cache_required flag
21337 		 * since the SYNC CACHE command failed
21338 		 */
21339 		mutex_enter(SD_MUTEX(un));
21340 		un->un_f_sync_cache_required = TRUE;
21341 		mutex_exit(SD_MUTEX(un));
21342 
21343 		/*
21344 		 * Don't log an error message if this device
21345 		 * has removable media.
21346 		 */
21347 		if (!un->un_f_has_removable_media) {
21348 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21349 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
21350 		}
21351 		break;
21352 	}
21353 
21354 done:
21355 	if (uip->ui_dkc.dkc_callback != NULL) {
21356 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21357 	}
21358 
21359 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
21360 	freerbuf(bp);
21361 	kmem_free(uip, sizeof (struct sd_uscsi_info));
21362 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21363 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21364 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
21365 
21366 	return (status);
21367 }
21368 
21369 
21370 /*
21371  *    Function: sd_send_scsi_GET_CONFIGURATION
21372  *
21373  * Description: Issues the get configuration command to the device.
21374  *		Called from sd_check_for_writable_cd & sd_get_media_info
21375  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21376  *   Arguments: ssc
21377  *		ucmdbuf
21378  *		rqbuf
21379  *		rqbuflen
21380  *		bufaddr
21381  *		buflen
21382  *		path_flag
21383  *
21384  * Return Code: 0   - Success
21385  *		errno return code from sd_ssc_send()
21386  *
21387  *     Context: Can sleep. Does not return until command is completed.
21388  *
21389  */
21390 
21391 static int
21392 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21393 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21394 	int path_flag)
21395 {
21396 	char	cdb[CDB_GROUP1];
21397 	int	status;
21398 	struct sd_lun	*un;
21399 
21400 	ASSERT(ssc != NULL);
21401 	un = ssc->ssc_un;
21402 	ASSERT(un != NULL);
21403 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21404 	ASSERT(bufaddr != NULL);
21405 	ASSERT(ucmdbuf != NULL);
21406 	ASSERT(rqbuf != NULL);
21407 
21408 	SD_TRACE(SD_LOG_IO, un,
21409 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21410 
21411 	bzero(cdb, sizeof (cdb));
21412 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21413 	bzero(rqbuf, rqbuflen);
21414 	bzero(bufaddr, buflen);
21415 
21416 	/*
21417 	 * Set up cdb field for the get configuration command.
21418 	 */
21419 	cdb[0] = SCMD_GET_CONFIGURATION;
21420 	cdb[1] = 0x02;  /* Requested Type */
21421 	cdb[8] = SD_PROFILE_HEADER_LEN;
21422 	ucmdbuf->uscsi_cdb = cdb;
21423 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21424 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21425 	ucmdbuf->uscsi_buflen = buflen;
21426 	ucmdbuf->uscsi_timeout = sd_io_time;
21427 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21428 	ucmdbuf->uscsi_rqlen = rqbuflen;
21429 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21430 
21431 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21432 	    UIO_SYSSPACE, path_flag);
21433 
21434 	switch (status) {
21435 	case 0:
21436 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21437 		break;  /* Success! */
21438 	case EIO:
21439 		switch (ucmdbuf->uscsi_status) {
21440 		case STATUS_RESERVATION_CONFLICT:
21441 			status = EACCES;
21442 			break;
21443 		default:
21444 			break;
21445 		}
21446 		break;
21447 	default:
21448 		break;
21449 	}
21450 
21451 	if (status == 0) {
21452 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21453 		    "sd_send_scsi_GET_CONFIGURATION: data",
21454 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21455 	}
21456 
21457 	SD_TRACE(SD_LOG_IO, un,
21458 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
21459 
21460 	return (status);
21461 }
21462 
21463 /*
21464  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21465  *
21466  * Description: Issues the get configuration command to the device to
21467  *              retrieve a specific feature. Called from
21468  *		sd_check_for_writable_cd & sd_set_mmc_caps.
21469  *   Arguments: ssc
21470  *              ucmdbuf
21471  *              rqbuf
21472  *              rqbuflen
21473  *              bufaddr
21474  *              buflen
21475  *		feature
21476  *
21477  * Return Code: 0   - Success
21478  *              errno return code from sd_ssc_send()
21479  *
21480  *     Context: Can sleep. Does not return until command is completed.
21481  *
21482  */
21483 static int
21484 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
21485 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
21486 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag)
21487 {
21488 	char    cdb[CDB_GROUP1];
21489 	int	status;
21490 	struct sd_lun	*un;
21491 
21492 	ASSERT(ssc != NULL);
21493 	un = ssc->ssc_un;
21494 	ASSERT(un != NULL);
21495 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21496 	ASSERT(bufaddr != NULL);
21497 	ASSERT(ucmdbuf != NULL);
21498 	ASSERT(rqbuf != NULL);
21499 
21500 	SD_TRACE(SD_LOG_IO, un,
21501 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21502 
21503 	bzero(cdb, sizeof (cdb));
21504 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21505 	bzero(rqbuf, rqbuflen);
21506 	bzero(bufaddr, buflen);
21507 
21508 	/*
21509 	 * Set up cdb field for the get configuration command.
21510 	 */
21511 	cdb[0] = SCMD_GET_CONFIGURATION;
21512 	cdb[1] = 0x02;  /* Requested Type */
21513 	cdb[3] = feature;
21514 	cdb[8] = buflen;
21515 	ucmdbuf->uscsi_cdb = cdb;
21516 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21517 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21518 	ucmdbuf->uscsi_buflen = buflen;
21519 	ucmdbuf->uscsi_timeout = sd_io_time;
21520 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21521 	ucmdbuf->uscsi_rqlen = rqbuflen;
21522 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21523 
21524 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21525 	    UIO_SYSSPACE, path_flag);
21526 
21527 	switch (status) {
21528 	case 0:
21529 
21530 		break;  /* Success! */
21531 	case EIO:
21532 		switch (ucmdbuf->uscsi_status) {
21533 		case STATUS_RESERVATION_CONFLICT:
21534 			status = EACCES;
21535 			break;
21536 		default:
21537 			break;
21538 		}
21539 		break;
21540 	default:
21541 		break;
21542 	}
21543 
21544 	if (status == 0) {
21545 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21546 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
21547 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21548 	}
21549 
21550 	SD_TRACE(SD_LOG_IO, un,
21551 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21552 
21553 	return (status);
21554 }
21555 
21556 
21557 /*
21558  *    Function: sd_send_scsi_MODE_SENSE
21559  *
21560  * Description: Utility function for issuing a scsi MODE SENSE command.
21561  *		Note: This routine uses a consistent implementation for Group0,
21562  *		Group1, and Group2 commands across all platforms. ATAPI devices
21563  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21564  *
21565  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21566  *                      structure for this target.
21567  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21568  *			  CDB_GROUP[1|2] (10 byte).
21569  *		bufaddr - buffer for page data retrieved from the target.
21570  *		buflen - size of page to be retrieved.
21571  *		page_code - page code of data to be retrieved from the target.
21572  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21573  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21574  *			to use the USCSI "direct" chain and bypass the normal
21575  *			command waitq.
21576  *
21577  * Return Code: 0   - Success
21578  *		errno return code from sd_ssc_send()
21579  *
21580  *     Context: Can sleep. Does not return until command is completed.
21581  */
21582 
21583 static int
21584 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21585 	size_t buflen,  uchar_t page_code, int path_flag)
21586 {
21587 	struct	scsi_extended_sense	sense_buf;
21588 	union scsi_cdb		cdb;
21589 	struct uscsi_cmd	ucmd_buf;
21590 	int			status;
21591 	int			headlen;
21592 	struct sd_lun		*un;
21593 
21594 	ASSERT(ssc != NULL);
21595 	un = ssc->ssc_un;
21596 	ASSERT(un != NULL);
21597 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21598 	ASSERT(bufaddr != NULL);
21599 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21600 	    (cdbsize == CDB_GROUP2));
21601 
21602 	SD_TRACE(SD_LOG_IO, un,
21603 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21604 
21605 	bzero(&cdb, sizeof (cdb));
21606 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21607 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21608 	bzero(bufaddr, buflen);
21609 
21610 	if (cdbsize == CDB_GROUP0) {
21611 		cdb.scc_cmd = SCMD_MODE_SENSE;
21612 		cdb.cdb_opaque[2] = page_code;
21613 		FORMG0COUNT(&cdb, buflen);
21614 		headlen = MODE_HEADER_LENGTH;
21615 	} else {
21616 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21617 		cdb.cdb_opaque[2] = page_code;
21618 		FORMG1COUNT(&cdb, buflen);
21619 		headlen = MODE_HEADER_LENGTH_GRP2;
21620 	}
21621 
21622 	ASSERT(headlen <= buflen);
21623 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21624 
21625 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21626 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21627 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21628 	ucmd_buf.uscsi_buflen	= buflen;
21629 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21630 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21631 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21632 	ucmd_buf.uscsi_timeout	= 60;
21633 
21634 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21635 	    UIO_SYSSPACE, path_flag);
21636 
21637 	switch (status) {
21638 	case 0:
21639 		/*
21640 		 * sr_check_wp() uses 0x3f page code and check the header of
21641 		 * mode page to determine if target device is write-protected.
21642 		 * But some USB devices return 0 bytes for 0x3f page code. For
21643 		 * this case, make sure that mode page header is returned at
21644 		 * least.
21645 		 */
21646 		if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21647 			status = EIO;
21648 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21649 			    "mode page header is not returned");
21650 		}
21651 		break;	/* Success! */
21652 	case EIO:
21653 		switch (ucmd_buf.uscsi_status) {
21654 		case STATUS_RESERVATION_CONFLICT:
21655 			status = EACCES;
21656 			break;
21657 		default:
21658 			break;
21659 		}
21660 		break;
21661 	default:
21662 		break;
21663 	}
21664 
21665 	if (status == 0) {
21666 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21667 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21668 	}
21669 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21670 
21671 	return (status);
21672 }
21673 
21674 
21675 /*
21676  *    Function: sd_send_scsi_MODE_SELECT
21677  *
21678  * Description: Utility function for issuing a scsi MODE SELECT command.
21679  *		Note: This routine uses a consistent implementation for Group0,
21680  *		Group1, and Group2 commands across all platforms. ATAPI devices
21681  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21682  *
21683  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21684  *                      structure for this target.
21685  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21686  *			  CDB_GROUP[1|2] (10 byte).
21687  *		bufaddr - buffer for page data retrieved from the target.
21688  *		buflen - size of page to be retrieved.
21689  *		save_page - boolean to determin if SP bit should be set.
21690  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21691  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21692  *			to use the USCSI "direct" chain and bypass the normal
21693  *			command waitq.
21694  *
21695  * Return Code: 0   - Success
21696  *		errno return code from sd_ssc_send()
21697  *
21698  *     Context: Can sleep. Does not return until command is completed.
21699  */
21700 
21701 static int
21702 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21703 	size_t buflen,  uchar_t save_page, int path_flag)
21704 {
21705 	struct	scsi_extended_sense	sense_buf;
21706 	union scsi_cdb		cdb;
21707 	struct uscsi_cmd	ucmd_buf;
21708 	int			status;
21709 	struct sd_lun		*un;
21710 
21711 	ASSERT(ssc != NULL);
21712 	un = ssc->ssc_un;
21713 	ASSERT(un != NULL);
21714 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21715 	ASSERT(bufaddr != NULL);
21716 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21717 	    (cdbsize == CDB_GROUP2));
21718 
21719 	SD_TRACE(SD_LOG_IO, un,
21720 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21721 
21722 	bzero(&cdb, sizeof (cdb));
21723 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21724 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21725 
21726 	/* Set the PF bit for many third party drives */
21727 	cdb.cdb_opaque[1] = 0x10;
21728 
21729 	/* Set the savepage(SP) bit if given */
21730 	if (save_page == SD_SAVE_PAGE) {
21731 		cdb.cdb_opaque[1] |= 0x01;
21732 	}
21733 
21734 	if (cdbsize == CDB_GROUP0) {
21735 		cdb.scc_cmd = SCMD_MODE_SELECT;
21736 		FORMG0COUNT(&cdb, buflen);
21737 	} else {
21738 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21739 		FORMG1COUNT(&cdb, buflen);
21740 	}
21741 
21742 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21743 
21744 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21745 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21746 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21747 	ucmd_buf.uscsi_buflen	= buflen;
21748 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21749 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21750 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21751 	ucmd_buf.uscsi_timeout	= 60;
21752 
21753 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21754 	    UIO_SYSSPACE, path_flag);
21755 
21756 	switch (status) {
21757 	case 0:
21758 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21759 		break;	/* Success! */
21760 	case EIO:
21761 		switch (ucmd_buf.uscsi_status) {
21762 		case STATUS_RESERVATION_CONFLICT:
21763 			status = EACCES;
21764 			break;
21765 		default:
21766 			break;
21767 		}
21768 		break;
21769 	default:
21770 		break;
21771 	}
21772 
21773 	if (status == 0) {
21774 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21775 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21776 	}
21777 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21778 
21779 	return (status);
21780 }
21781 
21782 
21783 /*
21784  *    Function: sd_send_scsi_RDWR
21785  *
21786  * Description: Issue a scsi READ or WRITE command with the given parameters.
21787  *
21788  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21789  *                      structure for this target.
21790  *		cmd:	 SCMD_READ or SCMD_WRITE
21791  *		bufaddr: Address of caller's buffer to receive the RDWR data
21792  *		buflen:  Length of caller's buffer receive the RDWR data.
21793  *		start_block: Block number for the start of the RDWR operation.
21794  *			 (Assumes target-native block size.)
21795  *		residp:  Pointer to variable to receive the redisual of the
21796  *			 RDWR operation (may be NULL of no residual requested).
21797  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21798  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21799  *			to use the USCSI "direct" chain and bypass the normal
21800  *			command waitq.
21801  *
21802  * Return Code: 0   - Success
21803  *		errno return code from sd_ssc_send()
21804  *
21805  *     Context: Can sleep. Does not return until command is completed.
21806  */
21807 
21808 static int
21809 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21810 	size_t buflen, daddr_t start_block, int path_flag)
21811 {
21812 	struct	scsi_extended_sense	sense_buf;
21813 	union scsi_cdb		cdb;
21814 	struct uscsi_cmd	ucmd_buf;
21815 	uint32_t		block_count;
21816 	int			status;
21817 	int			cdbsize;
21818 	uchar_t			flag;
21819 	struct sd_lun		*un;
21820 
21821 	ASSERT(ssc != NULL);
21822 	un = ssc->ssc_un;
21823 	ASSERT(un != NULL);
21824 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21825 	ASSERT(bufaddr != NULL);
21826 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21827 
21828 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21829 
21830 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21831 		return (EINVAL);
21832 	}
21833 
21834 	mutex_enter(SD_MUTEX(un));
21835 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21836 	mutex_exit(SD_MUTEX(un));
21837 
21838 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21839 
21840 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21841 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21842 	    bufaddr, buflen, start_block, block_count);
21843 
21844 	bzero(&cdb, sizeof (cdb));
21845 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21846 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21847 
21848 	/* Compute CDB size to use */
21849 	if (start_block > 0xffffffff)
21850 		cdbsize = CDB_GROUP4;
21851 	else if ((start_block & 0xFFE00000) ||
21852 	    (un->un_f_cfg_is_atapi == TRUE))
21853 		cdbsize = CDB_GROUP1;
21854 	else
21855 		cdbsize = CDB_GROUP0;
21856 
21857 	switch (cdbsize) {
21858 	case CDB_GROUP0:	/* 6-byte CDBs */
21859 		cdb.scc_cmd = cmd;
21860 		FORMG0ADDR(&cdb, start_block);
21861 		FORMG0COUNT(&cdb, block_count);
21862 		break;
21863 	case CDB_GROUP1:	/* 10-byte CDBs */
21864 		cdb.scc_cmd = cmd | SCMD_GROUP1;
21865 		FORMG1ADDR(&cdb, start_block);
21866 		FORMG1COUNT(&cdb, block_count);
21867 		break;
21868 	case CDB_GROUP4:	/* 16-byte CDBs */
21869 		cdb.scc_cmd = cmd | SCMD_GROUP4;
21870 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21871 		FORMG4COUNT(&cdb, block_count);
21872 		break;
21873 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
21874 	default:
21875 		/* All others reserved */
21876 		return (EINVAL);
21877 	}
21878 
21879 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21880 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21881 
21882 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21883 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21884 	ucmd_buf.uscsi_bufaddr	= bufaddr;
21885 	ucmd_buf.uscsi_buflen	= buflen;
21886 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21887 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21888 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
21889 	ucmd_buf.uscsi_timeout	= 60;
21890 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21891 	    UIO_SYSSPACE, path_flag);
21892 
21893 	switch (status) {
21894 	case 0:
21895 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21896 		break;	/* Success! */
21897 	case EIO:
21898 		switch (ucmd_buf.uscsi_status) {
21899 		case STATUS_RESERVATION_CONFLICT:
21900 			status = EACCES;
21901 			break;
21902 		default:
21903 			break;
21904 		}
21905 		break;
21906 	default:
21907 		break;
21908 	}
21909 
21910 	if (status == 0) {
21911 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21912 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21913 	}
21914 
21915 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21916 
21917 	return (status);
21918 }
21919 
21920 
21921 /*
21922  *    Function: sd_send_scsi_LOG_SENSE
21923  *
21924  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21925  *
21926  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21927  *                      structure for this target.
21928  *
21929  * Return Code: 0   - Success
21930  *		errno return code from sd_ssc_send()
21931  *
21932  *     Context: Can sleep. Does not return until command is completed.
21933  */
21934 
21935 static int
21936 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21937 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21938 	int path_flag)
21939 
21940 {
21941 	struct scsi_extended_sense	sense_buf;
21942 	union scsi_cdb		cdb;
21943 	struct uscsi_cmd	ucmd_buf;
21944 	int			status;
21945 	struct sd_lun		*un;
21946 
21947 	ASSERT(ssc != NULL);
21948 	un = ssc->ssc_un;
21949 	ASSERT(un != NULL);
21950 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21951 
21952 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
21953 
21954 	bzero(&cdb, sizeof (cdb));
21955 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21956 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21957 
21958 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
21959 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
21960 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
21961 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
21962 	FORMG1COUNT(&cdb, buflen);
21963 
21964 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21965 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21966 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21967 	ucmd_buf.uscsi_buflen	= buflen;
21968 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21969 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21970 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21971 	ucmd_buf.uscsi_timeout	= 60;
21972 
21973 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21974 	    UIO_SYSSPACE, path_flag);
21975 
21976 	switch (status) {
21977 	case 0:
21978 		break;
21979 	case EIO:
21980 		switch (ucmd_buf.uscsi_status) {
21981 		case STATUS_RESERVATION_CONFLICT:
21982 			status = EACCES;
21983 			break;
21984 		case STATUS_CHECK:
21985 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21986 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21987 				KEY_ILLEGAL_REQUEST) &&
21988 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
21989 				/*
21990 				 * ASC 0x24: INVALID FIELD IN CDB
21991 				 */
21992 				switch (page_code) {
21993 				case START_STOP_CYCLE_PAGE:
21994 					/*
21995 					 * The start stop cycle counter is
21996 					 * implemented as page 0x31 in earlier
21997 					 * generation disks. In new generation
21998 					 * disks the start stop cycle counter is
21999 					 * implemented as page 0xE. To properly
22000 					 * handle this case if an attempt for
22001 					 * log page 0xE is made and fails we
22002 					 * will try again using page 0x31.
22003 					 *
22004 					 * Network storage BU committed to
22005 					 * maintain the page 0x31 for this
22006 					 * purpose and will not have any other
22007 					 * page implemented with page code 0x31
22008 					 * until all disks transition to the
22009 					 * standard page.
22010 					 */
22011 					mutex_enter(SD_MUTEX(un));
22012 					un->un_start_stop_cycle_page =
22013 					    START_STOP_CYCLE_VU_PAGE;
22014 					cdb.cdb_opaque[2] =
22015 					    (char)(page_control << 6) |
22016 					    un->un_start_stop_cycle_page;
22017 					mutex_exit(SD_MUTEX(un));
22018 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22019 					status = sd_ssc_send(
22020 					    ssc, &ucmd_buf, FKIOCTL,
22021 					    UIO_SYSSPACE, path_flag);
22022 
22023 					break;
22024 				case TEMPERATURE_PAGE:
22025 					status = ENOTTY;
22026 					break;
22027 				default:
22028 					break;
22029 				}
22030 			}
22031 			break;
22032 		default:
22033 			break;
22034 		}
22035 		break;
22036 	default:
22037 		break;
22038 	}
22039 
22040 	if (status == 0) {
22041 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22042 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22043 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22044 	}
22045 
22046 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22047 
22048 	return (status);
22049 }
22050 
22051 
22052 /*
22053  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22054  *
22055  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22056  *
22057  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22058  *                      structure for this target.
22059  *		bufaddr
22060  *		buflen
22061  *		class_req
22062  *
22063  * Return Code: 0   - Success
22064  *		errno return code from sd_ssc_send()
22065  *
22066  *     Context: Can sleep. Does not return until command is completed.
22067  */
22068 
22069 static int
22070 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22071 	size_t buflen, uchar_t class_req)
22072 {
22073 	union scsi_cdb		cdb;
22074 	struct uscsi_cmd	ucmd_buf;
22075 	int			status;
22076 	struct sd_lun		*un;
22077 
22078 	ASSERT(ssc != NULL);
22079 	un = ssc->ssc_un;
22080 	ASSERT(un != NULL);
22081 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22082 	ASSERT(bufaddr != NULL);
22083 
22084 	SD_TRACE(SD_LOG_IO, un,
22085 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22086 
22087 	bzero(&cdb, sizeof (cdb));
22088 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22089 	bzero(bufaddr, buflen);
22090 
22091 	cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22092 	cdb.cdb_opaque[1] = 1; /* polled */
22093 	cdb.cdb_opaque[4] = class_req;
22094 	FORMG1COUNT(&cdb, buflen);
22095 
22096 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22097 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22098 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22099 	ucmd_buf.uscsi_buflen	= buflen;
22100 	ucmd_buf.uscsi_rqbuf	= NULL;
22101 	ucmd_buf.uscsi_rqlen	= 0;
22102 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
22103 	ucmd_buf.uscsi_timeout	= 60;
22104 
22105 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22106 	    UIO_SYSSPACE, SD_PATH_DIRECT);
22107 
22108 	/*
22109 	 * Only handle status == 0, the upper-level caller
22110 	 * will put different assessment based on the context.
22111 	 */
22112 	if (status == 0) {
22113 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22114 
22115 		if (ucmd_buf.uscsi_resid != 0) {
22116 			status = EIO;
22117 		}
22118 	}
22119 
22120 	SD_TRACE(SD_LOG_IO, un,
22121 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22122 
22123 	return (status);
22124 }
22125 
22126 
22127 static boolean_t
22128 sd_gesn_media_data_valid(uchar_t *data)
22129 {
22130 	uint16_t			len;
22131 
22132 	len = (data[1] << 8) | data[0];
22133 	return ((len >= 6) &&
22134 	    ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22135 	    ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22136 	    ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22137 }
22138 
22139 
22140 /*
22141  *    Function: sdioctl
22142  *
22143  * Description: Driver's ioctl(9e) entry point function.
22144  *
22145  *   Arguments: dev     - device number
22146  *		cmd     - ioctl operation to be performed
22147  *		arg     - user argument, contains data to be set or reference
22148  *			  parameter for get
22149  *		flag    - bit flag, indicating open settings, 32/64 bit type
22150  *		cred_p  - user credential pointer
22151  *		rval_p  - calling process return value (OPT)
22152  *
22153  * Return Code: EINVAL
22154  *		ENOTTY
22155  *		ENXIO
22156  *		EIO
22157  *		EFAULT
22158  *		ENOTSUP
22159  *		EPERM
22160  *
22161  *     Context: Called from the device switch at normal priority.
22162  */
22163 
22164 static int
22165 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22166 {
22167 	struct sd_lun	*un = NULL;
22168 	int		err = 0;
22169 	int		i = 0;
22170 	cred_t		*cr;
22171 	int		tmprval = EINVAL;
22172 	boolean_t	is_valid;
22173 	sd_ssc_t	*ssc;
22174 
22175 	/*
22176 	 * All device accesses go thru sdstrategy where we check on suspend
22177 	 * status
22178 	 */
22179 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22180 		return (ENXIO);
22181 	}
22182 
22183 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22184 
22185 	/* Initialize sd_ssc_t for internal uscsi commands */
22186 	ssc = sd_ssc_init(un);
22187 
22188 	is_valid = SD_IS_VALID_LABEL(un);
22189 
22190 	/*
22191 	 * Moved this wait from sd_uscsi_strategy to here for
22192 	 * reasons of deadlock prevention. Internal driver commands,
22193 	 * specifically those to change a devices power level, result
22194 	 * in a call to sd_uscsi_strategy.
22195 	 */
22196 	mutex_enter(SD_MUTEX(un));
22197 	while ((un->un_state == SD_STATE_SUSPENDED) ||
22198 	    (un->un_state == SD_STATE_PM_CHANGING)) {
22199 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22200 	}
22201 	/*
22202 	 * Twiddling the counter here protects commands from now
22203 	 * through to the top of sd_uscsi_strategy. Without the
22204 	 * counter inc. a power down, for example, could get in
22205 	 * after the above check for state is made and before
22206 	 * execution gets to the top of sd_uscsi_strategy.
22207 	 * That would cause problems.
22208 	 */
22209 	un->un_ncmds_in_driver++;
22210 
22211 	if (!is_valid &&
22212 	    (flag & (FNDELAY | FNONBLOCK))) {
22213 		switch (cmd) {
22214 		case DKIOCGGEOM:	/* SD_PATH_DIRECT */
22215 		case DKIOCGVTOC:
22216 		case DKIOCGEXTVTOC:
22217 		case DKIOCGAPART:
22218 		case DKIOCPARTINFO:
22219 		case DKIOCEXTPARTINFO:
22220 		case DKIOCSGEOM:
22221 		case DKIOCSAPART:
22222 		case DKIOCGETEFI:
22223 		case DKIOCPARTITION:
22224 		case DKIOCSVTOC:
22225 		case DKIOCSEXTVTOC:
22226 		case DKIOCSETEFI:
22227 		case DKIOCGMBOOT:
22228 		case DKIOCSMBOOT:
22229 		case DKIOCG_PHYGEOM:
22230 		case DKIOCG_VIRTGEOM:
22231 #if defined(__i386) || defined(__amd64)
22232 		case DKIOCSETEXTPART:
22233 #endif
22234 			/* let cmlb handle it */
22235 			goto skip_ready_valid;
22236 
22237 		case CDROMPAUSE:
22238 		case CDROMRESUME:
22239 		case CDROMPLAYMSF:
22240 		case CDROMPLAYTRKIND:
22241 		case CDROMREADTOCHDR:
22242 		case CDROMREADTOCENTRY:
22243 		case CDROMSTOP:
22244 		case CDROMSTART:
22245 		case CDROMVOLCTRL:
22246 		case CDROMSUBCHNL:
22247 		case CDROMREADMODE2:
22248 		case CDROMREADMODE1:
22249 		case CDROMREADOFFSET:
22250 		case CDROMSBLKMODE:
22251 		case CDROMGBLKMODE:
22252 		case CDROMGDRVSPEED:
22253 		case CDROMSDRVSPEED:
22254 		case CDROMCDDA:
22255 		case CDROMCDXA:
22256 		case CDROMSUBCODE:
22257 			if (!ISCD(un)) {
22258 				un->un_ncmds_in_driver--;
22259 				ASSERT(un->un_ncmds_in_driver >= 0);
22260 				mutex_exit(SD_MUTEX(un));
22261 				err = ENOTTY;
22262 				goto done_without_assess;
22263 			}
22264 			break;
22265 		case FDEJECT:
22266 		case DKIOCEJECT:
22267 		case CDROMEJECT:
22268 			if (!un->un_f_eject_media_supported) {
22269 				un->un_ncmds_in_driver--;
22270 				ASSERT(un->un_ncmds_in_driver >= 0);
22271 				mutex_exit(SD_MUTEX(un));
22272 				err = ENOTTY;
22273 				goto done_without_assess;
22274 			}
22275 			break;
22276 		case DKIOCFLUSHWRITECACHE:
22277 			mutex_exit(SD_MUTEX(un));
22278 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22279 			if (err != 0) {
22280 				mutex_enter(SD_MUTEX(un));
22281 				un->un_ncmds_in_driver--;
22282 				ASSERT(un->un_ncmds_in_driver >= 0);
22283 				mutex_exit(SD_MUTEX(un));
22284 				err = EIO;
22285 				goto done_quick_assess;
22286 			}
22287 			mutex_enter(SD_MUTEX(un));
22288 			/* FALLTHROUGH */
22289 		case DKIOCREMOVABLE:
22290 		case DKIOCHOTPLUGGABLE:
22291 		case DKIOCINFO:
22292 		case DKIOCGMEDIAINFO:
22293 		case DKIOCGMEDIAINFOEXT:
22294 		case MHIOCENFAILFAST:
22295 		case MHIOCSTATUS:
22296 		case MHIOCTKOWN:
22297 		case MHIOCRELEASE:
22298 		case MHIOCGRP_INKEYS:
22299 		case MHIOCGRP_INRESV:
22300 		case MHIOCGRP_REGISTER:
22301 		case MHIOCGRP_CLEAR:
22302 		case MHIOCGRP_RESERVE:
22303 		case MHIOCGRP_PREEMPTANDABORT:
22304 		case MHIOCGRP_REGISTERANDIGNOREKEY:
22305 		case CDROMCLOSETRAY:
22306 		case USCSICMD:
22307 			goto skip_ready_valid;
22308 		default:
22309 			break;
22310 		}
22311 
22312 		mutex_exit(SD_MUTEX(un));
22313 		err = sd_ready_and_valid(ssc, SDPART(dev));
22314 		mutex_enter(SD_MUTEX(un));
22315 
22316 		if (err != SD_READY_VALID) {
22317 			switch (cmd) {
22318 			case DKIOCSTATE:
22319 			case CDROMGDRVSPEED:
22320 			case CDROMSDRVSPEED:
22321 			case FDEJECT:	/* for eject command */
22322 			case DKIOCEJECT:
22323 			case CDROMEJECT:
22324 			case DKIOCREMOVABLE:
22325 			case DKIOCHOTPLUGGABLE:
22326 				break;
22327 			default:
22328 				if (un->un_f_has_removable_media) {
22329 					err = ENXIO;
22330 				} else {
22331 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
22332 					if (err == SD_RESERVED_BY_OTHERS) {
22333 						err = EACCES;
22334 					} else {
22335 						err = EIO;
22336 					}
22337 				}
22338 				un->un_ncmds_in_driver--;
22339 				ASSERT(un->un_ncmds_in_driver >= 0);
22340 				mutex_exit(SD_MUTEX(un));
22341 
22342 				goto done_without_assess;
22343 			}
22344 		}
22345 	}
22346 
22347 skip_ready_valid:
22348 	mutex_exit(SD_MUTEX(un));
22349 
22350 	switch (cmd) {
22351 	case DKIOCINFO:
22352 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22353 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22354 		break;
22355 
22356 	case DKIOCGMEDIAINFO:
22357 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22358 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
22359 		break;
22360 
22361 	case DKIOCGMEDIAINFOEXT:
22362 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22363 		err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22364 		break;
22365 
22366 	case DKIOCGGEOM:
22367 	case DKIOCGVTOC:
22368 	case DKIOCGEXTVTOC:
22369 	case DKIOCGAPART:
22370 	case DKIOCPARTINFO:
22371 	case DKIOCEXTPARTINFO:
22372 	case DKIOCSGEOM:
22373 	case DKIOCSAPART:
22374 	case DKIOCGETEFI:
22375 	case DKIOCPARTITION:
22376 	case DKIOCSVTOC:
22377 	case DKIOCSEXTVTOC:
22378 	case DKIOCSETEFI:
22379 	case DKIOCGMBOOT:
22380 	case DKIOCSMBOOT:
22381 	case DKIOCG_PHYGEOM:
22382 	case DKIOCG_VIRTGEOM:
22383 #if defined(__i386) || defined(__amd64)
22384 	case DKIOCSETEXTPART:
22385 #endif
22386 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22387 
22388 		/* TUR should spin up */
22389 
22390 		if (un->un_f_has_removable_media)
22391 			err = sd_send_scsi_TEST_UNIT_READY(ssc,
22392 			    SD_CHECK_FOR_MEDIA);
22393 
22394 		else
22395 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22396 
22397 		if (err != 0)
22398 			goto done_with_assess;
22399 
22400 		err = cmlb_ioctl(un->un_cmlbhandle, dev,
22401 		    cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22402 
22403 		if ((err == 0) &&
22404 		    ((cmd == DKIOCSETEFI) ||
22405 		    (un->un_f_pkstats_enabled) &&
22406 		    (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22407 		    cmd == DKIOCSEXTVTOC))) {
22408 
22409 			tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22410 			    (void *)SD_PATH_DIRECT);
22411 			if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22412 				sd_set_pstats(un);
22413 				SD_TRACE(SD_LOG_IO_PARTITION, un,
22414 				    "sd_ioctl: un:0x%p pstats created and "
22415 				    "set\n", un);
22416 			}
22417 		}
22418 
22419 		if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22420 		    ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22421 
22422 			mutex_enter(SD_MUTEX(un));
22423 			if (un->un_f_devid_supported &&
22424 			    (un->un_f_opt_fab_devid == TRUE)) {
22425 				if (un->un_devid == NULL) {
22426 					sd_register_devid(ssc, SD_DEVINFO(un),
22427 					    SD_TARGET_IS_UNRESERVED);
22428 				} else {
22429 					/*
22430 					 * The device id for this disk
22431 					 * has been fabricated. The
22432 					 * device id must be preserved
22433 					 * by writing it back out to
22434 					 * disk.
22435 					 */
22436 					if (sd_write_deviceid(ssc) != 0) {
22437 						ddi_devid_free(un->un_devid);
22438 						un->un_devid = NULL;
22439 					}
22440 				}
22441 			}
22442 			mutex_exit(SD_MUTEX(un));
22443 		}
22444 
22445 		break;
22446 
22447 	case DKIOCLOCK:
22448 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22449 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22450 		    SD_PATH_STANDARD);
22451 		goto done_with_assess;
22452 
22453 	case DKIOCUNLOCK:
22454 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22455 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22456 		    SD_PATH_STANDARD);
22457 		goto done_with_assess;
22458 
22459 	case DKIOCSTATE: {
22460 		enum dkio_state		state;
22461 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22462 
22463 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22464 			err = EFAULT;
22465 		} else {
22466 			err = sd_check_media(dev, state);
22467 			if (err == 0) {
22468 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
22469 				    sizeof (int), flag) != 0)
22470 					err = EFAULT;
22471 			}
22472 		}
22473 		break;
22474 	}
22475 
22476 	case DKIOCREMOVABLE:
22477 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22478 		i = un->un_f_has_removable_media ? 1 : 0;
22479 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22480 			err = EFAULT;
22481 		} else {
22482 			err = 0;
22483 		}
22484 		break;
22485 
22486 	case DKIOCHOTPLUGGABLE:
22487 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22488 		i = un->un_f_is_hotpluggable ? 1 : 0;
22489 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22490 			err = EFAULT;
22491 		} else {
22492 			err = 0;
22493 		}
22494 		break;
22495 
22496 	case DKIOCREADONLY:
22497 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22498 		i = 0;
22499 		if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22500 		    (sr_check_wp(dev) != 0)) {
22501 			i = 1;
22502 		}
22503 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22504 			err = EFAULT;
22505 		} else {
22506 			err = 0;
22507 		}
22508 		break;
22509 
22510 	case DKIOCGTEMPERATURE:
22511 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22512 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22513 		break;
22514 
22515 	case MHIOCENFAILFAST:
22516 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22517 		if ((err = drv_priv(cred_p)) == 0) {
22518 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22519 		}
22520 		break;
22521 
22522 	case MHIOCTKOWN:
22523 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22524 		if ((err = drv_priv(cred_p)) == 0) {
22525 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22526 		}
22527 		break;
22528 
22529 	case MHIOCRELEASE:
22530 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22531 		if ((err = drv_priv(cred_p)) == 0) {
22532 			err = sd_mhdioc_release(dev);
22533 		}
22534 		break;
22535 
22536 	case MHIOCSTATUS:
22537 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22538 		if ((err = drv_priv(cred_p)) == 0) {
22539 			switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22540 			case 0:
22541 				err = 0;
22542 				break;
22543 			case EACCES:
22544 				*rval_p = 1;
22545 				err = 0;
22546 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22547 				break;
22548 			default:
22549 				err = EIO;
22550 				goto done_with_assess;
22551 			}
22552 		}
22553 		break;
22554 
22555 	case MHIOCQRESERVE:
22556 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22557 		if ((err = drv_priv(cred_p)) == 0) {
22558 			err = sd_reserve_release(dev, SD_RESERVE);
22559 		}
22560 		break;
22561 
22562 	case MHIOCREREGISTERDEVID:
22563 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22564 		if (drv_priv(cred_p) == EPERM) {
22565 			err = EPERM;
22566 		} else if (!un->un_f_devid_supported) {
22567 			err = ENOTTY;
22568 		} else {
22569 			err = sd_mhdioc_register_devid(dev);
22570 		}
22571 		break;
22572 
22573 	case MHIOCGRP_INKEYS:
22574 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22575 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22576 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22577 				err = ENOTSUP;
22578 			} else {
22579 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22580 				    flag);
22581 			}
22582 		}
22583 		break;
22584 
22585 	case MHIOCGRP_INRESV:
22586 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22587 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22588 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22589 				err = ENOTSUP;
22590 			} else {
22591 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22592 			}
22593 		}
22594 		break;
22595 
22596 	case MHIOCGRP_REGISTER:
22597 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22598 		if ((err = drv_priv(cred_p)) != EPERM) {
22599 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22600 				err = ENOTSUP;
22601 			} else if (arg != NULL) {
22602 				mhioc_register_t reg;
22603 				if (ddi_copyin((void *)arg, &reg,
22604 				    sizeof (mhioc_register_t), flag) != 0) {
22605 					err = EFAULT;
22606 				} else {
22607 					err =
22608 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22609 					    ssc, SD_SCSI3_REGISTER,
22610 					    (uchar_t *)&reg);
22611 					if (err != 0)
22612 						goto done_with_assess;
22613 				}
22614 			}
22615 		}
22616 		break;
22617 
22618 	case MHIOCGRP_CLEAR:
22619 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22620 		if ((err = drv_priv(cred_p)) != EPERM) {
22621 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22622 				err = ENOTSUP;
22623 			} else if (arg != NULL) {
22624 				mhioc_register_t reg;
22625 				if (ddi_copyin((void *)arg, &reg,
22626 				    sizeof (mhioc_register_t), flag) != 0) {
22627 					err = EFAULT;
22628 				} else {
22629 					err =
22630 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22631 					    ssc, SD_SCSI3_CLEAR,
22632 					    (uchar_t *)&reg);
22633 					if (err != 0)
22634 						goto done_with_assess;
22635 				}
22636 			}
22637 		}
22638 		break;
22639 
22640 	case MHIOCGRP_RESERVE:
22641 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22642 		if ((err = drv_priv(cred_p)) != EPERM) {
22643 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22644 				err = ENOTSUP;
22645 			} else if (arg != NULL) {
22646 				mhioc_resv_desc_t resv_desc;
22647 				if (ddi_copyin((void *)arg, &resv_desc,
22648 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
22649 					err = EFAULT;
22650 				} else {
22651 					err =
22652 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22653 					    ssc, SD_SCSI3_RESERVE,
22654 					    (uchar_t *)&resv_desc);
22655 					if (err != 0)
22656 						goto done_with_assess;
22657 				}
22658 			}
22659 		}
22660 		break;
22661 
22662 	case MHIOCGRP_PREEMPTANDABORT:
22663 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22664 		if ((err = drv_priv(cred_p)) != EPERM) {
22665 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22666 				err = ENOTSUP;
22667 			} else if (arg != NULL) {
22668 				mhioc_preemptandabort_t preempt_abort;
22669 				if (ddi_copyin((void *)arg, &preempt_abort,
22670 				    sizeof (mhioc_preemptandabort_t),
22671 				    flag) != 0) {
22672 					err = EFAULT;
22673 				} else {
22674 					err =
22675 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22676 					    ssc, SD_SCSI3_PREEMPTANDABORT,
22677 					    (uchar_t *)&preempt_abort);
22678 					if (err != 0)
22679 						goto done_with_assess;
22680 				}
22681 			}
22682 		}
22683 		break;
22684 
22685 	case MHIOCGRP_REGISTERANDIGNOREKEY:
22686 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22687 		if ((err = drv_priv(cred_p)) != EPERM) {
22688 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22689 				err = ENOTSUP;
22690 			} else if (arg != NULL) {
22691 				mhioc_registerandignorekey_t r_and_i;
22692 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
22693 				    sizeof (mhioc_registerandignorekey_t),
22694 				    flag) != 0) {
22695 					err = EFAULT;
22696 				} else {
22697 					err =
22698 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22699 					    ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22700 					    (uchar_t *)&r_and_i);
22701 					if (err != 0)
22702 						goto done_with_assess;
22703 				}
22704 			}
22705 		}
22706 		break;
22707 
22708 	case USCSICMD:
22709 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22710 		cr = ddi_get_cred();
22711 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22712 			err = EPERM;
22713 		} else {
22714 			enum uio_seg	uioseg;
22715 
22716 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22717 			    UIO_USERSPACE;
22718 			if (un->un_f_format_in_progress == TRUE) {
22719 				err = EAGAIN;
22720 				break;
22721 			}
22722 
22723 			err = sd_ssc_send(ssc,
22724 			    (struct uscsi_cmd *)arg,
22725 			    flag, uioseg, SD_PATH_STANDARD);
22726 			if (err != 0)
22727 				goto done_with_assess;
22728 			else
22729 				sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22730 		}
22731 		break;
22732 
22733 	case CDROMPAUSE:
22734 	case CDROMRESUME:
22735 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22736 		if (!ISCD(un)) {
22737 			err = ENOTTY;
22738 		} else {
22739 			err = sr_pause_resume(dev, cmd);
22740 		}
22741 		break;
22742 
22743 	case CDROMPLAYMSF:
22744 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22745 		if (!ISCD(un)) {
22746 			err = ENOTTY;
22747 		} else {
22748 			err = sr_play_msf(dev, (caddr_t)arg, flag);
22749 		}
22750 		break;
22751 
22752 	case CDROMPLAYTRKIND:
22753 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22754 #if defined(__i386) || defined(__amd64)
22755 		/*
22756 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22757 		 */
22758 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22759 #else
22760 		if (!ISCD(un)) {
22761 #endif
22762 			err = ENOTTY;
22763 		} else {
22764 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
22765 		}
22766 		break;
22767 
22768 	case CDROMREADTOCHDR:
22769 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22770 		if (!ISCD(un)) {
22771 			err = ENOTTY;
22772 		} else {
22773 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22774 		}
22775 		break;
22776 
22777 	case CDROMREADTOCENTRY:
22778 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22779 		if (!ISCD(un)) {
22780 			err = ENOTTY;
22781 		} else {
22782 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22783 		}
22784 		break;
22785 
22786 	case CDROMSTOP:
22787 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22788 		if (!ISCD(un)) {
22789 			err = ENOTTY;
22790 		} else {
22791 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22792 			    SD_TARGET_STOP, SD_PATH_STANDARD);
22793 			goto done_with_assess;
22794 		}
22795 		break;
22796 
22797 	case CDROMSTART:
22798 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22799 		if (!ISCD(un)) {
22800 			err = ENOTTY;
22801 		} else {
22802 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22803 			    SD_TARGET_START, SD_PATH_STANDARD);
22804 			goto done_with_assess;
22805 		}
22806 		break;
22807 
22808 	case CDROMCLOSETRAY:
22809 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22810 		if (!ISCD(un)) {
22811 			err = ENOTTY;
22812 		} else {
22813 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22814 			    SD_TARGET_CLOSE, SD_PATH_STANDARD);
22815 			goto done_with_assess;
22816 		}
22817 		break;
22818 
22819 	case FDEJECT:	/* for eject command */
22820 	case DKIOCEJECT:
22821 	case CDROMEJECT:
22822 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22823 		if (!un->un_f_eject_media_supported) {
22824 			err = ENOTTY;
22825 		} else {
22826 			err = sr_eject(dev);
22827 		}
22828 		break;
22829 
22830 	case CDROMVOLCTRL:
22831 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22832 		if (!ISCD(un)) {
22833 			err = ENOTTY;
22834 		} else {
22835 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22836 		}
22837 		break;
22838 
22839 	case CDROMSUBCHNL:
22840 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22841 		if (!ISCD(un)) {
22842 			err = ENOTTY;
22843 		} else {
22844 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22845 		}
22846 		break;
22847 
22848 	case CDROMREADMODE2:
22849 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22850 		if (!ISCD(un)) {
22851 			err = ENOTTY;
22852 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22853 			/*
22854 			 * If the drive supports READ CD, use that instead of
22855 			 * switching the LBA size via a MODE SELECT
22856 			 * Block Descriptor
22857 			 */
22858 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22859 		} else {
22860 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
22861 		}
22862 		break;
22863 
22864 	case CDROMREADMODE1:
22865 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22866 		if (!ISCD(un)) {
22867 			err = ENOTTY;
22868 		} else {
22869 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
22870 		}
22871 		break;
22872 
22873 	case CDROMREADOFFSET:
22874 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22875 		if (!ISCD(un)) {
22876 			err = ENOTTY;
22877 		} else {
22878 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22879 			    flag);
22880 		}
22881 		break;
22882 
22883 	case CDROMSBLKMODE:
22884 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22885 		/*
22886 		 * There is no means of changing block size in case of atapi
22887 		 * drives, thus return ENOTTY if drive type is atapi
22888 		 */
22889 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22890 			err = ENOTTY;
22891 		} else if (un->un_f_mmc_cap == TRUE) {
22892 
22893 			/*
22894 			 * MMC Devices do not support changing the
22895 			 * logical block size
22896 			 *
22897 			 * Note: EINVAL is being returned instead of ENOTTY to
22898 			 * maintain consistancy with the original mmc
22899 			 * driver update.
22900 			 */
22901 			err = EINVAL;
22902 		} else {
22903 			mutex_enter(SD_MUTEX(un));
22904 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22905 			    (un->un_ncmds_in_transport > 0)) {
22906 				mutex_exit(SD_MUTEX(un));
22907 				err = EINVAL;
22908 			} else {
22909 				mutex_exit(SD_MUTEX(un));
22910 				err = sr_change_blkmode(dev, cmd, arg, flag);
22911 			}
22912 		}
22913 		break;
22914 
22915 	case CDROMGBLKMODE:
22916 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22917 		if (!ISCD(un)) {
22918 			err = ENOTTY;
22919 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
22920 		    (un->un_f_blockcount_is_valid != FALSE)) {
22921 			/*
22922 			 * Drive is an ATAPI drive so return target block
22923 			 * size for ATAPI drives since we cannot change the
22924 			 * blocksize on ATAPI drives. Used primarily to detect
22925 			 * if an ATAPI cdrom is present.
22926 			 */
22927 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22928 			    sizeof (int), flag) != 0) {
22929 				err = EFAULT;
22930 			} else {
22931 				err = 0;
22932 			}
22933 
22934 		} else {
22935 			/*
22936 			 * Drive supports changing block sizes via a Mode
22937 			 * Select.
22938 			 */
22939 			err = sr_change_blkmode(dev, cmd, arg, flag);
22940 		}
22941 		break;
22942 
22943 	case CDROMGDRVSPEED:
22944 	case CDROMSDRVSPEED:
22945 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
22946 		if (!ISCD(un)) {
22947 			err = ENOTTY;
22948 		} else if (un->un_f_mmc_cap == TRUE) {
22949 			/*
22950 			 * Note: In the future the driver implementation
22951 			 * for getting and
22952 			 * setting cd speed should entail:
22953 			 * 1) If non-mmc try the Toshiba mode page
22954 			 *    (sr_change_speed)
22955 			 * 2) If mmc but no support for Real Time Streaming try
22956 			 *    the SET CD SPEED (0xBB) command
22957 			 *   (sr_atapi_change_speed)
22958 			 * 3) If mmc and support for Real Time Streaming
22959 			 *    try the GET PERFORMANCE and SET STREAMING
22960 			 *    commands (not yet implemented, 4380808)
22961 			 */
22962 			/*
22963 			 * As per recent MMC spec, CD-ROM speed is variable
22964 			 * and changes with LBA. Since there is no such
22965 			 * things as drive speed now, fail this ioctl.
22966 			 *
22967 			 * Note: EINVAL is returned for consistancy of original
22968 			 * implementation which included support for getting
22969 			 * the drive speed of mmc devices but not setting
22970 			 * the drive speed. Thus EINVAL would be returned
22971 			 * if a set request was made for an mmc device.
22972 			 * We no longer support get or set speed for
22973 			 * mmc but need to remain consistent with regard
22974 			 * to the error code returned.
22975 			 */
22976 			err = EINVAL;
22977 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22978 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
22979 		} else {
22980 			err = sr_change_speed(dev, cmd, arg, flag);
22981 		}
22982 		break;
22983 
22984 	case CDROMCDDA:
22985 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
22986 		if (!ISCD(un)) {
22987 			err = ENOTTY;
22988 		} else {
22989 			err = sr_read_cdda(dev, (void *)arg, flag);
22990 		}
22991 		break;
22992 
22993 	case CDROMCDXA:
22994 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
22995 		if (!ISCD(un)) {
22996 			err = ENOTTY;
22997 		} else {
22998 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
22999 		}
23000 		break;
23001 
23002 	case CDROMSUBCODE:
23003 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23004 		if (!ISCD(un)) {
23005 			err = ENOTTY;
23006 		} else {
23007 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23008 		}
23009 		break;
23010 
23011 
23012 #ifdef SDDEBUG
23013 /* RESET/ABORTS testing ioctls */
23014 	case DKIOCRESET: {
23015 		int	reset_level;
23016 
23017 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23018 			err = EFAULT;
23019 		} else {
23020 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23021 			    "reset_level = 0x%lx\n", reset_level);
23022 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23023 				err = 0;
23024 			} else {
23025 				err = EIO;
23026 			}
23027 		}
23028 		break;
23029 	}
23030 
23031 	case DKIOCABORT:
23032 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23033 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
23034 			err = 0;
23035 		} else {
23036 			err = EIO;
23037 		}
23038 		break;
23039 #endif
23040 
23041 #ifdef SD_FAULT_INJECTION
23042 /* SDIOC FaultInjection testing ioctls */
23043 	case SDIOCSTART:
23044 	case SDIOCSTOP:
23045 	case SDIOCINSERTPKT:
23046 	case SDIOCINSERTXB:
23047 	case SDIOCINSERTUN:
23048 	case SDIOCINSERTARQ:
23049 	case SDIOCPUSH:
23050 	case SDIOCRETRIEVE:
23051 	case SDIOCRUN:
23052 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23053 		    "SDIOC detected cmd:0x%X:\n", cmd);
23054 		/* call error generator */
23055 		sd_faultinjection_ioctl(cmd, arg, un);
23056 		err = 0;
23057 		break;
23058 
23059 #endif /* SD_FAULT_INJECTION */
23060 
23061 	case DKIOCFLUSHWRITECACHE:
23062 		{
23063 			struct dk_callback *dkc = (struct dk_callback *)arg;
23064 
23065 			mutex_enter(SD_MUTEX(un));
23066 			if (!un->un_f_sync_cache_supported ||
23067 			    !un->un_f_write_cache_enabled) {
23068 				err = un->un_f_sync_cache_supported ?
23069 				    0 : ENOTSUP;
23070 				mutex_exit(SD_MUTEX(un));
23071 				if ((flag & FKIOCTL) && dkc != NULL &&
23072 				    dkc->dkc_callback != NULL) {
23073 					(*dkc->dkc_callback)(dkc->dkc_cookie,
23074 					    err);
23075 					/*
23076 					 * Did callback and reported error.
23077 					 * Since we did a callback, ioctl
23078 					 * should return 0.
23079 					 */
23080 					err = 0;
23081 				}
23082 				break;
23083 			}
23084 			mutex_exit(SD_MUTEX(un));
23085 
23086 			if ((flag & FKIOCTL) && dkc != NULL &&
23087 			    dkc->dkc_callback != NULL) {
23088 				/* async SYNC CACHE request */
23089 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23090 			} else {
23091 				/* synchronous SYNC CACHE request */
23092 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23093 			}
23094 		}
23095 		break;
23096 
23097 	case DKIOCGETWCE: {
23098 
23099 		int wce;
23100 
23101 		if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23102 			break;
23103 		}
23104 
23105 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23106 			err = EFAULT;
23107 		}
23108 		break;
23109 	}
23110 
23111 	case DKIOCSETWCE: {
23112 
23113 		int wce, sync_supported;
23114 		int cur_wce = 0;
23115 
23116 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23117 			err = EFAULT;
23118 			break;
23119 		}
23120 
23121 		/*
23122 		 * Synchronize multiple threads trying to enable
23123 		 * or disable the cache via the un_f_wcc_cv
23124 		 * condition variable.
23125 		 */
23126 		mutex_enter(SD_MUTEX(un));
23127 
23128 		/*
23129 		 * Don't allow the cache to be enabled if the
23130 		 * config file has it disabled.
23131 		 */
23132 		if (un->un_f_opt_disable_cache && wce) {
23133 			mutex_exit(SD_MUTEX(un));
23134 			err = EINVAL;
23135 			break;
23136 		}
23137 
23138 		/*
23139 		 * Wait for write cache change in progress
23140 		 * bit to be clear before proceeding.
23141 		 */
23142 		while (un->un_f_wcc_inprog)
23143 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23144 
23145 		un->un_f_wcc_inprog = 1;
23146 
23147 		mutex_exit(SD_MUTEX(un));
23148 
23149 		/*
23150 		 * Get the current write cache state
23151 		 */
23152 		if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23153 			mutex_enter(SD_MUTEX(un));
23154 			un->un_f_wcc_inprog = 0;
23155 			cv_broadcast(&un->un_wcc_cv);
23156 			mutex_exit(SD_MUTEX(un));
23157 			break;
23158 		}
23159 
23160 		mutex_enter(SD_MUTEX(un));
23161 		un->un_f_write_cache_enabled = (cur_wce != 0);
23162 
23163 		if (un->un_f_write_cache_enabled && wce == 0) {
23164 			/*
23165 			 * Disable the write cache.  Don't clear
23166 			 * un_f_write_cache_enabled until after
23167 			 * the mode select and flush are complete.
23168 			 */
23169 			sync_supported = un->un_f_sync_cache_supported;
23170 
23171 			/*
23172 			 * If cache flush is suppressed, we assume that the
23173 			 * controller firmware will take care of managing the
23174 			 * write cache for us: no need to explicitly
23175 			 * disable it.
23176 			 */
23177 			if (!un->un_f_suppress_cache_flush) {
23178 				mutex_exit(SD_MUTEX(un));
23179 				if ((err = sd_cache_control(ssc,
23180 				    SD_CACHE_NOCHANGE,
23181 				    SD_CACHE_DISABLE)) == 0 &&
23182 				    sync_supported) {
23183 					err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23184 					    NULL);
23185 				}
23186 			} else {
23187 				mutex_exit(SD_MUTEX(un));
23188 			}
23189 
23190 			mutex_enter(SD_MUTEX(un));
23191 			if (err == 0) {
23192 				un->un_f_write_cache_enabled = 0;
23193 			}
23194 
23195 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
23196 			/*
23197 			 * Set un_f_write_cache_enabled first, so there is
23198 			 * no window where the cache is enabled, but the
23199 			 * bit says it isn't.
23200 			 */
23201 			un->un_f_write_cache_enabled = 1;
23202 
23203 			/*
23204 			 * If cache flush is suppressed, we assume that the
23205 			 * controller firmware will take care of managing the
23206 			 * write cache for us: no need to explicitly
23207 			 * enable it.
23208 			 */
23209 			if (!un->un_f_suppress_cache_flush) {
23210 				mutex_exit(SD_MUTEX(un));
23211 				err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23212 				    SD_CACHE_ENABLE);
23213 			} else {
23214 				mutex_exit(SD_MUTEX(un));
23215 			}
23216 
23217 			mutex_enter(SD_MUTEX(un));
23218 
23219 			if (err) {
23220 				un->un_f_write_cache_enabled = 0;
23221 			}
23222 		}
23223 
23224 		un->un_f_wcc_inprog = 0;
23225 		cv_broadcast(&un->un_wcc_cv);
23226 		mutex_exit(SD_MUTEX(un));
23227 		break;
23228 	}
23229 
23230 	default:
23231 		err = ENOTTY;
23232 		break;
23233 	}
23234 	mutex_enter(SD_MUTEX(un));
23235 	un->un_ncmds_in_driver--;
23236 	ASSERT(un->un_ncmds_in_driver >= 0);
23237 	mutex_exit(SD_MUTEX(un));
23238 
23239 
23240 done_without_assess:
23241 	sd_ssc_fini(ssc);
23242 
23243 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23244 	return (err);
23245 
23246 done_with_assess:
23247 	mutex_enter(SD_MUTEX(un));
23248 	un->un_ncmds_in_driver--;
23249 	ASSERT(un->un_ncmds_in_driver >= 0);
23250 	mutex_exit(SD_MUTEX(un));
23251 
23252 done_quick_assess:
23253 	if (err != 0)
23254 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23255 	/* Uninitialize sd_ssc_t pointer */
23256 	sd_ssc_fini(ssc);
23257 
23258 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23259 	return (err);
23260 }
23261 
23262 
23263 /*
23264  *    Function: sd_dkio_ctrl_info
23265  *
23266  * Description: This routine is the driver entry point for handling controller
23267  *		information ioctl requests (DKIOCINFO).
23268  *
23269  *   Arguments: dev  - the device number
23270  *		arg  - pointer to user provided dk_cinfo structure
23271  *		       specifying the controller type and attributes.
23272  *		flag - this argument is a pass through to ddi_copyxxx()
23273  *		       directly from the mode argument of ioctl().
23274  *
23275  * Return Code: 0
23276  *		EFAULT
23277  *		ENXIO
23278  */
23279 
23280 static int
23281 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23282 {
23283 	struct sd_lun	*un = NULL;
23284 	struct dk_cinfo	*info;
23285 	dev_info_t	*pdip;
23286 	int		lun, tgt;
23287 
23288 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23289 		return (ENXIO);
23290 	}
23291 
23292 	info = (struct dk_cinfo *)
23293 	    kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23294 
23295 	switch (un->un_ctype) {
23296 	case CTYPE_CDROM:
23297 		info->dki_ctype = DKC_CDROM;
23298 		break;
23299 	default:
23300 		info->dki_ctype = DKC_SCSI_CCS;
23301 		break;
23302 	}
23303 	pdip = ddi_get_parent(SD_DEVINFO(un));
23304 	info->dki_cnum = ddi_get_instance(pdip);
23305 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23306 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
23307 	} else {
23308 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
23309 		    DK_DEVLEN - 1);
23310 	}
23311 
23312 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23313 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23314 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23315 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23316 
23317 	/* Unit Information */
23318 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23319 	info->dki_slave = ((tgt << 3) | lun);
23320 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23321 	    DK_DEVLEN - 1);
23322 	info->dki_flags = DKI_FMTVOL;
23323 	info->dki_partition = SDPART(dev);
23324 
23325 	/* Max Transfer size of this device in blocks */
23326 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23327 	info->dki_addr = 0;
23328 	info->dki_space = 0;
23329 	info->dki_prio = 0;
23330 	info->dki_vec = 0;
23331 
23332 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23333 		kmem_free(info, sizeof (struct dk_cinfo));
23334 		return (EFAULT);
23335 	} else {
23336 		kmem_free(info, sizeof (struct dk_cinfo));
23337 		return (0);
23338 	}
23339 }
23340 
23341 /*
23342  *    Function: sd_get_media_info_com
23343  *
23344  * Description: This routine returns the information required to populate
23345  *		the fields for the dk_minfo/dk_minfo_ext structures.
23346  *
23347  *   Arguments: dev		- the device number
23348  *		dki_media_type	- media_type
23349  *		dki_lbsize	- logical block size
23350  *		dki_capacity	- capacity in blocks
23351  *		dki_pbsize	- physical block size (if requested)
23352  *
23353  * Return Code: 0
23354  *		EACCESS
23355  *		EFAULT
23356  *		ENXIO
23357  *		EIO
23358  */
23359 static int
23360 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23361 	diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23362 {
23363 	struct sd_lun		*un = NULL;
23364 	struct uscsi_cmd	com;
23365 	struct scsi_inquiry	*sinq;
23366 	u_longlong_t		media_capacity;
23367 	uint64_t		capacity;
23368 	uint_t			lbasize;
23369 	uint_t			pbsize;
23370 	uchar_t			*out_data;
23371 	uchar_t			*rqbuf;
23372 	int			rval = 0;
23373 	int			rtn;
23374 	sd_ssc_t		*ssc;
23375 
23376 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23377 	    (un->un_state == SD_STATE_OFFLINE)) {
23378 		return (ENXIO);
23379 	}
23380 
23381 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23382 
23383 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23384 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23385 	ssc = sd_ssc_init(un);
23386 
23387 	/* Issue a TUR to determine if the drive is ready with media present */
23388 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23389 	if (rval == ENXIO) {
23390 		goto done;
23391 	} else if (rval != 0) {
23392 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23393 	}
23394 
23395 	/* Now get configuration data */
23396 	if (ISCD(un)) {
23397 		*dki_media_type = DK_CDROM;
23398 
23399 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23400 		if (un->un_f_mmc_cap == TRUE) {
23401 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23402 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23403 			    SD_PATH_STANDARD);
23404 
23405 			if (rtn) {
23406 				/*
23407 				 * We ignore all failures for CD and need to
23408 				 * put the assessment before processing code
23409 				 * to avoid missing assessment for FMA.
23410 				 */
23411 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23412 				/*
23413 				 * Failed for other than an illegal request
23414 				 * or command not supported
23415 				 */
23416 				if ((com.uscsi_status == STATUS_CHECK) &&
23417 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23418 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23419 					    (rqbuf[12] != 0x20)) {
23420 						rval = EIO;
23421 						goto no_assessment;
23422 					}
23423 				}
23424 			} else {
23425 				/*
23426 				 * The GET CONFIGURATION command succeeded
23427 				 * so set the media type according to the
23428 				 * returned data
23429 				 */
23430 				*dki_media_type = out_data[6];
23431 				*dki_media_type <<= 8;
23432 				*dki_media_type |= out_data[7];
23433 			}
23434 		}
23435 	} else {
23436 		/*
23437 		 * The profile list is not available, so we attempt to identify
23438 		 * the media type based on the inquiry data
23439 		 */
23440 		sinq = un->un_sd->sd_inq;
23441 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23442 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23443 			/* This is a direct access device  or optical disk */
23444 			*dki_media_type = DK_FIXED_DISK;
23445 
23446 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23447 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23448 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23449 					*dki_media_type = DK_ZIP;
23450 				} else if (
23451 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23452 					*dki_media_type = DK_JAZ;
23453 				}
23454 			}
23455 		} else {
23456 			/*
23457 			 * Not a CD, direct access or optical disk so return
23458 			 * unknown media
23459 			 */
23460 			*dki_media_type = DK_UNKNOWN;
23461 		}
23462 	}
23463 
23464 	/*
23465 	 * Now read the capacity so we can provide the lbasize,
23466 	 * pbsize and capacity.
23467 	 */
23468 	if (dki_pbsize && un->un_f_descr_format_supported)
23469 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23470 		    &pbsize, SD_PATH_DIRECT);
23471 
23472 	if (dki_pbsize == NULL || rval != 0 ||
23473 	    !un->un_f_descr_format_supported) {
23474 		rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23475 		    SD_PATH_DIRECT);
23476 
23477 		switch (rval) {
23478 		case 0:
23479 			if (un->un_f_enable_rmw &&
23480 			    un->un_phy_blocksize != 0) {
23481 				pbsize = un->un_phy_blocksize;
23482 			} else {
23483 				pbsize = lbasize;
23484 			}
23485 			media_capacity = capacity;
23486 
23487 			/*
23488 			 * sd_send_scsi_READ_CAPACITY() reports capacity in
23489 			 * un->un_sys_blocksize chunks. So we need to convert
23490 			 * it into cap.lbsize chunks.
23491 			 */
23492 			if (un->un_f_has_removable_media) {
23493 				media_capacity *= un->un_sys_blocksize;
23494 				media_capacity /= lbasize;
23495 			}
23496 			break;
23497 		case EACCES:
23498 			rval = EACCES;
23499 			goto done;
23500 		default:
23501 			rval = EIO;
23502 			goto done;
23503 		}
23504 	} else {
23505 		if (un->un_f_enable_rmw &&
23506 		    !ISP2(pbsize % DEV_BSIZE)) {
23507 			pbsize = SSD_SECSIZE;
23508 		} else if (!ISP2(lbasize % DEV_BSIZE) ||
23509 		    !ISP2(pbsize % DEV_BSIZE)) {
23510 			pbsize = lbasize = DEV_BSIZE;
23511 		}
23512 		media_capacity = capacity;
23513 	}
23514 
23515 	/*
23516 	 * If lun is expanded dynamically, update the un structure.
23517 	 */
23518 	mutex_enter(SD_MUTEX(un));
23519 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23520 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23521 	    (capacity > un->un_blockcount)) {
23522 		un->un_f_expnevent = B_FALSE;
23523 		sd_update_block_info(un, lbasize, capacity);
23524 	}
23525 	mutex_exit(SD_MUTEX(un));
23526 
23527 	*dki_lbsize = lbasize;
23528 	*dki_capacity = media_capacity;
23529 	if (dki_pbsize)
23530 		*dki_pbsize = pbsize;
23531 
23532 done:
23533 	if (rval != 0) {
23534 		if (rval == EIO)
23535 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23536 		else
23537 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23538 	}
23539 no_assessment:
23540 	sd_ssc_fini(ssc);
23541 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23542 	kmem_free(rqbuf, SENSE_LENGTH);
23543 	return (rval);
23544 }
23545 
23546 /*
23547  *    Function: sd_get_media_info
23548  *
23549  * Description: This routine is the driver entry point for handling ioctl
23550  *		requests for the media type or command set profile used by the
23551  *		drive to operate on the media (DKIOCGMEDIAINFO).
23552  *
23553  *   Arguments: dev	- the device number
23554  *		arg	- pointer to user provided dk_minfo structure
23555  *			  specifying the media type, logical block size and
23556  *			  drive capacity.
23557  *		flag	- this argument is a pass through to ddi_copyxxx()
23558  *			  directly from the mode argument of ioctl().
23559  *
23560  * Return Code: returns the value from sd_get_media_info_com
23561  */
23562 static int
23563 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23564 {
23565 	struct dk_minfo		mi;
23566 	int			rval;
23567 
23568 	rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23569 	    &mi.dki_lbsize, &mi.dki_capacity, NULL);
23570 
23571 	if (rval)
23572 		return (rval);
23573 	if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23574 		rval = EFAULT;
23575 	return (rval);
23576 }
23577 
23578 /*
23579  *    Function: sd_get_media_info_ext
23580  *
23581  * Description: This routine is the driver entry point for handling ioctl
23582  *		requests for the media type or command set profile used by the
23583  *		drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23584  *		difference this ioctl and DKIOCGMEDIAINFO is the return value
23585  *		of this ioctl contains both logical block size and physical
23586  *		block size.
23587  *
23588  *
23589  *   Arguments: dev	- the device number
23590  *		arg	- pointer to user provided dk_minfo_ext structure
23591  *			  specifying the media type, logical block size,
23592  *			  physical block size and disk capacity.
23593  *		flag	- this argument is a pass through to ddi_copyxxx()
23594  *			  directly from the mode argument of ioctl().
23595  *
23596  * Return Code: returns the value from sd_get_media_info_com
23597  */
23598 static int
23599 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23600 {
23601 	struct dk_minfo_ext	mie;
23602 	int			rval = 0;
23603 
23604 	rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23605 	    &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23606 
23607 	if (rval)
23608 		return (rval);
23609 	if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23610 		rval = EFAULT;
23611 	return (rval);
23612 
23613 }
23614 
23615 /*
23616  *    Function: sd_watch_request_submit
23617  *
23618  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23619  *		depending on which is supported by device.
23620  */
23621 static opaque_t
23622 sd_watch_request_submit(struct sd_lun *un)
23623 {
23624 	dev_t			dev;
23625 
23626 	/* All submissions are unified to use same device number */
23627 	dev = sd_make_device(SD_DEVINFO(un));
23628 
23629 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23630 		return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23631 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23632 		    (caddr_t)dev));
23633 	} else {
23634 		return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23635 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23636 		    (caddr_t)dev));
23637 	}
23638 }
23639 
23640 
23641 /*
23642  *    Function: sd_check_media
23643  *
23644  * Description: This utility routine implements the functionality for the
23645  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23646  *		driver state changes from that specified by the user
23647  *		(inserted or ejected). For example, if the user specifies
23648  *		DKIO_EJECTED and the current media state is inserted this
23649  *		routine will immediately return DKIO_INSERTED. However, if the
23650  *		current media state is not inserted the user thread will be
23651  *		blocked until the drive state changes. If DKIO_NONE is specified
23652  *		the user thread will block until a drive state change occurs.
23653  *
23654  *   Arguments: dev  - the device number
23655  *		state  - user pointer to a dkio_state, updated with the current
23656  *			drive state at return.
23657  *
23658  * Return Code: ENXIO
23659  *		EIO
23660  *		EAGAIN
23661  *		EINTR
23662  */
23663 
23664 static int
23665 sd_check_media(dev_t dev, enum dkio_state state)
23666 {
23667 	struct sd_lun		*un = NULL;
23668 	enum dkio_state		prev_state;
23669 	opaque_t		token = NULL;
23670 	int			rval = 0;
23671 	sd_ssc_t		*ssc;
23672 
23673 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23674 		return (ENXIO);
23675 	}
23676 
23677 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23678 
23679 	ssc = sd_ssc_init(un);
23680 
23681 	mutex_enter(SD_MUTEX(un));
23682 
23683 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23684 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
23685 
23686 	prev_state = un->un_mediastate;
23687 
23688 	/* is there anything to do? */
23689 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23690 		/*
23691 		 * submit the request to the scsi_watch service;
23692 		 * scsi_media_watch_cb() does the real work
23693 		 */
23694 		mutex_exit(SD_MUTEX(un));
23695 
23696 		/*
23697 		 * This change handles the case where a scsi watch request is
23698 		 * added to a device that is powered down. To accomplish this
23699 		 * we power up the device before adding the scsi watch request,
23700 		 * since the scsi watch sends a TUR directly to the device
23701 		 * which the device cannot handle if it is powered down.
23702 		 */
23703 		if (sd_pm_entry(un) != DDI_SUCCESS) {
23704 			mutex_enter(SD_MUTEX(un));
23705 			goto done;
23706 		}
23707 
23708 		token = sd_watch_request_submit(un);
23709 
23710 		sd_pm_exit(un);
23711 
23712 		mutex_enter(SD_MUTEX(un));
23713 		if (token == NULL) {
23714 			rval = EAGAIN;
23715 			goto done;
23716 		}
23717 
23718 		/*
23719 		 * This is a special case IOCTL that doesn't return
23720 		 * until the media state changes. Routine sdpower
23721 		 * knows about and handles this so don't count it
23722 		 * as an active cmd in the driver, which would
23723 		 * keep the device busy to the pm framework.
23724 		 * If the count isn't decremented the device can't
23725 		 * be powered down.
23726 		 */
23727 		un->un_ncmds_in_driver--;
23728 		ASSERT(un->un_ncmds_in_driver >= 0);
23729 
23730 		/*
23731 		 * if a prior request had been made, this will be the same
23732 		 * token, as scsi_watch was designed that way.
23733 		 */
23734 		un->un_swr_token = token;
23735 		un->un_specified_mediastate = state;
23736 
23737 		/*
23738 		 * now wait for media change
23739 		 * we will not be signalled unless mediastate == state but it is
23740 		 * still better to test for this condition, since there is a
23741 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23742 		 */
23743 		SD_TRACE(SD_LOG_COMMON, un,
23744 		    "sd_check_media: waiting for media state change\n");
23745 		while (un->un_mediastate == state) {
23746 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23747 				SD_TRACE(SD_LOG_COMMON, un,
23748 				    "sd_check_media: waiting for media state "
23749 				    "was interrupted\n");
23750 				un->un_ncmds_in_driver++;
23751 				rval = EINTR;
23752 				goto done;
23753 			}
23754 			SD_TRACE(SD_LOG_COMMON, un,
23755 			    "sd_check_media: received signal, state=%x\n",
23756 			    un->un_mediastate);
23757 		}
23758 		/*
23759 		 * Inc the counter to indicate the device once again
23760 		 * has an active outstanding cmd.
23761 		 */
23762 		un->un_ncmds_in_driver++;
23763 	}
23764 
23765 	/* invalidate geometry */
23766 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23767 		sr_ejected(un);
23768 	}
23769 
23770 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23771 		uint64_t	capacity;
23772 		uint_t		lbasize;
23773 
23774 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23775 		mutex_exit(SD_MUTEX(un));
23776 		/*
23777 		 * Since the following routines use SD_PATH_DIRECT, we must
23778 		 * call PM directly before the upcoming disk accesses. This
23779 		 * may cause the disk to be power/spin up.
23780 		 */
23781 
23782 		if (sd_pm_entry(un) == DDI_SUCCESS) {
23783 			rval = sd_send_scsi_READ_CAPACITY(ssc,
23784 			    &capacity, &lbasize, SD_PATH_DIRECT);
23785 			if (rval != 0) {
23786 				sd_pm_exit(un);
23787 				if (rval == EIO)
23788 					sd_ssc_assessment(ssc,
23789 					    SD_FMT_STATUS_CHECK);
23790 				else
23791 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23792 				mutex_enter(SD_MUTEX(un));
23793 				goto done;
23794 			}
23795 		} else {
23796 			rval = EIO;
23797 			mutex_enter(SD_MUTEX(un));
23798 			goto done;
23799 		}
23800 		mutex_enter(SD_MUTEX(un));
23801 
23802 		sd_update_block_info(un, lbasize, capacity);
23803 
23804 		/*
23805 		 *  Check if the media in the device is writable or not
23806 		 */
23807 		if (ISCD(un)) {
23808 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23809 		}
23810 
23811 		mutex_exit(SD_MUTEX(un));
23812 		cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23813 		if ((cmlb_validate(un->un_cmlbhandle, 0,
23814 		    (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23815 			sd_set_pstats(un);
23816 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23817 			    "sd_check_media: un:0x%p pstats created and "
23818 			    "set\n", un);
23819 		}
23820 
23821 		rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23822 		    SD_PATH_DIRECT);
23823 
23824 		sd_pm_exit(un);
23825 
23826 		if (rval != 0) {
23827 			if (rval == EIO)
23828 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23829 			else
23830 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23831 		}
23832 
23833 		mutex_enter(SD_MUTEX(un));
23834 	}
23835 done:
23836 	sd_ssc_fini(ssc);
23837 	un->un_f_watcht_stopped = FALSE;
23838 	if (token != NULL && un->un_swr_token != NULL) {
23839 		/*
23840 		 * Use of this local token and the mutex ensures that we avoid
23841 		 * some race conditions associated with terminating the
23842 		 * scsi watch.
23843 		 */
23844 		token = un->un_swr_token;
23845 		mutex_exit(SD_MUTEX(un));
23846 		(void) scsi_watch_request_terminate(token,
23847 		    SCSI_WATCH_TERMINATE_WAIT);
23848 		if (scsi_watch_get_ref_count(token) == 0) {
23849 			mutex_enter(SD_MUTEX(un));
23850 			un->un_swr_token = (opaque_t)NULL;
23851 		} else {
23852 			mutex_enter(SD_MUTEX(un));
23853 		}
23854 	}
23855 
23856 	/*
23857 	 * Update the capacity kstat value, if no media previously
23858 	 * (capacity kstat is 0) and a media has been inserted
23859 	 * (un_f_blockcount_is_valid == TRUE)
23860 	 */
23861 	if (un->un_errstats) {
23862 		struct sd_errstats	*stp = NULL;
23863 
23864 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
23865 		if ((stp->sd_capacity.value.ui64 == 0) &&
23866 		    (un->un_f_blockcount_is_valid == TRUE)) {
23867 			stp->sd_capacity.value.ui64 =
23868 			    (uint64_t)((uint64_t)un->un_blockcount *
23869 			    un->un_sys_blocksize);
23870 		}
23871 	}
23872 	mutex_exit(SD_MUTEX(un));
23873 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23874 	return (rval);
23875 }
23876 
23877 
23878 /*
23879  *    Function: sd_delayed_cv_broadcast
23880  *
23881  * Description: Delayed cv_broadcast to allow for target to recover from media
23882  *		insertion.
23883  *
23884  *   Arguments: arg - driver soft state (unit) structure
23885  */
23886 
23887 static void
23888 sd_delayed_cv_broadcast(void *arg)
23889 {
23890 	struct sd_lun *un = arg;
23891 
23892 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23893 
23894 	mutex_enter(SD_MUTEX(un));
23895 	un->un_dcvb_timeid = NULL;
23896 	cv_broadcast(&un->un_state_cv);
23897 	mutex_exit(SD_MUTEX(un));
23898 }
23899 
23900 
23901 /*
23902  *    Function: sd_media_watch_cb
23903  *
23904  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23905  *		routine processes the TUR sense data and updates the driver
23906  *		state if a transition has occurred. The user thread
23907  *		(sd_check_media) is then signalled.
23908  *
23909  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23910  *			among multiple watches that share this callback function
23911  *		resultp - scsi watch facility result packet containing scsi
23912  *			  packet, status byte and sense data
23913  *
23914  * Return Code: 0 for success, -1 for failure
23915  */
23916 
23917 static int
23918 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23919 {
23920 	struct sd_lun			*un;
23921 	struct scsi_status		*statusp = resultp->statusp;
23922 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
23923 	enum dkio_state			state = DKIO_NONE;
23924 	dev_t				dev = (dev_t)arg;
23925 	uchar_t				actual_sense_length;
23926 	uint8_t				skey, asc, ascq;
23927 
23928 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23929 		return (-1);
23930 	}
23931 	actual_sense_length = resultp->actual_sense_length;
23932 
23933 	mutex_enter(SD_MUTEX(un));
23934 	SD_TRACE(SD_LOG_COMMON, un,
23935 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23936 	    *((char *)statusp), (void *)sensep, actual_sense_length);
23937 
23938 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23939 		un->un_mediastate = DKIO_DEV_GONE;
23940 		cv_broadcast(&un->un_state_cv);
23941 		mutex_exit(SD_MUTEX(un));
23942 
23943 		return (0);
23944 	}
23945 
23946 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23947 		if (sd_gesn_media_data_valid(resultp->mmc_data)) {
23948 			if ((resultp->mmc_data[5] &
23949 			    SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
23950 				state = DKIO_INSERTED;
23951 			} else {
23952 				state = DKIO_EJECTED;
23953 			}
23954 			if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
23955 			    SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
23956 				sd_log_eject_request_event(un, KM_NOSLEEP);
23957 			}
23958 		}
23959 	} else if (sensep != NULL) {
23960 		/*
23961 		 * If there was a check condition then sensep points to valid
23962 		 * sense data. If status was not a check condition but a
23963 		 * reservation or busy status then the new state is DKIO_NONE.
23964 		 */
23965 		skey = scsi_sense_key(sensep);
23966 		asc = scsi_sense_asc(sensep);
23967 		ascq = scsi_sense_ascq(sensep);
23968 
23969 		SD_INFO(SD_LOG_COMMON, un,
23970 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
23971 		    skey, asc, ascq);
23972 		/* This routine only uses up to 13 bytes of sense data. */
23973 		if (actual_sense_length >= 13) {
23974 			if (skey == KEY_UNIT_ATTENTION) {
23975 				if (asc == 0x28) {
23976 					state = DKIO_INSERTED;
23977 				}
23978 			} else if (skey == KEY_NOT_READY) {
23979 				/*
23980 				 * Sense data of 02/06/00 means that the
23981 				 * drive could not read the media (No
23982 				 * reference position found). In this case
23983 				 * to prevent a hang on the DKIOCSTATE IOCTL
23984 				 * we set the media state to DKIO_INSERTED.
23985 				 */
23986 				if (asc == 0x06 && ascq == 0x00)
23987 					state = DKIO_INSERTED;
23988 
23989 				/*
23990 				 * if 02/04/02  means that the host
23991 				 * should send start command. Explicitly
23992 				 * leave the media state as is
23993 				 * (inserted) as the media is inserted
23994 				 * and host has stopped device for PM
23995 				 * reasons. Upon next true read/write
23996 				 * to this media will bring the
23997 				 * device to the right state good for
23998 				 * media access.
23999 				 */
24000 				if (asc == 0x3a) {
24001 					state = DKIO_EJECTED;
24002 				} else {
24003 					/*
24004 					 * If the drive is busy with an
24005 					 * operation or long write, keep the
24006 					 * media in an inserted state.
24007 					 */
24008 
24009 					if ((asc == 0x04) &&
24010 					    ((ascq == 0x02) ||
24011 					    (ascq == 0x07) ||
24012 					    (ascq == 0x08))) {
24013 						state = DKIO_INSERTED;
24014 					}
24015 				}
24016 			} else if (skey == KEY_NO_SENSE) {
24017 				if ((asc == 0x00) && (ascq == 0x00)) {
24018 					/*
24019 					 * Sense Data 00/00/00 does not provide
24020 					 * any information about the state of
24021 					 * the media. Ignore it.
24022 					 */
24023 					mutex_exit(SD_MUTEX(un));
24024 					return (0);
24025 				}
24026 			}
24027 		}
24028 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24029 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24030 		state = DKIO_INSERTED;
24031 	}
24032 
24033 	SD_TRACE(SD_LOG_COMMON, un,
24034 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24035 	    state, un->un_specified_mediastate);
24036 
24037 	/*
24038 	 * now signal the waiting thread if this is *not* the specified state;
24039 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24040 	 * to recover
24041 	 */
24042 	if (state != un->un_specified_mediastate) {
24043 		un->un_mediastate = state;
24044 		if (state == DKIO_INSERTED) {
24045 			/*
24046 			 * delay the signal to give the drive a chance
24047 			 * to do what it apparently needs to do
24048 			 */
24049 			SD_TRACE(SD_LOG_COMMON, un,
24050 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24051 			if (un->un_dcvb_timeid == NULL) {
24052 				un->un_dcvb_timeid =
24053 				    timeout(sd_delayed_cv_broadcast, un,
24054 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24055 			}
24056 		} else {
24057 			SD_TRACE(SD_LOG_COMMON, un,
24058 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24059 			cv_broadcast(&un->un_state_cv);
24060 		}
24061 	}
24062 	mutex_exit(SD_MUTEX(un));
24063 	return (0);
24064 }
24065 
24066 
24067 /*
24068  *    Function: sd_dkio_get_temp
24069  *
24070  * Description: This routine is the driver entry point for handling ioctl
24071  *		requests to get the disk temperature.
24072  *
24073  *   Arguments: dev  - the device number
24074  *		arg  - pointer to user provided dk_temperature structure.
24075  *		flag - this argument is a pass through to ddi_copyxxx()
24076  *		       directly from the mode argument of ioctl().
24077  *
24078  * Return Code: 0
24079  *		EFAULT
24080  *		ENXIO
24081  *		EAGAIN
24082  */
24083 
24084 static int
24085 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24086 {
24087 	struct sd_lun		*un = NULL;
24088 	struct dk_temperature	*dktemp = NULL;
24089 	uchar_t			*temperature_page;
24090 	int			rval = 0;
24091 	int			path_flag = SD_PATH_STANDARD;
24092 	sd_ssc_t		*ssc;
24093 
24094 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24095 		return (ENXIO);
24096 	}
24097 
24098 	ssc = sd_ssc_init(un);
24099 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24100 
24101 	/* copyin the disk temp argument to get the user flags */
24102 	if (ddi_copyin((void *)arg, dktemp,
24103 	    sizeof (struct dk_temperature), flag) != 0) {
24104 		rval = EFAULT;
24105 		goto done;
24106 	}
24107 
24108 	/* Initialize the temperature to invalid. */
24109 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24110 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24111 
24112 	/*
24113 	 * Note: Investigate removing the "bypass pm" semantic.
24114 	 * Can we just bypass PM always?
24115 	 */
24116 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24117 		path_flag = SD_PATH_DIRECT;
24118 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24119 		mutex_enter(&un->un_pm_mutex);
24120 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24121 			/*
24122 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24123 			 * in low power mode, we can not wake it up, Need to
24124 			 * return EAGAIN.
24125 			 */
24126 			mutex_exit(&un->un_pm_mutex);
24127 			rval = EAGAIN;
24128 			goto done;
24129 		} else {
24130 			/*
24131 			 * Indicate to PM the device is busy. This is required
24132 			 * to avoid a race - i.e. the ioctl is issuing a
24133 			 * command and the pm framework brings down the device
24134 			 * to low power mode (possible power cut-off on some
24135 			 * platforms).
24136 			 */
24137 			mutex_exit(&un->un_pm_mutex);
24138 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24139 				rval = EAGAIN;
24140 				goto done;
24141 			}
24142 		}
24143 	}
24144 
24145 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24146 
24147 	rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24148 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24149 	if (rval != 0)
24150 		goto done2;
24151 
24152 	/*
24153 	 * For the current temperature verify that the parameter length is 0x02
24154 	 * and the parameter code is 0x00
24155 	 */
24156 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24157 	    (temperature_page[5] == 0x00)) {
24158 		if (temperature_page[9] == 0xFF) {
24159 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24160 		} else {
24161 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24162 		}
24163 	}
24164 
24165 	/*
24166 	 * For the reference temperature verify that the parameter
24167 	 * length is 0x02 and the parameter code is 0x01
24168 	 */
24169 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24170 	    (temperature_page[11] == 0x01)) {
24171 		if (temperature_page[15] == 0xFF) {
24172 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24173 		} else {
24174 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24175 		}
24176 	}
24177 
24178 	/* Do the copyout regardless of the temperature commands status. */
24179 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24180 	    flag) != 0) {
24181 		rval = EFAULT;
24182 		goto done1;
24183 	}
24184 
24185 done2:
24186 	if (rval != 0) {
24187 		if (rval == EIO)
24188 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24189 		else
24190 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24191 	}
24192 done1:
24193 	if (path_flag == SD_PATH_DIRECT) {
24194 		sd_pm_exit(un);
24195 	}
24196 
24197 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24198 done:
24199 	sd_ssc_fini(ssc);
24200 	if (dktemp != NULL) {
24201 		kmem_free(dktemp, sizeof (struct dk_temperature));
24202 	}
24203 
24204 	return (rval);
24205 }
24206 
24207 
24208 /*
24209  *    Function: sd_log_page_supported
24210  *
24211  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24212  *		supported log pages.
24213  *
24214  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24215  *                      structure for this target.
24216  *		log_page -
24217  *
24218  * Return Code: -1 - on error (log sense is optional and may not be supported).
24219  *		0  - log page not found.
24220  *  		1  - log page found.
24221  */
24222 
24223 static int
24224 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24225 {
24226 	uchar_t *log_page_data;
24227 	int	i;
24228 	int	match = 0;
24229 	int	log_size;
24230 	int	status = 0;
24231 	struct sd_lun	*un;
24232 
24233 	ASSERT(ssc != NULL);
24234 	un = ssc->ssc_un;
24235 	ASSERT(un != NULL);
24236 
24237 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24238 
24239 	status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24240 	    SD_PATH_DIRECT);
24241 
24242 	if (status != 0) {
24243 		if (status == EIO) {
24244 			/*
24245 			 * Some disks do not support log sense, we
24246 			 * should ignore this kind of error(sense key is
24247 			 * 0x5 - illegal request).
24248 			 */
24249 			uint8_t *sensep;
24250 			int senlen;
24251 
24252 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24253 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24254 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
24255 
24256 			if (senlen > 0 &&
24257 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24258 				sd_ssc_assessment(ssc,
24259 				    SD_FMT_IGNORE_COMPROMISE);
24260 			} else {
24261 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24262 			}
24263 		} else {
24264 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24265 		}
24266 
24267 		SD_ERROR(SD_LOG_COMMON, un,
24268 		    "sd_log_page_supported: failed log page retrieval\n");
24269 		kmem_free(log_page_data, 0xFF);
24270 		return (-1);
24271 	}
24272 
24273 	log_size = log_page_data[3];
24274 
24275 	/*
24276 	 * The list of supported log pages start from the fourth byte. Check
24277 	 * until we run out of log pages or a match is found.
24278 	 */
24279 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24280 		if (log_page_data[i] == log_page) {
24281 			match++;
24282 		}
24283 	}
24284 	kmem_free(log_page_data, 0xFF);
24285 	return (match);
24286 }
24287 
24288 
24289 /*
24290  *    Function: sd_mhdioc_failfast
24291  *
24292  * Description: This routine is the driver entry point for handling ioctl
24293  *		requests to enable/disable the multihost failfast option.
24294  *		(MHIOCENFAILFAST)
24295  *
24296  *   Arguments: dev	- the device number
24297  *		arg	- user specified probing interval.
24298  *		flag	- this argument is a pass through to ddi_copyxxx()
24299  *			  directly from the mode argument of ioctl().
24300  *
24301  * Return Code: 0
24302  *		EFAULT
24303  *		ENXIO
24304  */
24305 
24306 static int
24307 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24308 {
24309 	struct sd_lun	*un = NULL;
24310 	int		mh_time;
24311 	int		rval = 0;
24312 
24313 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24314 		return (ENXIO);
24315 	}
24316 
24317 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24318 		return (EFAULT);
24319 
24320 	if (mh_time) {
24321 		mutex_enter(SD_MUTEX(un));
24322 		un->un_resvd_status |= SD_FAILFAST;
24323 		mutex_exit(SD_MUTEX(un));
24324 		/*
24325 		 * If mh_time is INT_MAX, then this ioctl is being used for
24326 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24327 		 */
24328 		if (mh_time != INT_MAX) {
24329 			rval = sd_check_mhd(dev, mh_time);
24330 		}
24331 	} else {
24332 		(void) sd_check_mhd(dev, 0);
24333 		mutex_enter(SD_MUTEX(un));
24334 		un->un_resvd_status &= ~SD_FAILFAST;
24335 		mutex_exit(SD_MUTEX(un));
24336 	}
24337 	return (rval);
24338 }
24339 
24340 
24341 /*
24342  *    Function: sd_mhdioc_takeown
24343  *
24344  * Description: This routine is the driver entry point for handling ioctl
24345  *		requests to forcefully acquire exclusive access rights to the
24346  *		multihost disk (MHIOCTKOWN).
24347  *
24348  *   Arguments: dev	- the device number
24349  *		arg	- user provided structure specifying the delay
24350  *			  parameters in milliseconds
24351  *		flag	- this argument is a pass through to ddi_copyxxx()
24352  *			  directly from the mode argument of ioctl().
24353  *
24354  * Return Code: 0
24355  *		EFAULT
24356  *		ENXIO
24357  */
24358 
24359 static int
24360 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24361 {
24362 	struct sd_lun		*un = NULL;
24363 	struct mhioctkown	*tkown = NULL;
24364 	int			rval = 0;
24365 
24366 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24367 		return (ENXIO);
24368 	}
24369 
24370 	if (arg != NULL) {
24371 		tkown = (struct mhioctkown *)
24372 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24373 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24374 		if (rval != 0) {
24375 			rval = EFAULT;
24376 			goto error;
24377 		}
24378 	}
24379 
24380 	rval = sd_take_ownership(dev, tkown);
24381 	mutex_enter(SD_MUTEX(un));
24382 	if (rval == 0) {
24383 		un->un_resvd_status |= SD_RESERVE;
24384 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24385 			sd_reinstate_resv_delay =
24386 			    tkown->reinstate_resv_delay * 1000;
24387 		} else {
24388 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24389 		}
24390 		/*
24391 		 * Give the scsi_watch routine interval set by
24392 		 * the MHIOCENFAILFAST ioctl precedence here.
24393 		 */
24394 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24395 			mutex_exit(SD_MUTEX(un));
24396 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24397 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24398 			    "sd_mhdioc_takeown : %d\n",
24399 			    sd_reinstate_resv_delay);
24400 		} else {
24401 			mutex_exit(SD_MUTEX(un));
24402 		}
24403 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24404 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24405 	} else {
24406 		un->un_resvd_status &= ~SD_RESERVE;
24407 		mutex_exit(SD_MUTEX(un));
24408 	}
24409 
24410 error:
24411 	if (tkown != NULL) {
24412 		kmem_free(tkown, sizeof (struct mhioctkown));
24413 	}
24414 	return (rval);
24415 }
24416 
24417 
24418 /*
24419  *    Function: sd_mhdioc_release
24420  *
24421  * Description: This routine is the driver entry point for handling ioctl
24422  *		requests to release exclusive access rights to the multihost
24423  *		disk (MHIOCRELEASE).
24424  *
24425  *   Arguments: dev	- the device number
24426  *
24427  * Return Code: 0
24428  *		ENXIO
24429  */
24430 
24431 static int
24432 sd_mhdioc_release(dev_t dev)
24433 {
24434 	struct sd_lun		*un = NULL;
24435 	timeout_id_t		resvd_timeid_save;
24436 	int			resvd_status_save;
24437 	int			rval = 0;
24438 
24439 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24440 		return (ENXIO);
24441 	}
24442 
24443 	mutex_enter(SD_MUTEX(un));
24444 	resvd_status_save = un->un_resvd_status;
24445 	un->un_resvd_status &=
24446 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24447 	if (un->un_resvd_timeid) {
24448 		resvd_timeid_save = un->un_resvd_timeid;
24449 		un->un_resvd_timeid = NULL;
24450 		mutex_exit(SD_MUTEX(un));
24451 		(void) untimeout(resvd_timeid_save);
24452 	} else {
24453 		mutex_exit(SD_MUTEX(un));
24454 	}
24455 
24456 	/*
24457 	 * destroy any pending timeout thread that may be attempting to
24458 	 * reinstate reservation on this device.
24459 	 */
24460 	sd_rmv_resv_reclaim_req(dev);
24461 
24462 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24463 		mutex_enter(SD_MUTEX(un));
24464 		if ((un->un_mhd_token) &&
24465 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24466 			mutex_exit(SD_MUTEX(un));
24467 			(void) sd_check_mhd(dev, 0);
24468 		} else {
24469 			mutex_exit(SD_MUTEX(un));
24470 		}
24471 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24472 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24473 	} else {
24474 		/*
24475 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24476 		 */
24477 		mutex_enter(SD_MUTEX(un));
24478 		un->un_resvd_status = resvd_status_save;
24479 		mutex_exit(SD_MUTEX(un));
24480 	}
24481 	return (rval);
24482 }
24483 
24484 
24485 /*
24486  *    Function: sd_mhdioc_register_devid
24487  *
24488  * Description: This routine is the driver entry point for handling ioctl
24489  *		requests to register the device id (MHIOCREREGISTERDEVID).
24490  *
24491  *		Note: The implementation for this ioctl has been updated to
24492  *		be consistent with the original PSARC case (1999/357)
24493  *		(4375899, 4241671, 4220005)
24494  *
24495  *   Arguments: dev	- the device number
24496  *
24497  * Return Code: 0
24498  *		ENXIO
24499  */
24500 
24501 static int
24502 sd_mhdioc_register_devid(dev_t dev)
24503 {
24504 	struct sd_lun	*un = NULL;
24505 	int		rval = 0;
24506 	sd_ssc_t	*ssc;
24507 
24508 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24509 		return (ENXIO);
24510 	}
24511 
24512 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24513 
24514 	mutex_enter(SD_MUTEX(un));
24515 
24516 	/* If a devid already exists, de-register it */
24517 	if (un->un_devid != NULL) {
24518 		ddi_devid_unregister(SD_DEVINFO(un));
24519 		/*
24520 		 * After unregister devid, needs to free devid memory
24521 		 */
24522 		ddi_devid_free(un->un_devid);
24523 		un->un_devid = NULL;
24524 	}
24525 
24526 	/* Check for reservation conflict */
24527 	mutex_exit(SD_MUTEX(un));
24528 	ssc = sd_ssc_init(un);
24529 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24530 	mutex_enter(SD_MUTEX(un));
24531 
24532 	switch (rval) {
24533 	case 0:
24534 		sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24535 		break;
24536 	case EACCES:
24537 		break;
24538 	default:
24539 		rval = EIO;
24540 	}
24541 
24542 	mutex_exit(SD_MUTEX(un));
24543 	if (rval != 0) {
24544 		if (rval == EIO)
24545 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24546 		else
24547 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24548 	}
24549 	sd_ssc_fini(ssc);
24550 	return (rval);
24551 }
24552 
24553 
24554 /*
24555  *    Function: sd_mhdioc_inkeys
24556  *
24557  * Description: This routine is the driver entry point for handling ioctl
24558  *		requests to issue the SCSI-3 Persistent In Read Keys command
24559  *		to the device (MHIOCGRP_INKEYS).
24560  *
24561  *   Arguments: dev	- the device number
24562  *		arg	- user provided in_keys structure
24563  *		flag	- this argument is a pass through to ddi_copyxxx()
24564  *			  directly from the mode argument of ioctl().
24565  *
24566  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24567  *		ENXIO
24568  *		EFAULT
24569  */
24570 
24571 static int
24572 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24573 {
24574 	struct sd_lun		*un;
24575 	mhioc_inkeys_t		inkeys;
24576 	int			rval = 0;
24577 
24578 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24579 		return (ENXIO);
24580 	}
24581 
24582 #ifdef _MULTI_DATAMODEL
24583 	switch (ddi_model_convert_from(flag & FMODELS)) {
24584 	case DDI_MODEL_ILP32: {
24585 		struct mhioc_inkeys32	inkeys32;
24586 
24587 		if (ddi_copyin(arg, &inkeys32,
24588 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24589 			return (EFAULT);
24590 		}
24591 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24592 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24593 		    &inkeys, flag)) != 0) {
24594 			return (rval);
24595 		}
24596 		inkeys32.generation = inkeys.generation;
24597 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24598 		    flag) != 0) {
24599 			return (EFAULT);
24600 		}
24601 		break;
24602 	}
24603 	case DDI_MODEL_NONE:
24604 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24605 		    flag) != 0) {
24606 			return (EFAULT);
24607 		}
24608 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24609 		    &inkeys, flag)) != 0) {
24610 			return (rval);
24611 		}
24612 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24613 		    flag) != 0) {
24614 			return (EFAULT);
24615 		}
24616 		break;
24617 	}
24618 
24619 #else /* ! _MULTI_DATAMODEL */
24620 
24621 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24622 		return (EFAULT);
24623 	}
24624 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24625 	if (rval != 0) {
24626 		return (rval);
24627 	}
24628 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24629 		return (EFAULT);
24630 	}
24631 
24632 #endif /* _MULTI_DATAMODEL */
24633 
24634 	return (rval);
24635 }
24636 
24637 
24638 /*
24639  *    Function: sd_mhdioc_inresv
24640  *
24641  * Description: This routine is the driver entry point for handling ioctl
24642  *		requests to issue the SCSI-3 Persistent In Read Reservations
24643  *		command to the device (MHIOCGRP_INKEYS).
24644  *
24645  *   Arguments: dev	- the device number
24646  *		arg	- user provided in_resv structure
24647  *		flag	- this argument is a pass through to ddi_copyxxx()
24648  *			  directly from the mode argument of ioctl().
24649  *
24650  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24651  *		ENXIO
24652  *		EFAULT
24653  */
24654 
24655 static int
24656 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24657 {
24658 	struct sd_lun		*un;
24659 	mhioc_inresvs_t		inresvs;
24660 	int			rval = 0;
24661 
24662 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24663 		return (ENXIO);
24664 	}
24665 
24666 #ifdef _MULTI_DATAMODEL
24667 
24668 	switch (ddi_model_convert_from(flag & FMODELS)) {
24669 	case DDI_MODEL_ILP32: {
24670 		struct mhioc_inresvs32	inresvs32;
24671 
24672 		if (ddi_copyin(arg, &inresvs32,
24673 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24674 			return (EFAULT);
24675 		}
24676 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24677 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24678 		    &inresvs, flag)) != 0) {
24679 			return (rval);
24680 		}
24681 		inresvs32.generation = inresvs.generation;
24682 		if (ddi_copyout(&inresvs32, arg,
24683 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24684 			return (EFAULT);
24685 		}
24686 		break;
24687 	}
24688 	case DDI_MODEL_NONE:
24689 		if (ddi_copyin(arg, &inresvs,
24690 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24691 			return (EFAULT);
24692 		}
24693 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24694 		    &inresvs, flag)) != 0) {
24695 			return (rval);
24696 		}
24697 		if (ddi_copyout(&inresvs, arg,
24698 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24699 			return (EFAULT);
24700 		}
24701 		break;
24702 	}
24703 
24704 #else /* ! _MULTI_DATAMODEL */
24705 
24706 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24707 		return (EFAULT);
24708 	}
24709 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24710 	if (rval != 0) {
24711 		return (rval);
24712 	}
24713 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24714 		return (EFAULT);
24715 	}
24716 
24717 #endif /* ! _MULTI_DATAMODEL */
24718 
24719 	return (rval);
24720 }
24721 
24722 
24723 /*
24724  * The following routines support the clustering functionality described below
24725  * and implement lost reservation reclaim functionality.
24726  *
24727  * Clustering
24728  * ----------
24729  * The clustering code uses two different, independent forms of SCSI
24730  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24731  * Persistent Group Reservations. For any particular disk, it will use either
24732  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24733  *
24734  * SCSI-2
24735  * The cluster software takes ownership of a multi-hosted disk by issuing the
24736  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24737  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24738  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24739  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24740  * driver. The meaning of failfast is that if the driver (on this host) ever
24741  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24742  * it should immediately panic the host. The motivation for this ioctl is that
24743  * if this host does encounter reservation conflict, the underlying cause is
24744  * that some other host of the cluster has decided that this host is no longer
24745  * in the cluster and has seized control of the disks for itself. Since this
24746  * host is no longer in the cluster, it ought to panic itself. The
24747  * MHIOCENFAILFAST ioctl does two things:
24748  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24749  *      error to panic the host
24750  *      (b) it sets up a periodic timer to test whether this host still has
24751  *      "access" (in that no other host has reserved the device):  if the
24752  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24753  *      purpose of that periodic timer is to handle scenarios where the host is
24754  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24755  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24756  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24757  * the device itself.
24758  *
24759  * SCSI-3 PGR
24760  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24761  * facility is supported through the shared multihost disk ioctls
24762  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24763  * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24764  *
24765  * Reservation Reclaim:
24766  * --------------------
24767  * To support the lost reservation reclaim operations this driver creates a
24768  * single thread to handle reinstating reservations on all devices that have
24769  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24770  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24771  * and the reservation reclaim thread loops through the requests to regain the
24772  * lost reservations.
24773  */
24774 
24775 /*
24776  *    Function: sd_check_mhd()
24777  *
24778  * Description: This function sets up and submits a scsi watch request or
24779  *		terminates an existing watch request. This routine is used in
24780  *		support of reservation reclaim.
24781  *
24782  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24783  *			 among multiple watches that share the callback function
24784  *		interval - the number of microseconds specifying the watch
24785  *			   interval for issuing TEST UNIT READY commands. If
24786  *			   set to 0 the watch should be terminated. If the
24787  *			   interval is set to 0 and if the device is required
24788  *			   to hold reservation while disabling failfast, the
24789  *			   watch is restarted with an interval of
24790  *			   reinstate_resv_delay.
24791  *
24792  * Return Code: 0	   - Successful submit/terminate of scsi watch request
24793  *		ENXIO      - Indicates an invalid device was specified
24794  *		EAGAIN     - Unable to submit the scsi watch request
24795  */
24796 
24797 static int
24798 sd_check_mhd(dev_t dev, int interval)
24799 {
24800 	struct sd_lun	*un;
24801 	opaque_t	token;
24802 
24803 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24804 		return (ENXIO);
24805 	}
24806 
24807 	/* is this a watch termination request? */
24808 	if (interval == 0) {
24809 		mutex_enter(SD_MUTEX(un));
24810 		/* if there is an existing watch task then terminate it */
24811 		if (un->un_mhd_token) {
24812 			token = un->un_mhd_token;
24813 			un->un_mhd_token = NULL;
24814 			mutex_exit(SD_MUTEX(un));
24815 			(void) scsi_watch_request_terminate(token,
24816 			    SCSI_WATCH_TERMINATE_ALL_WAIT);
24817 			mutex_enter(SD_MUTEX(un));
24818 		} else {
24819 			mutex_exit(SD_MUTEX(un));
24820 			/*
24821 			 * Note: If we return here we don't check for the
24822 			 * failfast case. This is the original legacy
24823 			 * implementation but perhaps we should be checking
24824 			 * the failfast case.
24825 			 */
24826 			return (0);
24827 		}
24828 		/*
24829 		 * If the device is required to hold reservation while
24830 		 * disabling failfast, we need to restart the scsi_watch
24831 		 * routine with an interval of reinstate_resv_delay.
24832 		 */
24833 		if (un->un_resvd_status & SD_RESERVE) {
24834 			interval = sd_reinstate_resv_delay/1000;
24835 		} else {
24836 			/* no failfast so bail */
24837 			mutex_exit(SD_MUTEX(un));
24838 			return (0);
24839 		}
24840 		mutex_exit(SD_MUTEX(un));
24841 	}
24842 
24843 	/*
24844 	 * adjust minimum time interval to 1 second,
24845 	 * and convert from msecs to usecs
24846 	 */
24847 	if (interval > 0 && interval < 1000) {
24848 		interval = 1000;
24849 	}
24850 	interval *= 1000;
24851 
24852 	/*
24853 	 * submit the request to the scsi_watch service
24854 	 */
24855 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24856 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24857 	if (token == NULL) {
24858 		return (EAGAIN);
24859 	}
24860 
24861 	/*
24862 	 * save token for termination later on
24863 	 */
24864 	mutex_enter(SD_MUTEX(un));
24865 	un->un_mhd_token = token;
24866 	mutex_exit(SD_MUTEX(un));
24867 	return (0);
24868 }
24869 
24870 
24871 /*
24872  *    Function: sd_mhd_watch_cb()
24873  *
24874  * Description: This function is the call back function used by the scsi watch
24875  *		facility. The scsi watch facility sends the "Test Unit Ready"
24876  *		and processes the status. If applicable (i.e. a "Unit Attention"
24877  *		status and automatic "Request Sense" not used) the scsi watch
24878  *		facility will send a "Request Sense" and retrieve the sense data
24879  *		to be passed to this callback function. In either case the
24880  *		automatic "Request Sense" or the facility submitting one, this
24881  *		callback is passed the status and sense data.
24882  *
24883  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24884  *			among multiple watches that share this callback function
24885  *		resultp - scsi watch facility result packet containing scsi
24886  *			  packet, status byte and sense data
24887  *
24888  * Return Code: 0 - continue the watch task
24889  *		non-zero - terminate the watch task
24890  */
24891 
24892 static int
24893 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24894 {
24895 	struct sd_lun			*un;
24896 	struct scsi_status		*statusp;
24897 	uint8_t				*sensep;
24898 	struct scsi_pkt			*pkt;
24899 	uchar_t				actual_sense_length;
24900 	dev_t  				dev = (dev_t)arg;
24901 
24902 	ASSERT(resultp != NULL);
24903 	statusp			= resultp->statusp;
24904 	sensep			= (uint8_t *)resultp->sensep;
24905 	pkt			= resultp->pkt;
24906 	actual_sense_length	= resultp->actual_sense_length;
24907 
24908 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24909 		return (ENXIO);
24910 	}
24911 
24912 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
24913 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24914 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24915 
24916 	/* Begin processing of the status and/or sense data */
24917 	if (pkt->pkt_reason != CMD_CMPLT) {
24918 		/* Handle the incomplete packet */
24919 		sd_mhd_watch_incomplete(un, pkt);
24920 		return (0);
24921 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24922 		if (*((unsigned char *)statusp)
24923 		    == STATUS_RESERVATION_CONFLICT) {
24924 			/*
24925 			 * Handle a reservation conflict by panicking if
24926 			 * configured for failfast or by logging the conflict
24927 			 * and updating the reservation status
24928 			 */
24929 			mutex_enter(SD_MUTEX(un));
24930 			if ((un->un_resvd_status & SD_FAILFAST) &&
24931 			    (sd_failfast_enable)) {
24932 				sd_panic_for_res_conflict(un);
24933 				/*NOTREACHED*/
24934 			}
24935 			SD_INFO(SD_LOG_IOCTL_MHD, un,
24936 			    "sd_mhd_watch_cb: Reservation Conflict\n");
24937 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24938 			mutex_exit(SD_MUTEX(un));
24939 		}
24940 	}
24941 
24942 	if (sensep != NULL) {
24943 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24944 			mutex_enter(SD_MUTEX(un));
24945 			if ((scsi_sense_asc(sensep) ==
24946 			    SD_SCSI_RESET_SENSE_CODE) &&
24947 			    (un->un_resvd_status & SD_RESERVE)) {
24948 				/*
24949 				 * The additional sense code indicates a power
24950 				 * on or bus device reset has occurred; update
24951 				 * the reservation status.
24952 				 */
24953 				un->un_resvd_status |=
24954 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
24955 				SD_INFO(SD_LOG_IOCTL_MHD, un,
24956 				    "sd_mhd_watch_cb: Lost Reservation\n");
24957 			}
24958 		} else {
24959 			return (0);
24960 		}
24961 	} else {
24962 		mutex_enter(SD_MUTEX(un));
24963 	}
24964 
24965 	if ((un->un_resvd_status & SD_RESERVE) &&
24966 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
24967 		if (un->un_resvd_status & SD_WANT_RESERVE) {
24968 			/*
24969 			 * A reset occurred in between the last probe and this
24970 			 * one so if a timeout is pending cancel it.
24971 			 */
24972 			if (un->un_resvd_timeid) {
24973 				timeout_id_t temp_id = un->un_resvd_timeid;
24974 				un->un_resvd_timeid = NULL;
24975 				mutex_exit(SD_MUTEX(un));
24976 				(void) untimeout(temp_id);
24977 				mutex_enter(SD_MUTEX(un));
24978 			}
24979 			un->un_resvd_status &= ~SD_WANT_RESERVE;
24980 		}
24981 		if (un->un_resvd_timeid == 0) {
24982 			/* Schedule a timeout to handle the lost reservation */
24983 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
24984 			    (void *)dev,
24985 			    drv_usectohz(sd_reinstate_resv_delay));
24986 		}
24987 	}
24988 	mutex_exit(SD_MUTEX(un));
24989 	return (0);
24990 }
24991 
24992 
24993 /*
24994  *    Function: sd_mhd_watch_incomplete()
24995  *
24996  * Description: This function is used to find out why a scsi pkt sent by the
24997  *		scsi watch facility was not completed. Under some scenarios this
24998  *		routine will return. Otherwise it will send a bus reset to see
24999  *		if the drive is still online.
25000  *
25001  *   Arguments: un  - driver soft state (unit) structure
25002  *		pkt - incomplete scsi pkt
25003  */
25004 
25005 static void
25006 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25007 {
25008 	int	be_chatty;
25009 	int	perr;
25010 
25011 	ASSERT(pkt != NULL);
25012 	ASSERT(un != NULL);
25013 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25014 	perr		= (pkt->pkt_statistics & STAT_PERR);
25015 
25016 	mutex_enter(SD_MUTEX(un));
25017 	if (un->un_state == SD_STATE_DUMPING) {
25018 		mutex_exit(SD_MUTEX(un));
25019 		return;
25020 	}
25021 
25022 	switch (pkt->pkt_reason) {
25023 	case CMD_UNX_BUS_FREE:
25024 		/*
25025 		 * If we had a parity error that caused the target to drop BSY*,
25026 		 * don't be chatty about it.
25027 		 */
25028 		if (perr && be_chatty) {
25029 			be_chatty = 0;
25030 		}
25031 		break;
25032 	case CMD_TAG_REJECT:
25033 		/*
25034 		 * The SCSI-2 spec states that a tag reject will be sent by the
25035 		 * target if tagged queuing is not supported. A tag reject may
25036 		 * also be sent during certain initialization periods or to
25037 		 * control internal resources. For the latter case the target
25038 		 * may also return Queue Full.
25039 		 *
25040 		 * If this driver receives a tag reject from a target that is
25041 		 * going through an init period or controlling internal
25042 		 * resources tagged queuing will be disabled. This is a less
25043 		 * than optimal behavior but the driver is unable to determine
25044 		 * the target state and assumes tagged queueing is not supported
25045 		 */
25046 		pkt->pkt_flags = 0;
25047 		un->un_tagflags = 0;
25048 
25049 		if (un->un_f_opt_queueing == TRUE) {
25050 			un->un_throttle = min(un->un_throttle, 3);
25051 		} else {
25052 			un->un_throttle = 1;
25053 		}
25054 		mutex_exit(SD_MUTEX(un));
25055 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25056 		mutex_enter(SD_MUTEX(un));
25057 		break;
25058 	case CMD_INCOMPLETE:
25059 		/*
25060 		 * The transport stopped with an abnormal state, fallthrough and
25061 		 * reset the target and/or bus unless selection did not complete
25062 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25063 		 * go through a target/bus reset
25064 		 */
25065 		if (pkt->pkt_state == STATE_GOT_BUS) {
25066 			break;
25067 		}
25068 		/*FALLTHROUGH*/
25069 
25070 	case CMD_TIMEOUT:
25071 	default:
25072 		/*
25073 		 * The lun may still be running the command, so a lun reset
25074 		 * should be attempted. If the lun reset fails or cannot be
25075 		 * issued, than try a target reset. Lastly try a bus reset.
25076 		 */
25077 		if ((pkt->pkt_statistics &
25078 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25079 			int reset_retval = 0;
25080 			mutex_exit(SD_MUTEX(un));
25081 			if (un->un_f_allow_bus_device_reset == TRUE) {
25082 				if (un->un_f_lun_reset_enabled == TRUE) {
25083 					reset_retval =
25084 					    scsi_reset(SD_ADDRESS(un),
25085 					    RESET_LUN);
25086 				}
25087 				if (reset_retval == 0) {
25088 					reset_retval =
25089 					    scsi_reset(SD_ADDRESS(un),
25090 					    RESET_TARGET);
25091 				}
25092 			}
25093 			if (reset_retval == 0) {
25094 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25095 			}
25096 			mutex_enter(SD_MUTEX(un));
25097 		}
25098 		break;
25099 	}
25100 
25101 	/* A device/bus reset has occurred; update the reservation status. */
25102 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25103 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25104 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25105 			un->un_resvd_status |=
25106 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25107 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25108 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25109 		}
25110 	}
25111 
25112 	/*
25113 	 * The disk has been turned off; Update the device state.
25114 	 *
25115 	 * Note: Should we be offlining the disk here?
25116 	 */
25117 	if (pkt->pkt_state == STATE_GOT_BUS) {
25118 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25119 		    "Disk not responding to selection\n");
25120 		if (un->un_state != SD_STATE_OFFLINE) {
25121 			New_state(un, SD_STATE_OFFLINE);
25122 		}
25123 	} else if (be_chatty) {
25124 		/*
25125 		 * suppress messages if they are all the same pkt reason;
25126 		 * with TQ, many (up to 256) are returned with the same
25127 		 * pkt_reason
25128 		 */
25129 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25130 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25131 			    "sd_mhd_watch_incomplete: "
25132 			    "SCSI transport failed: reason '%s'\n",
25133 			    scsi_rname(pkt->pkt_reason));
25134 		}
25135 	}
25136 	un->un_last_pkt_reason = pkt->pkt_reason;
25137 	mutex_exit(SD_MUTEX(un));
25138 }
25139 
25140 
25141 /*
25142  *    Function: sd_sname()
25143  *
25144  * Description: This is a simple little routine to return a string containing
25145  *		a printable description of command status byte for use in
25146  *		logging.
25147  *
25148  *   Arguments: status - pointer to a status byte
25149  *
25150  * Return Code: char * - string containing status description.
25151  */
25152 
25153 static char *
25154 sd_sname(uchar_t status)
25155 {
25156 	switch (status & STATUS_MASK) {
25157 	case STATUS_GOOD:
25158 		return ("good status");
25159 	case STATUS_CHECK:
25160 		return ("check condition");
25161 	case STATUS_MET:
25162 		return ("condition met");
25163 	case STATUS_BUSY:
25164 		return ("busy");
25165 	case STATUS_INTERMEDIATE:
25166 		return ("intermediate");
25167 	case STATUS_INTERMEDIATE_MET:
25168 		return ("intermediate - condition met");
25169 	case STATUS_RESERVATION_CONFLICT:
25170 		return ("reservation_conflict");
25171 	case STATUS_TERMINATED:
25172 		return ("command terminated");
25173 	case STATUS_QFULL:
25174 		return ("queue full");
25175 	default:
25176 		return ("<unknown status>");
25177 	}
25178 }
25179 
25180 
25181 /*
25182  *    Function: sd_mhd_resvd_recover()
25183  *
25184  * Description: This function adds a reservation entry to the
25185  *		sd_resv_reclaim_request list and signals the reservation
25186  *		reclaim thread that there is work pending. If the reservation
25187  *		reclaim thread has not been previously created this function
25188  *		will kick it off.
25189  *
25190  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25191  *			among multiple watches that share this callback function
25192  *
25193  *     Context: This routine is called by timeout() and is run in interrupt
25194  *		context. It must not sleep or call other functions which may
25195  *		sleep.
25196  */
25197 
25198 static void
25199 sd_mhd_resvd_recover(void *arg)
25200 {
25201 	dev_t			dev = (dev_t)arg;
25202 	struct sd_lun		*un;
25203 	struct sd_thr_request	*sd_treq = NULL;
25204 	struct sd_thr_request	*sd_cur = NULL;
25205 	struct sd_thr_request	*sd_prev = NULL;
25206 	int			already_there = 0;
25207 
25208 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25209 		return;
25210 	}
25211 
25212 	mutex_enter(SD_MUTEX(un));
25213 	un->un_resvd_timeid = NULL;
25214 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25215 		/*
25216 		 * There was a reset so don't issue the reserve, allow the
25217 		 * sd_mhd_watch_cb callback function to notice this and
25218 		 * reschedule the timeout for reservation.
25219 		 */
25220 		mutex_exit(SD_MUTEX(un));
25221 		return;
25222 	}
25223 	mutex_exit(SD_MUTEX(un));
25224 
25225 	/*
25226 	 * Add this device to the sd_resv_reclaim_request list and the
25227 	 * sd_resv_reclaim_thread should take care of the rest.
25228 	 *
25229 	 * Note: We can't sleep in this context so if the memory allocation
25230 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25231 	 * reschedule the timeout for reservation.  (4378460)
25232 	 */
25233 	sd_treq = (struct sd_thr_request *)
25234 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25235 	if (sd_treq == NULL) {
25236 		return;
25237 	}
25238 
25239 	sd_treq->sd_thr_req_next = NULL;
25240 	sd_treq->dev = dev;
25241 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25242 	if (sd_tr.srq_thr_req_head == NULL) {
25243 		sd_tr.srq_thr_req_head = sd_treq;
25244 	} else {
25245 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25246 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25247 			if (sd_cur->dev == dev) {
25248 				/*
25249 				 * already in Queue so don't log
25250 				 * another request for the device
25251 				 */
25252 				already_there = 1;
25253 				break;
25254 			}
25255 			sd_prev = sd_cur;
25256 		}
25257 		if (!already_there) {
25258 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25259 			    "logging request for %lx\n", dev);
25260 			sd_prev->sd_thr_req_next = sd_treq;
25261 		} else {
25262 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25263 		}
25264 	}
25265 
25266 	/*
25267 	 * Create a kernel thread to do the reservation reclaim and free up this
25268 	 * thread. We cannot block this thread while we go away to do the
25269 	 * reservation reclaim
25270 	 */
25271 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25272 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25273 		    sd_resv_reclaim_thread, NULL,
25274 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25275 
25276 	/* Tell the reservation reclaim thread that it has work to do */
25277 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25278 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25279 }
25280 
25281 /*
25282  *    Function: sd_resv_reclaim_thread()
25283  *
25284  * Description: This function implements the reservation reclaim operations
25285  *
25286  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25287  *		      among multiple watches that share this callback function
25288  */
25289 
25290 static void
25291 sd_resv_reclaim_thread()
25292 {
25293 	struct sd_lun		*un;
25294 	struct sd_thr_request	*sd_mhreq;
25295 
25296 	/* Wait for work */
25297 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25298 	if (sd_tr.srq_thr_req_head == NULL) {
25299 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25300 		    &sd_tr.srq_resv_reclaim_mutex);
25301 	}
25302 
25303 	/* Loop while we have work */
25304 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25305 		un = ddi_get_soft_state(sd_state,
25306 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25307 		if (un == NULL) {
25308 			/*
25309 			 * softstate structure is NULL so just
25310 			 * dequeue the request and continue
25311 			 */
25312 			sd_tr.srq_thr_req_head =
25313 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25314 			kmem_free(sd_tr.srq_thr_cur_req,
25315 			    sizeof (struct sd_thr_request));
25316 			continue;
25317 		}
25318 
25319 		/* dequeue the request */
25320 		sd_mhreq = sd_tr.srq_thr_cur_req;
25321 		sd_tr.srq_thr_req_head =
25322 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25323 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25324 
25325 		/*
25326 		 * Reclaim reservation only if SD_RESERVE is still set. There
25327 		 * may have been a call to MHIOCRELEASE before we got here.
25328 		 */
25329 		mutex_enter(SD_MUTEX(un));
25330 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25331 			/*
25332 			 * Note: The SD_LOST_RESERVE flag is cleared before
25333 			 * reclaiming the reservation. If this is done after the
25334 			 * call to sd_reserve_release a reservation loss in the
25335 			 * window between pkt completion of reserve cmd and
25336 			 * mutex_enter below may not be recognized
25337 			 */
25338 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25339 			mutex_exit(SD_MUTEX(un));
25340 
25341 			if (sd_reserve_release(sd_mhreq->dev,
25342 			    SD_RESERVE) == 0) {
25343 				mutex_enter(SD_MUTEX(un));
25344 				un->un_resvd_status |= SD_RESERVE;
25345 				mutex_exit(SD_MUTEX(un));
25346 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25347 				    "sd_resv_reclaim_thread: "
25348 				    "Reservation Recovered\n");
25349 			} else {
25350 				mutex_enter(SD_MUTEX(un));
25351 				un->un_resvd_status |= SD_LOST_RESERVE;
25352 				mutex_exit(SD_MUTEX(un));
25353 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25354 				    "sd_resv_reclaim_thread: Failed "
25355 				    "Reservation Recovery\n");
25356 			}
25357 		} else {
25358 			mutex_exit(SD_MUTEX(un));
25359 		}
25360 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25361 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25362 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25363 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25364 		/*
25365 		 * wakeup the destroy thread if anyone is waiting on
25366 		 * us to complete.
25367 		 */
25368 		cv_signal(&sd_tr.srq_inprocess_cv);
25369 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25370 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25371 	}
25372 
25373 	/*
25374 	 * cleanup the sd_tr structure now that this thread will not exist
25375 	 */
25376 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25377 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25378 	sd_tr.srq_resv_reclaim_thread = NULL;
25379 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25380 	thread_exit();
25381 }
25382 
25383 
25384 /*
25385  *    Function: sd_rmv_resv_reclaim_req()
25386  *
25387  * Description: This function removes any pending reservation reclaim requests
25388  *		for the specified device.
25389  *
25390  *   Arguments: dev - the device 'dev_t'
25391  */
25392 
25393 static void
25394 sd_rmv_resv_reclaim_req(dev_t dev)
25395 {
25396 	struct sd_thr_request *sd_mhreq;
25397 	struct sd_thr_request *sd_prev;
25398 
25399 	/* Remove a reservation reclaim request from the list */
25400 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25401 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25402 		/*
25403 		 * We are attempting to reinstate reservation for
25404 		 * this device. We wait for sd_reserve_release()
25405 		 * to return before we return.
25406 		 */
25407 		cv_wait(&sd_tr.srq_inprocess_cv,
25408 		    &sd_tr.srq_resv_reclaim_mutex);
25409 	} else {
25410 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25411 		if (sd_mhreq && sd_mhreq->dev == dev) {
25412 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25413 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25414 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25415 			return;
25416 		}
25417 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25418 			if (sd_mhreq && sd_mhreq->dev == dev) {
25419 				break;
25420 			}
25421 			sd_prev = sd_mhreq;
25422 		}
25423 		if (sd_mhreq != NULL) {
25424 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25425 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25426 		}
25427 	}
25428 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25429 }
25430 
25431 
25432 /*
25433  *    Function: sd_mhd_reset_notify_cb()
25434  *
25435  * Description: This is a call back function for scsi_reset_notify. This
25436  *		function updates the softstate reserved status and logs the
25437  *		reset. The driver scsi watch facility callback function
25438  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25439  *		will reclaim the reservation.
25440  *
25441  *   Arguments: arg  - driver soft state (unit) structure
25442  */
25443 
25444 static void
25445 sd_mhd_reset_notify_cb(caddr_t arg)
25446 {
25447 	struct sd_lun *un = (struct sd_lun *)arg;
25448 
25449 	mutex_enter(SD_MUTEX(un));
25450 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25451 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25452 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25453 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25454 	}
25455 	mutex_exit(SD_MUTEX(un));
25456 }
25457 
25458 
25459 /*
25460  *    Function: sd_take_ownership()
25461  *
25462  * Description: This routine implements an algorithm to achieve a stable
25463  *		reservation on disks which don't implement priority reserve,
25464  *		and makes sure that other host lose re-reservation attempts.
25465  *		This algorithm contains of a loop that keeps issuing the RESERVE
25466  *		for some period of time (min_ownership_delay, default 6 seconds)
25467  *		During that loop, it looks to see if there has been a bus device
25468  *		reset or bus reset (both of which cause an existing reservation
25469  *		to be lost). If the reservation is lost issue RESERVE until a
25470  *		period of min_ownership_delay with no resets has gone by, or
25471  *		until max_ownership_delay has expired. This loop ensures that
25472  *		the host really did manage to reserve the device, in spite of
25473  *		resets. The looping for min_ownership_delay (default six
25474  *		seconds) is important to early generation clustering products,
25475  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25476  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25477  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25478  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25479  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25480  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25481  *		no longer "owns" the disk and will have panicked itself.  Thus,
25482  *		the host issuing the MHIOCTKOWN is assured (with timing
25483  *		dependencies) that by the time it actually starts to use the
25484  *		disk for real work, the old owner is no longer accessing it.
25485  *
25486  *		min_ownership_delay is the minimum amount of time for which the
25487  *		disk must be reserved continuously devoid of resets before the
25488  *		MHIOCTKOWN ioctl will return success.
25489  *
25490  *		max_ownership_delay indicates the amount of time by which the
25491  *		take ownership should succeed or timeout with an error.
25492  *
25493  *   Arguments: dev - the device 'dev_t'
25494  *		*p  - struct containing timing info.
25495  *
25496  * Return Code: 0 for success or error code
25497  */
25498 
25499 static int
25500 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25501 {
25502 	struct sd_lun	*un;
25503 	int		rval;
25504 	int		err;
25505 	int		reservation_count   = 0;
25506 	int		min_ownership_delay =  6000000; /* in usec */
25507 	int		max_ownership_delay = 30000000; /* in usec */
25508 	clock_t		start_time;	/* starting time of this algorithm */
25509 	clock_t		end_time;	/* time limit for giving up */
25510 	clock_t		ownership_time;	/* time limit for stable ownership */
25511 	clock_t		current_time;
25512 	clock_t		previous_current_time;
25513 
25514 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25515 		return (ENXIO);
25516 	}
25517 
25518 	/*
25519 	 * Attempt a device reservation. A priority reservation is requested.
25520 	 */
25521 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25522 	    != SD_SUCCESS) {
25523 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25524 		    "sd_take_ownership: return(1)=%d\n", rval);
25525 		return (rval);
25526 	}
25527 
25528 	/* Update the softstate reserved status to indicate the reservation */
25529 	mutex_enter(SD_MUTEX(un));
25530 	un->un_resvd_status |= SD_RESERVE;
25531 	un->un_resvd_status &=
25532 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25533 	mutex_exit(SD_MUTEX(un));
25534 
25535 	if (p != NULL) {
25536 		if (p->min_ownership_delay != 0) {
25537 			min_ownership_delay = p->min_ownership_delay * 1000;
25538 		}
25539 		if (p->max_ownership_delay != 0) {
25540 			max_ownership_delay = p->max_ownership_delay * 1000;
25541 		}
25542 	}
25543 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25544 	    "sd_take_ownership: min, max delays: %d, %d\n",
25545 	    min_ownership_delay, max_ownership_delay);
25546 
25547 	start_time = ddi_get_lbolt();
25548 	current_time	= start_time;
25549 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25550 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25551 
25552 	while (current_time - end_time < 0) {
25553 		delay(drv_usectohz(500000));
25554 
25555 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25556 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25557 				mutex_enter(SD_MUTEX(un));
25558 				rval = (un->un_resvd_status &
25559 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25560 				mutex_exit(SD_MUTEX(un));
25561 				break;
25562 			}
25563 		}
25564 		previous_current_time = current_time;
25565 		current_time = ddi_get_lbolt();
25566 		mutex_enter(SD_MUTEX(un));
25567 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25568 			ownership_time = ddi_get_lbolt() +
25569 			    drv_usectohz(min_ownership_delay);
25570 			reservation_count = 0;
25571 		} else {
25572 			reservation_count++;
25573 		}
25574 		un->un_resvd_status |= SD_RESERVE;
25575 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25576 		mutex_exit(SD_MUTEX(un));
25577 
25578 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25579 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25580 		    "reservation=%s\n", (current_time - previous_current_time),
25581 		    reservation_count ? "ok" : "reclaimed");
25582 
25583 		if (current_time - ownership_time >= 0 &&
25584 		    reservation_count >= 4) {
25585 			rval = 0; /* Achieved a stable ownership */
25586 			break;
25587 		}
25588 		if (current_time - end_time >= 0) {
25589 			rval = EACCES; /* No ownership in max possible time */
25590 			break;
25591 		}
25592 	}
25593 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25594 	    "sd_take_ownership: return(2)=%d\n", rval);
25595 	return (rval);
25596 }
25597 
25598 
25599 /*
25600  *    Function: sd_reserve_release()
25601  *
25602  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25603  *		PRIORITY RESERVE commands based on a user specified command type
25604  *
25605  *   Arguments: dev - the device 'dev_t'
25606  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25607  *		      SD_RESERVE, SD_RELEASE
25608  *
25609  * Return Code: 0 or Error Code
25610  */
25611 
25612 static int
25613 sd_reserve_release(dev_t dev, int cmd)
25614 {
25615 	struct uscsi_cmd	*com = NULL;
25616 	struct sd_lun		*un = NULL;
25617 	char			cdb[CDB_GROUP0];
25618 	int			rval;
25619 
25620 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25621 	    (cmd == SD_PRIORITY_RESERVE));
25622 
25623 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25624 		return (ENXIO);
25625 	}
25626 
25627 	/* instantiate and initialize the command and cdb */
25628 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25629 	bzero(cdb, CDB_GROUP0);
25630 	com->uscsi_flags   = USCSI_SILENT;
25631 	com->uscsi_timeout = un->un_reserve_release_time;
25632 	com->uscsi_cdblen  = CDB_GROUP0;
25633 	com->uscsi_cdb	   = cdb;
25634 	if (cmd == SD_RELEASE) {
25635 		cdb[0] = SCMD_RELEASE;
25636 	} else {
25637 		cdb[0] = SCMD_RESERVE;
25638 	}
25639 
25640 	/* Send the command. */
25641 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25642 	    SD_PATH_STANDARD);
25643 
25644 	/*
25645 	 * "break" a reservation that is held by another host, by issuing a
25646 	 * reset if priority reserve is desired, and we could not get the
25647 	 * device.
25648 	 */
25649 	if ((cmd == SD_PRIORITY_RESERVE) &&
25650 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25651 		/*
25652 		 * First try to reset the LUN. If we cannot, then try a target
25653 		 * reset, followed by a bus reset if the target reset fails.
25654 		 */
25655 		int reset_retval = 0;
25656 		if (un->un_f_lun_reset_enabled == TRUE) {
25657 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25658 		}
25659 		if (reset_retval == 0) {
25660 			/* The LUN reset either failed or was not issued */
25661 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25662 		}
25663 		if ((reset_retval == 0) &&
25664 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25665 			rval = EIO;
25666 			kmem_free(com, sizeof (*com));
25667 			return (rval);
25668 		}
25669 
25670 		bzero(com, sizeof (struct uscsi_cmd));
25671 		com->uscsi_flags   = USCSI_SILENT;
25672 		com->uscsi_cdb	   = cdb;
25673 		com->uscsi_cdblen  = CDB_GROUP0;
25674 		com->uscsi_timeout = 5;
25675 
25676 		/*
25677 		 * Reissue the last reserve command, this time without request
25678 		 * sense.  Assume that it is just a regular reserve command.
25679 		 */
25680 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25681 		    SD_PATH_STANDARD);
25682 	}
25683 
25684 	/* Return an error if still getting a reservation conflict. */
25685 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25686 		rval = EACCES;
25687 	}
25688 
25689 	kmem_free(com, sizeof (*com));
25690 	return (rval);
25691 }
25692 
25693 
25694 #define	SD_NDUMP_RETRIES	12
25695 /*
25696  *	System Crash Dump routine
25697  */
25698 
25699 static int
25700 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25701 {
25702 	int		instance;
25703 	int		partition;
25704 	int		i;
25705 	int		err;
25706 	struct sd_lun	*un;
25707 	struct scsi_pkt *wr_pktp;
25708 	struct buf	*wr_bp;
25709 	struct buf	wr_buf;
25710 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25711 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25712 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25713 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25714 	size_t		io_start_offset;
25715 	int		doing_rmw = FALSE;
25716 	int		rval;
25717 	ssize_t		dma_resid;
25718 	daddr_t		oblkno;
25719 	diskaddr_t	nblks = 0;
25720 	diskaddr_t	start_block;
25721 
25722 	instance = SDUNIT(dev);
25723 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25724 	    !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25725 		return (ENXIO);
25726 	}
25727 
25728 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25729 
25730 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25731 
25732 	partition = SDPART(dev);
25733 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25734 
25735 	if (!(NOT_DEVBSIZE(un))) {
25736 		int secmask = 0;
25737 		int blknomask = 0;
25738 
25739 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25740 		secmask = un->un_tgt_blocksize - 1;
25741 
25742 		if (blkno & blknomask) {
25743 			SD_TRACE(SD_LOG_DUMP, un,
25744 			    "sddump: dump start block not modulo %d\n",
25745 			    un->un_tgt_blocksize);
25746 			return (EINVAL);
25747 		}
25748 
25749 		if ((nblk * DEV_BSIZE) & secmask) {
25750 			SD_TRACE(SD_LOG_DUMP, un,
25751 			    "sddump: dump length not modulo %d\n",
25752 			    un->un_tgt_blocksize);
25753 			return (EINVAL);
25754 		}
25755 
25756 	}
25757 
25758 	/* Validate blocks to dump at against partition size. */
25759 
25760 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
25761 	    &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25762 
25763 	if (NOT_DEVBSIZE(un)) {
25764 		if ((blkno + nblk) > nblks) {
25765 			SD_TRACE(SD_LOG_DUMP, un,
25766 			    "sddump: dump range larger than partition: "
25767 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25768 			    blkno, nblk, nblks);
25769 			return (EINVAL);
25770 		}
25771 	} else {
25772 		if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25773 		    (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25774 			SD_TRACE(SD_LOG_DUMP, un,
25775 			    "sddump: dump range larger than partition: "
25776 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25777 			    blkno, nblk, nblks);
25778 			return (EINVAL);
25779 		}
25780 	}
25781 
25782 	mutex_enter(&un->un_pm_mutex);
25783 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25784 		struct scsi_pkt *start_pktp;
25785 
25786 		mutex_exit(&un->un_pm_mutex);
25787 
25788 		/*
25789 		 * use pm framework to power on HBA 1st
25790 		 */
25791 		(void) pm_raise_power(SD_DEVINFO(un), 0,
25792 		    SD_PM_STATE_ACTIVE(un));
25793 
25794 		/*
25795 		 * Dump no long uses sdpower to power on a device, it's
25796 		 * in-line here so it can be done in polled mode.
25797 		 */
25798 
25799 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25800 
25801 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25802 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25803 
25804 		if (start_pktp == NULL) {
25805 			/* We were not given a SCSI packet, fail. */
25806 			return (EIO);
25807 		}
25808 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25809 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25810 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25811 		start_pktp->pkt_flags = FLAG_NOINTR;
25812 
25813 		mutex_enter(SD_MUTEX(un));
25814 		SD_FILL_SCSI1_LUN(un, start_pktp);
25815 		mutex_exit(SD_MUTEX(un));
25816 		/*
25817 		 * Scsi_poll returns 0 (success) if the command completes and
25818 		 * the status block is STATUS_GOOD.
25819 		 */
25820 		if (sd_scsi_poll(un, start_pktp) != 0) {
25821 			scsi_destroy_pkt(start_pktp);
25822 			return (EIO);
25823 		}
25824 		scsi_destroy_pkt(start_pktp);
25825 		(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25826 		    SD_PM_STATE_CHANGE);
25827 	} else {
25828 		mutex_exit(&un->un_pm_mutex);
25829 	}
25830 
25831 	mutex_enter(SD_MUTEX(un));
25832 	un->un_throttle = 0;
25833 
25834 	/*
25835 	 * The first time through, reset the specific target device.
25836 	 * However, when cpr calls sddump we know that sd is in a
25837 	 * a good state so no bus reset is required.
25838 	 * Clear sense data via Request Sense cmd.
25839 	 * In sddump we don't care about allow_bus_device_reset anymore
25840 	 */
25841 
25842 	if ((un->un_state != SD_STATE_SUSPENDED) &&
25843 	    (un->un_state != SD_STATE_DUMPING)) {
25844 
25845 		New_state(un, SD_STATE_DUMPING);
25846 
25847 		if (un->un_f_is_fibre == FALSE) {
25848 			mutex_exit(SD_MUTEX(un));
25849 			/*
25850 			 * Attempt a bus reset for parallel scsi.
25851 			 *
25852 			 * Note: A bus reset is required because on some host
25853 			 * systems (i.e. E420R) a bus device reset is
25854 			 * insufficient to reset the state of the target.
25855 			 *
25856 			 * Note: Don't issue the reset for fibre-channel,
25857 			 * because this tends to hang the bus (loop) for
25858 			 * too long while everyone is logging out and in
25859 			 * and the deadman timer for dumping will fire
25860 			 * before the dump is complete.
25861 			 */
25862 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25863 				mutex_enter(SD_MUTEX(un));
25864 				Restore_state(un);
25865 				mutex_exit(SD_MUTEX(un));
25866 				return (EIO);
25867 			}
25868 
25869 			/* Delay to give the device some recovery time. */
25870 			drv_usecwait(10000);
25871 
25872 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
25873 				SD_INFO(SD_LOG_DUMP, un,
25874 				    "sddump: sd_send_polled_RQS failed\n");
25875 			}
25876 			mutex_enter(SD_MUTEX(un));
25877 		}
25878 	}
25879 
25880 	/*
25881 	 * Convert the partition-relative block number to a
25882 	 * disk physical block number.
25883 	 */
25884 	if (NOT_DEVBSIZE(un)) {
25885 		blkno += start_block;
25886 	} else {
25887 		blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25888 		blkno += start_block;
25889 	}
25890 
25891 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25892 
25893 
25894 	/*
25895 	 * Check if the device has a non-512 block size.
25896 	 */
25897 	wr_bp = NULL;
25898 	if (NOT_DEVBSIZE(un)) {
25899 		tgt_byte_offset = blkno * un->un_sys_blocksize;
25900 		tgt_byte_count = nblk * un->un_sys_blocksize;
25901 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25902 		    (tgt_byte_count % un->un_tgt_blocksize)) {
25903 			doing_rmw = TRUE;
25904 			/*
25905 			 * Calculate the block number and number of block
25906 			 * in terms of the media block size.
25907 			 */
25908 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25909 			tgt_nblk =
25910 			    ((tgt_byte_offset + tgt_byte_count +
25911 			    (un->un_tgt_blocksize - 1)) /
25912 			    un->un_tgt_blocksize) - tgt_blkno;
25913 
25914 			/*
25915 			 * Invoke the routine which is going to do read part
25916 			 * of read-modify-write.
25917 			 * Note that this routine returns a pointer to
25918 			 * a valid bp in wr_bp.
25919 			 */
25920 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25921 			    &wr_bp);
25922 			if (err) {
25923 				mutex_exit(SD_MUTEX(un));
25924 				return (err);
25925 			}
25926 			/*
25927 			 * Offset is being calculated as -
25928 			 * (original block # * system block size) -
25929 			 * (new block # * target block size)
25930 			 */
25931 			io_start_offset =
25932 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
25933 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25934 
25935 			ASSERT((io_start_offset >= 0) &&
25936 			    (io_start_offset < un->un_tgt_blocksize));
25937 			/*
25938 			 * Do the modify portion of read modify write.
25939 			 */
25940 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25941 			    (size_t)nblk * un->un_sys_blocksize);
25942 		} else {
25943 			doing_rmw = FALSE;
25944 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25945 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25946 		}
25947 
25948 		/* Convert blkno and nblk to target blocks */
25949 		blkno = tgt_blkno;
25950 		nblk = tgt_nblk;
25951 	} else {
25952 		wr_bp = &wr_buf;
25953 		bzero(wr_bp, sizeof (struct buf));
25954 		wr_bp->b_flags		= B_BUSY;
25955 		wr_bp->b_un.b_addr	= addr;
25956 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
25957 		wr_bp->b_resid		= 0;
25958 	}
25959 
25960 	mutex_exit(SD_MUTEX(un));
25961 
25962 	/*
25963 	 * Obtain a SCSI packet for the write command.
25964 	 * It should be safe to call the allocator here without
25965 	 * worrying about being locked for DVMA mapping because
25966 	 * the address we're passed is already a DVMA mapping
25967 	 *
25968 	 * We are also not going to worry about semaphore ownership
25969 	 * in the dump buffer. Dumping is single threaded at present.
25970 	 */
25971 
25972 	wr_pktp = NULL;
25973 
25974 	dma_resid = wr_bp->b_bcount;
25975 	oblkno = blkno;
25976 
25977 	if (!(NOT_DEVBSIZE(un))) {
25978 		nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
25979 	}
25980 
25981 	while (dma_resid != 0) {
25982 
25983 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25984 		wr_bp->b_flags &= ~B_ERROR;
25985 
25986 		if (un->un_partial_dma_supported == 1) {
25987 			blkno = oblkno +
25988 			    ((wr_bp->b_bcount - dma_resid) /
25989 			    un->un_tgt_blocksize);
25990 			nblk = dma_resid / un->un_tgt_blocksize;
25991 
25992 			if (wr_pktp) {
25993 				/*
25994 				 * Partial DMA transfers after initial transfer
25995 				 */
25996 				rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
25997 				    blkno, nblk);
25998 			} else {
25999 				/* Initial transfer */
26000 				rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26001 				    un->un_pkt_flags, NULL_FUNC, NULL,
26002 				    blkno, nblk);
26003 			}
26004 		} else {
26005 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26006 			    0, NULL_FUNC, NULL, blkno, nblk);
26007 		}
26008 
26009 		if (rval == 0) {
26010 			/* We were given a SCSI packet, continue. */
26011 			break;
26012 		}
26013 
26014 		if (i == 0) {
26015 			if (wr_bp->b_flags & B_ERROR) {
26016 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26017 				    "no resources for dumping; "
26018 				    "error code: 0x%x, retrying",
26019 				    geterror(wr_bp));
26020 			} else {
26021 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26022 				    "no resources for dumping; retrying");
26023 			}
26024 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26025 			if (wr_bp->b_flags & B_ERROR) {
26026 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26027 				    "no resources for dumping; error code: "
26028 				    "0x%x, retrying\n", geterror(wr_bp));
26029 			}
26030 		} else {
26031 			if (wr_bp->b_flags & B_ERROR) {
26032 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26033 				    "no resources for dumping; "
26034 				    "error code: 0x%x, retries failed, "
26035 				    "giving up.\n", geterror(wr_bp));
26036 			} else {
26037 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26038 				    "no resources for dumping; "
26039 				    "retries failed, giving up.\n");
26040 			}
26041 			mutex_enter(SD_MUTEX(un));
26042 			Restore_state(un);
26043 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26044 				mutex_exit(SD_MUTEX(un));
26045 				scsi_free_consistent_buf(wr_bp);
26046 			} else {
26047 				mutex_exit(SD_MUTEX(un));
26048 			}
26049 			return (EIO);
26050 		}
26051 		drv_usecwait(10000);
26052 	}
26053 
26054 	if (un->un_partial_dma_supported == 1) {
26055 		/*
26056 		 * save the resid from PARTIAL_DMA
26057 		 */
26058 		dma_resid = wr_pktp->pkt_resid;
26059 		if (dma_resid != 0)
26060 			nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26061 		wr_pktp->pkt_resid = 0;
26062 	} else {
26063 		dma_resid = 0;
26064 	}
26065 
26066 	/* SunBug 1222170 */
26067 	wr_pktp->pkt_flags = FLAG_NOINTR;
26068 
26069 	err = EIO;
26070 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26071 
26072 		/*
26073 		 * Scsi_poll returns 0 (success) if the command completes and
26074 		 * the status block is STATUS_GOOD.  We should only check
26075 		 * errors if this condition is not true.  Even then we should
26076 		 * send our own request sense packet only if we have a check
26077 		 * condition and auto request sense has not been performed by
26078 		 * the hba.
26079 		 */
26080 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26081 
26082 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26083 		    (wr_pktp->pkt_resid == 0)) {
26084 			err = SD_SUCCESS;
26085 			break;
26086 		}
26087 
26088 		/*
26089 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26090 		 */
26091 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26092 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26093 			    "Error while dumping state...Device is gone\n");
26094 			break;
26095 		}
26096 
26097 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26098 			SD_INFO(SD_LOG_DUMP, un,
26099 			    "sddump: write failed with CHECK, try # %d\n", i);
26100 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26101 				(void) sd_send_polled_RQS(un);
26102 			}
26103 
26104 			continue;
26105 		}
26106 
26107 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26108 			int reset_retval = 0;
26109 
26110 			SD_INFO(SD_LOG_DUMP, un,
26111 			    "sddump: write failed with BUSY, try # %d\n", i);
26112 
26113 			if (un->un_f_lun_reset_enabled == TRUE) {
26114 				reset_retval = scsi_reset(SD_ADDRESS(un),
26115 				    RESET_LUN);
26116 			}
26117 			if (reset_retval == 0) {
26118 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26119 			}
26120 			(void) sd_send_polled_RQS(un);
26121 
26122 		} else {
26123 			SD_INFO(SD_LOG_DUMP, un,
26124 			    "sddump: write failed with 0x%x, try # %d\n",
26125 			    SD_GET_PKT_STATUS(wr_pktp), i);
26126 			mutex_enter(SD_MUTEX(un));
26127 			sd_reset_target(un, wr_pktp);
26128 			mutex_exit(SD_MUTEX(un));
26129 		}
26130 
26131 		/*
26132 		 * If we are not getting anywhere with lun/target resets,
26133 		 * let's reset the bus.
26134 		 */
26135 		if (i == SD_NDUMP_RETRIES/2) {
26136 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26137 			(void) sd_send_polled_RQS(un);
26138 		}
26139 	}
26140 	}
26141 
26142 	scsi_destroy_pkt(wr_pktp);
26143 	mutex_enter(SD_MUTEX(un));
26144 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26145 		mutex_exit(SD_MUTEX(un));
26146 		scsi_free_consistent_buf(wr_bp);
26147 	} else {
26148 		mutex_exit(SD_MUTEX(un));
26149 	}
26150 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26151 	return (err);
26152 }
26153 
26154 /*
26155  *    Function: sd_scsi_poll()
26156  *
26157  * Description: This is a wrapper for the scsi_poll call.
26158  *
26159  *   Arguments: sd_lun - The unit structure
26160  *              scsi_pkt - The scsi packet being sent to the device.
26161  *
26162  * Return Code: 0 - Command completed successfully with good status
26163  *             -1 - Command failed.  This could indicate a check condition
26164  *                  or other status value requiring recovery action.
26165  *
26166  * NOTE: This code is only called off sddump().
26167  */
26168 
26169 static int
26170 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26171 {
26172 	int status;
26173 
26174 	ASSERT(un != NULL);
26175 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26176 	ASSERT(pktp != NULL);
26177 
26178 	status = SD_SUCCESS;
26179 
26180 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26181 		pktp->pkt_flags |= un->un_tagflags;
26182 		pktp->pkt_flags &= ~FLAG_NODISCON;
26183 	}
26184 
26185 	status = sd_ddi_scsi_poll(pktp);
26186 	/*
26187 	 * Scsi_poll returns 0 (success) if the command completes and the
26188 	 * status block is STATUS_GOOD.  We should only check errors if this
26189 	 * condition is not true.  Even then we should send our own request
26190 	 * sense packet only if we have a check condition and auto
26191 	 * request sense has not been performed by the hba.
26192 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26193 	 */
26194 	if ((status != SD_SUCCESS) &&
26195 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26196 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26197 	    (pktp->pkt_reason != CMD_DEV_GONE))
26198 		(void) sd_send_polled_RQS(un);
26199 
26200 	return (status);
26201 }
26202 
26203 /*
26204  *    Function: sd_send_polled_RQS()
26205  *
26206  * Description: This sends the request sense command to a device.
26207  *
26208  *   Arguments: sd_lun - The unit structure
26209  *
26210  * Return Code: 0 - Command completed successfully with good status
26211  *             -1 - Command failed.
26212  *
26213  */
26214 
26215 static int
26216 sd_send_polled_RQS(struct sd_lun *un)
26217 {
26218 	int	ret_val;
26219 	struct	scsi_pkt	*rqs_pktp;
26220 	struct	buf		*rqs_bp;
26221 
26222 	ASSERT(un != NULL);
26223 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26224 
26225 	ret_val = SD_SUCCESS;
26226 
26227 	rqs_pktp = un->un_rqs_pktp;
26228 	rqs_bp	 = un->un_rqs_bp;
26229 
26230 	mutex_enter(SD_MUTEX(un));
26231 
26232 	if (un->un_sense_isbusy) {
26233 		ret_val = SD_FAILURE;
26234 		mutex_exit(SD_MUTEX(un));
26235 		return (ret_val);
26236 	}
26237 
26238 	/*
26239 	 * If the request sense buffer (and packet) is not in use,
26240 	 * let's set the un_sense_isbusy and send our packet
26241 	 */
26242 	un->un_sense_isbusy 	= 1;
26243 	rqs_pktp->pkt_resid  	= 0;
26244 	rqs_pktp->pkt_reason 	= 0;
26245 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26246 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26247 
26248 	mutex_exit(SD_MUTEX(un));
26249 
26250 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26251 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26252 
26253 	/*
26254 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26255 	 * axle - it has a call into us!
26256 	 */
26257 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26258 		SD_INFO(SD_LOG_COMMON, un,
26259 		    "sd_send_polled_RQS: RQS failed\n");
26260 	}
26261 
26262 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26263 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26264 
26265 	mutex_enter(SD_MUTEX(un));
26266 	un->un_sense_isbusy = 0;
26267 	mutex_exit(SD_MUTEX(un));
26268 
26269 	return (ret_val);
26270 }
26271 
26272 /*
26273  * Defines needed for localized version of the scsi_poll routine.
26274  */
26275 #define	CSEC		10000			/* usecs */
26276 #define	SEC_TO_CSEC	(1000000/CSEC)
26277 
26278 /*
26279  *    Function: sd_ddi_scsi_poll()
26280  *
26281  * Description: Localized version of the scsi_poll routine.  The purpose is to
26282  *		send a scsi_pkt to a device as a polled command.  This version
26283  *		is to ensure more robust handling of transport errors.
26284  *		Specifically this routine cures not ready, coming ready
26285  *		transition for power up and reset of sonoma's.  This can take
26286  *		up to 45 seconds for power-on and 20 seconds for reset of a
26287  * 		sonoma lun.
26288  *
26289  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26290  *
26291  * Return Code: 0 - Command completed successfully with good status
26292  *             -1 - Command failed.
26293  *
26294  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26295  * be fixed (removing this code), we need to determine how to handle the
26296  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26297  *
26298  * NOTE: This code is only called off sddump().
26299  */
26300 static int
26301 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26302 {
26303 	int			rval = -1;
26304 	int			savef;
26305 	long			savet;
26306 	void			(*savec)();
26307 	int			timeout;
26308 	int			busy_count;
26309 	int			poll_delay;
26310 	int			rc;
26311 	uint8_t			*sensep;
26312 	struct scsi_arq_status	*arqstat;
26313 	extern int		do_polled_io;
26314 
26315 	ASSERT(pkt->pkt_scbp);
26316 
26317 	/*
26318 	 * save old flags..
26319 	 */
26320 	savef = pkt->pkt_flags;
26321 	savec = pkt->pkt_comp;
26322 	savet = pkt->pkt_time;
26323 
26324 	pkt->pkt_flags |= FLAG_NOINTR;
26325 
26326 	/*
26327 	 * XXX there is nothing in the SCSA spec that states that we should not
26328 	 * do a callback for polled cmds; however, removing this will break sd
26329 	 * and probably other target drivers
26330 	 */
26331 	pkt->pkt_comp = NULL;
26332 
26333 	/*
26334 	 * we don't like a polled command without timeout.
26335 	 * 60 seconds seems long enough.
26336 	 */
26337 	if (pkt->pkt_time == 0)
26338 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26339 
26340 	/*
26341 	 * Send polled cmd.
26342 	 *
26343 	 * We do some error recovery for various errors.  Tran_busy,
26344 	 * queue full, and non-dispatched commands are retried every 10 msec.
26345 	 * as they are typically transient failures.  Busy status and Not
26346 	 * Ready are retried every second as this status takes a while to
26347 	 * change.
26348 	 */
26349 	timeout = pkt->pkt_time * SEC_TO_CSEC;
26350 
26351 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26352 		/*
26353 		 * Initialize pkt status variables.
26354 		 */
26355 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26356 
26357 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26358 			if (rc != TRAN_BUSY) {
26359 				/* Transport failed - give up. */
26360 				break;
26361 			} else {
26362 				/* Transport busy - try again. */
26363 				poll_delay = 1 * CSEC;		/* 10 msec. */
26364 			}
26365 		} else {
26366 			/*
26367 			 * Transport accepted - check pkt status.
26368 			 */
26369 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26370 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26371 			    (rc == STATUS_CHECK) &&
26372 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
26373 				arqstat =
26374 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26375 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26376 			} else {
26377 				sensep = NULL;
26378 			}
26379 
26380 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26381 			    (rc == STATUS_GOOD)) {
26382 				/* No error - we're done */
26383 				rval = 0;
26384 				break;
26385 
26386 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26387 				/* Lost connection - give up */
26388 				break;
26389 
26390 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26391 			    (pkt->pkt_state == 0)) {
26392 				/* Pkt not dispatched - try again. */
26393 				poll_delay = 1 * CSEC;		/* 10 msec. */
26394 
26395 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26396 			    (rc == STATUS_QFULL)) {
26397 				/* Queue full - try again. */
26398 				poll_delay = 1 * CSEC;		/* 10 msec. */
26399 
26400 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26401 			    (rc == STATUS_BUSY)) {
26402 				/* Busy - try again. */
26403 				poll_delay = 100 * CSEC;	/* 1 sec. */
26404 				busy_count += (SEC_TO_CSEC - 1);
26405 
26406 			} else if ((sensep != NULL) &&
26407 			    (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26408 				/*
26409 				 * Unit Attention - try again.
26410 				 * Pretend it took 1 sec.
26411 				 * NOTE: 'continue' avoids poll_delay
26412 				 */
26413 				busy_count += (SEC_TO_CSEC - 1);
26414 				continue;
26415 
26416 			} else if ((sensep != NULL) &&
26417 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26418 			    (scsi_sense_asc(sensep) == 0x04) &&
26419 			    (scsi_sense_ascq(sensep) == 0x01)) {
26420 				/*
26421 				 * Not ready -> ready - try again.
26422 				 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26423 				 * ...same as STATUS_BUSY
26424 				 */
26425 				poll_delay = 100 * CSEC;	/* 1 sec. */
26426 				busy_count += (SEC_TO_CSEC - 1);
26427 
26428 			} else {
26429 				/* BAD status - give up. */
26430 				break;
26431 			}
26432 		}
26433 
26434 		if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26435 		    !do_polled_io) {
26436 			delay(drv_usectohz(poll_delay));
26437 		} else {
26438 			/* we busy wait during cpr_dump or interrupt threads */
26439 			drv_usecwait(poll_delay);
26440 		}
26441 	}
26442 
26443 	pkt->pkt_flags = savef;
26444 	pkt->pkt_comp = savec;
26445 	pkt->pkt_time = savet;
26446 
26447 	/* return on error */
26448 	if (rval)
26449 		return (rval);
26450 
26451 	/*
26452 	 * This is not a performance critical code path.
26453 	 *
26454 	 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26455 	 * issues associated with looking at DMA memory prior to
26456 	 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26457 	 */
26458 	scsi_sync_pkt(pkt);
26459 	return (0);
26460 }
26461 
26462 
26463 
26464 /*
26465  *    Function: sd_persistent_reservation_in_read_keys
26466  *
26467  * Description: This routine is the driver entry point for handling CD-ROM
26468  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26469  *		by sending the SCSI-3 PRIN commands to the device.
26470  *		Processes the read keys command response by copying the
26471  *		reservation key information into the user provided buffer.
26472  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26473  *
26474  *   Arguments: un   -  Pointer to soft state struct for the target.
26475  *		usrp -	user provided pointer to multihost Persistent In Read
26476  *			Keys structure (mhioc_inkeys_t)
26477  *		flag -	this argument is a pass through to ddi_copyxxx()
26478  *			directly from the mode argument of ioctl().
26479  *
26480  * Return Code: 0   - Success
26481  *		EACCES
26482  *		ENOTSUP
26483  *		errno return code from sd_send_scsi_cmd()
26484  *
26485  *     Context: Can sleep. Does not return until command is completed.
26486  */
26487 
26488 static int
26489 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26490     mhioc_inkeys_t *usrp, int flag)
26491 {
26492 #ifdef _MULTI_DATAMODEL
26493 	struct mhioc_key_list32	li32;
26494 #endif
26495 	sd_prin_readkeys_t	*in;
26496 	mhioc_inkeys_t		*ptr;
26497 	mhioc_key_list_t	li;
26498 	uchar_t			*data_bufp;
26499 	int 			data_len;
26500 	int			rval = 0;
26501 	size_t			copysz;
26502 	sd_ssc_t		*ssc;
26503 
26504 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26505 		return (EINVAL);
26506 	}
26507 	bzero(&li, sizeof (mhioc_key_list_t));
26508 
26509 	ssc = sd_ssc_init(un);
26510 
26511 	/*
26512 	 * Get the listsize from user
26513 	 */
26514 #ifdef _MULTI_DATAMODEL
26515 
26516 	switch (ddi_model_convert_from(flag & FMODELS)) {
26517 	case DDI_MODEL_ILP32:
26518 		copysz = sizeof (struct mhioc_key_list32);
26519 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26520 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26521 			    "sd_persistent_reservation_in_read_keys: "
26522 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26523 			rval = EFAULT;
26524 			goto done;
26525 		}
26526 		li.listsize = li32.listsize;
26527 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26528 		break;
26529 
26530 	case DDI_MODEL_NONE:
26531 		copysz = sizeof (mhioc_key_list_t);
26532 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26533 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26534 			    "sd_persistent_reservation_in_read_keys: "
26535 			    "failed ddi_copyin: mhioc_key_list_t\n");
26536 			rval = EFAULT;
26537 			goto done;
26538 		}
26539 		break;
26540 	}
26541 
26542 #else /* ! _MULTI_DATAMODEL */
26543 	copysz = sizeof (mhioc_key_list_t);
26544 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26545 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26546 		    "sd_persistent_reservation_in_read_keys: "
26547 		    "failed ddi_copyin: mhioc_key_list_t\n");
26548 		rval = EFAULT;
26549 		goto done;
26550 	}
26551 #endif
26552 
26553 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26554 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26555 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26556 
26557 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26558 	    data_len, data_bufp);
26559 	if (rval != 0) {
26560 		if (rval == EIO)
26561 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26562 		else
26563 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26564 		goto done;
26565 	}
26566 	in = (sd_prin_readkeys_t *)data_bufp;
26567 	ptr->generation = BE_32(in->generation);
26568 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26569 
26570 	/*
26571 	 * Return the min(listsize, listlen) keys
26572 	 */
26573 #ifdef _MULTI_DATAMODEL
26574 
26575 	switch (ddi_model_convert_from(flag & FMODELS)) {
26576 	case DDI_MODEL_ILP32:
26577 		li32.listlen = li.listlen;
26578 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26579 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26580 			    "sd_persistent_reservation_in_read_keys: "
26581 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26582 			rval = EFAULT;
26583 			goto done;
26584 		}
26585 		break;
26586 
26587 	case DDI_MODEL_NONE:
26588 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26589 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26590 			    "sd_persistent_reservation_in_read_keys: "
26591 			    "failed ddi_copyout: mhioc_key_list_t\n");
26592 			rval = EFAULT;
26593 			goto done;
26594 		}
26595 		break;
26596 	}
26597 
26598 #else /* ! _MULTI_DATAMODEL */
26599 
26600 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26601 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26602 		    "sd_persistent_reservation_in_read_keys: "
26603 		    "failed ddi_copyout: mhioc_key_list_t\n");
26604 		rval = EFAULT;
26605 		goto done;
26606 	}
26607 
26608 #endif /* _MULTI_DATAMODEL */
26609 
26610 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26611 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26612 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26613 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26614 		    "sd_persistent_reservation_in_read_keys: "
26615 		    "failed ddi_copyout: keylist\n");
26616 		rval = EFAULT;
26617 	}
26618 done:
26619 	sd_ssc_fini(ssc);
26620 	kmem_free(data_bufp, data_len);
26621 	return (rval);
26622 }
26623 
26624 
26625 /*
26626  *    Function: sd_persistent_reservation_in_read_resv
26627  *
26628  * Description: This routine is the driver entry point for handling CD-ROM
26629  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26630  *		by sending the SCSI-3 PRIN commands to the device.
26631  *		Process the read persistent reservations command response by
26632  *		copying the reservation information into the user provided
26633  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26634  *
26635  *   Arguments: un   -  Pointer to soft state struct for the target.
26636  *		usrp -	user provided pointer to multihost Persistent In Read
26637  *			Keys structure (mhioc_inkeys_t)
26638  *		flag -	this argument is a pass through to ddi_copyxxx()
26639  *			directly from the mode argument of ioctl().
26640  *
26641  * Return Code: 0   - Success
26642  *		EACCES
26643  *		ENOTSUP
26644  *		errno return code from sd_send_scsi_cmd()
26645  *
26646  *     Context: Can sleep. Does not return until command is completed.
26647  */
26648 
26649 static int
26650 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26651     mhioc_inresvs_t *usrp, int flag)
26652 {
26653 #ifdef _MULTI_DATAMODEL
26654 	struct mhioc_resv_desc_list32 resvlist32;
26655 #endif
26656 	sd_prin_readresv_t	*in;
26657 	mhioc_inresvs_t		*ptr;
26658 	sd_readresv_desc_t	*readresv_ptr;
26659 	mhioc_resv_desc_list_t	resvlist;
26660 	mhioc_resv_desc_t 	resvdesc;
26661 	uchar_t			*data_bufp = NULL;
26662 	int 			data_len;
26663 	int			rval = 0;
26664 	int			i;
26665 	size_t			copysz;
26666 	mhioc_resv_desc_t	*bufp;
26667 	sd_ssc_t		*ssc;
26668 
26669 	if ((ptr = usrp) == NULL) {
26670 		return (EINVAL);
26671 	}
26672 
26673 	ssc = sd_ssc_init(un);
26674 
26675 	/*
26676 	 * Get the listsize from user
26677 	 */
26678 #ifdef _MULTI_DATAMODEL
26679 	switch (ddi_model_convert_from(flag & FMODELS)) {
26680 	case DDI_MODEL_ILP32:
26681 		copysz = sizeof (struct mhioc_resv_desc_list32);
26682 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26683 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26684 			    "sd_persistent_reservation_in_read_resv: "
26685 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26686 			rval = EFAULT;
26687 			goto done;
26688 		}
26689 		resvlist.listsize = resvlist32.listsize;
26690 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26691 		break;
26692 
26693 	case DDI_MODEL_NONE:
26694 		copysz = sizeof (mhioc_resv_desc_list_t);
26695 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26696 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26697 			    "sd_persistent_reservation_in_read_resv: "
26698 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26699 			rval = EFAULT;
26700 			goto done;
26701 		}
26702 		break;
26703 	}
26704 #else /* ! _MULTI_DATAMODEL */
26705 	copysz = sizeof (mhioc_resv_desc_list_t);
26706 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26707 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26708 		    "sd_persistent_reservation_in_read_resv: "
26709 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26710 		rval = EFAULT;
26711 		goto done;
26712 	}
26713 #endif /* ! _MULTI_DATAMODEL */
26714 
26715 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26716 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26717 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26718 
26719 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26720 	    data_len, data_bufp);
26721 	if (rval != 0) {
26722 		if (rval == EIO)
26723 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26724 		else
26725 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26726 		goto done;
26727 	}
26728 	in = (sd_prin_readresv_t *)data_bufp;
26729 	ptr->generation = BE_32(in->generation);
26730 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26731 
26732 	/*
26733 	 * Return the min(listsize, listlen( keys
26734 	 */
26735 #ifdef _MULTI_DATAMODEL
26736 
26737 	switch (ddi_model_convert_from(flag & FMODELS)) {
26738 	case DDI_MODEL_ILP32:
26739 		resvlist32.listlen = resvlist.listlen;
26740 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26741 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26742 			    "sd_persistent_reservation_in_read_resv: "
26743 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26744 			rval = EFAULT;
26745 			goto done;
26746 		}
26747 		break;
26748 
26749 	case DDI_MODEL_NONE:
26750 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26751 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26752 			    "sd_persistent_reservation_in_read_resv: "
26753 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26754 			rval = EFAULT;
26755 			goto done;
26756 		}
26757 		break;
26758 	}
26759 
26760 #else /* ! _MULTI_DATAMODEL */
26761 
26762 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26763 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26764 		    "sd_persistent_reservation_in_read_resv: "
26765 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26766 		rval = EFAULT;
26767 		goto done;
26768 	}
26769 
26770 #endif /* ! _MULTI_DATAMODEL */
26771 
26772 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26773 	bufp = resvlist.list;
26774 	copysz = sizeof (mhioc_resv_desc_t);
26775 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26776 	    i++, readresv_ptr++, bufp++) {
26777 
26778 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26779 		    MHIOC_RESV_KEY_SIZE);
26780 		resvdesc.type  = readresv_ptr->type;
26781 		resvdesc.scope = readresv_ptr->scope;
26782 		resvdesc.scope_specific_addr =
26783 		    BE_32(readresv_ptr->scope_specific_addr);
26784 
26785 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26786 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26787 			    "sd_persistent_reservation_in_read_resv: "
26788 			    "failed ddi_copyout: resvlist\n");
26789 			rval = EFAULT;
26790 			goto done;
26791 		}
26792 	}
26793 done:
26794 	sd_ssc_fini(ssc);
26795 	/* only if data_bufp is allocated, we need to free it */
26796 	if (data_bufp) {
26797 		kmem_free(data_bufp, data_len);
26798 	}
26799 	return (rval);
26800 }
26801 
26802 
26803 /*
26804  *    Function: sr_change_blkmode()
26805  *
26806  * Description: This routine is the driver entry point for handling CD-ROM
26807  *		block mode ioctl requests. Support for returning and changing
26808  *		the current block size in use by the device is implemented. The
26809  *		LBA size is changed via a MODE SELECT Block Descriptor.
26810  *
26811  *		This routine issues a mode sense with an allocation length of
26812  *		12 bytes for the mode page header and a single block descriptor.
26813  *
26814  *   Arguments: dev - the device 'dev_t'
26815  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26816  *		      CDROMSBLKMODE (set)
26817  *		data - current block size or requested block size
26818  *		flag - this argument is a pass through to ddi_copyxxx() directly
26819  *		       from the mode argument of ioctl().
26820  *
26821  * Return Code: the code returned by sd_send_scsi_cmd()
26822  *		EINVAL if invalid arguments are provided
26823  *		EFAULT if ddi_copyxxx() fails
26824  *		ENXIO if fail ddi_get_soft_state
26825  *		EIO if invalid mode sense block descriptor length
26826  *
26827  */
26828 
26829 static int
26830 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26831 {
26832 	struct sd_lun			*un = NULL;
26833 	struct mode_header		*sense_mhp, *select_mhp;
26834 	struct block_descriptor		*sense_desc, *select_desc;
26835 	int				current_bsize;
26836 	int				rval = EINVAL;
26837 	uchar_t				*sense = NULL;
26838 	uchar_t				*select = NULL;
26839 	sd_ssc_t			*ssc;
26840 
26841 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26842 
26843 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26844 		return (ENXIO);
26845 	}
26846 
26847 	/*
26848 	 * The block length is changed via the Mode Select block descriptor, the
26849 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26850 	 * required as part of this routine. Therefore the mode sense allocation
26851 	 * length is specified to be the length of a mode page header and a
26852 	 * block descriptor.
26853 	 */
26854 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26855 
26856 	ssc = sd_ssc_init(un);
26857 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26858 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26859 	sd_ssc_fini(ssc);
26860 	if (rval != 0) {
26861 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26862 		    "sr_change_blkmode: Mode Sense Failed\n");
26863 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26864 		return (rval);
26865 	}
26866 
26867 	/* Check the block descriptor len to handle only 1 block descriptor */
26868 	sense_mhp = (struct mode_header *)sense;
26869 	if ((sense_mhp->bdesc_length == 0) ||
26870 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26871 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26872 		    "sr_change_blkmode: Mode Sense returned invalid block"
26873 		    " descriptor length\n");
26874 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26875 		return (EIO);
26876 	}
26877 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26878 	current_bsize = ((sense_desc->blksize_hi << 16) |
26879 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26880 
26881 	/* Process command */
26882 	switch (cmd) {
26883 	case CDROMGBLKMODE:
26884 		/* Return the block size obtained during the mode sense */
26885 		if (ddi_copyout(&current_bsize, (void *)data,
26886 		    sizeof (int), flag) != 0)
26887 			rval = EFAULT;
26888 		break;
26889 	case CDROMSBLKMODE:
26890 		/* Validate the requested block size */
26891 		switch (data) {
26892 		case CDROM_BLK_512:
26893 		case CDROM_BLK_1024:
26894 		case CDROM_BLK_2048:
26895 		case CDROM_BLK_2056:
26896 		case CDROM_BLK_2336:
26897 		case CDROM_BLK_2340:
26898 		case CDROM_BLK_2352:
26899 		case CDROM_BLK_2368:
26900 		case CDROM_BLK_2448:
26901 		case CDROM_BLK_2646:
26902 		case CDROM_BLK_2647:
26903 			break;
26904 		default:
26905 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26906 			    "sr_change_blkmode: "
26907 			    "Block Size '%ld' Not Supported\n", data);
26908 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26909 			return (EINVAL);
26910 		}
26911 
26912 		/*
26913 		 * The current block size matches the requested block size so
26914 		 * there is no need to send the mode select to change the size
26915 		 */
26916 		if (current_bsize == data) {
26917 			break;
26918 		}
26919 
26920 		/* Build the select data for the requested block size */
26921 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26922 		select_mhp = (struct mode_header *)select;
26923 		select_desc =
26924 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26925 		/*
26926 		 * The LBA size is changed via the block descriptor, so the
26927 		 * descriptor is built according to the user data
26928 		 */
26929 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26930 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26931 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26932 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
26933 
26934 		/* Send the mode select for the requested block size */
26935 		ssc = sd_ssc_init(un);
26936 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26937 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26938 		    SD_PATH_STANDARD);
26939 		sd_ssc_fini(ssc);
26940 		if (rval != 0) {
26941 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26942 			    "sr_change_blkmode: Mode Select Failed\n");
26943 			/*
26944 			 * The mode select failed for the requested block size,
26945 			 * so reset the data for the original block size and
26946 			 * send it to the target. The error is indicated by the
26947 			 * return value for the failed mode select.
26948 			 */
26949 			select_desc->blksize_hi  = sense_desc->blksize_hi;
26950 			select_desc->blksize_mid = sense_desc->blksize_mid;
26951 			select_desc->blksize_lo  = sense_desc->blksize_lo;
26952 			ssc = sd_ssc_init(un);
26953 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
26954 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26955 			    SD_PATH_STANDARD);
26956 			sd_ssc_fini(ssc);
26957 		} else {
26958 			ASSERT(!mutex_owned(SD_MUTEX(un)));
26959 			mutex_enter(SD_MUTEX(un));
26960 			sd_update_block_info(un, (uint32_t)data, 0);
26961 			mutex_exit(SD_MUTEX(un));
26962 		}
26963 		break;
26964 	default:
26965 		/* should not reach here, but check anyway */
26966 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26967 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
26968 		rval = EINVAL;
26969 		break;
26970 	}
26971 
26972 	if (select) {
26973 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
26974 	}
26975 	if (sense) {
26976 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26977 	}
26978 	return (rval);
26979 }
26980 
26981 
26982 /*
26983  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
26984  * implement driver support for getting and setting the CD speed. The command
26985  * set used will be based on the device type. If the device has not been
26986  * identified as MMC the Toshiba vendor specific mode page will be used. If
26987  * the device is MMC but does not support the Real Time Streaming feature
26988  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
26989  * be used to read the speed.
26990  */
26991 
26992 /*
26993  *    Function: sr_change_speed()
26994  *
26995  * Description: This routine is the driver entry point for handling CD-ROM
26996  *		drive speed ioctl requests for devices supporting the Toshiba
26997  *		vendor specific drive speed mode page. Support for returning
26998  *		and changing the current drive speed in use by the device is
26999  *		implemented.
27000  *
27001  *   Arguments: dev - the device 'dev_t'
27002  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27003  *		      CDROMSDRVSPEED (set)
27004  *		data - current drive speed or requested drive speed
27005  *		flag - this argument is a pass through to ddi_copyxxx() directly
27006  *		       from the mode argument of ioctl().
27007  *
27008  * Return Code: the code returned by sd_send_scsi_cmd()
27009  *		EINVAL if invalid arguments are provided
27010  *		EFAULT if ddi_copyxxx() fails
27011  *		ENXIO if fail ddi_get_soft_state
27012  *		EIO if invalid mode sense block descriptor length
27013  */
27014 
27015 static int
27016 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27017 {
27018 	struct sd_lun			*un = NULL;
27019 	struct mode_header		*sense_mhp, *select_mhp;
27020 	struct mode_speed		*sense_page, *select_page;
27021 	int				current_speed;
27022 	int				rval = EINVAL;
27023 	int				bd_len;
27024 	uchar_t				*sense = NULL;
27025 	uchar_t				*select = NULL;
27026 	sd_ssc_t			*ssc;
27027 
27028 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27029 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27030 		return (ENXIO);
27031 	}
27032 
27033 	/*
27034 	 * Note: The drive speed is being modified here according to a Toshiba
27035 	 * vendor specific mode page (0x31).
27036 	 */
27037 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27038 
27039 	ssc = sd_ssc_init(un);
27040 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27041 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27042 	    SD_PATH_STANDARD);
27043 	sd_ssc_fini(ssc);
27044 	if (rval != 0) {
27045 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27046 		    "sr_change_speed: Mode Sense Failed\n");
27047 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27048 		return (rval);
27049 	}
27050 	sense_mhp  = (struct mode_header *)sense;
27051 
27052 	/* Check the block descriptor len to handle only 1 block descriptor */
27053 	bd_len = sense_mhp->bdesc_length;
27054 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27055 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27056 		    "sr_change_speed: Mode Sense returned invalid block "
27057 		    "descriptor length\n");
27058 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27059 		return (EIO);
27060 	}
27061 
27062 	sense_page = (struct mode_speed *)
27063 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27064 	current_speed = sense_page->speed;
27065 
27066 	/* Process command */
27067 	switch (cmd) {
27068 	case CDROMGDRVSPEED:
27069 		/* Return the drive speed obtained during the mode sense */
27070 		if (current_speed == 0x2) {
27071 			current_speed = CDROM_TWELVE_SPEED;
27072 		}
27073 		if (ddi_copyout(&current_speed, (void *)data,
27074 		    sizeof (int), flag) != 0) {
27075 			rval = EFAULT;
27076 		}
27077 		break;
27078 	case CDROMSDRVSPEED:
27079 		/* Validate the requested drive speed */
27080 		switch ((uchar_t)data) {
27081 		case CDROM_TWELVE_SPEED:
27082 			data = 0x2;
27083 			/*FALLTHROUGH*/
27084 		case CDROM_NORMAL_SPEED:
27085 		case CDROM_DOUBLE_SPEED:
27086 		case CDROM_QUAD_SPEED:
27087 		case CDROM_MAXIMUM_SPEED:
27088 			break;
27089 		default:
27090 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27091 			    "sr_change_speed: "
27092 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27093 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27094 			return (EINVAL);
27095 		}
27096 
27097 		/*
27098 		 * The current drive speed matches the requested drive speed so
27099 		 * there is no need to send the mode select to change the speed
27100 		 */
27101 		if (current_speed == data) {
27102 			break;
27103 		}
27104 
27105 		/* Build the select data for the requested drive speed */
27106 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27107 		select_mhp = (struct mode_header *)select;
27108 		select_mhp->bdesc_length = 0;
27109 		select_page =
27110 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27111 		select_page =
27112 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27113 		select_page->mode_page.code = CDROM_MODE_SPEED;
27114 		select_page->mode_page.length = 2;
27115 		select_page->speed = (uchar_t)data;
27116 
27117 		/* Send the mode select for the requested block size */
27118 		ssc = sd_ssc_init(un);
27119 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27120 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27121 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27122 		sd_ssc_fini(ssc);
27123 		if (rval != 0) {
27124 			/*
27125 			 * The mode select failed for the requested drive speed,
27126 			 * so reset the data for the original drive speed and
27127 			 * send it to the target. The error is indicated by the
27128 			 * return value for the failed mode select.
27129 			 */
27130 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27131 			    "sr_drive_speed: Mode Select Failed\n");
27132 			select_page->speed = sense_page->speed;
27133 			ssc = sd_ssc_init(un);
27134 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27135 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27136 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27137 			sd_ssc_fini(ssc);
27138 		}
27139 		break;
27140 	default:
27141 		/* should not reach here, but check anyway */
27142 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27143 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27144 		rval = EINVAL;
27145 		break;
27146 	}
27147 
27148 	if (select) {
27149 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27150 	}
27151 	if (sense) {
27152 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27153 	}
27154 
27155 	return (rval);
27156 }
27157 
27158 
27159 /*
27160  *    Function: sr_atapi_change_speed()
27161  *
27162  * Description: This routine is the driver entry point for handling CD-ROM
27163  *		drive speed ioctl requests for MMC devices that do not support
27164  *		the Real Time Streaming feature (0x107).
27165  *
27166  *		Note: This routine will use the SET SPEED command which may not
27167  *		be supported by all devices.
27168  *
27169  *   Arguments: dev- the device 'dev_t'
27170  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27171  *		     CDROMSDRVSPEED (set)
27172  *		data- current drive speed or requested drive speed
27173  *		flag- this argument is a pass through to ddi_copyxxx() directly
27174  *		      from the mode argument of ioctl().
27175  *
27176  * Return Code: the code returned by sd_send_scsi_cmd()
27177  *		EINVAL if invalid arguments are provided
27178  *		EFAULT if ddi_copyxxx() fails
27179  *		ENXIO if fail ddi_get_soft_state
27180  *		EIO if invalid mode sense block descriptor length
27181  */
27182 
27183 static int
27184 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27185 {
27186 	struct sd_lun			*un;
27187 	struct uscsi_cmd		*com = NULL;
27188 	struct mode_header_grp2		*sense_mhp;
27189 	uchar_t				*sense_page;
27190 	uchar_t				*sense = NULL;
27191 	char				cdb[CDB_GROUP5];
27192 	int				bd_len;
27193 	int				current_speed = 0;
27194 	int				max_speed = 0;
27195 	int				rval;
27196 	sd_ssc_t			*ssc;
27197 
27198 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27199 
27200 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27201 		return (ENXIO);
27202 	}
27203 
27204 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27205 
27206 	ssc = sd_ssc_init(un);
27207 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27208 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27209 	    SD_PATH_STANDARD);
27210 	sd_ssc_fini(ssc);
27211 	if (rval != 0) {
27212 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27213 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27214 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27215 		return (rval);
27216 	}
27217 
27218 	/* Check the block descriptor len to handle only 1 block descriptor */
27219 	sense_mhp = (struct mode_header_grp2 *)sense;
27220 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27221 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27222 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27223 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27224 		    "block descriptor length\n");
27225 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27226 		return (EIO);
27227 	}
27228 
27229 	/* Calculate the current and maximum drive speeds */
27230 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27231 	current_speed = (sense_page[14] << 8) | sense_page[15];
27232 	max_speed = (sense_page[8] << 8) | sense_page[9];
27233 
27234 	/* Process the command */
27235 	switch (cmd) {
27236 	case CDROMGDRVSPEED:
27237 		current_speed /= SD_SPEED_1X;
27238 		if (ddi_copyout(&current_speed, (void *)data,
27239 		    sizeof (int), flag) != 0)
27240 			rval = EFAULT;
27241 		break;
27242 	case CDROMSDRVSPEED:
27243 		/* Convert the speed code to KB/sec */
27244 		switch ((uchar_t)data) {
27245 		case CDROM_NORMAL_SPEED:
27246 			current_speed = SD_SPEED_1X;
27247 			break;
27248 		case CDROM_DOUBLE_SPEED:
27249 			current_speed = 2 * SD_SPEED_1X;
27250 			break;
27251 		case CDROM_QUAD_SPEED:
27252 			current_speed = 4 * SD_SPEED_1X;
27253 			break;
27254 		case CDROM_TWELVE_SPEED:
27255 			current_speed = 12 * SD_SPEED_1X;
27256 			break;
27257 		case CDROM_MAXIMUM_SPEED:
27258 			current_speed = 0xffff;
27259 			break;
27260 		default:
27261 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27262 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27263 			    (uchar_t)data);
27264 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27265 			return (EINVAL);
27266 		}
27267 
27268 		/* Check the request against the drive's max speed. */
27269 		if (current_speed != 0xffff) {
27270 			if (current_speed > max_speed) {
27271 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27272 				return (EINVAL);
27273 			}
27274 		}
27275 
27276 		/*
27277 		 * Build and send the SET SPEED command
27278 		 *
27279 		 * Note: The SET SPEED (0xBB) command used in this routine is
27280 		 * obsolete per the SCSI MMC spec but still supported in the
27281 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27282 		 * therefore the command is still implemented in this routine.
27283 		 */
27284 		bzero(cdb, sizeof (cdb));
27285 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27286 		cdb[2] = (uchar_t)(current_speed >> 8);
27287 		cdb[3] = (uchar_t)current_speed;
27288 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27289 		com->uscsi_cdb	   = (caddr_t)cdb;
27290 		com->uscsi_cdblen  = CDB_GROUP5;
27291 		com->uscsi_bufaddr = NULL;
27292 		com->uscsi_buflen  = 0;
27293 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27294 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27295 		break;
27296 	default:
27297 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27298 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27299 		rval = EINVAL;
27300 	}
27301 
27302 	if (sense) {
27303 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27304 	}
27305 	if (com) {
27306 		kmem_free(com, sizeof (*com));
27307 	}
27308 	return (rval);
27309 }
27310 
27311 
27312 /*
27313  *    Function: sr_pause_resume()
27314  *
27315  * Description: This routine is the driver entry point for handling CD-ROM
27316  *		pause/resume ioctl requests. This only affects the audio play
27317  *		operation.
27318  *
27319  *   Arguments: dev - the device 'dev_t'
27320  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27321  *		      for setting the resume bit of the cdb.
27322  *
27323  * Return Code: the code returned by sd_send_scsi_cmd()
27324  *		EINVAL if invalid mode specified
27325  *
27326  */
27327 
27328 static int
27329 sr_pause_resume(dev_t dev, int cmd)
27330 {
27331 	struct sd_lun		*un;
27332 	struct uscsi_cmd	*com;
27333 	char			cdb[CDB_GROUP1];
27334 	int			rval;
27335 
27336 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27337 		return (ENXIO);
27338 	}
27339 
27340 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27341 	bzero(cdb, CDB_GROUP1);
27342 	cdb[0] = SCMD_PAUSE_RESUME;
27343 	switch (cmd) {
27344 	case CDROMRESUME:
27345 		cdb[8] = 1;
27346 		break;
27347 	case CDROMPAUSE:
27348 		cdb[8] = 0;
27349 		break;
27350 	default:
27351 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27352 		    " Command '%x' Not Supported\n", cmd);
27353 		rval = EINVAL;
27354 		goto done;
27355 	}
27356 
27357 	com->uscsi_cdb    = cdb;
27358 	com->uscsi_cdblen = CDB_GROUP1;
27359 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27360 
27361 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27362 	    SD_PATH_STANDARD);
27363 
27364 done:
27365 	kmem_free(com, sizeof (*com));
27366 	return (rval);
27367 }
27368 
27369 
27370 /*
27371  *    Function: sr_play_msf()
27372  *
27373  * Description: This routine is the driver entry point for handling CD-ROM
27374  *		ioctl requests to output the audio signals at the specified
27375  *		starting address and continue the audio play until the specified
27376  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27377  *		Frame (MSF) format.
27378  *
27379  *   Arguments: dev	- the device 'dev_t'
27380  *		data	- pointer to user provided audio msf structure,
27381  *		          specifying start/end addresses.
27382  *		flag	- this argument is a pass through to ddi_copyxxx()
27383  *		          directly from the mode argument of ioctl().
27384  *
27385  * Return Code: the code returned by sd_send_scsi_cmd()
27386  *		EFAULT if ddi_copyxxx() fails
27387  *		ENXIO if fail ddi_get_soft_state
27388  *		EINVAL if data pointer is NULL
27389  */
27390 
27391 static int
27392 sr_play_msf(dev_t dev, caddr_t data, int flag)
27393 {
27394 	struct sd_lun		*un;
27395 	struct uscsi_cmd	*com;
27396 	struct cdrom_msf	msf_struct;
27397 	struct cdrom_msf	*msf = &msf_struct;
27398 	char			cdb[CDB_GROUP1];
27399 	int			rval;
27400 
27401 	if (data == NULL) {
27402 		return (EINVAL);
27403 	}
27404 
27405 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27406 		return (ENXIO);
27407 	}
27408 
27409 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27410 		return (EFAULT);
27411 	}
27412 
27413 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27414 	bzero(cdb, CDB_GROUP1);
27415 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27416 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27417 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27418 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27419 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27420 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27421 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27422 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27423 	} else {
27424 		cdb[3] = msf->cdmsf_min0;
27425 		cdb[4] = msf->cdmsf_sec0;
27426 		cdb[5] = msf->cdmsf_frame0;
27427 		cdb[6] = msf->cdmsf_min1;
27428 		cdb[7] = msf->cdmsf_sec1;
27429 		cdb[8] = msf->cdmsf_frame1;
27430 	}
27431 	com->uscsi_cdb    = cdb;
27432 	com->uscsi_cdblen = CDB_GROUP1;
27433 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27434 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27435 	    SD_PATH_STANDARD);
27436 	kmem_free(com, sizeof (*com));
27437 	return (rval);
27438 }
27439 
27440 
27441 /*
27442  *    Function: sr_play_trkind()
27443  *
27444  * Description: This routine is the driver entry point for handling CD-ROM
27445  *		ioctl requests to output the audio signals at the specified
27446  *		starting address and continue the audio play until the specified
27447  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27448  *		format.
27449  *
27450  *   Arguments: dev	- the device 'dev_t'
27451  *		data	- pointer to user provided audio track/index structure,
27452  *		          specifying start/end addresses.
27453  *		flag	- this argument is a pass through to ddi_copyxxx()
27454  *		          directly from the mode argument of ioctl().
27455  *
27456  * Return Code: the code returned by sd_send_scsi_cmd()
27457  *		EFAULT if ddi_copyxxx() fails
27458  *		ENXIO if fail ddi_get_soft_state
27459  *		EINVAL if data pointer is NULL
27460  */
27461 
27462 static int
27463 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27464 {
27465 	struct cdrom_ti		ti_struct;
27466 	struct cdrom_ti		*ti = &ti_struct;
27467 	struct uscsi_cmd	*com = NULL;
27468 	char			cdb[CDB_GROUP1];
27469 	int			rval;
27470 
27471 	if (data == NULL) {
27472 		return (EINVAL);
27473 	}
27474 
27475 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27476 		return (EFAULT);
27477 	}
27478 
27479 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27480 	bzero(cdb, CDB_GROUP1);
27481 	cdb[0] = SCMD_PLAYAUDIO_TI;
27482 	cdb[4] = ti->cdti_trk0;
27483 	cdb[5] = ti->cdti_ind0;
27484 	cdb[7] = ti->cdti_trk1;
27485 	cdb[8] = ti->cdti_ind1;
27486 	com->uscsi_cdb    = cdb;
27487 	com->uscsi_cdblen = CDB_GROUP1;
27488 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27489 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27490 	    SD_PATH_STANDARD);
27491 	kmem_free(com, sizeof (*com));
27492 	return (rval);
27493 }
27494 
27495 
27496 /*
27497  *    Function: sr_read_all_subcodes()
27498  *
27499  * Description: This routine is the driver entry point for handling CD-ROM
27500  *		ioctl requests to return raw subcode data while the target is
27501  *		playing audio (CDROMSUBCODE).
27502  *
27503  *   Arguments: dev	- the device 'dev_t'
27504  *		data	- pointer to user provided cdrom subcode structure,
27505  *		          specifying the transfer length and address.
27506  *		flag	- this argument is a pass through to ddi_copyxxx()
27507  *		          directly from the mode argument of ioctl().
27508  *
27509  * Return Code: the code returned by sd_send_scsi_cmd()
27510  *		EFAULT if ddi_copyxxx() fails
27511  *		ENXIO if fail ddi_get_soft_state
27512  *		EINVAL if data pointer is NULL
27513  */
27514 
27515 static int
27516 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27517 {
27518 	struct sd_lun		*un = NULL;
27519 	struct uscsi_cmd	*com = NULL;
27520 	struct cdrom_subcode	*subcode = NULL;
27521 	int			rval;
27522 	size_t			buflen;
27523 	char			cdb[CDB_GROUP5];
27524 
27525 #ifdef _MULTI_DATAMODEL
27526 	/* To support ILP32 applications in an LP64 world */
27527 	struct cdrom_subcode32		cdrom_subcode32;
27528 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27529 #endif
27530 	if (data == NULL) {
27531 		return (EINVAL);
27532 	}
27533 
27534 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27535 		return (ENXIO);
27536 	}
27537 
27538 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27539 
27540 #ifdef _MULTI_DATAMODEL
27541 	switch (ddi_model_convert_from(flag & FMODELS)) {
27542 	case DDI_MODEL_ILP32:
27543 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27544 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27545 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27546 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27547 			return (EFAULT);
27548 		}
27549 		/* Convert the ILP32 uscsi data from the application to LP64 */
27550 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27551 		break;
27552 	case DDI_MODEL_NONE:
27553 		if (ddi_copyin(data, subcode,
27554 		    sizeof (struct cdrom_subcode), flag)) {
27555 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27556 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27557 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27558 			return (EFAULT);
27559 		}
27560 		break;
27561 	}
27562 #else /* ! _MULTI_DATAMODEL */
27563 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27564 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27565 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27566 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27567 		return (EFAULT);
27568 	}
27569 #endif /* _MULTI_DATAMODEL */
27570 
27571 	/*
27572 	 * Since MMC-2 expects max 3 bytes for length, check if the
27573 	 * length input is greater than 3 bytes
27574 	 */
27575 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27576 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27577 		    "sr_read_all_subcodes: "
27578 		    "cdrom transfer length too large: %d (limit %d)\n",
27579 		    subcode->cdsc_length, 0xFFFFFF);
27580 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27581 		return (EINVAL);
27582 	}
27583 
27584 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27585 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27586 	bzero(cdb, CDB_GROUP5);
27587 
27588 	if (un->un_f_mmc_cap == TRUE) {
27589 		cdb[0] = (char)SCMD_READ_CD;
27590 		cdb[2] = (char)0xff;
27591 		cdb[3] = (char)0xff;
27592 		cdb[4] = (char)0xff;
27593 		cdb[5] = (char)0xff;
27594 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27595 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27596 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27597 		cdb[10] = 1;
27598 	} else {
27599 		/*
27600 		 * Note: A vendor specific command (0xDF) is being used her to
27601 		 * request a read of all subcodes.
27602 		 */
27603 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27604 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27605 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27606 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27607 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27608 	}
27609 	com->uscsi_cdb	   = cdb;
27610 	com->uscsi_cdblen  = CDB_GROUP5;
27611 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27612 	com->uscsi_buflen  = buflen;
27613 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27614 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27615 	    SD_PATH_STANDARD);
27616 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27617 	kmem_free(com, sizeof (*com));
27618 	return (rval);
27619 }
27620 
27621 
27622 /*
27623  *    Function: sr_read_subchannel()
27624  *
27625  * Description: This routine is the driver entry point for handling CD-ROM
27626  *		ioctl requests to return the Q sub-channel data of the CD
27627  *		current position block. (CDROMSUBCHNL) The data includes the
27628  *		track number, index number, absolute CD-ROM address (LBA or MSF
27629  *		format per the user) , track relative CD-ROM address (LBA or MSF
27630  *		format per the user), control data and audio status.
27631  *
27632  *   Arguments: dev	- the device 'dev_t'
27633  *		data	- pointer to user provided cdrom sub-channel structure
27634  *		flag	- this argument is a pass through to ddi_copyxxx()
27635  *		          directly from the mode argument of ioctl().
27636  *
27637  * Return Code: the code returned by sd_send_scsi_cmd()
27638  *		EFAULT if ddi_copyxxx() fails
27639  *		ENXIO if fail ddi_get_soft_state
27640  *		EINVAL if data pointer is NULL
27641  */
27642 
27643 static int
27644 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27645 {
27646 	struct sd_lun		*un;
27647 	struct uscsi_cmd	*com;
27648 	struct cdrom_subchnl	subchanel;
27649 	struct cdrom_subchnl	*subchnl = &subchanel;
27650 	char			cdb[CDB_GROUP1];
27651 	caddr_t			buffer;
27652 	int			rval;
27653 
27654 	if (data == NULL) {
27655 		return (EINVAL);
27656 	}
27657 
27658 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27659 	    (un->un_state == SD_STATE_OFFLINE)) {
27660 		return (ENXIO);
27661 	}
27662 
27663 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27664 		return (EFAULT);
27665 	}
27666 
27667 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27668 	bzero(cdb, CDB_GROUP1);
27669 	cdb[0] = SCMD_READ_SUBCHANNEL;
27670 	/* Set the MSF bit based on the user requested address format */
27671 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27672 	/*
27673 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27674 	 * returned
27675 	 */
27676 	cdb[2] = 0x40;
27677 	/*
27678 	 * Set byte 3 to specify the return data format. A value of 0x01
27679 	 * indicates that the CD-ROM current position should be returned.
27680 	 */
27681 	cdb[3] = 0x01;
27682 	cdb[8] = 0x10;
27683 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27684 	com->uscsi_cdb	   = cdb;
27685 	com->uscsi_cdblen  = CDB_GROUP1;
27686 	com->uscsi_bufaddr = buffer;
27687 	com->uscsi_buflen  = 16;
27688 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27689 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27690 	    SD_PATH_STANDARD);
27691 	if (rval != 0) {
27692 		kmem_free(buffer, 16);
27693 		kmem_free(com, sizeof (*com));
27694 		return (rval);
27695 	}
27696 
27697 	/* Process the returned Q sub-channel data */
27698 	subchnl->cdsc_audiostatus = buffer[1];
27699 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
27700 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27701 	subchnl->cdsc_trk	= buffer[6];
27702 	subchnl->cdsc_ind	= buffer[7];
27703 	if (subchnl->cdsc_format & CDROM_LBA) {
27704 		subchnl->cdsc_absaddr.lba =
27705 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27706 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27707 		subchnl->cdsc_reladdr.lba =
27708 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27709 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27710 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27711 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27712 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27713 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27714 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27715 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27716 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27717 	} else {
27718 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27719 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27720 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27721 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27722 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27723 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27724 	}
27725 	kmem_free(buffer, 16);
27726 	kmem_free(com, sizeof (*com));
27727 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27728 	    != 0) {
27729 		return (EFAULT);
27730 	}
27731 	return (rval);
27732 }
27733 
27734 
27735 /*
27736  *    Function: sr_read_tocentry()
27737  *
27738  * Description: This routine is the driver entry point for handling CD-ROM
27739  *		ioctl requests to read from the Table of Contents (TOC)
27740  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27741  *		fields, the starting address (LBA or MSF format per the user)
27742  *		and the data mode if the user specified track is a data track.
27743  *
27744  *		Note: The READ HEADER (0x44) command used in this routine is
27745  *		obsolete per the SCSI MMC spec but still supported in the
27746  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27747  *		therefore the command is still implemented in this routine.
27748  *
27749  *   Arguments: dev	- the device 'dev_t'
27750  *		data	- pointer to user provided toc entry structure,
27751  *			  specifying the track # and the address format
27752  *			  (LBA or MSF).
27753  *		flag	- this argument is a pass through to ddi_copyxxx()
27754  *		          directly from the mode argument of ioctl().
27755  *
27756  * Return Code: the code returned by sd_send_scsi_cmd()
27757  *		EFAULT if ddi_copyxxx() fails
27758  *		ENXIO if fail ddi_get_soft_state
27759  *		EINVAL if data pointer is NULL
27760  */
27761 
27762 static int
27763 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27764 {
27765 	struct sd_lun		*un = NULL;
27766 	struct uscsi_cmd	*com;
27767 	struct cdrom_tocentry	toc_entry;
27768 	struct cdrom_tocentry	*entry = &toc_entry;
27769 	caddr_t			buffer;
27770 	int			rval;
27771 	char			cdb[CDB_GROUP1];
27772 
27773 	if (data == NULL) {
27774 		return (EINVAL);
27775 	}
27776 
27777 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27778 	    (un->un_state == SD_STATE_OFFLINE)) {
27779 		return (ENXIO);
27780 	}
27781 
27782 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27783 		return (EFAULT);
27784 	}
27785 
27786 	/* Validate the requested track and address format */
27787 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27788 		return (EINVAL);
27789 	}
27790 
27791 	if (entry->cdte_track == 0) {
27792 		return (EINVAL);
27793 	}
27794 
27795 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27796 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27797 	bzero(cdb, CDB_GROUP1);
27798 
27799 	cdb[0] = SCMD_READ_TOC;
27800 	/* Set the MSF bit based on the user requested address format  */
27801 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27802 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27803 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27804 	} else {
27805 		cdb[6] = entry->cdte_track;
27806 	}
27807 
27808 	/*
27809 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27810 	 * (4 byte TOC response header + 8 byte track descriptor)
27811 	 */
27812 	cdb[8] = 12;
27813 	com->uscsi_cdb	   = cdb;
27814 	com->uscsi_cdblen  = CDB_GROUP1;
27815 	com->uscsi_bufaddr = buffer;
27816 	com->uscsi_buflen  = 0x0C;
27817 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27818 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27819 	    SD_PATH_STANDARD);
27820 	if (rval != 0) {
27821 		kmem_free(buffer, 12);
27822 		kmem_free(com, sizeof (*com));
27823 		return (rval);
27824 	}
27825 
27826 	/* Process the toc entry */
27827 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27828 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27829 	if (entry->cdte_format & CDROM_LBA) {
27830 		entry->cdte_addr.lba =
27831 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27832 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27833 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27834 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27835 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27836 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27837 		/*
27838 		 * Send a READ TOC command using the LBA address format to get
27839 		 * the LBA for the track requested so it can be used in the
27840 		 * READ HEADER request
27841 		 *
27842 		 * Note: The MSF bit of the READ HEADER command specifies the
27843 		 * output format. The block address specified in that command
27844 		 * must be in LBA format.
27845 		 */
27846 		cdb[1] = 0;
27847 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27848 		    SD_PATH_STANDARD);
27849 		if (rval != 0) {
27850 			kmem_free(buffer, 12);
27851 			kmem_free(com, sizeof (*com));
27852 			return (rval);
27853 		}
27854 	} else {
27855 		entry->cdte_addr.msf.minute	= buffer[9];
27856 		entry->cdte_addr.msf.second	= buffer[10];
27857 		entry->cdte_addr.msf.frame	= buffer[11];
27858 		/*
27859 		 * Send a READ TOC command using the LBA address format to get
27860 		 * the LBA for the track requested so it can be used in the
27861 		 * READ HEADER request
27862 		 *
27863 		 * Note: The MSF bit of the READ HEADER command specifies the
27864 		 * output format. The block address specified in that command
27865 		 * must be in LBA format.
27866 		 */
27867 		cdb[1] = 0;
27868 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27869 		    SD_PATH_STANDARD);
27870 		if (rval != 0) {
27871 			kmem_free(buffer, 12);
27872 			kmem_free(com, sizeof (*com));
27873 			return (rval);
27874 		}
27875 	}
27876 
27877 	/*
27878 	 * Build and send the READ HEADER command to determine the data mode of
27879 	 * the user specified track.
27880 	 */
27881 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27882 	    (entry->cdte_track != CDROM_LEADOUT)) {
27883 		bzero(cdb, CDB_GROUP1);
27884 		cdb[0] = SCMD_READ_HEADER;
27885 		cdb[2] = buffer[8];
27886 		cdb[3] = buffer[9];
27887 		cdb[4] = buffer[10];
27888 		cdb[5] = buffer[11];
27889 		cdb[8] = 0x08;
27890 		com->uscsi_buflen = 0x08;
27891 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27892 		    SD_PATH_STANDARD);
27893 		if (rval == 0) {
27894 			entry->cdte_datamode = buffer[0];
27895 		} else {
27896 			/*
27897 			 * READ HEADER command failed, since this is
27898 			 * obsoleted in one spec, its better to return
27899 			 * -1 for an invlid track so that we can still
27900 			 * receive the rest of the TOC data.
27901 			 */
27902 			entry->cdte_datamode = (uchar_t)-1;
27903 		}
27904 	} else {
27905 		entry->cdte_datamode = (uchar_t)-1;
27906 	}
27907 
27908 	kmem_free(buffer, 12);
27909 	kmem_free(com, sizeof (*com));
27910 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27911 		return (EFAULT);
27912 
27913 	return (rval);
27914 }
27915 
27916 
27917 /*
27918  *    Function: sr_read_tochdr()
27919  *
27920  * Description: This routine is the driver entry point for handling CD-ROM
27921  * 		ioctl requests to read the Table of Contents (TOC) header
27922  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
27923  *		and ending track numbers
27924  *
27925  *   Arguments: dev	- the device 'dev_t'
27926  *		data	- pointer to user provided toc header structure,
27927  *			  specifying the starting and ending track numbers.
27928  *		flag	- this argument is a pass through to ddi_copyxxx()
27929  *			  directly from the mode argument of ioctl().
27930  *
27931  * Return Code: the code returned by sd_send_scsi_cmd()
27932  *		EFAULT if ddi_copyxxx() fails
27933  *		ENXIO if fail ddi_get_soft_state
27934  *		EINVAL if data pointer is NULL
27935  */
27936 
27937 static int
27938 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27939 {
27940 	struct sd_lun		*un;
27941 	struct uscsi_cmd	*com;
27942 	struct cdrom_tochdr	toc_header;
27943 	struct cdrom_tochdr	*hdr = &toc_header;
27944 	char			cdb[CDB_GROUP1];
27945 	int			rval;
27946 	caddr_t			buffer;
27947 
27948 	if (data == NULL) {
27949 		return (EINVAL);
27950 	}
27951 
27952 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27953 	    (un->un_state == SD_STATE_OFFLINE)) {
27954 		return (ENXIO);
27955 	}
27956 
27957 	buffer = kmem_zalloc(4, KM_SLEEP);
27958 	bzero(cdb, CDB_GROUP1);
27959 	cdb[0] = SCMD_READ_TOC;
27960 	/*
27961 	 * Specifying a track number of 0x00 in the READ TOC command indicates
27962 	 * that the TOC header should be returned
27963 	 */
27964 	cdb[6] = 0x00;
27965 	/*
27966 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27967 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27968 	 */
27969 	cdb[8] = 0x04;
27970 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27971 	com->uscsi_cdb	   = cdb;
27972 	com->uscsi_cdblen  = CDB_GROUP1;
27973 	com->uscsi_bufaddr = buffer;
27974 	com->uscsi_buflen  = 0x04;
27975 	com->uscsi_timeout = 300;
27976 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27977 
27978 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27979 	    SD_PATH_STANDARD);
27980 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27981 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
27982 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
27983 	} else {
27984 		hdr->cdth_trk0 = buffer[2];
27985 		hdr->cdth_trk1 = buffer[3];
27986 	}
27987 	kmem_free(buffer, 4);
27988 	kmem_free(com, sizeof (*com));
27989 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
27990 		return (EFAULT);
27991 	}
27992 	return (rval);
27993 }
27994 
27995 
27996 /*
27997  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
27998  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
27999  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28000  * digital audio and extended architecture digital audio. These modes are
28001  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28002  * MMC specs.
28003  *
28004  * In addition to support for the various data formats these routines also
28005  * include support for devices that implement only the direct access READ
28006  * commands (0x08, 0x28), devices that implement the READ_CD commands
28007  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28008  * READ CDXA commands (0xD8, 0xDB)
28009  */
28010 
28011 /*
28012  *    Function: sr_read_mode1()
28013  *
28014  * Description: This routine is the driver entry point for handling CD-ROM
28015  *		ioctl read mode1 requests (CDROMREADMODE1).
28016  *
28017  *   Arguments: dev	- the device 'dev_t'
28018  *		data	- pointer to user provided cd read structure specifying
28019  *			  the lba buffer address and length.
28020  *		flag	- this argument is a pass through to ddi_copyxxx()
28021  *			  directly from the mode argument of ioctl().
28022  *
28023  * Return Code: the code returned by sd_send_scsi_cmd()
28024  *		EFAULT if ddi_copyxxx() fails
28025  *		ENXIO if fail ddi_get_soft_state
28026  *		EINVAL if data pointer is NULL
28027  */
28028 
28029 static int
28030 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28031 {
28032 	struct sd_lun		*un;
28033 	struct cdrom_read	mode1_struct;
28034 	struct cdrom_read	*mode1 = &mode1_struct;
28035 	int			rval;
28036 	sd_ssc_t		*ssc;
28037 
28038 #ifdef _MULTI_DATAMODEL
28039 	/* To support ILP32 applications in an LP64 world */
28040 	struct cdrom_read32	cdrom_read32;
28041 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28042 #endif /* _MULTI_DATAMODEL */
28043 
28044 	if (data == NULL) {
28045 		return (EINVAL);
28046 	}
28047 
28048 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28049 	    (un->un_state == SD_STATE_OFFLINE)) {
28050 		return (ENXIO);
28051 	}
28052 
28053 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28054 	    "sd_read_mode1: entry: un:0x%p\n", un);
28055 
28056 #ifdef _MULTI_DATAMODEL
28057 	switch (ddi_model_convert_from(flag & FMODELS)) {
28058 	case DDI_MODEL_ILP32:
28059 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28060 			return (EFAULT);
28061 		}
28062 		/* Convert the ILP32 uscsi data from the application to LP64 */
28063 		cdrom_read32tocdrom_read(cdrd32, mode1);
28064 		break;
28065 	case DDI_MODEL_NONE:
28066 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28067 			return (EFAULT);
28068 		}
28069 	}
28070 #else /* ! _MULTI_DATAMODEL */
28071 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28072 		return (EFAULT);
28073 	}
28074 #endif /* _MULTI_DATAMODEL */
28075 
28076 	ssc = sd_ssc_init(un);
28077 	rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28078 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28079 	sd_ssc_fini(ssc);
28080 
28081 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28082 	    "sd_read_mode1: exit: un:0x%p\n", un);
28083 
28084 	return (rval);
28085 }
28086 
28087 
28088 /*
28089  *    Function: sr_read_cd_mode2()
28090  *
28091  * Description: This routine is the driver entry point for handling CD-ROM
28092  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28093  *		support the READ CD (0xBE) command or the 1st generation
28094  *		READ CD (0xD4) command.
28095  *
28096  *   Arguments: dev	- the device 'dev_t'
28097  *		data	- pointer to user provided cd read structure specifying
28098  *			  the lba buffer address and length.
28099  *		flag	- this argument is a pass through to ddi_copyxxx()
28100  *			  directly from the mode argument of ioctl().
28101  *
28102  * Return Code: the code returned by sd_send_scsi_cmd()
28103  *		EFAULT if ddi_copyxxx() fails
28104  *		ENXIO if fail ddi_get_soft_state
28105  *		EINVAL if data pointer is NULL
28106  */
28107 
28108 static int
28109 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28110 {
28111 	struct sd_lun		*un;
28112 	struct uscsi_cmd	*com;
28113 	struct cdrom_read	mode2_struct;
28114 	struct cdrom_read	*mode2 = &mode2_struct;
28115 	uchar_t			cdb[CDB_GROUP5];
28116 	int			nblocks;
28117 	int			rval;
28118 #ifdef _MULTI_DATAMODEL
28119 	/*  To support ILP32 applications in an LP64 world */
28120 	struct cdrom_read32	cdrom_read32;
28121 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28122 #endif /* _MULTI_DATAMODEL */
28123 
28124 	if (data == NULL) {
28125 		return (EINVAL);
28126 	}
28127 
28128 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28129 	    (un->un_state == SD_STATE_OFFLINE)) {
28130 		return (ENXIO);
28131 	}
28132 
28133 #ifdef _MULTI_DATAMODEL
28134 	switch (ddi_model_convert_from(flag & FMODELS)) {
28135 	case DDI_MODEL_ILP32:
28136 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28137 			return (EFAULT);
28138 		}
28139 		/* Convert the ILP32 uscsi data from the application to LP64 */
28140 		cdrom_read32tocdrom_read(cdrd32, mode2);
28141 		break;
28142 	case DDI_MODEL_NONE:
28143 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28144 			return (EFAULT);
28145 		}
28146 		break;
28147 	}
28148 
28149 #else /* ! _MULTI_DATAMODEL */
28150 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28151 		return (EFAULT);
28152 	}
28153 #endif /* _MULTI_DATAMODEL */
28154 
28155 	bzero(cdb, sizeof (cdb));
28156 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28157 		/* Read command supported by 1st generation atapi drives */
28158 		cdb[0] = SCMD_READ_CDD4;
28159 	} else {
28160 		/* Universal CD Access Command */
28161 		cdb[0] = SCMD_READ_CD;
28162 	}
28163 
28164 	/*
28165 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28166 	 */
28167 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28168 
28169 	/* set the start address */
28170 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28171 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28172 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28173 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28174 
28175 	/* set the transfer length */
28176 	nblocks = mode2->cdread_buflen / 2336;
28177 	cdb[6] = (uchar_t)(nblocks >> 16);
28178 	cdb[7] = (uchar_t)(nblocks >> 8);
28179 	cdb[8] = (uchar_t)nblocks;
28180 
28181 	/* set the filter bits */
28182 	cdb[9] = CDROM_READ_CD_USERDATA;
28183 
28184 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28185 	com->uscsi_cdb = (caddr_t)cdb;
28186 	com->uscsi_cdblen = sizeof (cdb);
28187 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28188 	com->uscsi_buflen = mode2->cdread_buflen;
28189 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28190 
28191 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28192 	    SD_PATH_STANDARD);
28193 	kmem_free(com, sizeof (*com));
28194 	return (rval);
28195 }
28196 
28197 
28198 /*
28199  *    Function: sr_read_mode2()
28200  *
28201  * Description: This routine is the driver entry point for handling CD-ROM
28202  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28203  *		do not support the READ CD (0xBE) command.
28204  *
28205  *   Arguments: dev	- the device 'dev_t'
28206  *		data	- pointer to user provided cd read structure specifying
28207  *			  the lba buffer address and length.
28208  *		flag	- this argument is a pass through to ddi_copyxxx()
28209  *			  directly from the mode argument of ioctl().
28210  *
28211  * Return Code: the code returned by sd_send_scsi_cmd()
28212  *		EFAULT if ddi_copyxxx() fails
28213  *		ENXIO if fail ddi_get_soft_state
28214  *		EINVAL if data pointer is NULL
28215  *		EIO if fail to reset block size
28216  *		EAGAIN if commands are in progress in the driver
28217  */
28218 
28219 static int
28220 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28221 {
28222 	struct sd_lun		*un;
28223 	struct cdrom_read	mode2_struct;
28224 	struct cdrom_read	*mode2 = &mode2_struct;
28225 	int			rval;
28226 	uint32_t		restore_blksize;
28227 	struct uscsi_cmd	*com;
28228 	uchar_t			cdb[CDB_GROUP0];
28229 	int			nblocks;
28230 
28231 #ifdef _MULTI_DATAMODEL
28232 	/* To support ILP32 applications in an LP64 world */
28233 	struct cdrom_read32	cdrom_read32;
28234 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28235 #endif /* _MULTI_DATAMODEL */
28236 
28237 	if (data == NULL) {
28238 		return (EINVAL);
28239 	}
28240 
28241 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28242 	    (un->un_state == SD_STATE_OFFLINE)) {
28243 		return (ENXIO);
28244 	}
28245 
28246 	/*
28247 	 * Because this routine will update the device and driver block size
28248 	 * being used we want to make sure there are no commands in progress.
28249 	 * If commands are in progress the user will have to try again.
28250 	 *
28251 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28252 	 * in sdioctl to protect commands from sdioctl through to the top of
28253 	 * sd_uscsi_strategy. See sdioctl for details.
28254 	 */
28255 	mutex_enter(SD_MUTEX(un));
28256 	if (un->un_ncmds_in_driver != 1) {
28257 		mutex_exit(SD_MUTEX(un));
28258 		return (EAGAIN);
28259 	}
28260 	mutex_exit(SD_MUTEX(un));
28261 
28262 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28263 	    "sd_read_mode2: entry: un:0x%p\n", un);
28264 
28265 #ifdef _MULTI_DATAMODEL
28266 	switch (ddi_model_convert_from(flag & FMODELS)) {
28267 	case DDI_MODEL_ILP32:
28268 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28269 			return (EFAULT);
28270 		}
28271 		/* Convert the ILP32 uscsi data from the application to LP64 */
28272 		cdrom_read32tocdrom_read(cdrd32, mode2);
28273 		break;
28274 	case DDI_MODEL_NONE:
28275 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28276 			return (EFAULT);
28277 		}
28278 		break;
28279 	}
28280 #else /* ! _MULTI_DATAMODEL */
28281 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28282 		return (EFAULT);
28283 	}
28284 #endif /* _MULTI_DATAMODEL */
28285 
28286 	/* Store the current target block size for restoration later */
28287 	restore_blksize = un->un_tgt_blocksize;
28288 
28289 	/* Change the device and soft state target block size to 2336 */
28290 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28291 		rval = EIO;
28292 		goto done;
28293 	}
28294 
28295 
28296 	bzero(cdb, sizeof (cdb));
28297 
28298 	/* set READ operation */
28299 	cdb[0] = SCMD_READ;
28300 
28301 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28302 	mode2->cdread_lba >>= 2;
28303 
28304 	/* set the start address */
28305 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28306 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28307 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28308 
28309 	/* set the transfer length */
28310 	nblocks = mode2->cdread_buflen / 2336;
28311 	cdb[4] = (uchar_t)nblocks & 0xFF;
28312 
28313 	/* build command */
28314 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28315 	com->uscsi_cdb = (caddr_t)cdb;
28316 	com->uscsi_cdblen = sizeof (cdb);
28317 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28318 	com->uscsi_buflen = mode2->cdread_buflen;
28319 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28320 
28321 	/*
28322 	 * Issue SCSI command with user space address for read buffer.
28323 	 *
28324 	 * This sends the command through main channel in the driver.
28325 	 *
28326 	 * Since this is accessed via an IOCTL call, we go through the
28327 	 * standard path, so that if the device was powered down, then
28328 	 * it would be 'awakened' to handle the command.
28329 	 */
28330 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28331 	    SD_PATH_STANDARD);
28332 
28333 	kmem_free(com, sizeof (*com));
28334 
28335 	/* Restore the device and soft state target block size */
28336 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28337 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28338 		    "can't do switch back to mode 1\n");
28339 		/*
28340 		 * If sd_send_scsi_READ succeeded we still need to report
28341 		 * an error because we failed to reset the block size
28342 		 */
28343 		if (rval == 0) {
28344 			rval = EIO;
28345 		}
28346 	}
28347 
28348 done:
28349 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28350 	    "sd_read_mode2: exit: un:0x%p\n", un);
28351 
28352 	return (rval);
28353 }
28354 
28355 
28356 /*
28357  *    Function: sr_sector_mode()
28358  *
28359  * Description: This utility function is used by sr_read_mode2 to set the target
28360  *		block size based on the user specified size. This is a legacy
28361  *		implementation based upon a vendor specific mode page
28362  *
28363  *   Arguments: dev	- the device 'dev_t'
28364  *		data	- flag indicating if block size is being set to 2336 or
28365  *			  512.
28366  *
28367  * Return Code: the code returned by sd_send_scsi_cmd()
28368  *		EFAULT if ddi_copyxxx() fails
28369  *		ENXIO if fail ddi_get_soft_state
28370  *		EINVAL if data pointer is NULL
28371  */
28372 
28373 static int
28374 sr_sector_mode(dev_t dev, uint32_t blksize)
28375 {
28376 	struct sd_lun	*un;
28377 	uchar_t		*sense;
28378 	uchar_t		*select;
28379 	int		rval;
28380 	sd_ssc_t	*ssc;
28381 
28382 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28383 	    (un->un_state == SD_STATE_OFFLINE)) {
28384 		return (ENXIO);
28385 	}
28386 
28387 	sense = kmem_zalloc(20, KM_SLEEP);
28388 
28389 	/* Note: This is a vendor specific mode page (0x81) */
28390 	ssc = sd_ssc_init(un);
28391 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28392 	    SD_PATH_STANDARD);
28393 	sd_ssc_fini(ssc);
28394 	if (rval != 0) {
28395 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28396 		    "sr_sector_mode: Mode Sense failed\n");
28397 		kmem_free(sense, 20);
28398 		return (rval);
28399 	}
28400 	select = kmem_zalloc(20, KM_SLEEP);
28401 	select[3] = 0x08;
28402 	select[10] = ((blksize >> 8) & 0xff);
28403 	select[11] = (blksize & 0xff);
28404 	select[12] = 0x01;
28405 	select[13] = 0x06;
28406 	select[14] = sense[14];
28407 	select[15] = sense[15];
28408 	if (blksize == SD_MODE2_BLKSIZE) {
28409 		select[14] |= 0x01;
28410 	}
28411 
28412 	ssc = sd_ssc_init(un);
28413 	rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28414 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28415 	sd_ssc_fini(ssc);
28416 	if (rval != 0) {
28417 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28418 		    "sr_sector_mode: Mode Select failed\n");
28419 	} else {
28420 		/*
28421 		 * Only update the softstate block size if we successfully
28422 		 * changed the device block mode.
28423 		 */
28424 		mutex_enter(SD_MUTEX(un));
28425 		sd_update_block_info(un, blksize, 0);
28426 		mutex_exit(SD_MUTEX(un));
28427 	}
28428 	kmem_free(sense, 20);
28429 	kmem_free(select, 20);
28430 	return (rval);
28431 }
28432 
28433 
28434 /*
28435  *    Function: sr_read_cdda()
28436  *
28437  * Description: This routine is the driver entry point for handling CD-ROM
28438  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28439  *		the target supports CDDA these requests are handled via a vendor
28440  *		specific command (0xD8) If the target does not support CDDA
28441  *		these requests are handled via the READ CD command (0xBE).
28442  *
28443  *   Arguments: dev	- the device 'dev_t'
28444  *		data	- pointer to user provided CD-DA structure specifying
28445  *			  the track starting address, transfer length, and
28446  *			  subcode options.
28447  *		flag	- this argument is a pass through to ddi_copyxxx()
28448  *			  directly from the mode argument of ioctl().
28449  *
28450  * Return Code: the code returned by sd_send_scsi_cmd()
28451  *		EFAULT if ddi_copyxxx() fails
28452  *		ENXIO if fail ddi_get_soft_state
28453  *		EINVAL if invalid arguments are provided
28454  *		ENOTTY
28455  */
28456 
28457 static int
28458 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28459 {
28460 	struct sd_lun			*un;
28461 	struct uscsi_cmd		*com;
28462 	struct cdrom_cdda		*cdda;
28463 	int				rval;
28464 	size_t				buflen;
28465 	char				cdb[CDB_GROUP5];
28466 
28467 #ifdef _MULTI_DATAMODEL
28468 	/* To support ILP32 applications in an LP64 world */
28469 	struct cdrom_cdda32	cdrom_cdda32;
28470 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28471 #endif /* _MULTI_DATAMODEL */
28472 
28473 	if (data == NULL) {
28474 		return (EINVAL);
28475 	}
28476 
28477 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28478 		return (ENXIO);
28479 	}
28480 
28481 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28482 
28483 #ifdef _MULTI_DATAMODEL
28484 	switch (ddi_model_convert_from(flag & FMODELS)) {
28485 	case DDI_MODEL_ILP32:
28486 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28487 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28488 			    "sr_read_cdda: ddi_copyin Failed\n");
28489 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28490 			return (EFAULT);
28491 		}
28492 		/* Convert the ILP32 uscsi data from the application to LP64 */
28493 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28494 		break;
28495 	case DDI_MODEL_NONE:
28496 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28497 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28498 			    "sr_read_cdda: ddi_copyin Failed\n");
28499 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28500 			return (EFAULT);
28501 		}
28502 		break;
28503 	}
28504 #else /* ! _MULTI_DATAMODEL */
28505 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28506 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28507 		    "sr_read_cdda: ddi_copyin Failed\n");
28508 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28509 		return (EFAULT);
28510 	}
28511 #endif /* _MULTI_DATAMODEL */
28512 
28513 	/*
28514 	 * Since MMC-2 expects max 3 bytes for length, check if the
28515 	 * length input is greater than 3 bytes
28516 	 */
28517 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28518 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28519 		    "cdrom transfer length too large: %d (limit %d)\n",
28520 		    cdda->cdda_length, 0xFFFFFF);
28521 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28522 		return (EINVAL);
28523 	}
28524 
28525 	switch (cdda->cdda_subcode) {
28526 	case CDROM_DA_NO_SUBCODE:
28527 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28528 		break;
28529 	case CDROM_DA_SUBQ:
28530 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28531 		break;
28532 	case CDROM_DA_ALL_SUBCODE:
28533 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28534 		break;
28535 	case CDROM_DA_SUBCODE_ONLY:
28536 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28537 		break;
28538 	default:
28539 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28540 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28541 		    cdda->cdda_subcode);
28542 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28543 		return (EINVAL);
28544 	}
28545 
28546 	/* Build and send the command */
28547 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28548 	bzero(cdb, CDB_GROUP5);
28549 
28550 	if (un->un_f_cfg_cdda == TRUE) {
28551 		cdb[0] = (char)SCMD_READ_CD;
28552 		cdb[1] = 0x04;
28553 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28554 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28555 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28556 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28557 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28558 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28559 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28560 		cdb[9] = 0x10;
28561 		switch (cdda->cdda_subcode) {
28562 		case CDROM_DA_NO_SUBCODE :
28563 			cdb[10] = 0x0;
28564 			break;
28565 		case CDROM_DA_SUBQ :
28566 			cdb[10] = 0x2;
28567 			break;
28568 		case CDROM_DA_ALL_SUBCODE :
28569 			cdb[10] = 0x1;
28570 			break;
28571 		case CDROM_DA_SUBCODE_ONLY :
28572 			/* FALLTHROUGH */
28573 		default :
28574 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28575 			kmem_free(com, sizeof (*com));
28576 			return (ENOTTY);
28577 		}
28578 	} else {
28579 		cdb[0] = (char)SCMD_READ_CDDA;
28580 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28581 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28582 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28583 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28584 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28585 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28586 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28587 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28588 		cdb[10] = cdda->cdda_subcode;
28589 	}
28590 
28591 	com->uscsi_cdb = cdb;
28592 	com->uscsi_cdblen = CDB_GROUP5;
28593 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28594 	com->uscsi_buflen = buflen;
28595 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28596 
28597 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28598 	    SD_PATH_STANDARD);
28599 
28600 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28601 	kmem_free(com, sizeof (*com));
28602 	return (rval);
28603 }
28604 
28605 
28606 /*
28607  *    Function: sr_read_cdxa()
28608  *
28609  * Description: This routine is the driver entry point for handling CD-ROM
28610  *		ioctl requests to return CD-XA (Extended Architecture) data.
28611  *		(CDROMCDXA).
28612  *
28613  *   Arguments: dev	- the device 'dev_t'
28614  *		data	- pointer to user provided CD-XA structure specifying
28615  *			  the data starting address, transfer length, and format
28616  *		flag	- this argument is a pass through to ddi_copyxxx()
28617  *			  directly from the mode argument of ioctl().
28618  *
28619  * Return Code: the code returned by sd_send_scsi_cmd()
28620  *		EFAULT if ddi_copyxxx() fails
28621  *		ENXIO if fail ddi_get_soft_state
28622  *		EINVAL if data pointer is NULL
28623  */
28624 
28625 static int
28626 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28627 {
28628 	struct sd_lun		*un;
28629 	struct uscsi_cmd	*com;
28630 	struct cdrom_cdxa	*cdxa;
28631 	int			rval;
28632 	size_t			buflen;
28633 	char			cdb[CDB_GROUP5];
28634 	uchar_t			read_flags;
28635 
28636 #ifdef _MULTI_DATAMODEL
28637 	/* To support ILP32 applications in an LP64 world */
28638 	struct cdrom_cdxa32		cdrom_cdxa32;
28639 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28640 #endif /* _MULTI_DATAMODEL */
28641 
28642 	if (data == NULL) {
28643 		return (EINVAL);
28644 	}
28645 
28646 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28647 		return (ENXIO);
28648 	}
28649 
28650 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28651 
28652 #ifdef _MULTI_DATAMODEL
28653 	switch (ddi_model_convert_from(flag & FMODELS)) {
28654 	case DDI_MODEL_ILP32:
28655 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28656 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28657 			return (EFAULT);
28658 		}
28659 		/*
28660 		 * Convert the ILP32 uscsi data from the
28661 		 * application to LP64 for internal use.
28662 		 */
28663 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28664 		break;
28665 	case DDI_MODEL_NONE:
28666 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28667 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28668 			return (EFAULT);
28669 		}
28670 		break;
28671 	}
28672 #else /* ! _MULTI_DATAMODEL */
28673 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28674 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28675 		return (EFAULT);
28676 	}
28677 #endif /* _MULTI_DATAMODEL */
28678 
28679 	/*
28680 	 * Since MMC-2 expects max 3 bytes for length, check if the
28681 	 * length input is greater than 3 bytes
28682 	 */
28683 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28684 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28685 		    "cdrom transfer length too large: %d (limit %d)\n",
28686 		    cdxa->cdxa_length, 0xFFFFFF);
28687 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28688 		return (EINVAL);
28689 	}
28690 
28691 	switch (cdxa->cdxa_format) {
28692 	case CDROM_XA_DATA:
28693 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28694 		read_flags = 0x10;
28695 		break;
28696 	case CDROM_XA_SECTOR_DATA:
28697 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28698 		read_flags = 0xf8;
28699 		break;
28700 	case CDROM_XA_DATA_W_ERROR:
28701 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28702 		read_flags = 0xfc;
28703 		break;
28704 	default:
28705 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28706 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28707 		    cdxa->cdxa_format);
28708 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28709 		return (EINVAL);
28710 	}
28711 
28712 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28713 	bzero(cdb, CDB_GROUP5);
28714 	if (un->un_f_mmc_cap == TRUE) {
28715 		cdb[0] = (char)SCMD_READ_CD;
28716 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28717 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28718 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28719 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28720 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28721 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28722 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28723 		cdb[9] = (char)read_flags;
28724 	} else {
28725 		/*
28726 		 * Note: A vendor specific command (0xDB) is being used her to
28727 		 * request a read of all subcodes.
28728 		 */
28729 		cdb[0] = (char)SCMD_READ_CDXA;
28730 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28731 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28732 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28733 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28734 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28735 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28736 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28737 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28738 		cdb[10] = cdxa->cdxa_format;
28739 	}
28740 	com->uscsi_cdb	   = cdb;
28741 	com->uscsi_cdblen  = CDB_GROUP5;
28742 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28743 	com->uscsi_buflen  = buflen;
28744 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28745 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28746 	    SD_PATH_STANDARD);
28747 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28748 	kmem_free(com, sizeof (*com));
28749 	return (rval);
28750 }
28751 
28752 
28753 /*
28754  *    Function: sr_eject()
28755  *
28756  * Description: This routine is the driver entry point for handling CD-ROM
28757  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28758  *
28759  *   Arguments: dev	- the device 'dev_t'
28760  *
28761  * Return Code: the code returned by sd_send_scsi_cmd()
28762  */
28763 
28764 static int
28765 sr_eject(dev_t dev)
28766 {
28767 	struct sd_lun	*un;
28768 	int		rval;
28769 	sd_ssc_t	*ssc;
28770 
28771 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28772 	    (un->un_state == SD_STATE_OFFLINE)) {
28773 		return (ENXIO);
28774 	}
28775 
28776 	/*
28777 	 * To prevent race conditions with the eject
28778 	 * command, keep track of an eject command as
28779 	 * it progresses. If we are already handling
28780 	 * an eject command in the driver for the given
28781 	 * unit and another request to eject is received
28782 	 * immediately return EAGAIN so we don't lose
28783 	 * the command if the current eject command fails.
28784 	 */
28785 	mutex_enter(SD_MUTEX(un));
28786 	if (un->un_f_ejecting == TRUE) {
28787 		mutex_exit(SD_MUTEX(un));
28788 		return (EAGAIN);
28789 	}
28790 	un->un_f_ejecting = TRUE;
28791 	mutex_exit(SD_MUTEX(un));
28792 
28793 	ssc = sd_ssc_init(un);
28794 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28795 	    SD_PATH_STANDARD);
28796 	sd_ssc_fini(ssc);
28797 
28798 	if (rval != 0) {
28799 		mutex_enter(SD_MUTEX(un));
28800 		un->un_f_ejecting = FALSE;
28801 		mutex_exit(SD_MUTEX(un));
28802 		return (rval);
28803 	}
28804 
28805 	ssc = sd_ssc_init(un);
28806 	rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28807 	    SD_TARGET_EJECT, SD_PATH_STANDARD);
28808 	sd_ssc_fini(ssc);
28809 
28810 	if (rval == 0) {
28811 		mutex_enter(SD_MUTEX(un));
28812 		sr_ejected(un);
28813 		un->un_mediastate = DKIO_EJECTED;
28814 		un->un_f_ejecting = FALSE;
28815 		cv_broadcast(&un->un_state_cv);
28816 		mutex_exit(SD_MUTEX(un));
28817 	} else {
28818 		mutex_enter(SD_MUTEX(un));
28819 		un->un_f_ejecting = FALSE;
28820 		mutex_exit(SD_MUTEX(un));
28821 	}
28822 	return (rval);
28823 }
28824 
28825 
28826 /*
28827  *    Function: sr_ejected()
28828  *
28829  * Description: This routine updates the soft state structure to invalidate the
28830  *		geometry information after the media has been ejected or a
28831  *		media eject has been detected.
28832  *
28833  *   Arguments: un - driver soft state (unit) structure
28834  */
28835 
28836 static void
28837 sr_ejected(struct sd_lun *un)
28838 {
28839 	struct sd_errstats *stp;
28840 
28841 	ASSERT(un != NULL);
28842 	ASSERT(mutex_owned(SD_MUTEX(un)));
28843 
28844 	un->un_f_blockcount_is_valid	= FALSE;
28845 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28846 	mutex_exit(SD_MUTEX(un));
28847 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28848 	mutex_enter(SD_MUTEX(un));
28849 
28850 	if (un->un_errstats != NULL) {
28851 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28852 		stp->sd_capacity.value.ui64 = 0;
28853 	}
28854 }
28855 
28856 
28857 /*
28858  *    Function: sr_check_wp()
28859  *
28860  * Description: This routine checks the write protection of a removable
28861  *      media disk and hotpluggable devices via the write protect bit of
28862  *      the Mode Page Header device specific field. Some devices choke
28863  *      on unsupported mode page. In order to workaround this issue,
28864  *      this routine has been implemented to use 0x3f mode page(request
28865  *      for all pages) for all device types.
28866  *
28867  *   Arguments: dev             - the device 'dev_t'
28868  *
28869  * Return Code: int indicating if the device is write protected (1) or not (0)
28870  *
28871  *     Context: Kernel thread.
28872  *
28873  */
28874 
28875 static int
28876 sr_check_wp(dev_t dev)
28877 {
28878 	struct sd_lun	*un;
28879 	uchar_t		device_specific;
28880 	uchar_t		*sense;
28881 	int		hdrlen;
28882 	int		rval = FALSE;
28883 	int		status;
28884 	sd_ssc_t	*ssc;
28885 
28886 	/*
28887 	 * Note: The return codes for this routine should be reworked to
28888 	 * properly handle the case of a NULL softstate.
28889 	 */
28890 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28891 		return (FALSE);
28892 	}
28893 
28894 	if (un->un_f_cfg_is_atapi == TRUE) {
28895 		/*
28896 		 * The mode page contents are not required; set the allocation
28897 		 * length for the mode page header only
28898 		 */
28899 		hdrlen = MODE_HEADER_LENGTH_GRP2;
28900 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28901 		ssc = sd_ssc_init(un);
28902 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28903 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28904 		sd_ssc_fini(ssc);
28905 		if (status != 0)
28906 			goto err_exit;
28907 		device_specific =
28908 		    ((struct mode_header_grp2 *)sense)->device_specific;
28909 	} else {
28910 		hdrlen = MODE_HEADER_LENGTH;
28911 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28912 		ssc = sd_ssc_init(un);
28913 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28914 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28915 		sd_ssc_fini(ssc);
28916 		if (status != 0)
28917 			goto err_exit;
28918 		device_specific =
28919 		    ((struct mode_header *)sense)->device_specific;
28920 	}
28921 
28922 
28923 	/*
28924 	 * Write protect mode sense failed; not all disks
28925 	 * understand this query. Return FALSE assuming that
28926 	 * these devices are not writable.
28927 	 */
28928 	if (device_specific & WRITE_PROTECT) {
28929 		rval = TRUE;
28930 	}
28931 
28932 err_exit:
28933 	kmem_free(sense, hdrlen);
28934 	return (rval);
28935 }
28936 
28937 /*
28938  *    Function: sr_volume_ctrl()
28939  *
28940  * Description: This routine is the driver entry point for handling CD-ROM
28941  *		audio output volume ioctl requests. (CDROMVOLCTRL)
28942  *
28943  *   Arguments: dev	- the device 'dev_t'
28944  *		data	- pointer to user audio volume control structure
28945  *		flag	- this argument is a pass through to ddi_copyxxx()
28946  *			  directly from the mode argument of ioctl().
28947  *
28948  * Return Code: the code returned by sd_send_scsi_cmd()
28949  *		EFAULT if ddi_copyxxx() fails
28950  *		ENXIO if fail ddi_get_soft_state
28951  *		EINVAL if data pointer is NULL
28952  *
28953  */
28954 
28955 static int
28956 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28957 {
28958 	struct sd_lun		*un;
28959 	struct cdrom_volctrl    volume;
28960 	struct cdrom_volctrl    *vol = &volume;
28961 	uchar_t			*sense_page;
28962 	uchar_t			*select_page;
28963 	uchar_t			*sense;
28964 	uchar_t			*select;
28965 	int			sense_buflen;
28966 	int			select_buflen;
28967 	int			rval;
28968 	sd_ssc_t		*ssc;
28969 
28970 	if (data == NULL) {
28971 		return (EINVAL);
28972 	}
28973 
28974 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28975 	    (un->un_state == SD_STATE_OFFLINE)) {
28976 		return (ENXIO);
28977 	}
28978 
28979 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
28980 		return (EFAULT);
28981 	}
28982 
28983 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28984 		struct mode_header_grp2		*sense_mhp;
28985 		struct mode_header_grp2		*select_mhp;
28986 		int				bd_len;
28987 
28988 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
28989 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
28990 		    MODEPAGE_AUDIO_CTRL_LEN;
28991 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28992 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28993 		ssc = sd_ssc_init(un);
28994 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
28995 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28996 		    SD_PATH_STANDARD);
28997 		sd_ssc_fini(ssc);
28998 
28999 		if (rval != 0) {
29000 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29001 			    "sr_volume_ctrl: Mode Sense Failed\n");
29002 			kmem_free(sense, sense_buflen);
29003 			kmem_free(select, select_buflen);
29004 			return (rval);
29005 		}
29006 		sense_mhp = (struct mode_header_grp2 *)sense;
29007 		select_mhp = (struct mode_header_grp2 *)select;
29008 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29009 		    sense_mhp->bdesc_length_lo;
29010 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29011 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29012 			    "sr_volume_ctrl: Mode Sense returned invalid "
29013 			    "block descriptor length\n");
29014 			kmem_free(sense, sense_buflen);
29015 			kmem_free(select, select_buflen);
29016 			return (EIO);
29017 		}
29018 		sense_page = (uchar_t *)
29019 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29020 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29021 		select_mhp->length_msb = 0;
29022 		select_mhp->length_lsb = 0;
29023 		select_mhp->bdesc_length_hi = 0;
29024 		select_mhp->bdesc_length_lo = 0;
29025 	} else {
29026 		struct mode_header		*sense_mhp, *select_mhp;
29027 
29028 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29029 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29030 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29031 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29032 		ssc = sd_ssc_init(un);
29033 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29034 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29035 		    SD_PATH_STANDARD);
29036 		sd_ssc_fini(ssc);
29037 
29038 		if (rval != 0) {
29039 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29040 			    "sr_volume_ctrl: Mode Sense Failed\n");
29041 			kmem_free(sense, sense_buflen);
29042 			kmem_free(select, select_buflen);
29043 			return (rval);
29044 		}
29045 		sense_mhp  = (struct mode_header *)sense;
29046 		select_mhp = (struct mode_header *)select;
29047 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29048 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29049 			    "sr_volume_ctrl: Mode Sense returned invalid "
29050 			    "block descriptor length\n");
29051 			kmem_free(sense, sense_buflen);
29052 			kmem_free(select, select_buflen);
29053 			return (EIO);
29054 		}
29055 		sense_page = (uchar_t *)
29056 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29057 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29058 		select_mhp->length = 0;
29059 		select_mhp->bdesc_length = 0;
29060 	}
29061 	/*
29062 	 * Note: An audio control data structure could be created and overlayed
29063 	 * on the following in place of the array indexing method implemented.
29064 	 */
29065 
29066 	/* Build the select data for the user volume data */
29067 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29068 	select_page[1] = 0xE;
29069 	/* Set the immediate bit */
29070 	select_page[2] = 0x04;
29071 	/* Zero out reserved fields */
29072 	select_page[3] = 0x00;
29073 	select_page[4] = 0x00;
29074 	/* Return sense data for fields not to be modified */
29075 	select_page[5] = sense_page[5];
29076 	select_page[6] = sense_page[6];
29077 	select_page[7] = sense_page[7];
29078 	/* Set the user specified volume levels for channel 0 and 1 */
29079 	select_page[8] = 0x01;
29080 	select_page[9] = vol->channel0;
29081 	select_page[10] = 0x02;
29082 	select_page[11] = vol->channel1;
29083 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29084 	select_page[12] = sense_page[12];
29085 	select_page[13] = sense_page[13];
29086 	select_page[14] = sense_page[14];
29087 	select_page[15] = sense_page[15];
29088 
29089 	ssc = sd_ssc_init(un);
29090 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29091 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29092 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29093 	} else {
29094 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29095 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29096 	}
29097 	sd_ssc_fini(ssc);
29098 
29099 	kmem_free(sense, sense_buflen);
29100 	kmem_free(select, select_buflen);
29101 	return (rval);
29102 }
29103 
29104 
29105 /*
29106  *    Function: sr_read_sony_session_offset()
29107  *
29108  * Description: This routine is the driver entry point for handling CD-ROM
29109  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29110  *		The address of the first track in the last session of a
29111  *		multi-session CD-ROM is returned
29112  *
29113  *		Note: This routine uses a vendor specific key value in the
29114  *		command control field without implementing any vendor check here
29115  *		or in the ioctl routine.
29116  *
29117  *   Arguments: dev	- the device 'dev_t'
29118  *		data	- pointer to an int to hold the requested address
29119  *		flag	- this argument is a pass through to ddi_copyxxx()
29120  *			  directly from the mode argument of ioctl().
29121  *
29122  * Return Code: the code returned by sd_send_scsi_cmd()
29123  *		EFAULT if ddi_copyxxx() fails
29124  *		ENXIO if fail ddi_get_soft_state
29125  *		EINVAL if data pointer is NULL
29126  */
29127 
29128 static int
29129 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29130 {
29131 	struct sd_lun		*un;
29132 	struct uscsi_cmd	*com;
29133 	caddr_t			buffer;
29134 	char			cdb[CDB_GROUP1];
29135 	int			session_offset = 0;
29136 	int			rval;
29137 
29138 	if (data == NULL) {
29139 		return (EINVAL);
29140 	}
29141 
29142 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29143 	    (un->un_state == SD_STATE_OFFLINE)) {
29144 		return (ENXIO);
29145 	}
29146 
29147 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29148 	bzero(cdb, CDB_GROUP1);
29149 	cdb[0] = SCMD_READ_TOC;
29150 	/*
29151 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29152 	 * (4 byte TOC response header + 8 byte response data)
29153 	 */
29154 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29155 	/* Byte 9 is the control byte. A vendor specific value is used */
29156 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29157 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29158 	com->uscsi_cdb = cdb;
29159 	com->uscsi_cdblen = CDB_GROUP1;
29160 	com->uscsi_bufaddr = buffer;
29161 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29162 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29163 
29164 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29165 	    SD_PATH_STANDARD);
29166 	if (rval != 0) {
29167 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29168 		kmem_free(com, sizeof (*com));
29169 		return (rval);
29170 	}
29171 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29172 		session_offset =
29173 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29174 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29175 		/*
29176 		 * Offset returned offset in current lbasize block's. Convert to
29177 		 * 2k block's to return to the user
29178 		 */
29179 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29180 			session_offset >>= 2;
29181 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29182 			session_offset >>= 1;
29183 		}
29184 	}
29185 
29186 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29187 		rval = EFAULT;
29188 	}
29189 
29190 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29191 	kmem_free(com, sizeof (*com));
29192 	return (rval);
29193 }
29194 
29195 
29196 /*
29197  *    Function: sd_wm_cache_constructor()
29198  *
29199  * Description: Cache Constructor for the wmap cache for the read/modify/write
29200  * 		devices.
29201  *
29202  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29203  *		un	- sd_lun structure for the device.
29204  *		flag	- the km flags passed to constructor
29205  *
29206  * Return Code: 0 on success.
29207  *		-1 on failure.
29208  */
29209 
29210 /*ARGSUSED*/
29211 static int
29212 sd_wm_cache_constructor(void *wm, void *un, int flags)
29213 {
29214 	bzero(wm, sizeof (struct sd_w_map));
29215 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29216 	return (0);
29217 }
29218 
29219 
29220 /*
29221  *    Function: sd_wm_cache_destructor()
29222  *
29223  * Description: Cache destructor for the wmap cache for the read/modify/write
29224  * 		devices.
29225  *
29226  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29227  *		un	- sd_lun structure for the device.
29228  */
29229 /*ARGSUSED*/
29230 static void
29231 sd_wm_cache_destructor(void *wm, void *un)
29232 {
29233 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29234 }
29235 
29236 
29237 /*
29238  *    Function: sd_range_lock()
29239  *
29240  * Description: Lock the range of blocks specified as parameter to ensure
29241  *		that read, modify write is atomic and no other i/o writes
29242  *		to the same location. The range is specified in terms
29243  *		of start and end blocks. Block numbers are the actual
29244  *		media block numbers and not system.
29245  *
29246  *   Arguments: un	- sd_lun structure for the device.
29247  *		startb - The starting block number
29248  *		endb - The end block number
29249  *		typ - type of i/o - simple/read_modify_write
29250  *
29251  * Return Code: wm  - pointer to the wmap structure.
29252  *
29253  *     Context: This routine can sleep.
29254  */
29255 
29256 static struct sd_w_map *
29257 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29258 {
29259 	struct sd_w_map *wmp = NULL;
29260 	struct sd_w_map *sl_wmp = NULL;
29261 	struct sd_w_map *tmp_wmp;
29262 	wm_state state = SD_WM_CHK_LIST;
29263 
29264 
29265 	ASSERT(un != NULL);
29266 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29267 
29268 	mutex_enter(SD_MUTEX(un));
29269 
29270 	while (state != SD_WM_DONE) {
29271 
29272 		switch (state) {
29273 		case SD_WM_CHK_LIST:
29274 			/*
29275 			 * This is the starting state. Check the wmap list
29276 			 * to see if the range is currently available.
29277 			 */
29278 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29279 				/*
29280 				 * If this is a simple write and no rmw
29281 				 * i/o is pending then try to lock the
29282 				 * range as the range should be available.
29283 				 */
29284 				state = SD_WM_LOCK_RANGE;
29285 			} else {
29286 				tmp_wmp = sd_get_range(un, startb, endb);
29287 				if (tmp_wmp != NULL) {
29288 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29289 						/*
29290 						 * Should not keep onlist wmps
29291 						 * while waiting this macro
29292 						 * will also do wmp = NULL;
29293 						 */
29294 						FREE_ONLIST_WMAP(un, wmp);
29295 					}
29296 					/*
29297 					 * sl_wmp is the wmap on which wait
29298 					 * is done, since the tmp_wmp points
29299 					 * to the inuse wmap, set sl_wmp to
29300 					 * tmp_wmp and change the state to sleep
29301 					 */
29302 					sl_wmp = tmp_wmp;
29303 					state = SD_WM_WAIT_MAP;
29304 				} else {
29305 					state = SD_WM_LOCK_RANGE;
29306 				}
29307 
29308 			}
29309 			break;
29310 
29311 		case SD_WM_LOCK_RANGE:
29312 			ASSERT(un->un_wm_cache);
29313 			/*
29314 			 * The range need to be locked, try to get a wmap.
29315 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29316 			 * if possible as we will have to release the sd mutex
29317 			 * if we have to sleep.
29318 			 */
29319 			if (wmp == NULL)
29320 				wmp = kmem_cache_alloc(un->un_wm_cache,
29321 				    KM_NOSLEEP);
29322 			if (wmp == NULL) {
29323 				mutex_exit(SD_MUTEX(un));
29324 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29325 				    (sd_lun::un_wm_cache))
29326 				wmp = kmem_cache_alloc(un->un_wm_cache,
29327 				    KM_SLEEP);
29328 				mutex_enter(SD_MUTEX(un));
29329 				/*
29330 				 * we released the mutex so recheck and go to
29331 				 * check list state.
29332 				 */
29333 				state = SD_WM_CHK_LIST;
29334 			} else {
29335 				/*
29336 				 * We exit out of state machine since we
29337 				 * have the wmap. Do the housekeeping first.
29338 				 * place the wmap on the wmap list if it is not
29339 				 * on it already and then set the state to done.
29340 				 */
29341 				wmp->wm_start = startb;
29342 				wmp->wm_end = endb;
29343 				wmp->wm_flags = typ | SD_WM_BUSY;
29344 				if (typ & SD_WTYPE_RMW) {
29345 					un->un_rmw_count++;
29346 				}
29347 				/*
29348 				 * If not already on the list then link
29349 				 */
29350 				if (!ONLIST(un, wmp)) {
29351 					wmp->wm_next = un->un_wm;
29352 					wmp->wm_prev = NULL;
29353 					if (wmp->wm_next)
29354 						wmp->wm_next->wm_prev = wmp;
29355 					un->un_wm = wmp;
29356 				}
29357 				state = SD_WM_DONE;
29358 			}
29359 			break;
29360 
29361 		case SD_WM_WAIT_MAP:
29362 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29363 			/*
29364 			 * Wait is done on sl_wmp, which is set in the
29365 			 * check_list state.
29366 			 */
29367 			sl_wmp->wm_wanted_count++;
29368 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29369 			sl_wmp->wm_wanted_count--;
29370 			/*
29371 			 * We can reuse the memory from the completed sl_wmp
29372 			 * lock range for our new lock, but only if noone is
29373 			 * waiting for it.
29374 			 */
29375 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29376 			if (sl_wmp->wm_wanted_count == 0) {
29377 				if (wmp != NULL)
29378 					CHK_N_FREEWMP(un, wmp);
29379 				wmp = sl_wmp;
29380 			}
29381 			sl_wmp = NULL;
29382 			/*
29383 			 * After waking up, need to recheck for availability of
29384 			 * range.
29385 			 */
29386 			state = SD_WM_CHK_LIST;
29387 			break;
29388 
29389 		default:
29390 			panic("sd_range_lock: "
29391 			    "Unknown state %d in sd_range_lock", state);
29392 			/*NOTREACHED*/
29393 		} /* switch(state) */
29394 
29395 	} /* while(state != SD_WM_DONE) */
29396 
29397 	mutex_exit(SD_MUTEX(un));
29398 
29399 	ASSERT(wmp != NULL);
29400 
29401 	return (wmp);
29402 }
29403 
29404 
29405 /*
29406  *    Function: sd_get_range()
29407  *
29408  * Description: Find if there any overlapping I/O to this one
29409  *		Returns the write-map of 1st such I/O, NULL otherwise.
29410  *
29411  *   Arguments: un	- sd_lun structure for the device.
29412  *		startb - The starting block number
29413  *		endb - The end block number
29414  *
29415  * Return Code: wm  - pointer to the wmap structure.
29416  */
29417 
29418 static struct sd_w_map *
29419 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29420 {
29421 	struct sd_w_map *wmp;
29422 
29423 	ASSERT(un != NULL);
29424 
29425 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29426 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29427 			continue;
29428 		}
29429 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29430 			break;
29431 		}
29432 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29433 			break;
29434 		}
29435 	}
29436 
29437 	return (wmp);
29438 }
29439 
29440 
29441 /*
29442  *    Function: sd_free_inlist_wmap()
29443  *
29444  * Description: Unlink and free a write map struct.
29445  *
29446  *   Arguments: un      - sd_lun structure for the device.
29447  *		wmp	- sd_w_map which needs to be unlinked.
29448  */
29449 
29450 static void
29451 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29452 {
29453 	ASSERT(un != NULL);
29454 
29455 	if (un->un_wm == wmp) {
29456 		un->un_wm = wmp->wm_next;
29457 	} else {
29458 		wmp->wm_prev->wm_next = wmp->wm_next;
29459 	}
29460 
29461 	if (wmp->wm_next) {
29462 		wmp->wm_next->wm_prev = wmp->wm_prev;
29463 	}
29464 
29465 	wmp->wm_next = wmp->wm_prev = NULL;
29466 
29467 	kmem_cache_free(un->un_wm_cache, wmp);
29468 }
29469 
29470 
29471 /*
29472  *    Function: sd_range_unlock()
29473  *
29474  * Description: Unlock the range locked by wm.
29475  *		Free write map if nobody else is waiting on it.
29476  *
29477  *   Arguments: un      - sd_lun structure for the device.
29478  *              wmp     - sd_w_map which needs to be unlinked.
29479  */
29480 
29481 static void
29482 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29483 {
29484 	ASSERT(un != NULL);
29485 	ASSERT(wm != NULL);
29486 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29487 
29488 	mutex_enter(SD_MUTEX(un));
29489 
29490 	if (wm->wm_flags & SD_WTYPE_RMW) {
29491 		un->un_rmw_count--;
29492 	}
29493 
29494 	if (wm->wm_wanted_count) {
29495 		wm->wm_flags = 0;
29496 		/*
29497 		 * Broadcast that the wmap is available now.
29498 		 */
29499 		cv_broadcast(&wm->wm_avail);
29500 	} else {
29501 		/*
29502 		 * If no one is waiting on the map, it should be free'ed.
29503 		 */
29504 		sd_free_inlist_wmap(un, wm);
29505 	}
29506 
29507 	mutex_exit(SD_MUTEX(un));
29508 }
29509 
29510 
29511 /*
29512  *    Function: sd_read_modify_write_task
29513  *
29514  * Description: Called from a taskq thread to initiate the write phase of
29515  *		a read-modify-write request.  This is used for targets where
29516  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29517  *
29518  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29519  *
29520  *     Context: Called under taskq thread context.
29521  */
29522 
29523 static void
29524 sd_read_modify_write_task(void *arg)
29525 {
29526 	struct sd_mapblocksize_info	*bsp;
29527 	struct buf	*bp;
29528 	struct sd_xbuf	*xp;
29529 	struct sd_lun	*un;
29530 
29531 	bp = arg;	/* The bp is given in arg */
29532 	ASSERT(bp != NULL);
29533 
29534 	/* Get the pointer to the layer-private data struct */
29535 	xp = SD_GET_XBUF(bp);
29536 	ASSERT(xp != NULL);
29537 	bsp = xp->xb_private;
29538 	ASSERT(bsp != NULL);
29539 
29540 	un = SD_GET_UN(bp);
29541 	ASSERT(un != NULL);
29542 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29543 
29544 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29545 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29546 
29547 	/*
29548 	 * This is the write phase of a read-modify-write request, called
29549 	 * under the context of a taskq thread in response to the completion
29550 	 * of the read portion of the rmw request completing under interrupt
29551 	 * context. The write request must be sent from here down the iostart
29552 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29553 	 * we use the layer index saved in the layer-private data area.
29554 	 */
29555 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29556 
29557 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29558 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29559 }
29560 
29561 
29562 /*
29563  *    Function: sddump_do_read_of_rmw()
29564  *
29565  * Description: This routine will be called from sddump, If sddump is called
29566  *		with an I/O which not aligned on device blocksize boundary
29567  *		then the write has to be converted to read-modify-write.
29568  *		Do the read part here in order to keep sddump simple.
29569  *		Note - That the sd_mutex is held across the call to this
29570  *		routine.
29571  *
29572  *   Arguments: un	- sd_lun
29573  *		blkno	- block number in terms of media block size.
29574  *		nblk	- number of blocks.
29575  *		bpp	- pointer to pointer to the buf structure. On return
29576  *			from this function, *bpp points to the valid buffer
29577  *			to which the write has to be done.
29578  *
29579  * Return Code: 0 for success or errno-type return code
29580  */
29581 
29582 static int
29583 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29584 	struct buf **bpp)
29585 {
29586 	int err;
29587 	int i;
29588 	int rval;
29589 	struct buf *bp;
29590 	struct scsi_pkt *pkt = NULL;
29591 	uint32_t target_blocksize;
29592 
29593 	ASSERT(un != NULL);
29594 	ASSERT(mutex_owned(SD_MUTEX(un)));
29595 
29596 	target_blocksize = un->un_tgt_blocksize;
29597 
29598 	mutex_exit(SD_MUTEX(un));
29599 
29600 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29601 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29602 	if (bp == NULL) {
29603 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29604 		    "no resources for dumping; giving up");
29605 		err = ENOMEM;
29606 		goto done;
29607 	}
29608 
29609 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29610 	    blkno, nblk);
29611 	if (rval != 0) {
29612 		scsi_free_consistent_buf(bp);
29613 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29614 		    "no resources for dumping; giving up");
29615 		err = ENOMEM;
29616 		goto done;
29617 	}
29618 
29619 	pkt->pkt_flags |= FLAG_NOINTR;
29620 
29621 	err = EIO;
29622 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29623 
29624 		/*
29625 		 * Scsi_poll returns 0 (success) if the command completes and
29626 		 * the status block is STATUS_GOOD.  We should only check
29627 		 * errors if this condition is not true.  Even then we should
29628 		 * send our own request sense packet only if we have a check
29629 		 * condition and auto request sense has not been performed by
29630 		 * the hba.
29631 		 */
29632 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29633 
29634 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29635 			err = 0;
29636 			break;
29637 		}
29638 
29639 		/*
29640 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29641 		 * no need to read RQS data.
29642 		 */
29643 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29644 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29645 			    "Error while dumping state with rmw..."
29646 			    "Device is gone\n");
29647 			break;
29648 		}
29649 
29650 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29651 			SD_INFO(SD_LOG_DUMP, un,
29652 			    "sddump: read failed with CHECK, try # %d\n", i);
29653 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29654 				(void) sd_send_polled_RQS(un);
29655 			}
29656 
29657 			continue;
29658 		}
29659 
29660 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29661 			int reset_retval = 0;
29662 
29663 			SD_INFO(SD_LOG_DUMP, un,
29664 			    "sddump: read failed with BUSY, try # %d\n", i);
29665 
29666 			if (un->un_f_lun_reset_enabled == TRUE) {
29667 				reset_retval = scsi_reset(SD_ADDRESS(un),
29668 				    RESET_LUN);
29669 			}
29670 			if (reset_retval == 0) {
29671 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29672 			}
29673 			(void) sd_send_polled_RQS(un);
29674 
29675 		} else {
29676 			SD_INFO(SD_LOG_DUMP, un,
29677 			    "sddump: read failed with 0x%x, try # %d\n",
29678 			    SD_GET_PKT_STATUS(pkt), i);
29679 			mutex_enter(SD_MUTEX(un));
29680 			sd_reset_target(un, pkt);
29681 			mutex_exit(SD_MUTEX(un));
29682 		}
29683 
29684 		/*
29685 		 * If we are not getting anywhere with lun/target resets,
29686 		 * let's reset the bus.
29687 		 */
29688 		if (i > SD_NDUMP_RETRIES/2) {
29689 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29690 			(void) sd_send_polled_RQS(un);
29691 		}
29692 
29693 	}
29694 	scsi_destroy_pkt(pkt);
29695 
29696 	if (err != 0) {
29697 		scsi_free_consistent_buf(bp);
29698 		*bpp = NULL;
29699 	} else {
29700 		*bpp = bp;
29701 	}
29702 
29703 done:
29704 	mutex_enter(SD_MUTEX(un));
29705 	return (err);
29706 }
29707 
29708 
29709 /*
29710  *    Function: sd_failfast_flushq
29711  *
29712  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29713  *		in b_flags and move them onto the failfast queue, then kick
29714  *		off a thread to return all bp's on the failfast queue to
29715  *		their owners with an error set.
29716  *
29717  *   Arguments: un - pointer to the soft state struct for the instance.
29718  *
29719  *     Context: may execute in interrupt context.
29720  */
29721 
29722 static void
29723 sd_failfast_flushq(struct sd_lun *un)
29724 {
29725 	struct buf *bp;
29726 	struct buf *next_waitq_bp;
29727 	struct buf *prev_waitq_bp = NULL;
29728 
29729 	ASSERT(un != NULL);
29730 	ASSERT(mutex_owned(SD_MUTEX(un)));
29731 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29732 	ASSERT(un->un_failfast_bp == NULL);
29733 
29734 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29735 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29736 
29737 	/*
29738 	 * Check if we should flush all bufs when entering failfast state, or
29739 	 * just those with B_FAILFAST set.
29740 	 */
29741 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29742 		/*
29743 		 * Move *all* bp's on the wait queue to the failfast flush
29744 		 * queue, including those that do NOT have B_FAILFAST set.
29745 		 */
29746 		if (un->un_failfast_headp == NULL) {
29747 			ASSERT(un->un_failfast_tailp == NULL);
29748 			un->un_failfast_headp = un->un_waitq_headp;
29749 		} else {
29750 			ASSERT(un->un_failfast_tailp != NULL);
29751 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29752 		}
29753 
29754 		un->un_failfast_tailp = un->un_waitq_tailp;
29755 
29756 		/* update kstat for each bp moved out of the waitq */
29757 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29758 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29759 		}
29760 
29761 		/* empty the waitq */
29762 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29763 
29764 	} else {
29765 		/*
29766 		 * Go thru the wait queue, pick off all entries with
29767 		 * B_FAILFAST set, and move these onto the failfast queue.
29768 		 */
29769 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29770 			/*
29771 			 * Save the pointer to the next bp on the wait queue,
29772 			 * so we get to it on the next iteration of this loop.
29773 			 */
29774 			next_waitq_bp = bp->av_forw;
29775 
29776 			/*
29777 			 * If this bp from the wait queue does NOT have
29778 			 * B_FAILFAST set, just move on to the next element
29779 			 * in the wait queue. Note, this is the only place
29780 			 * where it is correct to set prev_waitq_bp.
29781 			 */
29782 			if ((bp->b_flags & B_FAILFAST) == 0) {
29783 				prev_waitq_bp = bp;
29784 				continue;
29785 			}
29786 
29787 			/*
29788 			 * Remove the bp from the wait queue.
29789 			 */
29790 			if (bp == un->un_waitq_headp) {
29791 				/* The bp is the first element of the waitq. */
29792 				un->un_waitq_headp = next_waitq_bp;
29793 				if (un->un_waitq_headp == NULL) {
29794 					/* The wait queue is now empty */
29795 					un->un_waitq_tailp = NULL;
29796 				}
29797 			} else {
29798 				/*
29799 				 * The bp is either somewhere in the middle
29800 				 * or at the end of the wait queue.
29801 				 */
29802 				ASSERT(un->un_waitq_headp != NULL);
29803 				ASSERT(prev_waitq_bp != NULL);
29804 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29805 				    == 0);
29806 				if (bp == un->un_waitq_tailp) {
29807 					/* bp is the last entry on the waitq. */
29808 					ASSERT(next_waitq_bp == NULL);
29809 					un->un_waitq_tailp = prev_waitq_bp;
29810 				}
29811 				prev_waitq_bp->av_forw = next_waitq_bp;
29812 			}
29813 			bp->av_forw = NULL;
29814 
29815 			/*
29816 			 * update kstat since the bp is moved out of
29817 			 * the waitq
29818 			 */
29819 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29820 
29821 			/*
29822 			 * Now put the bp onto the failfast queue.
29823 			 */
29824 			if (un->un_failfast_headp == NULL) {
29825 				/* failfast queue is currently empty */
29826 				ASSERT(un->un_failfast_tailp == NULL);
29827 				un->un_failfast_headp =
29828 				    un->un_failfast_tailp = bp;
29829 			} else {
29830 				/* Add the bp to the end of the failfast q */
29831 				ASSERT(un->un_failfast_tailp != NULL);
29832 				ASSERT(un->un_failfast_tailp->b_flags &
29833 				    B_FAILFAST);
29834 				un->un_failfast_tailp->av_forw = bp;
29835 				un->un_failfast_tailp = bp;
29836 			}
29837 		}
29838 	}
29839 
29840 	/*
29841 	 * Now return all bp's on the failfast queue to their owners.
29842 	 */
29843 	while ((bp = un->un_failfast_headp) != NULL) {
29844 
29845 		un->un_failfast_headp = bp->av_forw;
29846 		if (un->un_failfast_headp == NULL) {
29847 			un->un_failfast_tailp = NULL;
29848 		}
29849 
29850 		/*
29851 		 * We want to return the bp with a failure error code, but
29852 		 * we do not want a call to sd_start_cmds() to occur here,
29853 		 * so use sd_return_failed_command_no_restart() instead of
29854 		 * sd_return_failed_command().
29855 		 */
29856 		sd_return_failed_command_no_restart(un, bp, EIO);
29857 	}
29858 
29859 	/* Flush the xbuf queues if required. */
29860 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29861 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29862 	}
29863 
29864 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29865 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29866 }
29867 
29868 
29869 /*
29870  *    Function: sd_failfast_flushq_callback
29871  *
29872  * Description: Return TRUE if the given bp meets the criteria for failfast
29873  *		flushing. Used with ddi_xbuf_flushq(9F).
29874  *
29875  *   Arguments: bp - ptr to buf struct to be examined.
29876  *
29877  *     Context: Any
29878  */
29879 
29880 static int
29881 sd_failfast_flushq_callback(struct buf *bp)
29882 {
29883 	/*
29884 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29885 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29886 	 */
29887 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29888 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29889 }
29890 
29891 
29892 
29893 /*
29894  * Function: sd_setup_next_xfer
29895  *
29896  * Description: Prepare next I/O operation using DMA_PARTIAL
29897  *
29898  */
29899 
29900 static int
29901 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29902     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29903 {
29904 	ssize_t	num_blks_not_xfered;
29905 	daddr_t	strt_blk_num;
29906 	ssize_t	bytes_not_xfered;
29907 	int	rval;
29908 
29909 	ASSERT(pkt->pkt_resid == 0);
29910 
29911 	/*
29912 	 * Calculate next block number and amount to be transferred.
29913 	 *
29914 	 * How much data NOT transfered to the HBA yet.
29915 	 */
29916 	bytes_not_xfered = xp->xb_dma_resid;
29917 
29918 	/*
29919 	 * figure how many blocks NOT transfered to the HBA yet.
29920 	 */
29921 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29922 
29923 	/*
29924 	 * set starting block number to the end of what WAS transfered.
29925 	 */
29926 	strt_blk_num = xp->xb_blkno +
29927 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29928 
29929 	/*
29930 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29931 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29932 	 * the disk mutex here.
29933 	 */
29934 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
29935 	    strt_blk_num, num_blks_not_xfered);
29936 
29937 	if (rval == 0) {
29938 
29939 		/*
29940 		 * Success.
29941 		 *
29942 		 * Adjust things if there are still more blocks to be
29943 		 * transfered.
29944 		 */
29945 		xp->xb_dma_resid = pkt->pkt_resid;
29946 		pkt->pkt_resid = 0;
29947 
29948 		return (1);
29949 	}
29950 
29951 	/*
29952 	 * There's really only one possible return value from
29953 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29954 	 * returns NULL.
29955 	 */
29956 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29957 
29958 	bp->b_resid = bp->b_bcount;
29959 	bp->b_flags |= B_ERROR;
29960 
29961 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29962 	    "Error setting up next portion of DMA transfer\n");
29963 
29964 	return (0);
29965 }
29966 
29967 /*
29968  *    Function: sd_panic_for_res_conflict
29969  *
29970  * Description: Call panic with a string formatted with "Reservation Conflict"
29971  *		and a human readable identifier indicating the SD instance
29972  *		that experienced the reservation conflict.
29973  *
29974  *   Arguments: un - pointer to the soft state struct for the instance.
29975  *
29976  *     Context: may execute in interrupt context.
29977  */
29978 
29979 #define	SD_RESV_CONFLICT_FMT_LEN 40
29980 void
29981 sd_panic_for_res_conflict(struct sd_lun *un)
29982 {
29983 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
29984 	char path_str[MAXPATHLEN];
29985 
29986 	(void) snprintf(panic_str, sizeof (panic_str),
29987 	    "Reservation Conflict\nDisk: %s",
29988 	    ddi_pathname(SD_DEVINFO(un), path_str));
29989 
29990 	panic(panic_str);
29991 }
29992 
29993 /*
29994  * Note: The following sd_faultinjection_ioctl( ) routines implement
29995  * driver support for handling fault injection for error analysis
29996  * causing faults in multiple layers of the driver.
29997  *
29998  */
29999 
30000 #ifdef SD_FAULT_INJECTION
30001 static uint_t   sd_fault_injection_on = 0;
30002 
30003 /*
30004  *    Function: sd_faultinjection_ioctl()
30005  *
30006  * Description: This routine is the driver entry point for handling
30007  *              faultinjection ioctls to inject errors into the
30008  *              layer model
30009  *
30010  *   Arguments: cmd	- the ioctl cmd received
30011  *		arg	- the arguments from user and returns
30012  */
30013 
30014 static void
30015 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30016 
30017 	uint_t i = 0;
30018 	uint_t rval;
30019 
30020 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30021 
30022 	mutex_enter(SD_MUTEX(un));
30023 
30024 	switch (cmd) {
30025 	case SDIOCRUN:
30026 		/* Allow pushed faults to be injected */
30027 		SD_INFO(SD_LOG_SDTEST, un,
30028 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30029 
30030 		sd_fault_injection_on = 1;
30031 
30032 		SD_INFO(SD_LOG_IOERR, un,
30033 		    "sd_faultinjection_ioctl: run finished\n");
30034 		break;
30035 
30036 	case SDIOCSTART:
30037 		/* Start Injection Session */
30038 		SD_INFO(SD_LOG_SDTEST, un,
30039 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30040 
30041 		sd_fault_injection_on = 0;
30042 		un->sd_injection_mask = 0xFFFFFFFF;
30043 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30044 			un->sd_fi_fifo_pkt[i] = NULL;
30045 			un->sd_fi_fifo_xb[i] = NULL;
30046 			un->sd_fi_fifo_un[i] = NULL;
30047 			un->sd_fi_fifo_arq[i] = NULL;
30048 		}
30049 		un->sd_fi_fifo_start = 0;
30050 		un->sd_fi_fifo_end = 0;
30051 
30052 		mutex_enter(&(un->un_fi_mutex));
30053 		un->sd_fi_log[0] = '\0';
30054 		un->sd_fi_buf_len = 0;
30055 		mutex_exit(&(un->un_fi_mutex));
30056 
30057 		SD_INFO(SD_LOG_IOERR, un,
30058 		    "sd_faultinjection_ioctl: start finished\n");
30059 		break;
30060 
30061 	case SDIOCSTOP:
30062 		/* Stop Injection Session */
30063 		SD_INFO(SD_LOG_SDTEST, un,
30064 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30065 		sd_fault_injection_on = 0;
30066 		un->sd_injection_mask = 0x0;
30067 
30068 		/* Empty stray or unuseds structs from fifo */
30069 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30070 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30071 				kmem_free(un->sd_fi_fifo_pkt[i],
30072 				    sizeof (struct sd_fi_pkt));
30073 			}
30074 			if (un->sd_fi_fifo_xb[i] != NULL) {
30075 				kmem_free(un->sd_fi_fifo_xb[i],
30076 				    sizeof (struct sd_fi_xb));
30077 			}
30078 			if (un->sd_fi_fifo_un[i] != NULL) {
30079 				kmem_free(un->sd_fi_fifo_un[i],
30080 				    sizeof (struct sd_fi_un));
30081 			}
30082 			if (un->sd_fi_fifo_arq[i] != NULL) {
30083 				kmem_free(un->sd_fi_fifo_arq[i],
30084 				    sizeof (struct sd_fi_arq));
30085 			}
30086 			un->sd_fi_fifo_pkt[i] = NULL;
30087 			un->sd_fi_fifo_un[i] = NULL;
30088 			un->sd_fi_fifo_xb[i] = NULL;
30089 			un->sd_fi_fifo_arq[i] = NULL;
30090 		}
30091 		un->sd_fi_fifo_start = 0;
30092 		un->sd_fi_fifo_end = 0;
30093 
30094 		SD_INFO(SD_LOG_IOERR, un,
30095 		    "sd_faultinjection_ioctl: stop finished\n");
30096 		break;
30097 
30098 	case SDIOCINSERTPKT:
30099 		/* Store a packet struct to be pushed onto fifo */
30100 		SD_INFO(SD_LOG_SDTEST, un,
30101 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30102 
30103 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30104 
30105 		sd_fault_injection_on = 0;
30106 
30107 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30108 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30109 			kmem_free(un->sd_fi_fifo_pkt[i],
30110 			    sizeof (struct sd_fi_pkt));
30111 		}
30112 		if (arg != NULL) {
30113 			un->sd_fi_fifo_pkt[i] =
30114 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30115 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30116 				/* Alloc failed don't store anything */
30117 				break;
30118 			}
30119 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30120 			    sizeof (struct sd_fi_pkt), 0);
30121 			if (rval == -1) {
30122 				kmem_free(un->sd_fi_fifo_pkt[i],
30123 				    sizeof (struct sd_fi_pkt));
30124 				un->sd_fi_fifo_pkt[i] = NULL;
30125 			}
30126 		} else {
30127 			SD_INFO(SD_LOG_IOERR, un,
30128 			    "sd_faultinjection_ioctl: pkt null\n");
30129 		}
30130 		break;
30131 
30132 	case SDIOCINSERTXB:
30133 		/* Store a xb struct to be pushed onto fifo */
30134 		SD_INFO(SD_LOG_SDTEST, un,
30135 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30136 
30137 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30138 
30139 		sd_fault_injection_on = 0;
30140 
30141 		if (un->sd_fi_fifo_xb[i] != NULL) {
30142 			kmem_free(un->sd_fi_fifo_xb[i],
30143 			    sizeof (struct sd_fi_xb));
30144 			un->sd_fi_fifo_xb[i] = NULL;
30145 		}
30146 		if (arg != NULL) {
30147 			un->sd_fi_fifo_xb[i] =
30148 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30149 			if (un->sd_fi_fifo_xb[i] == NULL) {
30150 				/* Alloc failed don't store anything */
30151 				break;
30152 			}
30153 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30154 			    sizeof (struct sd_fi_xb), 0);
30155 
30156 			if (rval == -1) {
30157 				kmem_free(un->sd_fi_fifo_xb[i],
30158 				    sizeof (struct sd_fi_xb));
30159 				un->sd_fi_fifo_xb[i] = NULL;
30160 			}
30161 		} else {
30162 			SD_INFO(SD_LOG_IOERR, un,
30163 			    "sd_faultinjection_ioctl: xb null\n");
30164 		}
30165 		break;
30166 
30167 	case SDIOCINSERTUN:
30168 		/* Store a un struct to be pushed onto fifo */
30169 		SD_INFO(SD_LOG_SDTEST, un,
30170 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30171 
30172 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30173 
30174 		sd_fault_injection_on = 0;
30175 
30176 		if (un->sd_fi_fifo_un[i] != NULL) {
30177 			kmem_free(un->sd_fi_fifo_un[i],
30178 			    sizeof (struct sd_fi_un));
30179 			un->sd_fi_fifo_un[i] = NULL;
30180 		}
30181 		if (arg != NULL) {
30182 			un->sd_fi_fifo_un[i] =
30183 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30184 			if (un->sd_fi_fifo_un[i] == NULL) {
30185 				/* Alloc failed don't store anything */
30186 				break;
30187 			}
30188 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30189 			    sizeof (struct sd_fi_un), 0);
30190 			if (rval == -1) {
30191 				kmem_free(un->sd_fi_fifo_un[i],
30192 				    sizeof (struct sd_fi_un));
30193 				un->sd_fi_fifo_un[i] = NULL;
30194 			}
30195 
30196 		} else {
30197 			SD_INFO(SD_LOG_IOERR, un,
30198 			    "sd_faultinjection_ioctl: un null\n");
30199 		}
30200 
30201 		break;
30202 
30203 	case SDIOCINSERTARQ:
30204 		/* Store a arq struct to be pushed onto fifo */
30205 		SD_INFO(SD_LOG_SDTEST, un,
30206 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30207 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30208 
30209 		sd_fault_injection_on = 0;
30210 
30211 		if (un->sd_fi_fifo_arq[i] != NULL) {
30212 			kmem_free(un->sd_fi_fifo_arq[i],
30213 			    sizeof (struct sd_fi_arq));
30214 			un->sd_fi_fifo_arq[i] = NULL;
30215 		}
30216 		if (arg != NULL) {
30217 			un->sd_fi_fifo_arq[i] =
30218 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30219 			if (un->sd_fi_fifo_arq[i] == NULL) {
30220 				/* Alloc failed don't store anything */
30221 				break;
30222 			}
30223 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30224 			    sizeof (struct sd_fi_arq), 0);
30225 			if (rval == -1) {
30226 				kmem_free(un->sd_fi_fifo_arq[i],
30227 				    sizeof (struct sd_fi_arq));
30228 				un->sd_fi_fifo_arq[i] = NULL;
30229 			}
30230 
30231 		} else {
30232 			SD_INFO(SD_LOG_IOERR, un,
30233 			    "sd_faultinjection_ioctl: arq null\n");
30234 		}
30235 
30236 		break;
30237 
30238 	case SDIOCPUSH:
30239 		/* Push stored xb, pkt, un, and arq onto fifo */
30240 		sd_fault_injection_on = 0;
30241 
30242 		if (arg != NULL) {
30243 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30244 			if (rval != -1 &&
30245 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30246 				un->sd_fi_fifo_end += i;
30247 			}
30248 		} else {
30249 			SD_INFO(SD_LOG_IOERR, un,
30250 			    "sd_faultinjection_ioctl: push arg null\n");
30251 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30252 				un->sd_fi_fifo_end++;
30253 			}
30254 		}
30255 		SD_INFO(SD_LOG_IOERR, un,
30256 		    "sd_faultinjection_ioctl: push to end=%d\n",
30257 		    un->sd_fi_fifo_end);
30258 		break;
30259 
30260 	case SDIOCRETRIEVE:
30261 		/* Return buffer of log from Injection session */
30262 		SD_INFO(SD_LOG_SDTEST, un,
30263 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30264 
30265 		sd_fault_injection_on = 0;
30266 
30267 		mutex_enter(&(un->un_fi_mutex));
30268 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30269 		    un->sd_fi_buf_len+1, 0);
30270 		mutex_exit(&(un->un_fi_mutex));
30271 
30272 		if (rval == -1) {
30273 			/*
30274 			 * arg is possibly invalid setting
30275 			 * it to NULL for return
30276 			 */
30277 			arg = NULL;
30278 		}
30279 		break;
30280 	}
30281 
30282 	mutex_exit(SD_MUTEX(un));
30283 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30284 			    " exit\n");
30285 }
30286 
30287 
30288 /*
30289  *    Function: sd_injection_log()
30290  *
30291  * Description: This routine adds buff to the already existing injection log
30292  *              for retrieval via faultinjection_ioctl for use in fault
30293  *              detection and recovery
30294  *
30295  *   Arguments: buf - the string to add to the log
30296  */
30297 
30298 static void
30299 sd_injection_log(char *buf, struct sd_lun *un)
30300 {
30301 	uint_t len;
30302 
30303 	ASSERT(un != NULL);
30304 	ASSERT(buf != NULL);
30305 
30306 	mutex_enter(&(un->un_fi_mutex));
30307 
30308 	len = min(strlen(buf), 255);
30309 	/* Add logged value to Injection log to be returned later */
30310 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30311 		uint_t	offset = strlen((char *)un->sd_fi_log);
30312 		char *destp = (char *)un->sd_fi_log + offset;
30313 		int i;
30314 		for (i = 0; i < len; i++) {
30315 			*destp++ = *buf++;
30316 		}
30317 		un->sd_fi_buf_len += len;
30318 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30319 	}
30320 
30321 	mutex_exit(&(un->un_fi_mutex));
30322 }
30323 
30324 
30325 /*
30326  *    Function: sd_faultinjection()
30327  *
30328  * Description: This routine takes the pkt and changes its
30329  *		content based on error injection scenerio.
30330  *
30331  *   Arguments: pktp	- packet to be changed
30332  */
30333 
30334 static void
30335 sd_faultinjection(struct scsi_pkt *pktp)
30336 {
30337 	uint_t i;
30338 	struct sd_fi_pkt *fi_pkt;
30339 	struct sd_fi_xb *fi_xb;
30340 	struct sd_fi_un *fi_un;
30341 	struct sd_fi_arq *fi_arq;
30342 	struct buf *bp;
30343 	struct sd_xbuf *xb;
30344 	struct sd_lun *un;
30345 
30346 	ASSERT(pktp != NULL);
30347 
30348 	/* pull bp xb and un from pktp */
30349 	bp = (struct buf *)pktp->pkt_private;
30350 	xb = SD_GET_XBUF(bp);
30351 	un = SD_GET_UN(bp);
30352 
30353 	ASSERT(un != NULL);
30354 
30355 	mutex_enter(SD_MUTEX(un));
30356 
30357 	SD_TRACE(SD_LOG_SDTEST, un,
30358 	    "sd_faultinjection: entry Injection from sdintr\n");
30359 
30360 	/* if injection is off return */
30361 	if (sd_fault_injection_on == 0 ||
30362 	    un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30363 		mutex_exit(SD_MUTEX(un));
30364 		return;
30365 	}
30366 
30367 	SD_INFO(SD_LOG_SDTEST, un,
30368 	    "sd_faultinjection: is working for copying\n");
30369 
30370 	/* take next set off fifo */
30371 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30372 
30373 	fi_pkt = un->sd_fi_fifo_pkt[i];
30374 	fi_xb = un->sd_fi_fifo_xb[i];
30375 	fi_un = un->sd_fi_fifo_un[i];
30376 	fi_arq = un->sd_fi_fifo_arq[i];
30377 
30378 
30379 	/* set variables accordingly */
30380 	/* set pkt if it was on fifo */
30381 	if (fi_pkt != NULL) {
30382 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30383 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30384 		if (fi_pkt->pkt_cdbp != 0xff)
30385 			SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30386 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30387 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30388 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30389 
30390 	}
30391 	/* set xb if it was on fifo */
30392 	if (fi_xb != NULL) {
30393 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30394 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30395 		if (fi_xb->xb_retry_count != 0)
30396 			SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30397 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30398 		    "xb_victim_retry_count");
30399 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30400 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30401 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30402 
30403 		/* copy in block data from sense */
30404 		/*
30405 		 * if (fi_xb->xb_sense_data[0] != -1) {
30406 		 *	bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30407 		 *	SENSE_LENGTH);
30408 		 * }
30409 		 */
30410 		bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30411 
30412 		/* copy in extended sense codes */
30413 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30414 		    xb, es_code, "es_code");
30415 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30416 		    xb, es_key, "es_key");
30417 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30418 		    xb, es_add_code, "es_add_code");
30419 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30420 		    xb, es_qual_code, "es_qual_code");
30421 		struct scsi_extended_sense *esp;
30422 		esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30423 		esp->es_class = CLASS_EXTENDED_SENSE;
30424 	}
30425 
30426 	/* set un if it was on fifo */
30427 	if (fi_un != NULL) {
30428 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30429 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30430 		SD_CONDSET(un, un, un_reset_retry_count,
30431 		    "un_reset_retry_count");
30432 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30433 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30434 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30435 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30436 		    "un_f_allow_bus_device_reset");
30437 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30438 
30439 	}
30440 
30441 	/* copy in auto request sense if it was on fifo */
30442 	if (fi_arq != NULL) {
30443 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30444 	}
30445 
30446 	/* free structs */
30447 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30448 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30449 	}
30450 	if (un->sd_fi_fifo_xb[i] != NULL) {
30451 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30452 	}
30453 	if (un->sd_fi_fifo_un[i] != NULL) {
30454 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30455 	}
30456 	if (un->sd_fi_fifo_arq[i] != NULL) {
30457 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30458 	}
30459 
30460 	/*
30461 	 * kmem_free does not gurantee to set to NULL
30462 	 * since we uses these to determine if we set
30463 	 * values or not lets confirm they are always
30464 	 * NULL after free
30465 	 */
30466 	un->sd_fi_fifo_pkt[i] = NULL;
30467 	un->sd_fi_fifo_un[i] = NULL;
30468 	un->sd_fi_fifo_xb[i] = NULL;
30469 	un->sd_fi_fifo_arq[i] = NULL;
30470 
30471 	un->sd_fi_fifo_start++;
30472 
30473 	mutex_exit(SD_MUTEX(un));
30474 
30475 	SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30476 }
30477 
30478 #endif /* SD_FAULT_INJECTION */
30479 
30480 /*
30481  * This routine is invoked in sd_unit_attach(). Before calling it, the
30482  * properties in conf file should be processed already, and "hotpluggable"
30483  * property was processed also.
30484  *
30485  * The sd driver distinguishes 3 different type of devices: removable media,
30486  * non-removable media, and hotpluggable. Below the differences are defined:
30487  *
30488  * 1. Device ID
30489  *
30490  *     The device ID of a device is used to identify this device. Refer to
30491  *     ddi_devid_register(9F).
30492  *
30493  *     For a non-removable media disk device which can provide 0x80 or 0x83
30494  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30495  *     device ID is created to identify this device. For other non-removable
30496  *     media devices, a default device ID is created only if this device has
30497  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30498  *
30499  *     -------------------------------------------------------
30500  *     removable media   hotpluggable  | Can Have Device ID
30501  *     -------------------------------------------------------
30502  *         false             false     |     Yes
30503  *         false             true      |     Yes
30504  *         true                x       |     No
30505  *     ------------------------------------------------------
30506  *
30507  *
30508  * 2. SCSI group 4 commands
30509  *
30510  *     In SCSI specs, only some commands in group 4 command set can use
30511  *     8-byte addresses that can be used to access >2TB storage spaces.
30512  *     Other commands have no such capability. Without supporting group4,
30513  *     it is impossible to make full use of storage spaces of a disk with
30514  *     capacity larger than 2TB.
30515  *
30516  *     -----------------------------------------------
30517  *     removable media   hotpluggable   LP64  |  Group
30518  *     -----------------------------------------------
30519  *           false          false       false |   1
30520  *           false          false       true  |   4
30521  *           false          true        false |   1
30522  *           false          true        true  |   4
30523  *           true             x           x   |   5
30524  *     -----------------------------------------------
30525  *
30526  *
30527  * 3. Check for VTOC Label
30528  *
30529  *     If a direct-access disk has no EFI label, sd will check if it has a
30530  *     valid VTOC label. Now, sd also does that check for removable media
30531  *     and hotpluggable devices.
30532  *
30533  *     --------------------------------------------------------------
30534  *     Direct-Access   removable media    hotpluggable |  Check Label
30535  *     -------------------------------------------------------------
30536  *         false          false           false        |   No
30537  *         false          false           true         |   No
30538  *         false          true            false        |   Yes
30539  *         false          true            true         |   Yes
30540  *         true            x                x          |   Yes
30541  *     --------------------------------------------------------------
30542  *
30543  *
30544  * 4. Building default VTOC label
30545  *
30546  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30547  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30548  *     create default VTOC for them. Currently sd creates default VTOC label
30549  *     for all devices on x86 platform (VTOC_16), but only for removable
30550  *     media devices on SPARC (VTOC_8).
30551  *
30552  *     -----------------------------------------------------------
30553  *       removable media hotpluggable platform   |   Default Label
30554  *     -----------------------------------------------------------
30555  *             false          false    sparc     |     No
30556  *             false          true      x86      |     Yes
30557  *             false          true     sparc     |     Yes
30558  *             true             x        x       |     Yes
30559  *     ----------------------------------------------------------
30560  *
30561  *
30562  * 5. Supported blocksizes of target devices
30563  *
30564  *     Sd supports non-512-byte blocksize for removable media devices only.
30565  *     For other devices, only 512-byte blocksize is supported. This may be
30566  *     changed in near future because some RAID devices require non-512-byte
30567  *     blocksize
30568  *
30569  *     -----------------------------------------------------------
30570  *     removable media    hotpluggable    | non-512-byte blocksize
30571  *     -----------------------------------------------------------
30572  *           false          false         |   No
30573  *           false          true          |   No
30574  *           true             x           |   Yes
30575  *     -----------------------------------------------------------
30576  *
30577  *
30578  * 6. Automatic mount & unmount
30579  *
30580  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30581  *     if a device is removable media device. It return 1 for removable media
30582  *     devices, and 0 for others.
30583  *
30584  *     The automatic mounting subsystem should distinguish between the types
30585  *     of devices and apply automounting policies to each.
30586  *
30587  *
30588  * 7. fdisk partition management
30589  *
30590  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30591  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30592  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30593  *     fdisk partitions on both x86 and SPARC platform.
30594  *
30595  *     -----------------------------------------------------------
30596  *       platform   removable media  USB/1394  |  fdisk supported
30597  *     -----------------------------------------------------------
30598  *        x86         X               X        |       true
30599  *     ------------------------------------------------------------
30600  *        sparc       X               X        |       false
30601  *     ------------------------------------------------------------
30602  *
30603  *
30604  * 8. MBOOT/MBR
30605  *
30606  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30607  *     read/write mboot for removable media devices on sparc platform.
30608  *
30609  *     -----------------------------------------------------------
30610  *       platform   removable media  USB/1394  |  mboot supported
30611  *     -----------------------------------------------------------
30612  *        x86         X               X        |       true
30613  *     ------------------------------------------------------------
30614  *        sparc      false           false     |       false
30615  *        sparc      false           true      |       true
30616  *        sparc      true            false     |       true
30617  *        sparc      true            true      |       true
30618  *     ------------------------------------------------------------
30619  *
30620  *
30621  * 9.  error handling during opening device
30622  *
30623  *     If failed to open a disk device, an errno is returned. For some kinds
30624  *     of errors, different errno is returned depending on if this device is
30625  *     a removable media device. This brings USB/1394 hard disks in line with
30626  *     expected hard disk behavior. It is not expected that this breaks any
30627  *     application.
30628  *
30629  *     ------------------------------------------------------
30630  *       removable media    hotpluggable   |  errno
30631  *     ------------------------------------------------------
30632  *             false          false        |   EIO
30633  *             false          true         |   EIO
30634  *             true             x          |   ENXIO
30635  *     ------------------------------------------------------
30636  *
30637  *
30638  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30639  *
30640  *     These IOCTLs are applicable only to removable media devices.
30641  *
30642  *     -----------------------------------------------------------
30643  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30644  *     -----------------------------------------------------------
30645  *             false          false        |     No
30646  *             false          true         |     No
30647  *             true            x           |     Yes
30648  *     -----------------------------------------------------------
30649  *
30650  *
30651  * 12. Kstats for partitions
30652  *
30653  *     sd creates partition kstat for non-removable media devices. USB and
30654  *     Firewire hard disks now have partition kstats
30655  *
30656  *      ------------------------------------------------------
30657  *       removable media    hotpluggable   |   kstat
30658  *      ------------------------------------------------------
30659  *             false          false        |    Yes
30660  *             false          true         |    Yes
30661  *             true             x          |    No
30662  *       ------------------------------------------------------
30663  *
30664  *
30665  * 13. Removable media & hotpluggable properties
30666  *
30667  *     Sd driver creates a "removable-media" property for removable media
30668  *     devices. Parent nexus drivers create a "hotpluggable" property if
30669  *     it supports hotplugging.
30670  *
30671  *     ---------------------------------------------------------------------
30672  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30673  *     ---------------------------------------------------------------------
30674  *       false            false       |    No                   No
30675  *       false            true        |    No                   Yes
30676  *       true             false       |    Yes                  No
30677  *       true             true        |    Yes                  Yes
30678  *     ---------------------------------------------------------------------
30679  *
30680  *
30681  * 14. Power Management
30682  *
30683  *     sd only power manages removable media devices or devices that support
30684  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30685  *
30686  *     A parent nexus that supports hotplugging can also set "pm-capable"
30687  *     if the disk can be power managed.
30688  *
30689  *     ------------------------------------------------------------
30690  *       removable media hotpluggable pm-capable  |   power manage
30691  *     ------------------------------------------------------------
30692  *             false          false     false     |     No
30693  *             false          false     true      |     Yes
30694  *             false          true      false     |     No
30695  *             false          true      true      |     Yes
30696  *             true             x        x        |     Yes
30697  *     ------------------------------------------------------------
30698  *
30699  *      USB and firewire hard disks can now be power managed independently
30700  *      of the framebuffer
30701  *
30702  *
30703  * 15. Support for USB disks with capacity larger than 1TB
30704  *
30705  *     Currently, sd doesn't permit a fixed disk device with capacity
30706  *     larger than 1TB to be used in a 32-bit operating system environment.
30707  *     However, sd doesn't do that for removable media devices. Instead, it
30708  *     assumes that removable media devices cannot have a capacity larger
30709  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30710  *     supported, which can cause some unexpected results.
30711  *
30712  *     ---------------------------------------------------------------------
30713  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30714  *     ---------------------------------------------------------------------
30715  *             false          false  |   true         |     no
30716  *             false          true   |   true         |     no
30717  *             true           false  |   true         |     Yes
30718  *             true           true   |   true         |     Yes
30719  *     ---------------------------------------------------------------------
30720  *
30721  *
30722  * 16. Check write-protection at open time
30723  *
30724  *     When a removable media device is being opened for writing without NDELAY
30725  *     flag, sd will check if this device is writable. If attempting to open
30726  *     without NDELAY flag a write-protected device, this operation will abort.
30727  *
30728  *     ------------------------------------------------------------
30729  *       removable media    USB/1394   |   WP Check
30730  *     ------------------------------------------------------------
30731  *             false          false    |     No
30732  *             false          true     |     No
30733  *             true           false    |     Yes
30734  *             true           true     |     Yes
30735  *     ------------------------------------------------------------
30736  *
30737  *
30738  * 17. syslog when corrupted VTOC is encountered
30739  *
30740  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30741  *      for fixed SCSI disks.
30742  *     ------------------------------------------------------------
30743  *       removable media    USB/1394   |   print syslog
30744  *     ------------------------------------------------------------
30745  *             false          false    |     Yes
30746  *             false          true     |     No
30747  *             true           false    |     No
30748  *             true           true     |     No
30749  *     ------------------------------------------------------------
30750  */
30751 static void
30752 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30753 {
30754 	int	pm_cap;
30755 
30756 	ASSERT(un->un_sd);
30757 	ASSERT(un->un_sd->sd_inq);
30758 
30759 	/*
30760 	 * Enable SYNC CACHE support for all devices.
30761 	 */
30762 	un->un_f_sync_cache_supported = TRUE;
30763 
30764 	/*
30765 	 * Set the sync cache required flag to false.
30766 	 * This would ensure that there is no SYNC CACHE
30767 	 * sent when there are no writes
30768 	 */
30769 	un->un_f_sync_cache_required = FALSE;
30770 
30771 	if (un->un_sd->sd_inq->inq_rmb) {
30772 		/*
30773 		 * The media of this device is removable. And for this kind
30774 		 * of devices, it is possible to change medium after opening
30775 		 * devices. Thus we should support this operation.
30776 		 */
30777 		un->un_f_has_removable_media = TRUE;
30778 
30779 		/*
30780 		 * support non-512-byte blocksize of removable media devices
30781 		 */
30782 		un->un_f_non_devbsize_supported = TRUE;
30783 
30784 		/*
30785 		 * Assume that all removable media devices support DOOR_LOCK
30786 		 */
30787 		un->un_f_doorlock_supported = TRUE;
30788 
30789 		/*
30790 		 * For a removable media device, it is possible to be opened
30791 		 * with NDELAY flag when there is no media in drive, in this
30792 		 * case we don't care if device is writable. But if without
30793 		 * NDELAY flag, we need to check if media is write-protected.
30794 		 */
30795 		un->un_f_chk_wp_open = TRUE;
30796 
30797 		/*
30798 		 * need to start a SCSI watch thread to monitor media state,
30799 		 * when media is being inserted or ejected, notify syseventd.
30800 		 */
30801 		un->un_f_monitor_media_state = TRUE;
30802 
30803 		/*
30804 		 * Some devices don't support START_STOP_UNIT command.
30805 		 * Therefore, we'd better check if a device supports it
30806 		 * before sending it.
30807 		 */
30808 		un->un_f_check_start_stop = TRUE;
30809 
30810 		/*
30811 		 * support eject media ioctl:
30812 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
30813 		 */
30814 		un->un_f_eject_media_supported = TRUE;
30815 
30816 		/*
30817 		 * Because many removable-media devices don't support
30818 		 * LOG_SENSE, we couldn't use this command to check if
30819 		 * a removable media device support power-management.
30820 		 * We assume that they support power-management via
30821 		 * START_STOP_UNIT command and can be spun up and down
30822 		 * without limitations.
30823 		 */
30824 		un->un_f_pm_supported = TRUE;
30825 
30826 		/*
30827 		 * Need to create a zero length (Boolean) property
30828 		 * removable-media for the removable media devices.
30829 		 * Note that the return value of the property is not being
30830 		 * checked, since if unable to create the property
30831 		 * then do not want the attach to fail altogether. Consistent
30832 		 * with other property creation in attach.
30833 		 */
30834 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30835 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30836 
30837 	} else {
30838 		/*
30839 		 * create device ID for device
30840 		 */
30841 		un->un_f_devid_supported = TRUE;
30842 
30843 		/*
30844 		 * Spin up non-removable-media devices once it is attached
30845 		 */
30846 		un->un_f_attach_spinup = TRUE;
30847 
30848 		/*
30849 		 * According to SCSI specification, Sense data has two kinds of
30850 		 * format: fixed format, and descriptor format. At present, we
30851 		 * don't support descriptor format sense data for removable
30852 		 * media.
30853 		 */
30854 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30855 			un->un_f_descr_format_supported = TRUE;
30856 		}
30857 
30858 		/*
30859 		 * kstats are created only for non-removable media devices.
30860 		 *
30861 		 * Set this in sd.conf to 0 in order to disable kstats.  The
30862 		 * default is 1, so they are enabled by default.
30863 		 */
30864 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30865 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
30866 		    "enable-partition-kstats", 1));
30867 
30868 		/*
30869 		 * Check if HBA has set the "pm-capable" property.
30870 		 * If "pm-capable" exists and is non-zero then we can
30871 		 * power manage the device without checking the start/stop
30872 		 * cycle count log sense page.
30873 		 *
30874 		 * If "pm-capable" exists and is set to be false (0),
30875 		 * then we should not power manage the device.
30876 		 *
30877 		 * If "pm-capable" doesn't exist then pm_cap will
30878 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
30879 		 * sd will check the start/stop cycle count log sense page
30880 		 * and power manage the device if the cycle count limit has
30881 		 * not been exceeded.
30882 		 */
30883 		pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30884 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30885 		if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30886 			un->un_f_log_sense_supported = TRUE;
30887 			if (!un->un_f_power_condition_disabled &&
30888 			    SD_INQUIRY(un)->inq_ansi == 6) {
30889 				un->un_f_power_condition_supported = TRUE;
30890 			}
30891 		} else {
30892 			/*
30893 			 * pm-capable property exists.
30894 			 *
30895 			 * Convert "TRUE" values for pm_cap to
30896 			 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30897 			 * later. "TRUE" values are any values defined in
30898 			 * inquiry.h.
30899 			 */
30900 			if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30901 				un->un_f_log_sense_supported = FALSE;
30902 			} else {
30903 				/* SD_PM_CAPABLE_IS_TRUE case */
30904 				un->un_f_pm_supported = TRUE;
30905 				if (!un->un_f_power_condition_disabled &&
30906 				    SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30907 					un->un_f_power_condition_supported =
30908 					    TRUE;
30909 				}
30910 				if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30911 					un->un_f_log_sense_supported = TRUE;
30912 					un->un_f_pm_log_sense_smart =
30913 					    SD_PM_CAP_SMART_LOG(pm_cap);
30914 				}
30915 			}
30916 
30917 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
30918 			    "sd_unit_attach: un:0x%p pm-capable "
30919 			    "property set to %d.\n", un, un->un_f_pm_supported);
30920 		}
30921 	}
30922 
30923 	if (un->un_f_is_hotpluggable) {
30924 
30925 		/*
30926 		 * Have to watch hotpluggable devices as well, since
30927 		 * that's the only way for userland applications to
30928 		 * detect hot removal while device is busy/mounted.
30929 		 */
30930 		un->un_f_monitor_media_state = TRUE;
30931 
30932 		un->un_f_check_start_stop = TRUE;
30933 
30934 	}
30935 }
30936 
30937 /*
30938  * sd_tg_rdwr:
30939  * Provides rdwr access for cmlb via sd_tgops. The start_block is
30940  * in sys block size, req_length in bytes.
30941  *
30942  */
30943 static int
30944 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
30945     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
30946 {
30947 	struct sd_lun *un;
30948 	int path_flag = (int)(uintptr_t)tg_cookie;
30949 	char *dkl = NULL;
30950 	diskaddr_t real_addr = start_block;
30951 	diskaddr_t first_byte, end_block;
30952 
30953 	size_t	buffer_size = reqlength;
30954 	int rval = 0;
30955 	diskaddr_t	cap;
30956 	uint32_t	lbasize;
30957 	sd_ssc_t	*ssc;
30958 
30959 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
30960 	if (un == NULL)
30961 		return (ENXIO);
30962 
30963 	if (cmd != TG_READ && cmd != TG_WRITE)
30964 		return (EINVAL);
30965 
30966 	ssc = sd_ssc_init(un);
30967 	mutex_enter(SD_MUTEX(un));
30968 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
30969 		mutex_exit(SD_MUTEX(un));
30970 		rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
30971 		    &lbasize, path_flag);
30972 		if (rval != 0)
30973 			goto done1;
30974 		mutex_enter(SD_MUTEX(un));
30975 		sd_update_block_info(un, lbasize, cap);
30976 		if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
30977 			mutex_exit(SD_MUTEX(un));
30978 			rval = EIO;
30979 			goto done;
30980 		}
30981 	}
30982 
30983 	if (NOT_DEVBSIZE(un)) {
30984 		/*
30985 		 * sys_blocksize != tgt_blocksize, need to re-adjust
30986 		 * blkno and save the index to beginning of dk_label
30987 		 */
30988 		first_byte  = SD_SYSBLOCKS2BYTES(start_block);
30989 		real_addr = first_byte / un->un_tgt_blocksize;
30990 
30991 		end_block = (first_byte + reqlength +
30992 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
30993 
30994 		/* round up buffer size to multiple of target block size */
30995 		buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
30996 
30997 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
30998 		    "label_addr: 0x%x allocation size: 0x%x\n",
30999 		    real_addr, buffer_size);
31000 
31001 		if (((first_byte % un->un_tgt_blocksize) != 0) ||
31002 		    (reqlength % un->un_tgt_blocksize) != 0)
31003 			/* the request is not aligned */
31004 			dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31005 	}
31006 
31007 	/*
31008 	 * The MMC standard allows READ CAPACITY to be
31009 	 * inaccurate by a bounded amount (in the interest of
31010 	 * response latency).  As a result, failed READs are
31011 	 * commonplace (due to the reading of metadata and not
31012 	 * data). Depending on the per-Vendor/drive Sense data,
31013 	 * the failed READ can cause many (unnecessary) retries.
31014 	 */
31015 
31016 	if (ISCD(un) && (cmd == TG_READ) &&
31017 	    (un->un_f_blockcount_is_valid == TRUE) &&
31018 	    ((start_block == (un->un_blockcount - 1))||
31019 	    (start_block == (un->un_blockcount - 2)))) {
31020 			path_flag = SD_PATH_DIRECT_PRIORITY;
31021 	}
31022 
31023 	mutex_exit(SD_MUTEX(un));
31024 	if (cmd == TG_READ) {
31025 		rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31026 		    buffer_size, real_addr, path_flag);
31027 		if (dkl != NULL)
31028 			bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31029 			    real_addr), bufaddr, reqlength);
31030 	} else {
31031 		if (dkl) {
31032 			rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31033 			    real_addr, path_flag);
31034 			if (rval) {
31035 				goto done1;
31036 			}
31037 			bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31038 			    real_addr), reqlength);
31039 		}
31040 		rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31041 		    buffer_size, real_addr, path_flag);
31042 	}
31043 
31044 done1:
31045 	if (dkl != NULL)
31046 		kmem_free(dkl, buffer_size);
31047 
31048 	if (rval != 0) {
31049 		if (rval == EIO)
31050 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31051 		else
31052 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31053 	}
31054 done:
31055 	sd_ssc_fini(ssc);
31056 	return (rval);
31057 }
31058 
31059 
31060 static int
31061 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31062 {
31063 
31064 	struct sd_lun *un;
31065 	diskaddr_t	cap;
31066 	uint32_t	lbasize;
31067 	int		path_flag = (int)(uintptr_t)tg_cookie;
31068 	int		ret = 0;
31069 
31070 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31071 	if (un == NULL)
31072 		return (ENXIO);
31073 
31074 	switch (cmd) {
31075 	case TG_GETPHYGEOM:
31076 	case TG_GETVIRTGEOM:
31077 	case TG_GETCAPACITY:
31078 	case TG_GETBLOCKSIZE:
31079 		mutex_enter(SD_MUTEX(un));
31080 
31081 		if ((un->un_f_blockcount_is_valid == TRUE) &&
31082 		    (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31083 			cap = un->un_blockcount;
31084 			lbasize = un->un_tgt_blocksize;
31085 			mutex_exit(SD_MUTEX(un));
31086 		} else {
31087 			sd_ssc_t	*ssc;
31088 			mutex_exit(SD_MUTEX(un));
31089 			ssc = sd_ssc_init(un);
31090 			ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31091 			    &lbasize, path_flag);
31092 			if (ret != 0) {
31093 				if (ret == EIO)
31094 					sd_ssc_assessment(ssc,
31095 					    SD_FMT_STATUS_CHECK);
31096 				else
31097 					sd_ssc_assessment(ssc,
31098 					    SD_FMT_IGNORE);
31099 				sd_ssc_fini(ssc);
31100 				return (ret);
31101 			}
31102 			sd_ssc_fini(ssc);
31103 			mutex_enter(SD_MUTEX(un));
31104 			sd_update_block_info(un, lbasize, cap);
31105 			if ((un->un_f_blockcount_is_valid == FALSE) ||
31106 			    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31107 				mutex_exit(SD_MUTEX(un));
31108 				return (EIO);
31109 			}
31110 			mutex_exit(SD_MUTEX(un));
31111 		}
31112 
31113 		if (cmd == TG_GETCAPACITY) {
31114 			*(diskaddr_t *)arg = cap;
31115 			return (0);
31116 		}
31117 
31118 		if (cmd == TG_GETBLOCKSIZE) {
31119 			*(uint32_t *)arg = lbasize;
31120 			return (0);
31121 		}
31122 
31123 		if (cmd == TG_GETPHYGEOM)
31124 			ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31125 			    cap, lbasize, path_flag);
31126 		else
31127 			/* TG_GETVIRTGEOM */
31128 			ret = sd_get_virtual_geometry(un,
31129 			    (cmlb_geom_t *)arg, cap, lbasize);
31130 
31131 		return (ret);
31132 
31133 	case TG_GETATTR:
31134 		mutex_enter(SD_MUTEX(un));
31135 		((tg_attribute_t *)arg)->media_is_writable =
31136 		    un->un_f_mmc_writable_media;
31137 		((tg_attribute_t *)arg)->media_is_solid_state =
31138 		    un->un_f_is_solid_state;
31139 		mutex_exit(SD_MUTEX(un));
31140 		return (0);
31141 	default:
31142 		return (ENOTTY);
31143 
31144 	}
31145 }
31146 
31147 /*
31148  *    Function: sd_ssc_ereport_post
31149  *
31150  * Description: Will be called when SD driver need to post an ereport.
31151  *
31152  *    Context: Kernel thread or interrupt context.
31153  */
31154 
31155 #define	DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31156 
31157 static void
31158 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31159 {
31160 	int uscsi_path_instance = 0;
31161 	uchar_t	uscsi_pkt_reason;
31162 	uint32_t uscsi_pkt_state;
31163 	uint32_t uscsi_pkt_statistics;
31164 	uint64_t uscsi_ena;
31165 	uchar_t op_code;
31166 	uint8_t *sensep;
31167 	union scsi_cdb *cdbp;
31168 	uint_t cdblen = 0;
31169 	uint_t senlen = 0;
31170 	struct sd_lun *un;
31171 	dev_info_t *dip;
31172 	char *devid;
31173 	int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31174 	    SSC_FLAGS_INVALID_STATUS |
31175 	    SSC_FLAGS_INVALID_SENSE |
31176 	    SSC_FLAGS_INVALID_DATA;
31177 	char assessment[16];
31178 
31179 	ASSERT(ssc != NULL);
31180 	ASSERT(ssc->ssc_uscsi_cmd != NULL);
31181 	ASSERT(ssc->ssc_uscsi_info != NULL);
31182 
31183 	un = ssc->ssc_un;
31184 	ASSERT(un != NULL);
31185 
31186 	dip = un->un_sd->sd_dev;
31187 
31188 	/*
31189 	 * Get the devid:
31190 	 *	devid will only be passed to non-transport error reports.
31191 	 */
31192 	devid = DEVI(dip)->devi_devid_str;
31193 
31194 	/*
31195 	 * If we are syncing or dumping, the command will not be executed
31196 	 * so we bypass this situation.
31197 	 */
31198 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31199 	    (un->un_state == SD_STATE_DUMPING))
31200 		return;
31201 
31202 	uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31203 	uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31204 	uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31205 	uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31206 	uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31207 
31208 	sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31209 	cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31210 
31211 	/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31212 	if (cdbp == NULL) {
31213 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31214 		    "sd_ssc_ereport_post meet empty cdb\n");
31215 		return;
31216 	}
31217 
31218 	op_code = cdbp->scc_cmd;
31219 
31220 	cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31221 	senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31222 	    ssc->ssc_uscsi_cmd->uscsi_rqresid);
31223 
31224 	if (senlen > 0)
31225 		ASSERT(sensep != NULL);
31226 
31227 	/*
31228 	 * Initialize drv_assess to corresponding values.
31229 	 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31230 	 * on the sense-key returned back.
31231 	 */
31232 	switch (drv_assess) {
31233 		case SD_FM_DRV_RECOVERY:
31234 			(void) sprintf(assessment, "%s", "recovered");
31235 			break;
31236 		case SD_FM_DRV_RETRY:
31237 			(void) sprintf(assessment, "%s", "retry");
31238 			break;
31239 		case SD_FM_DRV_NOTICE:
31240 			(void) sprintf(assessment, "%s", "info");
31241 			break;
31242 		case SD_FM_DRV_FATAL:
31243 		default:
31244 			(void) sprintf(assessment, "%s", "unknown");
31245 	}
31246 	/*
31247 	 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31248 	 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31249 	 * driver-assessment will always be "recovered" here.
31250 	 */
31251 	if (drv_assess == SD_FM_DRV_RECOVERY) {
31252 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31253 		    "cmd.disk.recovered", uscsi_ena, devid, NULL,
31254 		    DDI_NOSLEEP, NULL,
31255 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31256 		    DEVID_IF_KNOWN(devid),
31257 		    "driver-assessment", DATA_TYPE_STRING, assessment,
31258 		    "op-code", DATA_TYPE_UINT8, op_code,
31259 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31260 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31261 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31262 		    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31263 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31264 		    NULL);
31265 		return;
31266 	}
31267 
31268 	/*
31269 	 * If there is un-expected/un-decodable data, we should post
31270 	 * ereport.io.scsi.cmd.disk.dev.uderr.
31271 	 * driver-assessment will be set based on parameter drv_assess.
31272 	 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31273 	 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31274 	 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31275 	 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31276 	 */
31277 	if (ssc->ssc_flags & ssc_invalid_flags) {
31278 		if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31279 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31280 			    NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31281 			    NULL, DDI_NOSLEEP, NULL,
31282 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31283 			    DEVID_IF_KNOWN(devid),
31284 			    "driver-assessment", DATA_TYPE_STRING,
31285 			    drv_assess == SD_FM_DRV_FATAL ?
31286 			    "fail" : assessment,
31287 			    "op-code", DATA_TYPE_UINT8, op_code,
31288 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31289 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31290 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31291 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31292 			    "pkt-stats", DATA_TYPE_UINT32,
31293 			    uscsi_pkt_statistics,
31294 			    "stat-code", DATA_TYPE_UINT8,
31295 			    ssc->ssc_uscsi_cmd->uscsi_status,
31296 			    "un-decode-info", DATA_TYPE_STRING,
31297 			    ssc->ssc_info,
31298 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31299 			    senlen, sensep,
31300 			    NULL);
31301 		} else {
31302 			/*
31303 			 * For other type of invalid data, the
31304 			 * un-decode-value field would be empty because the
31305 			 * un-decodable content could be seen from upper
31306 			 * level payload or inside un-decode-info.
31307 			 */
31308 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31309 			    NULL,
31310 			    "cmd.disk.dev.uderr", uscsi_ena, devid,
31311 			    NULL, DDI_NOSLEEP, NULL,
31312 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31313 			    DEVID_IF_KNOWN(devid),
31314 			    "driver-assessment", DATA_TYPE_STRING,
31315 			    drv_assess == SD_FM_DRV_FATAL ?
31316 			    "fail" : assessment,
31317 			    "op-code", DATA_TYPE_UINT8, op_code,
31318 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31319 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31320 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31321 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31322 			    "pkt-stats", DATA_TYPE_UINT32,
31323 			    uscsi_pkt_statistics,
31324 			    "stat-code", DATA_TYPE_UINT8,
31325 			    ssc->ssc_uscsi_cmd->uscsi_status,
31326 			    "un-decode-info", DATA_TYPE_STRING,
31327 			    ssc->ssc_info,
31328 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31329 			    0, NULL,
31330 			    NULL);
31331 		}
31332 		ssc->ssc_flags &= ~ssc_invalid_flags;
31333 		return;
31334 	}
31335 
31336 	if (uscsi_pkt_reason != CMD_CMPLT ||
31337 	    (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31338 		/*
31339 		 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31340 		 * set inside sd_start_cmds due to errors(bad packet or
31341 		 * fatal transport error), we should take it as a
31342 		 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31343 		 * driver-assessment will be set based on drv_assess.
31344 		 * We will set devid to NULL because it is a transport
31345 		 * error.
31346 		 */
31347 		if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31348 			ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31349 
31350 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31351 		    "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31352 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31353 		    DEVID_IF_KNOWN(devid),
31354 		    "driver-assessment", DATA_TYPE_STRING,
31355 		    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31356 		    "op-code", DATA_TYPE_UINT8, op_code,
31357 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31358 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31359 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31360 		    "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31361 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31362 		    NULL);
31363 	} else {
31364 		/*
31365 		 * If we got here, we have a completed command, and we need
31366 		 * to further investigate the sense data to see what kind
31367 		 * of ereport we should post.
31368 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr
31369 		 * if sense-key == 0x3.
31370 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31371 		 * driver-assessment will be set based on the parameter
31372 		 * drv_assess.
31373 		 */
31374 		if (senlen > 0) {
31375 			/*
31376 			 * Here we have sense data available.
31377 			 */
31378 			uint8_t sense_key;
31379 			sense_key = scsi_sense_key(sensep);
31380 			if (sense_key == 0x3) {
31381 				/*
31382 				 * sense-key == 0x3(medium error),
31383 				 * driver-assessment should be "fatal" if
31384 				 * drv_assess is SD_FM_DRV_FATAL.
31385 				 */
31386 				scsi_fm_ereport_post(un->un_sd,
31387 				    uscsi_path_instance, NULL,
31388 				    "cmd.disk.dev.rqs.merr",
31389 				    uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31390 				    FM_VERSION, DATA_TYPE_UINT8,
31391 				    FM_EREPORT_VERS0,
31392 				    DEVID_IF_KNOWN(devid),
31393 				    "driver-assessment",
31394 				    DATA_TYPE_STRING,
31395 				    drv_assess == SD_FM_DRV_FATAL ?
31396 				    "fatal" : assessment,
31397 				    "op-code",
31398 				    DATA_TYPE_UINT8, op_code,
31399 				    "cdb",
31400 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31401 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31402 				    "pkt-reason",
31403 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31404 				    "pkt-state",
31405 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31406 				    "pkt-stats",
31407 				    DATA_TYPE_UINT32,
31408 				    uscsi_pkt_statistics,
31409 				    "stat-code",
31410 				    DATA_TYPE_UINT8,
31411 				    ssc->ssc_uscsi_cmd->uscsi_status,
31412 				    "key",
31413 				    DATA_TYPE_UINT8,
31414 				    scsi_sense_key(sensep),
31415 				    "asc",
31416 				    DATA_TYPE_UINT8,
31417 				    scsi_sense_asc(sensep),
31418 				    "ascq",
31419 				    DATA_TYPE_UINT8,
31420 				    scsi_sense_ascq(sensep),
31421 				    "sense-data",
31422 				    DATA_TYPE_UINT8_ARRAY,
31423 				    senlen, sensep,
31424 				    "lba",
31425 				    DATA_TYPE_UINT64,
31426 				    ssc->ssc_uscsi_info->ui_lba,
31427 				    NULL);
31428 				} else {
31429 					/*
31430 					 * if sense-key == 0x4(hardware
31431 					 * error), driver-assessment should
31432 					 * be "fatal" if drv_assess is
31433 					 * SD_FM_DRV_FATAL.
31434 					 */
31435 					scsi_fm_ereport_post(un->un_sd,
31436 					    uscsi_path_instance, NULL,
31437 					    "cmd.disk.dev.rqs.derr",
31438 					    uscsi_ena, devid,
31439 					    NULL, DDI_NOSLEEP, NULL,
31440 					    FM_VERSION,
31441 					    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31442 					    DEVID_IF_KNOWN(devid),
31443 					    "driver-assessment",
31444 					    DATA_TYPE_STRING,
31445 					    drv_assess == SD_FM_DRV_FATAL ?
31446 					    (sense_key == 0x4 ?
31447 					    "fatal" : "fail") : assessment,
31448 					    "op-code",
31449 					    DATA_TYPE_UINT8, op_code,
31450 					    "cdb",
31451 					    DATA_TYPE_UINT8_ARRAY, cdblen,
31452 					    ssc->ssc_uscsi_cmd->uscsi_cdb,
31453 					    "pkt-reason",
31454 					    DATA_TYPE_UINT8, uscsi_pkt_reason,
31455 					    "pkt-state",
31456 					    DATA_TYPE_UINT8, uscsi_pkt_state,
31457 					    "pkt-stats",
31458 					    DATA_TYPE_UINT32,
31459 					    uscsi_pkt_statistics,
31460 					    "stat-code",
31461 					    DATA_TYPE_UINT8,
31462 					    ssc->ssc_uscsi_cmd->uscsi_status,
31463 					    "key",
31464 					    DATA_TYPE_UINT8,
31465 					    scsi_sense_key(sensep),
31466 					    "asc",
31467 					    DATA_TYPE_UINT8,
31468 					    scsi_sense_asc(sensep),
31469 					    "ascq",
31470 					    DATA_TYPE_UINT8,
31471 					    scsi_sense_ascq(sensep),
31472 					    "sense-data",
31473 					    DATA_TYPE_UINT8_ARRAY,
31474 					    senlen, sensep,
31475 					    NULL);
31476 				}
31477 		} else {
31478 			/*
31479 			 * For stat_code == STATUS_GOOD, this is not a
31480 			 * hardware error.
31481 			 */
31482 			if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31483 				return;
31484 
31485 			/*
31486 			 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31487 			 * stat-code but with sense data unavailable.
31488 			 * driver-assessment will be set based on parameter
31489 			 * drv_assess.
31490 			 */
31491 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31492 			    NULL,
31493 			    "cmd.disk.dev.serr", uscsi_ena,
31494 			    devid, NULL, DDI_NOSLEEP, NULL,
31495 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31496 			    DEVID_IF_KNOWN(devid),
31497 			    "driver-assessment", DATA_TYPE_STRING,
31498 			    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31499 			    "op-code", DATA_TYPE_UINT8, op_code,
31500 			    "cdb",
31501 			    DATA_TYPE_UINT8_ARRAY,
31502 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31503 			    "pkt-reason",
31504 			    DATA_TYPE_UINT8, uscsi_pkt_reason,
31505 			    "pkt-state",
31506 			    DATA_TYPE_UINT8, uscsi_pkt_state,
31507 			    "pkt-stats",
31508 			    DATA_TYPE_UINT32, uscsi_pkt_statistics,
31509 			    "stat-code",
31510 			    DATA_TYPE_UINT8,
31511 			    ssc->ssc_uscsi_cmd->uscsi_status,
31512 			    NULL);
31513 		}
31514 	}
31515 }
31516 
31517 /*
31518  *     Function: sd_ssc_extract_info
31519  *
31520  * Description: Extract information available to help generate ereport.
31521  *
31522  *     Context: Kernel thread or interrupt context.
31523  */
31524 static void
31525 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31526     struct buf *bp, struct sd_xbuf *xp)
31527 {
31528 	size_t senlen = 0;
31529 	union scsi_cdb *cdbp;
31530 	int path_instance;
31531 	/*
31532 	 * Need scsi_cdb_size array to determine the cdb length.
31533 	 */
31534 	extern uchar_t	scsi_cdb_size[];
31535 
31536 	ASSERT(un != NULL);
31537 	ASSERT(pktp != NULL);
31538 	ASSERT(bp != NULL);
31539 	ASSERT(xp != NULL);
31540 	ASSERT(ssc != NULL);
31541 	ASSERT(mutex_owned(SD_MUTEX(un)));
31542 
31543 	/*
31544 	 * Transfer the cdb buffer pointer here.
31545 	 */
31546 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31547 
31548 	ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31549 	ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31550 
31551 	/*
31552 	 * Transfer the sense data buffer pointer if sense data is available,
31553 	 * calculate the sense data length first.
31554 	 */
31555 	if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31556 	    (xp->xb_sense_state & STATE_ARQ_DONE)) {
31557 		/*
31558 		 * For arq case, we will enter here.
31559 		 */
31560 		if (xp->xb_sense_state & STATE_XARQ_DONE) {
31561 			senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31562 		} else {
31563 			senlen = SENSE_LENGTH;
31564 		}
31565 	} else {
31566 		/*
31567 		 * For non-arq case, we will enter this branch.
31568 		 */
31569 		if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31570 		    (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31571 			senlen = SENSE_LENGTH - xp->xb_sense_resid;
31572 		}
31573 
31574 	}
31575 
31576 	ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31577 	ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31578 	ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31579 
31580 	ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31581 
31582 	/*
31583 	 * Only transfer path_instance when scsi_pkt was properly allocated.
31584 	 */
31585 	path_instance = pktp->pkt_path_instance;
31586 	if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31587 		ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31588 	else
31589 		ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31590 
31591 	/*
31592 	 * Copy in the other fields we may need when posting ereport.
31593 	 */
31594 	ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31595 	ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31596 	ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31597 	ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31598 
31599 	/*
31600 	 * For partially read/write command, we will not create ena
31601 	 * in case of a successful command be reconized as recovered.
31602 	 */
31603 	if ((pktp->pkt_reason == CMD_CMPLT) &&
31604 	    (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31605 	    (senlen == 0)) {
31606 		return;
31607 	}
31608 
31609 	/*
31610 	 * To associate ereports of a single command execution flow, we
31611 	 * need a shared ena for a specific command.
31612 	 */
31613 	if (xp->xb_ena == 0)
31614 		xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31615 	ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31616 }
31617 
31618 
31619 /*
31620  *     Function: sd_check_solid_state
31621  *
31622  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31623  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31624  *              RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the
31625  *              device is a solid state drive.
31626  *
31627  *     Context: Kernel thread or interrupt context.
31628  */
31629 
31630 static void
31631 sd_check_solid_state(sd_ssc_t *ssc)
31632 {
31633 	int		rval		= 0;
31634 	uchar_t		*inqb1		= NULL;
31635 	size_t		inqb1_len	= MAX_INQUIRY_SIZE;
31636 	size_t		inqb1_resid	= 0;
31637 	struct sd_lun	*un;
31638 
31639 	ASSERT(ssc != NULL);
31640 	un = ssc->ssc_un;
31641 	ASSERT(un != NULL);
31642 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31643 
31644 	mutex_enter(SD_MUTEX(un));
31645 	un->un_f_is_solid_state = FALSE;
31646 
31647 	if (ISCD(un)) {
31648 		mutex_exit(SD_MUTEX(un));
31649 		return;
31650 	}
31651 
31652 	if (sd_check_vpd_page_support(ssc) == 0 &&
31653 	    un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31654 		mutex_exit(SD_MUTEX(un));
31655 		/* collect page b1 data */
31656 		inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31657 
31658 		rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31659 		    0x01, 0xB1, &inqb1_resid);
31660 
31661 		if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31662 			SD_TRACE(SD_LOG_COMMON, un,
31663 			    "sd_check_solid_state: \
31664 			    successfully get VPD page: %x \
31665 			    PAGE LENGTH: %x BYTE 4: %x \
31666 			    BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31667 			    inqb1[5]);
31668 
31669 			mutex_enter(SD_MUTEX(un));
31670 			/*
31671 			 * Check the MEDIUM ROTATION RATE. If it is set
31672 			 * to 1, the device is a solid state drive.
31673 			 */
31674 			if (inqb1[4] == 0 && inqb1[5] == 1) {
31675 				un->un_f_is_solid_state = TRUE;
31676 				/* solid state drives don't need disksort */
31677 				un->un_f_disksort_disabled = TRUE;
31678 			}
31679 			mutex_exit(SD_MUTEX(un));
31680 		} else if (rval != 0) {
31681 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31682 		}
31683 
31684 		kmem_free(inqb1, inqb1_len);
31685 	} else {
31686 		mutex_exit(SD_MUTEX(un));
31687 	}
31688 }
31689 
31690 /*
31691  *	Function: sd_check_emulation_mode
31692  *
31693  *   Description: Check whether the SSD is at emulation mode
31694  *		  by issuing READ_CAPACITY_16 to see whether
31695  *		  we can get physical block size of the drive.
31696  *
31697  *	 Context: Kernel thread or interrupt context.
31698  */
31699 
31700 static void
31701 sd_check_emulation_mode(sd_ssc_t *ssc)
31702 {
31703 	int		rval = 0;
31704 	uint64_t	capacity;
31705 	uint_t		lbasize;
31706 	uint_t		pbsize;
31707 	int		i;
31708 	int		devid_len;
31709 	struct sd_lun	*un;
31710 
31711 	ASSERT(ssc != NULL);
31712 	un = ssc->ssc_un;
31713 	ASSERT(un != NULL);
31714 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31715 
31716 	mutex_enter(SD_MUTEX(un));
31717 	if (ISCD(un)) {
31718 		mutex_exit(SD_MUTEX(un));
31719 		return;
31720 	}
31721 
31722 	if (un->un_f_descr_format_supported) {
31723 		mutex_exit(SD_MUTEX(un));
31724 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31725 		    &pbsize, SD_PATH_DIRECT);
31726 		mutex_enter(SD_MUTEX(un));
31727 
31728 		if (rval != 0) {
31729 			un->un_phy_blocksize = DEV_BSIZE;
31730 		} else {
31731 			if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31732 				un->un_phy_blocksize = DEV_BSIZE;
31733 			} else {
31734 				un->un_phy_blocksize = pbsize;
31735 			}
31736 		}
31737 	}
31738 
31739 	for (i = 0; i < sd_flash_dev_table_size; i++) {
31740 		devid_len = (int)strlen(sd_flash_dev_table[i]);
31741 		if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31742 		    == SD_SUCCESS) {
31743 			un->un_phy_blocksize = SSD_SECSIZE;
31744 			if (un->un_f_is_solid_state &&
31745 			    un->un_phy_blocksize != un->un_tgt_blocksize)
31746 				un->un_f_enable_rmw = TRUE;
31747 		}
31748 	}
31749 
31750 	mutex_exit(SD_MUTEX(un));
31751 }
31752