xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 1b4eec21)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 /*
26  * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
27  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
29  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
30  */
31 /*
32  * Copyright 2011 cyril.galibern@opensvc.com
33  */
34 
35 /*
36  * SCSI disk target driver.
37  */
38 #include <sys/scsi/scsi.h>
39 #include <sys/dkbad.h>
40 #include <sys/dklabel.h>
41 #include <sys/dkio.h>
42 #include <sys/fdio.h>
43 #include <sys/cdio.h>
44 #include <sys/mhd.h>
45 #include <sys/vtoc.h>
46 #include <sys/dktp/fdisk.h>
47 #include <sys/kstat.h>
48 #include <sys/vtrace.h>
49 #include <sys/note.h>
50 #include <sys/thread.h>
51 #include <sys/proc.h>
52 #include <sys/efi_partition.h>
53 #include <sys/var.h>
54 #include <sys/aio_req.h>
55 
56 #ifdef __lock_lint
57 #define	_LP64
58 #define	__amd64
59 #endif
60 
61 #if (defined(__fibre))
62 /* Note: is there a leadville version of the following? */
63 #include <sys/fc4/fcal_linkapp.h>
64 #endif
65 #include <sys/taskq.h>
66 #include <sys/uuid.h>
67 #include <sys/byteorder.h>
68 #include <sys/sdt.h>
69 
70 #include "sd_xbuf.h"
71 
72 #include <sys/scsi/targets/sddef.h>
73 #include <sys/cmlb.h>
74 #include <sys/sysevent/eventdefs.h>
75 #include <sys/sysevent/dev.h>
76 
77 #include <sys/fm/protocol.h>
78 
79 /*
80  * Loadable module info.
81  */
82 #if (defined(__fibre))
83 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
84 #else /* !__fibre */
85 #define	SD_MODULE_NAME	"SCSI Disk Driver"
86 #endif /* !__fibre */
87 
88 /*
89  * Define the interconnect type, to allow the driver to distinguish
90  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
91  *
92  * This is really for backward compatibility. In the future, the driver
93  * should actually check the "interconnect-type" property as reported by
94  * the HBA; however at present this property is not defined by all HBAs,
95  * so we will use this #define (1) to permit the driver to run in
96  * backward-compatibility mode; and (2) to print a notification message
97  * if an FC HBA does not support the "interconnect-type" property.  The
98  * behavior of the driver will be to assume parallel SCSI behaviors unless
99  * the "interconnect-type" property is defined by the HBA **AND** has a
100  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
101  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
102  * Channel behaviors (as per the old ssd).  (Note that the
103  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
104  * will result in the driver assuming parallel SCSI behaviors.)
105  *
106  * (see common/sys/scsi/impl/services.h)
107  *
108  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
109  * since some FC HBAs may already support that, and there is some code in
110  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
111  * default would confuse that code, and besides things should work fine
112  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
113  * "interconnect_type" property.
114  *
115  */
116 #if (defined(__fibre))
117 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
118 #else
119 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
120 #endif
121 
122 /*
123  * The name of the driver, established from the module name in _init.
124  */
125 static	char *sd_label			= NULL;
126 
127 /*
128  * Driver name is unfortunately prefixed on some driver.conf properties.
129  */
130 #if (defined(__fibre))
131 #define	sd_max_xfer_size		ssd_max_xfer_size
132 #define	sd_config_list			ssd_config_list
133 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
134 static	char *sd_config_list		= "ssd-config-list";
135 #else
136 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
137 static	char *sd_config_list		= "sd-config-list";
138 #endif
139 
140 /*
141  * Driver global variables
142  */
143 
144 #if (defined(__fibre))
145 /*
146  * These #defines are to avoid namespace collisions that occur because this
147  * code is currently used to compile two separate driver modules: sd and ssd.
148  * All global variables need to be treated this way (even if declared static)
149  * in order to allow the debugger to resolve the names properly.
150  * It is anticipated that in the near future the ssd module will be obsoleted,
151  * at which time this namespace issue should go away.
152  */
153 #define	sd_state			ssd_state
154 #define	sd_io_time			ssd_io_time
155 #define	sd_failfast_enable		ssd_failfast_enable
156 #define	sd_ua_retry_count		ssd_ua_retry_count
157 #define	sd_report_pfa			ssd_report_pfa
158 #define	sd_max_throttle			ssd_max_throttle
159 #define	sd_min_throttle			ssd_min_throttle
160 #define	sd_rot_delay			ssd_rot_delay
161 
162 #define	sd_retry_on_reservation_conflict	\
163 					ssd_retry_on_reservation_conflict
164 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
165 #define	sd_resv_conflict_name		ssd_resv_conflict_name
166 
167 #define	sd_component_mask		ssd_component_mask
168 #define	sd_level_mask			ssd_level_mask
169 #define	sd_debug_un			ssd_debug_un
170 #define	sd_error_level			ssd_error_level
171 
172 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
173 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
174 
175 #define	sd_tr				ssd_tr
176 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
177 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
178 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
179 #define	sd_check_media_time		ssd_check_media_time
180 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
181 #define	sd_label_mutex			ssd_label_mutex
182 #define	sd_detach_mutex			ssd_detach_mutex
183 #define	sd_log_buf			ssd_log_buf
184 #define	sd_log_mutex			ssd_log_mutex
185 
186 #define	sd_disk_table			ssd_disk_table
187 #define	sd_disk_table_size		ssd_disk_table_size
188 #define	sd_sense_mutex			ssd_sense_mutex
189 #define	sd_cdbtab			ssd_cdbtab
190 
191 #define	sd_cb_ops			ssd_cb_ops
192 #define	sd_ops				ssd_ops
193 #define	sd_additional_codes		ssd_additional_codes
194 #define	sd_tgops			ssd_tgops
195 
196 #define	sd_minor_data			ssd_minor_data
197 #define	sd_minor_data_efi		ssd_minor_data_efi
198 
199 #define	sd_tq				ssd_tq
200 #define	sd_wmr_tq			ssd_wmr_tq
201 #define	sd_taskq_name			ssd_taskq_name
202 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
203 #define	sd_taskq_minalloc		ssd_taskq_minalloc
204 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
205 
206 #define	sd_dump_format_string		ssd_dump_format_string
207 
208 #define	sd_iostart_chain		ssd_iostart_chain
209 #define	sd_iodone_chain			ssd_iodone_chain
210 
211 #define	sd_pm_idletime			ssd_pm_idletime
212 
213 #define	sd_force_pm_supported		ssd_force_pm_supported
214 
215 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
216 
217 #define	sd_ssc_init			ssd_ssc_init
218 #define	sd_ssc_send			ssd_ssc_send
219 #define	sd_ssc_fini			ssd_ssc_fini
220 #define	sd_ssc_assessment		ssd_ssc_assessment
221 #define	sd_ssc_post			ssd_ssc_post
222 #define	sd_ssc_print			ssd_ssc_print
223 #define	sd_ssc_ereport_post		ssd_ssc_ereport_post
224 #define	sd_ssc_set_info			ssd_ssc_set_info
225 #define	sd_ssc_extract_info		ssd_ssc_extract_info
226 
227 #endif
228 
229 #ifdef	SDDEBUG
230 int	sd_force_pm_supported		= 0;
231 #endif	/* SDDEBUG */
232 
233 void *sd_state				= NULL;
234 int sd_io_time				= SD_IO_TIME;
235 int sd_failfast_enable			= 1;
236 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
237 int sd_report_pfa			= 1;
238 int sd_max_throttle			= SD_MAX_THROTTLE;
239 int sd_min_throttle			= SD_MIN_THROTTLE;
240 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
241 int sd_qfull_throttle_enable		= TRUE;
242 
243 int sd_retry_on_reservation_conflict	= 1;
244 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
245 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
246 
247 static int sd_dtype_optical_bind	= -1;
248 
249 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
250 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
251 
252 /*
253  * Global data for debug logging. To enable debug printing, sd_component_mask
254  * and sd_level_mask should be set to the desired bit patterns as outlined in
255  * sddef.h.
256  */
257 uint_t	sd_component_mask		= 0x0;
258 uint_t	sd_level_mask			= 0x0;
259 struct	sd_lun *sd_debug_un		= NULL;
260 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
261 
262 /* Note: these may go away in the future... */
263 static uint32_t	sd_xbuf_active_limit	= 512;
264 static uint32_t sd_xbuf_reserve_limit	= 16;
265 
266 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
267 
268 /*
269  * Timer value used to reset the throttle after it has been reduced
270  * (typically in response to TRAN_BUSY or STATUS_QFULL)
271  */
272 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
273 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
274 
275 /*
276  * Interval value associated with the media change scsi watch.
277  */
278 static int sd_check_media_time		= 3000000;
279 
280 /*
281  * Wait value used for in progress operations during a DDI_SUSPEND
282  */
283 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
284 
285 /*
286  * sd_label_mutex protects a static buffer used in the disk label
287  * component of the driver
288  */
289 static kmutex_t sd_label_mutex;
290 
291 /*
292  * sd_detach_mutex protects un_layer_count, un_detach_count, and
293  * un_opens_in_progress in the sd_lun structure.
294  */
295 static kmutex_t sd_detach_mutex;
296 
297 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
298 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
299 
300 /*
301  * Global buffer and mutex for debug logging
302  */
303 static char	sd_log_buf[1024];
304 static kmutex_t	sd_log_mutex;
305 
306 /*
307  * Structs and globals for recording attached lun information.
308  * This maintains a chain. Each node in the chain represents a SCSI controller.
309  * The structure records the number of luns attached to each target connected
310  * with the controller.
311  * For parallel scsi device only.
312  */
313 struct sd_scsi_hba_tgt_lun {
314 	struct sd_scsi_hba_tgt_lun	*next;
315 	dev_info_t			*pdip;
316 	int				nlun[NTARGETS_WIDE];
317 };
318 
319 /*
320  * Flag to indicate the lun is attached or detached
321  */
322 #define	SD_SCSI_LUN_ATTACH	0
323 #define	SD_SCSI_LUN_DETACH	1
324 
325 static kmutex_t	sd_scsi_target_lun_mutex;
326 static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
327 
328 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
329     sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
330 
331 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
332     sd_scsi_target_lun_head))
333 
334 /*
335  * "Smart" Probe Caching structs, globals, #defines, etc.
336  * For parallel scsi and non-self-identify device only.
337  */
338 
339 /*
340  * The following resources and routines are implemented to support
341  * "smart" probing, which caches the scsi_probe() results in an array,
342  * in order to help avoid long probe times.
343  */
344 struct sd_scsi_probe_cache {
345 	struct	sd_scsi_probe_cache	*next;
346 	dev_info_t	*pdip;
347 	int		cache[NTARGETS_WIDE];
348 };
349 
350 static kmutex_t	sd_scsi_probe_cache_mutex;
351 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
352 
353 /*
354  * Really we only need protection on the head of the linked list, but
355  * better safe than sorry.
356  */
357 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
358     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
359 
360 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
361     sd_scsi_probe_cache_head))
362 
363 /*
364  * Power attribute table
365  */
366 static sd_power_attr_ss sd_pwr_ss = {
367 	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
368 	{0, 100},
369 	{30, 0},
370 	{20000, 0}
371 };
372 
373 static sd_power_attr_pc sd_pwr_pc = {
374 	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
375 		"3=active", NULL },
376 	{0, 0, 0, 100},
377 	{90, 90, 20, 0},
378 	{15000, 15000, 1000, 0}
379 };
380 
381 /*
382  * Power level to power condition
383  */
384 static int sd_pl2pc[] = {
385 	SD_TARGET_START_VALID,
386 	SD_TARGET_STANDBY,
387 	SD_TARGET_IDLE,
388 	SD_TARGET_ACTIVE
389 };
390 
391 /*
392  * Vendor specific data name property declarations
393  */
394 
395 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
396 
397 static sd_tunables seagate_properties = {
398 	SEAGATE_THROTTLE_VALUE,
399 	0,
400 	0,
401 	0,
402 	0,
403 	0,
404 	0,
405 	0,
406 	0
407 };
408 
409 
410 static sd_tunables fujitsu_properties = {
411 	FUJITSU_THROTTLE_VALUE,
412 	0,
413 	0,
414 	0,
415 	0,
416 	0,
417 	0,
418 	0,
419 	0
420 };
421 
422 static sd_tunables ibm_properties = {
423 	IBM_THROTTLE_VALUE,
424 	0,
425 	0,
426 	0,
427 	0,
428 	0,
429 	0,
430 	0,
431 	0
432 };
433 
434 static sd_tunables purple_properties = {
435 	PURPLE_THROTTLE_VALUE,
436 	0,
437 	0,
438 	PURPLE_BUSY_RETRIES,
439 	PURPLE_RESET_RETRY_COUNT,
440 	PURPLE_RESERVE_RELEASE_TIME,
441 	0,
442 	0,
443 	0
444 };
445 
446 static sd_tunables sve_properties = {
447 	SVE_THROTTLE_VALUE,
448 	0,
449 	0,
450 	SVE_BUSY_RETRIES,
451 	SVE_RESET_RETRY_COUNT,
452 	SVE_RESERVE_RELEASE_TIME,
453 	SVE_MIN_THROTTLE_VALUE,
454 	SVE_DISKSORT_DISABLED_FLAG,
455 	0
456 };
457 
458 static sd_tunables maserati_properties = {
459 	0,
460 	0,
461 	0,
462 	0,
463 	0,
464 	0,
465 	0,
466 	MASERATI_DISKSORT_DISABLED_FLAG,
467 	MASERATI_LUN_RESET_ENABLED_FLAG
468 };
469 
470 static sd_tunables pirus_properties = {
471 	PIRUS_THROTTLE_VALUE,
472 	0,
473 	PIRUS_NRR_COUNT,
474 	PIRUS_BUSY_RETRIES,
475 	PIRUS_RESET_RETRY_COUNT,
476 	0,
477 	PIRUS_MIN_THROTTLE_VALUE,
478 	PIRUS_DISKSORT_DISABLED_FLAG,
479 	PIRUS_LUN_RESET_ENABLED_FLAG
480 };
481 
482 #endif
483 
484 #if (defined(__sparc) && !defined(__fibre)) || \
485 	(defined(__i386) || defined(__amd64))
486 
487 
488 static sd_tunables elite_properties = {
489 	ELITE_THROTTLE_VALUE,
490 	0,
491 	0,
492 	0,
493 	0,
494 	0,
495 	0,
496 	0,
497 	0
498 };
499 
500 static sd_tunables st31200n_properties = {
501 	ST31200N_THROTTLE_VALUE,
502 	0,
503 	0,
504 	0,
505 	0,
506 	0,
507 	0,
508 	0,
509 	0
510 };
511 
512 #endif /* Fibre or not */
513 
514 static sd_tunables lsi_properties_scsi = {
515 	LSI_THROTTLE_VALUE,
516 	0,
517 	LSI_NOTREADY_RETRIES,
518 	0,
519 	0,
520 	0,
521 	0,
522 	0,
523 	0
524 };
525 
526 static sd_tunables symbios_properties = {
527 	SYMBIOS_THROTTLE_VALUE,
528 	0,
529 	SYMBIOS_NOTREADY_RETRIES,
530 	0,
531 	0,
532 	0,
533 	0,
534 	0,
535 	0
536 };
537 
538 static sd_tunables lsi_properties = {
539 	0,
540 	0,
541 	LSI_NOTREADY_RETRIES,
542 	0,
543 	0,
544 	0,
545 	0,
546 	0,
547 	0
548 };
549 
550 static sd_tunables lsi_oem_properties = {
551 	0,
552 	0,
553 	LSI_OEM_NOTREADY_RETRIES,
554 	0,
555 	0,
556 	0,
557 	0,
558 	0,
559 	0,
560 	1
561 };
562 
563 
564 
565 #if (defined(SD_PROP_TST))
566 
567 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
568 #define	SD_TST_THROTTLE_VAL	16
569 #define	SD_TST_NOTREADY_VAL	12
570 #define	SD_TST_BUSY_VAL		60
571 #define	SD_TST_RST_RETRY_VAL	36
572 #define	SD_TST_RSV_REL_TIME	60
573 
574 static sd_tunables tst_properties = {
575 	SD_TST_THROTTLE_VAL,
576 	SD_TST_CTYPE_VAL,
577 	SD_TST_NOTREADY_VAL,
578 	SD_TST_BUSY_VAL,
579 	SD_TST_RST_RETRY_VAL,
580 	SD_TST_RSV_REL_TIME,
581 	0,
582 	0,
583 	0
584 };
585 #endif
586 
587 /* This is similar to the ANSI toupper implementation */
588 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
589 
590 /*
591  * Static Driver Configuration Table
592  *
593  * This is the table of disks which need throttle adjustment (or, perhaps
594  * something else as defined by the flags at a future time.)  device_id
595  * is a string consisting of concatenated vid (vendor), pid (product/model)
596  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
597  * the parts of the string are as defined by the sizes in the scsi_inquiry
598  * structure.  Device type is searched as far as the device_id string is
599  * defined.  Flags defines which values are to be set in the driver from the
600  * properties list.
601  *
602  * Entries below which begin and end with a "*" are a special case.
603  * These do not have a specific vendor, and the string which follows
604  * can appear anywhere in the 16 byte PID portion of the inquiry data.
605  *
606  * Entries below which begin and end with a " " (blank) are a special
607  * case. The comparison function will treat multiple consecutive blanks
608  * as equivalent to a single blank. For example, this causes a
609  * sd_disk_table entry of " NEC CDROM " to match a device's id string
610  * of  "NEC       CDROM".
611  *
612  * Note: The MD21 controller type has been obsoleted.
613  *	 ST318202F is a Legacy device
614  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
615  *	 made with an FC connection. The entries here are a legacy.
616  */
617 static sd_disk_config_t sd_disk_table[] = {
618 #if defined(__fibre) || defined(__i386) || defined(__amd64)
619 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
620 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
621 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
622 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
623 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
624 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
625 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
632 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
633 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
634 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
635 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
639 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
640 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
641 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
642 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
643 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
644 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
645 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
646 	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
647 	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
648 	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649 	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650 	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651 	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
667 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
668 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
669 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
670 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
671 			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
672 	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
673 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
674 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
675 			SD_CONF_BSET_BSY_RETRY_COUNT|
676 			SD_CONF_BSET_RST_RETRIES|
677 			SD_CONF_BSET_RSV_REL_TIME,
678 		&purple_properties },
679 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
680 		SD_CONF_BSET_BSY_RETRY_COUNT|
681 		SD_CONF_BSET_RST_RETRIES|
682 		SD_CONF_BSET_RSV_REL_TIME|
683 		SD_CONF_BSET_MIN_THROTTLE|
684 		SD_CONF_BSET_DISKSORT_DISABLED,
685 		&sve_properties },
686 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
687 			SD_CONF_BSET_BSY_RETRY_COUNT|
688 			SD_CONF_BSET_RST_RETRIES|
689 			SD_CONF_BSET_RSV_REL_TIME,
690 		&purple_properties },
691 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
692 		SD_CONF_BSET_LUN_RESET_ENABLED,
693 		&maserati_properties },
694 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
695 		SD_CONF_BSET_NRR_COUNT|
696 		SD_CONF_BSET_BSY_RETRY_COUNT|
697 		SD_CONF_BSET_RST_RETRIES|
698 		SD_CONF_BSET_MIN_THROTTLE|
699 		SD_CONF_BSET_DISKSORT_DISABLED|
700 		SD_CONF_BSET_LUN_RESET_ENABLED,
701 		&pirus_properties },
702 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
703 		SD_CONF_BSET_NRR_COUNT|
704 		SD_CONF_BSET_BSY_RETRY_COUNT|
705 		SD_CONF_BSET_RST_RETRIES|
706 		SD_CONF_BSET_MIN_THROTTLE|
707 		SD_CONF_BSET_DISKSORT_DISABLED|
708 		SD_CONF_BSET_LUN_RESET_ENABLED,
709 		&pirus_properties },
710 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
711 		SD_CONF_BSET_NRR_COUNT|
712 		SD_CONF_BSET_BSY_RETRY_COUNT|
713 		SD_CONF_BSET_RST_RETRIES|
714 		SD_CONF_BSET_MIN_THROTTLE|
715 		SD_CONF_BSET_DISKSORT_DISABLED|
716 		SD_CONF_BSET_LUN_RESET_ENABLED,
717 		&pirus_properties },
718 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
719 		SD_CONF_BSET_NRR_COUNT|
720 		SD_CONF_BSET_BSY_RETRY_COUNT|
721 		SD_CONF_BSET_RST_RETRIES|
722 		SD_CONF_BSET_MIN_THROTTLE|
723 		SD_CONF_BSET_DISKSORT_DISABLED|
724 		SD_CONF_BSET_LUN_RESET_ENABLED,
725 		&pirus_properties },
726 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
727 		SD_CONF_BSET_NRR_COUNT|
728 		SD_CONF_BSET_BSY_RETRY_COUNT|
729 		SD_CONF_BSET_RST_RETRIES|
730 		SD_CONF_BSET_MIN_THROTTLE|
731 		SD_CONF_BSET_DISKSORT_DISABLED|
732 		SD_CONF_BSET_LUN_RESET_ENABLED,
733 		&pirus_properties },
734 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
735 		SD_CONF_BSET_NRR_COUNT|
736 		SD_CONF_BSET_BSY_RETRY_COUNT|
737 		SD_CONF_BSET_RST_RETRIES|
738 		SD_CONF_BSET_MIN_THROTTLE|
739 		SD_CONF_BSET_DISKSORT_DISABLED|
740 		SD_CONF_BSET_LUN_RESET_ENABLED,
741 		&pirus_properties },
742 	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
743 	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
744 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
747 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
748 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
749 #endif /* fibre or NON-sparc platforms */
750 #if ((defined(__sparc) && !defined(__fibre)) ||\
751 	(defined(__i386) || defined(__amd64)))
752 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
753 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
754 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
755 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
756 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
757 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
758 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
759 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
760 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
761 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
762 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
763 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
764 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
765 	    &symbios_properties },
766 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
767 	    &lsi_properties_scsi },
768 #if defined(__i386) || defined(__amd64)
769 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
770 				    | SD_CONF_BSET_READSUB_BCD
771 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
772 				    | SD_CONF_BSET_NO_READ_HEADER
773 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
774 
775 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
776 				    | SD_CONF_BSET_READSUB_BCD
777 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
778 				    | SD_CONF_BSET_NO_READ_HEADER
779 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
780 #endif /* __i386 || __amd64 */
781 #endif /* sparc NON-fibre or NON-sparc platforms */
782 
783 #if (defined(SD_PROP_TST))
784 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
785 				| SD_CONF_BSET_CTYPE
786 				| SD_CONF_BSET_NRR_COUNT
787 				| SD_CONF_BSET_FAB_DEVID
788 				| SD_CONF_BSET_NOCACHE
789 				| SD_CONF_BSET_BSY_RETRY_COUNT
790 				| SD_CONF_BSET_PLAYMSF_BCD
791 				| SD_CONF_BSET_READSUB_BCD
792 				| SD_CONF_BSET_READ_TOC_TRK_BCD
793 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
794 				| SD_CONF_BSET_NO_READ_HEADER
795 				| SD_CONF_BSET_READ_CD_XD4
796 				| SD_CONF_BSET_RST_RETRIES
797 				| SD_CONF_BSET_RSV_REL_TIME
798 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
799 #endif
800 };
801 
802 static const int sd_disk_table_size =
803 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
804 
805 /*
806  * Emulation mode disk drive VID/PID table
807  */
808 static char sd_flash_dev_table[][25] = {
809 	"ATA     MARVELL SD88SA02",
810 	"MARVELL SD88SA02",
811 	"TOSHIBA THNSNV05",
812 };
813 
814 static const int sd_flash_dev_table_size =
815 	sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
816 
817 #define	SD_INTERCONNECT_PARALLEL	0
818 #define	SD_INTERCONNECT_FABRIC		1
819 #define	SD_INTERCONNECT_FIBRE		2
820 #define	SD_INTERCONNECT_SSA		3
821 #define	SD_INTERCONNECT_SATA		4
822 #define	SD_INTERCONNECT_SAS		5
823 
824 #define	SD_IS_PARALLEL_SCSI(un)		\
825 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
826 #define	SD_IS_SERIAL(un)		\
827 	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
828 	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
829 
830 /*
831  * Definitions used by device id registration routines
832  */
833 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
834 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
835 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
836 
837 static kmutex_t sd_sense_mutex = {0};
838 
839 /*
840  * Macros for updates of the driver state
841  */
842 #define	New_state(un, s)        \
843 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
844 #define	Restore_state(un)	\
845 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
846 
847 static struct sd_cdbinfo sd_cdbtab[] = {
848 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
849 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
850 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
851 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
852 };
853 
854 /*
855  * Specifies the number of seconds that must have elapsed since the last
856  * cmd. has completed for a device to be declared idle to the PM framework.
857  */
858 static int sd_pm_idletime = 1;
859 
860 /*
861  * Internal function prototypes
862  */
863 
864 #if (defined(__fibre))
865 /*
866  * These #defines are to avoid namespace collisions that occur because this
867  * code is currently used to compile two separate driver modules: sd and ssd.
868  * All function names need to be treated this way (even if declared static)
869  * in order to allow the debugger to resolve the names properly.
870  * It is anticipated that in the near future the ssd module will be obsoleted,
871  * at which time this ugliness should go away.
872  */
873 #define	sd_log_trace			ssd_log_trace
874 #define	sd_log_info			ssd_log_info
875 #define	sd_log_err			ssd_log_err
876 #define	sdprobe				ssdprobe
877 #define	sdinfo				ssdinfo
878 #define	sd_prop_op			ssd_prop_op
879 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
880 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
881 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
882 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
883 #define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
884 #define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
885 #define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
886 #define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
887 #define	sd_spin_up_unit			ssd_spin_up_unit
888 #define	sd_enable_descr_sense		ssd_enable_descr_sense
889 #define	sd_reenable_dsense_task		ssd_reenable_dsense_task
890 #define	sd_set_mmc_caps			ssd_set_mmc_caps
891 #define	sd_read_unit_properties		ssd_read_unit_properties
892 #define	sd_process_sdconf_file		ssd_process_sdconf_file
893 #define	sd_process_sdconf_table		ssd_process_sdconf_table
894 #define	sd_sdconf_id_match		ssd_sdconf_id_match
895 #define	sd_blank_cmp			ssd_blank_cmp
896 #define	sd_chk_vers1_data		ssd_chk_vers1_data
897 #define	sd_set_vers1_properties		ssd_set_vers1_properties
898 #define	sd_check_solid_state		ssd_check_solid_state
899 #define	sd_check_emulation_mode		ssd_check_emulation_mode
900 
901 #define	sd_get_physical_geometry	ssd_get_physical_geometry
902 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
903 #define	sd_update_block_info		ssd_update_block_info
904 #define	sd_register_devid		ssd_register_devid
905 #define	sd_get_devid			ssd_get_devid
906 #define	sd_create_devid			ssd_create_devid
907 #define	sd_write_deviceid		ssd_write_deviceid
908 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
909 #define	sd_setup_pm			ssd_setup_pm
910 #define	sd_create_pm_components		ssd_create_pm_components
911 #define	sd_ddi_suspend			ssd_ddi_suspend
912 #define	sd_ddi_resume			ssd_ddi_resume
913 #define	sd_pm_state_change		ssd_pm_state_change
914 #define	sdpower				ssdpower
915 #define	sdattach			ssdattach
916 #define	sddetach			ssddetach
917 #define	sd_unit_attach			ssd_unit_attach
918 #define	sd_unit_detach			ssd_unit_detach
919 #define	sd_set_unit_attributes		ssd_set_unit_attributes
920 #define	sd_create_errstats		ssd_create_errstats
921 #define	sd_set_errstats			ssd_set_errstats
922 #define	sd_set_pstats			ssd_set_pstats
923 #define	sddump				ssddump
924 #define	sd_scsi_poll			ssd_scsi_poll
925 #define	sd_send_polled_RQS		ssd_send_polled_RQS
926 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
927 #define	sd_init_event_callbacks		ssd_init_event_callbacks
928 #define	sd_event_callback		ssd_event_callback
929 #define	sd_cache_control		ssd_cache_control
930 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
931 #define	sd_get_nv_sup			ssd_get_nv_sup
932 #define	sd_make_device			ssd_make_device
933 #define	sdopen				ssdopen
934 #define	sdclose				ssdclose
935 #define	sd_ready_and_valid		ssd_ready_and_valid
936 #define	sdmin				ssdmin
937 #define	sdread				ssdread
938 #define	sdwrite				ssdwrite
939 #define	sdaread				ssdaread
940 #define	sdawrite			ssdawrite
941 #define	sdstrategy			ssdstrategy
942 #define	sdioctl				ssdioctl
943 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
944 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
945 #define	sd_checksum_iostart		ssd_checksum_iostart
946 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
947 #define	sd_pm_iostart			ssd_pm_iostart
948 #define	sd_core_iostart			ssd_core_iostart
949 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
950 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
951 #define	sd_checksum_iodone		ssd_checksum_iodone
952 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
953 #define	sd_pm_iodone			ssd_pm_iodone
954 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
955 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
956 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
957 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
958 #define	sd_buf_iodone			ssd_buf_iodone
959 #define	sd_uscsi_strategy		ssd_uscsi_strategy
960 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
961 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
962 #define	sd_uscsi_iodone			ssd_uscsi_iodone
963 #define	sd_xbuf_strategy		ssd_xbuf_strategy
964 #define	sd_xbuf_init			ssd_xbuf_init
965 #define	sd_pm_entry			ssd_pm_entry
966 #define	sd_pm_exit			ssd_pm_exit
967 
968 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
969 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
970 
971 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
972 #define	sdintr				ssdintr
973 #define	sd_start_cmds			ssd_start_cmds
974 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
975 #define	sd_bioclone_alloc		ssd_bioclone_alloc
976 #define	sd_bioclone_free		ssd_bioclone_free
977 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
978 #define	sd_shadow_buf_free		ssd_shadow_buf_free
979 #define	sd_print_transport_rejected_message	\
980 					ssd_print_transport_rejected_message
981 #define	sd_retry_command		ssd_retry_command
982 #define	sd_set_retry_bp			ssd_set_retry_bp
983 #define	sd_send_request_sense_command	ssd_send_request_sense_command
984 #define	sd_start_retry_command		ssd_start_retry_command
985 #define	sd_start_direct_priority_command	\
986 					ssd_start_direct_priority_command
987 #define	sd_return_failed_command	ssd_return_failed_command
988 #define	sd_return_failed_command_no_restart	\
989 					ssd_return_failed_command_no_restart
990 #define	sd_return_command		ssd_return_command
991 #define	sd_sync_with_callback		ssd_sync_with_callback
992 #define	sdrunout			ssdrunout
993 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
994 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
995 #define	sd_reduce_throttle		ssd_reduce_throttle
996 #define	sd_restore_throttle		ssd_restore_throttle
997 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
998 #define	sd_init_cdb_limits		ssd_init_cdb_limits
999 #define	sd_pkt_status_good		ssd_pkt_status_good
1000 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
1001 #define	sd_pkt_status_busy		ssd_pkt_status_busy
1002 #define	sd_pkt_status_reservation_conflict	\
1003 					ssd_pkt_status_reservation_conflict
1004 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
1005 #define	sd_handle_request_sense		ssd_handle_request_sense
1006 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
1007 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
1008 #define	sd_validate_sense_data		ssd_validate_sense_data
1009 #define	sd_decode_sense			ssd_decode_sense
1010 #define	sd_print_sense_msg		ssd_print_sense_msg
1011 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
1012 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
1013 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
1014 #define	sd_sense_key_medium_or_hardware_error	\
1015 					ssd_sense_key_medium_or_hardware_error
1016 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1017 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1018 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1019 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1020 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1021 #define	sd_sense_key_default		ssd_sense_key_default
1022 #define	sd_print_retry_msg		ssd_print_retry_msg
1023 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1024 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1025 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1026 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1027 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1028 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1029 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1030 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1031 #define	sd_pkt_reason_default		ssd_pkt_reason_default
1032 #define	sd_reset_target			ssd_reset_target
1033 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1034 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1035 #define	sd_taskq_create			ssd_taskq_create
1036 #define	sd_taskq_delete			ssd_taskq_delete
1037 #define	sd_target_change_task		ssd_target_change_task
1038 #define	sd_log_dev_status_event		ssd_log_dev_status_event
1039 #define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1040 #define	sd_log_eject_request_event	ssd_log_eject_request_event
1041 #define	sd_media_change_task		ssd_media_change_task
1042 #define	sd_handle_mchange		ssd_handle_mchange
1043 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1044 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1045 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1046 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1047 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
1048 					sd_send_scsi_feature_GET_CONFIGURATION
1049 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1050 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1051 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1052 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1053 					ssd_send_scsi_PERSISTENT_RESERVE_IN
1054 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1055 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1056 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1057 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1058 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1059 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1060 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1061 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1062 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1063 #define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1064 				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1065 #define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1066 #define	sd_alloc_rqs			ssd_alloc_rqs
1067 #define	sd_free_rqs			ssd_free_rqs
1068 #define	sd_dump_memory			ssd_dump_memory
1069 #define	sd_get_media_info_com		ssd_get_media_info_com
1070 #define	sd_get_media_info		ssd_get_media_info
1071 #define	sd_get_media_info_ext		ssd_get_media_info_ext
1072 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1073 #define	sd_nvpair_str_decode		ssd_nvpair_str_decode
1074 #define	sd_strtok_r			ssd_strtok_r
1075 #define	sd_set_properties		ssd_set_properties
1076 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1077 #define	sd_setup_next_xfer		ssd_setup_next_xfer
1078 #define	sd_dkio_get_temp		ssd_dkio_get_temp
1079 #define	sd_check_mhd			ssd_check_mhd
1080 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1081 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1082 #define	sd_sname			ssd_sname
1083 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1084 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1085 #define	sd_take_ownership		ssd_take_ownership
1086 #define	sd_reserve_release		ssd_reserve_release
1087 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1088 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1089 #define	sd_persistent_reservation_in_read_keys	\
1090 					ssd_persistent_reservation_in_read_keys
1091 #define	sd_persistent_reservation_in_read_resv	\
1092 					ssd_persistent_reservation_in_read_resv
1093 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1094 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1095 #define	sd_mhdioc_release		ssd_mhdioc_release
1096 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1097 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1098 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1099 #define	sr_change_blkmode		ssr_change_blkmode
1100 #define	sr_change_speed			ssr_change_speed
1101 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1102 #define	sr_pause_resume			ssr_pause_resume
1103 #define	sr_play_msf			ssr_play_msf
1104 #define	sr_play_trkind			ssr_play_trkind
1105 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1106 #define	sr_read_subchannel		ssr_read_subchannel
1107 #define	sr_read_tocentry		ssr_read_tocentry
1108 #define	sr_read_tochdr			ssr_read_tochdr
1109 #define	sr_read_cdda			ssr_read_cdda
1110 #define	sr_read_cdxa			ssr_read_cdxa
1111 #define	sr_read_mode1			ssr_read_mode1
1112 #define	sr_read_mode2			ssr_read_mode2
1113 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1114 #define	sr_sector_mode			ssr_sector_mode
1115 #define	sr_eject			ssr_eject
1116 #define	sr_ejected			ssr_ejected
1117 #define	sr_check_wp			ssr_check_wp
1118 #define	sd_watch_request_submit		ssd_watch_request_submit
1119 #define	sd_check_media			ssd_check_media
1120 #define	sd_media_watch_cb		ssd_media_watch_cb
1121 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1122 #define	sr_volume_ctrl			ssr_volume_ctrl
1123 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1124 #define	sd_log_page_supported		ssd_log_page_supported
1125 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1126 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1127 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1128 #define	sd_range_lock			ssd_range_lock
1129 #define	sd_get_range			ssd_get_range
1130 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1131 #define	sd_range_unlock			ssd_range_unlock
1132 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1133 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1134 
1135 #define	sd_iostart_chain		ssd_iostart_chain
1136 #define	sd_iodone_chain			ssd_iodone_chain
1137 #define	sd_initpkt_map			ssd_initpkt_map
1138 #define	sd_destroypkt_map		ssd_destroypkt_map
1139 #define	sd_chain_type_map		ssd_chain_type_map
1140 #define	sd_chain_index_map		ssd_chain_index_map
1141 
1142 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1143 #define	sd_failfast_flushq		ssd_failfast_flushq
1144 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1145 
1146 #define	sd_is_lsi			ssd_is_lsi
1147 #define	sd_tg_rdwr			ssd_tg_rdwr
1148 #define	sd_tg_getinfo			ssd_tg_getinfo
1149 #define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
1150 
1151 #endif	/* #if (defined(__fibre)) */
1152 
1153 
1154 int _init(void);
1155 int _fini(void);
1156 int _info(struct modinfo *modinfop);
1157 
1158 /*PRINTFLIKE3*/
1159 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1160 /*PRINTFLIKE3*/
1161 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1162 /*PRINTFLIKE3*/
1163 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1164 
1165 static int sdprobe(dev_info_t *devi);
1166 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1167     void **result);
1168 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1169     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1170 
1171 /*
1172  * Smart probe for parallel scsi
1173  */
1174 static void sd_scsi_probe_cache_init(void);
1175 static void sd_scsi_probe_cache_fini(void);
1176 static void sd_scsi_clear_probe_cache(void);
1177 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1178 
1179 /*
1180  * Attached luns on target for parallel scsi
1181  */
1182 static void sd_scsi_target_lun_init(void);
1183 static void sd_scsi_target_lun_fini(void);
1184 static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1185 static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1186 
1187 static int	sd_spin_up_unit(sd_ssc_t *ssc);
1188 
1189 /*
1190  * Using sd_ssc_init to establish sd_ssc_t struct
1191  * Using sd_ssc_send to send uscsi internal command
1192  * Using sd_ssc_fini to free sd_ssc_t struct
1193  */
1194 static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1195 static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1196     int flag, enum uio_seg dataspace, int path_flag);
1197 static void sd_ssc_fini(sd_ssc_t *ssc);
1198 
1199 /*
1200  * Using sd_ssc_assessment to set correct type-of-assessment
1201  * Using sd_ssc_post to post ereport & system log
1202  *       sd_ssc_post will call sd_ssc_print to print system log
1203  *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1204  */
1205 static void sd_ssc_assessment(sd_ssc_t *ssc,
1206     enum sd_type_assessment tp_assess);
1207 
1208 static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1209 static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1210 static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1211     enum sd_driver_assessment drv_assess);
1212 
1213 /*
1214  * Using sd_ssc_set_info to mark an un-decodable-data error.
1215  * Using sd_ssc_extract_info to transfer information from internal
1216  *       data structures to sd_ssc_t.
1217  */
1218 static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1219     const char *fmt, ...);
1220 static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1221     struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1222 
1223 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1224     enum uio_seg dataspace, int path_flag);
1225 
1226 #ifdef _LP64
1227 static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1228 static void	sd_reenable_dsense_task(void *arg);
1229 #endif /* _LP64 */
1230 
1231 static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1232 
1233 static void sd_read_unit_properties(struct sd_lun *un);
1234 static int  sd_process_sdconf_file(struct sd_lun *un);
1235 static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1236 static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1237 static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1238 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1239     int *data_list, sd_tunables *values);
1240 static void sd_process_sdconf_table(struct sd_lun *un);
1241 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1242 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1243 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1244 	int list_len, char *dataname_ptr);
1245 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1246     sd_tunables *prop_list);
1247 
1248 static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1249     int reservation_flag);
1250 static int  sd_get_devid(sd_ssc_t *ssc);
1251 static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1252 static int  sd_write_deviceid(sd_ssc_t *ssc);
1253 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1254 static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1255 
1256 static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1257 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1258 
1259 static int  sd_ddi_suspend(dev_info_t *devi);
1260 static int  sd_ddi_resume(dev_info_t *devi);
1261 static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1262 static int  sdpower(dev_info_t *devi, int component, int level);
1263 
1264 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1265 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1266 static int  sd_unit_attach(dev_info_t *devi);
1267 static int  sd_unit_detach(dev_info_t *devi);
1268 
1269 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1270 static void sd_create_errstats(struct sd_lun *un, int instance);
1271 static void sd_set_errstats(struct sd_lun *un);
1272 static void sd_set_pstats(struct sd_lun *un);
1273 
1274 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1275 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1276 static int  sd_send_polled_RQS(struct sd_lun *un);
1277 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1278 
1279 #if (defined(__fibre))
1280 /*
1281  * Event callbacks (photon)
1282  */
1283 static void sd_init_event_callbacks(struct sd_lun *un);
1284 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1285 #endif
1286 
1287 /*
1288  * Defines for sd_cache_control
1289  */
1290 
1291 #define	SD_CACHE_ENABLE		1
1292 #define	SD_CACHE_DISABLE	0
1293 #define	SD_CACHE_NOCHANGE	-1
1294 
1295 static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1296 static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1297 static void  sd_get_nv_sup(sd_ssc_t *ssc);
1298 static dev_t sd_make_device(dev_info_t *devi);
1299 static void  sd_check_solid_state(sd_ssc_t *ssc);
1300 static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1301 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1302 	uint64_t capacity);
1303 
1304 /*
1305  * Driver entry point functions.
1306  */
1307 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1308 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1309 static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1310 
1311 static void sdmin(struct buf *bp);
1312 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1313 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1314 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1315 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1316 
1317 static int sdstrategy(struct buf *bp);
1318 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1319 
1320 /*
1321  * Function prototypes for layering functions in the iostart chain.
1322  */
1323 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1324 	struct buf *bp);
1325 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1326 	struct buf *bp);
1327 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1328 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1329 	struct buf *bp);
1330 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1331 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1332 
1333 /*
1334  * Function prototypes for layering functions in the iodone chain.
1335  */
1336 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1337 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1338 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1339 	struct buf *bp);
1340 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1341 	struct buf *bp);
1342 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1343 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1344 	struct buf *bp);
1345 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1346 
1347 /*
1348  * Prototypes for functions to support buf(9S) based IO.
1349  */
1350 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1351 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1352 static void sd_destroypkt_for_buf(struct buf *);
1353 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1354 	struct buf *bp, int flags,
1355 	int (*callback)(caddr_t), caddr_t callback_arg,
1356 	diskaddr_t lba, uint32_t blockcount);
1357 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1358 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1359 
1360 /*
1361  * Prototypes for functions to support USCSI IO.
1362  */
1363 static int sd_uscsi_strategy(struct buf *bp);
1364 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1365 static void sd_destroypkt_for_uscsi(struct buf *);
1366 
1367 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1368 	uchar_t chain_type, void *pktinfop);
1369 
1370 static int  sd_pm_entry(struct sd_lun *un);
1371 static void sd_pm_exit(struct sd_lun *un);
1372 
1373 static void sd_pm_idletimeout_handler(void *arg);
1374 
1375 /*
1376  * sd_core internal functions (used at the sd_core_io layer).
1377  */
1378 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1379 static void sdintr(struct scsi_pkt *pktp);
1380 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1381 
1382 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1383 	enum uio_seg dataspace, int path_flag);
1384 
1385 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1386 	daddr_t blkno, int (*func)(struct buf *));
1387 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1388 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1389 static void sd_bioclone_free(struct buf *bp);
1390 static void sd_shadow_buf_free(struct buf *bp);
1391 
1392 static void sd_print_transport_rejected_message(struct sd_lun *un,
1393 	struct sd_xbuf *xp, int code);
1394 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1395     void *arg, int code);
1396 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1397     void *arg, int code);
1398 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1399     void *arg, int code);
1400 
1401 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1402 	int retry_check_flag,
1403 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1404 		int c),
1405 	void *user_arg, int failure_code,  clock_t retry_delay,
1406 	void (*statp)(kstat_io_t *));
1407 
1408 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1409 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1410 
1411 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1412 	struct scsi_pkt *pktp);
1413 static void sd_start_retry_command(void *arg);
1414 static void sd_start_direct_priority_command(void *arg);
1415 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1416 	int errcode);
1417 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1418 	struct buf *bp, int errcode);
1419 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1420 static void sd_sync_with_callback(struct sd_lun *un);
1421 static int sdrunout(caddr_t arg);
1422 
1423 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1424 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1425 
1426 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1427 static void sd_restore_throttle(void *arg);
1428 
1429 static void sd_init_cdb_limits(struct sd_lun *un);
1430 
1431 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1432 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1433 
1434 /*
1435  * Error handling functions
1436  */
1437 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1438 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1439 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1440 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1441 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1442 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1443 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1444 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1445 
1446 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1447 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1448 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1449 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1450 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1451 	struct sd_xbuf *xp, size_t actual_len);
1452 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1453 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1454 
1455 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1456 	void *arg, int code);
1457 
1458 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1459 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1460 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1461 	uint8_t *sense_datap,
1462 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1463 static void sd_sense_key_not_ready(struct sd_lun *un,
1464 	uint8_t *sense_datap,
1465 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1466 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1467 	uint8_t *sense_datap,
1468 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1469 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1470 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1471 static void sd_sense_key_unit_attention(struct sd_lun *un,
1472 	uint8_t *sense_datap,
1473 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1474 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1475 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1476 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1477 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1478 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1479 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1480 static void sd_sense_key_default(struct sd_lun *un,
1481 	uint8_t *sense_datap,
1482 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1483 
1484 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1485 	void *arg, int flag);
1486 
1487 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1488 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1489 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1490 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1491 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1492 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1493 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1494 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1495 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1496 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1497 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1498 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1499 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1500 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1501 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1502 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1503 
1504 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1505 
1506 static void sd_start_stop_unit_callback(void *arg);
1507 static void sd_start_stop_unit_task(void *arg);
1508 
1509 static void sd_taskq_create(void);
1510 static void sd_taskq_delete(void);
1511 static void sd_target_change_task(void *arg);
1512 static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1513 static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1514 static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1515 static void sd_media_change_task(void *arg);
1516 
1517 static int sd_handle_mchange(struct sd_lun *un);
1518 static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1519 static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1520 	uint32_t *lbap, int path_flag);
1521 static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1522 	uint32_t *lbap, uint32_t *psp, int path_flag);
1523 static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1524 	int flag, int path_flag);
1525 static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1526 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1527 static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1528 static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1529 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1530 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1531 	uchar_t usr_cmd, uchar_t *usr_bufp);
1532 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1533 	struct dk_callback *dkc);
1534 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1535 static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1536 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1537 	uchar_t *bufaddr, uint_t buflen, int path_flag);
1538 static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1539 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1540 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1541 static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1542 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1543 static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1544 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1545 static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1546 	size_t buflen, daddr_t start_block, int path_flag);
1547 #define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1548 	sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1549 	path_flag)
1550 #define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1551 	sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1552 	path_flag)
1553 
1554 static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1555 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1556 	uint16_t param_ptr, int path_flag);
1557 static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1558 	uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1559 static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1560 
1561 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1562 static void sd_free_rqs(struct sd_lun *un);
1563 
1564 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1565 	uchar_t *data, int len, int fmt);
1566 static void sd_panic_for_res_conflict(struct sd_lun *un);
1567 
1568 /*
1569  * Disk Ioctl Function Prototypes
1570  */
1571 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1572 static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1573 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1574 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1575 
1576 /*
1577  * Multi-host Ioctl Prototypes
1578  */
1579 static int sd_check_mhd(dev_t dev, int interval);
1580 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1581 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1582 static char *sd_sname(uchar_t status);
1583 static void sd_mhd_resvd_recover(void *arg);
1584 static void sd_resv_reclaim_thread();
1585 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1586 static int sd_reserve_release(dev_t dev, int cmd);
1587 static void sd_rmv_resv_reclaim_req(dev_t dev);
1588 static void sd_mhd_reset_notify_cb(caddr_t arg);
1589 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1590 	mhioc_inkeys_t *usrp, int flag);
1591 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1592 	mhioc_inresvs_t *usrp, int flag);
1593 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1594 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1595 static int sd_mhdioc_release(dev_t dev);
1596 static int sd_mhdioc_register_devid(dev_t dev);
1597 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1598 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1599 
1600 /*
1601  * SCSI removable prototypes
1602  */
1603 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1604 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1605 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1606 static int sr_pause_resume(dev_t dev, int mode);
1607 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1608 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1609 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1610 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1611 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1612 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1613 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1614 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1615 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1616 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1617 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1618 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1619 static int sr_eject(dev_t dev);
1620 static void sr_ejected(register struct sd_lun *un);
1621 static int sr_check_wp(dev_t dev);
1622 static opaque_t sd_watch_request_submit(struct sd_lun *un);
1623 static int sd_check_media(dev_t dev, enum dkio_state state);
1624 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1625 static void sd_delayed_cv_broadcast(void *arg);
1626 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1627 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1628 
1629 static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1630 
1631 /*
1632  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1633  */
1634 static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1635 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1636 static void sd_wm_cache_destructor(void *wm, void *un);
1637 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1638 	daddr_t endb, ushort_t typ);
1639 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1640 	daddr_t endb);
1641 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1642 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1643 static void sd_read_modify_write_task(void * arg);
1644 static int
1645 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1646 	struct buf **bpp);
1647 
1648 
1649 /*
1650  * Function prototypes for failfast support.
1651  */
1652 static void sd_failfast_flushq(struct sd_lun *un);
1653 static int sd_failfast_flushq_callback(struct buf *bp);
1654 
1655 /*
1656  * Function prototypes to check for lsi devices
1657  */
1658 static void sd_is_lsi(struct sd_lun *un);
1659 
1660 /*
1661  * Function prototypes for partial DMA support
1662  */
1663 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1664 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1665 
1666 
1667 /* Function prototypes for cmlb */
1668 static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1669     diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1670 
1671 static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1672 
1673 /*
1674  * For printing RMW warning message timely
1675  */
1676 static void sd_rmw_msg_print_handler(void *arg);
1677 
1678 /*
1679  * Constants for failfast support:
1680  *
1681  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1682  * failfast processing being performed.
1683  *
1684  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1685  * failfast processing on all bufs with B_FAILFAST set.
1686  */
1687 
1688 #define	SD_FAILFAST_INACTIVE		0
1689 #define	SD_FAILFAST_ACTIVE		1
1690 
1691 /*
1692  * Bitmask to control behavior of buf(9S) flushes when a transition to
1693  * the failfast state occurs. Optional bits include:
1694  *
1695  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1696  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1697  * be flushed.
1698  *
1699  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1700  * driver, in addition to the regular wait queue. This includes the xbuf
1701  * queues. When clear, only the driver's wait queue will be flushed.
1702  */
1703 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1704 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1705 
1706 /*
1707  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1708  * to flush all queues within the driver.
1709  */
1710 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1711 
1712 
1713 /*
1714  * SD Testing Fault Injection
1715  */
1716 #ifdef SD_FAULT_INJECTION
1717 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1718 static void sd_faultinjection(struct scsi_pkt *pktp);
1719 static void sd_injection_log(char *buf, struct sd_lun *un);
1720 #endif
1721 
1722 /*
1723  * Device driver ops vector
1724  */
1725 static struct cb_ops sd_cb_ops = {
1726 	sdopen,			/* open */
1727 	sdclose,		/* close */
1728 	sdstrategy,		/* strategy */
1729 	nodev,			/* print */
1730 	sddump,			/* dump */
1731 	sdread,			/* read */
1732 	sdwrite,		/* write */
1733 	sdioctl,		/* ioctl */
1734 	nodev,			/* devmap */
1735 	nodev,			/* mmap */
1736 	nodev,			/* segmap */
1737 	nochpoll,		/* poll */
1738 	sd_prop_op,		/* cb_prop_op */
1739 	0,			/* streamtab  */
1740 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1741 	CB_REV,			/* cb_rev */
1742 	sdaread, 		/* async I/O read entry point */
1743 	sdawrite		/* async I/O write entry point */
1744 };
1745 
1746 struct dev_ops sd_ops = {
1747 	DEVO_REV,		/* devo_rev, */
1748 	0,			/* refcnt  */
1749 	sdinfo,			/* info */
1750 	nulldev,		/* identify */
1751 	sdprobe,		/* probe */
1752 	sdattach,		/* attach */
1753 	sddetach,		/* detach */
1754 	nodev,			/* reset */
1755 	&sd_cb_ops,		/* driver operations */
1756 	NULL,			/* bus operations */
1757 	sdpower,		/* power */
1758 	ddi_quiesce_not_needed,		/* quiesce */
1759 };
1760 
1761 /*
1762  * This is the loadable module wrapper.
1763  */
1764 #include <sys/modctl.h>
1765 
1766 #ifndef XPV_HVM_DRIVER
1767 static struct modldrv modldrv = {
1768 	&mod_driverops,		/* Type of module. This one is a driver */
1769 	SD_MODULE_NAME,		/* Module name. */
1770 	&sd_ops			/* driver ops */
1771 };
1772 
1773 static struct modlinkage modlinkage = {
1774 	MODREV_1, &modldrv, NULL
1775 };
1776 
1777 #else /* XPV_HVM_DRIVER */
1778 static struct modlmisc modlmisc = {
1779 	&mod_miscops,		/* Type of module. This one is a misc */
1780 	"HVM " SD_MODULE_NAME,		/* Module name. */
1781 };
1782 
1783 static struct modlinkage modlinkage = {
1784 	MODREV_1, &modlmisc, NULL
1785 };
1786 
1787 #endif /* XPV_HVM_DRIVER */
1788 
1789 static cmlb_tg_ops_t sd_tgops = {
1790 	TG_DK_OPS_VERSION_1,
1791 	sd_tg_rdwr,
1792 	sd_tg_getinfo
1793 };
1794 
1795 static struct scsi_asq_key_strings sd_additional_codes[] = {
1796 	0x81, 0, "Logical Unit is Reserved",
1797 	0x85, 0, "Audio Address Not Valid",
1798 	0xb6, 0, "Media Load Mechanism Failed",
1799 	0xB9, 0, "Audio Play Operation Aborted",
1800 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1801 	0x53, 2, "Medium removal prevented",
1802 	0x6f, 0, "Authentication failed during key exchange",
1803 	0x6f, 1, "Key not present",
1804 	0x6f, 2, "Key not established",
1805 	0x6f, 3, "Read without proper authentication",
1806 	0x6f, 4, "Mismatched region to this logical unit",
1807 	0x6f, 5, "Region reset count error",
1808 	0xffff, 0x0, NULL
1809 };
1810 
1811 
1812 /*
1813  * Struct for passing printing information for sense data messages
1814  */
1815 struct sd_sense_info {
1816 	int	ssi_severity;
1817 	int	ssi_pfa_flag;
1818 };
1819 
1820 /*
1821  * Table of function pointers for iostart-side routines. Separate "chains"
1822  * of layered function calls are formed by placing the function pointers
1823  * sequentially in the desired order. Functions are called according to an
1824  * incrementing table index ordering. The last function in each chain must
1825  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1826  * in the sd_iodone_chain[] array.
1827  *
1828  * Note: It may seem more natural to organize both the iostart and iodone
1829  * functions together, into an array of structures (or some similar
1830  * organization) with a common index, rather than two separate arrays which
1831  * must be maintained in synchronization. The purpose of this division is
1832  * to achieve improved performance: individual arrays allows for more
1833  * effective cache line utilization on certain platforms.
1834  */
1835 
1836 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1837 
1838 
1839 static sd_chain_t sd_iostart_chain[] = {
1840 
1841 	/* Chain for buf IO for disk drive targets (PM enabled) */
1842 	sd_mapblockaddr_iostart,	/* Index: 0 */
1843 	sd_pm_iostart,			/* Index: 1 */
1844 	sd_core_iostart,		/* Index: 2 */
1845 
1846 	/* Chain for buf IO for disk drive targets (PM disabled) */
1847 	sd_mapblockaddr_iostart,	/* Index: 3 */
1848 	sd_core_iostart,		/* Index: 4 */
1849 
1850 	/*
1851 	 * Chain for buf IO for removable-media or large sector size
1852 	 * disk drive targets with RMW needed (PM enabled)
1853 	 */
1854 	sd_mapblockaddr_iostart,	/* Index: 5 */
1855 	sd_mapblocksize_iostart,	/* Index: 6 */
1856 	sd_pm_iostart,			/* Index: 7 */
1857 	sd_core_iostart,		/* Index: 8 */
1858 
1859 	/*
1860 	 * Chain for buf IO for removable-media or large sector size
1861 	 * disk drive targets with RMW needed (PM disabled)
1862 	 */
1863 	sd_mapblockaddr_iostart,	/* Index: 9 */
1864 	sd_mapblocksize_iostart,	/* Index: 10 */
1865 	sd_core_iostart,		/* Index: 11 */
1866 
1867 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1868 	sd_mapblockaddr_iostart,	/* Index: 12 */
1869 	sd_checksum_iostart,		/* Index: 13 */
1870 	sd_pm_iostart,			/* Index: 14 */
1871 	sd_core_iostart,		/* Index: 15 */
1872 
1873 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1874 	sd_mapblockaddr_iostart,	/* Index: 16 */
1875 	sd_checksum_iostart,		/* Index: 17 */
1876 	sd_core_iostart,		/* Index: 18 */
1877 
1878 	/* Chain for USCSI commands (all targets) */
1879 	sd_pm_iostart,			/* Index: 19 */
1880 	sd_core_iostart,		/* Index: 20 */
1881 
1882 	/* Chain for checksumming USCSI commands (all targets) */
1883 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1884 	sd_pm_iostart,			/* Index: 22 */
1885 	sd_core_iostart,		/* Index: 23 */
1886 
1887 	/* Chain for "direct" USCSI commands (all targets) */
1888 	sd_core_iostart,		/* Index: 24 */
1889 
1890 	/* Chain for "direct priority" USCSI commands (all targets) */
1891 	sd_core_iostart,		/* Index: 25 */
1892 
1893 	/*
1894 	 * Chain for buf IO for large sector size disk drive targets
1895 	 * with RMW needed with checksumming (PM enabled)
1896 	 */
1897 	sd_mapblockaddr_iostart,	/* Index: 26 */
1898 	sd_mapblocksize_iostart,	/* Index: 27 */
1899 	sd_checksum_iostart,		/* Index: 28 */
1900 	sd_pm_iostart,			/* Index: 29 */
1901 	sd_core_iostart,		/* Index: 30 */
1902 
1903 	/*
1904 	 * Chain for buf IO for large sector size disk drive targets
1905 	 * with RMW needed with checksumming (PM disabled)
1906 	 */
1907 	sd_mapblockaddr_iostart,	/* Index: 31 */
1908 	sd_mapblocksize_iostart,	/* Index: 32 */
1909 	sd_checksum_iostart,		/* Index: 33 */
1910 	sd_core_iostart,		/* Index: 34 */
1911 
1912 };
1913 
1914 /*
1915  * Macros to locate the first function of each iostart chain in the
1916  * sd_iostart_chain[] array. These are located by the index in the array.
1917  */
1918 #define	SD_CHAIN_DISK_IOSTART			0
1919 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1920 #define	SD_CHAIN_MSS_DISK_IOSTART		5
1921 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1922 #define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1923 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1924 #define	SD_CHAIN_CHKSUM_IOSTART			12
1925 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1926 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1927 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1928 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1929 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1930 #define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1931 #define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1932 
1933 
1934 /*
1935  * Table of function pointers for the iodone-side routines for the driver-
1936  * internal layering mechanism.  The calling sequence for iodone routines
1937  * uses a decrementing table index, so the last routine called in a chain
1938  * must be at the lowest array index location for that chain.  The last
1939  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1940  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1941  * of the functions in an iodone side chain must correspond to the ordering
1942  * of the iostart routines for that chain.  Note that there is no iodone
1943  * side routine that corresponds to sd_core_iostart(), so there is no
1944  * entry in the table for this.
1945  */
1946 
1947 static sd_chain_t sd_iodone_chain[] = {
1948 
1949 	/* Chain for buf IO for disk drive targets (PM enabled) */
1950 	sd_buf_iodone,			/* Index: 0 */
1951 	sd_mapblockaddr_iodone,		/* Index: 1 */
1952 	sd_pm_iodone,			/* Index: 2 */
1953 
1954 	/* Chain for buf IO for disk drive targets (PM disabled) */
1955 	sd_buf_iodone,			/* Index: 3 */
1956 	sd_mapblockaddr_iodone,		/* Index: 4 */
1957 
1958 	/*
1959 	 * Chain for buf IO for removable-media or large sector size
1960 	 * disk drive targets with RMW needed (PM enabled)
1961 	 */
1962 	sd_buf_iodone,			/* Index: 5 */
1963 	sd_mapblockaddr_iodone,		/* Index: 6 */
1964 	sd_mapblocksize_iodone,		/* Index: 7 */
1965 	sd_pm_iodone,			/* Index: 8 */
1966 
1967 	/*
1968 	 * Chain for buf IO for removable-media or large sector size
1969 	 * disk drive targets with RMW needed (PM disabled)
1970 	 */
1971 	sd_buf_iodone,			/* Index: 9 */
1972 	sd_mapblockaddr_iodone,		/* Index: 10 */
1973 	sd_mapblocksize_iodone,		/* Index: 11 */
1974 
1975 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1976 	sd_buf_iodone,			/* Index: 12 */
1977 	sd_mapblockaddr_iodone,		/* Index: 13 */
1978 	sd_checksum_iodone,		/* Index: 14 */
1979 	sd_pm_iodone,			/* Index: 15 */
1980 
1981 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1982 	sd_buf_iodone,			/* Index: 16 */
1983 	sd_mapblockaddr_iodone,		/* Index: 17 */
1984 	sd_checksum_iodone,		/* Index: 18 */
1985 
1986 	/* Chain for USCSI commands (non-checksum targets) */
1987 	sd_uscsi_iodone,		/* Index: 19 */
1988 	sd_pm_iodone,			/* Index: 20 */
1989 
1990 	/* Chain for USCSI commands (checksum targets) */
1991 	sd_uscsi_iodone,		/* Index: 21 */
1992 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1993 	sd_pm_iodone,			/* Index: 22 */
1994 
1995 	/* Chain for "direct" USCSI commands (all targets) */
1996 	sd_uscsi_iodone,		/* Index: 24 */
1997 
1998 	/* Chain for "direct priority" USCSI commands (all targets) */
1999 	sd_uscsi_iodone,		/* Index: 25 */
2000 
2001 	/*
2002 	 * Chain for buf IO for large sector size disk drive targets
2003 	 * with checksumming (PM enabled)
2004 	 */
2005 	sd_buf_iodone,			/* Index: 26 */
2006 	sd_mapblockaddr_iodone,		/* Index: 27 */
2007 	sd_mapblocksize_iodone,		/* Index: 28 */
2008 	sd_checksum_iodone,		/* Index: 29 */
2009 	sd_pm_iodone,			/* Index: 30 */
2010 
2011 	/*
2012 	 * Chain for buf IO for large sector size disk drive targets
2013 	 * with checksumming (PM disabled)
2014 	 */
2015 	sd_buf_iodone,			/* Index: 31 */
2016 	sd_mapblockaddr_iodone,		/* Index: 32 */
2017 	sd_mapblocksize_iodone,		/* Index: 33 */
2018 	sd_checksum_iodone,		/* Index: 34 */
2019 };
2020 
2021 
2022 /*
2023  * Macros to locate the "first" function in the sd_iodone_chain[] array for
2024  * each iodone-side chain. These are located by the array index, but as the
2025  * iodone side functions are called in a decrementing-index order, the
2026  * highest index number in each chain must be specified (as these correspond
2027  * to the first function in the iodone chain that will be called by the core
2028  * at IO completion time).
2029  */
2030 
2031 #define	SD_CHAIN_DISK_IODONE			2
2032 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
2033 #define	SD_CHAIN_RMMEDIA_IODONE			8
2034 #define	SD_CHAIN_MSS_DISK_IODONE		8
2035 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2036 #define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2037 #define	SD_CHAIN_CHKSUM_IODONE			15
2038 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2039 #define	SD_CHAIN_USCSI_CMD_IODONE		20
2040 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2041 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
2042 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2043 #define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2044 #define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2045 
2046 
2047 
2048 /*
2049  * Array to map a layering chain index to the appropriate initpkt routine.
2050  * The redundant entries are present so that the index used for accessing
2051  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2052  * with this table as well.
2053  */
2054 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2055 
2056 static sd_initpkt_t	sd_initpkt_map[] = {
2057 
2058 	/* Chain for buf IO for disk drive targets (PM enabled) */
2059 	sd_initpkt_for_buf,		/* Index: 0 */
2060 	sd_initpkt_for_buf,		/* Index: 1 */
2061 	sd_initpkt_for_buf,		/* Index: 2 */
2062 
2063 	/* Chain for buf IO for disk drive targets (PM disabled) */
2064 	sd_initpkt_for_buf,		/* Index: 3 */
2065 	sd_initpkt_for_buf,		/* Index: 4 */
2066 
2067 	/*
2068 	 * Chain for buf IO for removable-media or large sector size
2069 	 * disk drive targets (PM enabled)
2070 	 */
2071 	sd_initpkt_for_buf,		/* Index: 5 */
2072 	sd_initpkt_for_buf,		/* Index: 6 */
2073 	sd_initpkt_for_buf,		/* Index: 7 */
2074 	sd_initpkt_for_buf,		/* Index: 8 */
2075 
2076 	/*
2077 	 * Chain for buf IO for removable-media or large sector size
2078 	 * disk drive targets (PM disabled)
2079 	 */
2080 	sd_initpkt_for_buf,		/* Index: 9 */
2081 	sd_initpkt_for_buf,		/* Index: 10 */
2082 	sd_initpkt_for_buf,		/* Index: 11 */
2083 
2084 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2085 	sd_initpkt_for_buf,		/* Index: 12 */
2086 	sd_initpkt_for_buf,		/* Index: 13 */
2087 	sd_initpkt_for_buf,		/* Index: 14 */
2088 	sd_initpkt_for_buf,		/* Index: 15 */
2089 
2090 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2091 	sd_initpkt_for_buf,		/* Index: 16 */
2092 	sd_initpkt_for_buf,		/* Index: 17 */
2093 	sd_initpkt_for_buf,		/* Index: 18 */
2094 
2095 	/* Chain for USCSI commands (non-checksum targets) */
2096 	sd_initpkt_for_uscsi,		/* Index: 19 */
2097 	sd_initpkt_for_uscsi,		/* Index: 20 */
2098 
2099 	/* Chain for USCSI commands (checksum targets) */
2100 	sd_initpkt_for_uscsi,		/* Index: 21 */
2101 	sd_initpkt_for_uscsi,		/* Index: 22 */
2102 	sd_initpkt_for_uscsi,		/* Index: 22 */
2103 
2104 	/* Chain for "direct" USCSI commands (all targets) */
2105 	sd_initpkt_for_uscsi,		/* Index: 24 */
2106 
2107 	/* Chain for "direct priority" USCSI commands (all targets) */
2108 	sd_initpkt_for_uscsi,		/* Index: 25 */
2109 
2110 	/*
2111 	 * Chain for buf IO for large sector size disk drive targets
2112 	 * with checksumming (PM enabled)
2113 	 */
2114 	sd_initpkt_for_buf,		/* Index: 26 */
2115 	sd_initpkt_for_buf,		/* Index: 27 */
2116 	sd_initpkt_for_buf,		/* Index: 28 */
2117 	sd_initpkt_for_buf,		/* Index: 29 */
2118 	sd_initpkt_for_buf,		/* Index: 30 */
2119 
2120 	/*
2121 	 * Chain for buf IO for large sector size disk drive targets
2122 	 * with checksumming (PM disabled)
2123 	 */
2124 	sd_initpkt_for_buf,		/* Index: 31 */
2125 	sd_initpkt_for_buf,		/* Index: 32 */
2126 	sd_initpkt_for_buf,		/* Index: 33 */
2127 	sd_initpkt_for_buf,		/* Index: 34 */
2128 };
2129 
2130 
2131 /*
2132  * Array to map a layering chain index to the appropriate destroypktpkt routine.
2133  * The redundant entries are present so that the index used for accessing
2134  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2135  * with this table as well.
2136  */
2137 typedef void (*sd_destroypkt_t)(struct buf *);
2138 
2139 static sd_destroypkt_t	sd_destroypkt_map[] = {
2140 
2141 	/* Chain for buf IO for disk drive targets (PM enabled) */
2142 	sd_destroypkt_for_buf,		/* Index: 0 */
2143 	sd_destroypkt_for_buf,		/* Index: 1 */
2144 	sd_destroypkt_for_buf,		/* Index: 2 */
2145 
2146 	/* Chain for buf IO for disk drive targets (PM disabled) */
2147 	sd_destroypkt_for_buf,		/* Index: 3 */
2148 	sd_destroypkt_for_buf,		/* Index: 4 */
2149 
2150 	/*
2151 	 * Chain for buf IO for removable-media or large sector size
2152 	 * disk drive targets (PM enabled)
2153 	 */
2154 	sd_destroypkt_for_buf,		/* Index: 5 */
2155 	sd_destroypkt_for_buf,		/* Index: 6 */
2156 	sd_destroypkt_for_buf,		/* Index: 7 */
2157 	sd_destroypkt_for_buf,		/* Index: 8 */
2158 
2159 	/*
2160 	 * Chain for buf IO for removable-media or large sector size
2161 	 * disk drive targets (PM disabled)
2162 	 */
2163 	sd_destroypkt_for_buf,		/* Index: 9 */
2164 	sd_destroypkt_for_buf,		/* Index: 10 */
2165 	sd_destroypkt_for_buf,		/* Index: 11 */
2166 
2167 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2168 	sd_destroypkt_for_buf,		/* Index: 12 */
2169 	sd_destroypkt_for_buf,		/* Index: 13 */
2170 	sd_destroypkt_for_buf,		/* Index: 14 */
2171 	sd_destroypkt_for_buf,		/* Index: 15 */
2172 
2173 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2174 	sd_destroypkt_for_buf,		/* Index: 16 */
2175 	sd_destroypkt_for_buf,		/* Index: 17 */
2176 	sd_destroypkt_for_buf,		/* Index: 18 */
2177 
2178 	/* Chain for USCSI commands (non-checksum targets) */
2179 	sd_destroypkt_for_uscsi,	/* Index: 19 */
2180 	sd_destroypkt_for_uscsi,	/* Index: 20 */
2181 
2182 	/* Chain for USCSI commands (checksum targets) */
2183 	sd_destroypkt_for_uscsi,	/* Index: 21 */
2184 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2185 	sd_destroypkt_for_uscsi,	/* Index: 22 */
2186 
2187 	/* Chain for "direct" USCSI commands (all targets) */
2188 	sd_destroypkt_for_uscsi,	/* Index: 24 */
2189 
2190 	/* Chain for "direct priority" USCSI commands (all targets) */
2191 	sd_destroypkt_for_uscsi,	/* Index: 25 */
2192 
2193 	/*
2194 	 * Chain for buf IO for large sector size disk drive targets
2195 	 * with checksumming (PM disabled)
2196 	 */
2197 	sd_destroypkt_for_buf,		/* Index: 26 */
2198 	sd_destroypkt_for_buf,		/* Index: 27 */
2199 	sd_destroypkt_for_buf,		/* Index: 28 */
2200 	sd_destroypkt_for_buf,		/* Index: 29 */
2201 	sd_destroypkt_for_buf,		/* Index: 30 */
2202 
2203 	/*
2204 	 * Chain for buf IO for large sector size disk drive targets
2205 	 * with checksumming (PM enabled)
2206 	 */
2207 	sd_destroypkt_for_buf,		/* Index: 31 */
2208 	sd_destroypkt_for_buf,		/* Index: 32 */
2209 	sd_destroypkt_for_buf,		/* Index: 33 */
2210 	sd_destroypkt_for_buf,		/* Index: 34 */
2211 };
2212 
2213 
2214 
2215 /*
2216  * Array to map a layering chain index to the appropriate chain "type".
2217  * The chain type indicates a specific property/usage of the chain.
2218  * The redundant entries are present so that the index used for accessing
2219  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2220  * with this table as well.
2221  */
2222 
2223 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2224 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2225 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2226 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2227 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2228 						/* (for error recovery) */
2229 
2230 static int sd_chain_type_map[] = {
2231 
2232 	/* Chain for buf IO for disk drive targets (PM enabled) */
2233 	SD_CHAIN_BUFIO,			/* Index: 0 */
2234 	SD_CHAIN_BUFIO,			/* Index: 1 */
2235 	SD_CHAIN_BUFIO,			/* Index: 2 */
2236 
2237 	/* Chain for buf IO for disk drive targets (PM disabled) */
2238 	SD_CHAIN_BUFIO,			/* Index: 3 */
2239 	SD_CHAIN_BUFIO,			/* Index: 4 */
2240 
2241 	/*
2242 	 * Chain for buf IO for removable-media or large sector size
2243 	 * disk drive targets (PM enabled)
2244 	 */
2245 	SD_CHAIN_BUFIO,			/* Index: 5 */
2246 	SD_CHAIN_BUFIO,			/* Index: 6 */
2247 	SD_CHAIN_BUFIO,			/* Index: 7 */
2248 	SD_CHAIN_BUFIO,			/* Index: 8 */
2249 
2250 	/*
2251 	 * Chain for buf IO for removable-media or large sector size
2252 	 * disk drive targets (PM disabled)
2253 	 */
2254 	SD_CHAIN_BUFIO,			/* Index: 9 */
2255 	SD_CHAIN_BUFIO,			/* Index: 10 */
2256 	SD_CHAIN_BUFIO,			/* Index: 11 */
2257 
2258 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2259 	SD_CHAIN_BUFIO,			/* Index: 12 */
2260 	SD_CHAIN_BUFIO,			/* Index: 13 */
2261 	SD_CHAIN_BUFIO,			/* Index: 14 */
2262 	SD_CHAIN_BUFIO,			/* Index: 15 */
2263 
2264 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2265 	SD_CHAIN_BUFIO,			/* Index: 16 */
2266 	SD_CHAIN_BUFIO,			/* Index: 17 */
2267 	SD_CHAIN_BUFIO,			/* Index: 18 */
2268 
2269 	/* Chain for USCSI commands (non-checksum targets) */
2270 	SD_CHAIN_USCSI,			/* Index: 19 */
2271 	SD_CHAIN_USCSI,			/* Index: 20 */
2272 
2273 	/* Chain for USCSI commands (checksum targets) */
2274 	SD_CHAIN_USCSI,			/* Index: 21 */
2275 	SD_CHAIN_USCSI,			/* Index: 22 */
2276 	SD_CHAIN_USCSI,			/* Index: 23 */
2277 
2278 	/* Chain for "direct" USCSI commands (all targets) */
2279 	SD_CHAIN_DIRECT,		/* Index: 24 */
2280 
2281 	/* Chain for "direct priority" USCSI commands (all targets) */
2282 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2283 
2284 	/*
2285 	 * Chain for buf IO for large sector size disk drive targets
2286 	 * with checksumming (PM enabled)
2287 	 */
2288 	SD_CHAIN_BUFIO,			/* Index: 26 */
2289 	SD_CHAIN_BUFIO,			/* Index: 27 */
2290 	SD_CHAIN_BUFIO,			/* Index: 28 */
2291 	SD_CHAIN_BUFIO,			/* Index: 29 */
2292 	SD_CHAIN_BUFIO,			/* Index: 30 */
2293 
2294 	/*
2295 	 * Chain for buf IO for large sector size disk drive targets
2296 	 * with checksumming (PM disabled)
2297 	 */
2298 	SD_CHAIN_BUFIO,			/* Index: 31 */
2299 	SD_CHAIN_BUFIO,			/* Index: 32 */
2300 	SD_CHAIN_BUFIO,			/* Index: 33 */
2301 	SD_CHAIN_BUFIO,			/* Index: 34 */
2302 };
2303 
2304 
2305 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2306 #define	SD_IS_BUFIO(xp)			\
2307 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2308 
2309 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2310 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2311 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2312 
2313 
2314 
2315 /*
2316  * Struct, array, and macros to map a specific chain to the appropriate
2317  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2318  *
2319  * The sd_chain_index_map[] array is used at attach time to set the various
2320  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2321  * chain to be used with the instance. This allows different instances to use
2322  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2323  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2324  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2325  * dynamically & without the use of locking; and (2) a layer may update the
2326  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2327  * to allow for deferred processing of an IO within the same chain from a
2328  * different execution context.
2329  */
2330 
2331 struct sd_chain_index {
2332 	int	sci_iostart_index;
2333 	int	sci_iodone_index;
2334 };
2335 
2336 static struct sd_chain_index	sd_chain_index_map[] = {
2337 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2338 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2339 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2340 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2341 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2342 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2343 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2344 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2345 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2346 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2347 	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2348 	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2349 
2350 };
2351 
2352 
2353 /*
2354  * The following are indexes into the sd_chain_index_map[] array.
2355  */
2356 
2357 /* un->un_buf_chain_type must be set to one of these */
2358 #define	SD_CHAIN_INFO_DISK		0
2359 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2360 #define	SD_CHAIN_INFO_RMMEDIA		2
2361 #define	SD_CHAIN_INFO_MSS_DISK		2
2362 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2363 #define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2364 #define	SD_CHAIN_INFO_CHKSUM		4
2365 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2366 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2367 #define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2368 
2369 /* un->un_uscsi_chain_type must be set to one of these */
2370 #define	SD_CHAIN_INFO_USCSI_CMD		6
2371 /* USCSI with PM disabled is the same as DIRECT */
2372 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2373 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2374 
2375 /* un->un_direct_chain_type must be set to one of these */
2376 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2377 
2378 /* un->un_priority_chain_type must be set to one of these */
2379 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2380 
2381 /* size for devid inquiries */
2382 #define	MAX_INQUIRY_SIZE		0xF0
2383 
2384 /*
2385  * Macros used by functions to pass a given buf(9S) struct along to the
2386  * next function in the layering chain for further processing.
2387  *
2388  * In the following macros, passing more than three arguments to the called
2389  * routines causes the optimizer for the SPARC compiler to stop doing tail
2390  * call elimination which results in significant performance degradation.
2391  */
2392 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2393 	((*(sd_iostart_chain[index]))(index, un, bp))
2394 
2395 #define	SD_BEGIN_IODONE(index, un, bp)	\
2396 	((*(sd_iodone_chain[index]))(index, un, bp))
2397 
2398 #define	SD_NEXT_IOSTART(index, un, bp)				\
2399 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2400 
2401 #define	SD_NEXT_IODONE(index, un, bp)				\
2402 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2403 
2404 /*
2405  *    Function: _init
2406  *
2407  * Description: This is the driver _init(9E) entry point.
2408  *
2409  * Return Code: Returns the value from mod_install(9F) or
2410  *		ddi_soft_state_init(9F) as appropriate.
2411  *
2412  *     Context: Called when driver module loaded.
2413  */
2414 
2415 int
2416 _init(void)
2417 {
2418 	int	err;
2419 
2420 	/* establish driver name from module name */
2421 	sd_label = (char *)mod_modname(&modlinkage);
2422 
2423 #ifndef XPV_HVM_DRIVER
2424 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2425 	    SD_MAXUNIT);
2426 	if (err != 0) {
2427 		return (err);
2428 	}
2429 
2430 #else /* XPV_HVM_DRIVER */
2431 	/* Remove the leading "hvm_" from the module name */
2432 	ASSERT(strncmp(sd_label, "hvm_", strlen("hvm_")) == 0);
2433 	sd_label += strlen("hvm_");
2434 
2435 #endif /* XPV_HVM_DRIVER */
2436 
2437 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2438 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2439 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2440 
2441 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2442 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2443 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2444 
2445 	/*
2446 	 * it's ok to init here even for fibre device
2447 	 */
2448 	sd_scsi_probe_cache_init();
2449 
2450 	sd_scsi_target_lun_init();
2451 
2452 	/*
2453 	 * Creating taskq before mod_install ensures that all callers (threads)
2454 	 * that enter the module after a successful mod_install encounter
2455 	 * a valid taskq.
2456 	 */
2457 	sd_taskq_create();
2458 
2459 	err = mod_install(&modlinkage);
2460 	if (err != 0) {
2461 		/* delete taskq if install fails */
2462 		sd_taskq_delete();
2463 
2464 		mutex_destroy(&sd_detach_mutex);
2465 		mutex_destroy(&sd_log_mutex);
2466 		mutex_destroy(&sd_label_mutex);
2467 
2468 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2469 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2470 		cv_destroy(&sd_tr.srq_inprocess_cv);
2471 
2472 		sd_scsi_probe_cache_fini();
2473 
2474 		sd_scsi_target_lun_fini();
2475 
2476 #ifndef XPV_HVM_DRIVER
2477 		ddi_soft_state_fini(&sd_state);
2478 #endif /* !XPV_HVM_DRIVER */
2479 		return (err);
2480 	}
2481 
2482 	return (err);
2483 }
2484 
2485 
2486 /*
2487  *    Function: _fini
2488  *
2489  * Description: This is the driver _fini(9E) entry point.
2490  *
2491  * Return Code: Returns the value from mod_remove(9F)
2492  *
2493  *     Context: Called when driver module is unloaded.
2494  */
2495 
2496 int
2497 _fini(void)
2498 {
2499 	int err;
2500 
2501 	if ((err = mod_remove(&modlinkage)) != 0) {
2502 		return (err);
2503 	}
2504 
2505 	sd_taskq_delete();
2506 
2507 	mutex_destroy(&sd_detach_mutex);
2508 	mutex_destroy(&sd_log_mutex);
2509 	mutex_destroy(&sd_label_mutex);
2510 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2511 
2512 	sd_scsi_probe_cache_fini();
2513 
2514 	sd_scsi_target_lun_fini();
2515 
2516 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2517 	cv_destroy(&sd_tr.srq_inprocess_cv);
2518 
2519 #ifndef XPV_HVM_DRIVER
2520 	ddi_soft_state_fini(&sd_state);
2521 #endif /* !XPV_HVM_DRIVER */
2522 
2523 	return (err);
2524 }
2525 
2526 
2527 /*
2528  *    Function: _info
2529  *
2530  * Description: This is the driver _info(9E) entry point.
2531  *
2532  *   Arguments: modinfop - pointer to the driver modinfo structure
2533  *
2534  * Return Code: Returns the value from mod_info(9F).
2535  *
2536  *     Context: Kernel thread context
2537  */
2538 
2539 int
2540 _info(struct modinfo *modinfop)
2541 {
2542 	return (mod_info(&modlinkage, modinfop));
2543 }
2544 
2545 
2546 /*
2547  * The following routines implement the driver message logging facility.
2548  * They provide component- and level- based debug output filtering.
2549  * Output may also be restricted to messages for a single instance by
2550  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2551  * to NULL, then messages for all instances are printed.
2552  *
2553  * These routines have been cloned from each other due to the language
2554  * constraints of macros and variable argument list processing.
2555  */
2556 
2557 
2558 /*
2559  *    Function: sd_log_err
2560  *
2561  * Description: This routine is called by the SD_ERROR macro for debug
2562  *		logging of error conditions.
2563  *
2564  *   Arguments: comp - driver component being logged
2565  *		dev  - pointer to driver info structure
2566  *		fmt  - error string and format to be logged
2567  */
2568 
2569 static void
2570 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2571 {
2572 	va_list		ap;
2573 	dev_info_t	*dev;
2574 
2575 	ASSERT(un != NULL);
2576 	dev = SD_DEVINFO(un);
2577 	ASSERT(dev != NULL);
2578 
2579 	/*
2580 	 * Filter messages based on the global component and level masks.
2581 	 * Also print if un matches the value of sd_debug_un, or if
2582 	 * sd_debug_un is set to NULL.
2583 	 */
2584 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2585 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2586 		mutex_enter(&sd_log_mutex);
2587 		va_start(ap, fmt);
2588 		(void) vsprintf(sd_log_buf, fmt, ap);
2589 		va_end(ap);
2590 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2591 		mutex_exit(&sd_log_mutex);
2592 	}
2593 #ifdef SD_FAULT_INJECTION
2594 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2595 	if (un->sd_injection_mask & comp) {
2596 		mutex_enter(&sd_log_mutex);
2597 		va_start(ap, fmt);
2598 		(void) vsprintf(sd_log_buf, fmt, ap);
2599 		va_end(ap);
2600 		sd_injection_log(sd_log_buf, un);
2601 		mutex_exit(&sd_log_mutex);
2602 	}
2603 #endif
2604 }
2605 
2606 
2607 /*
2608  *    Function: sd_log_info
2609  *
2610  * Description: This routine is called by the SD_INFO macro for debug
2611  *		logging of general purpose informational conditions.
2612  *
2613  *   Arguments: comp - driver component being logged
2614  *		dev  - pointer to driver info structure
2615  *		fmt  - info string and format to be logged
2616  */
2617 
2618 static void
2619 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2620 {
2621 	va_list		ap;
2622 	dev_info_t	*dev;
2623 
2624 	ASSERT(un != NULL);
2625 	dev = SD_DEVINFO(un);
2626 	ASSERT(dev != NULL);
2627 
2628 	/*
2629 	 * Filter messages based on the global component and level masks.
2630 	 * Also print if un matches the value of sd_debug_un, or if
2631 	 * sd_debug_un is set to NULL.
2632 	 */
2633 	if ((sd_component_mask & component) &&
2634 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2635 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2636 		mutex_enter(&sd_log_mutex);
2637 		va_start(ap, fmt);
2638 		(void) vsprintf(sd_log_buf, fmt, ap);
2639 		va_end(ap);
2640 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2641 		mutex_exit(&sd_log_mutex);
2642 	}
2643 #ifdef SD_FAULT_INJECTION
2644 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2645 	if (un->sd_injection_mask & component) {
2646 		mutex_enter(&sd_log_mutex);
2647 		va_start(ap, fmt);
2648 		(void) vsprintf(sd_log_buf, fmt, ap);
2649 		va_end(ap);
2650 		sd_injection_log(sd_log_buf, un);
2651 		mutex_exit(&sd_log_mutex);
2652 	}
2653 #endif
2654 }
2655 
2656 
2657 /*
2658  *    Function: sd_log_trace
2659  *
2660  * Description: This routine is called by the SD_TRACE macro for debug
2661  *		logging of trace conditions (i.e. function entry/exit).
2662  *
2663  *   Arguments: comp - driver component being logged
2664  *		dev  - pointer to driver info structure
2665  *		fmt  - trace string and format to be logged
2666  */
2667 
2668 static void
2669 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2670 {
2671 	va_list		ap;
2672 	dev_info_t	*dev;
2673 
2674 	ASSERT(un != NULL);
2675 	dev = SD_DEVINFO(un);
2676 	ASSERT(dev != NULL);
2677 
2678 	/*
2679 	 * Filter messages based on the global component and level masks.
2680 	 * Also print if un matches the value of sd_debug_un, or if
2681 	 * sd_debug_un is set to NULL.
2682 	 */
2683 	if ((sd_component_mask & component) &&
2684 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2685 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2686 		mutex_enter(&sd_log_mutex);
2687 		va_start(ap, fmt);
2688 		(void) vsprintf(sd_log_buf, fmt, ap);
2689 		va_end(ap);
2690 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2691 		mutex_exit(&sd_log_mutex);
2692 	}
2693 #ifdef SD_FAULT_INJECTION
2694 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2695 	if (un->sd_injection_mask & component) {
2696 		mutex_enter(&sd_log_mutex);
2697 		va_start(ap, fmt);
2698 		(void) vsprintf(sd_log_buf, fmt, ap);
2699 		va_end(ap);
2700 		sd_injection_log(sd_log_buf, un);
2701 		mutex_exit(&sd_log_mutex);
2702 	}
2703 #endif
2704 }
2705 
2706 
2707 /*
2708  *    Function: sdprobe
2709  *
2710  * Description: This is the driver probe(9e) entry point function.
2711  *
2712  *   Arguments: devi - opaque device info handle
2713  *
2714  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2715  *              DDI_PROBE_FAILURE: If the probe failed.
2716  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2717  *				   but may be present in the future.
2718  */
2719 
2720 static int
2721 sdprobe(dev_info_t *devi)
2722 {
2723 	struct scsi_device	*devp;
2724 	int			rval;
2725 #ifndef XPV_HVM_DRIVER
2726 	int			instance = ddi_get_instance(devi);
2727 #endif /* !XPV_HVM_DRIVER */
2728 
2729 	/*
2730 	 * if it wasn't for pln, sdprobe could actually be nulldev
2731 	 * in the "__fibre" case.
2732 	 */
2733 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2734 		return (DDI_PROBE_DONTCARE);
2735 	}
2736 
2737 	devp = ddi_get_driver_private(devi);
2738 
2739 	if (devp == NULL) {
2740 		/* Ooops... nexus driver is mis-configured... */
2741 		return (DDI_PROBE_FAILURE);
2742 	}
2743 
2744 #ifndef XPV_HVM_DRIVER
2745 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2746 		return (DDI_PROBE_PARTIAL);
2747 	}
2748 #endif /* !XPV_HVM_DRIVER */
2749 
2750 	/*
2751 	 * Call the SCSA utility probe routine to see if we actually
2752 	 * have a target at this SCSI nexus.
2753 	 */
2754 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2755 	case SCSIPROBE_EXISTS:
2756 		switch (devp->sd_inq->inq_dtype) {
2757 		case DTYPE_DIRECT:
2758 			rval = DDI_PROBE_SUCCESS;
2759 			break;
2760 		case DTYPE_RODIRECT:
2761 			/* CDs etc. Can be removable media */
2762 			rval = DDI_PROBE_SUCCESS;
2763 			break;
2764 		case DTYPE_OPTICAL:
2765 			/*
2766 			 * Rewritable optical driver HP115AA
2767 			 * Can also be removable media
2768 			 */
2769 
2770 			/*
2771 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2772 			 * pre solaris 9 sparc sd behavior is required
2773 			 *
2774 			 * If first time through and sd_dtype_optical_bind
2775 			 * has not been set in /etc/system check properties
2776 			 */
2777 
2778 			if (sd_dtype_optical_bind  < 0) {
2779 				sd_dtype_optical_bind = ddi_prop_get_int
2780 				    (DDI_DEV_T_ANY, devi, 0,
2781 				    "optical-device-bind", 1);
2782 			}
2783 
2784 			if (sd_dtype_optical_bind == 0) {
2785 				rval = DDI_PROBE_FAILURE;
2786 			} else {
2787 				rval = DDI_PROBE_SUCCESS;
2788 			}
2789 			break;
2790 
2791 		case DTYPE_NOTPRESENT:
2792 		default:
2793 			rval = DDI_PROBE_FAILURE;
2794 			break;
2795 		}
2796 		break;
2797 	default:
2798 		rval = DDI_PROBE_PARTIAL;
2799 		break;
2800 	}
2801 
2802 	/*
2803 	 * This routine checks for resource allocation prior to freeing,
2804 	 * so it will take care of the "smart probing" case where a
2805 	 * scsi_probe() may or may not have been issued and will *not*
2806 	 * free previously-freed resources.
2807 	 */
2808 	scsi_unprobe(devp);
2809 	return (rval);
2810 }
2811 
2812 
2813 /*
2814  *    Function: sdinfo
2815  *
2816  * Description: This is the driver getinfo(9e) entry point function.
2817  * 		Given the device number, return the devinfo pointer from
2818  *		the scsi_device structure or the instance number
2819  *		associated with the dev_t.
2820  *
2821  *   Arguments: dip     - pointer to device info structure
2822  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2823  *			  DDI_INFO_DEVT2INSTANCE)
2824  *		arg     - driver dev_t
2825  *		resultp - user buffer for request response
2826  *
2827  * Return Code: DDI_SUCCESS
2828  *              DDI_FAILURE
2829  */
2830 /* ARGSUSED */
2831 static int
2832 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2833 {
2834 	struct sd_lun	*un;
2835 	dev_t		dev;
2836 	int		instance;
2837 	int		error;
2838 
2839 	switch (infocmd) {
2840 	case DDI_INFO_DEVT2DEVINFO:
2841 		dev = (dev_t)arg;
2842 		instance = SDUNIT(dev);
2843 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2844 			return (DDI_FAILURE);
2845 		}
2846 		*result = (void *) SD_DEVINFO(un);
2847 		error = DDI_SUCCESS;
2848 		break;
2849 	case DDI_INFO_DEVT2INSTANCE:
2850 		dev = (dev_t)arg;
2851 		instance = SDUNIT(dev);
2852 		*result = (void *)(uintptr_t)instance;
2853 		error = DDI_SUCCESS;
2854 		break;
2855 	default:
2856 		error = DDI_FAILURE;
2857 	}
2858 	return (error);
2859 }
2860 
2861 /*
2862  *    Function: sd_prop_op
2863  *
2864  * Description: This is the driver prop_op(9e) entry point function.
2865  *		Return the number of blocks for the partition in question
2866  *		or forward the request to the property facilities.
2867  *
2868  *   Arguments: dev       - device number
2869  *		dip       - pointer to device info structure
2870  *		prop_op   - property operator
2871  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2872  *		name      - pointer to property name
2873  *		valuep    - pointer or address of the user buffer
2874  *		lengthp   - property length
2875  *
2876  * Return Code: DDI_PROP_SUCCESS
2877  *              DDI_PROP_NOT_FOUND
2878  *              DDI_PROP_UNDEFINED
2879  *              DDI_PROP_NO_MEMORY
2880  *              DDI_PROP_BUF_TOO_SMALL
2881  */
2882 
2883 static int
2884 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2885 	char *name, caddr_t valuep, int *lengthp)
2886 {
2887 	struct sd_lun	*un;
2888 
2889 	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2890 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2891 		    name, valuep, lengthp));
2892 
2893 	return (cmlb_prop_op(un->un_cmlbhandle,
2894 	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2895 	    SDPART(dev), (void *)SD_PATH_DIRECT));
2896 }
2897 
2898 /*
2899  * The following functions are for smart probing:
2900  * sd_scsi_probe_cache_init()
2901  * sd_scsi_probe_cache_fini()
2902  * sd_scsi_clear_probe_cache()
2903  * sd_scsi_probe_with_cache()
2904  */
2905 
2906 /*
2907  *    Function: sd_scsi_probe_cache_init
2908  *
2909  * Description: Initializes the probe response cache mutex and head pointer.
2910  *
2911  *     Context: Kernel thread context
2912  */
2913 
2914 static void
2915 sd_scsi_probe_cache_init(void)
2916 {
2917 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2918 	sd_scsi_probe_cache_head = NULL;
2919 }
2920 
2921 
2922 /*
2923  *    Function: sd_scsi_probe_cache_fini
2924  *
2925  * Description: Frees all resources associated with the probe response cache.
2926  *
2927  *     Context: Kernel thread context
2928  */
2929 
2930 static void
2931 sd_scsi_probe_cache_fini(void)
2932 {
2933 	struct sd_scsi_probe_cache *cp;
2934 	struct sd_scsi_probe_cache *ncp;
2935 
2936 	/* Clean up our smart probing linked list */
2937 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2938 		ncp = cp->next;
2939 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2940 	}
2941 	sd_scsi_probe_cache_head = NULL;
2942 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2943 }
2944 
2945 
2946 /*
2947  *    Function: sd_scsi_clear_probe_cache
2948  *
2949  * Description: This routine clears the probe response cache. This is
2950  *		done when open() returns ENXIO so that when deferred
2951  *		attach is attempted (possibly after a device has been
2952  *		turned on) we will retry the probe. Since we don't know
2953  *		which target we failed to open, we just clear the
2954  *		entire cache.
2955  *
2956  *     Context: Kernel thread context
2957  */
2958 
2959 static void
2960 sd_scsi_clear_probe_cache(void)
2961 {
2962 	struct sd_scsi_probe_cache	*cp;
2963 	int				i;
2964 
2965 	mutex_enter(&sd_scsi_probe_cache_mutex);
2966 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2967 		/*
2968 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2969 		 * force probing to be performed the next time
2970 		 * sd_scsi_probe_with_cache is called.
2971 		 */
2972 		for (i = 0; i < NTARGETS_WIDE; i++) {
2973 			cp->cache[i] = SCSIPROBE_EXISTS;
2974 		}
2975 	}
2976 	mutex_exit(&sd_scsi_probe_cache_mutex);
2977 }
2978 
2979 
2980 /*
2981  *    Function: sd_scsi_probe_with_cache
2982  *
2983  * Description: This routine implements support for a scsi device probe
2984  *		with cache. The driver maintains a cache of the target
2985  *		responses to scsi probes. If we get no response from a
2986  *		target during a probe inquiry, we remember that, and we
2987  *		avoid additional calls to scsi_probe on non-zero LUNs
2988  *		on the same target until the cache is cleared. By doing
2989  *		so we avoid the 1/4 sec selection timeout for nonzero
2990  *		LUNs. lun0 of a target is always probed.
2991  *
2992  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2993  *              waitfunc - indicates what the allocator routines should
2994  *			   do when resources are not available. This value
2995  *			   is passed on to scsi_probe() when that routine
2996  *			   is called.
2997  *
2998  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2999  *		otherwise the value returned by scsi_probe(9F).
3000  *
3001  *     Context: Kernel thread context
3002  */
3003 
3004 static int
3005 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3006 {
3007 	struct sd_scsi_probe_cache	*cp;
3008 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
3009 	int		lun, tgt;
3010 
3011 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3012 	    SCSI_ADDR_PROP_LUN, 0);
3013 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3014 	    SCSI_ADDR_PROP_TARGET, -1);
3015 
3016 	/* Make sure caching enabled and target in range */
3017 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3018 		/* do it the old way (no cache) */
3019 		return (scsi_probe(devp, waitfn));
3020 	}
3021 
3022 	mutex_enter(&sd_scsi_probe_cache_mutex);
3023 
3024 	/* Find the cache for this scsi bus instance */
3025 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3026 		if (cp->pdip == pdip) {
3027 			break;
3028 		}
3029 	}
3030 
3031 	/* If we can't find a cache for this pdip, create one */
3032 	if (cp == NULL) {
3033 		int i;
3034 
3035 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3036 		    KM_SLEEP);
3037 		cp->pdip = pdip;
3038 		cp->next = sd_scsi_probe_cache_head;
3039 		sd_scsi_probe_cache_head = cp;
3040 		for (i = 0; i < NTARGETS_WIDE; i++) {
3041 			cp->cache[i] = SCSIPROBE_EXISTS;
3042 		}
3043 	}
3044 
3045 	mutex_exit(&sd_scsi_probe_cache_mutex);
3046 
3047 	/* Recompute the cache for this target if LUN zero */
3048 	if (lun == 0) {
3049 		cp->cache[tgt] = SCSIPROBE_EXISTS;
3050 	}
3051 
3052 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3053 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3054 		return (SCSIPROBE_NORESP);
3055 	}
3056 
3057 	/* Do the actual probe; save & return the result */
3058 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3059 }
3060 
3061 
3062 /*
3063  *    Function: sd_scsi_target_lun_init
3064  *
3065  * Description: Initializes the attached lun chain mutex and head pointer.
3066  *
3067  *     Context: Kernel thread context
3068  */
3069 
3070 static void
3071 sd_scsi_target_lun_init(void)
3072 {
3073 	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3074 	sd_scsi_target_lun_head = NULL;
3075 }
3076 
3077 
3078 /*
3079  *    Function: sd_scsi_target_lun_fini
3080  *
3081  * Description: Frees all resources associated with the attached lun
3082  *              chain
3083  *
3084  *     Context: Kernel thread context
3085  */
3086 
3087 static void
3088 sd_scsi_target_lun_fini(void)
3089 {
3090 	struct sd_scsi_hba_tgt_lun	*cp;
3091 	struct sd_scsi_hba_tgt_lun	*ncp;
3092 
3093 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3094 		ncp = cp->next;
3095 		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3096 	}
3097 	sd_scsi_target_lun_head = NULL;
3098 	mutex_destroy(&sd_scsi_target_lun_mutex);
3099 }
3100 
3101 
3102 /*
3103  *    Function: sd_scsi_get_target_lun_count
3104  *
3105  * Description: This routine will check in the attached lun chain to see
3106  * 		how many luns are attached on the required SCSI controller
3107  * 		and target. Currently, some capabilities like tagged queue
3108  *		are supported per target based by HBA. So all luns in a
3109  *		target have the same capabilities. Based on this assumption,
3110  * 		sd should only set these capabilities once per target. This
3111  *		function is called when sd needs to decide how many luns
3112  *		already attached on a target.
3113  *
3114  *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3115  *			  controller device.
3116  *              target	- The target ID on the controller's SCSI bus.
3117  *
3118  * Return Code: The number of luns attached on the required target and
3119  *		controller.
3120  *		-1 if target ID is not in parallel SCSI scope or the given
3121  * 		dip is not in the chain.
3122  *
3123  *     Context: Kernel thread context
3124  */
3125 
3126 static int
3127 sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3128 {
3129 	struct sd_scsi_hba_tgt_lun	*cp;
3130 
3131 	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3132 		return (-1);
3133 	}
3134 
3135 	mutex_enter(&sd_scsi_target_lun_mutex);
3136 
3137 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3138 		if (cp->pdip == dip) {
3139 			break;
3140 		}
3141 	}
3142 
3143 	mutex_exit(&sd_scsi_target_lun_mutex);
3144 
3145 	if (cp == NULL) {
3146 		return (-1);
3147 	}
3148 
3149 	return (cp->nlun[target]);
3150 }
3151 
3152 
3153 /*
3154  *    Function: sd_scsi_update_lun_on_target
3155  *
3156  * Description: This routine is used to update the attached lun chain when a
3157  *		lun is attached or detached on a target.
3158  *
3159  *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3160  *                        controller device.
3161  *              target  - The target ID on the controller's SCSI bus.
3162  *		flag	- Indicate the lun is attached or detached.
3163  *
3164  *     Context: Kernel thread context
3165  */
3166 
3167 static void
3168 sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3169 {
3170 	struct sd_scsi_hba_tgt_lun	*cp;
3171 
3172 	mutex_enter(&sd_scsi_target_lun_mutex);
3173 
3174 	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3175 		if (cp->pdip == dip) {
3176 			break;
3177 		}
3178 	}
3179 
3180 	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3181 		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3182 		    KM_SLEEP);
3183 		cp->pdip = dip;
3184 		cp->next = sd_scsi_target_lun_head;
3185 		sd_scsi_target_lun_head = cp;
3186 	}
3187 
3188 	mutex_exit(&sd_scsi_target_lun_mutex);
3189 
3190 	if (cp != NULL) {
3191 		if (flag == SD_SCSI_LUN_ATTACH) {
3192 			cp->nlun[target] ++;
3193 		} else {
3194 			cp->nlun[target] --;
3195 		}
3196 	}
3197 }
3198 
3199 
3200 /*
3201  *    Function: sd_spin_up_unit
3202  *
3203  * Description: Issues the following commands to spin-up the device:
3204  *		START STOP UNIT, and INQUIRY.
3205  *
3206  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3207  *                      structure for this target.
3208  *
3209  * Return Code: 0 - success
3210  *		EIO - failure
3211  *		EACCES - reservation conflict
3212  *
3213  *     Context: Kernel thread context
3214  */
3215 
3216 static int
3217 sd_spin_up_unit(sd_ssc_t *ssc)
3218 {
3219 	size_t	resid		= 0;
3220 	int	has_conflict	= FALSE;
3221 	uchar_t *bufaddr;
3222 	int 	status;
3223 	struct sd_lun	*un;
3224 
3225 	ASSERT(ssc != NULL);
3226 	un = ssc->ssc_un;
3227 	ASSERT(un != NULL);
3228 
3229 	/*
3230 	 * Send a throwaway START UNIT command.
3231 	 *
3232 	 * If we fail on this, we don't care presently what precisely
3233 	 * is wrong.  EMC's arrays will also fail this with a check
3234 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3235 	 * we don't want to fail the attach because it may become
3236 	 * "active" later.
3237 	 * We don't know if power condition is supported or not at
3238 	 * this stage, use START STOP bit.
3239 	 */
3240 	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3241 	    SD_TARGET_START, SD_PATH_DIRECT);
3242 
3243 	if (status != 0) {
3244 		if (status == EACCES)
3245 			has_conflict = TRUE;
3246 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3247 	}
3248 
3249 	/*
3250 	 * Send another INQUIRY command to the target. This is necessary for
3251 	 * non-removable media direct access devices because their INQUIRY data
3252 	 * may not be fully qualified until they are spun up (perhaps via the
3253 	 * START command above).  Note: This seems to be needed for some
3254 	 * legacy devices only.) The INQUIRY command should succeed even if a
3255 	 * Reservation Conflict is present.
3256 	 */
3257 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3258 
3259 	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3260 	    != 0) {
3261 		kmem_free(bufaddr, SUN_INQSIZE);
3262 		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3263 		return (EIO);
3264 	}
3265 
3266 	/*
3267 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3268 	 * Note that this routine does not return a failure here even if the
3269 	 * INQUIRY command did not return any data.  This is a legacy behavior.
3270 	 */
3271 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3272 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3273 	}
3274 
3275 	kmem_free(bufaddr, SUN_INQSIZE);
3276 
3277 	/* If we hit a reservation conflict above, tell the caller. */
3278 	if (has_conflict == TRUE) {
3279 		return (EACCES);
3280 	}
3281 
3282 	return (0);
3283 }
3284 
3285 #ifdef _LP64
3286 /*
3287  *    Function: sd_enable_descr_sense
3288  *
3289  * Description: This routine attempts to select descriptor sense format
3290  *		using the Control mode page.  Devices that support 64 bit
3291  *		LBAs (for >2TB luns) should also implement descriptor
3292  *		sense data so we will call this function whenever we see
3293  *		a lun larger than 2TB.  If for some reason the device
3294  *		supports 64 bit LBAs but doesn't support descriptor sense
3295  *		presumably the mode select will fail.  Everything will
3296  *		continue to work normally except that we will not get
3297  *		complete sense data for commands that fail with an LBA
3298  *		larger than 32 bits.
3299  *
3300  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3301  *                      structure for this target.
3302  *
3303  *     Context: Kernel thread context only
3304  */
3305 
3306 static void
3307 sd_enable_descr_sense(sd_ssc_t *ssc)
3308 {
3309 	uchar_t			*header;
3310 	struct mode_control_scsi3 *ctrl_bufp;
3311 	size_t			buflen;
3312 	size_t			bd_len;
3313 	int			status;
3314 	struct sd_lun		*un;
3315 
3316 	ASSERT(ssc != NULL);
3317 	un = ssc->ssc_un;
3318 	ASSERT(un != NULL);
3319 
3320 	/*
3321 	 * Read MODE SENSE page 0xA, Control Mode Page
3322 	 */
3323 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3324 	    sizeof (struct mode_control_scsi3);
3325 	header = kmem_zalloc(buflen, KM_SLEEP);
3326 
3327 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3328 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3329 
3330 	if (status != 0) {
3331 		SD_ERROR(SD_LOG_COMMON, un,
3332 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3333 		goto eds_exit;
3334 	}
3335 
3336 	/*
3337 	 * Determine size of Block Descriptors in order to locate
3338 	 * the mode page data. ATAPI devices return 0, SCSI devices
3339 	 * should return MODE_BLK_DESC_LENGTH.
3340 	 */
3341 	bd_len  = ((struct mode_header *)header)->bdesc_length;
3342 
3343 	/* Clear the mode data length field for MODE SELECT */
3344 	((struct mode_header *)header)->length = 0;
3345 
3346 	ctrl_bufp = (struct mode_control_scsi3 *)
3347 	    (header + MODE_HEADER_LENGTH + bd_len);
3348 
3349 	/*
3350 	 * If the page length is smaller than the expected value,
3351 	 * the target device doesn't support D_SENSE. Bail out here.
3352 	 */
3353 	if (ctrl_bufp->mode_page.length <
3354 	    sizeof (struct mode_control_scsi3) - 2) {
3355 		SD_ERROR(SD_LOG_COMMON, un,
3356 		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3357 		goto eds_exit;
3358 	}
3359 
3360 	/*
3361 	 * Clear PS bit for MODE SELECT
3362 	 */
3363 	ctrl_bufp->mode_page.ps = 0;
3364 
3365 	/*
3366 	 * Set D_SENSE to enable descriptor sense format.
3367 	 */
3368 	ctrl_bufp->d_sense = 1;
3369 
3370 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3371 
3372 	/*
3373 	 * Use MODE SELECT to commit the change to the D_SENSE bit
3374 	 */
3375 	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3376 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3377 
3378 	if (status != 0) {
3379 		SD_INFO(SD_LOG_COMMON, un,
3380 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3381 	} else {
3382 		kmem_free(header, buflen);
3383 		return;
3384 	}
3385 
3386 eds_exit:
3387 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3388 	kmem_free(header, buflen);
3389 }
3390 
3391 /*
3392  *    Function: sd_reenable_dsense_task
3393  *
3394  * Description: Re-enable descriptor sense after device or bus reset
3395  *
3396  *     Context: Executes in a taskq() thread context
3397  */
3398 static void
3399 sd_reenable_dsense_task(void *arg)
3400 {
3401 	struct	sd_lun	*un = arg;
3402 	sd_ssc_t	*ssc;
3403 
3404 	ASSERT(un != NULL);
3405 
3406 	ssc = sd_ssc_init(un);
3407 	sd_enable_descr_sense(ssc);
3408 	sd_ssc_fini(ssc);
3409 }
3410 #endif /* _LP64 */
3411 
3412 /*
3413  *    Function: sd_set_mmc_caps
3414  *
3415  * Description: This routine determines if the device is MMC compliant and if
3416  *		the device supports CDDA via a mode sense of the CDVD
3417  *		capabilities mode page. Also checks if the device is a
3418  *		dvdram writable device.
3419  *
3420  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3421  *                      structure for this target.
3422  *
3423  *     Context: Kernel thread context only
3424  */
3425 
3426 static void
3427 sd_set_mmc_caps(sd_ssc_t *ssc)
3428 {
3429 	struct mode_header_grp2		*sense_mhp;
3430 	uchar_t				*sense_page;
3431 	caddr_t				buf;
3432 	int				bd_len;
3433 	int				status;
3434 	struct uscsi_cmd		com;
3435 	int				rtn;
3436 	uchar_t				*out_data_rw, *out_data_hd;
3437 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3438 	uchar_t				*out_data_gesn;
3439 	int				gesn_len;
3440 	struct sd_lun			*un;
3441 
3442 	ASSERT(ssc != NULL);
3443 	un = ssc->ssc_un;
3444 	ASSERT(un != NULL);
3445 
3446 	/*
3447 	 * The flags which will be set in this function are - mmc compliant,
3448 	 * dvdram writable device, cdda support. Initialize them to FALSE
3449 	 * and if a capability is detected - it will be set to TRUE.
3450 	 */
3451 	un->un_f_mmc_cap = FALSE;
3452 	un->un_f_dvdram_writable_device = FALSE;
3453 	un->un_f_cfg_cdda = FALSE;
3454 
3455 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3456 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3457 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3458 
3459 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3460 
3461 	if (status != 0) {
3462 		/* command failed; just return */
3463 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3464 		return;
3465 	}
3466 	/*
3467 	 * If the mode sense request for the CDROM CAPABILITIES
3468 	 * page (0x2A) succeeds the device is assumed to be MMC.
3469 	 */
3470 	un->un_f_mmc_cap = TRUE;
3471 
3472 	/* See if GET STATUS EVENT NOTIFICATION is supported */
3473 	if (un->un_f_mmc_gesn_polling) {
3474 		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3475 		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3476 
3477 		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3478 		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3479 
3480 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3481 
3482 		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3483 			un->un_f_mmc_gesn_polling = FALSE;
3484 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3485 			    "sd_set_mmc_caps: gesn not supported "
3486 			    "%d %x %x %x %x\n", rtn,
3487 			    out_data_gesn[0], out_data_gesn[1],
3488 			    out_data_gesn[2], out_data_gesn[3]);
3489 		}
3490 
3491 		kmem_free(out_data_gesn, gesn_len);
3492 	}
3493 
3494 	/* Get to the page data */
3495 	sense_mhp = (struct mode_header_grp2 *)buf;
3496 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3497 	    sense_mhp->bdesc_length_lo;
3498 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3499 		/*
3500 		 * We did not get back the expected block descriptor
3501 		 * length so we cannot determine if the device supports
3502 		 * CDDA. However, we still indicate the device is MMC
3503 		 * according to the successful response to the page
3504 		 * 0x2A mode sense request.
3505 		 */
3506 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3507 		    "sd_set_mmc_caps: Mode Sense returned "
3508 		    "invalid block descriptor length\n");
3509 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3510 		return;
3511 	}
3512 
3513 	/* See if read CDDA is supported */
3514 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3515 	    bd_len);
3516 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3517 
3518 	/* See if writing DVD RAM is supported. */
3519 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3520 	if (un->un_f_dvdram_writable_device == TRUE) {
3521 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3522 		return;
3523 	}
3524 
3525 	/*
3526 	 * If the device presents DVD or CD capabilities in the mode
3527 	 * page, we can return here since a RRD will not have
3528 	 * these capabilities.
3529 	 */
3530 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3531 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3532 		return;
3533 	}
3534 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3535 
3536 	/*
3537 	 * If un->un_f_dvdram_writable_device is still FALSE,
3538 	 * check for a Removable Rigid Disk (RRD).  A RRD
3539 	 * device is identified by the features RANDOM_WRITABLE and
3540 	 * HARDWARE_DEFECT_MANAGEMENT.
3541 	 */
3542 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3543 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3544 
3545 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3546 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3547 	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3548 
3549 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3550 
3551 	if (rtn != 0) {
3552 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3553 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3554 		return;
3555 	}
3556 
3557 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3558 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3559 
3560 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3561 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3562 	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3563 
3564 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3565 
3566 	if (rtn == 0) {
3567 		/*
3568 		 * We have good information, check for random writable
3569 		 * and hardware defect features.
3570 		 */
3571 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3572 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3573 			un->un_f_dvdram_writable_device = TRUE;
3574 		}
3575 	}
3576 
3577 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3578 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3579 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3580 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3581 }
3582 
3583 /*
3584  *    Function: sd_check_for_writable_cd
3585  *
3586  * Description: This routine determines if the media in the device is
3587  *		writable or not. It uses the get configuration command (0x46)
3588  *		to determine if the media is writable
3589  *
3590  *   Arguments: un - driver soft state (unit) structure
3591  *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3592  *                           chain and the normal command waitq, or
3593  *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3594  *                           "direct" chain and bypass the normal command
3595  *                           waitq.
3596  *
3597  *     Context: Never called at interrupt context.
3598  */
3599 
3600 static void
3601 sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3602 {
3603 	struct uscsi_cmd		com;
3604 	uchar_t				*out_data;
3605 	uchar_t				*rqbuf;
3606 	int				rtn;
3607 	uchar_t				*out_data_rw, *out_data_hd;
3608 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3609 	struct mode_header_grp2		*sense_mhp;
3610 	uchar_t				*sense_page;
3611 	caddr_t				buf;
3612 	int				bd_len;
3613 	int				status;
3614 	struct sd_lun			*un;
3615 
3616 	ASSERT(ssc != NULL);
3617 	un = ssc->ssc_un;
3618 	ASSERT(un != NULL);
3619 	ASSERT(mutex_owned(SD_MUTEX(un)));
3620 
3621 	/*
3622 	 * Initialize the writable media to false, if configuration info.
3623 	 * tells us otherwise then only we will set it.
3624 	 */
3625 	un->un_f_mmc_writable_media = FALSE;
3626 	mutex_exit(SD_MUTEX(un));
3627 
3628 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3629 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3630 
3631 	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3632 	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
3633 
3634 	if (rtn != 0)
3635 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3636 
3637 	mutex_enter(SD_MUTEX(un));
3638 	if (rtn == 0) {
3639 		/*
3640 		 * We have good information, check for writable DVD.
3641 		 */
3642 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3643 			un->un_f_mmc_writable_media = TRUE;
3644 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3645 			kmem_free(rqbuf, SENSE_LENGTH);
3646 			return;
3647 		}
3648 	}
3649 
3650 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3651 	kmem_free(rqbuf, SENSE_LENGTH);
3652 
3653 	/*
3654 	 * Determine if this is a RRD type device.
3655 	 */
3656 	mutex_exit(SD_MUTEX(un));
3657 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3658 	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3659 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3660 
3661 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3662 
3663 	mutex_enter(SD_MUTEX(un));
3664 	if (status != 0) {
3665 		/* command failed; just return */
3666 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3667 		return;
3668 	}
3669 
3670 	/* Get to the page data */
3671 	sense_mhp = (struct mode_header_grp2 *)buf;
3672 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3673 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3674 		/*
3675 		 * We did not get back the expected block descriptor length so
3676 		 * we cannot check the mode page.
3677 		 */
3678 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3679 		    "sd_check_for_writable_cd: Mode Sense returned "
3680 		    "invalid block descriptor length\n");
3681 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3682 		return;
3683 	}
3684 
3685 	/*
3686 	 * If the device presents DVD or CD capabilities in the mode
3687 	 * page, we can return here since a RRD device will not have
3688 	 * these capabilities.
3689 	 */
3690 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3691 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3692 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3693 		return;
3694 	}
3695 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3696 
3697 	/*
3698 	 * If un->un_f_mmc_writable_media is still FALSE,
3699 	 * check for RRD type media.  A RRD device is identified
3700 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3701 	 */
3702 	mutex_exit(SD_MUTEX(un));
3703 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3704 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3705 
3706 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3707 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3708 	    RANDOM_WRITABLE, path_flag);
3709 
3710 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3711 	if (rtn != 0) {
3712 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3713 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3714 		mutex_enter(SD_MUTEX(un));
3715 		return;
3716 	}
3717 
3718 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3719 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3720 
3721 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3722 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3723 	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
3724 
3725 	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3726 	mutex_enter(SD_MUTEX(un));
3727 	if (rtn == 0) {
3728 		/*
3729 		 * We have good information, check for random writable
3730 		 * and hardware defect features as current.
3731 		 */
3732 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3733 		    (out_data_rw[10] & 0x1) &&
3734 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3735 		    (out_data_hd[10] & 0x1)) {
3736 			un->un_f_mmc_writable_media = TRUE;
3737 		}
3738 	}
3739 
3740 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3741 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3742 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3743 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3744 }
3745 
3746 /*
3747  *    Function: sd_read_unit_properties
3748  *
3749  * Description: The following implements a property lookup mechanism.
3750  *		Properties for particular disks (keyed on vendor, model
3751  *		and rev numbers) are sought in the sd.conf file via
3752  *		sd_process_sdconf_file(), and if not found there, are
3753  *		looked for in a list hardcoded in this driver via
3754  *		sd_process_sdconf_table() Once located the properties
3755  *		are used to update the driver unit structure.
3756  *
3757  *   Arguments: un - driver soft state (unit) structure
3758  */
3759 
3760 static void
3761 sd_read_unit_properties(struct sd_lun *un)
3762 {
3763 	/*
3764 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3765 	 * the "sd-config-list" property (from the sd.conf file) or if
3766 	 * there was not a match for the inquiry vid/pid. If this event
3767 	 * occurs the static driver configuration table is searched for
3768 	 * a match.
3769 	 */
3770 	ASSERT(un != NULL);
3771 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3772 		sd_process_sdconf_table(un);
3773 	}
3774 
3775 	/* check for LSI device */
3776 	sd_is_lsi(un);
3777 
3778 
3779 }
3780 
3781 
3782 /*
3783  *    Function: sd_process_sdconf_file
3784  *
3785  * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3786  *		driver's config file (ie, sd.conf) and update the driver
3787  *		soft state structure accordingly.
3788  *
3789  *   Arguments: un - driver soft state (unit) structure
3790  *
3791  * Return Code: SD_SUCCESS - The properties were successfully set according
3792  *			     to the driver configuration file.
3793  *		SD_FAILURE - The driver config list was not obtained or
3794  *			     there was no vid/pid match. This indicates that
3795  *			     the static config table should be used.
3796  *
3797  * The config file has a property, "sd-config-list". Currently we support
3798  * two kinds of formats. For both formats, the value of this property
3799  * is a list of duplets:
3800  *
3801  *  sd-config-list=
3802  *	<duplet>,
3803  *	[,<duplet>]*;
3804  *
3805  * For the improved format, where
3806  *
3807  *     <duplet>:= "<vid+pid>","<tunable-list>"
3808  *
3809  * and
3810  *
3811  *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3812  *     <tunable> =        <name> : <value>
3813  *
3814  * The <vid+pid> is the string that is returned by the target device on a
3815  * SCSI inquiry command, the <tunable-list> contains one or more tunables
3816  * to apply to all target devices with the specified <vid+pid>.
3817  *
3818  * Each <tunable> is a "<name> : <value>" pair.
3819  *
3820  * For the old format, the structure of each duplet is as follows:
3821  *
3822  *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3823  *
3824  * The first entry of the duplet is the device ID string (the concatenated
3825  * vid & pid; not to be confused with a device_id).  This is defined in
3826  * the same way as in the sd_disk_table.
3827  *
3828  * The second part of the duplet is a string that identifies a
3829  * data-property-name-list. The data-property-name-list is defined as
3830  * follows:
3831  *
3832  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3833  *
3834  * The syntax of <data-property-name> depends on the <version> field.
3835  *
3836  * If version = SD_CONF_VERSION_1 we have the following syntax:
3837  *
3838  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3839  *
3840  * where the prop0 value will be used to set prop0 if bit0 set in the
3841  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3842  *
3843  */
3844 
3845 static int
3846 sd_process_sdconf_file(struct sd_lun *un)
3847 {
3848 	char	**config_list = NULL;
3849 	uint_t	nelements;
3850 	char	*vidptr;
3851 	int	vidlen;
3852 	char	*dnlist_ptr;
3853 	char	*dataname_ptr;
3854 	char	*dataname_lasts;
3855 	int	*data_list = NULL;
3856 	uint_t	data_list_len;
3857 	int	rval = SD_FAILURE;
3858 	int	i;
3859 
3860 	ASSERT(un != NULL);
3861 
3862 	/* Obtain the configuration list associated with the .conf file */
3863 	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3864 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3865 	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
3866 		return (SD_FAILURE);
3867 	}
3868 
3869 	/*
3870 	 * Compare vids in each duplet to the inquiry vid - if a match is
3871 	 * made, get the data value and update the soft state structure
3872 	 * accordingly.
3873 	 *
3874 	 * Each duplet should show as a pair of strings, return SD_FAILURE
3875 	 * otherwise.
3876 	 */
3877 	if (nelements & 1) {
3878 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3879 		    "sd-config-list should show as pairs of strings.\n");
3880 		if (config_list)
3881 			ddi_prop_free(config_list);
3882 		return (SD_FAILURE);
3883 	}
3884 
3885 	for (i = 0; i < nelements; i += 2) {
3886 		/*
3887 		 * Note: The assumption here is that each vid entry is on
3888 		 * a unique line from its associated duplet.
3889 		 */
3890 		vidptr = config_list[i];
3891 		vidlen = (int)strlen(vidptr);
3892 		if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3893 			continue;
3894 		}
3895 
3896 		/*
3897 		 * dnlist contains 1 or more blank separated
3898 		 * data-property-name entries
3899 		 */
3900 		dnlist_ptr = config_list[i + 1];
3901 
3902 		if (strchr(dnlist_ptr, ':') != NULL) {
3903 			/*
3904 			 * Decode the improved format sd-config-list.
3905 			 */
3906 			sd_nvpair_str_decode(un, dnlist_ptr);
3907 		} else {
3908 			/*
3909 			 * The old format sd-config-list, loop through all
3910 			 * data-property-name entries in the
3911 			 * data-property-name-list
3912 			 * setting the properties for each.
3913 			 */
3914 			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3915 			    &dataname_lasts); dataname_ptr != NULL;
3916 			    dataname_ptr = sd_strtok_r(NULL, " \t",
3917 			    &dataname_lasts)) {
3918 				int version;
3919 
3920 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3921 				    "sd_process_sdconf_file: disk:%s, "
3922 				    "data:%s\n", vidptr, dataname_ptr);
3923 
3924 				/* Get the data list */
3925 				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3926 				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3927 				    &data_list_len) != DDI_PROP_SUCCESS) {
3928 					SD_INFO(SD_LOG_ATTACH_DETACH, un,
3929 					    "sd_process_sdconf_file: data "
3930 					    "property (%s) has no value\n",
3931 					    dataname_ptr);
3932 					continue;
3933 				}
3934 
3935 				version = data_list[0];
3936 
3937 				if (version == SD_CONF_VERSION_1) {
3938 					sd_tunables values;
3939 
3940 					/* Set the properties */
3941 					if (sd_chk_vers1_data(un, data_list[1],
3942 					    &data_list[2], data_list_len,
3943 					    dataname_ptr) == SD_SUCCESS) {
3944 						sd_get_tunables_from_conf(un,
3945 						    data_list[1], &data_list[2],
3946 						    &values);
3947 						sd_set_vers1_properties(un,
3948 						    data_list[1], &values);
3949 						rval = SD_SUCCESS;
3950 					} else {
3951 						rval = SD_FAILURE;
3952 					}
3953 				} else {
3954 					scsi_log(SD_DEVINFO(un), sd_label,
3955 					    CE_WARN, "data property %s version "
3956 					    "0x%x is invalid.",
3957 					    dataname_ptr, version);
3958 					rval = SD_FAILURE;
3959 				}
3960 				if (data_list)
3961 					ddi_prop_free(data_list);
3962 			}
3963 		}
3964 	}
3965 
3966 	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
3967 	if (config_list) {
3968 		ddi_prop_free(config_list);
3969 	}
3970 
3971 	return (rval);
3972 }
3973 
3974 /*
3975  *    Function: sd_nvpair_str_decode()
3976  *
3977  * Description: Parse the improved format sd-config-list to get
3978  *    each entry of tunable, which includes a name-value pair.
3979  *    Then call sd_set_properties() to set the property.
3980  *
3981  *   Arguments: un - driver soft state (unit) structure
3982  *    nvpair_str - the tunable list
3983  */
3984 static void
3985 sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3986 {
3987 	char	*nv, *name, *value, *token;
3988 	char	*nv_lasts, *v_lasts, *x_lasts;
3989 
3990 	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3991 	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3992 		token = sd_strtok_r(nv, ":", &v_lasts);
3993 		name  = sd_strtok_r(token, " \t", &x_lasts);
3994 		token = sd_strtok_r(NULL, ":", &v_lasts);
3995 		value = sd_strtok_r(token, " \t", &x_lasts);
3996 		if (name == NULL || value == NULL) {
3997 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3998 			    "sd_nvpair_str_decode: "
3999 			    "name or value is not valid!\n");
4000 		} else {
4001 			sd_set_properties(un, name, value);
4002 		}
4003 	}
4004 }
4005 
4006 /*
4007  *    Function: sd_strtok_r()
4008  *
4009  * Description: This function uses strpbrk and strspn to break
4010  *    string into tokens on sequentially subsequent calls. Return
4011  *    NULL when no non-separator characters remain. The first
4012  *    argument is NULL for subsequent calls.
4013  */
4014 static char *
4015 sd_strtok_r(char *string, const char *sepset, char **lasts)
4016 {
4017 	char	*q, *r;
4018 
4019 	/* First or subsequent call */
4020 	if (string == NULL)
4021 		string = *lasts;
4022 
4023 	if (string == NULL)
4024 		return (NULL);
4025 
4026 	/* Skip leading separators */
4027 	q = string + strspn(string, sepset);
4028 
4029 	if (*q == '\0')
4030 		return (NULL);
4031 
4032 	if ((r = strpbrk(q, sepset)) == NULL)
4033 		*lasts = NULL;
4034 	else {
4035 		*r = '\0';
4036 		*lasts = r + 1;
4037 	}
4038 	return (q);
4039 }
4040 
4041 /*
4042  *    Function: sd_set_properties()
4043  *
4044  * Description: Set device properties based on the improved
4045  *    format sd-config-list.
4046  *
4047  *   Arguments: un - driver soft state (unit) structure
4048  *    name  - supported tunable name
4049  *    value - tunable value
4050  */
4051 static void
4052 sd_set_properties(struct sd_lun *un, char *name, char *value)
4053 {
4054 	char	*endptr = NULL;
4055 	long	val = 0;
4056 
4057 	if (strcasecmp(name, "cache-nonvolatile") == 0) {
4058 		if (strcasecmp(value, "true") == 0) {
4059 			un->un_f_suppress_cache_flush = TRUE;
4060 		} else if (strcasecmp(value, "false") == 0) {
4061 			un->un_f_suppress_cache_flush = FALSE;
4062 		} else {
4063 			goto value_invalid;
4064 		}
4065 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4066 		    "suppress_cache_flush flag set to %d\n",
4067 		    un->un_f_suppress_cache_flush);
4068 		return;
4069 	}
4070 
4071 	if (strcasecmp(name, "controller-type") == 0) {
4072 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4073 			un->un_ctype = val;
4074 		} else {
4075 			goto value_invalid;
4076 		}
4077 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4078 		    "ctype set to %d\n", un->un_ctype);
4079 		return;
4080 	}
4081 
4082 	if (strcasecmp(name, "delay-busy") == 0) {
4083 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4084 			un->un_busy_timeout = drv_usectohz(val / 1000);
4085 		} else {
4086 			goto value_invalid;
4087 		}
4088 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4089 		    "busy_timeout set to %d\n", un->un_busy_timeout);
4090 		return;
4091 	}
4092 
4093 	if (strcasecmp(name, "disksort") == 0) {
4094 		if (strcasecmp(value, "true") == 0) {
4095 			un->un_f_disksort_disabled = FALSE;
4096 		} else if (strcasecmp(value, "false") == 0) {
4097 			un->un_f_disksort_disabled = TRUE;
4098 		} else {
4099 			goto value_invalid;
4100 		}
4101 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4102 		    "disksort disabled flag set to %d\n",
4103 		    un->un_f_disksort_disabled);
4104 		return;
4105 	}
4106 
4107 	if (strcasecmp(name, "power-condition") == 0) {
4108 		if (strcasecmp(value, "true") == 0) {
4109 			un->un_f_power_condition_disabled = FALSE;
4110 		} else if (strcasecmp(value, "false") == 0) {
4111 			un->un_f_power_condition_disabled = TRUE;
4112 		} else {
4113 			goto value_invalid;
4114 		}
4115 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4116 		    "power condition disabled flag set to %d\n",
4117 		    un->un_f_power_condition_disabled);
4118 		return;
4119 	}
4120 
4121 	if (strcasecmp(name, "timeout-releasereservation") == 0) {
4122 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4123 			un->un_reserve_release_time = val;
4124 		} else {
4125 			goto value_invalid;
4126 		}
4127 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4128 		    "reservation release timeout set to %d\n",
4129 		    un->un_reserve_release_time);
4130 		return;
4131 	}
4132 
4133 	if (strcasecmp(name, "reset-lun") == 0) {
4134 		if (strcasecmp(value, "true") == 0) {
4135 			un->un_f_lun_reset_enabled = TRUE;
4136 		} else if (strcasecmp(value, "false") == 0) {
4137 			un->un_f_lun_reset_enabled = FALSE;
4138 		} else {
4139 			goto value_invalid;
4140 		}
4141 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4142 		    "lun reset enabled flag set to %d\n",
4143 		    un->un_f_lun_reset_enabled);
4144 		return;
4145 	}
4146 
4147 	if (strcasecmp(name, "retries-busy") == 0) {
4148 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4149 			un->un_busy_retry_count = val;
4150 		} else {
4151 			goto value_invalid;
4152 		}
4153 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4154 		    "busy retry count set to %d\n", un->un_busy_retry_count);
4155 		return;
4156 	}
4157 
4158 	if (strcasecmp(name, "retries-timeout") == 0) {
4159 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4160 			un->un_retry_count = val;
4161 		} else {
4162 			goto value_invalid;
4163 		}
4164 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4165 		    "timeout retry count set to %d\n", un->un_retry_count);
4166 		return;
4167 	}
4168 
4169 	if (strcasecmp(name, "retries-notready") == 0) {
4170 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4171 			un->un_notready_retry_count = val;
4172 		} else {
4173 			goto value_invalid;
4174 		}
4175 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4176 		    "notready retry count set to %d\n",
4177 		    un->un_notready_retry_count);
4178 		return;
4179 	}
4180 
4181 	if (strcasecmp(name, "retries-reset") == 0) {
4182 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4183 			un->un_reset_retry_count = val;
4184 		} else {
4185 			goto value_invalid;
4186 		}
4187 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4188 		    "reset retry count set to %d\n",
4189 		    un->un_reset_retry_count);
4190 		return;
4191 	}
4192 
4193 	if (strcasecmp(name, "throttle-max") == 0) {
4194 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4195 			un->un_saved_throttle = un->un_throttle = val;
4196 		} else {
4197 			goto value_invalid;
4198 		}
4199 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4200 		    "throttle set to %d\n", un->un_throttle);
4201 	}
4202 
4203 	if (strcasecmp(name, "throttle-min") == 0) {
4204 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4205 			un->un_min_throttle = val;
4206 		} else {
4207 			goto value_invalid;
4208 		}
4209 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4210 		    "min throttle set to %d\n", un->un_min_throttle);
4211 	}
4212 
4213 	if (strcasecmp(name, "rmw-type") == 0) {
4214 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4215 			un->un_f_rmw_type = val;
4216 		} else {
4217 			goto value_invalid;
4218 		}
4219 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4220 		    "RMW type set to %d\n", un->un_f_rmw_type);
4221 	}
4222 
4223 	if (strcasecmp(name, "physical-block-size") == 0) {
4224 		if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4225 		    ISP2(val) && val >= un->un_tgt_blocksize &&
4226 		    val >= un->un_sys_blocksize) {
4227 			un->un_phy_blocksize = val;
4228 		} else {
4229 			goto value_invalid;
4230 		}
4231 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4232 		    "physical block size set to %d\n", un->un_phy_blocksize);
4233 	}
4234 
4235 	if (strcasecmp(name, "retries-victim") == 0) {
4236 		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4237 			un->un_victim_retry_count = val;
4238 		} else {
4239 			goto value_invalid;
4240 		}
4241 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4242 		    "victim retry count set to %d\n",
4243 		    un->un_victim_retry_count);
4244 		return;
4245 	}
4246 
4247 	/*
4248 	 * Validate the throttle values.
4249 	 * If any of the numbers are invalid, set everything to defaults.
4250 	 */
4251 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4252 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4253 	    (un->un_min_throttle > un->un_throttle)) {
4254 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4255 		un->un_min_throttle = sd_min_throttle;
4256 	}
4257 
4258 	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4259 		if (strcasecmp(value, "true") == 0) {
4260 			un->un_f_mmc_gesn_polling = TRUE;
4261 		} else if (strcasecmp(value, "false") == 0) {
4262 			un->un_f_mmc_gesn_polling = FALSE;
4263 		} else {
4264 			goto value_invalid;
4265 		}
4266 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4267 		    "mmc-gesn-polling set to %d\n",
4268 		    un->un_f_mmc_gesn_polling);
4269 	}
4270 
4271 	return;
4272 
4273 value_invalid:
4274 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4275 	    "value of prop %s is invalid\n", name);
4276 }
4277 
4278 /*
4279  *    Function: sd_get_tunables_from_conf()
4280  *
4281  *
4282  *    This function reads the data list from the sd.conf file and pulls
4283  *    the values that can have numeric values as arguments and places
4284  *    the values in the appropriate sd_tunables member.
4285  *    Since the order of the data list members varies across platforms
4286  *    This function reads them from the data list in a platform specific
4287  *    order and places them into the correct sd_tunable member that is
4288  *    consistent across all platforms.
4289  */
4290 static void
4291 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4292     sd_tunables *values)
4293 {
4294 	int i;
4295 	int mask;
4296 
4297 	bzero(values, sizeof (sd_tunables));
4298 
4299 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4300 
4301 		mask = 1 << i;
4302 		if (mask > flags) {
4303 			break;
4304 		}
4305 
4306 		switch (mask & flags) {
4307 		case 0:	/* This mask bit not set in flags */
4308 			continue;
4309 		case SD_CONF_BSET_THROTTLE:
4310 			values->sdt_throttle = data_list[i];
4311 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4312 			    "sd_get_tunables_from_conf: throttle = %d\n",
4313 			    values->sdt_throttle);
4314 			break;
4315 		case SD_CONF_BSET_CTYPE:
4316 			values->sdt_ctype = data_list[i];
4317 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4318 			    "sd_get_tunables_from_conf: ctype = %d\n",
4319 			    values->sdt_ctype);
4320 			break;
4321 		case SD_CONF_BSET_NRR_COUNT:
4322 			values->sdt_not_rdy_retries = data_list[i];
4323 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4324 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4325 			    values->sdt_not_rdy_retries);
4326 			break;
4327 		case SD_CONF_BSET_BSY_RETRY_COUNT:
4328 			values->sdt_busy_retries = data_list[i];
4329 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4330 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
4331 			    values->sdt_busy_retries);
4332 			break;
4333 		case SD_CONF_BSET_RST_RETRIES:
4334 			values->sdt_reset_retries = data_list[i];
4335 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4336 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
4337 			    values->sdt_reset_retries);
4338 			break;
4339 		case SD_CONF_BSET_RSV_REL_TIME:
4340 			values->sdt_reserv_rel_time = data_list[i];
4341 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4342 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4343 			    values->sdt_reserv_rel_time);
4344 			break;
4345 		case SD_CONF_BSET_MIN_THROTTLE:
4346 			values->sdt_min_throttle = data_list[i];
4347 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4348 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
4349 			    values->sdt_min_throttle);
4350 			break;
4351 		case SD_CONF_BSET_DISKSORT_DISABLED:
4352 			values->sdt_disk_sort_dis = data_list[i];
4353 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4354 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4355 			    values->sdt_disk_sort_dis);
4356 			break;
4357 		case SD_CONF_BSET_LUN_RESET_ENABLED:
4358 			values->sdt_lun_reset_enable = data_list[i];
4359 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4360 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
4361 			    "\n", values->sdt_lun_reset_enable);
4362 			break;
4363 		case SD_CONF_BSET_CACHE_IS_NV:
4364 			values->sdt_suppress_cache_flush = data_list[i];
4365 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4366 			    "sd_get_tunables_from_conf: \
4367 			    suppress_cache_flush = %d"
4368 			    "\n", values->sdt_suppress_cache_flush);
4369 			break;
4370 		case SD_CONF_BSET_PC_DISABLED:
4371 			values->sdt_disk_sort_dis = data_list[i];
4372 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4373 			    "sd_get_tunables_from_conf: power_condition_dis = "
4374 			    "%d\n", values->sdt_power_condition_dis);
4375 			break;
4376 		}
4377 	}
4378 }
4379 
4380 /*
4381  *    Function: sd_process_sdconf_table
4382  *
4383  * Description: Search the static configuration table for a match on the
4384  *		inquiry vid/pid and update the driver soft state structure
4385  *		according to the table property values for the device.
4386  *
4387  *		The form of a configuration table entry is:
4388  *		  <vid+pid>,<flags>,<property-data>
4389  *		  "SEAGATE ST42400N",1,0x40000,
4390  *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4391  *
4392  *   Arguments: un - driver soft state (unit) structure
4393  */
4394 
4395 static void
4396 sd_process_sdconf_table(struct sd_lun *un)
4397 {
4398 	char	*id = NULL;
4399 	int	table_index;
4400 	int	idlen;
4401 
4402 	ASSERT(un != NULL);
4403 	for (table_index = 0; table_index < sd_disk_table_size;
4404 	    table_index++) {
4405 		id = sd_disk_table[table_index].device_id;
4406 		idlen = strlen(id);
4407 
4408 		/*
4409 		 * The static configuration table currently does not
4410 		 * implement version 10 properties. Additionally,
4411 		 * multiple data-property-name entries are not
4412 		 * implemented in the static configuration table.
4413 		 */
4414 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4415 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4416 			    "sd_process_sdconf_table: disk %s\n", id);
4417 			sd_set_vers1_properties(un,
4418 			    sd_disk_table[table_index].flags,
4419 			    sd_disk_table[table_index].properties);
4420 			break;
4421 		}
4422 	}
4423 }
4424 
4425 
4426 /*
4427  *    Function: sd_sdconf_id_match
4428  *
4429  * Description: This local function implements a case sensitive vid/pid
4430  *		comparison as well as the boundary cases of wild card and
4431  *		multiple blanks.
4432  *
4433  *		Note: An implicit assumption made here is that the scsi
4434  *		inquiry structure will always keep the vid, pid and
4435  *		revision strings in consecutive sequence, so they can be
4436  *		read as a single string. If this assumption is not the
4437  *		case, a separate string, to be used for the check, needs
4438  *		to be built with these strings concatenated.
4439  *
4440  *   Arguments: un - driver soft state (unit) structure
4441  *		id - table or config file vid/pid
4442  *		idlen  - length of the vid/pid (bytes)
4443  *
4444  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4445  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4446  */
4447 
4448 static int
4449 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4450 {
4451 	struct scsi_inquiry	*sd_inq;
4452 	int 			rval = SD_SUCCESS;
4453 
4454 	ASSERT(un != NULL);
4455 	sd_inq = un->un_sd->sd_inq;
4456 	ASSERT(id != NULL);
4457 
4458 	/*
4459 	 * We use the inq_vid as a pointer to a buffer containing the
4460 	 * vid and pid and use the entire vid/pid length of the table
4461 	 * entry for the comparison. This works because the inq_pid
4462 	 * data member follows inq_vid in the scsi_inquiry structure.
4463 	 */
4464 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4465 		/*
4466 		 * The user id string is compared to the inquiry vid/pid
4467 		 * using a case insensitive comparison and ignoring
4468 		 * multiple spaces.
4469 		 */
4470 		rval = sd_blank_cmp(un, id, idlen);
4471 		if (rval != SD_SUCCESS) {
4472 			/*
4473 			 * User id strings that start and end with a "*"
4474 			 * are a special case. These do not have a
4475 			 * specific vendor, and the product string can
4476 			 * appear anywhere in the 16 byte PID portion of
4477 			 * the inquiry data. This is a simple strstr()
4478 			 * type search for the user id in the inquiry data.
4479 			 */
4480 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4481 				char	*pidptr = &id[1];
4482 				int	i;
4483 				int	j;
4484 				int	pidstrlen = idlen - 2;
4485 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
4486 				    pidstrlen;
4487 
4488 				if (j < 0) {
4489 					return (SD_FAILURE);
4490 				}
4491 				for (i = 0; i < j; i++) {
4492 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4493 					    pidptr, pidstrlen) == 0) {
4494 						rval = SD_SUCCESS;
4495 						break;
4496 					}
4497 				}
4498 			}
4499 		}
4500 	}
4501 	return (rval);
4502 }
4503 
4504 
4505 /*
4506  *    Function: sd_blank_cmp
4507  *
4508  * Description: If the id string starts and ends with a space, treat
4509  *		multiple consecutive spaces as equivalent to a single
4510  *		space. For example, this causes a sd_disk_table entry
4511  *		of " NEC CDROM " to match a device's id string of
4512  *		"NEC       CDROM".
4513  *
4514  *		Note: The success exit condition for this routine is if
4515  *		the pointer to the table entry is '\0' and the cnt of
4516  *		the inquiry length is zero. This will happen if the inquiry
4517  *		string returned by the device is padded with spaces to be
4518  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4519  *		SCSI spec states that the inquiry string is to be padded with
4520  *		spaces.
4521  *
4522  *   Arguments: un - driver soft state (unit) structure
4523  *		id - table or config file vid/pid
4524  *		idlen  - length of the vid/pid (bytes)
4525  *
4526  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4527  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4528  */
4529 
4530 static int
4531 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4532 {
4533 	char		*p1;
4534 	char		*p2;
4535 	int		cnt;
4536 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4537 	    sizeof (SD_INQUIRY(un)->inq_pid);
4538 
4539 	ASSERT(un != NULL);
4540 	p2 = un->un_sd->sd_inq->inq_vid;
4541 	ASSERT(id != NULL);
4542 	p1 = id;
4543 
4544 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4545 		/*
4546 		 * Note: string p1 is terminated by a NUL but string p2
4547 		 * isn't.  The end of p2 is determined by cnt.
4548 		 */
4549 		for (;;) {
4550 			/* skip over any extra blanks in both strings */
4551 			while ((*p1 != '\0') && (*p1 == ' ')) {
4552 				p1++;
4553 			}
4554 			while ((cnt != 0) && (*p2 == ' ')) {
4555 				p2++;
4556 				cnt--;
4557 			}
4558 
4559 			/* compare the two strings */
4560 			if ((cnt == 0) ||
4561 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4562 				break;
4563 			}
4564 			while ((cnt > 0) &&
4565 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4566 				p1++;
4567 				p2++;
4568 				cnt--;
4569 			}
4570 		}
4571 	}
4572 
4573 	/* return SD_SUCCESS if both strings match */
4574 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4575 }
4576 
4577 
4578 /*
4579  *    Function: sd_chk_vers1_data
4580  *
4581  * Description: Verify the version 1 device properties provided by the
4582  *		user via the configuration file
4583  *
4584  *   Arguments: un	     - driver soft state (unit) structure
4585  *		flags	     - integer mask indicating properties to be set
4586  *		prop_list    - integer list of property values
4587  *		list_len     - number of the elements
4588  *
4589  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4590  *		SD_FAILURE - Indicates the user provided data is invalid
4591  */
4592 
4593 static int
4594 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4595     int list_len, char *dataname_ptr)
4596 {
4597 	int i;
4598 	int mask = 1;
4599 	int index = 0;
4600 
4601 	ASSERT(un != NULL);
4602 
4603 	/* Check for a NULL property name and list */
4604 	if (dataname_ptr == NULL) {
4605 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4606 		    "sd_chk_vers1_data: NULL data property name.");
4607 		return (SD_FAILURE);
4608 	}
4609 	if (prop_list == NULL) {
4610 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4611 		    "sd_chk_vers1_data: %s NULL data property list.",
4612 		    dataname_ptr);
4613 		return (SD_FAILURE);
4614 	}
4615 
4616 	/* Display a warning if undefined bits are set in the flags */
4617 	if (flags & ~SD_CONF_BIT_MASK) {
4618 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4619 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4620 		    "Properties not set.",
4621 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4622 		return (SD_FAILURE);
4623 	}
4624 
4625 	/*
4626 	 * Verify the length of the list by identifying the highest bit set
4627 	 * in the flags and validating that the property list has a length
4628 	 * up to the index of this bit.
4629 	 */
4630 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4631 		if (flags & mask) {
4632 			index++;
4633 		}
4634 		mask = 1 << i;
4635 	}
4636 	if (list_len < (index + 2)) {
4637 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4638 		    "sd_chk_vers1_data: "
4639 		    "Data property list %s size is incorrect. "
4640 		    "Properties not set.", dataname_ptr);
4641 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4642 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4643 		return (SD_FAILURE);
4644 	}
4645 	return (SD_SUCCESS);
4646 }
4647 
4648 
4649 /*
4650  *    Function: sd_set_vers1_properties
4651  *
4652  * Description: Set version 1 device properties based on a property list
4653  *		retrieved from the driver configuration file or static
4654  *		configuration table. Version 1 properties have the format:
4655  *
4656  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4657  *
4658  *		where the prop0 value will be used to set prop0 if bit0
4659  *		is set in the flags
4660  *
4661  *   Arguments: un	     - driver soft state (unit) structure
4662  *		flags	     - integer mask indicating properties to be set
4663  *		prop_list    - integer list of property values
4664  */
4665 
4666 static void
4667 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4668 {
4669 	ASSERT(un != NULL);
4670 
4671 	/*
4672 	 * Set the flag to indicate cache is to be disabled. An attempt
4673 	 * to disable the cache via sd_cache_control() will be made
4674 	 * later during attach once the basic initialization is complete.
4675 	 */
4676 	if (flags & SD_CONF_BSET_NOCACHE) {
4677 		un->un_f_opt_disable_cache = TRUE;
4678 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4679 		    "sd_set_vers1_properties: caching disabled flag set\n");
4680 	}
4681 
4682 	/* CD-specific configuration parameters */
4683 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4684 		un->un_f_cfg_playmsf_bcd = TRUE;
4685 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4686 		    "sd_set_vers1_properties: playmsf_bcd set\n");
4687 	}
4688 	if (flags & SD_CONF_BSET_READSUB_BCD) {
4689 		un->un_f_cfg_readsub_bcd = TRUE;
4690 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4691 		    "sd_set_vers1_properties: readsub_bcd set\n");
4692 	}
4693 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4694 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4695 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4696 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4697 	}
4698 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4699 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4700 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4701 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4702 	}
4703 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4704 		un->un_f_cfg_no_read_header = TRUE;
4705 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4706 		    "sd_set_vers1_properties: no_read_header set\n");
4707 	}
4708 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4709 		un->un_f_cfg_read_cd_xd4 = TRUE;
4710 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4711 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4712 	}
4713 
4714 	/* Support for devices which do not have valid/unique serial numbers */
4715 	if (flags & SD_CONF_BSET_FAB_DEVID) {
4716 		un->un_f_opt_fab_devid = TRUE;
4717 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4718 		    "sd_set_vers1_properties: fab_devid bit set\n");
4719 	}
4720 
4721 	/* Support for user throttle configuration */
4722 	if (flags & SD_CONF_BSET_THROTTLE) {
4723 		ASSERT(prop_list != NULL);
4724 		un->un_saved_throttle = un->un_throttle =
4725 		    prop_list->sdt_throttle;
4726 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4727 		    "sd_set_vers1_properties: throttle set to %d\n",
4728 		    prop_list->sdt_throttle);
4729 	}
4730 
4731 	/* Set the per disk retry count according to the conf file or table. */
4732 	if (flags & SD_CONF_BSET_NRR_COUNT) {
4733 		ASSERT(prop_list != NULL);
4734 		if (prop_list->sdt_not_rdy_retries) {
4735 			un->un_notready_retry_count =
4736 			    prop_list->sdt_not_rdy_retries;
4737 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4738 			    "sd_set_vers1_properties: not ready retry count"
4739 			    " set to %d\n", un->un_notready_retry_count);
4740 		}
4741 	}
4742 
4743 	/* The controller type is reported for generic disk driver ioctls */
4744 	if (flags & SD_CONF_BSET_CTYPE) {
4745 		ASSERT(prop_list != NULL);
4746 		switch (prop_list->sdt_ctype) {
4747 		case CTYPE_CDROM:
4748 			un->un_ctype = prop_list->sdt_ctype;
4749 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4750 			    "sd_set_vers1_properties: ctype set to "
4751 			    "CTYPE_CDROM\n");
4752 			break;
4753 		case CTYPE_CCS:
4754 			un->un_ctype = prop_list->sdt_ctype;
4755 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4756 			    "sd_set_vers1_properties: ctype set to "
4757 			    "CTYPE_CCS\n");
4758 			break;
4759 		case CTYPE_ROD:		/* RW optical */
4760 			un->un_ctype = prop_list->sdt_ctype;
4761 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4762 			    "sd_set_vers1_properties: ctype set to "
4763 			    "CTYPE_ROD\n");
4764 			break;
4765 		default:
4766 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4767 			    "sd_set_vers1_properties: Could not set "
4768 			    "invalid ctype value (%d)",
4769 			    prop_list->sdt_ctype);
4770 		}
4771 	}
4772 
4773 	/* Purple failover timeout */
4774 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4775 		ASSERT(prop_list != NULL);
4776 		un->un_busy_retry_count =
4777 		    prop_list->sdt_busy_retries;
4778 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4779 		    "sd_set_vers1_properties: "
4780 		    "busy retry count set to %d\n",
4781 		    un->un_busy_retry_count);
4782 	}
4783 
4784 	/* Purple reset retry count */
4785 	if (flags & SD_CONF_BSET_RST_RETRIES) {
4786 		ASSERT(prop_list != NULL);
4787 		un->un_reset_retry_count =
4788 		    prop_list->sdt_reset_retries;
4789 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4790 		    "sd_set_vers1_properties: "
4791 		    "reset retry count set to %d\n",
4792 		    un->un_reset_retry_count);
4793 	}
4794 
4795 	/* Purple reservation release timeout */
4796 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4797 		ASSERT(prop_list != NULL);
4798 		un->un_reserve_release_time =
4799 		    prop_list->sdt_reserv_rel_time;
4800 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4801 		    "sd_set_vers1_properties: "
4802 		    "reservation release timeout set to %d\n",
4803 		    un->un_reserve_release_time);
4804 	}
4805 
4806 	/*
4807 	 * Driver flag telling the driver to verify that no commands are pending
4808 	 * for a device before issuing a Test Unit Ready. This is a workaround
4809 	 * for a firmware bug in some Seagate eliteI drives.
4810 	 */
4811 	if (flags & SD_CONF_BSET_TUR_CHECK) {
4812 		un->un_f_cfg_tur_check = TRUE;
4813 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4814 		    "sd_set_vers1_properties: tur queue check set\n");
4815 	}
4816 
4817 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4818 		un->un_min_throttle = prop_list->sdt_min_throttle;
4819 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4820 		    "sd_set_vers1_properties: min throttle set to %d\n",
4821 		    un->un_min_throttle);
4822 	}
4823 
4824 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4825 		un->un_f_disksort_disabled =
4826 		    (prop_list->sdt_disk_sort_dis != 0) ?
4827 		    TRUE : FALSE;
4828 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4829 		    "sd_set_vers1_properties: disksort disabled "
4830 		    "flag set to %d\n",
4831 		    prop_list->sdt_disk_sort_dis);
4832 	}
4833 
4834 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4835 		un->un_f_lun_reset_enabled =
4836 		    (prop_list->sdt_lun_reset_enable != 0) ?
4837 		    TRUE : FALSE;
4838 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4839 		    "sd_set_vers1_properties: lun reset enabled "
4840 		    "flag set to %d\n",
4841 		    prop_list->sdt_lun_reset_enable);
4842 	}
4843 
4844 	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4845 		un->un_f_suppress_cache_flush =
4846 		    (prop_list->sdt_suppress_cache_flush != 0) ?
4847 		    TRUE : FALSE;
4848 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4849 		    "sd_set_vers1_properties: suppress_cache_flush "
4850 		    "flag set to %d\n",
4851 		    prop_list->sdt_suppress_cache_flush);
4852 	}
4853 
4854 	if (flags & SD_CONF_BSET_PC_DISABLED) {
4855 		un->un_f_power_condition_disabled =
4856 		    (prop_list->sdt_power_condition_dis != 0) ?
4857 		    TRUE : FALSE;
4858 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4859 		    "sd_set_vers1_properties: power_condition_disabled "
4860 		    "flag set to %d\n",
4861 		    prop_list->sdt_power_condition_dis);
4862 	}
4863 
4864 	/*
4865 	 * Validate the throttle values.
4866 	 * If any of the numbers are invalid, set everything to defaults.
4867 	 */
4868 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4869 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4870 	    (un->un_min_throttle > un->un_throttle)) {
4871 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4872 		un->un_min_throttle = sd_min_throttle;
4873 	}
4874 }
4875 
4876 /*
4877  *   Function: sd_is_lsi()
4878  *
4879  *   Description: Check for lsi devices, step through the static device
4880  *	table to match vid/pid.
4881  *
4882  *   Args: un - ptr to sd_lun
4883  *
4884  *   Notes:  When creating new LSI property, need to add the new LSI property
4885  *		to this function.
4886  */
4887 static void
4888 sd_is_lsi(struct sd_lun *un)
4889 {
4890 	char	*id = NULL;
4891 	int	table_index;
4892 	int	idlen;
4893 	void	*prop;
4894 
4895 	ASSERT(un != NULL);
4896 	for (table_index = 0; table_index < sd_disk_table_size;
4897 	    table_index++) {
4898 		id = sd_disk_table[table_index].device_id;
4899 		idlen = strlen(id);
4900 		if (idlen == 0) {
4901 			continue;
4902 		}
4903 
4904 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4905 			prop = sd_disk_table[table_index].properties;
4906 			if (prop == &lsi_properties ||
4907 			    prop == &lsi_oem_properties ||
4908 			    prop == &lsi_properties_scsi ||
4909 			    prop == &symbios_properties) {
4910 				un->un_f_cfg_is_lsi = TRUE;
4911 			}
4912 			break;
4913 		}
4914 	}
4915 }
4916 
4917 /*
4918  *    Function: sd_get_physical_geometry
4919  *
4920  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4921  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4922  *		target, and use this information to initialize the physical
4923  *		geometry cache specified by pgeom_p.
4924  *
4925  *		MODE SENSE is an optional command, so failure in this case
4926  *		does not necessarily denote an error. We want to use the
4927  *		MODE SENSE commands to derive the physical geometry of the
4928  *		device, but if either command fails, the logical geometry is
4929  *		used as the fallback for disk label geometry in cmlb.
4930  *
4931  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4932  *		have already been initialized for the current target and
4933  *		that the current values be passed as args so that we don't
4934  *		end up ever trying to use -1 as a valid value. This could
4935  *		happen if either value is reset while we're not holding
4936  *		the mutex.
4937  *
4938  *   Arguments: un - driver soft state (unit) structure
4939  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4940  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4941  *			to use the USCSI "direct" chain and bypass the normal
4942  *			command waitq.
4943  *
4944  *     Context: Kernel thread only (can sleep).
4945  */
4946 
4947 static int
4948 sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4949 	diskaddr_t capacity, int lbasize, int path_flag)
4950 {
4951 	struct	mode_format	*page3p;
4952 	struct	mode_geometry	*page4p;
4953 	struct	mode_header	*headerp;
4954 	int	sector_size;
4955 	int	nsect;
4956 	int	nhead;
4957 	int	ncyl;
4958 	int	intrlv;
4959 	int	spc;
4960 	diskaddr_t	modesense_capacity;
4961 	int	rpm;
4962 	int	bd_len;
4963 	int	mode_header_length;
4964 	uchar_t	*p3bufp;
4965 	uchar_t	*p4bufp;
4966 	int	cdbsize;
4967 	int 	ret = EIO;
4968 	sd_ssc_t *ssc;
4969 	int	status;
4970 
4971 	ASSERT(un != NULL);
4972 
4973 	if (lbasize == 0) {
4974 		if (ISCD(un)) {
4975 			lbasize = 2048;
4976 		} else {
4977 			lbasize = un->un_sys_blocksize;
4978 		}
4979 	}
4980 	pgeom_p->g_secsize = (unsigned short)lbasize;
4981 
4982 	/*
4983 	 * If the unit is a cd/dvd drive MODE SENSE page three
4984 	 * and MODE SENSE page four are reserved (see SBC spec
4985 	 * and MMC spec). To prevent soft errors just return
4986 	 * using the default LBA size.
4987 	 *
4988 	 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4989 	 * implement support for mode pages 3 and 4 return here to prevent
4990 	 * illegal requests on SATA drives.
4991 	 *
4992 	 * These pages are also reserved in SBC-2 and later.  We assume SBC-2
4993 	 * or later for a direct-attached block device if the SCSI version is
4994 	 * at least SPC-3.
4995 	 */
4996 
4997 	if (ISCD(un) ||
4998 	    un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4999 	    (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
5000 		return (ret);
5001 
5002 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
5003 
5004 	/*
5005 	 * Retrieve MODE SENSE page 3 - Format Device Page
5006 	 */
5007 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
5008 	ssc = sd_ssc_init(un);
5009 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
5010 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
5011 	if (status != 0) {
5012 		SD_ERROR(SD_LOG_COMMON, un,
5013 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
5014 		goto page3_exit;
5015 	}
5016 
5017 	/*
5018 	 * Determine size of Block Descriptors in order to locate the mode
5019 	 * page data.  ATAPI devices return 0, SCSI devices should return
5020 	 * MODE_BLK_DESC_LENGTH.
5021 	 */
5022 	headerp = (struct mode_header *)p3bufp;
5023 	if (un->un_f_cfg_is_atapi == TRUE) {
5024 		struct mode_header_grp2 *mhp =
5025 		    (struct mode_header_grp2 *)headerp;
5026 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
5027 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5028 	} else {
5029 		mode_header_length = MODE_HEADER_LENGTH;
5030 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5031 	}
5032 
5033 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5034 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5035 		    "sd_get_physical_geometry: received unexpected bd_len "
5036 		    "of %d, page3\n", bd_len);
5037 		status = EIO;
5038 		goto page3_exit;
5039 	}
5040 
5041 	page3p = (struct mode_format *)
5042 	    ((caddr_t)headerp + mode_header_length + bd_len);
5043 
5044 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5045 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5046 		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
5047 		    "%d\n", page3p->mode_page.code);
5048 		status = EIO;
5049 		goto page3_exit;
5050 	}
5051 
5052 	/*
5053 	 * Use this physical geometry data only if BOTH MODE SENSE commands
5054 	 * complete successfully; otherwise, revert to the logical geometry.
5055 	 * So, we need to save everything in temporary variables.
5056 	 */
5057 	sector_size = BE_16(page3p->data_bytes_sect);
5058 
5059 	/*
5060 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5061 	 */
5062 	if (sector_size == 0) {
5063 		sector_size = un->un_sys_blocksize;
5064 	} else {
5065 		sector_size &= ~(un->un_sys_blocksize - 1);
5066 	}
5067 
5068 	nsect  = BE_16(page3p->sect_track);
5069 	intrlv = BE_16(page3p->interleave);
5070 
5071 	SD_INFO(SD_LOG_COMMON, un,
5072 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5073 	SD_INFO(SD_LOG_COMMON, un,
5074 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5075 	    page3p->mode_page.code, nsect, sector_size);
5076 	SD_INFO(SD_LOG_COMMON, un,
5077 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5078 	    BE_16(page3p->track_skew),
5079 	    BE_16(page3p->cylinder_skew));
5080 
5081 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5082 
5083 	/*
5084 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5085 	 */
5086 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5087 	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5088 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5089 	if (status != 0) {
5090 		SD_ERROR(SD_LOG_COMMON, un,
5091 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5092 		goto page4_exit;
5093 	}
5094 
5095 	/*
5096 	 * Determine size of Block Descriptors in order to locate the mode
5097 	 * page data.  ATAPI devices return 0, SCSI devices should return
5098 	 * MODE_BLK_DESC_LENGTH.
5099 	 */
5100 	headerp = (struct mode_header *)p4bufp;
5101 	if (un->un_f_cfg_is_atapi == TRUE) {
5102 		struct mode_header_grp2 *mhp =
5103 		    (struct mode_header_grp2 *)headerp;
5104 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5105 	} else {
5106 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5107 	}
5108 
5109 	if (bd_len > MODE_BLK_DESC_LENGTH) {
5110 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5111 		    "sd_get_physical_geometry: received unexpected bd_len of "
5112 		    "%d, page4\n", bd_len);
5113 		status = EIO;
5114 		goto page4_exit;
5115 	}
5116 
5117 	page4p = (struct mode_geometry *)
5118 	    ((caddr_t)headerp + mode_header_length + bd_len);
5119 
5120 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5121 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5122 		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
5123 		    "%d\n", page4p->mode_page.code);
5124 		status = EIO;
5125 		goto page4_exit;
5126 	}
5127 
5128 	/*
5129 	 * Stash the data now, after we know that both commands completed.
5130 	 */
5131 
5132 
5133 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5134 	spc   = nhead * nsect;
5135 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5136 	rpm   = BE_16(page4p->rpm);
5137 
5138 	modesense_capacity = spc * ncyl;
5139 
5140 	SD_INFO(SD_LOG_COMMON, un,
5141 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5142 	SD_INFO(SD_LOG_COMMON, un,
5143 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5144 	SD_INFO(SD_LOG_COMMON, un,
5145 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5146 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5147 	    (void *)pgeom_p, capacity);
5148 
5149 	/*
5150 	 * Compensate if the drive's geometry is not rectangular, i.e.,
5151 	 * the product of C * H * S returned by MODE SENSE >= that returned
5152 	 * by read capacity. This is an idiosyncrasy of the original x86
5153 	 * disk subsystem.
5154 	 */
5155 	if (modesense_capacity >= capacity) {
5156 		SD_INFO(SD_LOG_COMMON, un,
5157 		    "sd_get_physical_geometry: adjusting acyl; "
5158 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5159 		    (modesense_capacity - capacity + spc - 1) / spc);
5160 		if (sector_size != 0) {
5161 			/* 1243403: NEC D38x7 drives don't support sec size */
5162 			pgeom_p->g_secsize = (unsigned short)sector_size;
5163 		}
5164 		pgeom_p->g_nsect    = (unsigned short)nsect;
5165 		pgeom_p->g_nhead    = (unsigned short)nhead;
5166 		pgeom_p->g_capacity = capacity;
5167 		pgeom_p->g_acyl	    =
5168 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5169 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5170 	}
5171 
5172 	pgeom_p->g_rpm    = (unsigned short)rpm;
5173 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5174 	ret = 0;
5175 
5176 	SD_INFO(SD_LOG_COMMON, un,
5177 	    "sd_get_physical_geometry: mode sense geometry:\n");
5178 	SD_INFO(SD_LOG_COMMON, un,
5179 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5180 	    nsect, sector_size, intrlv);
5181 	SD_INFO(SD_LOG_COMMON, un,
5182 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5183 	    nhead, ncyl, rpm, modesense_capacity);
5184 	SD_INFO(SD_LOG_COMMON, un,
5185 	    "sd_get_physical_geometry: (cached)\n");
5186 	SD_INFO(SD_LOG_COMMON, un,
5187 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5188 	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5189 	    pgeom_p->g_nhead, pgeom_p->g_nsect);
5190 	SD_INFO(SD_LOG_COMMON, un,
5191 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5192 	    pgeom_p->g_secsize, pgeom_p->g_capacity,
5193 	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
5194 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5195 
5196 page4_exit:
5197 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5198 
5199 page3_exit:
5200 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5201 
5202 	if (status != 0) {
5203 		if (status == EIO) {
5204 			/*
5205 			 * Some disks do not support mode sense(6), we
5206 			 * should ignore this kind of error(sense key is
5207 			 * 0x5 - illegal request).
5208 			 */
5209 			uint8_t *sensep;
5210 			int senlen;
5211 
5212 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5213 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5214 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
5215 
5216 			if (senlen > 0 &&
5217 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5218 				sd_ssc_assessment(ssc,
5219 				    SD_FMT_IGNORE_COMPROMISE);
5220 			} else {
5221 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5222 			}
5223 		} else {
5224 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5225 		}
5226 	}
5227 	sd_ssc_fini(ssc);
5228 	return (ret);
5229 }
5230 
5231 /*
5232  *    Function: sd_get_virtual_geometry
5233  *
5234  * Description: Ask the controller to tell us about the target device.
5235  *
5236  *   Arguments: un - pointer to softstate
5237  *		capacity - disk capacity in #blocks
5238  *		lbasize - disk block size in bytes
5239  *
5240  *     Context: Kernel thread only
5241  */
5242 
5243 static int
5244 sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5245     diskaddr_t capacity, int lbasize)
5246 {
5247 	uint_t	geombuf;
5248 	int	spc;
5249 
5250 	ASSERT(un != NULL);
5251 
5252 	/* Set sector size, and total number of sectors */
5253 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5254 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5255 
5256 	/* Let the HBA tell us its geometry */
5257 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5258 
5259 	/* A value of -1 indicates an undefined "geometry" property */
5260 	if (geombuf == (-1)) {
5261 		return (EINVAL);
5262 	}
5263 
5264 	/* Initialize the logical geometry cache. */
5265 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5266 	lgeom_p->g_nsect   = geombuf & 0xffff;
5267 	lgeom_p->g_secsize = un->un_sys_blocksize;
5268 
5269 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5270 
5271 	/*
5272 	 * Note: The driver originally converted the capacity value from
5273 	 * target blocks to system blocks. However, the capacity value passed
5274 	 * to this routine is already in terms of system blocks (this scaling
5275 	 * is done when the READ CAPACITY command is issued and processed).
5276 	 * This 'error' may have gone undetected because the usage of g_ncyl
5277 	 * (which is based upon g_capacity) is very limited within the driver
5278 	 */
5279 	lgeom_p->g_capacity = capacity;
5280 
5281 	/*
5282 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5283 	 * hba may return zero values if the device has been removed.
5284 	 */
5285 	if (spc == 0) {
5286 		lgeom_p->g_ncyl = 0;
5287 	} else {
5288 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5289 	}
5290 	lgeom_p->g_acyl = 0;
5291 
5292 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5293 	return (0);
5294 
5295 }
5296 /*
5297  *    Function: sd_update_block_info
5298  *
5299  * Description: Calculate a byte count to sector count bitshift value
5300  *		from sector size.
5301  *
5302  *   Arguments: un: unit struct.
5303  *		lbasize: new target sector size
5304  *		capacity: new target capacity, ie. block count
5305  *
5306  *     Context: Kernel thread context
5307  */
5308 
5309 static void
5310 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5311 {
5312 	if (lbasize != 0) {
5313 		un->un_tgt_blocksize = lbasize;
5314 		un->un_f_tgt_blocksize_is_valid = TRUE;
5315 		if (!un->un_f_has_removable_media) {
5316 			un->un_sys_blocksize = lbasize;
5317 		}
5318 	}
5319 
5320 	if (capacity != 0) {
5321 		un->un_blockcount		= capacity;
5322 		un->un_f_blockcount_is_valid	= TRUE;
5323 
5324 		/*
5325 		 * The capacity has changed so update the errstats.
5326 		 */
5327 		if (un->un_errstats != NULL) {
5328 			struct sd_errstats *stp;
5329 
5330 			capacity *= un->un_sys_blocksize;
5331 			stp = (struct sd_errstats *)un->un_errstats->ks_data;
5332 			if (stp->sd_capacity.value.ui64 < capacity)
5333 				stp->sd_capacity.value.ui64 = capacity;
5334 		}
5335 	}
5336 }
5337 
5338 
5339 /*
5340  *    Function: sd_register_devid
5341  *
5342  * Description: This routine will obtain the device id information from the
5343  *		target, obtain the serial number, and register the device
5344  *		id with the ddi framework.
5345  *
5346  *   Arguments: devi - the system's dev_info_t for the device.
5347  *		un - driver soft state (unit) structure
5348  *		reservation_flag - indicates if a reservation conflict
5349  *		occurred during attach
5350  *
5351  *     Context: Kernel Thread
5352  */
5353 static void
5354 sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5355 {
5356 	int		rval		= 0;
5357 	uchar_t		*inq80		= NULL;
5358 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5359 	size_t		inq80_resid	= 0;
5360 	uchar_t		*inq83		= NULL;
5361 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5362 	size_t		inq83_resid	= 0;
5363 	int		dlen, len;
5364 	char		*sn;
5365 	struct sd_lun	*un;
5366 
5367 	ASSERT(ssc != NULL);
5368 	un = ssc->ssc_un;
5369 	ASSERT(un != NULL);
5370 	ASSERT(mutex_owned(SD_MUTEX(un)));
5371 	ASSERT((SD_DEVINFO(un)) == devi);
5372 
5373 
5374 	/*
5375 	 * We check the availability of the World Wide Name (0x83) and Unit
5376 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5377 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5378 	 * 0x83 is available, that is the best choice.  Our next choice is
5379 	 * 0x80.  If neither are available, we munge the devid from the device
5380 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5381 	 * to fabricate a devid for non-Sun qualified disks.
5382 	 */
5383 	if (sd_check_vpd_page_support(ssc) == 0) {
5384 		/* collect page 80 data if available */
5385 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5386 
5387 			mutex_exit(SD_MUTEX(un));
5388 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5389 
5390 			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5391 			    0x01, 0x80, &inq80_resid);
5392 
5393 			if (rval != 0) {
5394 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5395 				kmem_free(inq80, inq80_len);
5396 				inq80 = NULL;
5397 				inq80_len = 0;
5398 			} else if (ddi_prop_exists(
5399 			    DDI_DEV_T_NONE, SD_DEVINFO(un),
5400 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5401 			    INQUIRY_SERIAL_NO) == 0) {
5402 				/*
5403 				 * If we don't already have a serial number
5404 				 * property, do quick verify of data returned
5405 				 * and define property.
5406 				 */
5407 				dlen = inq80_len - inq80_resid;
5408 				len = (size_t)inq80[3];
5409 				if ((dlen >= 4) && ((len + 4) <= dlen)) {
5410 					/*
5411 					 * Ensure sn termination, skip leading
5412 					 * blanks, and create property
5413 					 * 'inquiry-serial-no'.
5414 					 */
5415 					sn = (char *)&inq80[4];
5416 					sn[len] = 0;
5417 					while (*sn && (*sn == ' '))
5418 						sn++;
5419 					if (*sn) {
5420 						(void) ddi_prop_update_string(
5421 						    DDI_DEV_T_NONE,
5422 						    SD_DEVINFO(un),
5423 						    INQUIRY_SERIAL_NO, sn);
5424 					}
5425 				}
5426 			}
5427 			mutex_enter(SD_MUTEX(un));
5428 		}
5429 
5430 		/* collect page 83 data if available */
5431 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5432 			mutex_exit(SD_MUTEX(un));
5433 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5434 
5435 			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5436 			    0x01, 0x83, &inq83_resid);
5437 
5438 			if (rval != 0) {
5439 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5440 				kmem_free(inq83, inq83_len);
5441 				inq83 = NULL;
5442 				inq83_len = 0;
5443 			}
5444 			mutex_enter(SD_MUTEX(un));
5445 		}
5446 	}
5447 
5448 	/*
5449 	 * If transport has already registered a devid for this target
5450 	 * then that takes precedence over the driver's determination
5451 	 * of the devid.
5452 	 *
5453 	 * NOTE: The reason this check is done here instead of at the beginning
5454 	 * of the function is to allow the code above to create the
5455 	 * 'inquiry-serial-no' property.
5456 	 */
5457 	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5458 		ASSERT(un->un_devid);
5459 		un->un_f_devid_transport_defined = TRUE;
5460 		goto cleanup; /* use devid registered by the transport */
5461 	}
5462 
5463 	/*
5464 	 * This is the case of antiquated Sun disk drives that have the
5465 	 * FAB_DEVID property set in the disk_table.  These drives
5466 	 * manage the devid's by storing them in last 2 available sectors
5467 	 * on the drive and have them fabricated by the ddi layer by calling
5468 	 * ddi_devid_init and passing the DEVID_FAB flag.
5469 	 */
5470 	if (un->un_f_opt_fab_devid == TRUE) {
5471 		/*
5472 		 * Depending on EINVAL isn't reliable, since a reserved disk
5473 		 * may result in invalid geometry, so check to make sure a
5474 		 * reservation conflict did not occur during attach.
5475 		 */
5476 		if ((sd_get_devid(ssc) == EINVAL) &&
5477 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5478 			/*
5479 			 * The devid is invalid AND there is no reservation
5480 			 * conflict.  Fabricate a new devid.
5481 			 */
5482 			(void) sd_create_devid(ssc);
5483 		}
5484 
5485 		/* Register the devid if it exists */
5486 		if (un->un_devid != NULL) {
5487 			(void) ddi_devid_register(SD_DEVINFO(un),
5488 			    un->un_devid);
5489 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5490 			    "sd_register_devid: Devid Fabricated\n");
5491 		}
5492 		goto cleanup;
5493 	}
5494 
5495 	/* encode best devid possible based on data available */
5496 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5497 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5498 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5499 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5500 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5501 
5502 		/* devid successfully encoded, register devid */
5503 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5504 
5505 	} else {
5506 		/*
5507 		 * Unable to encode a devid based on data available.
5508 		 * This is not a Sun qualified disk.  Older Sun disk
5509 		 * drives that have the SD_FAB_DEVID property
5510 		 * set in the disk_table and non Sun qualified
5511 		 * disks are treated in the same manner.  These
5512 		 * drives manage the devid's by storing them in
5513 		 * last 2 available sectors on the drive and
5514 		 * have them fabricated by the ddi layer by
5515 		 * calling ddi_devid_init and passing the
5516 		 * DEVID_FAB flag.
5517 		 * Create a fabricate devid only if there's no
5518 		 * fabricate devid existed.
5519 		 */
5520 		if (sd_get_devid(ssc) == EINVAL) {
5521 			(void) sd_create_devid(ssc);
5522 		}
5523 		un->un_f_opt_fab_devid = TRUE;
5524 
5525 		/* Register the devid if it exists */
5526 		if (un->un_devid != NULL) {
5527 			(void) ddi_devid_register(SD_DEVINFO(un),
5528 			    un->un_devid);
5529 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5530 			    "sd_register_devid: devid fabricated using "
5531 			    "ddi framework\n");
5532 		}
5533 	}
5534 
5535 cleanup:
5536 	/* clean up resources */
5537 	if (inq80 != NULL) {
5538 		kmem_free(inq80, inq80_len);
5539 	}
5540 	if (inq83 != NULL) {
5541 		kmem_free(inq83, inq83_len);
5542 	}
5543 }
5544 
5545 
5546 
5547 /*
5548  *    Function: sd_get_devid
5549  *
5550  * Description: This routine will return 0 if a valid device id has been
5551  *		obtained from the target and stored in the soft state. If a
5552  *		valid device id has not been previously read and stored, a
5553  *		read attempt will be made.
5554  *
5555  *   Arguments: un - driver soft state (unit) structure
5556  *
5557  * Return Code: 0 if we successfully get the device id
5558  *
5559  *     Context: Kernel Thread
5560  */
5561 
5562 static int
5563 sd_get_devid(sd_ssc_t *ssc)
5564 {
5565 	struct dk_devid		*dkdevid;
5566 	ddi_devid_t		tmpid;
5567 	uint_t			*ip;
5568 	size_t			sz;
5569 	diskaddr_t		blk;
5570 	int			status;
5571 	int			chksum;
5572 	int			i;
5573 	size_t			buffer_size;
5574 	struct sd_lun		*un;
5575 
5576 	ASSERT(ssc != NULL);
5577 	un = ssc->ssc_un;
5578 	ASSERT(un != NULL);
5579 	ASSERT(mutex_owned(SD_MUTEX(un)));
5580 
5581 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5582 	    un);
5583 
5584 	if (un->un_devid != NULL) {
5585 		return (0);
5586 	}
5587 
5588 	mutex_exit(SD_MUTEX(un));
5589 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5590 	    (void *)SD_PATH_DIRECT) != 0) {
5591 		mutex_enter(SD_MUTEX(un));
5592 		return (EINVAL);
5593 	}
5594 
5595 	/*
5596 	 * Read and verify device id, stored in the reserved cylinders at the
5597 	 * end of the disk. Backup label is on the odd sectors of the last
5598 	 * track of the last cylinder. Device id will be on track of the next
5599 	 * to last cylinder.
5600 	 */
5601 	mutex_enter(SD_MUTEX(un));
5602 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5603 	mutex_exit(SD_MUTEX(un));
5604 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5605 	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5606 	    SD_PATH_DIRECT);
5607 
5608 	if (status != 0) {
5609 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5610 		goto error;
5611 	}
5612 
5613 	/* Validate the revision */
5614 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5615 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5616 		status = EINVAL;
5617 		goto error;
5618 	}
5619 
5620 	/* Calculate the checksum */
5621 	chksum = 0;
5622 	ip = (uint_t *)dkdevid;
5623 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5624 	    i++) {
5625 		chksum ^= ip[i];
5626 	}
5627 
5628 	/* Compare the checksums */
5629 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5630 		status = EINVAL;
5631 		goto error;
5632 	}
5633 
5634 	/* Validate the device id */
5635 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5636 		status = EINVAL;
5637 		goto error;
5638 	}
5639 
5640 	/*
5641 	 * Store the device id in the driver soft state
5642 	 */
5643 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5644 	tmpid = kmem_alloc(sz, KM_SLEEP);
5645 
5646 	mutex_enter(SD_MUTEX(un));
5647 
5648 	un->un_devid = tmpid;
5649 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5650 
5651 	kmem_free(dkdevid, buffer_size);
5652 
5653 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5654 
5655 	return (status);
5656 error:
5657 	mutex_enter(SD_MUTEX(un));
5658 	kmem_free(dkdevid, buffer_size);
5659 	return (status);
5660 }
5661 
5662 
5663 /*
5664  *    Function: sd_create_devid
5665  *
5666  * Description: This routine will fabricate the device id and write it
5667  *		to the disk.
5668  *
5669  *   Arguments: un - driver soft state (unit) structure
5670  *
5671  * Return Code: value of the fabricated device id
5672  *
5673  *     Context: Kernel Thread
5674  */
5675 
5676 static ddi_devid_t
5677 sd_create_devid(sd_ssc_t *ssc)
5678 {
5679 	struct sd_lun	*un;
5680 
5681 	ASSERT(ssc != NULL);
5682 	un = ssc->ssc_un;
5683 	ASSERT(un != NULL);
5684 
5685 	/* Fabricate the devid */
5686 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5687 	    == DDI_FAILURE) {
5688 		return (NULL);
5689 	}
5690 
5691 	/* Write the devid to disk */
5692 	if (sd_write_deviceid(ssc) != 0) {
5693 		ddi_devid_free(un->un_devid);
5694 		un->un_devid = NULL;
5695 	}
5696 
5697 	return (un->un_devid);
5698 }
5699 
5700 
5701 /*
5702  *    Function: sd_write_deviceid
5703  *
5704  * Description: This routine will write the device id to the disk
5705  *		reserved sector.
5706  *
5707  *   Arguments: un - driver soft state (unit) structure
5708  *
5709  * Return Code: EINVAL
5710  *		value returned by sd_send_scsi_cmd
5711  *
5712  *     Context: Kernel Thread
5713  */
5714 
5715 static int
5716 sd_write_deviceid(sd_ssc_t *ssc)
5717 {
5718 	struct dk_devid		*dkdevid;
5719 	uchar_t			*buf;
5720 	diskaddr_t		blk;
5721 	uint_t			*ip, chksum;
5722 	int			status;
5723 	int			i;
5724 	struct sd_lun		*un;
5725 
5726 	ASSERT(ssc != NULL);
5727 	un = ssc->ssc_un;
5728 	ASSERT(un != NULL);
5729 	ASSERT(mutex_owned(SD_MUTEX(un)));
5730 
5731 	mutex_exit(SD_MUTEX(un));
5732 	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5733 	    (void *)SD_PATH_DIRECT) != 0) {
5734 		mutex_enter(SD_MUTEX(un));
5735 		return (-1);
5736 	}
5737 
5738 
5739 	/* Allocate the buffer */
5740 	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5741 	dkdevid = (struct dk_devid *)buf;
5742 
5743 	/* Fill in the revision */
5744 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5745 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5746 
5747 	/* Copy in the device id */
5748 	mutex_enter(SD_MUTEX(un));
5749 	bcopy(un->un_devid, &dkdevid->dkd_devid,
5750 	    ddi_devid_sizeof(un->un_devid));
5751 	mutex_exit(SD_MUTEX(un));
5752 
5753 	/* Calculate the checksum */
5754 	chksum = 0;
5755 	ip = (uint_t *)dkdevid;
5756 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5757 	    i++) {
5758 		chksum ^= ip[i];
5759 	}
5760 
5761 	/* Fill-in checksum */
5762 	DKD_FORMCHKSUM(chksum, dkdevid);
5763 
5764 	/* Write the reserved sector */
5765 	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5766 	    SD_PATH_DIRECT);
5767 	if (status != 0)
5768 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5769 
5770 	kmem_free(buf, un->un_sys_blocksize);
5771 
5772 	mutex_enter(SD_MUTEX(un));
5773 	return (status);
5774 }
5775 
5776 
5777 /*
5778  *    Function: sd_check_vpd_page_support
5779  *
5780  * Description: This routine sends an inquiry command with the EVPD bit set and
5781  *		a page code of 0x00 to the device. It is used to determine which
5782  *		vital product pages are available to find the devid. We are
5783  *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5784  *		the device does not support that command.
5785  *
5786  *   Arguments: un  - driver soft state (unit) structure
5787  *
5788  * Return Code: 0 - success
5789  *		1 - check condition
5790  *
5791  *     Context: This routine can sleep.
5792  */
5793 
5794 static int
5795 sd_check_vpd_page_support(sd_ssc_t *ssc)
5796 {
5797 	uchar_t	*page_list	= NULL;
5798 	uchar_t	page_length	= 0xff;	/* Use max possible length */
5799 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
5800 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
5801 	int    	rval		= 0;
5802 	int	counter;
5803 	struct sd_lun		*un;
5804 
5805 	ASSERT(ssc != NULL);
5806 	un = ssc->ssc_un;
5807 	ASSERT(un != NULL);
5808 	ASSERT(mutex_owned(SD_MUTEX(un)));
5809 
5810 	mutex_exit(SD_MUTEX(un));
5811 
5812 	/*
5813 	 * We'll set the page length to the maximum to save figuring it out
5814 	 * with an additional call.
5815 	 */
5816 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
5817 
5818 	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5819 	    page_code, NULL);
5820 
5821 	if (rval != 0)
5822 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5823 
5824 	mutex_enter(SD_MUTEX(un));
5825 
5826 	/*
5827 	 * Now we must validate that the device accepted the command, as some
5828 	 * drives do not support it.  If the drive does support it, we will
5829 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
5830 	 * not, we return -1.
5831 	 */
5832 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5833 		/* Loop to find one of the 2 pages we need */
5834 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5835 
5836 		/*
5837 		 * Pages are returned in ascending order, and 0x83 is what we
5838 		 * are hoping for.
5839 		 */
5840 		while ((page_list[counter] <= 0xB1) &&
5841 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
5842 		    VPD_HEAD_OFFSET))) {
5843 			/*
5844 			 * Add 3 because page_list[3] is the number of
5845 			 * pages minus 3
5846 			 */
5847 
5848 			switch (page_list[counter]) {
5849 			case 0x00:
5850 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5851 				break;
5852 			case 0x80:
5853 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5854 				break;
5855 			case 0x81:
5856 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5857 				break;
5858 			case 0x82:
5859 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5860 				break;
5861 			case 0x83:
5862 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5863 				break;
5864 			case 0x86:
5865 				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5866 				break;
5867 			case 0xB1:
5868 				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5869 				break;
5870 			}
5871 			counter++;
5872 		}
5873 
5874 	} else {
5875 		rval = -1;
5876 
5877 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
5878 		    "sd_check_vpd_page_support: This drive does not implement "
5879 		    "VPD pages.\n");
5880 	}
5881 
5882 	kmem_free(page_list, page_length);
5883 
5884 	return (rval);
5885 }
5886 
5887 
5888 /*
5889  *    Function: sd_setup_pm
5890  *
5891  * Description: Initialize Power Management on the device
5892  *
5893  *     Context: Kernel Thread
5894  */
5895 
5896 static void
5897 sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5898 {
5899 	uint_t		log_page_size;
5900 	uchar_t		*log_page_data;
5901 	int		rval = 0;
5902 	struct sd_lun	*un;
5903 
5904 	ASSERT(ssc != NULL);
5905 	un = ssc->ssc_un;
5906 	ASSERT(un != NULL);
5907 
5908 	/*
5909 	 * Since we are called from attach, holding a mutex for
5910 	 * un is unnecessary. Because some of the routines called
5911 	 * from here require SD_MUTEX to not be held, assert this
5912 	 * right up front.
5913 	 */
5914 	ASSERT(!mutex_owned(SD_MUTEX(un)));
5915 	/*
5916 	 * Since the sd device does not have the 'reg' property,
5917 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
5918 	 * The following code is to tell cpr that this device
5919 	 * DOES need to be suspended and resumed.
5920 	 */
5921 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
5922 	    "pm-hardware-state", "needs-suspend-resume");
5923 
5924 	/*
5925 	 * This complies with the new power management framework
5926 	 * for certain desktop machines. Create the pm_components
5927 	 * property as a string array property.
5928 	 * If un_f_pm_supported is TRUE, that means the disk
5929 	 * attached HBA has set the "pm-capable" property and
5930 	 * the value of this property is bigger than 0.
5931 	 */
5932 	if (un->un_f_pm_supported) {
5933 		/*
5934 		 * not all devices have a motor, try it first.
5935 		 * some devices may return ILLEGAL REQUEST, some
5936 		 * will hang
5937 		 * The following START_STOP_UNIT is used to check if target
5938 		 * device has a motor.
5939 		 */
5940 		un->un_f_start_stop_supported = TRUE;
5941 
5942 		if (un->un_f_power_condition_supported) {
5943 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5944 			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
5945 			    SD_PATH_DIRECT);
5946 			if (rval != 0) {
5947 				un->un_f_power_condition_supported = FALSE;
5948 			}
5949 		}
5950 		if (!un->un_f_power_condition_supported) {
5951 			rval = sd_send_scsi_START_STOP_UNIT(ssc,
5952 			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
5953 		}
5954 		if (rval != 0) {
5955 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5956 			un->un_f_start_stop_supported = FALSE;
5957 		}
5958 
5959 		/*
5960 		 * create pm properties anyways otherwise the parent can't
5961 		 * go to sleep
5962 		 */
5963 		un->un_f_pm_is_enabled = TRUE;
5964 		(void) sd_create_pm_components(devi, un);
5965 
5966 		/*
5967 		 * If it claims that log sense is supported, check it out.
5968 		 */
5969 		if (un->un_f_log_sense_supported) {
5970 			rval = sd_log_page_supported(ssc,
5971 			    START_STOP_CYCLE_PAGE);
5972 			if (rval == 1) {
5973 				/* Page found, use it. */
5974 				un->un_start_stop_cycle_page =
5975 				    START_STOP_CYCLE_PAGE;
5976 			} else {
5977 				/*
5978 				 * Page not found or log sense is not
5979 				 * supported.
5980 				 * Notice we do not check the old style
5981 				 * START_STOP_CYCLE_VU_PAGE because this
5982 				 * code path does not apply to old disks.
5983 				 */
5984 				un->un_f_log_sense_supported = FALSE;
5985 				un->un_f_pm_log_sense_smart = FALSE;
5986 			}
5987 		}
5988 
5989 		return;
5990 	}
5991 
5992 	/*
5993 	 * For the disk whose attached HBA has not set the "pm-capable"
5994 	 * property, check if it supports the power management.
5995 	 */
5996 	if (!un->un_f_log_sense_supported) {
5997 		un->un_power_level = SD_SPINDLE_ON;
5998 		un->un_f_pm_is_enabled = FALSE;
5999 		return;
6000 	}
6001 
6002 	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
6003 
6004 #ifdef	SDDEBUG
6005 	if (sd_force_pm_supported) {
6006 		/* Force a successful result */
6007 		rval = 1;
6008 	}
6009 #endif
6010 
6011 	/*
6012 	 * If the start-stop cycle counter log page is not supported
6013 	 * or if the pm-capable property is set to be false (0),
6014 	 * then we should not create the pm_components property.
6015 	 */
6016 	if (rval == -1) {
6017 		/*
6018 		 * Error.
6019 		 * Reading log sense failed, most likely this is
6020 		 * an older drive that does not support log sense.
6021 		 * If this fails auto-pm is not supported.
6022 		 */
6023 		un->un_power_level = SD_SPINDLE_ON;
6024 		un->un_f_pm_is_enabled = FALSE;
6025 
6026 	} else if (rval == 0) {
6027 		/*
6028 		 * Page not found.
6029 		 * The start stop cycle counter is implemented as page
6030 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6031 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6032 		 */
6033 		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6034 			/*
6035 			 * Page found, use this one.
6036 			 */
6037 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6038 			un->un_f_pm_is_enabled = TRUE;
6039 		} else {
6040 			/*
6041 			 * Error or page not found.
6042 			 * auto-pm is not supported for this device.
6043 			 */
6044 			un->un_power_level = SD_SPINDLE_ON;
6045 			un->un_f_pm_is_enabled = FALSE;
6046 		}
6047 	} else {
6048 		/*
6049 		 * Page found, use it.
6050 		 */
6051 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6052 		un->un_f_pm_is_enabled = TRUE;
6053 	}
6054 
6055 
6056 	if (un->un_f_pm_is_enabled == TRUE) {
6057 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6058 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6059 
6060 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6061 		    log_page_size, un->un_start_stop_cycle_page,
6062 		    0x01, 0, SD_PATH_DIRECT);
6063 
6064 		if (rval != 0) {
6065 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6066 		}
6067 
6068 #ifdef	SDDEBUG
6069 		if (sd_force_pm_supported) {
6070 			/* Force a successful result */
6071 			rval = 0;
6072 		}
6073 #endif
6074 
6075 		/*
6076 		 * If the Log sense for Page( Start/stop cycle counter page)
6077 		 * succeeds, then power management is supported and we can
6078 		 * enable auto-pm.
6079 		 */
6080 		if (rval == 0)  {
6081 			(void) sd_create_pm_components(devi, un);
6082 		} else {
6083 			un->un_power_level = SD_SPINDLE_ON;
6084 			un->un_f_pm_is_enabled = FALSE;
6085 		}
6086 
6087 		kmem_free(log_page_data, log_page_size);
6088 	}
6089 }
6090 
6091 
6092 /*
6093  *    Function: sd_create_pm_components
6094  *
6095  * Description: Initialize PM property.
6096  *
6097  *     Context: Kernel thread context
6098  */
6099 
6100 static void
6101 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6102 {
6103 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6104 
6105 	if (un->un_f_power_condition_supported) {
6106 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6107 		    "pm-components", sd_pwr_pc.pm_comp, 5)
6108 		    != DDI_PROP_SUCCESS) {
6109 			un->un_power_level = SD_SPINDLE_ACTIVE;
6110 			un->un_f_pm_is_enabled = FALSE;
6111 			return;
6112 		}
6113 	} else {
6114 		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6115 		    "pm-components", sd_pwr_ss.pm_comp, 3)
6116 		    != DDI_PROP_SUCCESS) {
6117 			un->un_power_level = SD_SPINDLE_ON;
6118 			un->un_f_pm_is_enabled = FALSE;
6119 			return;
6120 		}
6121 	}
6122 	/*
6123 	 * When components are initially created they are idle,
6124 	 * power up any non-removables.
6125 	 * Note: the return value of pm_raise_power can't be used
6126 	 * for determining if PM should be enabled for this device.
6127 	 * Even if you check the return values and remove this
6128 	 * property created above, the PM framework will not honor the
6129 	 * change after the first call to pm_raise_power. Hence,
6130 	 * removal of that property does not help if pm_raise_power
6131 	 * fails. In the case of removable media, the start/stop
6132 	 * will fail if the media is not present.
6133 	 */
6134 	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6135 	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6136 		mutex_enter(SD_MUTEX(un));
6137 		un->un_power_level = SD_PM_STATE_ACTIVE(un);
6138 		mutex_enter(&un->un_pm_mutex);
6139 		/* Set to on and not busy. */
6140 		un->un_pm_count = 0;
6141 	} else {
6142 		mutex_enter(SD_MUTEX(un));
6143 		un->un_power_level = SD_PM_STATE_STOPPED(un);
6144 		mutex_enter(&un->un_pm_mutex);
6145 		/* Set to off. */
6146 		un->un_pm_count = -1;
6147 	}
6148 	mutex_exit(&un->un_pm_mutex);
6149 	mutex_exit(SD_MUTEX(un));
6150 }
6151 
6152 
6153 /*
6154  *    Function: sd_ddi_suspend
6155  *
6156  * Description: Performs system power-down operations. This includes
6157  *		setting the drive state to indicate its suspended so
6158  *		that no new commands will be accepted. Also, wait for
6159  *		all commands that are in transport or queued to a timer
6160  *		for retry to complete. All timeout threads are cancelled.
6161  *
6162  * Return Code: DDI_FAILURE or DDI_SUCCESS
6163  *
6164  *     Context: Kernel thread context
6165  */
6166 
6167 static int
6168 sd_ddi_suspend(dev_info_t *devi)
6169 {
6170 	struct	sd_lun	*un;
6171 	clock_t		wait_cmds_complete;
6172 
6173 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6174 	if (un == NULL) {
6175 		return (DDI_FAILURE);
6176 	}
6177 
6178 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6179 
6180 	mutex_enter(SD_MUTEX(un));
6181 
6182 	/* Return success if the device is already suspended. */
6183 	if (un->un_state == SD_STATE_SUSPENDED) {
6184 		mutex_exit(SD_MUTEX(un));
6185 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6186 		    "device already suspended, exiting\n");
6187 		return (DDI_SUCCESS);
6188 	}
6189 
6190 	/* Return failure if the device is being used by HA */
6191 	if (un->un_resvd_status &
6192 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6193 		mutex_exit(SD_MUTEX(un));
6194 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6195 		    "device in use by HA, exiting\n");
6196 		return (DDI_FAILURE);
6197 	}
6198 
6199 	/*
6200 	 * Return failure if the device is in a resource wait
6201 	 * or power changing state.
6202 	 */
6203 	if ((un->un_state == SD_STATE_RWAIT) ||
6204 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6205 		mutex_exit(SD_MUTEX(un));
6206 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6207 		    "device in resource wait state, exiting\n");
6208 		return (DDI_FAILURE);
6209 	}
6210 
6211 
6212 	un->un_save_state = un->un_last_state;
6213 	New_state(un, SD_STATE_SUSPENDED);
6214 
6215 	/*
6216 	 * Wait for all commands that are in transport or queued to a timer
6217 	 * for retry to complete.
6218 	 *
6219 	 * While waiting, no new commands will be accepted or sent because of
6220 	 * the new state we set above.
6221 	 *
6222 	 * Wait till current operation has completed. If we are in the resource
6223 	 * wait state (with an intr outstanding) then we need to wait till the
6224 	 * intr completes and starts the next cmd. We want to wait for
6225 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6226 	 */
6227 	wait_cmds_complete = ddi_get_lbolt() +
6228 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6229 
6230 	while (un->un_ncmds_in_transport != 0) {
6231 		/*
6232 		 * Fail if commands do not finish in the specified time.
6233 		 */
6234 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6235 		    wait_cmds_complete) == -1) {
6236 			/*
6237 			 * Undo the state changes made above. Everything
6238 			 * must go back to it's original value.
6239 			 */
6240 			Restore_state(un);
6241 			un->un_last_state = un->un_save_state;
6242 			/* Wake up any threads that might be waiting. */
6243 			cv_broadcast(&un->un_suspend_cv);
6244 			mutex_exit(SD_MUTEX(un));
6245 			SD_ERROR(SD_LOG_IO_PM, un,
6246 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6247 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6248 			return (DDI_FAILURE);
6249 		}
6250 	}
6251 
6252 	/*
6253 	 * Cancel SCSI watch thread and timeouts, if any are active
6254 	 */
6255 
6256 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6257 		opaque_t temp_token = un->un_swr_token;
6258 		mutex_exit(SD_MUTEX(un));
6259 		scsi_watch_suspend(temp_token);
6260 		mutex_enter(SD_MUTEX(un));
6261 	}
6262 
6263 	if (un->un_reset_throttle_timeid != NULL) {
6264 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6265 		un->un_reset_throttle_timeid = NULL;
6266 		mutex_exit(SD_MUTEX(un));
6267 		(void) untimeout(temp_id);
6268 		mutex_enter(SD_MUTEX(un));
6269 	}
6270 
6271 	if (un->un_dcvb_timeid != NULL) {
6272 		timeout_id_t temp_id = un->un_dcvb_timeid;
6273 		un->un_dcvb_timeid = NULL;
6274 		mutex_exit(SD_MUTEX(un));
6275 		(void) untimeout(temp_id);
6276 		mutex_enter(SD_MUTEX(un));
6277 	}
6278 
6279 	mutex_enter(&un->un_pm_mutex);
6280 	if (un->un_pm_timeid != NULL) {
6281 		timeout_id_t temp_id = un->un_pm_timeid;
6282 		un->un_pm_timeid = NULL;
6283 		mutex_exit(&un->un_pm_mutex);
6284 		mutex_exit(SD_MUTEX(un));
6285 		(void) untimeout(temp_id);
6286 		mutex_enter(SD_MUTEX(un));
6287 	} else {
6288 		mutex_exit(&un->un_pm_mutex);
6289 	}
6290 
6291 	if (un->un_rmw_msg_timeid != NULL) {
6292 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
6293 		un->un_rmw_msg_timeid = NULL;
6294 		mutex_exit(SD_MUTEX(un));
6295 		(void) untimeout(temp_id);
6296 		mutex_enter(SD_MUTEX(un));
6297 	}
6298 
6299 	if (un->un_retry_timeid != NULL) {
6300 		timeout_id_t temp_id = un->un_retry_timeid;
6301 		un->un_retry_timeid = NULL;
6302 		mutex_exit(SD_MUTEX(un));
6303 		(void) untimeout(temp_id);
6304 		mutex_enter(SD_MUTEX(un));
6305 
6306 		if (un->un_retry_bp != NULL) {
6307 			un->un_retry_bp->av_forw = un->un_waitq_headp;
6308 			un->un_waitq_headp = un->un_retry_bp;
6309 			if (un->un_waitq_tailp == NULL) {
6310 				un->un_waitq_tailp = un->un_retry_bp;
6311 			}
6312 			un->un_retry_bp = NULL;
6313 			un->un_retry_statp = NULL;
6314 		}
6315 	}
6316 
6317 	if (un->un_direct_priority_timeid != NULL) {
6318 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6319 		un->un_direct_priority_timeid = NULL;
6320 		mutex_exit(SD_MUTEX(un));
6321 		(void) untimeout(temp_id);
6322 		mutex_enter(SD_MUTEX(un));
6323 	}
6324 
6325 	if (un->un_f_is_fibre == TRUE) {
6326 		/*
6327 		 * Remove callbacks for insert and remove events
6328 		 */
6329 		if (un->un_insert_event != NULL) {
6330 			mutex_exit(SD_MUTEX(un));
6331 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6332 			mutex_enter(SD_MUTEX(un));
6333 			un->un_insert_event = NULL;
6334 		}
6335 
6336 		if (un->un_remove_event != NULL) {
6337 			mutex_exit(SD_MUTEX(un));
6338 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6339 			mutex_enter(SD_MUTEX(un));
6340 			un->un_remove_event = NULL;
6341 		}
6342 	}
6343 
6344 	mutex_exit(SD_MUTEX(un));
6345 
6346 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6347 
6348 	return (DDI_SUCCESS);
6349 }
6350 
6351 
6352 /*
6353  *    Function: sd_ddi_resume
6354  *
6355  * Description: Performs system power-up operations..
6356  *
6357  * Return Code: DDI_SUCCESS
6358  *		DDI_FAILURE
6359  *
6360  *     Context: Kernel thread context
6361  */
6362 
6363 static int
6364 sd_ddi_resume(dev_info_t *devi)
6365 {
6366 	struct	sd_lun	*un;
6367 
6368 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6369 	if (un == NULL) {
6370 		return (DDI_FAILURE);
6371 	}
6372 
6373 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6374 
6375 	mutex_enter(SD_MUTEX(un));
6376 	Restore_state(un);
6377 
6378 	/*
6379 	 * Restore the state which was saved to give the
6380 	 * the right state in un_last_state
6381 	 */
6382 	un->un_last_state = un->un_save_state;
6383 	/*
6384 	 * Note: throttle comes back at full.
6385 	 * Also note: this MUST be done before calling pm_raise_power
6386 	 * otherwise the system can get hung in biowait. The scenario where
6387 	 * this'll happen is under cpr suspend. Writing of the system
6388 	 * state goes through sddump, which writes 0 to un_throttle. If
6389 	 * writing the system state then fails, example if the partition is
6390 	 * too small, then cpr attempts a resume. If throttle isn't restored
6391 	 * from the saved value until after calling pm_raise_power then
6392 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6393 	 * in biowait.
6394 	 */
6395 	un->un_throttle = un->un_saved_throttle;
6396 
6397 	/*
6398 	 * The chance of failure is very rare as the only command done in power
6399 	 * entry point is START command when you transition from 0->1 or
6400 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6401 	 * which suspend was done. Ignore the return value as the resume should
6402 	 * not be failed. In the case of removable media the media need not be
6403 	 * inserted and hence there is a chance that raise power will fail with
6404 	 * media not present.
6405 	 */
6406 	if (un->un_f_attach_spinup) {
6407 		mutex_exit(SD_MUTEX(un));
6408 		(void) pm_raise_power(SD_DEVINFO(un), 0,
6409 		    SD_PM_STATE_ACTIVE(un));
6410 		mutex_enter(SD_MUTEX(un));
6411 	}
6412 
6413 	/*
6414 	 * Don't broadcast to the suspend cv and therefore possibly
6415 	 * start I/O until after power has been restored.
6416 	 */
6417 	cv_broadcast(&un->un_suspend_cv);
6418 	cv_broadcast(&un->un_state_cv);
6419 
6420 	/* restart thread */
6421 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6422 		scsi_watch_resume(un->un_swr_token);
6423 	}
6424 
6425 #if (defined(__fibre))
6426 	if (un->un_f_is_fibre == TRUE) {
6427 		/*
6428 		 * Add callbacks for insert and remove events
6429 		 */
6430 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6431 			sd_init_event_callbacks(un);
6432 		}
6433 	}
6434 #endif
6435 
6436 	/*
6437 	 * Transport any pending commands to the target.
6438 	 *
6439 	 * If this is a low-activity device commands in queue will have to wait
6440 	 * until new commands come in, which may take awhile. Also, we
6441 	 * specifically don't check un_ncmds_in_transport because we know that
6442 	 * there really are no commands in progress after the unit was
6443 	 * suspended and we could have reached the throttle level, been
6444 	 * suspended, and have no new commands coming in for awhile. Highly
6445 	 * unlikely, but so is the low-activity disk scenario.
6446 	 */
6447 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6448 
6449 	sd_start_cmds(un, NULL);
6450 	mutex_exit(SD_MUTEX(un));
6451 
6452 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6453 
6454 	return (DDI_SUCCESS);
6455 }
6456 
6457 
6458 /*
6459  *    Function: sd_pm_state_change
6460  *
6461  * Description: Change the driver power state.
6462  * 		Someone else is required to actually change the driver
6463  * 		power level.
6464  *
6465  *   Arguments: un - driver soft state (unit) structure
6466  *              level - the power level that is changed to
6467  *              flag - to decide how to change the power state
6468  *
6469  * Return Code: DDI_SUCCESS
6470  *
6471  *     Context: Kernel thread context
6472  */
6473 static int
6474 sd_pm_state_change(struct sd_lun *un, int level, int flag)
6475 {
6476 	ASSERT(un != NULL);
6477 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: entry\n");
6478 
6479 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6480 	mutex_enter(SD_MUTEX(un));
6481 
6482 	if (flag == SD_PM_STATE_ROLLBACK || SD_PM_IS_IO_CAPABLE(un, level)) {
6483 		un->un_power_level = level;
6484 		ASSERT(!mutex_owned(&un->un_pm_mutex));
6485 		mutex_enter(&un->un_pm_mutex);
6486 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6487 			un->un_pm_count++;
6488 			ASSERT(un->un_pm_count == 0);
6489 		}
6490 		mutex_exit(&un->un_pm_mutex);
6491 	} else {
6492 		/*
6493 		 * Exit if power management is not enabled for this device,
6494 		 * or if the device is being used by HA.
6495 		 */
6496 		if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6497 		    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6498 			mutex_exit(SD_MUTEX(un));
6499 			SD_TRACE(SD_LOG_POWER, un,
6500 			    "sd_pm_state_change: exiting\n");
6501 			return (DDI_FAILURE);
6502 		}
6503 
6504 		SD_INFO(SD_LOG_POWER, un, "sd_pm_state_change: "
6505 		    "un_ncmds_in_driver=%ld\n", un->un_ncmds_in_driver);
6506 
6507 		/*
6508 		 * See if the device is not busy, ie.:
6509 		 *    - we have no commands in the driver for this device
6510 		 *    - not waiting for resources
6511 		 */
6512 		if ((un->un_ncmds_in_driver == 0) &&
6513 		    (un->un_state != SD_STATE_RWAIT)) {
6514 			/*
6515 			 * The device is not busy, so it is OK to go to low
6516 			 * power state. Indicate low power, but rely on someone
6517 			 * else to actually change it.
6518 			 */
6519 			mutex_enter(&un->un_pm_mutex);
6520 			un->un_pm_count = -1;
6521 			mutex_exit(&un->un_pm_mutex);
6522 			un->un_power_level = level;
6523 		}
6524 	}
6525 
6526 	mutex_exit(SD_MUTEX(un));
6527 
6528 	SD_TRACE(SD_LOG_POWER, un, "sd_pm_state_change: exit\n");
6529 
6530 	return (DDI_SUCCESS);
6531 }
6532 
6533 
6534 /*
6535  *    Function: sd_pm_idletimeout_handler
6536  *
6537  * Description: A timer routine that's active only while a device is busy.
6538  *		The purpose is to extend slightly the pm framework's busy
6539  *		view of the device to prevent busy/idle thrashing for
6540  *		back-to-back commands. Do this by comparing the current time
6541  *		to the time at which the last command completed and when the
6542  *		difference is greater than sd_pm_idletime, call
6543  *		pm_idle_component. In addition to indicating idle to the pm
6544  *		framework, update the chain type to again use the internal pm
6545  *		layers of the driver.
6546  *
6547  *   Arguments: arg - driver soft state (unit) structure
6548  *
6549  *     Context: Executes in a timeout(9F) thread context
6550  */
6551 
6552 static void
6553 sd_pm_idletimeout_handler(void *arg)
6554 {
6555 	const hrtime_t idletime = sd_pm_idletime * NANOSEC;
6556 	struct sd_lun *un = arg;
6557 
6558 	mutex_enter(&sd_detach_mutex);
6559 	if (un->un_detach_count != 0) {
6560 		/* Abort if the instance is detaching */
6561 		mutex_exit(&sd_detach_mutex);
6562 		return;
6563 	}
6564 	mutex_exit(&sd_detach_mutex);
6565 
6566 	/*
6567 	 * Grab both mutexes, in the proper order, since we're accessing
6568 	 * both PM and softstate variables.
6569 	 */
6570 	mutex_enter(SD_MUTEX(un));
6571 	mutex_enter(&un->un_pm_mutex);
6572 	if (((gethrtime() - un->un_pm_idle_time) > idletime) &&
6573 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6574 		/*
6575 		 * Update the chain types.
6576 		 * This takes affect on the next new command received.
6577 		 */
6578 		if (un->un_f_non_devbsize_supported) {
6579 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6580 		} else {
6581 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6582 		}
6583 		un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD;
6584 
6585 		SD_TRACE(SD_LOG_IO_PM, un,
6586 		    "sd_pm_idletimeout_handler: idling device\n");
6587 		(void) pm_idle_component(SD_DEVINFO(un), 0);
6588 		un->un_pm_idle_timeid = NULL;
6589 	} else {
6590 		un->un_pm_idle_timeid =
6591 		    timeout(sd_pm_idletimeout_handler, un,
6592 		    (drv_usectohz((clock_t)300000))); /* 300 ms. */
6593 	}
6594 	mutex_exit(&un->un_pm_mutex);
6595 	mutex_exit(SD_MUTEX(un));
6596 }
6597 
6598 
6599 /*
6600  *    Function: sd_pm_timeout_handler
6601  *
6602  * Description: Callback to tell framework we are idle.
6603  *
6604  *     Context: timeout(9f) thread context.
6605  */
6606 
6607 static void
6608 sd_pm_timeout_handler(void *arg)
6609 {
6610 	struct sd_lun *un = arg;
6611 
6612 	(void) pm_idle_component(SD_DEVINFO(un), 0);
6613 	mutex_enter(&un->un_pm_mutex);
6614 	un->un_pm_timeid = NULL;
6615 	mutex_exit(&un->un_pm_mutex);
6616 }
6617 
6618 
6619 /*
6620  *    Function: sdpower
6621  *
6622  * Description: PM entry point.
6623  *
6624  * Return Code: DDI_SUCCESS
6625  *		DDI_FAILURE
6626  *
6627  *     Context: Kernel thread context
6628  */
6629 
6630 static int
6631 sdpower(dev_info_t *devi, int component, int level)
6632 {
6633 	struct sd_lun	*un;
6634 	int		instance;
6635 	int		rval = DDI_SUCCESS;
6636 	uint_t		i, log_page_size, maxcycles, ncycles;
6637 	uchar_t		*log_page_data;
6638 	int		log_sense_page;
6639 	int		medium_present;
6640 	time_t		intvlp;
6641 	struct pm_trans_data	sd_pm_tran_data;
6642 	uchar_t		save_state;
6643 	int		sval;
6644 	uchar_t		state_before_pm;
6645 	int		got_semaphore_here;
6646 	sd_ssc_t	*ssc;
6647 	int	last_power_level;
6648 
6649 	instance = ddi_get_instance(devi);
6650 
6651 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6652 	    !SD_PM_IS_LEVEL_VALID(un, level) || component != 0) {
6653 		return (DDI_FAILURE);
6654 	}
6655 
6656 	ssc = sd_ssc_init(un);
6657 
6658 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6659 
6660 	/*
6661 	 * Must synchronize power down with close.
6662 	 * Attempt to decrement/acquire the open/close semaphore,
6663 	 * but do NOT wait on it. If it's not greater than zero,
6664 	 * ie. it can't be decremented without waiting, then
6665 	 * someone else, either open or close, already has it
6666 	 * and the try returns 0. Use that knowledge here to determine
6667 	 * if it's OK to change the device power level.
6668 	 * Also, only increment it on exit if it was decremented, ie. gotten,
6669 	 * here.
6670 	 */
6671 	got_semaphore_here = sema_tryp(&un->un_semoclose);
6672 
6673 	mutex_enter(SD_MUTEX(un));
6674 
6675 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
6676 	    un->un_ncmds_in_driver);
6677 
6678 	/*
6679 	 * If un_ncmds_in_driver is non-zero it indicates commands are
6680 	 * already being processed in the driver, or if the semaphore was
6681 	 * not gotten here it indicates an open or close is being processed.
6682 	 * At the same time somebody is requesting to go to a lower power
6683 	 * that can't perform I/O, which can't happen, therefore we need to
6684 	 * return failure.
6685 	 */
6686 	if ((!SD_PM_IS_IO_CAPABLE(un, level)) &&
6687 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
6688 		mutex_exit(SD_MUTEX(un));
6689 
6690 		if (got_semaphore_here != 0) {
6691 			sema_v(&un->un_semoclose);
6692 		}
6693 		SD_TRACE(SD_LOG_IO_PM, un,
6694 		    "sdpower: exit, device has queued cmds.\n");
6695 
6696 		goto sdpower_failed;
6697 	}
6698 
6699 	/*
6700 	 * if it is OFFLINE that means the disk is completely dead
6701 	 * in our case we have to put the disk in on or off by sending commands
6702 	 * Of course that will fail anyway so return back here.
6703 	 *
6704 	 * Power changes to a device that's OFFLINE or SUSPENDED
6705 	 * are not allowed.
6706 	 */
6707 	if ((un->un_state == SD_STATE_OFFLINE) ||
6708 	    (un->un_state == SD_STATE_SUSPENDED)) {
6709 		mutex_exit(SD_MUTEX(un));
6710 
6711 		if (got_semaphore_here != 0) {
6712 			sema_v(&un->un_semoclose);
6713 		}
6714 		SD_TRACE(SD_LOG_IO_PM, un,
6715 		    "sdpower: exit, device is off-line.\n");
6716 
6717 		goto sdpower_failed;
6718 	}
6719 
6720 	/*
6721 	 * Change the device's state to indicate it's power level
6722 	 * is being changed. Do this to prevent a power off in the
6723 	 * middle of commands, which is especially bad on devices
6724 	 * that are really powered off instead of just spun down.
6725 	 */
6726 	state_before_pm = un->un_state;
6727 	un->un_state = SD_STATE_PM_CHANGING;
6728 
6729 	mutex_exit(SD_MUTEX(un));
6730 
6731 	/*
6732 	 * If log sense command is not supported, bypass the
6733 	 * following checking, otherwise, check the log sense
6734 	 * information for this device.
6735 	 */
6736 	if (SD_PM_STOP_MOTOR_NEEDED(un, level) &&
6737 	    un->un_f_log_sense_supported) {
6738 		/*
6739 		 * Get the log sense information to understand whether the
6740 		 * the powercycle counts have gone beyond the threshhold.
6741 		 */
6742 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6743 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6744 
6745 		mutex_enter(SD_MUTEX(un));
6746 		log_sense_page = un->un_start_stop_cycle_page;
6747 		mutex_exit(SD_MUTEX(un));
6748 
6749 		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6750 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
6751 
6752 		if (rval != 0) {
6753 			if (rval == EIO)
6754 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6755 			else
6756 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6757 		}
6758 
6759 #ifdef	SDDEBUG
6760 		if (sd_force_pm_supported) {
6761 			/* Force a successful result */
6762 			rval = 0;
6763 		}
6764 #endif
6765 		if (rval != 0) {
6766 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
6767 			    "Log Sense Failed\n");
6768 
6769 			kmem_free(log_page_data, log_page_size);
6770 			/* Cannot support power management on those drives */
6771 
6772 			if (got_semaphore_here != 0) {
6773 				sema_v(&un->un_semoclose);
6774 			}
6775 			/*
6776 			 * On exit put the state back to it's original value
6777 			 * and broadcast to anyone waiting for the power
6778 			 * change completion.
6779 			 */
6780 			mutex_enter(SD_MUTEX(un));
6781 			un->un_state = state_before_pm;
6782 			cv_broadcast(&un->un_suspend_cv);
6783 			mutex_exit(SD_MUTEX(un));
6784 			SD_TRACE(SD_LOG_IO_PM, un,
6785 			    "sdpower: exit, Log Sense Failed.\n");
6786 
6787 			goto sdpower_failed;
6788 		}
6789 
6790 		/*
6791 		 * From the page data - Convert the essential information to
6792 		 * pm_trans_data
6793 		 */
6794 		maxcycles =
6795 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
6796 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
6797 
6798 		ncycles =
6799 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
6800 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
6801 
6802 		if (un->un_f_pm_log_sense_smart) {
6803 			sd_pm_tran_data.un.smart_count.allowed = maxcycles;
6804 			sd_pm_tran_data.un.smart_count.consumed = ncycles;
6805 			sd_pm_tran_data.un.smart_count.flag = 0;
6806 			sd_pm_tran_data.format = DC_SMART_FORMAT;
6807 		} else {
6808 			sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
6809 			sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
6810 			for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
6811 				sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
6812 				    log_page_data[8+i];
6813 			}
6814 			sd_pm_tran_data.un.scsi_cycles.flag = 0;
6815 			sd_pm_tran_data.format = DC_SCSI_FORMAT;
6816 		}
6817 
6818 		kmem_free(log_page_data, log_page_size);
6819 
6820 		/*
6821 		 * Call pm_trans_check routine to get the Ok from
6822 		 * the global policy
6823 		 */
6824 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
6825 #ifdef	SDDEBUG
6826 		if (sd_force_pm_supported) {
6827 			/* Force a successful result */
6828 			rval = 1;
6829 		}
6830 #endif
6831 		switch (rval) {
6832 		case 0:
6833 			/*
6834 			 * Not Ok to Power cycle or error in parameters passed
6835 			 * Would have given the advised time to consider power
6836 			 * cycle. Based on the new intvlp parameter we are
6837 			 * supposed to pretend we are busy so that pm framework
6838 			 * will never call our power entry point. Because of
6839 			 * that install a timeout handler and wait for the
6840 			 * recommended time to elapse so that power management
6841 			 * can be effective again.
6842 			 *
6843 			 * To effect this behavior, call pm_busy_component to
6844 			 * indicate to the framework this device is busy.
6845 			 * By not adjusting un_pm_count the rest of PM in
6846 			 * the driver will function normally, and independent
6847 			 * of this but because the framework is told the device
6848 			 * is busy it won't attempt powering down until it gets
6849 			 * a matching idle. The timeout handler sends this.
6850 			 * Note: sd_pm_entry can't be called here to do this
6851 			 * because sdpower may have been called as a result
6852 			 * of a call to pm_raise_power from within sd_pm_entry.
6853 			 *
6854 			 * If a timeout handler is already active then
6855 			 * don't install another.
6856 			 */
6857 			mutex_enter(&un->un_pm_mutex);
6858 			if (un->un_pm_timeid == NULL) {
6859 				un->un_pm_timeid =
6860 				    timeout(sd_pm_timeout_handler,
6861 				    un, intvlp * drv_usectohz(1000000));
6862 				mutex_exit(&un->un_pm_mutex);
6863 				(void) pm_busy_component(SD_DEVINFO(un), 0);
6864 			} else {
6865 				mutex_exit(&un->un_pm_mutex);
6866 			}
6867 			if (got_semaphore_here != 0) {
6868 				sema_v(&un->un_semoclose);
6869 			}
6870 			/*
6871 			 * On exit put the state back to it's original value
6872 			 * and broadcast to anyone waiting for the power
6873 			 * change completion.
6874 			 */
6875 			mutex_enter(SD_MUTEX(un));
6876 			un->un_state = state_before_pm;
6877 			cv_broadcast(&un->un_suspend_cv);
6878 			mutex_exit(SD_MUTEX(un));
6879 
6880 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
6881 			    "trans check Failed, not ok to power cycle.\n");
6882 
6883 			goto sdpower_failed;
6884 		case -1:
6885 			if (got_semaphore_here != 0) {
6886 				sema_v(&un->un_semoclose);
6887 			}
6888 			/*
6889 			 * On exit put the state back to it's original value
6890 			 * and broadcast to anyone waiting for the power
6891 			 * change completion.
6892 			 */
6893 			mutex_enter(SD_MUTEX(un));
6894 			un->un_state = state_before_pm;
6895 			cv_broadcast(&un->un_suspend_cv);
6896 			mutex_exit(SD_MUTEX(un));
6897 			SD_TRACE(SD_LOG_IO_PM, un,
6898 			    "sdpower: exit, trans check command Failed.\n");
6899 
6900 			goto sdpower_failed;
6901 		}
6902 	}
6903 
6904 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
6905 		/*
6906 		 * Save the last state... if the STOP FAILS we need it
6907 		 * for restoring
6908 		 */
6909 		mutex_enter(SD_MUTEX(un));
6910 		save_state = un->un_last_state;
6911 		last_power_level = un->un_power_level;
6912 		/*
6913 		 * There must not be any cmds. getting processed
6914 		 * in the driver when we get here. Power to the
6915 		 * device is potentially going off.
6916 		 */
6917 		ASSERT(un->un_ncmds_in_driver == 0);
6918 		mutex_exit(SD_MUTEX(un));
6919 
6920 		/*
6921 		 * For now PM suspend the device completely before spindle is
6922 		 * turned off
6923 		 */
6924 		if ((rval = sd_pm_state_change(un, level, SD_PM_STATE_CHANGE))
6925 		    == DDI_FAILURE) {
6926 			if (got_semaphore_here != 0) {
6927 				sema_v(&un->un_semoclose);
6928 			}
6929 			/*
6930 			 * On exit put the state back to it's original value
6931 			 * and broadcast to anyone waiting for the power
6932 			 * change completion.
6933 			 */
6934 			mutex_enter(SD_MUTEX(un));
6935 			un->un_state = state_before_pm;
6936 			un->un_power_level = last_power_level;
6937 			cv_broadcast(&un->un_suspend_cv);
6938 			mutex_exit(SD_MUTEX(un));
6939 			SD_TRACE(SD_LOG_IO_PM, un,
6940 			    "sdpower: exit, PM suspend Failed.\n");
6941 
6942 			goto sdpower_failed;
6943 		}
6944 	}
6945 
6946 	/*
6947 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
6948 	 * close, or strategy. Dump no long uses this routine, it uses it's
6949 	 * own code so it can be done in polled mode.
6950 	 */
6951 
6952 	medium_present = TRUE;
6953 
6954 	/*
6955 	 * When powering up, issue a TUR in case the device is at unit
6956 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
6957 	 * a deadlock on un_pm_busy_cv will occur.
6958 	 */
6959 	if (SD_PM_IS_IO_CAPABLE(un, level)) {
6960 		sval = sd_send_scsi_TEST_UNIT_READY(ssc,
6961 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
6962 		if (sval != 0)
6963 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6964 	}
6965 
6966 	if (un->un_f_power_condition_supported) {
6967 		char *pm_condition_name[] = {"STOPPED", "STANDBY",
6968 		    "IDLE", "ACTIVE"};
6969 		SD_TRACE(SD_LOG_IO_PM, un,
6970 		    "sdpower: sending \'%s\' power condition",
6971 		    pm_condition_name[level]);
6972 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
6973 		    sd_pl2pc[level], SD_PATH_DIRECT);
6974 	} else {
6975 		SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
6976 		    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
6977 		sval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
6978 		    ((level == SD_SPINDLE_ON) ? SD_TARGET_START :
6979 		    SD_TARGET_STOP), SD_PATH_DIRECT);
6980 	}
6981 	if (sval != 0) {
6982 		if (sval == EIO)
6983 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
6984 		else
6985 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6986 	}
6987 
6988 	/* Command failed, check for media present. */
6989 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
6990 		medium_present = FALSE;
6991 	}
6992 
6993 	/*
6994 	 * The conditions of interest here are:
6995 	 *   if a spindle off with media present fails,
6996 	 *	then restore the state and return an error.
6997 	 *   else if a spindle on fails,
6998 	 *	then return an error (there's no state to restore).
6999 	 * In all other cases we setup for the new state
7000 	 * and return success.
7001 	 */
7002 	if (!SD_PM_IS_IO_CAPABLE(un, level)) {
7003 		if ((medium_present == TRUE) && (sval != 0)) {
7004 			/* The stop command from above failed */
7005 			rval = DDI_FAILURE;
7006 			/*
7007 			 * The stop command failed, and we have media
7008 			 * present. Put the level back by calling the
7009 			 * sd_pm_resume() and set the state back to
7010 			 * it's previous value.
7011 			 */
7012 			(void) sd_pm_state_change(un, last_power_level,
7013 			    SD_PM_STATE_ROLLBACK);
7014 			mutex_enter(SD_MUTEX(un));
7015 			un->un_last_state = save_state;
7016 			mutex_exit(SD_MUTEX(un));
7017 		} else if (un->un_f_monitor_media_state) {
7018 			/*
7019 			 * The stop command from above succeeded.
7020 			 * Terminate watch thread in case of removable media
7021 			 * devices going into low power state. This is as per
7022 			 * the requirements of pm framework, otherwise commands
7023 			 * will be generated for the device (through watch
7024 			 * thread), even when the device is in low power state.
7025 			 */
7026 			mutex_enter(SD_MUTEX(un));
7027 			un->un_f_watcht_stopped = FALSE;
7028 			if (un->un_swr_token != NULL) {
7029 				opaque_t temp_token = un->un_swr_token;
7030 				un->un_f_watcht_stopped = TRUE;
7031 				un->un_swr_token = NULL;
7032 				mutex_exit(SD_MUTEX(un));
7033 				(void) scsi_watch_request_terminate(temp_token,
7034 				    SCSI_WATCH_TERMINATE_ALL_WAIT);
7035 			} else {
7036 				mutex_exit(SD_MUTEX(un));
7037 			}
7038 		}
7039 	} else {
7040 		/*
7041 		 * The level requested is I/O capable.
7042 		 * Legacy behavior: return success on a failed spinup
7043 		 * if there is no media in the drive.
7044 		 * Do this by looking at medium_present here.
7045 		 */
7046 		if ((sval != 0) && medium_present) {
7047 			/* The start command from above failed */
7048 			rval = DDI_FAILURE;
7049 		} else {
7050 			/*
7051 			 * The start command from above succeeded
7052 			 * PM resume the devices now that we have
7053 			 * started the disks
7054 			 */
7055 			(void) sd_pm_state_change(un, level,
7056 			    SD_PM_STATE_CHANGE);
7057 
7058 			/*
7059 			 * Resume the watch thread since it was suspended
7060 			 * when the device went into low power mode.
7061 			 */
7062 			if (un->un_f_monitor_media_state) {
7063 				mutex_enter(SD_MUTEX(un));
7064 				if (un->un_f_watcht_stopped == TRUE) {
7065 					opaque_t temp_token;
7066 
7067 					un->un_f_watcht_stopped = FALSE;
7068 					mutex_exit(SD_MUTEX(un));
7069 					temp_token =
7070 					    sd_watch_request_submit(un);
7071 					mutex_enter(SD_MUTEX(un));
7072 					un->un_swr_token = temp_token;
7073 				}
7074 				mutex_exit(SD_MUTEX(un));
7075 			}
7076 		}
7077 	}
7078 
7079 	if (got_semaphore_here != 0) {
7080 		sema_v(&un->un_semoclose);
7081 	}
7082 	/*
7083 	 * On exit put the state back to it's original value
7084 	 * and broadcast to anyone waiting for the power
7085 	 * change completion.
7086 	 */
7087 	mutex_enter(SD_MUTEX(un));
7088 	un->un_state = state_before_pm;
7089 	cv_broadcast(&un->un_suspend_cv);
7090 	mutex_exit(SD_MUTEX(un));
7091 
7092 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7093 
7094 	sd_ssc_fini(ssc);
7095 	return (rval);
7096 
7097 sdpower_failed:
7098 
7099 	sd_ssc_fini(ssc);
7100 	return (DDI_FAILURE);
7101 }
7102 
7103 
7104 
7105 /*
7106  *    Function: sdattach
7107  *
7108  * Description: Driver's attach(9e) entry point function.
7109  *
7110  *   Arguments: devi - opaque device info handle
7111  *		cmd  - attach  type
7112  *
7113  * Return Code: DDI_SUCCESS
7114  *		DDI_FAILURE
7115  *
7116  *     Context: Kernel thread context
7117  */
7118 
7119 static int
7120 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7121 {
7122 	switch (cmd) {
7123 	case DDI_ATTACH:
7124 		return (sd_unit_attach(devi));
7125 	case DDI_RESUME:
7126 		return (sd_ddi_resume(devi));
7127 	default:
7128 		break;
7129 	}
7130 	return (DDI_FAILURE);
7131 }
7132 
7133 
7134 /*
7135  *    Function: sddetach
7136  *
7137  * Description: Driver's detach(9E) entry point function.
7138  *
7139  *   Arguments: devi - opaque device info handle
7140  *		cmd  - detach  type
7141  *
7142  * Return Code: DDI_SUCCESS
7143  *		DDI_FAILURE
7144  *
7145  *     Context: Kernel thread context
7146  */
7147 
7148 static int
7149 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7150 {
7151 	switch (cmd) {
7152 	case DDI_DETACH:
7153 		return (sd_unit_detach(devi));
7154 	case DDI_SUSPEND:
7155 		return (sd_ddi_suspend(devi));
7156 	default:
7157 		break;
7158 	}
7159 	return (DDI_FAILURE);
7160 }
7161 
7162 
7163 /*
7164  *     Function: sd_sync_with_callback
7165  *
7166  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7167  *		 state while the callback routine is active.
7168  *
7169  *    Arguments: un: softstate structure for the instance
7170  *
7171  *	Context: Kernel thread context
7172  */
7173 
7174 static void
7175 sd_sync_with_callback(struct sd_lun *un)
7176 {
7177 	ASSERT(un != NULL);
7178 
7179 	mutex_enter(SD_MUTEX(un));
7180 
7181 	ASSERT(un->un_in_callback >= 0);
7182 
7183 	while (un->un_in_callback > 0) {
7184 		mutex_exit(SD_MUTEX(un));
7185 		delay(2);
7186 		mutex_enter(SD_MUTEX(un));
7187 	}
7188 
7189 	mutex_exit(SD_MUTEX(un));
7190 }
7191 
7192 /*
7193  *    Function: sd_unit_attach
7194  *
7195  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7196  *		the soft state structure for the device and performs
7197  *		all necessary structure and device initializations.
7198  *
7199  *   Arguments: devi: the system's dev_info_t for the device.
7200  *
7201  * Return Code: DDI_SUCCESS if attach is successful.
7202  *		DDI_FAILURE if any part of the attach fails.
7203  *
7204  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7205  *		Kernel thread context only.  Can sleep.
7206  */
7207 
7208 static int
7209 sd_unit_attach(dev_info_t *devi)
7210 {
7211 	struct	scsi_device	*devp;
7212 	struct	sd_lun		*un;
7213 	char			*variantp;
7214 	char			name_str[48];
7215 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7216 	int	instance;
7217 	int	rval;
7218 	int	wc_enabled;
7219 	int	tgt;
7220 	uint64_t	capacity;
7221 	uint_t		lbasize = 0;
7222 	dev_info_t	*pdip = ddi_get_parent(devi);
7223 	int		offbyone = 0;
7224 	int		geom_label_valid = 0;
7225 	sd_ssc_t	*ssc;
7226 	int		status;
7227 	struct sd_fm_internal	*sfip = NULL;
7228 	int		max_xfer_size;
7229 
7230 	/*
7231 	 * Retrieve the target driver's private data area. This was set
7232 	 * up by the HBA.
7233 	 */
7234 	devp = ddi_get_driver_private(devi);
7235 
7236 	/*
7237 	 * Retrieve the target ID of the device.
7238 	 */
7239 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7240 	    SCSI_ADDR_PROP_TARGET, -1);
7241 
7242 	/*
7243 	 * Since we have no idea what state things were left in by the last
7244 	 * user of the device, set up some 'default' settings, ie. turn 'em
7245 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7246 	 * Do this before the scsi_probe, which sends an inquiry.
7247 	 * This is a fix for bug (4430280).
7248 	 * Of special importance is wide-xfer. The drive could have been left
7249 	 * in wide transfer mode by the last driver to communicate with it,
7250 	 * this includes us. If that's the case, and if the following is not
7251 	 * setup properly or we don't re-negotiate with the drive prior to
7252 	 * transferring data to/from the drive, it causes bus parity errors,
7253 	 * data overruns, and unexpected interrupts. This first occurred when
7254 	 * the fix for bug (4378686) was made.
7255 	 */
7256 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7257 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7258 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7259 
7260 	/*
7261 	 * Currently, scsi_ifsetcap sets tagged-qing capability for all LUNs
7262 	 * on a target. Setting it per lun instance actually sets the
7263 	 * capability of this target, which affects those luns already
7264 	 * attached on the same target. So during attach, we can only disable
7265 	 * this capability only when no other lun has been attached on this
7266 	 * target. By doing this, we assume a target has the same tagged-qing
7267 	 * capability for every lun. The condition can be removed when HBA
7268 	 * is changed to support per lun based tagged-qing capability.
7269 	 */
7270 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
7271 		(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7272 	}
7273 
7274 	/*
7275 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7276 	 * This call will allocate and fill in the scsi_inquiry structure
7277 	 * and point the sd_inq member of the scsi_device structure to it.
7278 	 * If the attach succeeds, then this memory will not be de-allocated
7279 	 * (via scsi_unprobe()) until the instance is detached.
7280 	 */
7281 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7282 		goto probe_failed;
7283 	}
7284 
7285 	/*
7286 	 * Check the device type as specified in the inquiry data and
7287 	 * claim it if it is of a type that we support.
7288 	 */
7289 	switch (devp->sd_inq->inq_dtype) {
7290 	case DTYPE_DIRECT:
7291 		break;
7292 	case DTYPE_RODIRECT:
7293 		break;
7294 	case DTYPE_OPTICAL:
7295 		break;
7296 	case DTYPE_NOTPRESENT:
7297 	default:
7298 		/* Unsupported device type; fail the attach. */
7299 		goto probe_failed;
7300 	}
7301 
7302 	/*
7303 	 * Allocate the soft state structure for this unit.
7304 	 *
7305 	 * We rely upon this memory being set to all zeroes by
7306 	 * ddi_soft_state_zalloc().  We assume that any member of the
7307 	 * soft state structure that is not explicitly initialized by
7308 	 * this routine will have a value of zero.
7309 	 */
7310 	instance = ddi_get_instance(devp->sd_dev);
7311 #ifndef XPV_HVM_DRIVER
7312 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7313 		goto probe_failed;
7314 	}
7315 #endif /* !XPV_HVM_DRIVER */
7316 
7317 	/*
7318 	 * Retrieve a pointer to the newly-allocated soft state.
7319 	 *
7320 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7321 	 * was successful, unless something has gone horribly wrong and the
7322 	 * ddi's soft state internals are corrupt (in which case it is
7323 	 * probably better to halt here than just fail the attach....)
7324 	 */
7325 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7326 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7327 		    instance);
7328 		/*NOTREACHED*/
7329 	}
7330 
7331 	/*
7332 	 * Link the back ptr of the driver soft state to the scsi_device
7333 	 * struct for this lun.
7334 	 * Save a pointer to the softstate in the driver-private area of
7335 	 * the scsi_device struct.
7336 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7337 	 * we first set un->un_sd below.
7338 	 */
7339 	un->un_sd = devp;
7340 	devp->sd_private = (opaque_t)un;
7341 
7342 	/*
7343 	 * The following must be after devp is stored in the soft state struct.
7344 	 */
7345 #ifdef SDDEBUG
7346 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7347 	    "%s_unit_attach: un:0x%p instance:%d\n",
7348 	    ddi_driver_name(devi), un, instance);
7349 #endif
7350 
7351 	/*
7352 	 * Set up the device type and node type (for the minor nodes).
7353 	 * By default we assume that the device can at least support the
7354 	 * Common Command Set. Call it a CD-ROM if it reports itself
7355 	 * as a RODIRECT device.
7356 	 */
7357 	switch (devp->sd_inq->inq_dtype) {
7358 	case DTYPE_RODIRECT:
7359 		un->un_node_type = DDI_NT_CD_CHAN;
7360 		un->un_ctype	 = CTYPE_CDROM;
7361 		break;
7362 	case DTYPE_OPTICAL:
7363 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7364 		un->un_ctype	 = CTYPE_ROD;
7365 		break;
7366 	default:
7367 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7368 		un->un_ctype	 = CTYPE_CCS;
7369 		break;
7370 	}
7371 
7372 	/*
7373 	 * Try to read the interconnect type from the HBA.
7374 	 *
7375 	 * Note: This driver is currently compiled as two binaries, a parallel
7376 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7377 	 * differences are determined at compile time. In the future a single
7378 	 * binary will be provided and the interconnect type will be used to
7379 	 * differentiate between fibre and parallel scsi behaviors. At that time
7380 	 * it will be necessary for all fibre channel HBAs to support this
7381 	 * property.
7382 	 *
7383 	 * set un_f_is_fiber to TRUE ( default fiber )
7384 	 */
7385 	un->un_f_is_fibre = TRUE;
7386 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7387 	case INTERCONNECT_SSA:
7388 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7389 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7390 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7391 		break;
7392 	case INTERCONNECT_PARALLEL:
7393 		un->un_f_is_fibre = FALSE;
7394 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7395 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7396 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7397 		break;
7398 	case INTERCONNECT_SAS:
7399 		un->un_f_is_fibre = FALSE;
7400 		un->un_interconnect_type = SD_INTERCONNECT_SAS;
7401 		un->un_node_type = DDI_NT_BLOCK_SAS;
7402 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7403 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SAS\n", un);
7404 		break;
7405 	case INTERCONNECT_SATA:
7406 		un->un_f_is_fibre = FALSE;
7407 		un->un_interconnect_type = SD_INTERCONNECT_SATA;
7408 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7409 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SATA\n", un);
7410 		break;
7411 	case INTERCONNECT_FIBRE:
7412 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7413 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7414 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7415 		break;
7416 	case INTERCONNECT_FABRIC:
7417 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7418 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7419 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7420 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7421 		break;
7422 	default:
7423 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7424 		/*
7425 		 * The HBA does not support the "interconnect-type" property
7426 		 * (or did not provide a recognized type).
7427 		 *
7428 		 * Note: This will be obsoleted when a single fibre channel
7429 		 * and parallel scsi driver is delivered. In the meantime the
7430 		 * interconnect type will be set to the platform default.If that
7431 		 * type is not parallel SCSI, it means that we should be
7432 		 * assuming "ssd" semantics. However, here this also means that
7433 		 * the FC HBA is not supporting the "interconnect-type" property
7434 		 * like we expect it to, so log this occurrence.
7435 		 */
7436 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7437 		if (!SD_IS_PARALLEL_SCSI(un)) {
7438 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7439 			    "sd_unit_attach: un:0x%p Assuming "
7440 			    "INTERCONNECT_FIBRE\n", un);
7441 		} else {
7442 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7443 			    "sd_unit_attach: un:0x%p Assuming "
7444 			    "INTERCONNECT_PARALLEL\n", un);
7445 			un->un_f_is_fibre = FALSE;
7446 		}
7447 #else
7448 		/*
7449 		 * Note: This source will be implemented when a single fibre
7450 		 * channel and parallel scsi driver is delivered. The default
7451 		 * will be to assume that if a device does not support the
7452 		 * "interconnect-type" property it is a parallel SCSI HBA and
7453 		 * we will set the interconnect type for parallel scsi.
7454 		 */
7455 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7456 		un->un_f_is_fibre = FALSE;
7457 #endif
7458 		break;
7459 	}
7460 
7461 	if (un->un_f_is_fibre == TRUE) {
7462 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7463 		    SCSI_VERSION_3) {
7464 			switch (un->un_interconnect_type) {
7465 			case SD_INTERCONNECT_FIBRE:
7466 			case SD_INTERCONNECT_SSA:
7467 				un->un_node_type = DDI_NT_BLOCK_WWN;
7468 				break;
7469 			default:
7470 				break;
7471 			}
7472 		}
7473 	}
7474 
7475 	/*
7476 	 * Initialize the Request Sense command for the target
7477 	 */
7478 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7479 		goto alloc_rqs_failed;
7480 	}
7481 
7482 	/*
7483 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7484 	 * with separate binary for sd and ssd.
7485 	 *
7486 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7487 	 * The hardcoded values will go away when Sparc uses 1 binary
7488 	 * for sd and ssd.  This hardcoded values need to match
7489 	 * SD_RETRY_COUNT in sddef.h
7490 	 * The value used is base on interconnect type.
7491 	 * fibre = 3, parallel = 5
7492 	 */
7493 #if defined(__i386) || defined(__amd64)
7494 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7495 #else
7496 	un->un_retry_count = SD_RETRY_COUNT;
7497 #endif
7498 
7499 	/*
7500 	 * Set the per disk retry count to the default number of retries
7501 	 * for disks and CDROMs. This value can be overridden by the
7502 	 * disk property list or an entry in sd.conf.
7503 	 */
7504 	un->un_notready_retry_count =
7505 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7506 	    : DISK_NOT_READY_RETRY_COUNT(un);
7507 
7508 	/*
7509 	 * Set the busy retry count to the default value of un_retry_count.
7510 	 * This can be overridden by entries in sd.conf or the device
7511 	 * config table.
7512 	 */
7513 	un->un_busy_retry_count = un->un_retry_count;
7514 
7515 	/*
7516 	 * Init the reset threshold for retries.  This number determines
7517 	 * how many retries must be performed before a reset can be issued
7518 	 * (for certain error conditions). This can be overridden by entries
7519 	 * in sd.conf or the device config table.
7520 	 */
7521 	un->un_reset_retry_count = (un->un_retry_count / 2);
7522 
7523 	/*
7524 	 * Set the victim_retry_count to the default un_retry_count
7525 	 */
7526 	un->un_victim_retry_count = (2 * un->un_retry_count);
7527 
7528 	/*
7529 	 * Set the reservation release timeout to the default value of
7530 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7531 	 * device config table.
7532 	 */
7533 	un->un_reserve_release_time = 5;
7534 
7535 	/*
7536 	 * Set up the default maximum transfer size. Note that this may
7537 	 * get updated later in the attach, when setting up default wide
7538 	 * operations for disks.
7539 	 */
7540 #if defined(__i386) || defined(__amd64)
7541 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7542 	un->un_partial_dma_supported = 1;
7543 #else
7544 	un->un_max_xfer_size = (uint_t)maxphys;
7545 #endif
7546 
7547 	/*
7548 	 * Get "allow bus device reset" property (defaults to "enabled" if
7549 	 * the property was not defined). This is to disable bus resets for
7550 	 * certain kinds of error recovery. Note: In the future when a run-time
7551 	 * fibre check is available the soft state flag should default to
7552 	 * enabled.
7553 	 */
7554 	if (un->un_f_is_fibre == TRUE) {
7555 		un->un_f_allow_bus_device_reset = TRUE;
7556 	} else {
7557 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7558 		    "allow-bus-device-reset", 1) != 0) {
7559 			un->un_f_allow_bus_device_reset = TRUE;
7560 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7561 			    "sd_unit_attach: un:0x%p Bus device reset "
7562 			    "enabled\n", un);
7563 		} else {
7564 			un->un_f_allow_bus_device_reset = FALSE;
7565 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7566 			    "sd_unit_attach: un:0x%p Bus device reset "
7567 			    "disabled\n", un);
7568 		}
7569 	}
7570 
7571 	/*
7572 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7573 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7574 	 *
7575 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7576 	 * property. The new "variant" property with a value of "atapi" has been
7577 	 * introduced so that future 'variants' of standard SCSI behavior (like
7578 	 * atapi) could be specified by the underlying HBA drivers by supplying
7579 	 * a new value for the "variant" property, instead of having to define a
7580 	 * new property.
7581 	 */
7582 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7583 		un->un_f_cfg_is_atapi = TRUE;
7584 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7585 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7586 	}
7587 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7588 	    &variantp) == DDI_PROP_SUCCESS) {
7589 		if (strcmp(variantp, "atapi") == 0) {
7590 			un->un_f_cfg_is_atapi = TRUE;
7591 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7592 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7593 		}
7594 		ddi_prop_free(variantp);
7595 	}
7596 
7597 	un->un_cmd_timeout	= SD_IO_TIME;
7598 
7599 	un->un_busy_timeout  = SD_BSY_TIMEOUT;
7600 
7601 	/* Info on current states, statuses, etc. (Updated frequently) */
7602 	un->un_state		= SD_STATE_NORMAL;
7603 	un->un_last_state	= SD_STATE_NORMAL;
7604 
7605 	/* Control & status info for command throttling */
7606 	un->un_throttle		= sd_max_throttle;
7607 	un->un_saved_throttle	= sd_max_throttle;
7608 	un->un_min_throttle	= sd_min_throttle;
7609 
7610 	if (un->un_f_is_fibre == TRUE) {
7611 		un->un_f_use_adaptive_throttle = TRUE;
7612 	} else {
7613 		un->un_f_use_adaptive_throttle = FALSE;
7614 	}
7615 
7616 	/* Removable media support. */
7617 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7618 	un->un_mediastate		= DKIO_NONE;
7619 	un->un_specified_mediastate	= DKIO_NONE;
7620 
7621 	/* CVs for suspend/resume (PM or DR) */
7622 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7623 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7624 
7625 	/* Power management support. */
7626 	un->un_power_level = SD_SPINDLE_UNINIT;
7627 
7628 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
7629 	un->un_f_wcc_inprog = 0;
7630 
7631 	/*
7632 	 * The open/close semaphore is used to serialize threads executing
7633 	 * in the driver's open & close entry point routines for a given
7634 	 * instance.
7635 	 */
7636 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7637 
7638 	/*
7639 	 * The conf file entry and softstate variable is a forceful override,
7640 	 * meaning a non-zero value must be entered to change the default.
7641 	 */
7642 	un->un_f_disksort_disabled = FALSE;
7643 	un->un_f_rmw_type = SD_RMW_TYPE_DEFAULT;
7644 	un->un_f_enable_rmw = FALSE;
7645 
7646 	/*
7647 	 * GET EVENT STATUS NOTIFICATION media polling enabled by default, but
7648 	 * can be overridden via [s]sd-config-list "mmc-gesn-polling" property.
7649 	 */
7650 	un->un_f_mmc_gesn_polling = TRUE;
7651 
7652 	/*
7653 	 * physical sector size defaults to DEV_BSIZE currently. We can
7654 	 * override this value via the driver configuration file so we must
7655 	 * set it before calling sd_read_unit_properties().
7656 	 */
7657 	un->un_phy_blocksize = DEV_BSIZE;
7658 
7659 	/*
7660 	 * Retrieve the properties from the static driver table or the driver
7661 	 * configuration file (.conf) for this unit and update the soft state
7662 	 * for the device as needed for the indicated properties.
7663 	 * Note: the property configuration needs to occur here as some of the
7664 	 * following routines may have dependencies on soft state flags set
7665 	 * as part of the driver property configuration.
7666 	 */
7667 	sd_read_unit_properties(un);
7668 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7669 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7670 
7671 	/*
7672 	 * Only if a device has "hotpluggable" property, it is
7673 	 * treated as hotpluggable device. Otherwise, it is
7674 	 * regarded as non-hotpluggable one.
7675 	 */
7676 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
7677 	    -1) != -1) {
7678 		un->un_f_is_hotpluggable = TRUE;
7679 	}
7680 
7681 	/*
7682 	 * set unit's attributes(flags) according to "hotpluggable" and
7683 	 * RMB bit in INQUIRY data.
7684 	 */
7685 	sd_set_unit_attributes(un, devi);
7686 
7687 	/*
7688 	 * By default, we mark the capacity, lbasize, and geometry
7689 	 * as invalid. Only if we successfully read a valid capacity
7690 	 * will we update the un_blockcount and un_tgt_blocksize with the
7691 	 * valid values (the geometry will be validated later).
7692 	 */
7693 	un->un_f_blockcount_is_valid	= FALSE;
7694 	un->un_f_tgt_blocksize_is_valid	= FALSE;
7695 
7696 	/*
7697 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7698 	 * otherwise.
7699 	 */
7700 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7701 	un->un_blockcount = 0;
7702 
7703 	/*
7704 	 * Set up the per-instance info needed to determine the correct
7705 	 * CDBs and other info for issuing commands to the target.
7706 	 */
7707 	sd_init_cdb_limits(un);
7708 
7709 	/*
7710 	 * Set up the IO chains to use, based upon the target type.
7711 	 */
7712 	if (un->un_f_non_devbsize_supported) {
7713 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7714 	} else {
7715 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7716 	}
7717 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7718 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7719 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7720 
7721 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7722 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7723 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7724 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7725 
7726 
7727 	if (ISCD(un)) {
7728 		un->un_additional_codes = sd_additional_codes;
7729 	} else {
7730 		un->un_additional_codes = NULL;
7731 	}
7732 
7733 	/*
7734 	 * Create the kstats here so they can be available for attach-time
7735 	 * routines that send commands to the unit (either polled or via
7736 	 * sd_send_scsi_cmd).
7737 	 *
7738 	 * Note: This is a critical sequence that needs to be maintained:
7739 	 *	1) Instantiate the kstats here, before any routines using the
7740 	 *	   iopath (i.e. sd_send_scsi_cmd).
7741 	 *	2) Instantiate and initialize the partition stats
7742 	 *	   (sd_set_pstats).
7743 	 *	3) Initialize the error stats (sd_set_errstats), following
7744 	 *	   sd_validate_geometry(),sd_register_devid(),
7745 	 *	   and sd_cache_control().
7746 	 */
7747 
7748 	un->un_stats = kstat_create(sd_label, instance,
7749 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7750 	if (un->un_stats != NULL) {
7751 		un->un_stats->ks_lock = SD_MUTEX(un);
7752 		kstat_install(un->un_stats);
7753 	}
7754 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7755 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
7756 
7757 	sd_create_errstats(un, instance);
7758 	if (un->un_errstats == NULL) {
7759 		goto create_errstats_failed;
7760 	}
7761 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7762 	    "sd_unit_attach: un:0x%p errstats created\n", un);
7763 
7764 	/*
7765 	 * The following if/else code was relocated here from below as part
7766 	 * of the fix for bug (4430280). However with the default setup added
7767 	 * on entry to this routine, it's no longer absolutely necessary for
7768 	 * this to be before the call to sd_spin_up_unit.
7769 	 */
7770 	if (SD_IS_PARALLEL_SCSI(un) || SD_IS_SERIAL(un)) {
7771 		int tq_trigger_flag = (((devp->sd_inq->inq_ansi == 4) ||
7772 		    (devp->sd_inq->inq_ansi == 5)) &&
7773 		    devp->sd_inq->inq_bque) || devp->sd_inq->inq_cmdque;
7774 
7775 		/*
7776 		 * If tagged queueing is supported by the target
7777 		 * and by the host adapter then we will enable it
7778 		 */
7779 		un->un_tagflags = 0;
7780 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) && tq_trigger_flag &&
7781 		    (un->un_f_arq_enabled == TRUE)) {
7782 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7783 			    1, 1) == 1) {
7784 				un->un_tagflags = FLAG_STAG;
7785 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7786 				    "sd_unit_attach: un:0x%p tag queueing "
7787 				    "enabled\n", un);
7788 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
7789 			    "untagged-qing", 0) == 1) {
7790 				un->un_f_opt_queueing = TRUE;
7791 				un->un_saved_throttle = un->un_throttle =
7792 				    min(un->un_throttle, 3);
7793 			} else {
7794 				un->un_f_opt_queueing = FALSE;
7795 				un->un_saved_throttle = un->un_throttle = 1;
7796 			}
7797 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
7798 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
7799 			/* The Host Adapter supports internal queueing. */
7800 			un->un_f_opt_queueing = TRUE;
7801 			un->un_saved_throttle = un->un_throttle =
7802 			    min(un->un_throttle, 3);
7803 		} else {
7804 			un->un_f_opt_queueing = FALSE;
7805 			un->un_saved_throttle = un->un_throttle = 1;
7806 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7807 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
7808 		}
7809 
7810 		/*
7811 		 * Enable large transfers for SATA/SAS drives
7812 		 */
7813 		if (SD_IS_SERIAL(un)) {
7814 			un->un_max_xfer_size =
7815 			    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7816 			    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7817 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7818 			    "sd_unit_attach: un:0x%p max transfer "
7819 			    "size=0x%x\n", un, un->un_max_xfer_size);
7820 
7821 		}
7822 
7823 		/* Setup or tear down default wide operations for disks */
7824 
7825 		/*
7826 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
7827 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
7828 		 * system and be set to different values. In the future this
7829 		 * code may need to be updated when the ssd module is
7830 		 * obsoleted and removed from the system. (4299588)
7831 		 */
7832 		if (SD_IS_PARALLEL_SCSI(un) &&
7833 		    (devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7834 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
7835 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7836 			    1, 1) == 1) {
7837 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7838 				    "sd_unit_attach: un:0x%p Wide Transfer "
7839 				    "enabled\n", un);
7840 			}
7841 
7842 			/*
7843 			 * If tagged queuing has also been enabled, then
7844 			 * enable large xfers
7845 			 */
7846 			if (un->un_saved_throttle == sd_max_throttle) {
7847 				un->un_max_xfer_size =
7848 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
7849 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
7850 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7851 				    "sd_unit_attach: un:0x%p max transfer "
7852 				    "size=0x%x\n", un, un->un_max_xfer_size);
7853 			}
7854 		} else {
7855 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
7856 			    0, 1) == 1) {
7857 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
7858 				    "sd_unit_attach: un:0x%p "
7859 				    "Wide Transfer disabled\n", un);
7860 			}
7861 		}
7862 	} else {
7863 		un->un_tagflags = FLAG_STAG;
7864 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
7865 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
7866 	}
7867 
7868 	/*
7869 	 * If this target supports LUN reset, try to enable it.
7870 	 */
7871 	if (un->un_f_lun_reset_enabled) {
7872 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
7873 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7874 			    "un:0x%p lun_reset capability set\n", un);
7875 		} else {
7876 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7877 			    "un:0x%p lun-reset capability not set\n", un);
7878 		}
7879 	}
7880 
7881 	/*
7882 	 * Adjust the maximum transfer size. This is to fix
7883 	 * the problem of partial DMA support on SPARC. Some
7884 	 * HBA driver, like aac, has very small dma_attr_maxxfer
7885 	 * size, which requires partial DMA support on SPARC.
7886 	 * In the future the SPARC pci nexus driver may solve
7887 	 * the problem instead of this fix.
7888 	 */
7889 	max_xfer_size = scsi_ifgetcap(SD_ADDRESS(un), "dma-max", 1);
7890 	if ((max_xfer_size > 0) && (max_xfer_size < un->un_max_xfer_size)) {
7891 		/* We need DMA partial even on sparc to ensure sddump() works */
7892 		un->un_max_xfer_size = max_xfer_size;
7893 		if (un->un_partial_dma_supported == 0)
7894 			un->un_partial_dma_supported = 1;
7895 	}
7896 	if (ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7897 	    DDI_PROP_DONTPASS, "buf_break", 0) == 1) {
7898 		if (ddi_xbuf_attr_setup_brk(un->un_xbuf_attr,
7899 		    un->un_max_xfer_size) == 1) {
7900 			un->un_buf_breakup_supported = 1;
7901 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
7902 			    "un:0x%p Buf breakup enabled\n", un);
7903 		}
7904 	}
7905 
7906 	/*
7907 	 * Set PKT_DMA_PARTIAL flag.
7908 	 */
7909 	if (un->un_partial_dma_supported == 1) {
7910 		un->un_pkt_flags = PKT_DMA_PARTIAL;
7911 	} else {
7912 		un->un_pkt_flags = 0;
7913 	}
7914 
7915 	/* Initialize sd_ssc_t for internal uscsi commands */
7916 	ssc = sd_ssc_init(un);
7917 	scsi_fm_init(devp);
7918 
7919 	/*
7920 	 * Allocate memory for SCSI FMA stuffs.
7921 	 */
7922 	un->un_fm_private =
7923 	    kmem_zalloc(sizeof (struct sd_fm_internal), KM_SLEEP);
7924 	sfip = (struct sd_fm_internal *)un->un_fm_private;
7925 	sfip->fm_ssc.ssc_uscsi_cmd = &sfip->fm_ucmd;
7926 	sfip->fm_ssc.ssc_uscsi_info = &sfip->fm_uinfo;
7927 	sfip->fm_ssc.ssc_un = un;
7928 
7929 	if (ISCD(un) ||
7930 	    un->un_f_has_removable_media ||
7931 	    devp->sd_fm_capable == DDI_FM_NOT_CAPABLE) {
7932 		/*
7933 		 * We don't touch CDROM or the DDI_FM_NOT_CAPABLE device.
7934 		 * Their log are unchanged.
7935 		 */
7936 		sfip->fm_log_level = SD_FM_LOG_NSUP;
7937 	} else {
7938 		/*
7939 		 * If enter here, it should be non-CDROM and FM-capable
7940 		 * device, and it will not keep the old scsi_log as before
7941 		 * in /var/adm/messages. However, the property
7942 		 * "fm-scsi-log" will control whether the FM telemetry will
7943 		 * be logged in /var/adm/messages.
7944 		 */
7945 		int fm_scsi_log;
7946 		fm_scsi_log = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
7947 		    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, "fm-scsi-log", 0);
7948 
7949 		if (fm_scsi_log)
7950 			sfip->fm_log_level = SD_FM_LOG_EREPORT;
7951 		else
7952 			sfip->fm_log_level = SD_FM_LOG_SILENT;
7953 	}
7954 
7955 	/*
7956 	 * At this point in the attach, we have enough info in the
7957 	 * soft state to be able to issue commands to the target.
7958 	 *
7959 	 * All command paths used below MUST issue their commands as
7960 	 * SD_PATH_DIRECT. This is important as intermediate layers
7961 	 * are not all initialized yet (such as PM).
7962 	 */
7963 
7964 	/*
7965 	 * Send a TEST UNIT READY command to the device. This should clear
7966 	 * any outstanding UNIT ATTENTION that may be present.
7967 	 *
7968 	 * Note: Don't check for success, just track if there is a reservation,
7969 	 * this is a throw away command to clear any unit attentions.
7970 	 *
7971 	 * Note: This MUST be the first command issued to the target during
7972 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
7973 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
7974 	 * with attempts at spinning up a device with no media.
7975 	 */
7976 	status = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
7977 	if (status != 0) {
7978 		if (status == EACCES)
7979 			reservation_flag = SD_TARGET_IS_RESERVED;
7980 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
7981 	}
7982 
7983 	/*
7984 	 * If the device is NOT a removable media device, attempt to spin
7985 	 * it up (using the START_STOP_UNIT command) and read its capacity
7986 	 * (using the READ CAPACITY command).  Note, however, that either
7987 	 * of these could fail and in some cases we would continue with
7988 	 * the attach despite the failure (see below).
7989 	 */
7990 	if (un->un_f_descr_format_supported) {
7991 
7992 		switch (sd_spin_up_unit(ssc)) {
7993 		case 0:
7994 			/*
7995 			 * Spin-up was successful; now try to read the
7996 			 * capacity.  If successful then save the results
7997 			 * and mark the capacity & lbasize as valid.
7998 			 */
7999 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8000 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8001 
8002 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
8003 			    &lbasize, SD_PATH_DIRECT);
8004 
8005 			switch (status) {
8006 			case 0: {
8007 				if (capacity > DK_MAX_BLOCKS) {
8008 #ifdef _LP64
8009 					if ((capacity + 1) >
8010 					    SD_GROUP1_MAX_ADDRESS) {
8011 						/*
8012 						 * Enable descriptor format
8013 						 * sense data so that we can
8014 						 * get 64 bit sense data
8015 						 * fields.
8016 						 */
8017 						sd_enable_descr_sense(ssc);
8018 					}
8019 #else
8020 					/* 32-bit kernels can't handle this */
8021 					scsi_log(SD_DEVINFO(un),
8022 					    sd_label, CE_WARN,
8023 					    "disk has %llu blocks, which "
8024 					    "is too large for a 32-bit "
8025 					    "kernel", capacity);
8026 
8027 #if defined(__i386) || defined(__amd64)
8028 					/*
8029 					 * 1TB disk was treated as (1T - 512)B
8030 					 * in the past, so that it might have
8031 					 * valid VTOC and solaris partitions,
8032 					 * we have to allow it to continue to
8033 					 * work.
8034 					 */
8035 					if (capacity -1 > DK_MAX_BLOCKS)
8036 #endif
8037 					goto spinup_failed;
8038 #endif
8039 				}
8040 
8041 				/*
8042 				 * Here it's not necessary to check the case:
8043 				 * the capacity of the device is bigger than
8044 				 * what the max hba cdb can support. Because
8045 				 * sd_send_scsi_READ_CAPACITY will retrieve
8046 				 * the capacity by sending USCSI command, which
8047 				 * is constrained by the max hba cdb. Actually,
8048 				 * sd_send_scsi_READ_CAPACITY will return
8049 				 * EINVAL when using bigger cdb than required
8050 				 * cdb length. Will handle this case in
8051 				 * "case EINVAL".
8052 				 */
8053 
8054 				/*
8055 				 * The following relies on
8056 				 * sd_send_scsi_READ_CAPACITY never
8057 				 * returning 0 for capacity and/or lbasize.
8058 				 */
8059 				sd_update_block_info(un, lbasize, capacity);
8060 
8061 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8062 				    "sd_unit_attach: un:0x%p capacity = %ld "
8063 				    "blocks; lbasize= %ld.\n", un,
8064 				    un->un_blockcount, un->un_tgt_blocksize);
8065 
8066 				break;
8067 			}
8068 			case EINVAL:
8069 				/*
8070 				 * In the case where the max-cdb-length property
8071 				 * is smaller than the required CDB length for
8072 				 * a SCSI device, a target driver can fail to
8073 				 * attach to that device.
8074 				 */
8075 				scsi_log(SD_DEVINFO(un),
8076 				    sd_label, CE_WARN,
8077 				    "disk capacity is too large "
8078 				    "for current cdb length");
8079 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8080 
8081 				goto spinup_failed;
8082 			case EACCES:
8083 				/*
8084 				 * Should never get here if the spin-up
8085 				 * succeeded, but code it in anyway.
8086 				 * From here, just continue with the attach...
8087 				 */
8088 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8089 				    "sd_unit_attach: un:0x%p "
8090 				    "sd_send_scsi_READ_CAPACITY "
8091 				    "returned reservation conflict\n", un);
8092 				reservation_flag = SD_TARGET_IS_RESERVED;
8093 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8094 				break;
8095 			default:
8096 				/*
8097 				 * Likewise, should never get here if the
8098 				 * spin-up succeeded. Just continue with
8099 				 * the attach...
8100 				 */
8101 				if (status == EIO)
8102 					sd_ssc_assessment(ssc,
8103 					    SD_FMT_STATUS_CHECK);
8104 				else
8105 					sd_ssc_assessment(ssc,
8106 					    SD_FMT_IGNORE);
8107 				break;
8108 			}
8109 			break;
8110 		case EACCES:
8111 			/*
8112 			 * Device is reserved by another host.  In this case
8113 			 * we could not spin it up or read the capacity, but
8114 			 * we continue with the attach anyway.
8115 			 */
8116 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8117 			    "sd_unit_attach: un:0x%p spin-up reservation "
8118 			    "conflict.\n", un);
8119 			reservation_flag = SD_TARGET_IS_RESERVED;
8120 			break;
8121 		default:
8122 			/* Fail the attach if the spin-up failed. */
8123 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8124 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8125 			goto spinup_failed;
8126 		}
8127 
8128 	}
8129 
8130 	/*
8131 	 * Check to see if this is a MMC drive
8132 	 */
8133 	if (ISCD(un)) {
8134 		sd_set_mmc_caps(ssc);
8135 	}
8136 
8137 	/*
8138 	 * Add a zero-length attribute to tell the world we support
8139 	 * kernel ioctls (for layered drivers)
8140 	 */
8141 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8142 	    DDI_KERNEL_IOCTL, NULL, 0);
8143 
8144 	/*
8145 	 * Add a boolean property to tell the world we support
8146 	 * the B_FAILFAST flag (for layered drivers)
8147 	 */
8148 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8149 	    "ddi-failfast-supported", NULL, 0);
8150 
8151 	/*
8152 	 * Initialize power management
8153 	 */
8154 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8155 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8156 	sd_setup_pm(ssc, devi);
8157 	if (un->un_f_pm_is_enabled == FALSE) {
8158 		/*
8159 		 * For performance, point to a jump table that does
8160 		 * not include pm.
8161 		 * The direct and priority chains don't change with PM.
8162 		 *
8163 		 * Note: this is currently done based on individual device
8164 		 * capabilities. When an interface for determining system
8165 		 * power enabled state becomes available, or when additional
8166 		 * layers are added to the command chain, these values will
8167 		 * have to be re-evaluated for correctness.
8168 		 */
8169 		if (un->un_f_non_devbsize_supported) {
8170 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8171 		} else {
8172 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8173 		}
8174 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8175 	}
8176 
8177 	/*
8178 	 * This property is set to 0 by HA software to avoid retries
8179 	 * on a reserved disk. (The preferred property name is
8180 	 * "retry-on-reservation-conflict") (1189689)
8181 	 *
8182 	 * Note: The use of a global here can have unintended consequences. A
8183 	 * per instance variable is preferable to match the capabilities of
8184 	 * different underlying hba's (4402600)
8185 	 */
8186 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8187 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8188 	    sd_retry_on_reservation_conflict);
8189 	if (sd_retry_on_reservation_conflict != 0) {
8190 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8191 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8192 		    sd_retry_on_reservation_conflict);
8193 	}
8194 
8195 	/* Set up options for QFULL handling. */
8196 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8197 	    "qfull-retries", -1)) != -1) {
8198 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8199 		    rval, 1);
8200 	}
8201 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8202 	    "qfull-retry-interval", -1)) != -1) {
8203 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8204 		    rval, 1);
8205 	}
8206 
8207 	/*
8208 	 * This just prints a message that announces the existence of the
8209 	 * device. The message is always printed in the system logfile, but
8210 	 * only appears on the console if the system is booted with the
8211 	 * -v (verbose) argument.
8212 	 */
8213 	ddi_report_dev(devi);
8214 
8215 	un->un_mediastate = DKIO_NONE;
8216 
8217 	/*
8218 	 * Check if this is a SSD(Solid State Drive).
8219 	 */
8220 	sd_check_solid_state(ssc);
8221 
8222 	/*
8223 	 * Check whether the drive is in emulation mode.
8224 	 */
8225 	sd_check_emulation_mode(ssc);
8226 
8227 	cmlb_alloc_handle(&un->un_cmlbhandle);
8228 
8229 #if defined(__i386) || defined(__amd64)
8230 	/*
8231 	 * On x86, compensate for off-by-1 legacy error
8232 	 */
8233 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
8234 	    (lbasize == un->un_sys_blocksize))
8235 		offbyone = CMLB_OFF_BY_ONE;
8236 #endif
8237 
8238 	if (cmlb_attach(devi, &sd_tgops, (int)devp->sd_inq->inq_dtype,
8239 	    VOID2BOOLEAN(un->un_f_has_removable_media != 0),
8240 	    VOID2BOOLEAN(un->un_f_is_hotpluggable != 0),
8241 	    un->un_node_type, offbyone, un->un_cmlbhandle,
8242 	    (void *)SD_PATH_DIRECT) != 0) {
8243 		goto cmlb_attach_failed;
8244 	}
8245 
8246 
8247 	/*
8248 	 * Read and validate the device's geometry (ie, disk label)
8249 	 * A new unformatted drive will not have a valid geometry, but
8250 	 * the driver needs to successfully attach to this device so
8251 	 * the drive can be formatted via ioctls.
8252 	 */
8253 	geom_label_valid = (cmlb_validate(un->un_cmlbhandle, 0,
8254 	    (void *)SD_PATH_DIRECT) == 0) ? 1: 0;
8255 
8256 	mutex_enter(SD_MUTEX(un));
8257 
8258 	/*
8259 	 * Read and initialize the devid for the unit.
8260 	 */
8261 	if (un->un_f_devid_supported) {
8262 		sd_register_devid(ssc, devi, reservation_flag);
8263 	}
8264 	mutex_exit(SD_MUTEX(un));
8265 
8266 #if (defined(__fibre))
8267 	/*
8268 	 * Register callbacks for fibre only.  You can't do this solely
8269 	 * on the basis of the devid_type because this is hba specific.
8270 	 * We need to query our hba capabilities to find out whether to
8271 	 * register or not.
8272 	 */
8273 	if (un->un_f_is_fibre) {
8274 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8275 			sd_init_event_callbacks(un);
8276 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8277 			    "sd_unit_attach: un:0x%p event callbacks inserted",
8278 			    un);
8279 		}
8280 	}
8281 #endif
8282 
8283 	if (un->un_f_opt_disable_cache == TRUE) {
8284 		/*
8285 		 * Disable both read cache and write cache.  This is
8286 		 * the historic behavior of the keywords in the config file.
8287 		 */
8288 		if (sd_cache_control(ssc, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8289 		    0) {
8290 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8291 			    "sd_unit_attach: un:0x%p Could not disable "
8292 			    "caching", un);
8293 			goto devid_failed;
8294 		}
8295 	}
8296 
8297 	/*
8298 	 * Check the value of the WCE bit now and
8299 	 * set un_f_write_cache_enabled accordingly.
8300 	 */
8301 	(void) sd_get_write_cache_enabled(ssc, &wc_enabled);
8302 	mutex_enter(SD_MUTEX(un));
8303 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8304 	mutex_exit(SD_MUTEX(un));
8305 
8306 	if ((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR &&
8307 	    un->un_tgt_blocksize != DEV_BSIZE) ||
8308 	    un->un_f_enable_rmw) {
8309 		if (!(un->un_wm_cache)) {
8310 			(void) snprintf(name_str, sizeof (name_str),
8311 			    "%s%d_cache",
8312 			    ddi_driver_name(SD_DEVINFO(un)),
8313 			    ddi_get_instance(SD_DEVINFO(un)));
8314 			un->un_wm_cache = kmem_cache_create(
8315 			    name_str, sizeof (struct sd_w_map),
8316 			    8, sd_wm_cache_constructor,
8317 			    sd_wm_cache_destructor, NULL,
8318 			    (void *)un, NULL, 0);
8319 			if (!(un->un_wm_cache)) {
8320 				goto wm_cache_failed;
8321 			}
8322 		}
8323 	}
8324 
8325 	/*
8326 	 * Check the value of the NV_SUP bit and set
8327 	 * un_f_suppress_cache_flush accordingly.
8328 	 */
8329 	sd_get_nv_sup(ssc);
8330 
8331 	/*
8332 	 * Find out what type of reservation this disk supports.
8333 	 */
8334 	status = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS, 0, NULL);
8335 
8336 	switch (status) {
8337 	case 0:
8338 		/*
8339 		 * SCSI-3 reservations are supported.
8340 		 */
8341 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8342 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8343 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8344 		break;
8345 	case ENOTSUP:
8346 		/*
8347 		 * The PERSISTENT RESERVE IN command would not be recognized by
8348 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8349 		 */
8350 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8351 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8352 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8353 
8354 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8355 		break;
8356 	default:
8357 		/*
8358 		 * default to SCSI-3 reservations
8359 		 */
8360 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8361 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8362 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8363 
8364 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
8365 		break;
8366 	}
8367 
8368 	/*
8369 	 * Set the pstat and error stat values here, so data obtained during the
8370 	 * previous attach-time routines is available.
8371 	 *
8372 	 * Note: This is a critical sequence that needs to be maintained:
8373 	 *	1) Instantiate the kstats before any routines using the iopath
8374 	 *	   (i.e. sd_send_scsi_cmd).
8375 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8376 	 *	   stats (sd_set_pstats)here, following
8377 	 *	   cmlb_validate_geometry(), sd_register_devid(), and
8378 	 *	   sd_cache_control().
8379 	 */
8380 
8381 	if (un->un_f_pkstats_enabled && geom_label_valid) {
8382 		sd_set_pstats(un);
8383 		SD_TRACE(SD_LOG_IO_PARTITION, un,
8384 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8385 	}
8386 
8387 	sd_set_errstats(un);
8388 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8389 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8390 
8391 
8392 	/*
8393 	 * After successfully attaching an instance, we record the information
8394 	 * of how many luns have been attached on the relative target and
8395 	 * controller for parallel SCSI. This information is used when sd tries
8396 	 * to set the tagged queuing capability in HBA.
8397 	 */
8398 	if (SD_IS_PARALLEL_SCSI(un) && (tgt >= 0) && (tgt < NTARGETS_WIDE)) {
8399 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_ATTACH);
8400 	}
8401 
8402 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8403 	    "sd_unit_attach: un:0x%p exit success\n", un);
8404 
8405 	/* Uninitialize sd_ssc_t pointer */
8406 	sd_ssc_fini(ssc);
8407 
8408 	return (DDI_SUCCESS);
8409 
8410 	/*
8411 	 * An error occurred during the attach; clean up & return failure.
8412 	 */
8413 wm_cache_failed:
8414 devid_failed:
8415 
8416 setup_pm_failed:
8417 	ddi_remove_minor_node(devi, NULL);
8418 
8419 cmlb_attach_failed:
8420 	/*
8421 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8422 	 */
8423 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8424 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8425 
8426 	/*
8427 	 * Refer to the comments of setting tagged-qing in the beginning of
8428 	 * sd_unit_attach. We can only disable tagged queuing when there is
8429 	 * no lun attached on the target.
8430 	 */
8431 	if (sd_scsi_get_target_lun_count(pdip, tgt) < 1) {
8432 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8433 	}
8434 
8435 	if (un->un_f_is_fibre == FALSE) {
8436 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8437 	}
8438 
8439 spinup_failed:
8440 
8441 	/* Uninitialize sd_ssc_t pointer */
8442 	sd_ssc_fini(ssc);
8443 
8444 	mutex_enter(SD_MUTEX(un));
8445 
8446 	/* Deallocate SCSI FMA memory spaces */
8447 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8448 
8449 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8450 	if (un->un_direct_priority_timeid != NULL) {
8451 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8452 		un->un_direct_priority_timeid = NULL;
8453 		mutex_exit(SD_MUTEX(un));
8454 		(void) untimeout(temp_id);
8455 		mutex_enter(SD_MUTEX(un));
8456 	}
8457 
8458 	/* Cancel any pending start/stop timeouts */
8459 	if (un->un_startstop_timeid != NULL) {
8460 		timeout_id_t temp_id = un->un_startstop_timeid;
8461 		un->un_startstop_timeid = NULL;
8462 		mutex_exit(SD_MUTEX(un));
8463 		(void) untimeout(temp_id);
8464 		mutex_enter(SD_MUTEX(un));
8465 	}
8466 
8467 	/* Cancel any pending reset-throttle timeouts */
8468 	if (un->un_reset_throttle_timeid != NULL) {
8469 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8470 		un->un_reset_throttle_timeid = NULL;
8471 		mutex_exit(SD_MUTEX(un));
8472 		(void) untimeout(temp_id);
8473 		mutex_enter(SD_MUTEX(un));
8474 	}
8475 
8476 	/* Cancel rmw warning message timeouts */
8477 	if (un->un_rmw_msg_timeid != NULL) {
8478 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8479 		un->un_rmw_msg_timeid = NULL;
8480 		mutex_exit(SD_MUTEX(un));
8481 		(void) untimeout(temp_id);
8482 		mutex_enter(SD_MUTEX(un));
8483 	}
8484 
8485 	/* Cancel any pending retry timeouts */
8486 	if (un->un_retry_timeid != NULL) {
8487 		timeout_id_t temp_id = un->un_retry_timeid;
8488 		un->un_retry_timeid = NULL;
8489 		mutex_exit(SD_MUTEX(un));
8490 		(void) untimeout(temp_id);
8491 		mutex_enter(SD_MUTEX(un));
8492 	}
8493 
8494 	/* Cancel any pending delayed cv broadcast timeouts */
8495 	if (un->un_dcvb_timeid != NULL) {
8496 		timeout_id_t temp_id = un->un_dcvb_timeid;
8497 		un->un_dcvb_timeid = NULL;
8498 		mutex_exit(SD_MUTEX(un));
8499 		(void) untimeout(temp_id);
8500 		mutex_enter(SD_MUTEX(un));
8501 	}
8502 
8503 	mutex_exit(SD_MUTEX(un));
8504 
8505 	/* There should not be any in-progress I/O so ASSERT this check */
8506 	ASSERT(un->un_ncmds_in_transport == 0);
8507 	ASSERT(un->un_ncmds_in_driver == 0);
8508 
8509 	/* Do not free the softstate if the callback routine is active */
8510 	sd_sync_with_callback(un);
8511 
8512 	/*
8513 	 * Partition stats apparently are not used with removables. These would
8514 	 * not have been created during attach, so no need to clean them up...
8515 	 */
8516 	if (un->un_errstats != NULL) {
8517 		kstat_delete(un->un_errstats);
8518 		un->un_errstats = NULL;
8519 	}
8520 
8521 create_errstats_failed:
8522 
8523 	if (un->un_stats != NULL) {
8524 		kstat_delete(un->un_stats);
8525 		un->un_stats = NULL;
8526 	}
8527 
8528 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8529 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8530 
8531 	ddi_prop_remove_all(devi);
8532 	sema_destroy(&un->un_semoclose);
8533 	cv_destroy(&un->un_state_cv);
8534 
8535 getrbuf_failed:
8536 
8537 	sd_free_rqs(un);
8538 
8539 alloc_rqs_failed:
8540 
8541 	devp->sd_private = NULL;
8542 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8543 
8544 get_softstate_failed:
8545 	/*
8546 	 * Note: the man pages are unclear as to whether or not doing a
8547 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8548 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8549 	 * ddi_get_soft_state() fails.  The implication seems to be
8550 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8551 	 */
8552 #ifndef XPV_HVM_DRIVER
8553 	ddi_soft_state_free(sd_state, instance);
8554 #endif /* !XPV_HVM_DRIVER */
8555 
8556 probe_failed:
8557 	scsi_unprobe(devp);
8558 
8559 	return (DDI_FAILURE);
8560 }
8561 
8562 
8563 /*
8564  *    Function: sd_unit_detach
8565  *
8566  * Description: Performs DDI_DETACH processing for sddetach().
8567  *
8568  * Return Code: DDI_SUCCESS
8569  *		DDI_FAILURE
8570  *
8571  *     Context: Kernel thread context
8572  */
8573 
8574 static int
8575 sd_unit_detach(dev_info_t *devi)
8576 {
8577 	struct scsi_device	*devp;
8578 	struct sd_lun		*un;
8579 	int			i;
8580 	int			tgt;
8581 	dev_t			dev;
8582 	dev_info_t		*pdip = ddi_get_parent(devi);
8583 #ifndef XPV_HVM_DRIVER
8584 	int			instance = ddi_get_instance(devi);
8585 #endif /* !XPV_HVM_DRIVER */
8586 
8587 	mutex_enter(&sd_detach_mutex);
8588 
8589 	/*
8590 	 * Fail the detach for any of the following:
8591 	 *  - Unable to get the sd_lun struct for the instance
8592 	 *  - A layered driver has an outstanding open on the instance
8593 	 *  - Another thread is already detaching this instance
8594 	 *  - Another thread is currently performing an open
8595 	 */
8596 	devp = ddi_get_driver_private(devi);
8597 	if ((devp == NULL) ||
8598 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8599 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8600 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8601 		mutex_exit(&sd_detach_mutex);
8602 		return (DDI_FAILURE);
8603 	}
8604 
8605 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8606 
8607 	/*
8608 	 * Mark this instance as currently in a detach, to inhibit any
8609 	 * opens from a layered driver.
8610 	 */
8611 	un->un_detach_count++;
8612 	mutex_exit(&sd_detach_mutex);
8613 
8614 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
8615 	    SCSI_ADDR_PROP_TARGET, -1);
8616 
8617 	dev = sd_make_device(SD_DEVINFO(un));
8618 
8619 #ifndef lint
8620 	_NOTE(COMPETING_THREADS_NOW);
8621 #endif
8622 
8623 	mutex_enter(SD_MUTEX(un));
8624 
8625 	/*
8626 	 * Fail the detach if there are any outstanding layered
8627 	 * opens on this device.
8628 	 */
8629 	for (i = 0; i < NDKMAP; i++) {
8630 		if (un->un_ocmap.lyropen[i] != 0) {
8631 			goto err_notclosed;
8632 		}
8633 	}
8634 
8635 	/*
8636 	 * Verify there are NO outstanding commands issued to this device.
8637 	 * ie, un_ncmds_in_transport == 0.
8638 	 * It's possible to have outstanding commands through the physio
8639 	 * code path, even though everything's closed.
8640 	 */
8641 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8642 	    (un->un_direct_priority_timeid != NULL) ||
8643 	    (un->un_state == SD_STATE_RWAIT)) {
8644 		mutex_exit(SD_MUTEX(un));
8645 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8646 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8647 		goto err_stillbusy;
8648 	}
8649 
8650 	/*
8651 	 * If we have the device reserved, release the reservation.
8652 	 */
8653 	if ((un->un_resvd_status & SD_RESERVE) &&
8654 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8655 		mutex_exit(SD_MUTEX(un));
8656 		/*
8657 		 * Note: sd_reserve_release sends a command to the device
8658 		 * via the sd_ioctlcmd() path, and can sleep.
8659 		 */
8660 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8661 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8662 			    "sd_dr_detach: Cannot release reservation \n");
8663 		}
8664 	} else {
8665 		mutex_exit(SD_MUTEX(un));
8666 	}
8667 
8668 	/*
8669 	 * Untimeout any reserve recover, throttle reset, restart unit
8670 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8671 	 * from getting nulled by their callback functions.
8672 	 */
8673 	mutex_enter(SD_MUTEX(un));
8674 	if (un->un_resvd_timeid != NULL) {
8675 		timeout_id_t temp_id = un->un_resvd_timeid;
8676 		un->un_resvd_timeid = NULL;
8677 		mutex_exit(SD_MUTEX(un));
8678 		(void) untimeout(temp_id);
8679 		mutex_enter(SD_MUTEX(un));
8680 	}
8681 
8682 	if (un->un_reset_throttle_timeid != NULL) {
8683 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8684 		un->un_reset_throttle_timeid = NULL;
8685 		mutex_exit(SD_MUTEX(un));
8686 		(void) untimeout(temp_id);
8687 		mutex_enter(SD_MUTEX(un));
8688 	}
8689 
8690 	if (un->un_startstop_timeid != NULL) {
8691 		timeout_id_t temp_id = un->un_startstop_timeid;
8692 		un->un_startstop_timeid = NULL;
8693 		mutex_exit(SD_MUTEX(un));
8694 		(void) untimeout(temp_id);
8695 		mutex_enter(SD_MUTEX(un));
8696 	}
8697 
8698 	if (un->un_rmw_msg_timeid != NULL) {
8699 		timeout_id_t temp_id = un->un_rmw_msg_timeid;
8700 		un->un_rmw_msg_timeid = NULL;
8701 		mutex_exit(SD_MUTEX(un));
8702 		(void) untimeout(temp_id);
8703 		mutex_enter(SD_MUTEX(un));
8704 	}
8705 
8706 	if (un->un_dcvb_timeid != NULL) {
8707 		timeout_id_t temp_id = un->un_dcvb_timeid;
8708 		un->un_dcvb_timeid = NULL;
8709 		mutex_exit(SD_MUTEX(un));
8710 		(void) untimeout(temp_id);
8711 	} else {
8712 		mutex_exit(SD_MUTEX(un));
8713 	}
8714 
8715 	/* Remove any pending reservation reclaim requests for this device */
8716 	sd_rmv_resv_reclaim_req(dev);
8717 
8718 	mutex_enter(SD_MUTEX(un));
8719 
8720 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8721 	if (un->un_direct_priority_timeid != NULL) {
8722 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8723 		un->un_direct_priority_timeid = NULL;
8724 		mutex_exit(SD_MUTEX(un));
8725 		(void) untimeout(temp_id);
8726 		mutex_enter(SD_MUTEX(un));
8727 	}
8728 
8729 	/* Cancel any active multi-host disk watch thread requests */
8730 	if (un->un_mhd_token != NULL) {
8731 		mutex_exit(SD_MUTEX(un));
8732 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8733 		if (scsi_watch_request_terminate(un->un_mhd_token,
8734 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8735 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8736 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8737 			/*
8738 			 * Note: We are returning here after having removed
8739 			 * some driver timeouts above. This is consistent with
8740 			 * the legacy implementation but perhaps the watch
8741 			 * terminate call should be made with the wait flag set.
8742 			 */
8743 			goto err_stillbusy;
8744 		}
8745 		mutex_enter(SD_MUTEX(un));
8746 		un->un_mhd_token = NULL;
8747 	}
8748 
8749 	if (un->un_swr_token != NULL) {
8750 		mutex_exit(SD_MUTEX(un));
8751 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8752 		if (scsi_watch_request_terminate(un->un_swr_token,
8753 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8754 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8755 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8756 			/*
8757 			 * Note: We are returning here after having removed
8758 			 * some driver timeouts above. This is consistent with
8759 			 * the legacy implementation but perhaps the watch
8760 			 * terminate call should be made with the wait flag set.
8761 			 */
8762 			goto err_stillbusy;
8763 		}
8764 		mutex_enter(SD_MUTEX(un));
8765 		un->un_swr_token = NULL;
8766 	}
8767 
8768 	mutex_exit(SD_MUTEX(un));
8769 
8770 	/*
8771 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8772 	 * if we have not registered one.
8773 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8774 	 */
8775 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8776 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8777 
8778 	/*
8779 	 * protect the timeout pointers from getting nulled by
8780 	 * their callback functions during the cancellation process.
8781 	 * In such a scenario untimeout can be invoked with a null value.
8782 	 */
8783 	_NOTE(NO_COMPETING_THREADS_NOW);
8784 
8785 	mutex_enter(&un->un_pm_mutex);
8786 	if (un->un_pm_idle_timeid != NULL) {
8787 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8788 		un->un_pm_idle_timeid = NULL;
8789 		mutex_exit(&un->un_pm_mutex);
8790 
8791 		/*
8792 		 * Timeout is active; cancel it.
8793 		 * Note that it'll never be active on a device
8794 		 * that does not support PM therefore we don't
8795 		 * have to check before calling pm_idle_component.
8796 		 */
8797 		(void) untimeout(temp_id);
8798 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8799 		mutex_enter(&un->un_pm_mutex);
8800 	}
8801 
8802 	/*
8803 	 * Check whether there is already a timeout scheduled for power
8804 	 * management. If yes then don't lower the power here, that's.
8805 	 * the timeout handler's job.
8806 	 */
8807 	if (un->un_pm_timeid != NULL) {
8808 		timeout_id_t temp_id = un->un_pm_timeid;
8809 		un->un_pm_timeid = NULL;
8810 		mutex_exit(&un->un_pm_mutex);
8811 		/*
8812 		 * Timeout is active; cancel it.
8813 		 * Note that it'll never be active on a device
8814 		 * that does not support PM therefore we don't
8815 		 * have to check before calling pm_idle_component.
8816 		 */
8817 		(void) untimeout(temp_id);
8818 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8819 
8820 	} else {
8821 		mutex_exit(&un->un_pm_mutex);
8822 		if ((un->un_f_pm_is_enabled == TRUE) &&
8823 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_PM_STATE_STOPPED(un))
8824 		    != DDI_SUCCESS)) {
8825 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8826 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8827 			/*
8828 			 * Fix for bug: 4297749, item # 13
8829 			 * The above test now includes a check to see if PM is
8830 			 * supported by this device before call
8831 			 * pm_lower_power().
8832 			 * Note, the following is not dead code. The call to
8833 			 * pm_lower_power above will generate a call back into
8834 			 * our sdpower routine which might result in a timeout
8835 			 * handler getting activated. Therefore the following
8836 			 * code is valid and necessary.
8837 			 */
8838 			mutex_enter(&un->un_pm_mutex);
8839 			if (un->un_pm_timeid != NULL) {
8840 				timeout_id_t temp_id = un->un_pm_timeid;
8841 				un->un_pm_timeid = NULL;
8842 				mutex_exit(&un->un_pm_mutex);
8843 				(void) untimeout(temp_id);
8844 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8845 			} else {
8846 				mutex_exit(&un->un_pm_mutex);
8847 			}
8848 		}
8849 	}
8850 
8851 	/*
8852 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8853 	 * Relocated here from above to be after the call to
8854 	 * pm_lower_power, which was getting errors.
8855 	 */
8856 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8857 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8858 
8859 	/*
8860 	 * Currently, tagged queuing is supported per target based by HBA.
8861 	 * Setting this per lun instance actually sets the capability of this
8862 	 * target in HBA, which affects those luns already attached on the
8863 	 * same target. So during detach, we can only disable this capability
8864 	 * only when this is the only lun left on this target. By doing
8865 	 * this, we assume a target has the same tagged queuing capability
8866 	 * for every lun. The condition can be removed when HBA is changed to
8867 	 * support per lun based tagged queuing capability.
8868 	 */
8869 	if (sd_scsi_get_target_lun_count(pdip, tgt) <= 1) {
8870 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8871 	}
8872 
8873 	if (un->un_f_is_fibre == FALSE) {
8874 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8875 	}
8876 
8877 	/*
8878 	 * Remove any event callbacks, fibre only
8879 	 */
8880 	if (un->un_f_is_fibre == TRUE) {
8881 		if ((un->un_insert_event != NULL) &&
8882 		    (ddi_remove_event_handler(un->un_insert_cb_id) !=
8883 		    DDI_SUCCESS)) {
8884 			/*
8885 			 * Note: We are returning here after having done
8886 			 * substantial cleanup above. This is consistent
8887 			 * with the legacy implementation but this may not
8888 			 * be the right thing to do.
8889 			 */
8890 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8891 			    "sd_dr_detach: Cannot cancel insert event\n");
8892 			goto err_remove_event;
8893 		}
8894 		un->un_insert_event = NULL;
8895 
8896 		if ((un->un_remove_event != NULL) &&
8897 		    (ddi_remove_event_handler(un->un_remove_cb_id) !=
8898 		    DDI_SUCCESS)) {
8899 			/*
8900 			 * Note: We are returning here after having done
8901 			 * substantial cleanup above. This is consistent
8902 			 * with the legacy implementation but this may not
8903 			 * be the right thing to do.
8904 			 */
8905 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8906 			    "sd_dr_detach: Cannot cancel remove event\n");
8907 			goto err_remove_event;
8908 		}
8909 		un->un_remove_event = NULL;
8910 	}
8911 
8912 	/* Do not free the softstate if the callback routine is active */
8913 	sd_sync_with_callback(un);
8914 
8915 	cmlb_detach(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
8916 	cmlb_free_handle(&un->un_cmlbhandle);
8917 
8918 	/*
8919 	 * Hold the detach mutex here, to make sure that no other threads ever
8920 	 * can access a (partially) freed soft state structure.
8921 	 */
8922 	mutex_enter(&sd_detach_mutex);
8923 
8924 	/*
8925 	 * Clean up the soft state struct.
8926 	 * Cleanup is done in reverse order of allocs/inits.
8927 	 * At this point there should be no competing threads anymore.
8928 	 */
8929 
8930 	scsi_fm_fini(devp);
8931 
8932 	/*
8933 	 * Deallocate memory for SCSI FMA.
8934 	 */
8935 	kmem_free(un->un_fm_private, sizeof (struct sd_fm_internal));
8936 
8937 	/*
8938 	 * Unregister and free device id if it was not registered
8939 	 * by the transport.
8940 	 */
8941 	if (un->un_f_devid_transport_defined == FALSE)
8942 		ddi_devid_unregister(devi);
8943 
8944 	/*
8945 	 * free the devid structure if allocated before (by ddi_devid_init()
8946 	 * or ddi_devid_get()).
8947 	 */
8948 	if (un->un_devid) {
8949 		ddi_devid_free(un->un_devid);
8950 		un->un_devid = NULL;
8951 	}
8952 
8953 	/*
8954 	 * Destroy wmap cache if it exists.
8955 	 */
8956 	if (un->un_wm_cache != NULL) {
8957 		kmem_cache_destroy(un->un_wm_cache);
8958 		un->un_wm_cache = NULL;
8959 	}
8960 
8961 	/*
8962 	 * kstat cleanup is done in detach for all device types (4363169).
8963 	 * We do not want to fail detach if the device kstats are not deleted
8964 	 * since there is a confusion about the devo_refcnt for the device.
8965 	 * We just delete the kstats and let detach complete successfully.
8966 	 */
8967 	if (un->un_stats != NULL) {
8968 		kstat_delete(un->un_stats);
8969 		un->un_stats = NULL;
8970 	}
8971 	if (un->un_errstats != NULL) {
8972 		kstat_delete(un->un_errstats);
8973 		un->un_errstats = NULL;
8974 	}
8975 
8976 	/* Remove partition stats */
8977 	if (un->un_f_pkstats_enabled) {
8978 		for (i = 0; i < NSDMAP; i++) {
8979 			if (un->un_pstats[i] != NULL) {
8980 				kstat_delete(un->un_pstats[i]);
8981 				un->un_pstats[i] = NULL;
8982 			}
8983 		}
8984 	}
8985 
8986 	/* Remove xbuf registration */
8987 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8988 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8989 
8990 	/* Remove driver properties */
8991 	ddi_prop_remove_all(devi);
8992 
8993 	mutex_destroy(&un->un_pm_mutex);
8994 	cv_destroy(&un->un_pm_busy_cv);
8995 
8996 	cv_destroy(&un->un_wcc_cv);
8997 
8998 	/* Open/close semaphore */
8999 	sema_destroy(&un->un_semoclose);
9000 
9001 	/* Removable media condvar. */
9002 	cv_destroy(&un->un_state_cv);
9003 
9004 	/* Suspend/resume condvar. */
9005 	cv_destroy(&un->un_suspend_cv);
9006 	cv_destroy(&un->un_disk_busy_cv);
9007 
9008 	sd_free_rqs(un);
9009 
9010 	/* Free up soft state */
9011 	devp->sd_private = NULL;
9012 
9013 	bzero(un, sizeof (struct sd_lun));
9014 #ifndef XPV_HVM_DRIVER
9015 	ddi_soft_state_free(sd_state, instance);
9016 #endif /* !XPV_HVM_DRIVER */
9017 
9018 	mutex_exit(&sd_detach_mutex);
9019 
9020 	/* This frees up the INQUIRY data associated with the device. */
9021 	scsi_unprobe(devp);
9022 
9023 	/*
9024 	 * After successfully detaching an instance, we update the information
9025 	 * of how many luns have been attached in the relative target and
9026 	 * controller for parallel SCSI. This information is used when sd tries
9027 	 * to set the tagged queuing capability in HBA.
9028 	 * Since un has been released, we can't use SD_IS_PARALLEL_SCSI(un) to
9029 	 * check if the device is parallel SCSI. However, we don't need to
9030 	 * check here because we've already checked during attach. No device
9031 	 * that is not parallel SCSI is in the chain.
9032 	 */
9033 	if ((tgt >= 0) && (tgt < NTARGETS_WIDE)) {
9034 		sd_scsi_update_lun_on_target(pdip, tgt, SD_SCSI_LUN_DETACH);
9035 	}
9036 
9037 	return (DDI_SUCCESS);
9038 
9039 err_notclosed:
9040 	mutex_exit(SD_MUTEX(un));
9041 
9042 err_stillbusy:
9043 	_NOTE(NO_COMPETING_THREADS_NOW);
9044 
9045 err_remove_event:
9046 	mutex_enter(&sd_detach_mutex);
9047 	un->un_detach_count--;
9048 	mutex_exit(&sd_detach_mutex);
9049 
9050 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9051 	return (DDI_FAILURE);
9052 }
9053 
9054 
9055 /*
9056  *    Function: sd_create_errstats
9057  *
9058  * Description: This routine instantiates the device error stats.
9059  *
9060  *		Note: During attach the stats are instantiated first so they are
9061  *		available for attach-time routines that utilize the driver
9062  *		iopath to send commands to the device. The stats are initialized
9063  *		separately so data obtained during some attach-time routines is
9064  *		available. (4362483)
9065  *
9066  *   Arguments: un - driver soft state (unit) structure
9067  *		instance - driver instance
9068  *
9069  *     Context: Kernel thread context
9070  */
9071 
9072 static void
9073 sd_create_errstats(struct sd_lun *un, int instance)
9074 {
9075 	struct	sd_errstats	*stp;
9076 	char	kstatmodule_err[KSTAT_STRLEN];
9077 	char	kstatname[KSTAT_STRLEN];
9078 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9079 
9080 	ASSERT(un != NULL);
9081 
9082 	if (un->un_errstats != NULL) {
9083 		return;
9084 	}
9085 
9086 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9087 	    "%serr", sd_label);
9088 	(void) snprintf(kstatname, sizeof (kstatname),
9089 	    "%s%d,err", sd_label, instance);
9090 
9091 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9092 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9093 
9094 	if (un->un_errstats == NULL) {
9095 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9096 		    "sd_create_errstats: Failed kstat_create\n");
9097 		return;
9098 	}
9099 
9100 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9101 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9102 	    KSTAT_DATA_UINT32);
9103 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9104 	    KSTAT_DATA_UINT32);
9105 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9106 	    KSTAT_DATA_UINT32);
9107 	kstat_named_init(&stp->sd_vid,		"Vendor",
9108 	    KSTAT_DATA_CHAR);
9109 	kstat_named_init(&stp->sd_pid,		"Product",
9110 	    KSTAT_DATA_CHAR);
9111 	kstat_named_init(&stp->sd_revision,	"Revision",
9112 	    KSTAT_DATA_CHAR);
9113 	kstat_named_init(&stp->sd_serial,	"Serial No",
9114 	    KSTAT_DATA_CHAR);
9115 	kstat_named_init(&stp->sd_capacity,	"Size",
9116 	    KSTAT_DATA_ULONGLONG);
9117 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9118 	    KSTAT_DATA_UINT32);
9119 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9120 	    KSTAT_DATA_UINT32);
9121 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9122 	    KSTAT_DATA_UINT32);
9123 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9124 	    KSTAT_DATA_UINT32);
9125 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9126 	    KSTAT_DATA_UINT32);
9127 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9128 	    KSTAT_DATA_UINT32);
9129 
9130 	un->un_errstats->ks_private = un;
9131 	un->un_errstats->ks_update  = nulldev;
9132 
9133 	kstat_install(un->un_errstats);
9134 }
9135 
9136 
9137 /*
9138  *    Function: sd_set_errstats
9139  *
9140  * Description: This routine sets the value of the vendor id, product id,
9141  *		revision, serial number, and capacity device error stats.
9142  *
9143  *		Note: During attach the stats are instantiated first so they are
9144  *		available for attach-time routines that utilize the driver
9145  *		iopath to send commands to the device. The stats are initialized
9146  *		separately so data obtained during some attach-time routines is
9147  *		available. (4362483)
9148  *
9149  *   Arguments: un - driver soft state (unit) structure
9150  *
9151  *     Context: Kernel thread context
9152  */
9153 
9154 static void
9155 sd_set_errstats(struct sd_lun *un)
9156 {
9157 	struct	sd_errstats	*stp;
9158 	char 			*sn;
9159 
9160 	ASSERT(un != NULL);
9161 	ASSERT(un->un_errstats != NULL);
9162 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9163 	ASSERT(stp != NULL);
9164 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9165 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9166 	(void) strncpy(stp->sd_revision.value.c,
9167 	    un->un_sd->sd_inq->inq_revision, 4);
9168 
9169 	/*
9170 	 * All the errstats are persistent across detach/attach,
9171 	 * so reset all the errstats here in case of the hot
9172 	 * replacement of disk drives, except for not changed
9173 	 * Sun qualified drives.
9174 	 */
9175 	if ((bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) != 0) ||
9176 	    (bcmp(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9177 	    sizeof (SD_INQUIRY(un)->inq_serial)) != 0)) {
9178 		stp->sd_softerrs.value.ui32 = 0;
9179 		stp->sd_harderrs.value.ui32 = 0;
9180 		stp->sd_transerrs.value.ui32 = 0;
9181 		stp->sd_rq_media_err.value.ui32 = 0;
9182 		stp->sd_rq_ntrdy_err.value.ui32 = 0;
9183 		stp->sd_rq_nodev_err.value.ui32 = 0;
9184 		stp->sd_rq_recov_err.value.ui32 = 0;
9185 		stp->sd_rq_illrq_err.value.ui32 = 0;
9186 		stp->sd_rq_pfa_err.value.ui32 = 0;
9187 	}
9188 
9189 	/*
9190 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9191 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9192 	 * (4376302))
9193 	 */
9194 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9195 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9196 		    sizeof (SD_INQUIRY(un)->inq_serial));
9197 	} else {
9198 		/*
9199 		 * Set the "Serial No" kstat for non-Sun qualified drives
9200 		 */
9201 		if (ddi_prop_lookup_string(DDI_DEV_T_ANY, SD_DEVINFO(un),
9202 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
9203 		    INQUIRY_SERIAL_NO, &sn) == DDI_SUCCESS) {
9204 			(void) strlcpy(stp->sd_serial.value.c, sn,
9205 			    sizeof (stp->sd_serial.value.c));
9206 			ddi_prop_free(sn);
9207 		}
9208 	}
9209 
9210 	if (un->un_f_blockcount_is_valid != TRUE) {
9211 		/*
9212 		 * Set capacity error stat to 0 for no media. This ensures
9213 		 * a valid capacity is displayed in response to 'iostat -E'
9214 		 * when no media is present in the device.
9215 		 */
9216 		stp->sd_capacity.value.ui64 = 0;
9217 	} else {
9218 		/*
9219 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9220 		 * capacity.
9221 		 *
9222 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9223 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9224 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9225 		 */
9226 		stp->sd_capacity.value.ui64 = (uint64_t)
9227 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9228 	}
9229 }
9230 
9231 
9232 /*
9233  *    Function: sd_set_pstats
9234  *
9235  * Description: This routine instantiates and initializes the partition
9236  *              stats for each partition with more than zero blocks.
9237  *		(4363169)
9238  *
9239  *   Arguments: un - driver soft state (unit) structure
9240  *
9241  *     Context: Kernel thread context
9242  */
9243 
9244 static void
9245 sd_set_pstats(struct sd_lun *un)
9246 {
9247 	char	kstatname[KSTAT_STRLEN];
9248 	int	instance;
9249 	int	i;
9250 	diskaddr_t	nblks = 0;
9251 	char	*partname = NULL;
9252 
9253 	ASSERT(un != NULL);
9254 
9255 	instance = ddi_get_instance(SD_DEVINFO(un));
9256 
9257 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9258 	for (i = 0; i < NSDMAP; i++) {
9259 
9260 		if (cmlb_partinfo(un->un_cmlbhandle, i,
9261 		    &nblks, NULL, &partname, NULL, (void *)SD_PATH_DIRECT) != 0)
9262 			continue;
9263 		mutex_enter(SD_MUTEX(un));
9264 
9265 		if ((un->un_pstats[i] == NULL) &&
9266 		    (nblks != 0)) {
9267 
9268 			(void) snprintf(kstatname, sizeof (kstatname),
9269 			    "%s%d,%s", sd_label, instance,
9270 			    partname);
9271 
9272 			un->un_pstats[i] = kstat_create(sd_label,
9273 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9274 			    1, KSTAT_FLAG_PERSISTENT);
9275 			if (un->un_pstats[i] != NULL) {
9276 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9277 				kstat_install(un->un_pstats[i]);
9278 			}
9279 		}
9280 		mutex_exit(SD_MUTEX(un));
9281 	}
9282 }
9283 
9284 
9285 #if (defined(__fibre))
9286 /*
9287  *    Function: sd_init_event_callbacks
9288  *
9289  * Description: This routine initializes the insertion and removal event
9290  *		callbacks. (fibre only)
9291  *
9292  *   Arguments: un - driver soft state (unit) structure
9293  *
9294  *     Context: Kernel thread context
9295  */
9296 
9297 static void
9298 sd_init_event_callbacks(struct sd_lun *un)
9299 {
9300 	ASSERT(un != NULL);
9301 
9302 	if ((un->un_insert_event == NULL) &&
9303 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9304 	    &un->un_insert_event) == DDI_SUCCESS)) {
9305 		/*
9306 		 * Add the callback for an insertion event
9307 		 */
9308 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9309 		    un->un_insert_event, sd_event_callback, (void *)un,
9310 		    &(un->un_insert_cb_id));
9311 	}
9312 
9313 	if ((un->un_remove_event == NULL) &&
9314 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9315 	    &un->un_remove_event) == DDI_SUCCESS)) {
9316 		/*
9317 		 * Add the callback for a removal event
9318 		 */
9319 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9320 		    un->un_remove_event, sd_event_callback, (void *)un,
9321 		    &(un->un_remove_cb_id));
9322 	}
9323 }
9324 
9325 
9326 /*
9327  *    Function: sd_event_callback
9328  *
9329  * Description: This routine handles insert/remove events (photon). The
9330  *		state is changed to OFFLINE which can be used to supress
9331  *		error msgs. (fibre only)
9332  *
9333  *   Arguments: un - driver soft state (unit) structure
9334  *
9335  *     Context: Callout thread context
9336  */
9337 /* ARGSUSED */
9338 static void
9339 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9340     void *bus_impldata)
9341 {
9342 	struct sd_lun *un = (struct sd_lun *)arg;
9343 
9344 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9345 	if (event == un->un_insert_event) {
9346 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9347 		mutex_enter(SD_MUTEX(un));
9348 		if (un->un_state == SD_STATE_OFFLINE) {
9349 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9350 				un->un_state = un->un_last_state;
9351 			} else {
9352 				/*
9353 				 * We have gone through SUSPEND/RESUME while
9354 				 * we were offline. Restore the last state
9355 				 */
9356 				un->un_state = un->un_save_state;
9357 			}
9358 		}
9359 		mutex_exit(SD_MUTEX(un));
9360 
9361 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9362 	} else if (event == un->un_remove_event) {
9363 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9364 		mutex_enter(SD_MUTEX(un));
9365 		/*
9366 		 * We need to handle an event callback that occurs during
9367 		 * the suspend operation, since we don't prevent it.
9368 		 */
9369 		if (un->un_state != SD_STATE_OFFLINE) {
9370 			if (un->un_state != SD_STATE_SUSPENDED) {
9371 				New_state(un, SD_STATE_OFFLINE);
9372 			} else {
9373 				un->un_last_state = SD_STATE_OFFLINE;
9374 			}
9375 		}
9376 		mutex_exit(SD_MUTEX(un));
9377 	} else {
9378 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9379 		    "!Unknown event\n");
9380 	}
9381 
9382 }
9383 #endif
9384 
9385 /*
9386  *    Function: sd_cache_control()
9387  *
9388  * Description: This routine is the driver entry point for setting
9389  *		read and write caching by modifying the WCE (write cache
9390  *		enable) and RCD (read cache disable) bits of mode
9391  *		page 8 (MODEPAGE_CACHING).
9392  *
9393  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
9394  *                      structure for this target.
9395  *		rcd_flag - flag for controlling the read cache
9396  *		wce_flag - flag for controlling the write cache
9397  *
9398  * Return Code: EIO
9399  *		code returned by sd_send_scsi_MODE_SENSE and
9400  *		sd_send_scsi_MODE_SELECT
9401  *
9402  *     Context: Kernel Thread
9403  */
9404 
9405 static int
9406 sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag)
9407 {
9408 	struct mode_caching	*mode_caching_page;
9409 	uchar_t			*header;
9410 	size_t			buflen;
9411 	int			hdrlen;
9412 	int			bd_len;
9413 	int			rval = 0;
9414 	struct mode_header_grp2	*mhp;
9415 	struct sd_lun		*un;
9416 	int			status;
9417 
9418 	ASSERT(ssc != NULL);
9419 	un = ssc->ssc_un;
9420 	ASSERT(un != NULL);
9421 
9422 	/*
9423 	 * Do a test unit ready, otherwise a mode sense may not work if this
9424 	 * is the first command sent to the device after boot.
9425 	 */
9426 	status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9427 	if (status != 0)
9428 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9429 
9430 	if (un->un_f_cfg_is_atapi == TRUE) {
9431 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9432 	} else {
9433 		hdrlen = MODE_HEADER_LENGTH;
9434 	}
9435 
9436 	/*
9437 	 * Allocate memory for the retrieved mode page and its headers.  Set
9438 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
9439 	 * we get all of the mode sense data otherwise, the mode select
9440 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
9441 	 */
9442 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9443 	    sizeof (struct mode_cache_scsi3);
9444 
9445 	header = kmem_zalloc(buflen, KM_SLEEP);
9446 
9447 	/* Get the information from the device. */
9448 	if (un->un_f_cfg_is_atapi == TRUE) {
9449 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9450 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9451 	} else {
9452 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9453 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9454 	}
9455 
9456 	if (rval != 0) {
9457 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9458 		    "sd_cache_control: Mode Sense Failed\n");
9459 		goto mode_sense_failed;
9460 	}
9461 
9462 	/*
9463 	 * Determine size of Block Descriptors in order to locate
9464 	 * the mode page data. ATAPI devices return 0, SCSI devices
9465 	 * should return MODE_BLK_DESC_LENGTH.
9466 	 */
9467 	if (un->un_f_cfg_is_atapi == TRUE) {
9468 		mhp	= (struct mode_header_grp2 *)header;
9469 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9470 	} else {
9471 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9472 	}
9473 
9474 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9475 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9476 		    "sd_cache_control: Mode Sense returned invalid block "
9477 		    "descriptor length\n");
9478 		rval = EIO;
9479 		goto mode_sense_failed;
9480 	}
9481 
9482 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9483 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9484 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9485 		    "sd_cache_control: Mode Sense caching page code mismatch "
9486 		    "%d\n", mode_caching_page->mode_page.code);
9487 		rval = EIO;
9488 		goto mode_sense_failed;
9489 	}
9490 
9491 	/* Check the relevant bits on successful mode sense. */
9492 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9493 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9494 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9495 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9496 
9497 		size_t sbuflen;
9498 		uchar_t save_pg;
9499 
9500 		/*
9501 		 * Construct select buffer length based on the
9502 		 * length of the sense data returned.
9503 		 */
9504 		sbuflen =  hdrlen + bd_len +
9505 		    sizeof (struct mode_page) +
9506 		    (int)mode_caching_page->mode_page.length;
9507 
9508 		/*
9509 		 * Set the caching bits as requested.
9510 		 */
9511 		if (rcd_flag == SD_CACHE_ENABLE)
9512 			mode_caching_page->rcd = 0;
9513 		else if (rcd_flag == SD_CACHE_DISABLE)
9514 			mode_caching_page->rcd = 1;
9515 
9516 		if (wce_flag == SD_CACHE_ENABLE)
9517 			mode_caching_page->wce = 1;
9518 		else if (wce_flag == SD_CACHE_DISABLE)
9519 			mode_caching_page->wce = 0;
9520 
9521 		/*
9522 		 * Save the page if the mode sense says the
9523 		 * drive supports it.
9524 		 */
9525 		save_pg = mode_caching_page->mode_page.ps ?
9526 		    SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9527 
9528 		/* Clear reserved bits before mode select. */
9529 		mode_caching_page->mode_page.ps = 0;
9530 
9531 		/*
9532 		 * Clear out mode header for mode select.
9533 		 * The rest of the retrieved page will be reused.
9534 		 */
9535 		bzero(header, hdrlen);
9536 
9537 		if (un->un_f_cfg_is_atapi == TRUE) {
9538 			mhp = (struct mode_header_grp2 *)header;
9539 			mhp->bdesc_length_hi = bd_len >> 8;
9540 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9541 		} else {
9542 			((struct mode_header *)header)->bdesc_length = bd_len;
9543 		}
9544 
9545 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9546 
9547 		/* Issue mode select to change the cache settings */
9548 		if (un->un_f_cfg_is_atapi == TRUE) {
9549 			rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, header,
9550 			    sbuflen, save_pg, SD_PATH_DIRECT);
9551 		} else {
9552 			rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
9553 			    sbuflen, save_pg, SD_PATH_DIRECT);
9554 		}
9555 
9556 	}
9557 
9558 
9559 mode_sense_failed:
9560 
9561 	kmem_free(header, buflen);
9562 
9563 	if (rval != 0) {
9564 		if (rval == EIO)
9565 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9566 		else
9567 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9568 	}
9569 	return (rval);
9570 }
9571 
9572 
9573 /*
9574  *    Function: sd_get_write_cache_enabled()
9575  *
9576  * Description: This routine is the driver entry point for determining if
9577  *		write caching is enabled.  It examines the WCE (write cache
9578  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
9579  *
9580  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
9581  *                      structure for this target.
9582  *		is_enabled - pointer to int where write cache enabled state
9583  *		is returned (non-zero -> write cache enabled)
9584  *
9585  *
9586  * Return Code: EIO
9587  *		code returned by sd_send_scsi_MODE_SENSE
9588  *
9589  *     Context: Kernel Thread
9590  *
9591  * NOTE: If ioctl is added to disable write cache, this sequence should
9592  * be followed so that no locking is required for accesses to
9593  * un->un_f_write_cache_enabled:
9594  * 	do mode select to clear wce
9595  * 	do synchronize cache to flush cache
9596  * 	set un->un_f_write_cache_enabled = FALSE
9597  *
9598  * Conversely, an ioctl to enable the write cache should be done
9599  * in this order:
9600  * 	set un->un_f_write_cache_enabled = TRUE
9601  * 	do mode select to set wce
9602  */
9603 
9604 static int
9605 sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled)
9606 {
9607 	struct mode_caching	*mode_caching_page;
9608 	uchar_t			*header;
9609 	size_t			buflen;
9610 	int			hdrlen;
9611 	int			bd_len;
9612 	int			rval = 0;
9613 	struct sd_lun		*un;
9614 	int			status;
9615 
9616 	ASSERT(ssc != NULL);
9617 	un = ssc->ssc_un;
9618 	ASSERT(un != NULL);
9619 	ASSERT(is_enabled != NULL);
9620 
9621 	/* in case of error, flag as enabled */
9622 	*is_enabled = TRUE;
9623 
9624 	/*
9625 	 * Do a test unit ready, otherwise a mode sense may not work if this
9626 	 * is the first command sent to the device after boot.
9627 	 */
9628 	status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
9629 
9630 	if (status != 0)
9631 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9632 
9633 	if (un->un_f_cfg_is_atapi == TRUE) {
9634 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9635 	} else {
9636 		hdrlen = MODE_HEADER_LENGTH;
9637 	}
9638 
9639 	/*
9640 	 * Allocate memory for the retrieved mode page and its headers.  Set
9641 	 * a pointer to the page itself.
9642 	 */
9643 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9644 	header = kmem_zalloc(buflen, KM_SLEEP);
9645 
9646 	/* Get the information from the device. */
9647 	if (un->un_f_cfg_is_atapi == TRUE) {
9648 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, header, buflen,
9649 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9650 	} else {
9651 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
9652 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9653 	}
9654 
9655 	if (rval != 0) {
9656 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9657 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
9658 		goto mode_sense_failed;
9659 	}
9660 
9661 	/*
9662 	 * Determine size of Block Descriptors in order to locate
9663 	 * the mode page data. ATAPI devices return 0, SCSI devices
9664 	 * should return MODE_BLK_DESC_LENGTH.
9665 	 */
9666 	if (un->un_f_cfg_is_atapi == TRUE) {
9667 		struct mode_header_grp2	*mhp;
9668 		mhp	= (struct mode_header_grp2 *)header;
9669 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9670 	} else {
9671 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9672 	}
9673 
9674 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9675 		/* FMA should make upset complain here */
9676 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, 0,
9677 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
9678 		    "block descriptor length\n");
9679 		rval = EIO;
9680 		goto mode_sense_failed;
9681 	}
9682 
9683 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9684 	if (mode_caching_page->mode_page.code != MODEPAGE_CACHING) {
9685 		/* FMA could make upset complain here */
9686 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
9687 		    "sd_get_write_cache_enabled: Mode Sense caching page "
9688 		    "code mismatch %d\n", mode_caching_page->mode_page.code);
9689 		rval = EIO;
9690 		goto mode_sense_failed;
9691 	}
9692 	*is_enabled = mode_caching_page->wce;
9693 
9694 mode_sense_failed:
9695 	if (rval == 0) {
9696 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
9697 	} else if (rval == EIO) {
9698 		/*
9699 		 * Some disks do not support mode sense(6), we
9700 		 * should ignore this kind of error(sense key is
9701 		 * 0x5 - illegal request).
9702 		 */
9703 		uint8_t *sensep;
9704 		int senlen;
9705 
9706 		sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
9707 		senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
9708 		    ssc->ssc_uscsi_cmd->uscsi_rqresid);
9709 
9710 		if (senlen > 0 &&
9711 		    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
9712 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
9713 		} else {
9714 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
9715 		}
9716 	} else {
9717 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9718 	}
9719 	kmem_free(header, buflen);
9720 	return (rval);
9721 }
9722 
9723 /*
9724  *    Function: sd_get_nv_sup()
9725  *
9726  * Description: This routine is the driver entry point for
9727  * determining whether non-volatile cache is supported. This
9728  * determination process works as follows:
9729  *
9730  * 1. sd first queries sd.conf on whether
9731  * suppress_cache_flush bit is set for this device.
9732  *
9733  * 2. if not there, then queries the internal disk table.
9734  *
9735  * 3. if either sd.conf or internal disk table specifies
9736  * cache flush be suppressed, we don't bother checking
9737  * NV_SUP bit.
9738  *
9739  * If SUPPRESS_CACHE_FLUSH bit is not set to 1, sd queries
9740  * the optional INQUIRY VPD page 0x86. If the device
9741  * supports VPD page 0x86, sd examines the NV_SUP
9742  * (non-volatile cache support) bit in the INQUIRY VPD page
9743  * 0x86:
9744  *   o If NV_SUP bit is set, sd assumes the device has a
9745  *   non-volatile cache and set the
9746  *   un_f_sync_nv_supported to TRUE.
9747  *   o Otherwise cache is not non-volatile,
9748  *   un_f_sync_nv_supported is set to FALSE.
9749  *
9750  * Arguments: un - driver soft state (unit) structure
9751  *
9752  * Return Code:
9753  *
9754  *     Context: Kernel Thread
9755  */
9756 
9757 static void
9758 sd_get_nv_sup(sd_ssc_t *ssc)
9759 {
9760 	int		rval		= 0;
9761 	uchar_t		*inq86		= NULL;
9762 	size_t		inq86_len	= MAX_INQUIRY_SIZE;
9763 	size_t		inq86_resid	= 0;
9764 	struct		dk_callback *dkc;
9765 	struct sd_lun	*un;
9766 
9767 	ASSERT(ssc != NULL);
9768 	un = ssc->ssc_un;
9769 	ASSERT(un != NULL);
9770 
9771 	mutex_enter(SD_MUTEX(un));
9772 
9773 	/*
9774 	 * Be conservative on the device's support of
9775 	 * SYNC_NV bit: un_f_sync_nv_supported is
9776 	 * initialized to be false.
9777 	 */
9778 	un->un_f_sync_nv_supported = FALSE;
9779 
9780 	/*
9781 	 * If either sd.conf or internal disk table
9782 	 * specifies cache flush be suppressed, then
9783 	 * we don't bother checking NV_SUP bit.
9784 	 */
9785 	if (un->un_f_suppress_cache_flush == TRUE) {
9786 		mutex_exit(SD_MUTEX(un));
9787 		return;
9788 	}
9789 
9790 	if (sd_check_vpd_page_support(ssc) == 0 &&
9791 	    un->un_vpd_page_mask & SD_VPD_EXTENDED_DATA_PG) {
9792 		mutex_exit(SD_MUTEX(un));
9793 		/* collect page 86 data if available */
9794 		inq86 = kmem_zalloc(inq86_len, KM_SLEEP);
9795 
9796 		rval = sd_send_scsi_INQUIRY(ssc, inq86, inq86_len,
9797 		    0x01, 0x86, &inq86_resid);
9798 
9799 		if (rval == 0 && (inq86_len - inq86_resid > 6)) {
9800 			SD_TRACE(SD_LOG_COMMON, un,
9801 			    "sd_get_nv_sup: \
9802 			    successfully get VPD page: %x \
9803 			    PAGE LENGTH: %x BYTE 6: %x\n",
9804 			    inq86[1], inq86[3], inq86[6]);
9805 
9806 			mutex_enter(SD_MUTEX(un));
9807 			/*
9808 			 * check the value of NV_SUP bit: only if the device
9809 			 * reports NV_SUP bit to be 1, the
9810 			 * un_f_sync_nv_supported bit will be set to true.
9811 			 */
9812 			if (inq86[6] & SD_VPD_NV_SUP) {
9813 				un->un_f_sync_nv_supported = TRUE;
9814 			}
9815 			mutex_exit(SD_MUTEX(un));
9816 		} else if (rval != 0) {
9817 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9818 		}
9819 
9820 		kmem_free(inq86, inq86_len);
9821 	} else {
9822 		mutex_exit(SD_MUTEX(un));
9823 	}
9824 
9825 	/*
9826 	 * Send a SYNC CACHE command to check whether
9827 	 * SYNC_NV bit is supported. This command should have
9828 	 * un_f_sync_nv_supported set to correct value.
9829 	 */
9830 	mutex_enter(SD_MUTEX(un));
9831 	if (un->un_f_sync_nv_supported) {
9832 		mutex_exit(SD_MUTEX(un));
9833 		dkc = kmem_zalloc(sizeof (struct dk_callback), KM_SLEEP);
9834 		dkc->dkc_flag = FLUSH_VOLATILE;
9835 		(void) sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
9836 
9837 		/*
9838 		 * Send a TEST UNIT READY command to the device. This should
9839 		 * clear any outstanding UNIT ATTENTION that may be present.
9840 		 */
9841 		rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_DONT_RETRY_TUR);
9842 		if (rval != 0)
9843 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
9844 
9845 		kmem_free(dkc, sizeof (struct dk_callback));
9846 	} else {
9847 		mutex_exit(SD_MUTEX(un));
9848 	}
9849 
9850 	SD_TRACE(SD_LOG_COMMON, un, "sd_get_nv_sup: \
9851 	    un_f_suppress_cache_flush is set to %d\n",
9852 	    un->un_f_suppress_cache_flush);
9853 }
9854 
9855 /*
9856  *    Function: sd_make_device
9857  *
9858  * Description: Utility routine to return the Solaris device number from
9859  *		the data in the device's dev_info structure.
9860  *
9861  * Return Code: The Solaris device number
9862  *
9863  *     Context: Any
9864  */
9865 
9866 static dev_t
9867 sd_make_device(dev_info_t *devi)
9868 {
9869 	return (makedevice(ddi_driver_major(devi),
9870 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9871 }
9872 
9873 
9874 /*
9875  *    Function: sd_pm_entry
9876  *
9877  * Description: Called at the start of a new command to manage power
9878  *		and busy status of a device. This includes determining whether
9879  *		the current power state of the device is sufficient for
9880  *		performing the command or whether it must be changed.
9881  *		The PM framework is notified appropriately.
9882  *		Only with a return status of DDI_SUCCESS will the
9883  *		component be busy to the framework.
9884  *
9885  *		All callers of sd_pm_entry must check the return status
9886  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9887  *		of DDI_FAILURE indicates the device failed to power up.
9888  *		In this case un_pm_count has been adjusted so the result
9889  *		on exit is still powered down, ie. count is less than 0.
9890  *		Calling sd_pm_exit with this count value hits an ASSERT.
9891  *
9892  * Return Code: DDI_SUCCESS or DDI_FAILURE
9893  *
9894  *     Context: Kernel thread context.
9895  */
9896 
9897 static int
9898 sd_pm_entry(struct sd_lun *un)
9899 {
9900 	int return_status = DDI_SUCCESS;
9901 
9902 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9903 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9904 
9905 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9906 
9907 	if (un->un_f_pm_is_enabled == FALSE) {
9908 		SD_TRACE(SD_LOG_IO_PM, un,
9909 		    "sd_pm_entry: exiting, PM not enabled\n");
9910 		return (return_status);
9911 	}
9912 
9913 	/*
9914 	 * Just increment a counter if PM is enabled. On the transition from
9915 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9916 	 * the count with each IO and mark the device as idle when the count
9917 	 * hits 0.
9918 	 *
9919 	 * If the count is less than 0 the device is powered down. If a powered
9920 	 * down device is successfully powered up then the count must be
9921 	 * incremented to reflect the power up. Note that it'll get incremented
9922 	 * a second time to become busy.
9923 	 *
9924 	 * Because the following has the potential to change the device state
9925 	 * and must release the un_pm_mutex to do so, only one thread can be
9926 	 * allowed through at a time.
9927 	 */
9928 
9929 	mutex_enter(&un->un_pm_mutex);
9930 	while (un->un_pm_busy == TRUE) {
9931 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9932 	}
9933 	un->un_pm_busy = TRUE;
9934 
9935 	if (un->un_pm_count < 1) {
9936 
9937 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9938 
9939 		/*
9940 		 * Indicate we are now busy so the framework won't attempt to
9941 		 * power down the device. This call will only fail if either
9942 		 * we passed a bad component number or the device has no
9943 		 * components. Neither of these should ever happen.
9944 		 */
9945 		mutex_exit(&un->un_pm_mutex);
9946 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9947 		ASSERT(return_status == DDI_SUCCESS);
9948 
9949 		mutex_enter(&un->un_pm_mutex);
9950 
9951 		if (un->un_pm_count < 0) {
9952 			mutex_exit(&un->un_pm_mutex);
9953 
9954 			SD_TRACE(SD_LOG_IO_PM, un,
9955 			    "sd_pm_entry: power up component\n");
9956 
9957 			/*
9958 			 * pm_raise_power will cause sdpower to be called
9959 			 * which brings the device power level to the
9960 			 * desired state, If successful, un_pm_count and
9961 			 * un_power_level will be updated appropriately.
9962 			 */
9963 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9964 			    SD_PM_STATE_ACTIVE(un));
9965 
9966 			mutex_enter(&un->un_pm_mutex);
9967 
9968 			if (return_status != DDI_SUCCESS) {
9969 				/*
9970 				 * Power up failed.
9971 				 * Idle the device and adjust the count
9972 				 * so the result on exit is that we're
9973 				 * still powered down, ie. count is less than 0.
9974 				 */
9975 				SD_TRACE(SD_LOG_IO_PM, un,
9976 				    "sd_pm_entry: power up failed,"
9977 				    " idle the component\n");
9978 
9979 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9980 				un->un_pm_count--;
9981 			} else {
9982 				/*
9983 				 * Device is powered up, verify the
9984 				 * count is non-negative.
9985 				 * This is debug only.
9986 				 */
9987 				ASSERT(un->un_pm_count == 0);
9988 			}
9989 		}
9990 
9991 		if (return_status == DDI_SUCCESS) {
9992 			/*
9993 			 * For performance, now that the device has been tagged
9994 			 * as busy, and it's known to be powered up, update the
9995 			 * chain types to use jump tables that do not include
9996 			 * pm. This significantly lowers the overhead and
9997 			 * therefore improves performance.
9998 			 */
9999 
10000 			mutex_exit(&un->un_pm_mutex);
10001 			mutex_enter(SD_MUTEX(un));
10002 			SD_TRACE(SD_LOG_IO_PM, un,
10003 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
10004 			    un->un_uscsi_chain_type);
10005 
10006 			if (un->un_f_non_devbsize_supported) {
10007 				un->un_buf_chain_type =
10008 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10009 			} else {
10010 				un->un_buf_chain_type =
10011 				    SD_CHAIN_INFO_DISK_NO_PM;
10012 			}
10013 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10014 
10015 			SD_TRACE(SD_LOG_IO_PM, un,
10016 			    "             changed  uscsi_chain_type to   %d\n",
10017 			    un->un_uscsi_chain_type);
10018 			mutex_exit(SD_MUTEX(un));
10019 			mutex_enter(&un->un_pm_mutex);
10020 
10021 			if (un->un_pm_idle_timeid == NULL) {
10022 				/* 300 ms. */
10023 				un->un_pm_idle_timeid =
10024 				    timeout(sd_pm_idletimeout_handler, un,
10025 				    (drv_usectohz((clock_t)300000)));
10026 				/*
10027 				 * Include an extra call to busy which keeps the
10028 				 * device busy with-respect-to the PM layer
10029 				 * until the timer fires, at which time it'll
10030 				 * get the extra idle call.
10031 				 */
10032 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10033 			}
10034 		}
10035 	}
10036 	un->un_pm_busy = FALSE;
10037 	/* Next... */
10038 	cv_signal(&un->un_pm_busy_cv);
10039 
10040 	un->un_pm_count++;
10041 
10042 	SD_TRACE(SD_LOG_IO_PM, un,
10043 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10044 
10045 	mutex_exit(&un->un_pm_mutex);
10046 
10047 	return (return_status);
10048 }
10049 
10050 
10051 /*
10052  *    Function: sd_pm_exit
10053  *
10054  * Description: Called at the completion of a command to manage busy
10055  *		status for the device. If the device becomes idle the
10056  *		PM framework is notified.
10057  *
10058  *     Context: Kernel thread context
10059  */
10060 
10061 static void
10062 sd_pm_exit(struct sd_lun *un)
10063 {
10064 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10065 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10066 
10067 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10068 
10069 	/*
10070 	 * After attach the following flag is only read, so don't
10071 	 * take the penalty of acquiring a mutex for it.
10072 	 */
10073 	if (un->un_f_pm_is_enabled == TRUE) {
10074 
10075 		mutex_enter(&un->un_pm_mutex);
10076 		un->un_pm_count--;
10077 
10078 		SD_TRACE(SD_LOG_IO_PM, un,
10079 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10080 
10081 		ASSERT(un->un_pm_count >= 0);
10082 		if (un->un_pm_count == 0) {
10083 			mutex_exit(&un->un_pm_mutex);
10084 
10085 			SD_TRACE(SD_LOG_IO_PM, un,
10086 			    "sd_pm_exit: idle component\n");
10087 
10088 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10089 
10090 		} else {
10091 			mutex_exit(&un->un_pm_mutex);
10092 		}
10093 	}
10094 
10095 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10096 }
10097 
10098 
10099 /*
10100  *    Function: sdopen
10101  *
10102  * Description: Driver's open(9e) entry point function.
10103  *
10104  *   Arguments: dev_i   - pointer to device number
10105  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10106  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10107  *		cred_p  - user credential pointer
10108  *
10109  * Return Code: EINVAL
10110  *		ENXIO
10111  *		EIO
10112  *		EROFS
10113  *		EBUSY
10114  *
10115  *     Context: Kernel thread context
10116  */
10117 /* ARGSUSED */
10118 static int
10119 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10120 {
10121 	struct sd_lun	*un;
10122 	int		nodelay;
10123 	int		part;
10124 	uint64_t	partmask;
10125 	int		instance;
10126 	dev_t		dev;
10127 	int		rval = EIO;
10128 	diskaddr_t	nblks = 0;
10129 	diskaddr_t	label_cap;
10130 
10131 	/* Validate the open type */
10132 	if (otyp >= OTYPCNT) {
10133 		return (EINVAL);
10134 	}
10135 
10136 	dev = *dev_p;
10137 	instance = SDUNIT(dev);
10138 	mutex_enter(&sd_detach_mutex);
10139 
10140 	/*
10141 	 * Fail the open if there is no softstate for the instance, or
10142 	 * if another thread somewhere is trying to detach the instance.
10143 	 */
10144 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10145 	    (un->un_detach_count != 0)) {
10146 		mutex_exit(&sd_detach_mutex);
10147 		/*
10148 		 * The probe cache only needs to be cleared when open (9e) fails
10149 		 * with ENXIO (4238046).
10150 		 */
10151 		/*
10152 		 * un-conditionally clearing probe cache is ok with
10153 		 * separate sd/ssd binaries
10154 		 * x86 platform can be an issue with both parallel
10155 		 * and fibre in 1 binary
10156 		 */
10157 		sd_scsi_clear_probe_cache();
10158 		return (ENXIO);
10159 	}
10160 
10161 	/*
10162 	 * The un_layer_count is to prevent another thread in specfs from
10163 	 * trying to detach the instance, which can happen when we are
10164 	 * called from a higher-layer driver instead of thru specfs.
10165 	 * This will not be needed when DDI provides a layered driver
10166 	 * interface that allows specfs to know that an instance is in
10167 	 * use by a layered driver & should not be detached.
10168 	 *
10169 	 * Note: the semantics for layered driver opens are exactly one
10170 	 * close for every open.
10171 	 */
10172 	if (otyp == OTYP_LYR) {
10173 		un->un_layer_count++;
10174 	}
10175 
10176 	/*
10177 	 * Keep a count of the current # of opens in progress. This is because
10178 	 * some layered drivers try to call us as a regular open. This can
10179 	 * cause problems that we cannot prevent, however by keeping this count
10180 	 * we can at least keep our open and detach routines from racing against
10181 	 * each other under such conditions.
10182 	 */
10183 	un->un_opens_in_progress++;
10184 	mutex_exit(&sd_detach_mutex);
10185 
10186 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10187 	part	 = SDPART(dev);
10188 	partmask = 1 << part;
10189 
10190 	/*
10191 	 * We use a semaphore here in order to serialize
10192 	 * open and close requests on the device.
10193 	 */
10194 	sema_p(&un->un_semoclose);
10195 
10196 	mutex_enter(SD_MUTEX(un));
10197 
10198 	/*
10199 	 * All device accesses go thru sdstrategy() where we check
10200 	 * on suspend status but there could be a scsi_poll command,
10201 	 * which bypasses sdstrategy(), so we need to check pm
10202 	 * status.
10203 	 */
10204 
10205 	if (!nodelay) {
10206 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10207 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10208 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10209 		}
10210 
10211 		mutex_exit(SD_MUTEX(un));
10212 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10213 			rval = EIO;
10214 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10215 			    "sdopen: sd_pm_entry failed\n");
10216 			goto open_failed_with_pm;
10217 		}
10218 		mutex_enter(SD_MUTEX(un));
10219 	}
10220 
10221 	/* check for previous exclusive open */
10222 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10223 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10224 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10225 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10226 
10227 	if (un->un_exclopen & (partmask)) {
10228 		goto excl_open_fail;
10229 	}
10230 
10231 	if (flag & FEXCL) {
10232 		int i;
10233 		if (un->un_ocmap.lyropen[part]) {
10234 			goto excl_open_fail;
10235 		}
10236 		for (i = 0; i < (OTYPCNT - 1); i++) {
10237 			if (un->un_ocmap.regopen[i] & (partmask)) {
10238 				goto excl_open_fail;
10239 			}
10240 		}
10241 	}
10242 
10243 	/*
10244 	 * Check the write permission if this is a removable media device,
10245 	 * NDELAY has not been set, and writable permission is requested.
10246 	 *
10247 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10248 	 * attempt will fail with EIO as part of the I/O processing. This is a
10249 	 * more permissive implementation that allows the open to succeed and
10250 	 * WRITE attempts to fail when appropriate.
10251 	 */
10252 	if (un->un_f_chk_wp_open) {
10253 		if ((flag & FWRITE) && (!nodelay)) {
10254 			mutex_exit(SD_MUTEX(un));
10255 			/*
10256 			 * Defer the check for write permission on writable
10257 			 * DVD drive till sdstrategy and will not fail open even
10258 			 * if FWRITE is set as the device can be writable
10259 			 * depending upon the media and the media can change
10260 			 * after the call to open().
10261 			 */
10262 			if (un->un_f_dvdram_writable_device == FALSE) {
10263 				if (ISCD(un) || sr_check_wp(dev)) {
10264 				rval = EROFS;
10265 				mutex_enter(SD_MUTEX(un));
10266 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10267 				    "write to cd or write protected media\n");
10268 				goto open_fail;
10269 				}
10270 			}
10271 			mutex_enter(SD_MUTEX(un));
10272 		}
10273 	}
10274 
10275 	/*
10276 	 * If opening in NDELAY/NONBLOCK mode, just return.
10277 	 * Check if disk is ready and has a valid geometry later.
10278 	 */
10279 	if (!nodelay) {
10280 		sd_ssc_t	*ssc;
10281 
10282 		mutex_exit(SD_MUTEX(un));
10283 		ssc = sd_ssc_init(un);
10284 		rval = sd_ready_and_valid(ssc, part);
10285 		sd_ssc_fini(ssc);
10286 		mutex_enter(SD_MUTEX(un));
10287 		/*
10288 		 * Fail if device is not ready or if the number of disk
10289 		 * blocks is zero or negative for non CD devices.
10290 		 */
10291 
10292 		nblks = 0;
10293 
10294 		if (rval == SD_READY_VALID && (!ISCD(un))) {
10295 			/* if cmlb_partinfo fails, nblks remains 0 */
10296 			mutex_exit(SD_MUTEX(un));
10297 			(void) cmlb_partinfo(un->un_cmlbhandle, part, &nblks,
10298 			    NULL, NULL, NULL, (void *)SD_PATH_DIRECT);
10299 			mutex_enter(SD_MUTEX(un));
10300 		}
10301 
10302 		if ((rval != SD_READY_VALID) ||
10303 		    (!ISCD(un) && nblks <= 0)) {
10304 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10305 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10306 			    "device not ready or invalid disk block value\n");
10307 			goto open_fail;
10308 		}
10309 #if defined(__i386) || defined(__amd64)
10310 	} else {
10311 		uchar_t *cp;
10312 		/*
10313 		 * x86 requires special nodelay handling, so that p0 is
10314 		 * always defined and accessible.
10315 		 * Invalidate geometry only if device is not already open.
10316 		 */
10317 		cp = &un->un_ocmap.chkd[0];
10318 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10319 			if (*cp != (uchar_t)0) {
10320 				break;
10321 			}
10322 			cp++;
10323 		}
10324 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10325 			mutex_exit(SD_MUTEX(un));
10326 			cmlb_invalidate(un->un_cmlbhandle,
10327 			    (void *)SD_PATH_DIRECT);
10328 			mutex_enter(SD_MUTEX(un));
10329 		}
10330 
10331 #endif
10332 	}
10333 
10334 	if (otyp == OTYP_LYR) {
10335 		un->un_ocmap.lyropen[part]++;
10336 	} else {
10337 		un->un_ocmap.regopen[otyp] |= partmask;
10338 	}
10339 
10340 	/* Set up open and exclusive open flags */
10341 	if (flag & FEXCL) {
10342 		un->un_exclopen |= (partmask);
10343 	}
10344 
10345 	/*
10346 	 * If the lun is EFI labeled and lun capacity is greater than the
10347 	 * capacity contained in the label, log a sys-event to notify the
10348 	 * interested module.
10349 	 * To avoid an infinite loop of logging sys-event, we only log the
10350 	 * event when the lun is not opened in NDELAY mode. The event handler
10351 	 * should open the lun in NDELAY mode.
10352 	 */
10353 	if (!nodelay) {
10354 		mutex_exit(SD_MUTEX(un));
10355 		if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
10356 		    (void*)SD_PATH_DIRECT) == 0) {
10357 			mutex_enter(SD_MUTEX(un));
10358 			if (un->un_f_blockcount_is_valid &&
10359 			    un->un_blockcount > label_cap &&
10360 			    un->un_f_expnevent == B_FALSE) {
10361 				un->un_f_expnevent = B_TRUE;
10362 				mutex_exit(SD_MUTEX(un));
10363 				sd_log_lun_expansion_event(un,
10364 				    (nodelay ? KM_NOSLEEP : KM_SLEEP));
10365 				mutex_enter(SD_MUTEX(un));
10366 			}
10367 		} else {
10368 			mutex_enter(SD_MUTEX(un));
10369 		}
10370 	}
10371 
10372 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10373 	    "open of part %d type %d\n", part, otyp);
10374 
10375 	mutex_exit(SD_MUTEX(un));
10376 	if (!nodelay) {
10377 		sd_pm_exit(un);
10378 	}
10379 
10380 	sema_v(&un->un_semoclose);
10381 
10382 	mutex_enter(&sd_detach_mutex);
10383 	un->un_opens_in_progress--;
10384 	mutex_exit(&sd_detach_mutex);
10385 
10386 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10387 	return (DDI_SUCCESS);
10388 
10389 excl_open_fail:
10390 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10391 	rval = EBUSY;
10392 
10393 open_fail:
10394 	mutex_exit(SD_MUTEX(un));
10395 
10396 	/*
10397 	 * On a failed open we must exit the pm management.
10398 	 */
10399 	if (!nodelay) {
10400 		sd_pm_exit(un);
10401 	}
10402 open_failed_with_pm:
10403 	sema_v(&un->un_semoclose);
10404 
10405 	mutex_enter(&sd_detach_mutex);
10406 	un->un_opens_in_progress--;
10407 	if (otyp == OTYP_LYR) {
10408 		un->un_layer_count--;
10409 	}
10410 	mutex_exit(&sd_detach_mutex);
10411 
10412 	return (rval);
10413 }
10414 
10415 
10416 /*
10417  *    Function: sdclose
10418  *
10419  * Description: Driver's close(9e) entry point function.
10420  *
10421  *   Arguments: dev    - device number
10422  *		flag   - file status flag, informational only
10423  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10424  *		cred_p - user credential pointer
10425  *
10426  * Return Code: ENXIO
10427  *
10428  *     Context: Kernel thread context
10429  */
10430 /* ARGSUSED */
10431 static int
10432 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10433 {
10434 	struct sd_lun	*un;
10435 	uchar_t		*cp;
10436 	int		part;
10437 	int		nodelay;
10438 	int		rval = 0;
10439 
10440 	/* Validate the open type */
10441 	if (otyp >= OTYPCNT) {
10442 		return (ENXIO);
10443 	}
10444 
10445 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10446 		return (ENXIO);
10447 	}
10448 
10449 	part = SDPART(dev);
10450 	nodelay = flag & (FNDELAY | FNONBLOCK);
10451 
10452 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10453 	    "sdclose: close of part %d type %d\n", part, otyp);
10454 
10455 	/*
10456 	 * We use a semaphore here in order to serialize
10457 	 * open and close requests on the device.
10458 	 */
10459 	sema_p(&un->un_semoclose);
10460 
10461 	mutex_enter(SD_MUTEX(un));
10462 
10463 	/* Don't proceed if power is being changed. */
10464 	while (un->un_state == SD_STATE_PM_CHANGING) {
10465 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10466 	}
10467 
10468 	if (un->un_exclopen & (1 << part)) {
10469 		un->un_exclopen &= ~(1 << part);
10470 	}
10471 
10472 	/* Update the open partition map */
10473 	if (otyp == OTYP_LYR) {
10474 		un->un_ocmap.lyropen[part] -= 1;
10475 	} else {
10476 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10477 	}
10478 
10479 	cp = &un->un_ocmap.chkd[0];
10480 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10481 		if (*cp != NULL) {
10482 			break;
10483 		}
10484 		cp++;
10485 	}
10486 
10487 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10488 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10489 
10490 		/*
10491 		 * We avoid persistance upon the last close, and set
10492 		 * the throttle back to the maximum.
10493 		 */
10494 		un->un_throttle = un->un_saved_throttle;
10495 
10496 		if (un->un_state == SD_STATE_OFFLINE) {
10497 			if (un->un_f_is_fibre == FALSE) {
10498 				scsi_log(SD_DEVINFO(un), sd_label,
10499 				    CE_WARN, "offline\n");
10500 			}
10501 			mutex_exit(SD_MUTEX(un));
10502 			cmlb_invalidate(un->un_cmlbhandle,
10503 			    (void *)SD_PATH_DIRECT);
10504 			mutex_enter(SD_MUTEX(un));
10505 
10506 		} else {
10507 			/*
10508 			 * Flush any outstanding writes in NVRAM cache.
10509 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10510 			 * cmd, it may not work for non-Pluto devices.
10511 			 * SYNCHRONIZE CACHE is not required for removables,
10512 			 * except DVD-RAM drives.
10513 			 *
10514 			 * Also note: because SYNCHRONIZE CACHE is currently
10515 			 * the only command issued here that requires the
10516 			 * drive be powered up, only do the power up before
10517 			 * sending the Sync Cache command. If additional
10518 			 * commands are added which require a powered up
10519 			 * drive, the following sequence may have to change.
10520 			 *
10521 			 * And finally, note that parallel SCSI on SPARC
10522 			 * only issues a Sync Cache to DVD-RAM, a newly
10523 			 * supported device.
10524 			 */
10525 #if defined(__i386) || defined(__amd64)
10526 			if ((un->un_f_sync_cache_supported &&
10527 			    un->un_f_sync_cache_required) ||
10528 			    un->un_f_dvdram_writable_device == TRUE) {
10529 #else
10530 			if (un->un_f_dvdram_writable_device == TRUE) {
10531 #endif
10532 				mutex_exit(SD_MUTEX(un));
10533 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10534 					rval =
10535 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10536 					    NULL);
10537 					/* ignore error if not supported */
10538 					if (rval == ENOTSUP) {
10539 						rval = 0;
10540 					} else if (rval != 0) {
10541 						rval = EIO;
10542 					}
10543 					sd_pm_exit(un);
10544 				} else {
10545 					rval = EIO;
10546 				}
10547 				mutex_enter(SD_MUTEX(un));
10548 			}
10549 
10550 			/*
10551 			 * For devices which supports DOOR_LOCK, send an ALLOW
10552 			 * MEDIA REMOVAL command, but don't get upset if it
10553 			 * fails. We need to raise the power of the drive before
10554 			 * we can call sd_send_scsi_DOORLOCK()
10555 			 */
10556 			if (un->un_f_doorlock_supported) {
10557 				mutex_exit(SD_MUTEX(un));
10558 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10559 					sd_ssc_t	*ssc;
10560 
10561 					ssc = sd_ssc_init(un);
10562 					rval = sd_send_scsi_DOORLOCK(ssc,
10563 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10564 					if (rval != 0)
10565 						sd_ssc_assessment(ssc,
10566 						    SD_FMT_IGNORE);
10567 					sd_ssc_fini(ssc);
10568 
10569 					sd_pm_exit(un);
10570 					if (ISCD(un) && (rval != 0) &&
10571 					    (nodelay != 0)) {
10572 						rval = ENXIO;
10573 					}
10574 				} else {
10575 					rval = EIO;
10576 				}
10577 				mutex_enter(SD_MUTEX(un));
10578 			}
10579 
10580 			/*
10581 			 * If a device has removable media, invalidate all
10582 			 * parameters related to media, such as geometry,
10583 			 * blocksize, and blockcount.
10584 			 */
10585 			if (un->un_f_has_removable_media) {
10586 				sr_ejected(un);
10587 			}
10588 
10589 			/*
10590 			 * Destroy the cache (if it exists) which was
10591 			 * allocated for the write maps since this is
10592 			 * the last close for this media.
10593 			 */
10594 			if (un->un_wm_cache) {
10595 				/*
10596 				 * Check if there are pending commands.
10597 				 * and if there are give a warning and
10598 				 * do not destroy the cache.
10599 				 */
10600 				if (un->un_ncmds_in_driver > 0) {
10601 					scsi_log(SD_DEVINFO(un),
10602 					    sd_label, CE_WARN,
10603 					    "Unable to clean up memory "
10604 					    "because of pending I/O\n");
10605 				} else {
10606 					kmem_cache_destroy(
10607 					    un->un_wm_cache);
10608 					un->un_wm_cache = NULL;
10609 				}
10610 			}
10611 		}
10612 	}
10613 
10614 	mutex_exit(SD_MUTEX(un));
10615 	sema_v(&un->un_semoclose);
10616 
10617 	if (otyp == OTYP_LYR) {
10618 		mutex_enter(&sd_detach_mutex);
10619 		/*
10620 		 * The detach routine may run when the layer count
10621 		 * drops to zero.
10622 		 */
10623 		un->un_layer_count--;
10624 		mutex_exit(&sd_detach_mutex);
10625 	}
10626 
10627 	return (rval);
10628 }
10629 
10630 
10631 /*
10632  *    Function: sd_ready_and_valid
10633  *
10634  * Description: Test if device is ready and has a valid geometry.
10635  *
10636  *   Arguments: ssc - sd_ssc_t will contain un
10637  *		un  - driver soft state (unit) structure
10638  *
10639  * Return Code: SD_READY_VALID		ready and valid label
10640  *		SD_NOT_READY_VALID	not ready, no label
10641  *		SD_RESERVED_BY_OTHERS	reservation conflict
10642  *
10643  *     Context: Never called at interrupt context.
10644  */
10645 
10646 static int
10647 sd_ready_and_valid(sd_ssc_t *ssc, int part)
10648 {
10649 	struct sd_errstats	*stp;
10650 	uint64_t		capacity;
10651 	uint_t			lbasize;
10652 	int			rval = SD_READY_VALID;
10653 	char			name_str[48];
10654 	boolean_t		is_valid;
10655 	struct sd_lun		*un;
10656 	int			status;
10657 
10658 	ASSERT(ssc != NULL);
10659 	un = ssc->ssc_un;
10660 	ASSERT(un != NULL);
10661 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10662 
10663 	mutex_enter(SD_MUTEX(un));
10664 	/*
10665 	 * If a device has removable media, we must check if media is
10666 	 * ready when checking if this device is ready and valid.
10667 	 */
10668 	if (un->un_f_has_removable_media) {
10669 		mutex_exit(SD_MUTEX(un));
10670 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10671 
10672 		if (status != 0) {
10673 			rval = SD_NOT_READY_VALID;
10674 			mutex_enter(SD_MUTEX(un));
10675 
10676 			/* Ignore all failed status for removalbe media */
10677 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10678 
10679 			goto done;
10680 		}
10681 
10682 		is_valid = SD_IS_VALID_LABEL(un);
10683 		mutex_enter(SD_MUTEX(un));
10684 		if (!is_valid ||
10685 		    (un->un_f_blockcount_is_valid == FALSE) ||
10686 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10687 
10688 			/* capacity has to be read every open. */
10689 			mutex_exit(SD_MUTEX(un));
10690 			status = sd_send_scsi_READ_CAPACITY(ssc, &capacity,
10691 			    &lbasize, SD_PATH_DIRECT);
10692 
10693 			if (status != 0) {
10694 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10695 
10696 				cmlb_invalidate(un->un_cmlbhandle,
10697 				    (void *)SD_PATH_DIRECT);
10698 				mutex_enter(SD_MUTEX(un));
10699 				rval = SD_NOT_READY_VALID;
10700 
10701 				goto done;
10702 			} else {
10703 				mutex_enter(SD_MUTEX(un));
10704 				sd_update_block_info(un, lbasize, capacity);
10705 			}
10706 		}
10707 
10708 		/*
10709 		 * Check if the media in the device is writable or not.
10710 		 */
10711 		if (!is_valid && ISCD(un)) {
10712 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
10713 		}
10714 
10715 	} else {
10716 		/*
10717 		 * Do a test unit ready to clear any unit attention from non-cd
10718 		 * devices.
10719 		 */
10720 		mutex_exit(SD_MUTEX(un));
10721 
10722 		status = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10723 		if (status != 0) {
10724 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10725 		}
10726 
10727 		mutex_enter(SD_MUTEX(un));
10728 	}
10729 
10730 
10731 	/*
10732 	 * If this is a non 512 block device, allocate space for
10733 	 * the wmap cache. This is being done here since every time
10734 	 * a media is changed this routine will be called and the
10735 	 * block size is a function of media rather than device.
10736 	 */
10737 	if (((un->un_f_rmw_type != SD_RMW_TYPE_RETURN_ERROR ||
10738 	    un->un_f_non_devbsize_supported) &&
10739 	    un->un_tgt_blocksize != DEV_BSIZE) ||
10740 	    un->un_f_enable_rmw) {
10741 		if (!(un->un_wm_cache)) {
10742 			(void) snprintf(name_str, sizeof (name_str),
10743 			    "%s%d_cache",
10744 			    ddi_driver_name(SD_DEVINFO(un)),
10745 			    ddi_get_instance(SD_DEVINFO(un)));
10746 			un->un_wm_cache = kmem_cache_create(
10747 			    name_str, sizeof (struct sd_w_map),
10748 			    8, sd_wm_cache_constructor,
10749 			    sd_wm_cache_destructor, NULL,
10750 			    (void *)un, NULL, 0);
10751 			if (!(un->un_wm_cache)) {
10752 				rval = ENOMEM;
10753 				goto done;
10754 			}
10755 		}
10756 	}
10757 
10758 	if (un->un_state == SD_STATE_NORMAL) {
10759 		/*
10760 		 * If the target is not yet ready here (defined by a TUR
10761 		 * failure), invalidate the geometry and print an 'offline'
10762 		 * message. This is a legacy message, as the state of the
10763 		 * target is not actually changed to SD_STATE_OFFLINE.
10764 		 *
10765 		 * If the TUR fails for EACCES (Reservation Conflict),
10766 		 * SD_RESERVED_BY_OTHERS will be returned to indicate
10767 		 * reservation conflict. If the TUR fails for other
10768 		 * reasons, SD_NOT_READY_VALID will be returned.
10769 		 */
10770 		int err;
10771 
10772 		mutex_exit(SD_MUTEX(un));
10773 		err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
10774 		mutex_enter(SD_MUTEX(un));
10775 
10776 		if (err != 0) {
10777 			mutex_exit(SD_MUTEX(un));
10778 			cmlb_invalidate(un->un_cmlbhandle,
10779 			    (void *)SD_PATH_DIRECT);
10780 			mutex_enter(SD_MUTEX(un));
10781 			if (err == EACCES) {
10782 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10783 				    "reservation conflict\n");
10784 				rval = SD_RESERVED_BY_OTHERS;
10785 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10786 			} else {
10787 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10788 				    "drive offline\n");
10789 				rval = SD_NOT_READY_VALID;
10790 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
10791 			}
10792 			goto done;
10793 		}
10794 	}
10795 
10796 	if (un->un_f_format_in_progress == FALSE) {
10797 		mutex_exit(SD_MUTEX(un));
10798 
10799 		(void) cmlb_validate(un->un_cmlbhandle, 0,
10800 		    (void *)SD_PATH_DIRECT);
10801 		if (cmlb_partinfo(un->un_cmlbhandle, part, NULL, NULL, NULL,
10802 		    NULL, (void *) SD_PATH_DIRECT) != 0) {
10803 			rval = SD_NOT_READY_VALID;
10804 			mutex_enter(SD_MUTEX(un));
10805 
10806 			goto done;
10807 		}
10808 		if (un->un_f_pkstats_enabled) {
10809 			sd_set_pstats(un);
10810 			SD_TRACE(SD_LOG_IO_PARTITION, un,
10811 			    "sd_ready_and_valid: un:0x%p pstats created and "
10812 			    "set\n", un);
10813 		}
10814 		mutex_enter(SD_MUTEX(un));
10815 	}
10816 
10817 	/*
10818 	 * If this device supports DOOR_LOCK command, try and send
10819 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10820 	 * if it fails. For a CD, however, it is an error
10821 	 */
10822 	if (un->un_f_doorlock_supported) {
10823 		mutex_exit(SD_MUTEX(un));
10824 		status = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
10825 		    SD_PATH_DIRECT);
10826 
10827 		if ((status != 0) && ISCD(un)) {
10828 			rval = SD_NOT_READY_VALID;
10829 			mutex_enter(SD_MUTEX(un));
10830 
10831 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10832 
10833 			goto done;
10834 		} else if (status != 0)
10835 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
10836 		mutex_enter(SD_MUTEX(un));
10837 	}
10838 
10839 	/* The state has changed, inform the media watch routines */
10840 	un->un_mediastate = DKIO_INSERTED;
10841 	cv_broadcast(&un->un_state_cv);
10842 	rval = SD_READY_VALID;
10843 
10844 done:
10845 
10846 	/*
10847 	 * Initialize the capacity kstat value, if no media previously
10848 	 * (capacity kstat is 0) and a media has been inserted
10849 	 * (un_blockcount > 0).
10850 	 */
10851 	if (un->un_errstats != NULL) {
10852 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10853 		if ((stp->sd_capacity.value.ui64 == 0) &&
10854 		    (un->un_f_blockcount_is_valid == TRUE)) {
10855 			stp->sd_capacity.value.ui64 =
10856 			    (uint64_t)((uint64_t)un->un_blockcount *
10857 			    un->un_sys_blocksize);
10858 		}
10859 	}
10860 
10861 	mutex_exit(SD_MUTEX(un));
10862 	return (rval);
10863 }
10864 
10865 
10866 /*
10867  *    Function: sdmin
10868  *
10869  * Description: Routine to limit the size of a data transfer. Used in
10870  *		conjunction with physio(9F).
10871  *
10872  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10873  *
10874  *     Context: Kernel thread context.
10875  */
10876 
10877 static void
10878 sdmin(struct buf *bp)
10879 {
10880 	struct sd_lun	*un;
10881 	int		instance;
10882 
10883 	instance = SDUNIT(bp->b_edev);
10884 
10885 	un = ddi_get_soft_state(sd_state, instance);
10886 	ASSERT(un != NULL);
10887 
10888 	/*
10889 	 * We depend on buf breakup to restrict
10890 	 * IO size if it is enabled.
10891 	 */
10892 	if (un->un_buf_breakup_supported) {
10893 		return;
10894 	}
10895 
10896 	if (bp->b_bcount > un->un_max_xfer_size) {
10897 		bp->b_bcount = un->un_max_xfer_size;
10898 	}
10899 }
10900 
10901 
10902 /*
10903  *    Function: sdread
10904  *
10905  * Description: Driver's read(9e) entry point function.
10906  *
10907  *   Arguments: dev   - device number
10908  *		uio   - structure pointer describing where data is to be stored
10909  *			in user's space
10910  *		cred_p  - user credential pointer
10911  *
10912  * Return Code: ENXIO
10913  *		EIO
10914  *		EINVAL
10915  *		value returned by physio
10916  *
10917  *     Context: Kernel thread context.
10918  */
10919 /* ARGSUSED */
10920 static int
10921 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10922 {
10923 	struct sd_lun	*un = NULL;
10924 	int		secmask;
10925 	int		err = 0;
10926 	sd_ssc_t	*ssc;
10927 
10928 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10929 		return (ENXIO);
10930 	}
10931 
10932 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10933 
10934 
10935 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
10936 		mutex_enter(SD_MUTEX(un));
10937 		/*
10938 		 * Because the call to sd_ready_and_valid will issue I/O we
10939 		 * must wait here if either the device is suspended or
10940 		 * if it's power level is changing.
10941 		 */
10942 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10943 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10944 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10945 		}
10946 		un->un_ncmds_in_driver++;
10947 		mutex_exit(SD_MUTEX(un));
10948 
10949 		/* Initialize sd_ssc_t for internal uscsi commands */
10950 		ssc = sd_ssc_init(un);
10951 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
10952 			err = EIO;
10953 		} else {
10954 			err = 0;
10955 		}
10956 		sd_ssc_fini(ssc);
10957 
10958 		mutex_enter(SD_MUTEX(un));
10959 		un->un_ncmds_in_driver--;
10960 		ASSERT(un->un_ncmds_in_driver >= 0);
10961 		mutex_exit(SD_MUTEX(un));
10962 		if (err != 0)
10963 			return (err);
10964 	}
10965 
10966 	/*
10967 	 * Read requests are restricted to multiples of the system block size.
10968 	 */
10969 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
10970 	    !un->un_f_enable_rmw)
10971 		secmask = un->un_tgt_blocksize - 1;
10972 	else
10973 		secmask = DEV_BSIZE - 1;
10974 
10975 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10976 		SD_ERROR(SD_LOG_READ_WRITE, un,
10977 		    "sdread: file offset not modulo %d\n",
10978 		    secmask + 1);
10979 		err = EINVAL;
10980 	} else if (uio->uio_iov->iov_len & (secmask)) {
10981 		SD_ERROR(SD_LOG_READ_WRITE, un,
10982 		    "sdread: transfer length not modulo %d\n",
10983 		    secmask + 1);
10984 		err = EINVAL;
10985 	} else {
10986 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10987 	}
10988 
10989 	return (err);
10990 }
10991 
10992 
10993 /*
10994  *    Function: sdwrite
10995  *
10996  * Description: Driver's write(9e) entry point function.
10997  *
10998  *   Arguments: dev   - device number
10999  *		uio   - structure pointer describing where data is stored in
11000  *			user's space
11001  *		cred_p  - user credential pointer
11002  *
11003  * Return Code: ENXIO
11004  *		EIO
11005  *		EINVAL
11006  *		value returned by physio
11007  *
11008  *     Context: Kernel thread context.
11009  */
11010 /* ARGSUSED */
11011 static int
11012 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
11013 {
11014 	struct sd_lun	*un = NULL;
11015 	int		secmask;
11016 	int		err = 0;
11017 	sd_ssc_t	*ssc;
11018 
11019 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11020 		return (ENXIO);
11021 	}
11022 
11023 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11024 
11025 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11026 		mutex_enter(SD_MUTEX(un));
11027 		/*
11028 		 * Because the call to sd_ready_and_valid will issue I/O we
11029 		 * must wait here if either the device is suspended or
11030 		 * if it's power level is changing.
11031 		 */
11032 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11033 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11034 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11035 		}
11036 		un->un_ncmds_in_driver++;
11037 		mutex_exit(SD_MUTEX(un));
11038 
11039 		/* Initialize sd_ssc_t for internal uscsi commands */
11040 		ssc = sd_ssc_init(un);
11041 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11042 			err = EIO;
11043 		} else {
11044 			err = 0;
11045 		}
11046 		sd_ssc_fini(ssc);
11047 
11048 		mutex_enter(SD_MUTEX(un));
11049 		un->un_ncmds_in_driver--;
11050 		ASSERT(un->un_ncmds_in_driver >= 0);
11051 		mutex_exit(SD_MUTEX(un));
11052 		if (err != 0)
11053 			return (err);
11054 	}
11055 
11056 	/*
11057 	 * Write requests are restricted to multiples of the system block size.
11058 	 */
11059 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11060 	    !un->un_f_enable_rmw)
11061 		secmask = un->un_tgt_blocksize - 1;
11062 	else
11063 		secmask = DEV_BSIZE - 1;
11064 
11065 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11066 		SD_ERROR(SD_LOG_READ_WRITE, un,
11067 		    "sdwrite: file offset not modulo %d\n",
11068 		    secmask + 1);
11069 		err = EINVAL;
11070 	} else if (uio->uio_iov->iov_len & (secmask)) {
11071 		SD_ERROR(SD_LOG_READ_WRITE, un,
11072 		    "sdwrite: transfer length not modulo %d\n",
11073 		    secmask + 1);
11074 		err = EINVAL;
11075 	} else {
11076 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
11077 	}
11078 
11079 	return (err);
11080 }
11081 
11082 
11083 /*
11084  *    Function: sdaread
11085  *
11086  * Description: Driver's aread(9e) entry point function.
11087  *
11088  *   Arguments: dev   - device number
11089  *		aio   - structure pointer describing where data is to be stored
11090  *		cred_p  - user credential pointer
11091  *
11092  * Return Code: ENXIO
11093  *		EIO
11094  *		EINVAL
11095  *		value returned by aphysio
11096  *
11097  *     Context: Kernel thread context.
11098  */
11099 /* ARGSUSED */
11100 static int
11101 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11102 {
11103 	struct sd_lun	*un = NULL;
11104 	struct uio	*uio = aio->aio_uio;
11105 	int		secmask;
11106 	int		err = 0;
11107 	sd_ssc_t	*ssc;
11108 
11109 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11110 		return (ENXIO);
11111 	}
11112 
11113 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11114 
11115 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11116 		mutex_enter(SD_MUTEX(un));
11117 		/*
11118 		 * Because the call to sd_ready_and_valid will issue I/O we
11119 		 * must wait here if either the device is suspended or
11120 		 * if it's power level is changing.
11121 		 */
11122 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11123 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11124 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11125 		}
11126 		un->un_ncmds_in_driver++;
11127 		mutex_exit(SD_MUTEX(un));
11128 
11129 		/* Initialize sd_ssc_t for internal uscsi commands */
11130 		ssc = sd_ssc_init(un);
11131 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11132 			err = EIO;
11133 		} else {
11134 			err = 0;
11135 		}
11136 		sd_ssc_fini(ssc);
11137 
11138 		mutex_enter(SD_MUTEX(un));
11139 		un->un_ncmds_in_driver--;
11140 		ASSERT(un->un_ncmds_in_driver >= 0);
11141 		mutex_exit(SD_MUTEX(un));
11142 		if (err != 0)
11143 			return (err);
11144 	}
11145 
11146 	/*
11147 	 * Read requests are restricted to multiples of the system block size.
11148 	 */
11149 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11150 	    !un->un_f_enable_rmw)
11151 		secmask = un->un_tgt_blocksize - 1;
11152 	else
11153 		secmask = DEV_BSIZE - 1;
11154 
11155 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11156 		SD_ERROR(SD_LOG_READ_WRITE, un,
11157 		    "sdaread: file offset not modulo %d\n",
11158 		    secmask + 1);
11159 		err = EINVAL;
11160 	} else if (uio->uio_iov->iov_len & (secmask)) {
11161 		SD_ERROR(SD_LOG_READ_WRITE, un,
11162 		    "sdaread: transfer length not modulo %d\n",
11163 		    secmask + 1);
11164 		err = EINVAL;
11165 	} else {
11166 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11167 	}
11168 
11169 	return (err);
11170 }
11171 
11172 
11173 /*
11174  *    Function: sdawrite
11175  *
11176  * Description: Driver's awrite(9e) entry point function.
11177  *
11178  *   Arguments: dev   - device number
11179  *		aio   - structure pointer describing where data is stored
11180  *		cred_p  - user credential pointer
11181  *
11182  * Return Code: ENXIO
11183  *		EIO
11184  *		EINVAL
11185  *		value returned by aphysio
11186  *
11187  *     Context: Kernel thread context.
11188  */
11189 /* ARGSUSED */
11190 static int
11191 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11192 {
11193 	struct sd_lun	*un = NULL;
11194 	struct uio	*uio = aio->aio_uio;
11195 	int		secmask;
11196 	int		err = 0;
11197 	sd_ssc_t	*ssc;
11198 
11199 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11200 		return (ENXIO);
11201 	}
11202 
11203 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11204 
11205 	if (!SD_IS_VALID_LABEL(un) && !ISCD(un)) {
11206 		mutex_enter(SD_MUTEX(un));
11207 		/*
11208 		 * Because the call to sd_ready_and_valid will issue I/O we
11209 		 * must wait here if either the device is suspended or
11210 		 * if it's power level is changing.
11211 		 */
11212 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11213 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11214 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11215 		}
11216 		un->un_ncmds_in_driver++;
11217 		mutex_exit(SD_MUTEX(un));
11218 
11219 		/* Initialize sd_ssc_t for internal uscsi commands */
11220 		ssc = sd_ssc_init(un);
11221 		if ((sd_ready_and_valid(ssc, SDPART(dev))) != SD_READY_VALID) {
11222 			err = EIO;
11223 		} else {
11224 			err = 0;
11225 		}
11226 		sd_ssc_fini(ssc);
11227 
11228 		mutex_enter(SD_MUTEX(un));
11229 		un->un_ncmds_in_driver--;
11230 		ASSERT(un->un_ncmds_in_driver >= 0);
11231 		mutex_exit(SD_MUTEX(un));
11232 		if (err != 0)
11233 			return (err);
11234 	}
11235 
11236 	/*
11237 	 * Write requests are restricted to multiples of the system block size.
11238 	 */
11239 	if (un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR &&
11240 	    !un->un_f_enable_rmw)
11241 		secmask = un->un_tgt_blocksize - 1;
11242 	else
11243 		secmask = DEV_BSIZE - 1;
11244 
11245 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11246 		SD_ERROR(SD_LOG_READ_WRITE, un,
11247 		    "sdawrite: file offset not modulo %d\n",
11248 		    secmask + 1);
11249 		err = EINVAL;
11250 	} else if (uio->uio_iov->iov_len & (secmask)) {
11251 		SD_ERROR(SD_LOG_READ_WRITE, un,
11252 		    "sdawrite: transfer length not modulo %d\n",
11253 		    secmask + 1);
11254 		err = EINVAL;
11255 	} else {
11256 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11257 	}
11258 
11259 	return (err);
11260 }
11261 
11262 
11263 
11264 
11265 
11266 /*
11267  * Driver IO processing follows the following sequence:
11268  *
11269  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11270  *         |                |                     ^
11271  *         v                v                     |
11272  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11273  *         |                |                     |                   |
11274  *         v                |                     |                   |
11275  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11276  *         |                |                     ^                   ^
11277  *         v                v                     |                   |
11278  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11279  *         |                |                     |                   |
11280  *     +---+                |                     +------------+      +-------+
11281  *     |                    |                                  |              |
11282  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11283  *     |                    v                                  |              |
11284  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11285  *     |                    |                                  ^              |
11286  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11287  *     |                    v                                  |              |
11288  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11289  *     |                    |                                  ^              |
11290  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11291  *     |                    v                                  |              |
11292  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11293  *     |                    |                                  ^              |
11294  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11295  *     |                    v                                  |              |
11296  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11297  *     |                    |                                  ^              |
11298  *     |                    |                                  |              |
11299  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11300  *                          |                           ^
11301  *                          v                           |
11302  *                   sd_core_iostart()                  |
11303  *                          |                           |
11304  *                          |                           +------>(*destroypkt)()
11305  *                          +-> sd_start_cmds() <-+     |           |
11306  *                          |                     |     |           v
11307  *                          |                     |     |  scsi_destroy_pkt(9F)
11308  *                          |                     |     |
11309  *                          +->(*initpkt)()       +- sdintr()
11310  *                          |  |                        |  |
11311  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11312  *                          |  +-> scsi_setup_cdb(9F)   |
11313  *                          |                           |
11314  *                          +--> scsi_transport(9F)     |
11315  *                                     |                |
11316  *                                     +----> SCSA ---->+
11317  *
11318  *
11319  * This code is based upon the following presumptions:
11320  *
11321  *   - iostart and iodone functions operate on buf(9S) structures. These
11322  *     functions perform the necessary operations on the buf(9S) and pass
11323  *     them along to the next function in the chain by using the macros
11324  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11325  *     (for iodone side functions).
11326  *
11327  *   - The iostart side functions may sleep. The iodone side functions
11328  *     are called under interrupt context and may NOT sleep. Therefore
11329  *     iodone side functions also may not call iostart side functions.
11330  *     (NOTE: iostart side functions should NOT sleep for memory, as
11331  *     this could result in deadlock.)
11332  *
11333  *   - An iostart side function may call its corresponding iodone side
11334  *     function directly (if necessary).
11335  *
11336  *   - In the event of an error, an iostart side function can return a buf(9S)
11337  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11338  *     b_error in the usual way of course).
11339  *
11340  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11341  *     requests to the iostart side functions.  The iostart side functions in
11342  *     this case would be called under the context of a taskq thread, so it's
11343  *     OK for them to block/sleep/spin in this case.
11344  *
11345  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11346  *     pass them along to the next function in the chain.  The corresponding
11347  *     iodone side functions must coalesce the "shadow" bufs and return
11348  *     the "original" buf to the next higher layer.
11349  *
11350  *   - The b_private field of the buf(9S) struct holds a pointer to
11351  *     an sd_xbuf struct, which contains information needed to
11352  *     construct the scsi_pkt for the command.
11353  *
11354  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11355  *     layer must acquire & release the SD_MUTEX(un) as needed.
11356  */
11357 
11358 
11359 /*
11360  * Create taskq for all targets in the system. This is created at
11361  * _init(9E) and destroyed at _fini(9E).
11362  *
11363  * Note: here we set the minalloc to a reasonably high number to ensure that
11364  * we will have an adequate supply of task entries available at interrupt time.
11365  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11366  * sd_create_taskq().  Since we do not want to sleep for allocations at
11367  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11368  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11369  * requests any one instant in time.
11370  */
11371 #define	SD_TASKQ_NUMTHREADS	8
11372 #define	SD_TASKQ_MINALLOC	256
11373 #define	SD_TASKQ_MAXALLOC	256
11374 
11375 static taskq_t	*sd_tq = NULL;
11376 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11377 
11378 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11379 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11380 
11381 /*
11382  * The following task queue is being created for the write part of
11383  * read-modify-write of non-512 block size devices.
11384  * Limit the number of threads to 1 for now. This number has been chosen
11385  * considering the fact that it applies only to dvd ram drives/MO drives
11386  * currently. Performance for which is not main criteria at this stage.
11387  * Note: It needs to be explored if we can use a single taskq in future
11388  */
11389 #define	SD_WMR_TASKQ_NUMTHREADS	1
11390 static taskq_t	*sd_wmr_tq = NULL;
11391 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11392 
11393 /*
11394  *    Function: sd_taskq_create
11395  *
11396  * Description: Create taskq thread(s) and preallocate task entries
11397  *
11398  * Return Code: Returns a pointer to the allocated taskq_t.
11399  *
11400  *     Context: Can sleep. Requires blockable context.
11401  *
11402  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11403  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11404  *		- taskq_create() will block for memory, also it will panic
11405  *		  if it cannot create the requested number of threads.
11406  *		- Currently taskq_create() creates threads that cannot be
11407  *		  swapped.
11408  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11409  *		  supply of taskq entries at interrupt time (ie, so that we
11410  *		  do not have to sleep for memory)
11411  */
11412 
11413 static void
11414 sd_taskq_create(void)
11415 {
11416 	char	taskq_name[TASKQ_NAMELEN];
11417 
11418 	ASSERT(sd_tq == NULL);
11419 	ASSERT(sd_wmr_tq == NULL);
11420 
11421 	(void) snprintf(taskq_name, sizeof (taskq_name),
11422 	    "%s_drv_taskq", sd_label);
11423 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11424 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11425 	    TASKQ_PREPOPULATE));
11426 
11427 	(void) snprintf(taskq_name, sizeof (taskq_name),
11428 	    "%s_rmw_taskq", sd_label);
11429 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11430 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11431 	    TASKQ_PREPOPULATE));
11432 }
11433 
11434 
11435 /*
11436  *    Function: sd_taskq_delete
11437  *
11438  * Description: Complementary cleanup routine for sd_taskq_create().
11439  *
11440  *     Context: Kernel thread context.
11441  */
11442 
11443 static void
11444 sd_taskq_delete(void)
11445 {
11446 	ASSERT(sd_tq != NULL);
11447 	ASSERT(sd_wmr_tq != NULL);
11448 	taskq_destroy(sd_tq);
11449 	taskq_destroy(sd_wmr_tq);
11450 	sd_tq = NULL;
11451 	sd_wmr_tq = NULL;
11452 }
11453 
11454 
11455 /*
11456  *    Function: sdstrategy
11457  *
11458  * Description: Driver's strategy (9E) entry point function.
11459  *
11460  *   Arguments: bp - pointer to buf(9S)
11461  *
11462  * Return Code: Always returns zero
11463  *
11464  *     Context: Kernel thread context.
11465  */
11466 
11467 static int
11468 sdstrategy(struct buf *bp)
11469 {
11470 	struct sd_lun *un;
11471 
11472 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11473 	if (un == NULL) {
11474 		bioerror(bp, EIO);
11475 		bp->b_resid = bp->b_bcount;
11476 		biodone(bp);
11477 		return (0);
11478 	}
11479 
11480 	/* As was done in the past, fail new cmds. if state is dumping. */
11481 	if (un->un_state == SD_STATE_DUMPING) {
11482 		bioerror(bp, ENXIO);
11483 		bp->b_resid = bp->b_bcount;
11484 		biodone(bp);
11485 		return (0);
11486 	}
11487 
11488 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11489 
11490 	/*
11491 	 * Commands may sneak in while we released the mutex in
11492 	 * DDI_SUSPEND, we should block new commands. However, old
11493 	 * commands that are still in the driver at this point should
11494 	 * still be allowed to drain.
11495 	 */
11496 	mutex_enter(SD_MUTEX(un));
11497 	/*
11498 	 * Must wait here if either the device is suspended or
11499 	 * if it's power level is changing.
11500 	 */
11501 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11502 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11503 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11504 	}
11505 
11506 	un->un_ncmds_in_driver++;
11507 
11508 	/*
11509 	 * atapi: Since we are running the CD for now in PIO mode we need to
11510 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11511 	 * the HBA's init_pkt routine.
11512 	 */
11513 	if (un->un_f_cfg_is_atapi == TRUE) {
11514 		mutex_exit(SD_MUTEX(un));
11515 		bp_mapin(bp);
11516 		mutex_enter(SD_MUTEX(un));
11517 	}
11518 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11519 	    un->un_ncmds_in_driver);
11520 
11521 	if (bp->b_flags & B_WRITE)
11522 		un->un_f_sync_cache_required = TRUE;
11523 
11524 	mutex_exit(SD_MUTEX(un));
11525 
11526 	/*
11527 	 * This will (eventually) allocate the sd_xbuf area and
11528 	 * call sd_xbuf_strategy().  We just want to return the
11529 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11530 	 * imized tail call which saves us a stack frame.
11531 	 */
11532 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11533 }
11534 
11535 
11536 /*
11537  *    Function: sd_xbuf_strategy
11538  *
11539  * Description: Function for initiating IO operations via the
11540  *		ddi_xbuf_qstrategy() mechanism.
11541  *
11542  *     Context: Kernel thread context.
11543  */
11544 
11545 static void
11546 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11547 {
11548 	struct sd_lun *un = arg;
11549 
11550 	ASSERT(bp != NULL);
11551 	ASSERT(xp != NULL);
11552 	ASSERT(un != NULL);
11553 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11554 
11555 	/*
11556 	 * Initialize the fields in the xbuf and save a pointer to the
11557 	 * xbuf in bp->b_private.
11558 	 */
11559 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11560 
11561 	/* Send the buf down the iostart chain */
11562 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11563 }
11564 
11565 
11566 /*
11567  *    Function: sd_xbuf_init
11568  *
11569  * Description: Prepare the given sd_xbuf struct for use.
11570  *
11571  *   Arguments: un - ptr to softstate
11572  *		bp - ptr to associated buf(9S)
11573  *		xp - ptr to associated sd_xbuf
11574  *		chain_type - IO chain type to use:
11575  *			SD_CHAIN_NULL
11576  *			SD_CHAIN_BUFIO
11577  *			SD_CHAIN_USCSI
11578  *			SD_CHAIN_DIRECT
11579  *			SD_CHAIN_DIRECT_PRIORITY
11580  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11581  *			initialization; may be NULL if none.
11582  *
11583  *     Context: Kernel thread context
11584  */
11585 
11586 static void
11587 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11588 	uchar_t chain_type, void *pktinfop)
11589 {
11590 	int index;
11591 
11592 	ASSERT(un != NULL);
11593 	ASSERT(bp != NULL);
11594 	ASSERT(xp != NULL);
11595 
11596 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11597 	    bp, chain_type);
11598 
11599 	xp->xb_un	= un;
11600 	xp->xb_pktp	= NULL;
11601 	xp->xb_pktinfo	= pktinfop;
11602 	xp->xb_private	= bp->b_private;
11603 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11604 
11605 	/*
11606 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11607 	 * upon the specified chain type to use.
11608 	 */
11609 	switch (chain_type) {
11610 	case SD_CHAIN_NULL:
11611 		/*
11612 		 * Fall thru to just use the values for the buf type, even
11613 		 * tho for the NULL chain these values will never be used.
11614 		 */
11615 		/* FALLTHRU */
11616 	case SD_CHAIN_BUFIO:
11617 		index = un->un_buf_chain_type;
11618 		if ((!un->un_f_has_removable_media) &&
11619 		    (un->un_tgt_blocksize != 0) &&
11620 		    (un->un_tgt_blocksize != DEV_BSIZE ||
11621 		    un->un_f_enable_rmw)) {
11622 			int secmask = 0, blknomask = 0;
11623 			if (un->un_f_enable_rmw) {
11624 				blknomask =
11625 				    (un->un_phy_blocksize / DEV_BSIZE) - 1;
11626 				secmask = un->un_phy_blocksize - 1;
11627 			} else {
11628 				blknomask =
11629 				    (un->un_tgt_blocksize / DEV_BSIZE) - 1;
11630 				secmask = un->un_tgt_blocksize - 1;
11631 			}
11632 
11633 			if ((bp->b_lblkno & (blknomask)) ||
11634 			    (bp->b_bcount & (secmask))) {
11635 				if ((un->un_f_rmw_type !=
11636 				    SD_RMW_TYPE_RETURN_ERROR) ||
11637 				    un->un_f_enable_rmw) {
11638 					if (un->un_f_pm_is_enabled == FALSE)
11639 						index =
11640 						    SD_CHAIN_INFO_MSS_DSK_NO_PM;
11641 					else
11642 						index =
11643 						    SD_CHAIN_INFO_MSS_DISK;
11644 				}
11645 			}
11646 		}
11647 		break;
11648 	case SD_CHAIN_USCSI:
11649 		index = un->un_uscsi_chain_type;
11650 		break;
11651 	case SD_CHAIN_DIRECT:
11652 		index = un->un_direct_chain_type;
11653 		break;
11654 	case SD_CHAIN_DIRECT_PRIORITY:
11655 		index = un->un_priority_chain_type;
11656 		break;
11657 	default:
11658 		/* We're really broken if we ever get here... */
11659 		panic("sd_xbuf_init: illegal chain type!");
11660 		/*NOTREACHED*/
11661 	}
11662 
11663 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11664 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11665 
11666 	/*
11667 	 * It might be a bit easier to simply bzero the entire xbuf above,
11668 	 * but it turns out that since we init a fair number of members anyway,
11669 	 * we save a fair number cycles by doing explicit assignment of zero.
11670 	 */
11671 	xp->xb_pkt_flags	= 0;
11672 	xp->xb_dma_resid	= 0;
11673 	xp->xb_retry_count	= 0;
11674 	xp->xb_victim_retry_count = 0;
11675 	xp->xb_ua_retry_count	= 0;
11676 	xp->xb_nr_retry_count	= 0;
11677 	xp->xb_sense_bp		= NULL;
11678 	xp->xb_sense_status	= 0;
11679 	xp->xb_sense_state	= 0;
11680 	xp->xb_sense_resid	= 0;
11681 	xp->xb_ena		= 0;
11682 
11683 	bp->b_private	= xp;
11684 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11685 	bp->b_resid	= 0;
11686 	bp->av_forw	= NULL;
11687 	bp->av_back	= NULL;
11688 	bioerror(bp, 0);
11689 
11690 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11691 }
11692 
11693 
11694 /*
11695  *    Function: sd_uscsi_strategy
11696  *
11697  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11698  *
11699  *   Arguments: bp - buf struct ptr
11700  *
11701  * Return Code: Always returns 0
11702  *
11703  *     Context: Kernel thread context
11704  */
11705 
11706 static int
11707 sd_uscsi_strategy(struct buf *bp)
11708 {
11709 	struct sd_lun		*un;
11710 	struct sd_uscsi_info	*uip;
11711 	struct sd_xbuf		*xp;
11712 	uchar_t			chain_type;
11713 	uchar_t			cmd;
11714 
11715 	ASSERT(bp != NULL);
11716 
11717 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11718 	if (un == NULL) {
11719 		bioerror(bp, EIO);
11720 		bp->b_resid = bp->b_bcount;
11721 		biodone(bp);
11722 		return (0);
11723 	}
11724 
11725 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11726 
11727 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11728 
11729 	/*
11730 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11731 	 */
11732 	ASSERT(bp->b_private != NULL);
11733 	uip = (struct sd_uscsi_info *)bp->b_private;
11734 	cmd = ((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_cdb[0];
11735 
11736 	mutex_enter(SD_MUTEX(un));
11737 	/*
11738 	 * atapi: Since we are running the CD for now in PIO mode we need to
11739 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11740 	 * the HBA's init_pkt routine.
11741 	 */
11742 	if (un->un_f_cfg_is_atapi == TRUE) {
11743 		mutex_exit(SD_MUTEX(un));
11744 		bp_mapin(bp);
11745 		mutex_enter(SD_MUTEX(un));
11746 	}
11747 	un->un_ncmds_in_driver++;
11748 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11749 	    un->un_ncmds_in_driver);
11750 
11751 	if ((bp->b_flags & B_WRITE) && (bp->b_bcount != 0) &&
11752 	    (cmd != SCMD_MODE_SELECT) && (cmd != SCMD_MODE_SELECT_G1))
11753 		un->un_f_sync_cache_required = TRUE;
11754 
11755 	mutex_exit(SD_MUTEX(un));
11756 
11757 	switch (uip->ui_flags) {
11758 	case SD_PATH_DIRECT:
11759 		chain_type = SD_CHAIN_DIRECT;
11760 		break;
11761 	case SD_PATH_DIRECT_PRIORITY:
11762 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11763 		break;
11764 	default:
11765 		chain_type = SD_CHAIN_USCSI;
11766 		break;
11767 	}
11768 
11769 	/*
11770 	 * We may allocate extra buf for external USCSI commands. If the
11771 	 * application asks for bigger than 20-byte sense data via USCSI,
11772 	 * SCSA layer will allocate 252 bytes sense buf for that command.
11773 	 */
11774 	if (((struct uscsi_cmd *)(uip->ui_cmdp))->uscsi_rqlen >
11775 	    SENSE_LENGTH) {
11776 		xp = kmem_zalloc(sizeof (struct sd_xbuf) - SENSE_LENGTH +
11777 		    MAX_SENSE_LENGTH, KM_SLEEP);
11778 	} else {
11779 		xp = kmem_zalloc(sizeof (struct sd_xbuf), KM_SLEEP);
11780 	}
11781 
11782 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11783 
11784 	/* Use the index obtained within xbuf_init */
11785 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11786 
11787 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11788 
11789 	return (0);
11790 }
11791 
11792 /*
11793  *    Function: sd_send_scsi_cmd
11794  *
11795  * Description: Runs a USCSI command for user (when called thru sdioctl),
11796  *		or for the driver
11797  *
11798  *   Arguments: dev - the dev_t for the device
11799  *		incmd - ptr to a valid uscsi_cmd struct
11800  *		flag - bit flag, indicating open settings, 32/64 bit type
11801  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11802  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11803  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11804  *			to use the USCSI "direct" chain and bypass the normal
11805  *			command waitq.
11806  *
11807  * Return Code: 0 -  successful completion of the given command
11808  *		EIO - scsi_uscsi_handle_command() failed
11809  *		ENXIO  - soft state not found for specified dev
11810  *		EINVAL
11811  *		EFAULT - copyin/copyout error
11812  *		return code of scsi_uscsi_handle_command():
11813  *			EIO
11814  *			ENXIO
11815  *			EACCES
11816  *
11817  *     Context: Waits for command to complete. Can sleep.
11818  */
11819 
11820 static int
11821 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
11822 	enum uio_seg dataspace, int path_flag)
11823 {
11824 	struct sd_lun	*un;
11825 	sd_ssc_t	*ssc;
11826 	int		rval;
11827 
11828 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11829 	if (un == NULL) {
11830 		return (ENXIO);
11831 	}
11832 
11833 	/*
11834 	 * Using sd_ssc_send to handle uscsi cmd
11835 	 */
11836 	ssc = sd_ssc_init(un);
11837 	rval = sd_ssc_send(ssc, incmd, flag, dataspace, path_flag);
11838 	sd_ssc_fini(ssc);
11839 
11840 	return (rval);
11841 }
11842 
11843 /*
11844  *    Function: sd_ssc_init
11845  *
11846  * Description: Uscsi end-user call this function to initialize necessary
11847  *              fields, such as uscsi_cmd and sd_uscsi_info struct.
11848  *
11849  *              The return value of sd_send_scsi_cmd will be treated as a
11850  *              fault in various conditions. Even it is not Zero, some
11851  *              callers may ignore the return value. That is to say, we can
11852  *              not make an accurate assessment in sdintr, since if a
11853  *              command is failed in sdintr it does not mean the caller of
11854  *              sd_send_scsi_cmd will treat it as a real failure.
11855  *
11856  *              To avoid printing too many error logs for a failed uscsi
11857  *              packet that the caller may not treat it as a failure, the
11858  *              sd will keep silent for handling all uscsi commands.
11859  *
11860  *              During detach->attach and attach-open, for some types of
11861  *              problems, the driver should be providing information about
11862  *              the problem encountered. Device use USCSI_SILENT, which
11863  *              suppresses all driver information. The result is that no
11864  *              information about the problem is available. Being
11865  *              completely silent during this time is inappropriate. The
11866  *              driver needs a more selective filter than USCSI_SILENT, so
11867  *              that information related to faults is provided.
11868  *
11869  *              To make the accurate accessment, the caller  of
11870  *              sd_send_scsi_USCSI_CMD should take the ownership and
11871  *              get necessary information to print error messages.
11872  *
11873  *              If we want to print necessary info of uscsi command, we need to
11874  *              keep the uscsi_cmd and sd_uscsi_info till we can make the
11875  *              assessment. We use sd_ssc_init to alloc necessary
11876  *              structs for sending an uscsi command and we are also
11877  *              responsible for free the memory by calling
11878  *              sd_ssc_fini.
11879  *
11880  *              The calling secquences will look like:
11881  *              sd_ssc_init->
11882  *
11883  *                  ...
11884  *
11885  *                  sd_send_scsi_USCSI_CMD->
11886  *                      sd_ssc_send-> - - - sdintr
11887  *                  ...
11888  *
11889  *                  if we think the return value should be treated as a
11890  *                  failure, we make the accessment here and print out
11891  *                  necessary by retrieving uscsi_cmd and sd_uscsi_info'
11892  *
11893  *                  ...
11894  *
11895  *              sd_ssc_fini
11896  *
11897  *
11898  *   Arguments: un - pointer to driver soft state (unit) structure for this
11899  *                   target.
11900  *
11901  * Return code: sd_ssc_t - pointer to allocated sd_ssc_t struct, it contains
11902  *                         uscsi_cmd and sd_uscsi_info.
11903  *                  NULL - if can not alloc memory for sd_ssc_t struct
11904  *
11905  *     Context: Kernel Thread.
11906  */
11907 static sd_ssc_t *
11908 sd_ssc_init(struct sd_lun *un)
11909 {
11910 	sd_ssc_t		*ssc;
11911 	struct uscsi_cmd	*ucmdp;
11912 	struct sd_uscsi_info	*uip;
11913 
11914 	ASSERT(un != NULL);
11915 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11916 
11917 	/*
11918 	 * Allocate sd_ssc_t structure
11919 	 */
11920 	ssc = kmem_zalloc(sizeof (sd_ssc_t), KM_SLEEP);
11921 
11922 	/*
11923 	 * Allocate uscsi_cmd by calling scsi_uscsi_alloc common routine
11924 	 */
11925 	ucmdp = scsi_uscsi_alloc();
11926 
11927 	/*
11928 	 * Allocate sd_uscsi_info structure
11929 	 */
11930 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11931 
11932 	ssc->ssc_uscsi_cmd = ucmdp;
11933 	ssc->ssc_uscsi_info = uip;
11934 	ssc->ssc_un = un;
11935 
11936 	return (ssc);
11937 }
11938 
11939 /*
11940  * Function: sd_ssc_fini
11941  *
11942  * Description: To free sd_ssc_t and it's hanging off
11943  *
11944  * Arguments: ssc - struct pointer of sd_ssc_t.
11945  */
11946 static void
11947 sd_ssc_fini(sd_ssc_t *ssc)
11948 {
11949 	scsi_uscsi_free(ssc->ssc_uscsi_cmd);
11950 
11951 	if (ssc->ssc_uscsi_info != NULL) {
11952 		kmem_free(ssc->ssc_uscsi_info, sizeof (struct sd_uscsi_info));
11953 		ssc->ssc_uscsi_info = NULL;
11954 	}
11955 
11956 	kmem_free(ssc, sizeof (sd_ssc_t));
11957 	ssc = NULL;
11958 }
11959 
11960 /*
11961  * Function: sd_ssc_send
11962  *
11963  * Description: Runs a USCSI command for user when called through sdioctl,
11964  *              or for the driver.
11965  *
11966  *   Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
11967  *                    sd_uscsi_info in.
11968  *		incmd - ptr to a valid uscsi_cmd struct
11969  *		flag - bit flag, indicating open settings, 32/64 bit type
11970  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11971  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11972  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11973  *			to use the USCSI "direct" chain and bypass the normal
11974  *			command waitq.
11975  *
11976  * Return Code: 0 -  successful completion of the given command
11977  *		EIO - scsi_uscsi_handle_command() failed
11978  *		ENXIO  - soft state not found for specified dev
11979  *		ECANCELED - command cancelled due to low power
11980  *		EINVAL
11981  *		EFAULT - copyin/copyout error
11982  *		return code of scsi_uscsi_handle_command():
11983  *			EIO
11984  *			ENXIO
11985  *			EACCES
11986  *
11987  *     Context: Kernel Thread;
11988  *              Waits for command to complete. Can sleep.
11989  */
11990 static int
11991 sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd, int flag,
11992 	enum uio_seg dataspace, int path_flag)
11993 {
11994 	struct sd_uscsi_info	*uip;
11995 	struct uscsi_cmd	*uscmd;
11996 	struct sd_lun		*un;
11997 	dev_t			dev;
11998 
11999 	int	format = 0;
12000 	int	rval;
12001 
12002 	ASSERT(ssc != NULL);
12003 	un = ssc->ssc_un;
12004 	ASSERT(un != NULL);
12005 	uscmd = ssc->ssc_uscsi_cmd;
12006 	ASSERT(uscmd != NULL);
12007 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12008 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12009 		/*
12010 		 * If enter here, it indicates that the previous uscsi
12011 		 * command has not been processed by sd_ssc_assessment.
12012 		 * This is violating our rules of FMA telemetry processing.
12013 		 * We should print out this message and the last undisposed
12014 		 * uscsi command.
12015 		 */
12016 		if (uscmd->uscsi_cdb != NULL) {
12017 			SD_INFO(SD_LOG_SDTEST, un,
12018 			    "sd_ssc_send is missing the alternative "
12019 			    "sd_ssc_assessment when running command 0x%x.\n",
12020 			    uscmd->uscsi_cdb[0]);
12021 		}
12022 		/*
12023 		 * Set the ssc_flags to SSC_FLAGS_UNKNOWN, which should be
12024 		 * the initial status.
12025 		 */
12026 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12027 	}
12028 
12029 	/*
12030 	 * We need to make sure sd_ssc_send will have sd_ssc_assessment
12031 	 * followed to avoid missing FMA telemetries.
12032 	 */
12033 	ssc->ssc_flags |= SSC_FLAGS_NEED_ASSESSMENT;
12034 
12035 	/*
12036 	 * if USCSI_PMFAILFAST is set and un is in low power, fail the
12037 	 * command immediately.
12038 	 */
12039 	mutex_enter(SD_MUTEX(un));
12040 	mutex_enter(&un->un_pm_mutex);
12041 	if ((uscmd->uscsi_flags & USCSI_PMFAILFAST) &&
12042 	    SD_DEVICE_IS_IN_LOW_POWER(un)) {
12043 		SD_TRACE(SD_LOG_IO, un, "sd_ssc_send:"
12044 		    "un:0x%p is in low power\n", un);
12045 		mutex_exit(&un->un_pm_mutex);
12046 		mutex_exit(SD_MUTEX(un));
12047 		return (ECANCELED);
12048 	}
12049 	mutex_exit(&un->un_pm_mutex);
12050 	mutex_exit(SD_MUTEX(un));
12051 
12052 #ifdef SDDEBUG
12053 	switch (dataspace) {
12054 	case UIO_USERSPACE:
12055 		SD_TRACE(SD_LOG_IO, un,
12056 		    "sd_ssc_send: entry: un:0x%p UIO_USERSPACE\n", un);
12057 		break;
12058 	case UIO_SYSSPACE:
12059 		SD_TRACE(SD_LOG_IO, un,
12060 		    "sd_ssc_send: entry: un:0x%p UIO_SYSSPACE\n", un);
12061 		break;
12062 	default:
12063 		SD_TRACE(SD_LOG_IO, un,
12064 		    "sd_ssc_send: entry: un:0x%p UNEXPECTED SPACE\n", un);
12065 		break;
12066 	}
12067 #endif
12068 
12069 	rval = scsi_uscsi_copyin((intptr_t)incmd, flag,
12070 	    SD_ADDRESS(un), &uscmd);
12071 	if (rval != 0) {
12072 		SD_TRACE(SD_LOG_IO, un, "sd_sense_scsi_cmd: "
12073 		    "scsi_uscsi_alloc_and_copyin failed\n", un);
12074 		return (rval);
12075 	}
12076 
12077 	if ((uscmd->uscsi_cdb != NULL) &&
12078 	    (uscmd->uscsi_cdb[0] == SCMD_FORMAT)) {
12079 		mutex_enter(SD_MUTEX(un));
12080 		un->un_f_format_in_progress = TRUE;
12081 		mutex_exit(SD_MUTEX(un));
12082 		format = 1;
12083 	}
12084 
12085 	/*
12086 	 * Allocate an sd_uscsi_info struct and fill it with the info
12087 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
12088 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
12089 	 * since we allocate the buf here in this function, we do not
12090 	 * need to preserve the prior contents of b_private.
12091 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
12092 	 */
12093 	uip = ssc->ssc_uscsi_info;
12094 	uip->ui_flags = path_flag;
12095 	uip->ui_cmdp = uscmd;
12096 
12097 	/*
12098 	 * Commands sent with priority are intended for error recovery
12099 	 * situations, and do not have retries performed.
12100 	 */
12101 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
12102 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
12103 	}
12104 	uscmd->uscsi_flags &= ~USCSI_NOINTR;
12105 
12106 	dev = SD_GET_DEV(un);
12107 	rval = scsi_uscsi_handle_cmd(dev, dataspace, uscmd,
12108 	    sd_uscsi_strategy, NULL, uip);
12109 
12110 	/*
12111 	 * mark ssc_flags right after handle_cmd to make sure
12112 	 * the uscsi has been sent
12113 	 */
12114 	ssc->ssc_flags |= SSC_FLAGS_CMD_ISSUED;
12115 
12116 #ifdef SDDEBUG
12117 	SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12118 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
12119 	    uscmd->uscsi_status, uscmd->uscsi_resid);
12120 	if (uscmd->uscsi_bufaddr != NULL) {
12121 		SD_INFO(SD_LOG_IO, un, "sd_ssc_send: "
12122 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
12123 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
12124 		if (dataspace == UIO_SYSSPACE) {
12125 			SD_DUMP_MEMORY(un, SD_LOG_IO,
12126 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
12127 			    uscmd->uscsi_buflen, SD_LOG_HEX);
12128 		}
12129 	}
12130 #endif
12131 
12132 	if (format == 1) {
12133 		mutex_enter(SD_MUTEX(un));
12134 		un->un_f_format_in_progress = FALSE;
12135 		mutex_exit(SD_MUTEX(un));
12136 	}
12137 
12138 	(void) scsi_uscsi_copyout((intptr_t)incmd, uscmd);
12139 
12140 	return (rval);
12141 }
12142 
12143 /*
12144  *     Function: sd_ssc_print
12145  *
12146  * Description: Print information available to the console.
12147  *
12148  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12149  *                    sd_uscsi_info in.
12150  *            sd_severity - log level.
12151  *     Context: Kernel thread or interrupt context.
12152  */
12153 static void
12154 sd_ssc_print(sd_ssc_t *ssc, int sd_severity)
12155 {
12156 	struct uscsi_cmd	*ucmdp;
12157 	struct scsi_device	*devp;
12158 	dev_info_t 		*devinfo;
12159 	uchar_t			*sensep;
12160 	int			senlen;
12161 	union scsi_cdb		*cdbp;
12162 	uchar_t			com;
12163 	extern struct scsi_key_strings scsi_cmds[];
12164 
12165 	ASSERT(ssc != NULL);
12166 	ASSERT(ssc->ssc_un != NULL);
12167 
12168 	if (SD_FM_LOG(ssc->ssc_un) != SD_FM_LOG_EREPORT)
12169 		return;
12170 	ucmdp = ssc->ssc_uscsi_cmd;
12171 	devp = SD_SCSI_DEVP(ssc->ssc_un);
12172 	devinfo = SD_DEVINFO(ssc->ssc_un);
12173 	ASSERT(ucmdp != NULL);
12174 	ASSERT(devp != NULL);
12175 	ASSERT(devinfo != NULL);
12176 	sensep = (uint8_t *)ucmdp->uscsi_rqbuf;
12177 	senlen = ucmdp->uscsi_rqlen - ucmdp->uscsi_rqresid;
12178 	cdbp = (union scsi_cdb *)ucmdp->uscsi_cdb;
12179 
12180 	/* In certain case (like DOORLOCK), the cdb could be NULL. */
12181 	if (cdbp == NULL)
12182 		return;
12183 	/* We don't print log if no sense data available. */
12184 	if (senlen == 0)
12185 		sensep = NULL;
12186 	com = cdbp->scc_cmd;
12187 	scsi_generic_errmsg(devp, sd_label, sd_severity, 0, 0, com,
12188 	    scsi_cmds, sensep, ssc->ssc_un->un_additional_codes, NULL);
12189 }
12190 
12191 /*
12192  *     Function: sd_ssc_assessment
12193  *
12194  * Description: We use this function to make an assessment at the point
12195  *              where SD driver may encounter a potential error.
12196  *
12197  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12198  *                  sd_uscsi_info in.
12199  *            tp_assess - a hint of strategy for ereport posting.
12200  *            Possible values of tp_assess include:
12201  *                SD_FMT_IGNORE - we don't post any ereport because we're
12202  *                sure that it is ok to ignore the underlying problems.
12203  *                SD_FMT_IGNORE_COMPROMISE - we don't post any ereport for now
12204  *                but it might be not correct to ignore the underlying hardware
12205  *                error.
12206  *                SD_FMT_STATUS_CHECK - we will post an ereport with the
12207  *                payload driver-assessment of value "fail" or
12208  *                "fatal"(depending on what information we have here). This
12209  *                assessment value is usually set when SD driver think there
12210  *                is a potential error occurred(Typically, when return value
12211  *                of the SCSI command is EIO).
12212  *                SD_FMT_STANDARD - we will post an ereport with the payload
12213  *                driver-assessment of value "info". This assessment value is
12214  *                set when the SCSI command returned successfully and with
12215  *                sense data sent back.
12216  *
12217  *     Context: Kernel thread.
12218  */
12219 static void
12220 sd_ssc_assessment(sd_ssc_t *ssc, enum sd_type_assessment tp_assess)
12221 {
12222 	int senlen = 0;
12223 	struct uscsi_cmd *ucmdp = NULL;
12224 	struct sd_lun *un;
12225 
12226 	ASSERT(ssc != NULL);
12227 	un = ssc->ssc_un;
12228 	ASSERT(un != NULL);
12229 	ucmdp = ssc->ssc_uscsi_cmd;
12230 	ASSERT(ucmdp != NULL);
12231 
12232 	if (ssc->ssc_flags & SSC_FLAGS_NEED_ASSESSMENT) {
12233 		ssc->ssc_flags &= ~SSC_FLAGS_NEED_ASSESSMENT;
12234 	} else {
12235 		/*
12236 		 * If enter here, it indicates that we have a wrong
12237 		 * calling sequence of sd_ssc_send and sd_ssc_assessment,
12238 		 * both of which should be called in a pair in case of
12239 		 * loss of FMA telemetries.
12240 		 */
12241 		if (ucmdp->uscsi_cdb != NULL) {
12242 			SD_INFO(SD_LOG_SDTEST, un,
12243 			    "sd_ssc_assessment is missing the "
12244 			    "alternative sd_ssc_send when running 0x%x, "
12245 			    "or there are superfluous sd_ssc_assessment for "
12246 			    "the same sd_ssc_send.\n",
12247 			    ucmdp->uscsi_cdb[0]);
12248 		}
12249 		/*
12250 		 * Set the ssc_flags to the initial value to avoid passing
12251 		 * down dirty flags to the following sd_ssc_send function.
12252 		 */
12253 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12254 		return;
12255 	}
12256 
12257 	/*
12258 	 * Only handle an issued command which is waiting for assessment.
12259 	 * A command which is not issued will not have
12260 	 * SSC_FLAGS_INVALID_DATA set, so it'ok we just return here.
12261 	 */
12262 	if (!(ssc->ssc_flags & SSC_FLAGS_CMD_ISSUED)) {
12263 		sd_ssc_print(ssc, SCSI_ERR_INFO);
12264 		return;
12265 	} else {
12266 		/*
12267 		 * For an issued command, we should clear this flag in
12268 		 * order to make the sd_ssc_t structure be used off
12269 		 * multiple uscsi commands.
12270 		 */
12271 		ssc->ssc_flags &= ~SSC_FLAGS_CMD_ISSUED;
12272 	}
12273 
12274 	/*
12275 	 * We will not deal with non-retryable(flag USCSI_DIAGNOSE set)
12276 	 * commands here. And we should clear the ssc_flags before return.
12277 	 */
12278 	if (ucmdp->uscsi_flags & USCSI_DIAGNOSE) {
12279 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12280 		return;
12281 	}
12282 
12283 	switch (tp_assess) {
12284 	case SD_FMT_IGNORE:
12285 	case SD_FMT_IGNORE_COMPROMISE:
12286 		break;
12287 	case SD_FMT_STATUS_CHECK:
12288 		/*
12289 		 * For a failed command(including the succeeded command
12290 		 * with invalid data sent back).
12291 		 */
12292 		sd_ssc_post(ssc, SD_FM_DRV_FATAL);
12293 		break;
12294 	case SD_FMT_STANDARD:
12295 		/*
12296 		 * Always for the succeeded commands probably with sense
12297 		 * data sent back.
12298 		 * Limitation:
12299 		 *	We can only handle a succeeded command with sense
12300 		 *	data sent back when auto-request-sense is enabled.
12301 		 */
12302 		senlen = ssc->ssc_uscsi_cmd->uscsi_rqlen -
12303 		    ssc->ssc_uscsi_cmd->uscsi_rqresid;
12304 		if ((ssc->ssc_uscsi_info->ui_pkt_state & STATE_ARQ_DONE) &&
12305 		    (un->un_f_arq_enabled == TRUE) &&
12306 		    senlen > 0 &&
12307 		    ssc->ssc_uscsi_cmd->uscsi_rqbuf != NULL) {
12308 			sd_ssc_post(ssc, SD_FM_DRV_NOTICE);
12309 		}
12310 		break;
12311 	default:
12312 		/*
12313 		 * Should not have other type of assessment.
12314 		 */
12315 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
12316 		    "sd_ssc_assessment got wrong "
12317 		    "sd_type_assessment %d.\n", tp_assess);
12318 		break;
12319 	}
12320 	/*
12321 	 * Clear up the ssc_flags before return.
12322 	 */
12323 	ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12324 }
12325 
12326 /*
12327  *    Function: sd_ssc_post
12328  *
12329  * Description: 1. read the driver property to get fm-scsi-log flag.
12330  *              2. print log if fm_log_capable is non-zero.
12331  *              3. call sd_ssc_ereport_post to post ereport if possible.
12332  *
12333  *    Context: May be called from kernel thread or interrupt context.
12334  */
12335 static void
12336 sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess)
12337 {
12338 	struct sd_lun	*un;
12339 	int		sd_severity;
12340 
12341 	ASSERT(ssc != NULL);
12342 	un = ssc->ssc_un;
12343 	ASSERT(un != NULL);
12344 
12345 	/*
12346 	 * We may enter here from sd_ssc_assessment(for USCSI command) or
12347 	 * by directly called from sdintr context.
12348 	 * We don't handle a non-disk drive(CD-ROM, removable media).
12349 	 * Clear the ssc_flags before return in case we've set
12350 	 * SSC_FLAGS_INVALID_XXX which should be skipped for a non-disk
12351 	 * driver.
12352 	 */
12353 	if (ISCD(un) || un->un_f_has_removable_media) {
12354 		ssc->ssc_flags = SSC_FLAGS_UNKNOWN;
12355 		return;
12356 	}
12357 
12358 	switch (sd_assess) {
12359 		case SD_FM_DRV_FATAL:
12360 			sd_severity = SCSI_ERR_FATAL;
12361 			break;
12362 		case SD_FM_DRV_RECOVERY:
12363 			sd_severity = SCSI_ERR_RECOVERED;
12364 			break;
12365 		case SD_FM_DRV_RETRY:
12366 			sd_severity = SCSI_ERR_RETRYABLE;
12367 			break;
12368 		case SD_FM_DRV_NOTICE:
12369 			sd_severity = SCSI_ERR_INFO;
12370 			break;
12371 		default:
12372 			sd_severity = SCSI_ERR_UNKNOWN;
12373 	}
12374 	/* print log */
12375 	sd_ssc_print(ssc, sd_severity);
12376 
12377 	/* always post ereport */
12378 	sd_ssc_ereport_post(ssc, sd_assess);
12379 }
12380 
12381 /*
12382  *    Function: sd_ssc_set_info
12383  *
12384  * Description: Mark ssc_flags and set ssc_info which would be the
12385  *              payload of uderr ereport. This function will cause
12386  *              sd_ssc_ereport_post to post uderr ereport only.
12387  *              Besides, when ssc_flags == SSC_FLAGS_INVALID_DATA(USCSI),
12388  *              the function will also call SD_ERROR or scsi_log for a
12389  *              CDROM/removable-media/DDI_FM_NOT_CAPABLE device.
12390  *
12391  * Arguments: ssc - the struct of sd_ssc_t will bring uscsi_cmd and
12392  *                  sd_uscsi_info in.
12393  *            ssc_flags - indicate the sub-category of a uderr.
12394  *            comp - this argument is meaningful only when
12395  *                   ssc_flags == SSC_FLAGS_INVALID_DATA, and its possible
12396  *                   values include:
12397  *                   > 0, SD_ERROR is used with comp as the driver logging
12398  *                   component;
12399  *                   = 0, scsi-log is used to log error telemetries;
12400  *                   < 0, no log available for this telemetry.
12401  *
12402  *    Context: Kernel thread or interrupt context
12403  */
12404 static void
12405 sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp, const char *fmt, ...)
12406 {
12407 	va_list	ap;
12408 
12409 	ASSERT(ssc != NULL);
12410 	ASSERT(ssc->ssc_un != NULL);
12411 
12412 	ssc->ssc_flags |= ssc_flags;
12413 	va_start(ap, fmt);
12414 	(void) vsnprintf(ssc->ssc_info, sizeof (ssc->ssc_info), fmt, ap);
12415 	va_end(ap);
12416 
12417 	/*
12418 	 * If SSC_FLAGS_INVALID_DATA is set, it should be a uscsi command
12419 	 * with invalid data sent back. For non-uscsi command, the
12420 	 * following code will be bypassed.
12421 	 */
12422 	if (ssc_flags & SSC_FLAGS_INVALID_DATA) {
12423 		if (SD_FM_LOG(ssc->ssc_un) == SD_FM_LOG_NSUP) {
12424 			/*
12425 			 * If the error belong to certain component and we
12426 			 * do not want it to show up on the console, we
12427 			 * will use SD_ERROR, otherwise scsi_log is
12428 			 * preferred.
12429 			 */
12430 			if (comp > 0) {
12431 				SD_ERROR(comp, ssc->ssc_un, ssc->ssc_info);
12432 			} else if (comp == 0) {
12433 				scsi_log(SD_DEVINFO(ssc->ssc_un), sd_label,
12434 				    CE_WARN, ssc->ssc_info);
12435 			}
12436 		}
12437 	}
12438 }
12439 
12440 /*
12441  *    Function: sd_buf_iodone
12442  *
12443  * Description: Frees the sd_xbuf & returns the buf to its originator.
12444  *
12445  *     Context: May be called from interrupt context.
12446  */
12447 /* ARGSUSED */
12448 static void
12449 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
12450 {
12451 	struct sd_xbuf *xp;
12452 
12453 	ASSERT(un != NULL);
12454 	ASSERT(bp != NULL);
12455 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12456 
12457 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12458 
12459 	xp = SD_GET_XBUF(bp);
12460 	ASSERT(xp != NULL);
12461 
12462 	/* xbuf is gone after this */
12463 	if (ddi_xbuf_done(bp, un->un_xbuf_attr)) {
12464 		mutex_enter(SD_MUTEX(un));
12465 
12466 		/*
12467 		 * Grab time when the cmd completed.
12468 		 * This is used for determining if the system has been
12469 		 * idle long enough to make it idle to the PM framework.
12470 		 * This is for lowering the overhead, and therefore improving
12471 		 * performance per I/O operation.
12472 		 */
12473 		un->un_pm_idle_time = gethrtime();
12474 
12475 		un->un_ncmds_in_driver--;
12476 		ASSERT(un->un_ncmds_in_driver >= 0);
12477 		SD_INFO(SD_LOG_IO, un,
12478 		    "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12479 		    un->un_ncmds_in_driver);
12480 
12481 		mutex_exit(SD_MUTEX(un));
12482 	}
12483 
12484 	biodone(bp);				/* bp is gone after this */
12485 
12486 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12487 }
12488 
12489 
12490 /*
12491  *    Function: sd_uscsi_iodone
12492  *
12493  * Description: Frees the sd_xbuf & returns the buf to its originator.
12494  *
12495  *     Context: May be called from interrupt context.
12496  */
12497 /* ARGSUSED */
12498 static void
12499 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12500 {
12501 	struct sd_xbuf *xp;
12502 
12503 	ASSERT(un != NULL);
12504 	ASSERT(bp != NULL);
12505 
12506 	xp = SD_GET_XBUF(bp);
12507 	ASSERT(xp != NULL);
12508 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12509 
12510 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12511 
12512 	bp->b_private = xp->xb_private;
12513 
12514 	mutex_enter(SD_MUTEX(un));
12515 
12516 	/*
12517 	 * Grab time when the cmd completed.
12518 	 * This is used for determining if the system has been
12519 	 * idle long enough to make it idle to the PM framework.
12520 	 * This is for lowering the overhead, and therefore improving
12521 	 * performance per I/O operation.
12522 	 */
12523 	un->un_pm_idle_time = gethrtime();
12524 
12525 	un->un_ncmds_in_driver--;
12526 	ASSERT(un->un_ncmds_in_driver >= 0);
12527 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12528 	    un->un_ncmds_in_driver);
12529 
12530 	mutex_exit(SD_MUTEX(un));
12531 
12532 	if (((struct uscsi_cmd *)(xp->xb_pktinfo))->uscsi_rqlen >
12533 	    SENSE_LENGTH) {
12534 		kmem_free(xp, sizeof (struct sd_xbuf) - SENSE_LENGTH +
12535 		    MAX_SENSE_LENGTH);
12536 	} else {
12537 		kmem_free(xp, sizeof (struct sd_xbuf));
12538 	}
12539 
12540 	biodone(bp);
12541 
12542 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12543 }
12544 
12545 
12546 /*
12547  *    Function: sd_mapblockaddr_iostart
12548  *
12549  * Description: Verify request lies within the partition limits for
12550  *		the indicated minor device.  Issue "overrun" buf if
12551  *		request would exceed partition range.  Converts
12552  *		partition-relative block address to absolute.
12553  *
12554  *              Upon exit of this function:
12555  *              1.I/O is aligned
12556  *                 xp->xb_blkno represents the absolute sector address
12557  *              2.I/O is misaligned
12558  *                 xp->xb_blkno represents the absolute logical block address
12559  *                 based on DEV_BSIZE. The logical block address will be
12560  *                 converted to physical sector address in sd_mapblocksize_\
12561  *                 iostart.
12562  *              3.I/O is misaligned but is aligned in "overrun" buf
12563  *                 xp->xb_blkno represents the absolute logical block address
12564  *                 based on DEV_BSIZE. The logical block address will be
12565  *                 converted to physical sector address in sd_mapblocksize_\
12566  *                 iostart. But no RMW will be issued in this case.
12567  *
12568  *     Context: Can sleep
12569  *
12570  *      Issues: This follows what the old code did, in terms of accessing
12571  *		some of the partition info in the unit struct without holding
12572  *		the mutext.  This is a general issue, if the partition info
12573  *		can be altered while IO is in progress... as soon as we send
12574  *		a buf, its partitioning can be invalid before it gets to the
12575  *		device.  Probably the right fix is to move partitioning out
12576  *		of the driver entirely.
12577  */
12578 
12579 static void
12580 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12581 {
12582 	diskaddr_t	nblocks;	/* #blocks in the given partition */
12583 	daddr_t	blocknum;	/* Block number specified by the buf */
12584 	size_t	requested_nblocks;
12585 	size_t	available_nblocks;
12586 	int	partition;
12587 	diskaddr_t	partition_offset;
12588 	struct sd_xbuf *xp;
12589 	int secmask = 0, blknomask = 0;
12590 	ushort_t is_aligned = TRUE;
12591 
12592 	ASSERT(un != NULL);
12593 	ASSERT(bp != NULL);
12594 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12595 
12596 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12597 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12598 
12599 	xp = SD_GET_XBUF(bp);
12600 	ASSERT(xp != NULL);
12601 
12602 	/*
12603 	 * If the geometry is not indicated as valid, attempt to access
12604 	 * the unit & verify the geometry/label. This can be the case for
12605 	 * removable-media devices, of if the device was opened in
12606 	 * NDELAY/NONBLOCK mode.
12607 	 */
12608 	partition = SDPART(bp->b_edev);
12609 
12610 	if (!SD_IS_VALID_LABEL(un)) {
12611 		sd_ssc_t *ssc;
12612 		/*
12613 		 * Initialize sd_ssc_t for internal uscsi commands
12614 		 * In case of potential porformance issue, we need
12615 		 * to alloc memory only if there is invalid label
12616 		 */
12617 		ssc = sd_ssc_init(un);
12618 
12619 		if (sd_ready_and_valid(ssc, partition) != SD_READY_VALID) {
12620 			/*
12621 			 * For removable devices it is possible to start an
12622 			 * I/O without a media by opening the device in nodelay
12623 			 * mode. Also for writable CDs there can be many
12624 			 * scenarios where there is no geometry yet but volume
12625 			 * manager is trying to issue a read() just because
12626 			 * it can see TOC on the CD. So do not print a message
12627 			 * for removables.
12628 			 */
12629 			if (!un->un_f_has_removable_media) {
12630 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12631 				    "i/o to invalid geometry\n");
12632 			}
12633 			bioerror(bp, EIO);
12634 			bp->b_resid = bp->b_bcount;
12635 			SD_BEGIN_IODONE(index, un, bp);
12636 
12637 			sd_ssc_fini(ssc);
12638 			return;
12639 		}
12640 		sd_ssc_fini(ssc);
12641 	}
12642 
12643 	nblocks = 0;
12644 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
12645 	    &nblocks, &partition_offset, NULL, NULL, (void *)SD_PATH_DIRECT);
12646 
12647 	if (un->un_f_enable_rmw) {
12648 		blknomask = (un->un_phy_blocksize / DEV_BSIZE) - 1;
12649 		secmask = un->un_phy_blocksize - 1;
12650 	} else {
12651 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
12652 		secmask = un->un_tgt_blocksize - 1;
12653 	}
12654 
12655 	if ((bp->b_lblkno & (blknomask)) || (bp->b_bcount & (secmask))) {
12656 		is_aligned = FALSE;
12657 	}
12658 
12659 	if (!(NOT_DEVBSIZE(un)) || un->un_f_enable_rmw) {
12660 		/*
12661 		 * If I/O is aligned, no need to involve RMW(Read Modify Write)
12662 		 * Convert the logical block number to target's physical sector
12663 		 * number.
12664 		 */
12665 		if (is_aligned) {
12666 			xp->xb_blkno = SD_SYS2TGTBLOCK(un, xp->xb_blkno);
12667 		} else {
12668 			/*
12669 			 * There is no RMW if we're just reading, so don't
12670 			 * warn or error out because of it.
12671 			 */
12672 			if (bp->b_flags & B_READ) {
12673 				/*EMPTY*/
12674 			} else if (!un->un_f_enable_rmw &&
12675 			    un->un_f_rmw_type == SD_RMW_TYPE_RETURN_ERROR) {
12676 				bp->b_flags |= B_ERROR;
12677 				goto error_exit;
12678 			} else if (un->un_f_rmw_type == SD_RMW_TYPE_DEFAULT) {
12679 				mutex_enter(SD_MUTEX(un));
12680 				if (!un->un_f_enable_rmw &&
12681 				    un->un_rmw_msg_timeid == NULL) {
12682 					scsi_log(SD_DEVINFO(un), sd_label,
12683 					    CE_WARN, "I/O request is not "
12684 					    "aligned with %d disk sector size. "
12685 					    "It is handled through Read Modify "
12686 					    "Write but the performance is "
12687 					    "very low.\n",
12688 					    un->un_tgt_blocksize);
12689 					un->un_rmw_msg_timeid =
12690 					    timeout(sd_rmw_msg_print_handler,
12691 					    un, SD_RMW_MSG_PRINT_TIMEOUT);
12692 				} else {
12693 					un->un_rmw_incre_count ++;
12694 				}
12695 				mutex_exit(SD_MUTEX(un));
12696 			}
12697 
12698 			nblocks = SD_TGT2SYSBLOCK(un, nblocks);
12699 			partition_offset = SD_TGT2SYSBLOCK(un,
12700 			    partition_offset);
12701 		}
12702 	}
12703 
12704 	/*
12705 	 * blocknum is the starting block number of the request. At this
12706 	 * point it is still relative to the start of the minor device.
12707 	 */
12708 	blocknum = xp->xb_blkno;
12709 
12710 	/*
12711 	 * Legacy: If the starting block number is one past the last block
12712 	 * in the partition, do not set B_ERROR in the buf.
12713 	 */
12714 	if (blocknum == nblocks)  {
12715 		goto error_exit;
12716 	}
12717 
12718 	/*
12719 	 * Confirm that the first block of the request lies within the
12720 	 * partition limits. Also the requested number of bytes must be
12721 	 * a multiple of the system block size.
12722 	 */
12723 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12724 	    ((bp->b_bcount & (DEV_BSIZE - 1)) != 0)) {
12725 		bp->b_flags |= B_ERROR;
12726 		goto error_exit;
12727 	}
12728 
12729 	/*
12730 	 * If the requsted # blocks exceeds the available # blocks, that
12731 	 * is an overrun of the partition.
12732 	 */
12733 	if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12734 		requested_nblocks = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12735 	} else {
12736 		requested_nblocks = SD_BYTES2SYSBLOCKS(bp->b_bcount);
12737 	}
12738 
12739 	available_nblocks = (size_t)(nblocks - blocknum);
12740 	ASSERT(nblocks >= blocknum);
12741 
12742 	if (requested_nblocks > available_nblocks) {
12743 		size_t resid;
12744 
12745 		/*
12746 		 * Allocate an "overrun" buf to allow the request to proceed
12747 		 * for the amount of space available in the partition. The
12748 		 * amount not transferred will be added into the b_resid
12749 		 * when the operation is complete. The overrun buf
12750 		 * replaces the original buf here, and the original buf
12751 		 * is saved inside the overrun buf, for later use.
12752 		 */
12753 		if ((!NOT_DEVBSIZE(un)) && is_aligned) {
12754 			resid = SD_TGTBLOCKS2BYTES(un,
12755 			    (offset_t)(requested_nblocks - available_nblocks));
12756 		} else {
12757 			resid = SD_SYSBLOCKS2BYTES(
12758 			    (offset_t)(requested_nblocks - available_nblocks));
12759 		}
12760 
12761 		size_t count = bp->b_bcount - resid;
12762 		/*
12763 		 * Note: count is an unsigned entity thus it'll NEVER
12764 		 * be less than 0 so ASSERT the original values are
12765 		 * correct.
12766 		 */
12767 		ASSERT(bp->b_bcount >= resid);
12768 
12769 		bp = sd_bioclone_alloc(bp, count, blocknum,
12770 		    (int (*)(struct buf *)) sd_mapblockaddr_iodone);
12771 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12772 		ASSERT(xp != NULL);
12773 	}
12774 
12775 	/* At this point there should be no residual for this buf. */
12776 	ASSERT(bp->b_resid == 0);
12777 
12778 	/* Convert the block number to an absolute address. */
12779 	xp->xb_blkno += partition_offset;
12780 
12781 	SD_NEXT_IOSTART(index, un, bp);
12782 
12783 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12784 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12785 
12786 	return;
12787 
12788 error_exit:
12789 	bp->b_resid = bp->b_bcount;
12790 	SD_BEGIN_IODONE(index, un, bp);
12791 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12792 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12793 }
12794 
12795 
12796 /*
12797  *    Function: sd_mapblockaddr_iodone
12798  *
12799  * Description: Completion-side processing for partition management.
12800  *
12801  *     Context: May be called under interrupt context
12802  */
12803 
12804 static void
12805 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12806 {
12807 	/* int	partition; */	/* Not used, see below. */
12808 	ASSERT(un != NULL);
12809 	ASSERT(bp != NULL);
12810 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12811 
12812 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12813 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12814 
12815 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12816 		/*
12817 		 * We have an "overrun" buf to deal with...
12818 		 */
12819 		struct sd_xbuf	*xp;
12820 		struct buf	*obp;	/* ptr to the original buf */
12821 
12822 		xp = SD_GET_XBUF(bp);
12823 		ASSERT(xp != NULL);
12824 
12825 		/* Retrieve the pointer to the original buf */
12826 		obp = (struct buf *)xp->xb_private;
12827 		ASSERT(obp != NULL);
12828 
12829 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12830 		bioerror(obp, bp->b_error);
12831 
12832 		sd_bioclone_free(bp);
12833 
12834 		/*
12835 		 * Get back the original buf.
12836 		 * Note that since the restoration of xb_blkno below
12837 		 * was removed, the sd_xbuf is not needed.
12838 		 */
12839 		bp = obp;
12840 		/*
12841 		 * xp = SD_GET_XBUF(bp);
12842 		 * ASSERT(xp != NULL);
12843 		 */
12844 	}
12845 
12846 	/*
12847 	 * Convert sd->xb_blkno back to a minor-device relative value.
12848 	 * Note: this has been commented out, as it is not needed in the
12849 	 * current implementation of the driver (ie, since this function
12850 	 * is at the top of the layering chains, so the info will be
12851 	 * discarded) and it is in the "hot" IO path.
12852 	 *
12853 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12854 	 * xp->xb_blkno -= un->un_offset[partition];
12855 	 */
12856 
12857 	SD_NEXT_IODONE(index, un, bp);
12858 
12859 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12860 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12861 }
12862 
12863 
12864 /*
12865  *    Function: sd_mapblocksize_iostart
12866  *
12867  * Description: Convert between system block size (un->un_sys_blocksize)
12868  *		and target block size (un->un_tgt_blocksize).
12869  *
12870  *     Context: Can sleep to allocate resources.
12871  *
12872  * Assumptions: A higher layer has already performed any partition validation,
12873  *		and converted the xp->xb_blkno to an absolute value relative
12874  *		to the start of the device.
12875  *
12876  *		It is also assumed that the higher layer has implemented
12877  *		an "overrun" mechanism for the case where the request would
12878  *		read/write beyond the end of a partition.  In this case we
12879  *		assume (and ASSERT) that bp->b_resid == 0.
12880  *
12881  *		Note: The implementation for this routine assumes the target
12882  *		block size remains constant between allocation and transport.
12883  */
12884 
12885 static void
12886 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12887 {
12888 	struct sd_mapblocksize_info	*bsp;
12889 	struct sd_xbuf			*xp;
12890 	offset_t first_byte;
12891 	daddr_t	start_block, end_block;
12892 	daddr_t	request_bytes;
12893 	ushort_t is_aligned = FALSE;
12894 
12895 	ASSERT(un != NULL);
12896 	ASSERT(bp != NULL);
12897 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12898 	ASSERT(bp->b_resid == 0);
12899 
12900 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12901 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12902 
12903 	/*
12904 	 * For a non-writable CD, a write request is an error
12905 	 */
12906 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12907 	    (un->un_f_mmc_writable_media == FALSE)) {
12908 		bioerror(bp, EIO);
12909 		bp->b_resid = bp->b_bcount;
12910 		SD_BEGIN_IODONE(index, un, bp);
12911 		return;
12912 	}
12913 
12914 	/*
12915 	 * We do not need a shadow buf if the device is using
12916 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12917 	 * In this case there is no layer-private data block allocated.
12918 	 */
12919 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
12920 	    (bp->b_bcount == 0)) {
12921 		goto done;
12922 	}
12923 
12924 #if defined(__i386) || defined(__amd64)
12925 	/* We do not support non-block-aligned transfers for ROD devices */
12926 	ASSERT(!ISROD(un));
12927 #endif
12928 
12929 	xp = SD_GET_XBUF(bp);
12930 	ASSERT(xp != NULL);
12931 
12932 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12933 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12934 	    un->un_tgt_blocksize, DEV_BSIZE);
12935 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12936 	    "request start block:0x%x\n", xp->xb_blkno);
12937 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12938 	    "request len:0x%x\n", bp->b_bcount);
12939 
12940 	/*
12941 	 * Allocate the layer-private data area for the mapblocksize layer.
12942 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12943 	 * struct to store the pointer to their layer-private data block, but
12944 	 * each layer also has the responsibility of restoring the prior
12945 	 * contents of xb_private before returning the buf/xbuf to the
12946 	 * higher layer that sent it.
12947 	 *
12948 	 * Here we save the prior contents of xp->xb_private into the
12949 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12950 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12951 	 * the layer-private area and returning the buf/xbuf to the layer
12952 	 * that sent it.
12953 	 *
12954 	 * Note that here we use kmem_zalloc for the allocation as there are
12955 	 * parts of the mapblocksize code that expect certain fields to be
12956 	 * zero unless explicitly set to a required value.
12957 	 */
12958 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12959 	bsp->mbs_oprivate = xp->xb_private;
12960 	xp->xb_private = bsp;
12961 
12962 	/*
12963 	 * This treats the data on the disk (target) as an array of bytes.
12964 	 * first_byte is the byte offset, from the beginning of the device,
12965 	 * to the location of the request. This is converted from a
12966 	 * un->un_sys_blocksize block address to a byte offset, and then back
12967 	 * to a block address based upon a un->un_tgt_blocksize block size.
12968 	 *
12969 	 * xp->xb_blkno should be absolute upon entry into this function,
12970 	 * but, but it is based upon partitions that use the "system"
12971 	 * block size. It must be adjusted to reflect the block size of
12972 	 * the target.
12973 	 *
12974 	 * Note that end_block is actually the block that follows the last
12975 	 * block of the request, but that's what is needed for the computation.
12976 	 */
12977 	first_byte  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
12978 	if (un->un_f_enable_rmw) {
12979 		start_block = xp->xb_blkno =
12980 		    (first_byte / un->un_phy_blocksize) *
12981 		    (un->un_phy_blocksize / DEV_BSIZE);
12982 		end_block   = ((first_byte + bp->b_bcount +
12983 		    un->un_phy_blocksize - 1) / un->un_phy_blocksize) *
12984 		    (un->un_phy_blocksize / DEV_BSIZE);
12985 	} else {
12986 		start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12987 		end_block   = (first_byte + bp->b_bcount +
12988 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
12989 	}
12990 
12991 	/* request_bytes is rounded up to a multiple of the target block size */
12992 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12993 
12994 	/*
12995 	 * See if the starting address of the request and the request
12996 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12997 	 * then we do not need to allocate a shadow buf to handle the request.
12998 	 */
12999 	if (un->un_f_enable_rmw) {
13000 		if (((first_byte % un->un_phy_blocksize) == 0) &&
13001 		    ((bp->b_bcount % un->un_phy_blocksize) == 0)) {
13002 			is_aligned = TRUE;
13003 		}
13004 	} else {
13005 		if (((first_byte % un->un_tgt_blocksize) == 0) &&
13006 		    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
13007 			is_aligned = TRUE;
13008 		}
13009 	}
13010 
13011 	if ((bp->b_flags & B_READ) == 0) {
13012 		/*
13013 		 * Lock the range for a write operation. An aligned request is
13014 		 * considered a simple write; otherwise the request must be a
13015 		 * read-modify-write.
13016 		 */
13017 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
13018 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
13019 	}
13020 
13021 	/*
13022 	 * Alloc a shadow buf if the request is not aligned. Also, this is
13023 	 * where the READ command is generated for a read-modify-write. (The
13024 	 * write phase is deferred until after the read completes.)
13025 	 */
13026 	if (is_aligned == FALSE) {
13027 
13028 		struct sd_mapblocksize_info	*shadow_bsp;
13029 		struct sd_xbuf	*shadow_xp;
13030 		struct buf	*shadow_bp;
13031 
13032 		/*
13033 		 * Allocate the shadow buf and it associated xbuf. Note that
13034 		 * after this call the xb_blkno value in both the original
13035 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
13036 		 * same: absolute relative to the start of the device, and
13037 		 * adjusted for the target block size. The b_blkno in the
13038 		 * shadow buf will also be set to this value. We should never
13039 		 * change b_blkno in the original bp however.
13040 		 *
13041 		 * Note also that the shadow buf will always need to be a
13042 		 * READ command, regardless of whether the incoming command
13043 		 * is a READ or a WRITE.
13044 		 */
13045 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
13046 		    xp->xb_blkno,
13047 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
13048 
13049 		shadow_xp = SD_GET_XBUF(shadow_bp);
13050 
13051 		/*
13052 		 * Allocate the layer-private data for the shadow buf.
13053 		 * (No need to preserve xb_private in the shadow xbuf.)
13054 		 */
13055 		shadow_xp->xb_private = shadow_bsp =
13056 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
13057 
13058 		/*
13059 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
13060 		 * to figure out where the start of the user data is (based upon
13061 		 * the system block size) in the data returned by the READ
13062 		 * command (which will be based upon the target blocksize). Note
13063 		 * that this is only really used if the request is unaligned.
13064 		 */
13065 		if (un->un_f_enable_rmw) {
13066 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13067 			    ((offset_t)xp->xb_blkno * un->un_sys_blocksize));
13068 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13069 			    (bsp->mbs_copy_offset < un->un_phy_blocksize));
13070 		} else {
13071 			bsp->mbs_copy_offset = (ssize_t)(first_byte -
13072 			    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
13073 			ASSERT((bsp->mbs_copy_offset >= 0) &&
13074 			    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
13075 		}
13076 
13077 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
13078 
13079 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
13080 
13081 		/* Transfer the wmap (if any) to the shadow buf */
13082 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
13083 		bsp->mbs_wmp = NULL;
13084 
13085 		/*
13086 		 * The shadow buf goes on from here in place of the
13087 		 * original buf.
13088 		 */
13089 		shadow_bsp->mbs_orig_bp = bp;
13090 		bp = shadow_bp;
13091 	}
13092 
13093 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13094 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
13095 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13096 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
13097 	    request_bytes);
13098 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
13099 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
13100 
13101 done:
13102 	SD_NEXT_IOSTART(index, un, bp);
13103 
13104 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13105 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
13106 }
13107 
13108 
13109 /*
13110  *    Function: sd_mapblocksize_iodone
13111  *
13112  * Description: Completion side processing for block-size mapping.
13113  *
13114  *     Context: May be called under interrupt context
13115  */
13116 
13117 static void
13118 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
13119 {
13120 	struct sd_mapblocksize_info	*bsp;
13121 	struct sd_xbuf	*xp;
13122 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
13123 	struct buf	*orig_bp;	/* ptr to the original buf */
13124 	offset_t	shadow_end;
13125 	offset_t	request_end;
13126 	offset_t	shadow_start;
13127 	ssize_t		copy_offset;
13128 	size_t		copy_length;
13129 	size_t		shortfall;
13130 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
13131 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
13132 
13133 	ASSERT(un != NULL);
13134 	ASSERT(bp != NULL);
13135 
13136 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
13137 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
13138 
13139 	/*
13140 	 * There is no shadow buf or layer-private data if the target is
13141 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
13142 	 */
13143 	if ((un->un_tgt_blocksize == DEV_BSIZE && !un->un_f_enable_rmw) ||
13144 	    (bp->b_bcount == 0)) {
13145 		goto exit;
13146 	}
13147 
13148 	xp = SD_GET_XBUF(bp);
13149 	ASSERT(xp != NULL);
13150 
13151 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
13152 	bsp = xp->xb_private;
13153 
13154 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
13155 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
13156 
13157 	if (is_write) {
13158 		/*
13159 		 * For a WRITE request we must free up the block range that
13160 		 * we have locked up.  This holds regardless of whether this is
13161 		 * an aligned write request or a read-modify-write request.
13162 		 */
13163 		sd_range_unlock(un, bsp->mbs_wmp);
13164 		bsp->mbs_wmp = NULL;
13165 	}
13166 
13167 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
13168 		/*
13169 		 * An aligned read or write command will have no shadow buf;
13170 		 * there is not much else to do with it.
13171 		 */
13172 		goto done;
13173 	}
13174 
13175 	orig_bp = bsp->mbs_orig_bp;
13176 	ASSERT(orig_bp != NULL);
13177 	orig_xp = SD_GET_XBUF(orig_bp);
13178 	ASSERT(orig_xp != NULL);
13179 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13180 
13181 	if (!is_write && has_wmap) {
13182 		/*
13183 		 * A READ with a wmap means this is the READ phase of a
13184 		 * read-modify-write. If an error occurred on the READ then
13185 		 * we do not proceed with the WRITE phase or copy any data.
13186 		 * Just release the write maps and return with an error.
13187 		 */
13188 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
13189 			orig_bp->b_resid = orig_bp->b_bcount;
13190 			bioerror(orig_bp, bp->b_error);
13191 			sd_range_unlock(un, bsp->mbs_wmp);
13192 			goto freebuf_done;
13193 		}
13194 	}
13195 
13196 	/*
13197 	 * Here is where we set up to copy the data from the shadow buf
13198 	 * into the space associated with the original buf.
13199 	 *
13200 	 * To deal with the conversion between block sizes, these
13201 	 * computations treat the data as an array of bytes, with the
13202 	 * first byte (byte 0) corresponding to the first byte in the
13203 	 * first block on the disk.
13204 	 */
13205 
13206 	/*
13207 	 * shadow_start and shadow_len indicate the location and size of
13208 	 * the data returned with the shadow IO request.
13209 	 */
13210 	if (un->un_f_enable_rmw) {
13211 		shadow_start  = SD_SYSBLOCKS2BYTES((offset_t)xp->xb_blkno);
13212 	} else {
13213 		shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
13214 	}
13215 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
13216 
13217 	/*
13218 	 * copy_offset gives the offset (in bytes) from the start of the first
13219 	 * block of the READ request to the beginning of the data.  We retrieve
13220 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
13221 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
13222 	 * data to be copied (in bytes).
13223 	 */
13224 	copy_offset  = bsp->mbs_copy_offset;
13225 	if (un->un_f_enable_rmw) {
13226 		ASSERT((copy_offset >= 0) &&
13227 		    (copy_offset < un->un_phy_blocksize));
13228 	} else {
13229 		ASSERT((copy_offset >= 0) &&
13230 		    (copy_offset < un->un_tgt_blocksize));
13231 	}
13232 
13233 	copy_length  = orig_bp->b_bcount;
13234 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
13235 
13236 	/*
13237 	 * Set up the resid and error fields of orig_bp as appropriate.
13238 	 */
13239 	if (shadow_end >= request_end) {
13240 		/* We got all the requested data; set resid to zero */
13241 		orig_bp->b_resid = 0;
13242 	} else {
13243 		/*
13244 		 * We failed to get enough data to fully satisfy the original
13245 		 * request. Just copy back whatever data we got and set
13246 		 * up the residual and error code as required.
13247 		 *
13248 		 * 'shortfall' is the amount by which the data received with the
13249 		 * shadow buf has "fallen short" of the requested amount.
13250 		 */
13251 		shortfall = (size_t)(request_end - shadow_end);
13252 
13253 		if (shortfall > orig_bp->b_bcount) {
13254 			/*
13255 			 * We did not get enough data to even partially
13256 			 * fulfill the original request.  The residual is
13257 			 * equal to the amount requested.
13258 			 */
13259 			orig_bp->b_resid = orig_bp->b_bcount;
13260 		} else {
13261 			/*
13262 			 * We did not get all the data that we requested
13263 			 * from the device, but we will try to return what
13264 			 * portion we did get.
13265 			 */
13266 			orig_bp->b_resid = shortfall;
13267 		}
13268 		ASSERT(copy_length >= orig_bp->b_resid);
13269 		copy_length  -= orig_bp->b_resid;
13270 	}
13271 
13272 	/* Propagate the error code from the shadow buf to the original buf */
13273 	bioerror(orig_bp, bp->b_error);
13274 
13275 	if (is_write) {
13276 		goto freebuf_done;	/* No data copying for a WRITE */
13277 	}
13278 
13279 	if (has_wmap) {
13280 		/*
13281 		 * This is a READ command from the READ phase of a
13282 		 * read-modify-write request. We have to copy the data given
13283 		 * by the user OVER the data returned by the READ command,
13284 		 * then convert the command from a READ to a WRITE and send
13285 		 * it back to the target.
13286 		 */
13287 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
13288 		    copy_length);
13289 
13290 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
13291 
13292 		/*
13293 		 * Dispatch the WRITE command to the taskq thread, which
13294 		 * will in turn send the command to the target. When the
13295 		 * WRITE command completes, we (sd_mapblocksize_iodone())
13296 		 * will get called again as part of the iodone chain
13297 		 * processing for it. Note that we will still be dealing
13298 		 * with the shadow buf at that point.
13299 		 */
13300 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
13301 		    KM_NOSLEEP) != 0) {
13302 			/*
13303 			 * Dispatch was successful so we are done. Return
13304 			 * without going any higher up the iodone chain. Do
13305 			 * not free up any layer-private data until after the
13306 			 * WRITE completes.
13307 			 */
13308 			return;
13309 		}
13310 
13311 		/*
13312 		 * Dispatch of the WRITE command failed; set up the error
13313 		 * condition and send this IO back up the iodone chain.
13314 		 */
13315 		bioerror(orig_bp, EIO);
13316 		orig_bp->b_resid = orig_bp->b_bcount;
13317 
13318 	} else {
13319 		/*
13320 		 * This is a regular READ request (ie, not a RMW). Copy the
13321 		 * data from the shadow buf into the original buf. The
13322 		 * copy_offset compensates for any "misalignment" between the
13323 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
13324 		 * original buf (with its un->un_sys_blocksize blocks).
13325 		 */
13326 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
13327 		    copy_length);
13328 	}
13329 
13330 freebuf_done:
13331 
13332 	/*
13333 	 * At this point we still have both the shadow buf AND the original
13334 	 * buf to deal with, as well as the layer-private data area in each.
13335 	 * Local variables are as follows:
13336 	 *
13337 	 * bp -- points to shadow buf
13338 	 * xp -- points to xbuf of shadow buf
13339 	 * bsp -- points to layer-private data area of shadow buf
13340 	 * orig_bp -- points to original buf
13341 	 *
13342 	 * First free the shadow buf and its associated xbuf, then free the
13343 	 * layer-private data area from the shadow buf. There is no need to
13344 	 * restore xb_private in the shadow xbuf.
13345 	 */
13346 	sd_shadow_buf_free(bp);
13347 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13348 
13349 	/*
13350 	 * Now update the local variables to point to the original buf, xbuf,
13351 	 * and layer-private area.
13352 	 */
13353 	bp = orig_bp;
13354 	xp = SD_GET_XBUF(bp);
13355 	ASSERT(xp != NULL);
13356 	ASSERT(xp == orig_xp);
13357 	bsp = xp->xb_private;
13358 	ASSERT(bsp != NULL);
13359 
13360 done:
13361 	/*
13362 	 * Restore xb_private to whatever it was set to by the next higher
13363 	 * layer in the chain, then free the layer-private data area.
13364 	 */
13365 	xp->xb_private = bsp->mbs_oprivate;
13366 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
13367 
13368 exit:
13369 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
13370 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
13371 
13372 	SD_NEXT_IODONE(index, un, bp);
13373 }
13374 
13375 
13376 /*
13377  *    Function: sd_checksum_iostart
13378  *
13379  * Description: A stub function for a layer that's currently not used.
13380  *		For now just a placeholder.
13381  *
13382  *     Context: Kernel thread context
13383  */
13384 
13385 static void
13386 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
13387 {
13388 	ASSERT(un != NULL);
13389 	ASSERT(bp != NULL);
13390 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13391 	SD_NEXT_IOSTART(index, un, bp);
13392 }
13393 
13394 
13395 /*
13396  *    Function: sd_checksum_iodone
13397  *
13398  * Description: A stub function for a layer that's currently not used.
13399  *		For now just a placeholder.
13400  *
13401  *     Context: May be called under interrupt context
13402  */
13403 
13404 static void
13405 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
13406 {
13407 	ASSERT(un != NULL);
13408 	ASSERT(bp != NULL);
13409 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13410 	SD_NEXT_IODONE(index, un, bp);
13411 }
13412 
13413 
13414 /*
13415  *    Function: sd_checksum_uscsi_iostart
13416  *
13417  * Description: A stub function for a layer that's currently not used.
13418  *		For now just a placeholder.
13419  *
13420  *     Context: Kernel thread context
13421  */
13422 
13423 static void
13424 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
13425 {
13426 	ASSERT(un != NULL);
13427 	ASSERT(bp != NULL);
13428 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13429 	SD_NEXT_IOSTART(index, un, bp);
13430 }
13431 
13432 
13433 /*
13434  *    Function: sd_checksum_uscsi_iodone
13435  *
13436  * Description: A stub function for a layer that's currently not used.
13437  *		For now just a placeholder.
13438  *
13439  *     Context: May be called under interrupt context
13440  */
13441 
13442 static void
13443 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
13444 {
13445 	ASSERT(un != NULL);
13446 	ASSERT(bp != NULL);
13447 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13448 	SD_NEXT_IODONE(index, un, bp);
13449 }
13450 
13451 
13452 /*
13453  *    Function: sd_pm_iostart
13454  *
13455  * Description: iostart-side routine for Power mangement.
13456  *
13457  *     Context: Kernel thread context
13458  */
13459 
13460 static void
13461 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
13462 {
13463 	ASSERT(un != NULL);
13464 	ASSERT(bp != NULL);
13465 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13466 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13467 
13468 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
13469 
13470 	if (sd_pm_entry(un) != DDI_SUCCESS) {
13471 		/*
13472 		 * Set up to return the failed buf back up the 'iodone'
13473 		 * side of the calling chain.
13474 		 */
13475 		bioerror(bp, EIO);
13476 		bp->b_resid = bp->b_bcount;
13477 
13478 		SD_BEGIN_IODONE(index, un, bp);
13479 
13480 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13481 		return;
13482 	}
13483 
13484 	SD_NEXT_IOSTART(index, un, bp);
13485 
13486 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
13487 }
13488 
13489 
13490 /*
13491  *    Function: sd_pm_iodone
13492  *
13493  * Description: iodone-side routine for power mangement.
13494  *
13495  *     Context: may be called from interrupt context
13496  */
13497 
13498 static void
13499 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
13500 {
13501 	ASSERT(un != NULL);
13502 	ASSERT(bp != NULL);
13503 	ASSERT(!mutex_owned(&un->un_pm_mutex));
13504 
13505 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
13506 
13507 	/*
13508 	 * After attach the following flag is only read, so don't
13509 	 * take the penalty of acquiring a mutex for it.
13510 	 */
13511 	if (un->un_f_pm_is_enabled == TRUE) {
13512 		sd_pm_exit(un);
13513 	}
13514 
13515 	SD_NEXT_IODONE(index, un, bp);
13516 
13517 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
13518 }
13519 
13520 
13521 /*
13522  *    Function: sd_core_iostart
13523  *
13524  * Description: Primary driver function for enqueuing buf(9S) structs from
13525  *		the system and initiating IO to the target device
13526  *
13527  *     Context: Kernel thread context. Can sleep.
13528  *
13529  * Assumptions:  - The given xp->xb_blkno is absolute
13530  *		   (ie, relative to the start of the device).
13531  *		 - The IO is to be done using the native blocksize of
13532  *		   the device, as specified in un->un_tgt_blocksize.
13533  */
13534 /* ARGSUSED */
13535 static void
13536 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
13537 {
13538 	struct sd_xbuf *xp;
13539 
13540 	ASSERT(un != NULL);
13541 	ASSERT(bp != NULL);
13542 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13543 	ASSERT(bp->b_resid == 0);
13544 
13545 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
13546 
13547 	xp = SD_GET_XBUF(bp);
13548 	ASSERT(xp != NULL);
13549 
13550 	mutex_enter(SD_MUTEX(un));
13551 
13552 	/*
13553 	 * If we are currently in the failfast state, fail any new IO
13554 	 * that has B_FAILFAST set, then return.
13555 	 */
13556 	if ((bp->b_flags & B_FAILFAST) &&
13557 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
13558 		mutex_exit(SD_MUTEX(un));
13559 		bioerror(bp, EIO);
13560 		bp->b_resid = bp->b_bcount;
13561 		SD_BEGIN_IODONE(index, un, bp);
13562 		return;
13563 	}
13564 
13565 	if (SD_IS_DIRECT_PRIORITY(xp)) {
13566 		/*
13567 		 * Priority command -- transport it immediately.
13568 		 *
13569 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
13570 		 * because all direct priority commands should be associated
13571 		 * with error recovery actions which we don't want to retry.
13572 		 */
13573 		sd_start_cmds(un, bp);
13574 	} else {
13575 		/*
13576 		 * Normal command -- add it to the wait queue, then start
13577 		 * transporting commands from the wait queue.
13578 		 */
13579 		sd_add_buf_to_waitq(un, bp);
13580 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
13581 		sd_start_cmds(un, NULL);
13582 	}
13583 
13584 	mutex_exit(SD_MUTEX(un));
13585 
13586 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
13587 }
13588 
13589 
13590 /*
13591  *    Function: sd_init_cdb_limits
13592  *
13593  * Description: This is to handle scsi_pkt initialization differences
13594  *		between the driver platforms.
13595  *
13596  *		Legacy behaviors:
13597  *
13598  *		If the block number or the sector count exceeds the
13599  *		capabilities of a Group 0 command, shift over to a
13600  *		Group 1 command. We don't blindly use Group 1
13601  *		commands because a) some drives (CDC Wren IVs) get a
13602  *		bit confused, and b) there is probably a fair amount
13603  *		of speed difference for a target to receive and decode
13604  *		a 10 byte command instead of a 6 byte command.
13605  *
13606  *		The xfer time difference of 6 vs 10 byte CDBs is
13607  *		still significant so this code is still worthwhile.
13608  *		10 byte CDBs are very inefficient with the fas HBA driver
13609  *		and older disks. Each CDB byte took 1 usec with some
13610  *		popular disks.
13611  *
13612  *     Context: Must be called at attach time
13613  */
13614 
13615 static void
13616 sd_init_cdb_limits(struct sd_lun *un)
13617 {
13618 	int hba_cdb_limit;
13619 
13620 	/*
13621 	 * Use CDB_GROUP1 commands for most devices except for
13622 	 * parallel SCSI fixed drives in which case we get better
13623 	 * performance using CDB_GROUP0 commands (where applicable).
13624 	 */
13625 	un->un_mincdb = SD_CDB_GROUP1;
13626 #if !defined(__fibre)
13627 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13628 	    !un->un_f_has_removable_media) {
13629 		un->un_mincdb = SD_CDB_GROUP0;
13630 	}
13631 #endif
13632 
13633 	/*
13634 	 * Try to read the max-cdb-length supported by HBA.
13635 	 */
13636 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13637 	if (0 >= un->un_max_hba_cdb) {
13638 		un->un_max_hba_cdb = CDB_GROUP4;
13639 		hba_cdb_limit = SD_CDB_GROUP4;
13640 	} else if (0 < un->un_max_hba_cdb &&
13641 	    un->un_max_hba_cdb < CDB_GROUP1) {
13642 		hba_cdb_limit = SD_CDB_GROUP0;
13643 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13644 	    un->un_max_hba_cdb < CDB_GROUP5) {
13645 		hba_cdb_limit = SD_CDB_GROUP1;
13646 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13647 	    un->un_max_hba_cdb < CDB_GROUP4) {
13648 		hba_cdb_limit = SD_CDB_GROUP5;
13649 	} else {
13650 		hba_cdb_limit = SD_CDB_GROUP4;
13651 	}
13652 
13653 	/*
13654 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13655 	 * commands for fixed disks unless we are building for a 32 bit
13656 	 * kernel.
13657 	 */
13658 #ifdef _LP64
13659 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13660 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13661 #else
13662 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13663 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13664 #endif
13665 
13666 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13667 	    ? sizeof (struct scsi_arq_status) : 1);
13668 	if (!ISCD(un))
13669 		un->un_cmd_timeout = (ushort_t)sd_io_time;
13670 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13671 }
13672 
13673 
13674 /*
13675  *    Function: sd_initpkt_for_buf
13676  *
13677  * Description: Allocate and initialize for transport a scsi_pkt struct,
13678  *		based upon the info specified in the given buf struct.
13679  *
13680  *		Assumes the xb_blkno in the request is absolute (ie,
13681  *		relative to the start of the device (NOT partition!).
13682  *		Also assumes that the request is using the native block
13683  *		size of the device (as returned by the READ CAPACITY
13684  *		command).
13685  *
13686  * Return Code: SD_PKT_ALLOC_SUCCESS
13687  *		SD_PKT_ALLOC_FAILURE
13688  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13689  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13690  *
13691  *     Context: Kernel thread and may be called from software interrupt context
13692  *		as part of a sdrunout callback. This function may not block or
13693  *		call routines that block
13694  */
13695 
13696 static int
13697 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13698 {
13699 	struct sd_xbuf	*xp;
13700 	struct scsi_pkt *pktp = NULL;
13701 	struct sd_lun	*un;
13702 	size_t		blockcount;
13703 	daddr_t		startblock;
13704 	int		rval;
13705 	int		cmd_flags;
13706 
13707 	ASSERT(bp != NULL);
13708 	ASSERT(pktpp != NULL);
13709 	xp = SD_GET_XBUF(bp);
13710 	ASSERT(xp != NULL);
13711 	un = SD_GET_UN(bp);
13712 	ASSERT(un != NULL);
13713 	ASSERT(mutex_owned(SD_MUTEX(un)));
13714 	ASSERT(bp->b_resid == 0);
13715 
13716 	SD_TRACE(SD_LOG_IO_CORE, un,
13717 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13718 
13719 	mutex_exit(SD_MUTEX(un));
13720 
13721 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13722 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13723 		/*
13724 		 * Already have a scsi_pkt -- just need DMA resources.
13725 		 * We must recompute the CDB in case the mapping returns
13726 		 * a nonzero pkt_resid.
13727 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13728 		 * that is being retried, the unmap/remap of the DMA resouces
13729 		 * will result in the entire transfer starting over again
13730 		 * from the very first block.
13731 		 */
13732 		ASSERT(xp->xb_pktp != NULL);
13733 		pktp = xp->xb_pktp;
13734 	} else {
13735 		pktp = NULL;
13736 	}
13737 #endif /* __i386 || __amd64 */
13738 
13739 	startblock = xp->xb_blkno;	/* Absolute block num. */
13740 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13741 
13742 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13743 
13744 	/*
13745 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13746 	 * call scsi_init_pkt, and build the CDB.
13747 	 */
13748 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13749 	    cmd_flags, sdrunout, (caddr_t)un,
13750 	    startblock, blockcount);
13751 
13752 	if (rval == 0) {
13753 		/*
13754 		 * Success.
13755 		 *
13756 		 * If partial DMA is being used and required for this transfer.
13757 		 * set it up here.
13758 		 */
13759 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13760 		    (pktp->pkt_resid != 0)) {
13761 
13762 			/*
13763 			 * Save the CDB length and pkt_resid for the
13764 			 * next xfer
13765 			 */
13766 			xp->xb_dma_resid = pktp->pkt_resid;
13767 
13768 			/* rezero resid */
13769 			pktp->pkt_resid = 0;
13770 
13771 		} else {
13772 			xp->xb_dma_resid = 0;
13773 		}
13774 
13775 		pktp->pkt_flags = un->un_tagflags;
13776 		pktp->pkt_time  = un->un_cmd_timeout;
13777 		pktp->pkt_comp  = sdintr;
13778 
13779 		pktp->pkt_private = bp;
13780 		*pktpp = pktp;
13781 
13782 		SD_TRACE(SD_LOG_IO_CORE, un,
13783 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13784 
13785 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13786 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13787 #endif
13788 
13789 		mutex_enter(SD_MUTEX(un));
13790 		return (SD_PKT_ALLOC_SUCCESS);
13791 
13792 	}
13793 
13794 	/*
13795 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13796 	 * from sd_setup_rw_pkt.
13797 	 */
13798 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13799 
13800 	if (rval == SD_PKT_ALLOC_FAILURE) {
13801 		*pktpp = NULL;
13802 		/*
13803 		 * Set the driver state to RWAIT to indicate the driver
13804 		 * is waiting on resource allocations. The driver will not
13805 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13806 		 */
13807 		mutex_enter(SD_MUTEX(un));
13808 		New_state(un, SD_STATE_RWAIT);
13809 
13810 		SD_ERROR(SD_LOG_IO_CORE, un,
13811 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13812 
13813 		if ((bp->b_flags & B_ERROR) != 0) {
13814 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13815 		}
13816 		return (SD_PKT_ALLOC_FAILURE);
13817 	} else {
13818 		/*
13819 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13820 		 *
13821 		 * This should never happen.  Maybe someone messed with the
13822 		 * kernel's minphys?
13823 		 */
13824 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13825 		    "Request rejected: too large for CDB: "
13826 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13827 		SD_ERROR(SD_LOG_IO_CORE, un,
13828 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13829 		mutex_enter(SD_MUTEX(un));
13830 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13831 
13832 	}
13833 }
13834 
13835 
13836 /*
13837  *    Function: sd_destroypkt_for_buf
13838  *
13839  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13840  *
13841  *     Context: Kernel thread or interrupt context
13842  */
13843 
13844 static void
13845 sd_destroypkt_for_buf(struct buf *bp)
13846 {
13847 	ASSERT(bp != NULL);
13848 	ASSERT(SD_GET_UN(bp) != NULL);
13849 
13850 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13851 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13852 
13853 	ASSERT(SD_GET_PKTP(bp) != NULL);
13854 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13855 
13856 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13857 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13858 }
13859 
13860 /*
13861  *    Function: sd_setup_rw_pkt
13862  *
13863  * Description: Determines appropriate CDB group for the requested LBA
13864  *		and transfer length, calls scsi_init_pkt, and builds
13865  *		the CDB.  Do not use for partial DMA transfers except
13866  *		for the initial transfer since the CDB size must
13867  *		remain constant.
13868  *
13869  *     Context: Kernel thread and may be called from software interrupt
13870  *		context as part of a sdrunout callback. This function may not
13871  *		block or call routines that block
13872  */
13873 
13874 
13875 int
13876 sd_setup_rw_pkt(struct sd_lun *un,
13877     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13878     int (*callback)(caddr_t), caddr_t callback_arg,
13879     diskaddr_t lba, uint32_t blockcount)
13880 {
13881 	struct scsi_pkt *return_pktp;
13882 	union scsi_cdb *cdbp;
13883 	struct sd_cdbinfo *cp = NULL;
13884 	int i;
13885 
13886 	/*
13887 	 * See which size CDB to use, based upon the request.
13888 	 */
13889 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13890 
13891 		/*
13892 		 * Check lba and block count against sd_cdbtab limits.
13893 		 * In the partial DMA case, we have to use the same size
13894 		 * CDB for all the transfers.  Check lba + blockcount
13895 		 * against the max LBA so we know that segment of the
13896 		 * transfer can use the CDB we select.
13897 		 */
13898 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13899 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13900 
13901 			/*
13902 			 * The command will fit into the CDB type
13903 			 * specified by sd_cdbtab[i].
13904 			 */
13905 			cp = sd_cdbtab + i;
13906 
13907 			/*
13908 			 * Call scsi_init_pkt so we can fill in the
13909 			 * CDB.
13910 			 */
13911 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13912 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13913 			    flags, callback, callback_arg);
13914 
13915 			if (return_pktp != NULL) {
13916 
13917 				/*
13918 				 * Return new value of pkt
13919 				 */
13920 				*pktpp = return_pktp;
13921 
13922 				/*
13923 				 * To be safe, zero the CDB insuring there is
13924 				 * no leftover data from a previous command.
13925 				 */
13926 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13927 
13928 				/*
13929 				 * Handle partial DMA mapping
13930 				 */
13931 				if (return_pktp->pkt_resid != 0) {
13932 
13933 					/*
13934 					 * Not going to xfer as many blocks as
13935 					 * originally expected
13936 					 */
13937 					blockcount -=
13938 					    SD_BYTES2TGTBLOCKS(un,
13939 					    return_pktp->pkt_resid);
13940 				}
13941 
13942 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13943 
13944 				/*
13945 				 * Set command byte based on the CDB
13946 				 * type we matched.
13947 				 */
13948 				cdbp->scc_cmd = cp->sc_grpmask |
13949 				    ((bp->b_flags & B_READ) ?
13950 				    SCMD_READ : SCMD_WRITE);
13951 
13952 				SD_FILL_SCSI1_LUN(un, return_pktp);
13953 
13954 				/*
13955 				 * Fill in LBA and length
13956 				 */
13957 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13958 				    (cp->sc_grpcode == CDB_GROUP4) ||
13959 				    (cp->sc_grpcode == CDB_GROUP0) ||
13960 				    (cp->sc_grpcode == CDB_GROUP5));
13961 
13962 				if (cp->sc_grpcode == CDB_GROUP1) {
13963 					FORMG1ADDR(cdbp, lba);
13964 					FORMG1COUNT(cdbp, blockcount);
13965 					return (0);
13966 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13967 					FORMG4LONGADDR(cdbp, lba);
13968 					FORMG4COUNT(cdbp, blockcount);
13969 					return (0);
13970 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13971 					FORMG0ADDR(cdbp, lba);
13972 					FORMG0COUNT(cdbp, blockcount);
13973 					return (0);
13974 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13975 					FORMG5ADDR(cdbp, lba);
13976 					FORMG5COUNT(cdbp, blockcount);
13977 					return (0);
13978 				}
13979 
13980 				/*
13981 				 * It should be impossible to not match one
13982 				 * of the CDB types above, so we should never
13983 				 * reach this point.  Set the CDB command byte
13984 				 * to test-unit-ready to avoid writing
13985 				 * to somewhere we don't intend.
13986 				 */
13987 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13988 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13989 			} else {
13990 				/*
13991 				 * Couldn't get scsi_pkt
13992 				 */
13993 				return (SD_PKT_ALLOC_FAILURE);
13994 			}
13995 		}
13996 	}
13997 
13998 	/*
13999 	 * None of the available CDB types were suitable.  This really
14000 	 * should never happen:  on a 64 bit system we support
14001 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
14002 	 * and on a 32 bit system we will refuse to bind to a device
14003 	 * larger than 2TB so addresses will never be larger than 32 bits.
14004 	 */
14005 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14006 }
14007 
14008 /*
14009  *    Function: sd_setup_next_rw_pkt
14010  *
14011  * Description: Setup packet for partial DMA transfers, except for the
14012  * 		initial transfer.  sd_setup_rw_pkt should be used for
14013  *		the initial transfer.
14014  *
14015  *     Context: Kernel thread and may be called from interrupt context.
14016  */
14017 
14018 int
14019 sd_setup_next_rw_pkt(struct sd_lun *un,
14020     struct scsi_pkt *pktp, struct buf *bp,
14021     diskaddr_t lba, uint32_t blockcount)
14022 {
14023 	uchar_t com;
14024 	union scsi_cdb *cdbp;
14025 	uchar_t cdb_group_id;
14026 
14027 	ASSERT(pktp != NULL);
14028 	ASSERT(pktp->pkt_cdbp != NULL);
14029 
14030 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
14031 	com = cdbp->scc_cmd;
14032 	cdb_group_id = CDB_GROUPID(com);
14033 
14034 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
14035 	    (cdb_group_id == CDB_GROUPID_1) ||
14036 	    (cdb_group_id == CDB_GROUPID_4) ||
14037 	    (cdb_group_id == CDB_GROUPID_5));
14038 
14039 	/*
14040 	 * Move pkt to the next portion of the xfer.
14041 	 * func is NULL_FUNC so we do not have to release
14042 	 * the disk mutex here.
14043 	 */
14044 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
14045 	    NULL_FUNC, NULL) == pktp) {
14046 		/* Success.  Handle partial DMA */
14047 		if (pktp->pkt_resid != 0) {
14048 			blockcount -=
14049 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
14050 		}
14051 
14052 		cdbp->scc_cmd = com;
14053 		SD_FILL_SCSI1_LUN(un, pktp);
14054 		if (cdb_group_id == CDB_GROUPID_1) {
14055 			FORMG1ADDR(cdbp, lba);
14056 			FORMG1COUNT(cdbp, blockcount);
14057 			return (0);
14058 		} else if (cdb_group_id == CDB_GROUPID_4) {
14059 			FORMG4LONGADDR(cdbp, lba);
14060 			FORMG4COUNT(cdbp, blockcount);
14061 			return (0);
14062 		} else if (cdb_group_id == CDB_GROUPID_0) {
14063 			FORMG0ADDR(cdbp, lba);
14064 			FORMG0COUNT(cdbp, blockcount);
14065 			return (0);
14066 		} else if (cdb_group_id == CDB_GROUPID_5) {
14067 			FORMG5ADDR(cdbp, lba);
14068 			FORMG5COUNT(cdbp, blockcount);
14069 			return (0);
14070 		}
14071 
14072 		/* Unreachable */
14073 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
14074 	}
14075 
14076 	/*
14077 	 * Error setting up next portion of cmd transfer.
14078 	 * Something is definitely very wrong and this
14079 	 * should not happen.
14080 	 */
14081 	return (SD_PKT_ALLOC_FAILURE);
14082 }
14083 
14084 /*
14085  *    Function: sd_initpkt_for_uscsi
14086  *
14087  * Description: Allocate and initialize for transport a scsi_pkt struct,
14088  *		based upon the info specified in the given uscsi_cmd struct.
14089  *
14090  * Return Code: SD_PKT_ALLOC_SUCCESS
14091  *		SD_PKT_ALLOC_FAILURE
14092  *		SD_PKT_ALLOC_FAILURE_NO_DMA
14093  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
14094  *
14095  *     Context: Kernel thread and may be called from software interrupt context
14096  *		as part of a sdrunout callback. This function may not block or
14097  *		call routines that block
14098  */
14099 
14100 static int
14101 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
14102 {
14103 	struct uscsi_cmd *uscmd;
14104 	struct sd_xbuf	*xp;
14105 	struct scsi_pkt	*pktp;
14106 	struct sd_lun	*un;
14107 	uint32_t	flags = 0;
14108 
14109 	ASSERT(bp != NULL);
14110 	ASSERT(pktpp != NULL);
14111 	xp = SD_GET_XBUF(bp);
14112 	ASSERT(xp != NULL);
14113 	un = SD_GET_UN(bp);
14114 	ASSERT(un != NULL);
14115 	ASSERT(mutex_owned(SD_MUTEX(un)));
14116 
14117 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14118 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14119 	ASSERT(uscmd != NULL);
14120 
14121 	SD_TRACE(SD_LOG_IO_CORE, un,
14122 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
14123 
14124 	/*
14125 	 * Allocate the scsi_pkt for the command.
14126 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
14127 	 *	 during scsi_init_pkt time and will continue to use the
14128 	 *	 same path as long as the same scsi_pkt is used without
14129 	 *	 intervening scsi_dma_free(). Since uscsi command does
14130 	 *	 not call scsi_dmafree() before retry failed command, it
14131 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
14132 	 *	 set such that scsi_vhci can use other available path for
14133 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
14134 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
14135 	 */
14136 	if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14137 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14138 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14139 		    ((int)(uscmd->uscsi_rqlen) + sizeof (struct scsi_arq_status)
14140 		    - sizeof (struct scsi_extended_sense)), 0,
14141 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL) | PKT_XARQ,
14142 		    sdrunout, (caddr_t)un);
14143 	} else {
14144 		pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
14145 		    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
14146 		    sizeof (struct scsi_arq_status), 0,
14147 		    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
14148 		    sdrunout, (caddr_t)un);
14149 	}
14150 
14151 	if (pktp == NULL) {
14152 		*pktpp = NULL;
14153 		/*
14154 		 * Set the driver state to RWAIT to indicate the driver
14155 		 * is waiting on resource allocations. The driver will not
14156 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
14157 		 */
14158 		New_state(un, SD_STATE_RWAIT);
14159 
14160 		SD_ERROR(SD_LOG_IO_CORE, un,
14161 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
14162 
14163 		if ((bp->b_flags & B_ERROR) != 0) {
14164 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
14165 		}
14166 		return (SD_PKT_ALLOC_FAILURE);
14167 	}
14168 
14169 	/*
14170 	 * We do not do DMA breakup for USCSI commands, so return failure
14171 	 * here if all the needed DMA resources were not allocated.
14172 	 */
14173 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
14174 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
14175 		scsi_destroy_pkt(pktp);
14176 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
14177 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
14178 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
14179 	}
14180 
14181 	/* Init the cdb from the given uscsi struct */
14182 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
14183 	    uscmd->uscsi_cdb[0], 0, 0, 0);
14184 
14185 	SD_FILL_SCSI1_LUN(un, pktp);
14186 
14187 	/*
14188 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
14189 	 * for listing of the supported flags.
14190 	 */
14191 
14192 	if (uscmd->uscsi_flags & USCSI_SILENT) {
14193 		flags |= FLAG_SILENT;
14194 	}
14195 
14196 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
14197 		flags |= FLAG_DIAGNOSE;
14198 	}
14199 
14200 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
14201 		flags |= FLAG_ISOLATE;
14202 	}
14203 
14204 	if (un->un_f_is_fibre == FALSE) {
14205 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
14206 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
14207 		}
14208 	}
14209 
14210 	/*
14211 	 * Set the pkt flags here so we save time later.
14212 	 * Note: These flags are NOT in the uscsi man page!!!
14213 	 */
14214 	if (uscmd->uscsi_flags & USCSI_HEAD) {
14215 		flags |= FLAG_HEAD;
14216 	}
14217 
14218 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
14219 		flags |= FLAG_NOINTR;
14220 	}
14221 
14222 	/*
14223 	 * For tagged queueing, things get a bit complicated.
14224 	 * Check first for head of queue and last for ordered queue.
14225 	 * If neither head nor order, use the default driver tag flags.
14226 	 */
14227 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
14228 		if (uscmd->uscsi_flags & USCSI_HTAG) {
14229 			flags |= FLAG_HTAG;
14230 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
14231 			flags |= FLAG_OTAG;
14232 		} else {
14233 			flags |= un->un_tagflags & FLAG_TAGMASK;
14234 		}
14235 	}
14236 
14237 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
14238 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
14239 	}
14240 
14241 	pktp->pkt_flags = flags;
14242 
14243 	/* Transfer uscsi information to scsi_pkt */
14244 	(void) scsi_uscsi_pktinit(uscmd, pktp);
14245 
14246 	/* Copy the caller's CDB into the pkt... */
14247 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
14248 
14249 	if (uscmd->uscsi_timeout == 0) {
14250 		pktp->pkt_time = un->un_uscsi_timeout;
14251 	} else {
14252 		pktp->pkt_time = uscmd->uscsi_timeout;
14253 	}
14254 
14255 	/* need it later to identify USCSI request in sdintr */
14256 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
14257 
14258 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
14259 
14260 	pktp->pkt_private = bp;
14261 	pktp->pkt_comp = sdintr;
14262 	*pktpp = pktp;
14263 
14264 	SD_TRACE(SD_LOG_IO_CORE, un,
14265 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
14266 
14267 	return (SD_PKT_ALLOC_SUCCESS);
14268 }
14269 
14270 
14271 /*
14272  *    Function: sd_destroypkt_for_uscsi
14273  *
14274  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
14275  *		IOs.. Also saves relevant info into the associated uscsi_cmd
14276  *		struct.
14277  *
14278  *     Context: May be called under interrupt context
14279  */
14280 
14281 static void
14282 sd_destroypkt_for_uscsi(struct buf *bp)
14283 {
14284 	struct uscsi_cmd *uscmd;
14285 	struct sd_xbuf	*xp;
14286 	struct scsi_pkt	*pktp;
14287 	struct sd_lun	*un;
14288 	struct sd_uscsi_info *suip;
14289 
14290 	ASSERT(bp != NULL);
14291 	xp = SD_GET_XBUF(bp);
14292 	ASSERT(xp != NULL);
14293 	un = SD_GET_UN(bp);
14294 	ASSERT(un != NULL);
14295 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14296 	pktp = SD_GET_PKTP(bp);
14297 	ASSERT(pktp != NULL);
14298 
14299 	SD_TRACE(SD_LOG_IO_CORE, un,
14300 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
14301 
14302 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
14303 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
14304 	ASSERT(uscmd != NULL);
14305 
14306 	/* Save the status and the residual into the uscsi_cmd struct */
14307 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
14308 	uscmd->uscsi_resid  = bp->b_resid;
14309 
14310 	/* Transfer scsi_pkt information to uscsi */
14311 	(void) scsi_uscsi_pktfini(pktp, uscmd);
14312 
14313 	/*
14314 	 * If enabled, copy any saved sense data into the area specified
14315 	 * by the uscsi command.
14316 	 */
14317 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
14318 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
14319 		/*
14320 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
14321 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
14322 		 */
14323 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
14324 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
14325 		if (uscmd->uscsi_rqlen > SENSE_LENGTH) {
14326 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14327 			    MAX_SENSE_LENGTH);
14328 		} else {
14329 			bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf,
14330 			    SENSE_LENGTH);
14331 		}
14332 	}
14333 	/*
14334 	 * The following assignments are for SCSI FMA.
14335 	 */
14336 	ASSERT(xp->xb_private != NULL);
14337 	suip = (struct sd_uscsi_info *)xp->xb_private;
14338 	suip->ui_pkt_reason = pktp->pkt_reason;
14339 	suip->ui_pkt_state = pktp->pkt_state;
14340 	suip->ui_pkt_statistics = pktp->pkt_statistics;
14341 	suip->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
14342 
14343 	/* We are done with the scsi_pkt; free it now */
14344 	ASSERT(SD_GET_PKTP(bp) != NULL);
14345 	scsi_destroy_pkt(SD_GET_PKTP(bp));
14346 
14347 	SD_TRACE(SD_LOG_IO_CORE, un,
14348 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
14349 }
14350 
14351 
14352 /*
14353  *    Function: sd_bioclone_alloc
14354  *
14355  * Description: Allocate a buf(9S) and init it as per the given buf
14356  *		and the various arguments.  The associated sd_xbuf
14357  *		struct is (nearly) duplicated.  The struct buf *bp
14358  *		argument is saved in new_xp->xb_private.
14359  *
14360  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14361  *		datalen - size of data area for the shadow bp
14362  *		blkno - starting LBA
14363  *		func - function pointer for b_iodone in the shadow buf. (May
14364  *			be NULL if none.)
14365  *
14366  * Return Code: Pointer to allocates buf(9S) struct
14367  *
14368  *     Context: Can sleep.
14369  */
14370 
14371 static struct buf *
14372 sd_bioclone_alloc(struct buf *bp, size_t datalen,
14373 	daddr_t blkno, int (*func)(struct buf *))
14374 {
14375 	struct	sd_lun	*un;
14376 	struct	sd_xbuf	*xp;
14377 	struct	sd_xbuf	*new_xp;
14378 	struct	buf	*new_bp;
14379 
14380 	ASSERT(bp != NULL);
14381 	xp = SD_GET_XBUF(bp);
14382 	ASSERT(xp != NULL);
14383 	un = SD_GET_UN(bp);
14384 	ASSERT(un != NULL);
14385 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14386 
14387 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
14388 	    NULL, KM_SLEEP);
14389 
14390 	new_bp->b_lblkno	= blkno;
14391 
14392 	/*
14393 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14394 	 * original xbuf into it.
14395 	 */
14396 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14397 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14398 
14399 	/*
14400 	 * The given bp is automatically saved in the xb_private member
14401 	 * of the new xbuf.  Callers are allowed to depend on this.
14402 	 */
14403 	new_xp->xb_private = bp;
14404 
14405 	new_bp->b_private  = new_xp;
14406 
14407 	return (new_bp);
14408 }
14409 
14410 /*
14411  *    Function: sd_shadow_buf_alloc
14412  *
14413  * Description: Allocate a buf(9S) and init it as per the given buf
14414  *		and the various arguments.  The associated sd_xbuf
14415  *		struct is (nearly) duplicated.  The struct buf *bp
14416  *		argument is saved in new_xp->xb_private.
14417  *
14418  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
14419  *		datalen - size of data area for the shadow bp
14420  *		bflags - B_READ or B_WRITE (pseudo flag)
14421  *		blkno - starting LBA
14422  *		func - function pointer for b_iodone in the shadow buf. (May
14423  *			be NULL if none.)
14424  *
14425  * Return Code: Pointer to allocates buf(9S) struct
14426  *
14427  *     Context: Can sleep.
14428  */
14429 
14430 static struct buf *
14431 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
14432 	daddr_t blkno, int (*func)(struct buf *))
14433 {
14434 	struct	sd_lun	*un;
14435 	struct	sd_xbuf	*xp;
14436 	struct	sd_xbuf	*new_xp;
14437 	struct	buf	*new_bp;
14438 
14439 	ASSERT(bp != NULL);
14440 	xp = SD_GET_XBUF(bp);
14441 	ASSERT(xp != NULL);
14442 	un = SD_GET_UN(bp);
14443 	ASSERT(un != NULL);
14444 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14445 
14446 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
14447 		bp_mapin(bp);
14448 	}
14449 
14450 	bflags &= (B_READ | B_WRITE);
14451 #if defined(__i386) || defined(__amd64)
14452 	new_bp = getrbuf(KM_SLEEP);
14453 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
14454 	new_bp->b_bcount = datalen;
14455 	new_bp->b_flags = bflags |
14456 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
14457 #else
14458 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
14459 	    datalen, bflags, SLEEP_FUNC, NULL);
14460 #endif
14461 	new_bp->av_forw	= NULL;
14462 	new_bp->av_back	= NULL;
14463 	new_bp->b_dev	= bp->b_dev;
14464 	new_bp->b_blkno	= blkno;
14465 	new_bp->b_iodone = func;
14466 	new_bp->b_edev	= bp->b_edev;
14467 	new_bp->b_resid	= 0;
14468 
14469 	/* We need to preserve the B_FAILFAST flag */
14470 	if (bp->b_flags & B_FAILFAST) {
14471 		new_bp->b_flags |= B_FAILFAST;
14472 	}
14473 
14474 	/*
14475 	 * Allocate an xbuf for the shadow bp and copy the contents of the
14476 	 * original xbuf into it.
14477 	 */
14478 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
14479 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
14480 
14481 	/* Need later to copy data between the shadow buf & original buf! */
14482 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
14483 
14484 	/*
14485 	 * The given bp is automatically saved in the xb_private member
14486 	 * of the new xbuf.  Callers are allowed to depend on this.
14487 	 */
14488 	new_xp->xb_private = bp;
14489 
14490 	new_bp->b_private  = new_xp;
14491 
14492 	return (new_bp);
14493 }
14494 
14495 /*
14496  *    Function: sd_bioclone_free
14497  *
14498  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
14499  *		in the larger than partition operation.
14500  *
14501  *     Context: May be called under interrupt context
14502  */
14503 
14504 static void
14505 sd_bioclone_free(struct buf *bp)
14506 {
14507 	struct sd_xbuf	*xp;
14508 
14509 	ASSERT(bp != NULL);
14510 	xp = SD_GET_XBUF(bp);
14511 	ASSERT(xp != NULL);
14512 
14513 	/*
14514 	 * Call bp_mapout() before freeing the buf,  in case a lower
14515 	 * layer or HBA  had done a bp_mapin().  we must do this here
14516 	 * as we are the "originator" of the shadow buf.
14517 	 */
14518 	bp_mapout(bp);
14519 
14520 	/*
14521 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14522 	 * never gets confused by a stale value in this field. (Just a little
14523 	 * extra defensiveness here.)
14524 	 */
14525 	bp->b_iodone = NULL;
14526 
14527 	freerbuf(bp);
14528 
14529 	kmem_free(xp, sizeof (struct sd_xbuf));
14530 }
14531 
14532 /*
14533  *    Function: sd_shadow_buf_free
14534  *
14535  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
14536  *
14537  *     Context: May be called under interrupt context
14538  */
14539 
14540 static void
14541 sd_shadow_buf_free(struct buf *bp)
14542 {
14543 	struct sd_xbuf	*xp;
14544 
14545 	ASSERT(bp != NULL);
14546 	xp = SD_GET_XBUF(bp);
14547 	ASSERT(xp != NULL);
14548 
14549 #if defined(__sparc)
14550 	/*
14551 	 * Call bp_mapout() before freeing the buf,  in case a lower
14552 	 * layer or HBA  had done a bp_mapin().  we must do this here
14553 	 * as we are the "originator" of the shadow buf.
14554 	 */
14555 	bp_mapout(bp);
14556 #endif
14557 
14558 	/*
14559 	 * Null out b_iodone before freeing the bp, to ensure that the driver
14560 	 * never gets confused by a stale value in this field. (Just a little
14561 	 * extra defensiveness here.)
14562 	 */
14563 	bp->b_iodone = NULL;
14564 
14565 #if defined(__i386) || defined(__amd64)
14566 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
14567 	freerbuf(bp);
14568 #else
14569 	scsi_free_consistent_buf(bp);
14570 #endif
14571 
14572 	kmem_free(xp, sizeof (struct sd_xbuf));
14573 }
14574 
14575 
14576 /*
14577  *    Function: sd_print_transport_rejected_message
14578  *
14579  * Description: This implements the ludicrously complex rules for printing
14580  *		a "transport rejected" message.  This is to address the
14581  *		specific problem of having a flood of this error message
14582  *		produced when a failover occurs.
14583  *
14584  *     Context: Any.
14585  */
14586 
14587 static void
14588 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
14589 	int code)
14590 {
14591 	ASSERT(un != NULL);
14592 	ASSERT(mutex_owned(SD_MUTEX(un)));
14593 	ASSERT(xp != NULL);
14594 
14595 	/*
14596 	 * Print the "transport rejected" message under the following
14597 	 * conditions:
14598 	 *
14599 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
14600 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
14601 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
14602 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
14603 	 *   scsi_transport(9F) (which indicates that the target might have
14604 	 *   gone off-line).  This uses the un->un_tran_fatal_count
14605 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
14606 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
14607 	 *   from scsi_transport().
14608 	 *
14609 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
14610 	 * the preceeding cases in order for the message to be printed.
14611 	 */
14612 	if (((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) &&
14613 	    (SD_FM_LOG(un) == SD_FM_LOG_NSUP)) {
14614 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14615 		    (code != TRAN_FATAL_ERROR) ||
14616 		    (un->un_tran_fatal_count == 1)) {
14617 			switch (code) {
14618 			case TRAN_BADPKT:
14619 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14620 				    "transport rejected bad packet\n");
14621 				break;
14622 			case TRAN_FATAL_ERROR:
14623 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14624 				    "transport rejected fatal error\n");
14625 				break;
14626 			default:
14627 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14628 				    "transport rejected (%d)\n", code);
14629 				break;
14630 			}
14631 		}
14632 	}
14633 }
14634 
14635 
14636 /*
14637  *    Function: sd_add_buf_to_waitq
14638  *
14639  * Description: Add the given buf(9S) struct to the wait queue for the
14640  *		instance.  If sorting is enabled, then the buf is added
14641  *		to the queue via an elevator sort algorithm (a la
14642  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14643  *		If sorting is not enabled, then the buf is just added
14644  *		to the end of the wait queue.
14645  *
14646  * Return Code: void
14647  *
14648  *     Context: Does not sleep/block, therefore technically can be called
14649  *		from any context.  However if sorting is enabled then the
14650  *		execution time is indeterminate, and may take long if
14651  *		the wait queue grows large.
14652  */
14653 
14654 static void
14655 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14656 {
14657 	struct buf *ap;
14658 
14659 	ASSERT(bp != NULL);
14660 	ASSERT(un != NULL);
14661 	ASSERT(mutex_owned(SD_MUTEX(un)));
14662 
14663 	/* If the queue is empty, add the buf as the only entry & return. */
14664 	if (un->un_waitq_headp == NULL) {
14665 		ASSERT(un->un_waitq_tailp == NULL);
14666 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14667 		bp->av_forw = NULL;
14668 		return;
14669 	}
14670 
14671 	ASSERT(un->un_waitq_tailp != NULL);
14672 
14673 	/*
14674 	 * If sorting is disabled, just add the buf to the tail end of
14675 	 * the wait queue and return.
14676 	 */
14677 	if (un->un_f_disksort_disabled || un->un_f_enable_rmw) {
14678 		un->un_waitq_tailp->av_forw = bp;
14679 		un->un_waitq_tailp = bp;
14680 		bp->av_forw = NULL;
14681 		return;
14682 	}
14683 
14684 	/*
14685 	 * Sort thru the list of requests currently on the wait queue
14686 	 * and add the new buf request at the appropriate position.
14687 	 *
14688 	 * The un->un_waitq_headp is an activity chain pointer on which
14689 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14690 	 * first queue holds those requests which are positioned after
14691 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14692 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14693 	 * Thus we implement a one way scan, retracting after reaching
14694 	 * the end of the drive to the first request on the second
14695 	 * queue, at which time it becomes the first queue.
14696 	 * A one-way scan is natural because of the way UNIX read-ahead
14697 	 * blocks are allocated.
14698 	 *
14699 	 * If we lie after the first request, then we must locate the
14700 	 * second request list and add ourselves to it.
14701 	 */
14702 	ap = un->un_waitq_headp;
14703 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14704 		while (ap->av_forw != NULL) {
14705 			/*
14706 			 * Look for an "inversion" in the (normally
14707 			 * ascending) block numbers. This indicates
14708 			 * the start of the second request list.
14709 			 */
14710 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14711 				/*
14712 				 * Search the second request list for the
14713 				 * first request at a larger block number.
14714 				 * We go before that; however if there is
14715 				 * no such request, we go at the end.
14716 				 */
14717 				do {
14718 					if (SD_GET_BLKNO(bp) <
14719 					    SD_GET_BLKNO(ap->av_forw)) {
14720 						goto insert;
14721 					}
14722 					ap = ap->av_forw;
14723 				} while (ap->av_forw != NULL);
14724 				goto insert;		/* after last */
14725 			}
14726 			ap = ap->av_forw;
14727 		}
14728 
14729 		/*
14730 		 * No inversions... we will go after the last, and
14731 		 * be the first request in the second request list.
14732 		 */
14733 		goto insert;
14734 	}
14735 
14736 	/*
14737 	 * Request is at/after the current request...
14738 	 * sort in the first request list.
14739 	 */
14740 	while (ap->av_forw != NULL) {
14741 		/*
14742 		 * We want to go after the current request (1) if
14743 		 * there is an inversion after it (i.e. it is the end
14744 		 * of the first request list), or (2) if the next
14745 		 * request is a larger block no. than our request.
14746 		 */
14747 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14748 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14749 			goto insert;
14750 		}
14751 		ap = ap->av_forw;
14752 	}
14753 
14754 	/*
14755 	 * Neither a second list nor a larger request, therefore
14756 	 * we go at the end of the first list (which is the same
14757 	 * as the end of the whole schebang).
14758 	 */
14759 insert:
14760 	bp->av_forw = ap->av_forw;
14761 	ap->av_forw = bp;
14762 
14763 	/*
14764 	 * If we inserted onto the tail end of the waitq, make sure the
14765 	 * tail pointer is updated.
14766 	 */
14767 	if (ap == un->un_waitq_tailp) {
14768 		un->un_waitq_tailp = bp;
14769 	}
14770 }
14771 
14772 
14773 /*
14774  *    Function: sd_start_cmds
14775  *
14776  * Description: Remove and transport cmds from the driver queues.
14777  *
14778  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14779  *
14780  *		immed_bp - ptr to a buf to be transported immediately. Only
14781  *		the immed_bp is transported; bufs on the waitq are not
14782  *		processed and the un_retry_bp is not checked.  If immed_bp is
14783  *		NULL, then normal queue processing is performed.
14784  *
14785  *     Context: May be called from kernel thread context, interrupt context,
14786  *		or runout callback context. This function may not block or
14787  *		call routines that block.
14788  */
14789 
14790 static void
14791 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14792 {
14793 	struct	sd_xbuf	*xp;
14794 	struct	buf	*bp;
14795 	void	(*statp)(kstat_io_t *);
14796 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14797 	void	(*saved_statp)(kstat_io_t *);
14798 #endif
14799 	int	rval;
14800 	struct sd_fm_internal *sfip = NULL;
14801 
14802 	ASSERT(un != NULL);
14803 	ASSERT(mutex_owned(SD_MUTEX(un)));
14804 	ASSERT(un->un_ncmds_in_transport >= 0);
14805 	ASSERT(un->un_throttle >= 0);
14806 
14807 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14808 
14809 	do {
14810 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14811 		saved_statp = NULL;
14812 #endif
14813 
14814 		/*
14815 		 * If we are syncing or dumping, fail the command to
14816 		 * avoid recursively calling back into scsi_transport().
14817 		 * The dump I/O itself uses a separate code path so this
14818 		 * only prevents non-dump I/O from being sent while dumping.
14819 		 * File system sync takes place before dumping begins.
14820 		 * During panic, filesystem I/O is allowed provided
14821 		 * un_in_callback is <= 1.  This is to prevent recursion
14822 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14823 		 * sd_start_cmds and so on.  See panic.c for more information
14824 		 * about the states the system can be in during panic.
14825 		 */
14826 		if ((un->un_state == SD_STATE_DUMPING) ||
14827 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14828 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14829 			    "sd_start_cmds: panicking\n");
14830 			goto exit;
14831 		}
14832 
14833 		if ((bp = immed_bp) != NULL) {
14834 			/*
14835 			 * We have a bp that must be transported immediately.
14836 			 * It's OK to transport the immed_bp here without doing
14837 			 * the throttle limit check because the immed_bp is
14838 			 * always used in a retry/recovery case. This means
14839 			 * that we know we are not at the throttle limit by
14840 			 * virtue of the fact that to get here we must have
14841 			 * already gotten a command back via sdintr(). This also
14842 			 * relies on (1) the command on un_retry_bp preventing
14843 			 * further commands from the waitq from being issued;
14844 			 * and (2) the code in sd_retry_command checking the
14845 			 * throttle limit before issuing a delayed or immediate
14846 			 * retry. This holds even if the throttle limit is
14847 			 * currently ratcheted down from its maximum value.
14848 			 */
14849 			statp = kstat_runq_enter;
14850 			if (bp == un->un_retry_bp) {
14851 				ASSERT((un->un_retry_statp == NULL) ||
14852 				    (un->un_retry_statp == kstat_waitq_enter) ||
14853 				    (un->un_retry_statp ==
14854 				    kstat_runq_back_to_waitq));
14855 				/*
14856 				 * If the waitq kstat was incremented when
14857 				 * sd_set_retry_bp() queued this bp for a retry,
14858 				 * then we must set up statp so that the waitq
14859 				 * count will get decremented correctly below.
14860 				 * Also we must clear un->un_retry_statp to
14861 				 * ensure that we do not act on a stale value
14862 				 * in this field.
14863 				 */
14864 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14865 				    (un->un_retry_statp ==
14866 				    kstat_runq_back_to_waitq)) {
14867 					statp = kstat_waitq_to_runq;
14868 				}
14869 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14870 				saved_statp = un->un_retry_statp;
14871 #endif
14872 				un->un_retry_statp = NULL;
14873 
14874 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14875 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14876 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14877 				    un, un->un_retry_bp, un->un_throttle,
14878 				    un->un_ncmds_in_transport);
14879 			} else {
14880 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14881 				    "processing priority bp:0x%p\n", bp);
14882 			}
14883 
14884 		} else if ((bp = un->un_waitq_headp) != NULL) {
14885 			/*
14886 			 * A command on the waitq is ready to go, but do not
14887 			 * send it if:
14888 			 *
14889 			 * (1) the throttle limit has been reached, or
14890 			 * (2) a retry is pending, or
14891 			 * (3) a START_STOP_UNIT callback pending, or
14892 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14893 			 *	command is pending.
14894 			 *
14895 			 * For all of these conditions, IO processing will
14896 			 * restart after the condition is cleared.
14897 			 */
14898 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14899 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14900 				    "sd_start_cmds: exiting, "
14901 				    "throttle limit reached!\n");
14902 				goto exit;
14903 			}
14904 			if (un->un_retry_bp != NULL) {
14905 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14906 				    "sd_start_cmds: exiting, retry pending!\n");
14907 				goto exit;
14908 			}
14909 			if (un->un_startstop_timeid != NULL) {
14910 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14911 				    "sd_start_cmds: exiting, "
14912 				    "START_STOP pending!\n");
14913 				goto exit;
14914 			}
14915 			if (un->un_direct_priority_timeid != NULL) {
14916 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14917 				    "sd_start_cmds: exiting, "
14918 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14919 				goto exit;
14920 			}
14921 
14922 			/* Dequeue the command */
14923 			un->un_waitq_headp = bp->av_forw;
14924 			if (un->un_waitq_headp == NULL) {
14925 				un->un_waitq_tailp = NULL;
14926 			}
14927 			bp->av_forw = NULL;
14928 			statp = kstat_waitq_to_runq;
14929 			SD_TRACE(SD_LOG_IO_CORE, un,
14930 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14931 
14932 		} else {
14933 			/* No work to do so bail out now */
14934 			SD_TRACE(SD_LOG_IO_CORE, un,
14935 			    "sd_start_cmds: no more work, exiting!\n");
14936 			goto exit;
14937 		}
14938 
14939 		/*
14940 		 * Reset the state to normal. This is the mechanism by which
14941 		 * the state transitions from either SD_STATE_RWAIT or
14942 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14943 		 * If state is SD_STATE_PM_CHANGING then this command is
14944 		 * part of the device power control and the state must
14945 		 * not be put back to normal. Doing so would would
14946 		 * allow new commands to proceed when they shouldn't,
14947 		 * the device may be going off.
14948 		 */
14949 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14950 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14951 			New_state(un, SD_STATE_NORMAL);
14952 		}
14953 
14954 		xp = SD_GET_XBUF(bp);
14955 		ASSERT(xp != NULL);
14956 
14957 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14958 		/*
14959 		 * Allocate the scsi_pkt if we need one, or attach DMA
14960 		 * resources if we have a scsi_pkt that needs them. The
14961 		 * latter should only occur for commands that are being
14962 		 * retried.
14963 		 */
14964 		if ((xp->xb_pktp == NULL) ||
14965 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14966 #else
14967 		if (xp->xb_pktp == NULL) {
14968 #endif
14969 			/*
14970 			 * There is no scsi_pkt allocated for this buf. Call
14971 			 * the initpkt function to allocate & init one.
14972 			 *
14973 			 * The scsi_init_pkt runout callback functionality is
14974 			 * implemented as follows:
14975 			 *
14976 			 * 1) The initpkt function always calls
14977 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14978 			 *    callback routine.
14979 			 * 2) A successful packet allocation is initialized and
14980 			 *    the I/O is transported.
14981 			 * 3) The I/O associated with an allocation resource
14982 			 *    failure is left on its queue to be retried via
14983 			 *    runout or the next I/O.
14984 			 * 4) The I/O associated with a DMA error is removed
14985 			 *    from the queue and failed with EIO. Processing of
14986 			 *    the transport queues is also halted to be
14987 			 *    restarted via runout or the next I/O.
14988 			 * 5) The I/O associated with a CDB size or packet
14989 			 *    size error is removed from the queue and failed
14990 			 *    with EIO. Processing of the transport queues is
14991 			 *    continued.
14992 			 *
14993 			 * Note: there is no interface for canceling a runout
14994 			 * callback. To prevent the driver from detaching or
14995 			 * suspending while a runout is pending the driver
14996 			 * state is set to SD_STATE_RWAIT
14997 			 *
14998 			 * Note: using the scsi_init_pkt callback facility can
14999 			 * result in an I/O request persisting at the head of
15000 			 * the list which cannot be satisfied even after
15001 			 * multiple retries. In the future the driver may
15002 			 * implement some kind of maximum runout count before
15003 			 * failing an I/O.
15004 			 *
15005 			 * Note: the use of funcp below may seem superfluous,
15006 			 * but it helps warlock figure out the correct
15007 			 * initpkt function calls (see [s]sd.wlcmd).
15008 			 */
15009 			struct scsi_pkt	*pktp;
15010 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
15011 
15012 			ASSERT(bp != un->un_rqs_bp);
15013 
15014 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
15015 			switch ((*funcp)(bp, &pktp)) {
15016 			case  SD_PKT_ALLOC_SUCCESS:
15017 				xp->xb_pktp = pktp;
15018 				SD_TRACE(SD_LOG_IO_CORE, un,
15019 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
15020 				    pktp);
15021 				goto got_pkt;
15022 
15023 			case SD_PKT_ALLOC_FAILURE:
15024 				/*
15025 				 * Temporary (hopefully) resource depletion.
15026 				 * Since retries and RQS commands always have a
15027 				 * scsi_pkt allocated, these cases should never
15028 				 * get here. So the only cases this needs to
15029 				 * handle is a bp from the waitq (which we put
15030 				 * back onto the waitq for sdrunout), or a bp
15031 				 * sent as an immed_bp (which we just fail).
15032 				 */
15033 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15034 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
15035 
15036 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15037 
15038 				if (bp == immed_bp) {
15039 					/*
15040 					 * If SD_XB_DMA_FREED is clear, then
15041 					 * this is a failure to allocate a
15042 					 * scsi_pkt, and we must fail the
15043 					 * command.
15044 					 */
15045 					if ((xp->xb_pkt_flags &
15046 					    SD_XB_DMA_FREED) == 0) {
15047 						break;
15048 					}
15049 
15050 					/*
15051 					 * If this immediate command is NOT our
15052 					 * un_retry_bp, then we must fail it.
15053 					 */
15054 					if (bp != un->un_retry_bp) {
15055 						break;
15056 					}
15057 
15058 					/*
15059 					 * We get here if this cmd is our
15060 					 * un_retry_bp that was DMAFREED, but
15061 					 * scsi_init_pkt() failed to reallocate
15062 					 * DMA resources when we attempted to
15063 					 * retry it. This can happen when an
15064 					 * mpxio failover is in progress, but
15065 					 * we don't want to just fail the
15066 					 * command in this case.
15067 					 *
15068 					 * Use timeout(9F) to restart it after
15069 					 * a 100ms delay.  We don't want to
15070 					 * let sdrunout() restart it, because
15071 					 * sdrunout() is just supposed to start
15072 					 * commands that are sitting on the
15073 					 * wait queue.  The un_retry_bp stays
15074 					 * set until the command completes, but
15075 					 * sdrunout can be called many times
15076 					 * before that happens.  Since sdrunout
15077 					 * cannot tell if the un_retry_bp is
15078 					 * already in the transport, it could
15079 					 * end up calling scsi_transport() for
15080 					 * the un_retry_bp multiple times.
15081 					 *
15082 					 * Also: don't schedule the callback
15083 					 * if some other callback is already
15084 					 * pending.
15085 					 */
15086 					if (un->un_retry_statp == NULL) {
15087 						/*
15088 						 * restore the kstat pointer to
15089 						 * keep kstat counts coherent
15090 						 * when we do retry the command.
15091 						 */
15092 						un->un_retry_statp =
15093 						    saved_statp;
15094 					}
15095 
15096 					if ((un->un_startstop_timeid == NULL) &&
15097 					    (un->un_retry_timeid == NULL) &&
15098 					    (un->un_direct_priority_timeid ==
15099 					    NULL)) {
15100 
15101 						un->un_retry_timeid =
15102 						    timeout(
15103 						    sd_start_retry_command,
15104 						    un, SD_RESTART_TIMEOUT);
15105 					}
15106 					goto exit;
15107 				}
15108 
15109 #else
15110 				if (bp == immed_bp) {
15111 					break;	/* Just fail the command */
15112 				}
15113 #endif
15114 
15115 				/* Add the buf back to the head of the waitq */
15116 				bp->av_forw = un->un_waitq_headp;
15117 				un->un_waitq_headp = bp;
15118 				if (un->un_waitq_tailp == NULL) {
15119 					un->un_waitq_tailp = bp;
15120 				}
15121 				goto exit;
15122 
15123 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
15124 				/*
15125 				 * HBA DMA resource failure. Fail the command
15126 				 * and continue processing of the queues.
15127 				 */
15128 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15129 				    "sd_start_cmds: "
15130 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
15131 				break;
15132 
15133 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
15134 				/*
15135 				 * Note:x86: Partial DMA mapping not supported
15136 				 * for USCSI commands, and all the needed DMA
15137 				 * resources were not allocated.
15138 				 */
15139 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15140 				    "sd_start_cmds: "
15141 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
15142 				break;
15143 
15144 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
15145 				/*
15146 				 * Note:x86: Request cannot fit into CDB based
15147 				 * on lba and len.
15148 				 */
15149 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15150 				    "sd_start_cmds: "
15151 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
15152 				break;
15153 
15154 			default:
15155 				/* Should NEVER get here! */
15156 				panic("scsi_initpkt error");
15157 				/*NOTREACHED*/
15158 			}
15159 
15160 			/*
15161 			 * Fatal error in allocating a scsi_pkt for this buf.
15162 			 * Update kstats & return the buf with an error code.
15163 			 * We must use sd_return_failed_command_no_restart() to
15164 			 * avoid a recursive call back into sd_start_cmds().
15165 			 * However this also means that we must keep processing
15166 			 * the waitq here in order to avoid stalling.
15167 			 */
15168 			if (statp == kstat_waitq_to_runq) {
15169 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
15170 			}
15171 			sd_return_failed_command_no_restart(un, bp, EIO);
15172 			if (bp == immed_bp) {
15173 				/* immed_bp is gone by now, so clear this */
15174 				immed_bp = NULL;
15175 			}
15176 			continue;
15177 		}
15178 got_pkt:
15179 		if (bp == immed_bp) {
15180 			/* goto the head of the class.... */
15181 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15182 		}
15183 
15184 		un->un_ncmds_in_transport++;
15185 		SD_UPDATE_KSTATS(un, statp, bp);
15186 
15187 		/*
15188 		 * Call scsi_transport() to send the command to the target.
15189 		 * According to SCSA architecture, we must drop the mutex here
15190 		 * before calling scsi_transport() in order to avoid deadlock.
15191 		 * Note that the scsi_pkt's completion routine can be executed
15192 		 * (from interrupt context) even before the call to
15193 		 * scsi_transport() returns.
15194 		 */
15195 		SD_TRACE(SD_LOG_IO_CORE, un,
15196 		    "sd_start_cmds: calling scsi_transport()\n");
15197 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
15198 
15199 		mutex_exit(SD_MUTEX(un));
15200 		rval = scsi_transport(xp->xb_pktp);
15201 		mutex_enter(SD_MUTEX(un));
15202 
15203 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15204 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
15205 
15206 		switch (rval) {
15207 		case TRAN_ACCEPT:
15208 			/* Clear this with every pkt accepted by the HBA */
15209 			un->un_tran_fatal_count = 0;
15210 			break;	/* Success; try the next cmd (if any) */
15211 
15212 		case TRAN_BUSY:
15213 			un->un_ncmds_in_transport--;
15214 			ASSERT(un->un_ncmds_in_transport >= 0);
15215 
15216 			/*
15217 			 * Don't retry request sense, the sense data
15218 			 * is lost when another request is sent.
15219 			 * Free up the rqs buf and retry
15220 			 * the original failed cmd.  Update kstat.
15221 			 */
15222 			if (bp == un->un_rqs_bp) {
15223 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15224 				bp = sd_mark_rqs_idle(un, xp);
15225 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
15226 				    NULL, NULL, EIO, un->un_busy_timeout / 500,
15227 				    kstat_waitq_enter);
15228 				goto exit;
15229 			}
15230 
15231 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
15232 			/*
15233 			 * Free the DMA resources for the  scsi_pkt. This will
15234 			 * allow mpxio to select another path the next time
15235 			 * we call scsi_transport() with this scsi_pkt.
15236 			 * See sdintr() for the rationalization behind this.
15237 			 */
15238 			if ((un->un_f_is_fibre == TRUE) &&
15239 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15240 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
15241 				scsi_dmafree(xp->xb_pktp);
15242 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15243 			}
15244 #endif
15245 
15246 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
15247 				/*
15248 				 * Commands that are SD_PATH_DIRECT_PRIORITY
15249 				 * are for error recovery situations. These do
15250 				 * not use the normal command waitq, so if they
15251 				 * get a TRAN_BUSY we cannot put them back onto
15252 				 * the waitq for later retry. One possible
15253 				 * problem is that there could already be some
15254 				 * other command on un_retry_bp that is waiting
15255 				 * for this one to complete, so we would be
15256 				 * deadlocked if we put this command back onto
15257 				 * the waitq for later retry (since un_retry_bp
15258 				 * must complete before the driver gets back to
15259 				 * commands on the waitq).
15260 				 *
15261 				 * To avoid deadlock we must schedule a callback
15262 				 * that will restart this command after a set
15263 				 * interval.  This should keep retrying for as
15264 				 * long as the underlying transport keeps
15265 				 * returning TRAN_BUSY (just like for other
15266 				 * commands).  Use the same timeout interval as
15267 				 * for the ordinary TRAN_BUSY retry.
15268 				 */
15269 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15270 				    "sd_start_cmds: scsi_transport() returned "
15271 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
15272 
15273 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15274 				un->un_direct_priority_timeid =
15275 				    timeout(sd_start_direct_priority_command,
15276 				    bp, un->un_busy_timeout / 500);
15277 
15278 				goto exit;
15279 			}
15280 
15281 			/*
15282 			 * For TRAN_BUSY, we want to reduce the throttle value,
15283 			 * unless we are retrying a command.
15284 			 */
15285 			if (bp != un->un_retry_bp) {
15286 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
15287 			}
15288 
15289 			/*
15290 			 * Set up the bp to be tried again 10 ms later.
15291 			 * Note:x86: Is there a timeout value in the sd_lun
15292 			 * for this condition?
15293 			 */
15294 			sd_set_retry_bp(un, bp, un->un_busy_timeout / 500,
15295 			    kstat_runq_back_to_waitq);
15296 			goto exit;
15297 
15298 		case TRAN_FATAL_ERROR:
15299 			un->un_tran_fatal_count++;
15300 			/* FALLTHRU */
15301 
15302 		case TRAN_BADPKT:
15303 		default:
15304 			un->un_ncmds_in_transport--;
15305 			ASSERT(un->un_ncmds_in_transport >= 0);
15306 
15307 			/*
15308 			 * If this is our REQUEST SENSE command with a
15309 			 * transport error, we must get back the pointers
15310 			 * to the original buf, and mark the REQUEST
15311 			 * SENSE command as "available".
15312 			 */
15313 			if (bp == un->un_rqs_bp) {
15314 				bp = sd_mark_rqs_idle(un, xp);
15315 				xp = SD_GET_XBUF(bp);
15316 			} else {
15317 				/*
15318 				 * Legacy behavior: do not update transport
15319 				 * error count for request sense commands.
15320 				 */
15321 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
15322 			}
15323 
15324 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15325 			sd_print_transport_rejected_message(un, xp, rval);
15326 
15327 			/*
15328 			 * This command will be terminated by SD driver due
15329 			 * to a fatal transport error. We should post
15330 			 * ereport.io.scsi.cmd.disk.tran with driver-assessment
15331 			 * of "fail" for any command to indicate this
15332 			 * situation.
15333 			 */
15334 			if (xp->xb_ena > 0) {
15335 				ASSERT(un->un_fm_private != NULL);
15336 				sfip = un->un_fm_private;
15337 				sfip->fm_ssc.ssc_flags |= SSC_FLAGS_TRAN_ABORT;
15338 				sd_ssc_extract_info(&sfip->fm_ssc, un,
15339 				    xp->xb_pktp, bp, xp);
15340 				sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15341 			}
15342 
15343 			/*
15344 			 * We must use sd_return_failed_command_no_restart() to
15345 			 * avoid a recursive call back into sd_start_cmds().
15346 			 * However this also means that we must keep processing
15347 			 * the waitq here in order to avoid stalling.
15348 			 */
15349 			sd_return_failed_command_no_restart(un, bp, EIO);
15350 
15351 			/*
15352 			 * Notify any threads waiting in sd_ddi_suspend() that
15353 			 * a command completion has occurred.
15354 			 */
15355 			if (un->un_state == SD_STATE_SUSPENDED) {
15356 				cv_broadcast(&un->un_disk_busy_cv);
15357 			}
15358 
15359 			if (bp == immed_bp) {
15360 				/* immed_bp is gone by now, so clear this */
15361 				immed_bp = NULL;
15362 			}
15363 			break;
15364 		}
15365 
15366 	} while (immed_bp == NULL);
15367 
15368 exit:
15369 	ASSERT(mutex_owned(SD_MUTEX(un)));
15370 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
15371 }
15372 
15373 
15374 /*
15375  *    Function: sd_return_command
15376  *
15377  * Description: Returns a command to its originator (with or without an
15378  *		error).  Also starts commands waiting to be transported
15379  *		to the target.
15380  *
15381  *     Context: May be called from interrupt, kernel, or timeout context
15382  */
15383 
15384 static void
15385 sd_return_command(struct sd_lun *un, struct buf *bp)
15386 {
15387 	struct sd_xbuf *xp;
15388 	struct scsi_pkt *pktp;
15389 	struct sd_fm_internal *sfip;
15390 
15391 	ASSERT(bp != NULL);
15392 	ASSERT(un != NULL);
15393 	ASSERT(mutex_owned(SD_MUTEX(un)));
15394 	ASSERT(bp != un->un_rqs_bp);
15395 	xp = SD_GET_XBUF(bp);
15396 	ASSERT(xp != NULL);
15397 
15398 	pktp = SD_GET_PKTP(bp);
15399 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15400 	ASSERT(sfip != NULL);
15401 
15402 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
15403 
15404 	/*
15405 	 * Note: check for the "sdrestart failed" case.
15406 	 */
15407 	if ((un->un_partial_dma_supported == 1) &&
15408 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
15409 	    (geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
15410 	    (xp->xb_pktp->pkt_resid == 0)) {
15411 
15412 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
15413 			/*
15414 			 * Successfully set up next portion of cmd
15415 			 * transfer, try sending it
15416 			 */
15417 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15418 			    NULL, NULL, 0, (clock_t)0, NULL);
15419 			sd_start_cmds(un, NULL);
15420 			return;	/* Note:x86: need a return here? */
15421 		}
15422 	}
15423 
15424 	/*
15425 	 * If this is the failfast bp, clear it from un_failfast_bp. This
15426 	 * can happen if upon being re-tried the failfast bp either
15427 	 * succeeded or encountered another error (possibly even a different
15428 	 * error than the one that precipitated the failfast state, but in
15429 	 * that case it would have had to exhaust retries as well). Regardless,
15430 	 * this should not occur whenever the instance is in the active
15431 	 * failfast state.
15432 	 */
15433 	if (bp == un->un_failfast_bp) {
15434 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15435 		un->un_failfast_bp = NULL;
15436 	}
15437 
15438 	/*
15439 	 * Clear the failfast state upon successful completion of ANY cmd.
15440 	 */
15441 	if (bp->b_error == 0) {
15442 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15443 		/*
15444 		 * If this is a successful command, but used to be retried,
15445 		 * we will take it as a recovered command and post an
15446 		 * ereport with driver-assessment of "recovered".
15447 		 */
15448 		if (xp->xb_ena > 0) {
15449 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15450 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RECOVERY);
15451 		}
15452 	} else {
15453 		/*
15454 		 * If this is a failed non-USCSI command we will post an
15455 		 * ereport with driver-assessment set accordingly("fail" or
15456 		 * "fatal").
15457 		 */
15458 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15459 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15460 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_FATAL);
15461 		}
15462 	}
15463 
15464 	/*
15465 	 * This is used if the command was retried one or more times. Show that
15466 	 * we are done with it, and allow processing of the waitq to resume.
15467 	 */
15468 	if (bp == un->un_retry_bp) {
15469 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15470 		    "sd_return_command: un:0x%p: "
15471 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15472 		un->un_retry_bp = NULL;
15473 		un->un_retry_statp = NULL;
15474 	}
15475 
15476 	SD_UPDATE_RDWR_STATS(un, bp);
15477 	SD_UPDATE_PARTITION_STATS(un, bp);
15478 
15479 	switch (un->un_state) {
15480 	case SD_STATE_SUSPENDED:
15481 		/*
15482 		 * Notify any threads waiting in sd_ddi_suspend() that
15483 		 * a command completion has occurred.
15484 		 */
15485 		cv_broadcast(&un->un_disk_busy_cv);
15486 		break;
15487 	default:
15488 		sd_start_cmds(un, NULL);
15489 		break;
15490 	}
15491 
15492 	/* Return this command up the iodone chain to its originator. */
15493 	mutex_exit(SD_MUTEX(un));
15494 
15495 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15496 	xp->xb_pktp = NULL;
15497 
15498 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15499 
15500 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15501 	mutex_enter(SD_MUTEX(un));
15502 
15503 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
15504 }
15505 
15506 
15507 /*
15508  *    Function: sd_return_failed_command
15509  *
15510  * Description: Command completion when an error occurred.
15511  *
15512  *     Context: May be called from interrupt context
15513  */
15514 
15515 static void
15516 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
15517 {
15518 	ASSERT(bp != NULL);
15519 	ASSERT(un != NULL);
15520 	ASSERT(mutex_owned(SD_MUTEX(un)));
15521 
15522 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15523 	    "sd_return_failed_command: entry\n");
15524 
15525 	/*
15526 	 * b_resid could already be nonzero due to a partial data
15527 	 * transfer, so do not change it here.
15528 	 */
15529 	SD_BIOERROR(bp, errcode);
15530 
15531 	sd_return_command(un, bp);
15532 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15533 	    "sd_return_failed_command: exit\n");
15534 }
15535 
15536 
15537 /*
15538  *    Function: sd_return_failed_command_no_restart
15539  *
15540  * Description: Same as sd_return_failed_command, but ensures that no
15541  *		call back into sd_start_cmds will be issued.
15542  *
15543  *     Context: May be called from interrupt context
15544  */
15545 
15546 static void
15547 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
15548 	int errcode)
15549 {
15550 	struct sd_xbuf *xp;
15551 
15552 	ASSERT(bp != NULL);
15553 	ASSERT(un != NULL);
15554 	ASSERT(mutex_owned(SD_MUTEX(un)));
15555 	xp = SD_GET_XBUF(bp);
15556 	ASSERT(xp != NULL);
15557 	ASSERT(errcode != 0);
15558 
15559 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15560 	    "sd_return_failed_command_no_restart: entry\n");
15561 
15562 	/*
15563 	 * b_resid could already be nonzero due to a partial data
15564 	 * transfer, so do not change it here.
15565 	 */
15566 	SD_BIOERROR(bp, errcode);
15567 
15568 	/*
15569 	 * If this is the failfast bp, clear it. This can happen if the
15570 	 * failfast bp encounterd a fatal error when we attempted to
15571 	 * re-try it (such as a scsi_transport(9F) failure).  However
15572 	 * we should NOT be in an active failfast state if the failfast
15573 	 * bp is not NULL.
15574 	 */
15575 	if (bp == un->un_failfast_bp) {
15576 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
15577 		un->un_failfast_bp = NULL;
15578 	}
15579 
15580 	if (bp == un->un_retry_bp) {
15581 		/*
15582 		 * This command was retried one or more times. Show that we are
15583 		 * done with it, and allow processing of the waitq to resume.
15584 		 */
15585 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15586 		    "sd_return_failed_command_no_restart: "
15587 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
15588 		un->un_retry_bp = NULL;
15589 		un->un_retry_statp = NULL;
15590 	}
15591 
15592 	SD_UPDATE_RDWR_STATS(un, bp);
15593 	SD_UPDATE_PARTITION_STATS(un, bp);
15594 
15595 	mutex_exit(SD_MUTEX(un));
15596 
15597 	if (xp->xb_pktp != NULL) {
15598 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
15599 		xp->xb_pktp = NULL;
15600 	}
15601 
15602 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
15603 
15604 	mutex_enter(SD_MUTEX(un));
15605 
15606 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15607 	    "sd_return_failed_command_no_restart: exit\n");
15608 }
15609 
15610 
15611 /*
15612  *    Function: sd_retry_command
15613  *
15614  * Description: queue up a command for retry, or (optionally) fail it
15615  *		if retry counts are exhausted.
15616  *
15617  *   Arguments: un - Pointer to the sd_lun struct for the target.
15618  *
15619  *		bp - Pointer to the buf for the command to be retried.
15620  *
15621  *		retry_check_flag - Flag to see which (if any) of the retry
15622  *		   counts should be decremented/checked. If the indicated
15623  *		   retry count is exhausted, then the command will not be
15624  *		   retried; it will be failed instead. This should use a
15625  *		   value equal to one of the following:
15626  *
15627  *			SD_RETRIES_NOCHECK
15628  *			SD_RESD_RETRIES_STANDARD
15629  *			SD_RETRIES_VICTIM
15630  *
15631  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
15632  *		   if the check should be made to see of FLAG_ISOLATE is set
15633  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
15634  *		   not retried, it is simply failed.
15635  *
15636  *		user_funcp - Ptr to function to call before dispatching the
15637  *		   command. May be NULL if no action needs to be performed.
15638  *		   (Primarily intended for printing messages.)
15639  *
15640  *		user_arg - Optional argument to be passed along to
15641  *		   the user_funcp call.
15642  *
15643  *		failure_code - errno return code to set in the bp if the
15644  *		   command is going to be failed.
15645  *
15646  *		retry_delay - Retry delay interval in (clock_t) units. May
15647  *		   be zero which indicates that the retry should be retried
15648  *		   immediately (ie, without an intervening delay).
15649  *
15650  *		statp - Ptr to kstat function to be updated if the command
15651  *		   is queued for a delayed retry. May be NULL if no kstat
15652  *		   update is desired.
15653  *
15654  *     Context: May be called from interrupt context.
15655  */
15656 
15657 static void
15658 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15659 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15660 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15661 	void (*statp)(kstat_io_t *))
15662 {
15663 	struct sd_xbuf	*xp;
15664 	struct scsi_pkt	*pktp;
15665 	struct sd_fm_internal *sfip;
15666 
15667 	ASSERT(un != NULL);
15668 	ASSERT(mutex_owned(SD_MUTEX(un)));
15669 	ASSERT(bp != NULL);
15670 	xp = SD_GET_XBUF(bp);
15671 	ASSERT(xp != NULL);
15672 	pktp = SD_GET_PKTP(bp);
15673 	ASSERT(pktp != NULL);
15674 
15675 	sfip = (struct sd_fm_internal *)un->un_fm_private;
15676 	ASSERT(sfip != NULL);
15677 
15678 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15679 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15680 
15681 	/*
15682 	 * If we are syncing or dumping, fail the command to avoid
15683 	 * recursively calling back into scsi_transport().
15684 	 */
15685 	if (ddi_in_panic()) {
15686 		goto fail_command_no_log;
15687 	}
15688 
15689 	/*
15690 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15691 	 * log an error and fail the command.
15692 	 */
15693 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15694 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15695 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15696 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15697 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15698 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15699 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15700 		goto fail_command;
15701 	}
15702 
15703 	/*
15704 	 * If we are suspended, then put the command onto head of the
15705 	 * wait queue since we don't want to start more commands, and
15706 	 * clear the un_retry_bp. Next time when we are resumed, will
15707 	 * handle the command in the wait queue.
15708 	 */
15709 	switch (un->un_state) {
15710 	case SD_STATE_SUSPENDED:
15711 	case SD_STATE_DUMPING:
15712 		bp->av_forw = un->un_waitq_headp;
15713 		un->un_waitq_headp = bp;
15714 		if (un->un_waitq_tailp == NULL) {
15715 			un->un_waitq_tailp = bp;
15716 		}
15717 		if (bp == un->un_retry_bp) {
15718 			un->un_retry_bp = NULL;
15719 			un->un_retry_statp = NULL;
15720 		}
15721 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15722 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15723 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15724 		return;
15725 	default:
15726 		break;
15727 	}
15728 
15729 	/*
15730 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15731 	 * is set; if it is then we do not want to retry the command.
15732 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15733 	 */
15734 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15735 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15736 			goto fail_command;
15737 		}
15738 	}
15739 
15740 
15741 	/*
15742 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15743 	 * command timeout or a selection timeout has occurred. This means
15744 	 * that we were unable to establish an kind of communication with
15745 	 * the target, and subsequent retries and/or commands are likely
15746 	 * to encounter similar results and take a long time to complete.
15747 	 *
15748 	 * If this is a failfast error condition, we need to update the
15749 	 * failfast state, even if this bp does not have B_FAILFAST set.
15750 	 */
15751 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15752 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15753 			ASSERT(un->un_failfast_bp == NULL);
15754 			/*
15755 			 * If we are already in the active failfast state, and
15756 			 * another failfast error condition has been detected,
15757 			 * then fail this command if it has B_FAILFAST set.
15758 			 * If B_FAILFAST is clear, then maintain the legacy
15759 			 * behavior of retrying heroically, even tho this will
15760 			 * take a lot more time to fail the command.
15761 			 */
15762 			if (bp->b_flags & B_FAILFAST) {
15763 				goto fail_command;
15764 			}
15765 		} else {
15766 			/*
15767 			 * We're not in the active failfast state, but we
15768 			 * have a failfast error condition, so we must begin
15769 			 * transition to the next state. We do this regardless
15770 			 * of whether or not this bp has B_FAILFAST set.
15771 			 */
15772 			if (un->un_failfast_bp == NULL) {
15773 				/*
15774 				 * This is the first bp to meet a failfast
15775 				 * condition so save it on un_failfast_bp &
15776 				 * do normal retry processing. Do not enter
15777 				 * active failfast state yet. This marks
15778 				 * entry into the "failfast pending" state.
15779 				 */
15780 				un->un_failfast_bp = bp;
15781 
15782 			} else if (un->un_failfast_bp == bp) {
15783 				/*
15784 				 * This is the second time *this* bp has
15785 				 * encountered a failfast error condition,
15786 				 * so enter active failfast state & flush
15787 				 * queues as appropriate.
15788 				 */
15789 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15790 				un->un_failfast_bp = NULL;
15791 				sd_failfast_flushq(un);
15792 
15793 				/*
15794 				 * Fail this bp now if B_FAILFAST set;
15795 				 * otherwise continue with retries. (It would
15796 				 * be pretty ironic if this bp succeeded on a
15797 				 * subsequent retry after we just flushed all
15798 				 * the queues).
15799 				 */
15800 				if (bp->b_flags & B_FAILFAST) {
15801 					goto fail_command;
15802 				}
15803 
15804 #if !defined(lint) && !defined(__lint)
15805 			} else {
15806 				/*
15807 				 * If neither of the preceeding conditionals
15808 				 * was true, it means that there is some
15809 				 * *other* bp that has met an inital failfast
15810 				 * condition and is currently either being
15811 				 * retried or is waiting to be retried. In
15812 				 * that case we should perform normal retry
15813 				 * processing on *this* bp, since there is a
15814 				 * chance that the current failfast condition
15815 				 * is transient and recoverable. If that does
15816 				 * not turn out to be the case, then retries
15817 				 * will be cleared when the wait queue is
15818 				 * flushed anyway.
15819 				 */
15820 #endif
15821 			}
15822 		}
15823 	} else {
15824 		/*
15825 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15826 		 * likely were able to at least establish some level of
15827 		 * communication with the target and subsequent commands
15828 		 * and/or retries are likely to get through to the target,
15829 		 * In this case we want to be aggressive about clearing
15830 		 * the failfast state. Note that this does not affect
15831 		 * the "failfast pending" condition.
15832 		 */
15833 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15834 	}
15835 
15836 
15837 	/*
15838 	 * Check the specified retry count to see if we can still do
15839 	 * any retries with this pkt before we should fail it.
15840 	 */
15841 	switch (retry_check_flag & SD_RETRIES_MASK) {
15842 	case SD_RETRIES_VICTIM:
15843 		/*
15844 		 * Check the victim retry count. If exhausted, then fall
15845 		 * thru & check against the standard retry count.
15846 		 */
15847 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15848 			/* Increment count & proceed with the retry */
15849 			xp->xb_victim_retry_count++;
15850 			break;
15851 		}
15852 		/* Victim retries exhausted, fall back to std. retries... */
15853 		/* FALLTHRU */
15854 
15855 	case SD_RETRIES_STANDARD:
15856 		if (xp->xb_retry_count >= un->un_retry_count) {
15857 			/* Retries exhausted, fail the command */
15858 			SD_TRACE(SD_LOG_IO_CORE, un,
15859 			    "sd_retry_command: retries exhausted!\n");
15860 			/*
15861 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15862 			 * commands with nonzero pkt_resid.
15863 			 */
15864 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15865 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15866 			    (pktp->pkt_resid != 0)) {
15867 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15868 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15869 					SD_UPDATE_B_RESID(bp, pktp);
15870 				}
15871 			}
15872 			goto fail_command;
15873 		}
15874 		xp->xb_retry_count++;
15875 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15876 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15877 		break;
15878 
15879 	case SD_RETRIES_UA:
15880 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15881 			/* Retries exhausted, fail the command */
15882 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15883 			    "Unit Attention retries exhausted. "
15884 			    "Check the target.\n");
15885 			goto fail_command;
15886 		}
15887 		xp->xb_ua_retry_count++;
15888 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15889 		    "sd_retry_command: retry count:%d\n",
15890 		    xp->xb_ua_retry_count);
15891 		break;
15892 
15893 	case SD_RETRIES_BUSY:
15894 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15895 			/* Retries exhausted, fail the command */
15896 			SD_TRACE(SD_LOG_IO_CORE, un,
15897 			    "sd_retry_command: retries exhausted!\n");
15898 			goto fail_command;
15899 		}
15900 		xp->xb_retry_count++;
15901 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15902 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15903 		break;
15904 
15905 	case SD_RETRIES_NOCHECK:
15906 	default:
15907 		/* No retry count to check. Just proceed with the retry */
15908 		break;
15909 	}
15910 
15911 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15912 
15913 	/*
15914 	 * If this is a non-USCSI command being retried
15915 	 * during execution last time, we should post an ereport with
15916 	 * driver-assessment of the value "retry".
15917 	 * For partial DMA, request sense and STATUS_QFULL, there are no
15918 	 * hardware errors, we bypass ereport posting.
15919 	 */
15920 	if (failure_code != 0) {
15921 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
15922 			sd_ssc_extract_info(&sfip->fm_ssc, un, pktp, bp, xp);
15923 			sd_ssc_post(&sfip->fm_ssc, SD_FM_DRV_RETRY);
15924 		}
15925 	}
15926 
15927 	/*
15928 	 * If we were given a zero timeout, we must attempt to retry the
15929 	 * command immediately (ie, without a delay).
15930 	 */
15931 	if (retry_delay == 0) {
15932 		/*
15933 		 * Check some limiting conditions to see if we can actually
15934 		 * do the immediate retry.  If we cannot, then we must
15935 		 * fall back to queueing up a delayed retry.
15936 		 */
15937 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15938 			/*
15939 			 * We are at the throttle limit for the target,
15940 			 * fall back to delayed retry.
15941 			 */
15942 			retry_delay = un->un_busy_timeout;
15943 			statp = kstat_waitq_enter;
15944 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15945 			    "sd_retry_command: immed. retry hit "
15946 			    "throttle!\n");
15947 		} else {
15948 			/*
15949 			 * We're clear to proceed with the immediate retry.
15950 			 * First call the user-provided function (if any)
15951 			 */
15952 			if (user_funcp != NULL) {
15953 				(*user_funcp)(un, bp, user_arg,
15954 				    SD_IMMEDIATE_RETRY_ISSUED);
15955 #ifdef __lock_lint
15956 				sd_print_incomplete_msg(un, bp, user_arg,
15957 				    SD_IMMEDIATE_RETRY_ISSUED);
15958 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15959 				    SD_IMMEDIATE_RETRY_ISSUED);
15960 				sd_print_sense_failed_msg(un, bp, user_arg,
15961 				    SD_IMMEDIATE_RETRY_ISSUED);
15962 #endif
15963 			}
15964 
15965 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15966 			    "sd_retry_command: issuing immediate retry\n");
15967 
15968 			/*
15969 			 * Call sd_start_cmds() to transport the command to
15970 			 * the target.
15971 			 */
15972 			sd_start_cmds(un, bp);
15973 
15974 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15975 			    "sd_retry_command exit\n");
15976 			return;
15977 		}
15978 	}
15979 
15980 	/*
15981 	 * Set up to retry the command after a delay.
15982 	 * First call the user-provided function (if any)
15983 	 */
15984 	if (user_funcp != NULL) {
15985 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15986 	}
15987 
15988 	sd_set_retry_bp(un, bp, retry_delay, statp);
15989 
15990 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15991 	return;
15992 
15993 fail_command:
15994 
15995 	if (user_funcp != NULL) {
15996 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15997 	}
15998 
15999 fail_command_no_log:
16000 
16001 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16002 	    "sd_retry_command: returning failed command\n");
16003 
16004 	sd_return_failed_command(un, bp, failure_code);
16005 
16006 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
16007 }
16008 
16009 
16010 /*
16011  *    Function: sd_set_retry_bp
16012  *
16013  * Description: Set up the given bp for retry.
16014  *
16015  *   Arguments: un - ptr to associated softstate
16016  *		bp - ptr to buf(9S) for the command
16017  *		retry_delay - time interval before issuing retry (may be 0)
16018  *		statp - optional pointer to kstat function
16019  *
16020  *     Context: May be called under interrupt context
16021  */
16022 
16023 static void
16024 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
16025 	void (*statp)(kstat_io_t *))
16026 {
16027 	ASSERT(un != NULL);
16028 	ASSERT(mutex_owned(SD_MUTEX(un)));
16029 	ASSERT(bp != NULL);
16030 
16031 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16032 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
16033 
16034 	/*
16035 	 * Indicate that the command is being retried. This will not allow any
16036 	 * other commands on the wait queue to be transported to the target
16037 	 * until this command has been completed (success or failure). The
16038 	 * "retry command" is not transported to the target until the given
16039 	 * time delay expires, unless the user specified a 0 retry_delay.
16040 	 *
16041 	 * Note: the timeout(9F) callback routine is what actually calls
16042 	 * sd_start_cmds() to transport the command, with the exception of a
16043 	 * zero retry_delay. The only current implementor of a zero retry delay
16044 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
16045 	 */
16046 	if (un->un_retry_bp == NULL) {
16047 		ASSERT(un->un_retry_statp == NULL);
16048 		un->un_retry_bp = bp;
16049 
16050 		/*
16051 		 * If the user has not specified a delay the command should
16052 		 * be queued and no timeout should be scheduled.
16053 		 */
16054 		if (retry_delay == 0) {
16055 			/*
16056 			 * Save the kstat pointer that will be used in the
16057 			 * call to SD_UPDATE_KSTATS() below, so that
16058 			 * sd_start_cmds() can correctly decrement the waitq
16059 			 * count when it is time to transport this command.
16060 			 */
16061 			un->un_retry_statp = statp;
16062 			goto done;
16063 		}
16064 	}
16065 
16066 	if (un->un_retry_bp == bp) {
16067 		/*
16068 		 * Save the kstat pointer that will be used in the call to
16069 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
16070 		 * correctly decrement the waitq count when it is time to
16071 		 * transport this command.
16072 		 */
16073 		un->un_retry_statp = statp;
16074 
16075 		/*
16076 		 * Schedule a timeout if:
16077 		 *   1) The user has specified a delay.
16078 		 *   2) There is not a START_STOP_UNIT callback pending.
16079 		 *
16080 		 * If no delay has been specified, then it is up to the caller
16081 		 * to ensure that IO processing continues without stalling.
16082 		 * Effectively, this means that the caller will issue the
16083 		 * required call to sd_start_cmds(). The START_STOP_UNIT
16084 		 * callback does this after the START STOP UNIT command has
16085 		 * completed. In either of these cases we should not schedule
16086 		 * a timeout callback here.  Also don't schedule the timeout if
16087 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
16088 		 */
16089 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
16090 		    (un->un_direct_priority_timeid == NULL)) {
16091 			un->un_retry_timeid =
16092 			    timeout(sd_start_retry_command, un, retry_delay);
16093 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16094 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
16095 			    " bp:0x%p un_retry_timeid:0x%p\n",
16096 			    un, bp, un->un_retry_timeid);
16097 		}
16098 	} else {
16099 		/*
16100 		 * We only get in here if there is already another command
16101 		 * waiting to be retried.  In this case, we just put the
16102 		 * given command onto the wait queue, so it can be transported
16103 		 * after the current retry command has completed.
16104 		 *
16105 		 * Also we have to make sure that if the command at the head
16106 		 * of the wait queue is the un_failfast_bp, that we do not
16107 		 * put ahead of it any other commands that are to be retried.
16108 		 */
16109 		if ((un->un_failfast_bp != NULL) &&
16110 		    (un->un_failfast_bp == un->un_waitq_headp)) {
16111 			/*
16112 			 * Enqueue this command AFTER the first command on
16113 			 * the wait queue (which is also un_failfast_bp).
16114 			 */
16115 			bp->av_forw = un->un_waitq_headp->av_forw;
16116 			un->un_waitq_headp->av_forw = bp;
16117 			if (un->un_waitq_headp == un->un_waitq_tailp) {
16118 				un->un_waitq_tailp = bp;
16119 			}
16120 		} else {
16121 			/* Enqueue this command at the head of the waitq. */
16122 			bp->av_forw = un->un_waitq_headp;
16123 			un->un_waitq_headp = bp;
16124 			if (un->un_waitq_tailp == NULL) {
16125 				un->un_waitq_tailp = bp;
16126 			}
16127 		}
16128 
16129 		if (statp == NULL) {
16130 			statp = kstat_waitq_enter;
16131 		}
16132 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16133 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
16134 	}
16135 
16136 done:
16137 	if (statp != NULL) {
16138 		SD_UPDATE_KSTATS(un, statp, bp);
16139 	}
16140 
16141 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16142 	    "sd_set_retry_bp: exit un:0x%p\n", un);
16143 }
16144 
16145 
16146 /*
16147  *    Function: sd_start_retry_command
16148  *
16149  * Description: Start the command that has been waiting on the target's
16150  *		retry queue.  Called from timeout(9F) context after the
16151  *		retry delay interval has expired.
16152  *
16153  *   Arguments: arg - pointer to associated softstate for the device.
16154  *
16155  *     Context: timeout(9F) thread context.  May not sleep.
16156  */
16157 
16158 static void
16159 sd_start_retry_command(void *arg)
16160 {
16161 	struct sd_lun *un = arg;
16162 
16163 	ASSERT(un != NULL);
16164 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16165 
16166 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16167 	    "sd_start_retry_command: entry\n");
16168 
16169 	mutex_enter(SD_MUTEX(un));
16170 
16171 	un->un_retry_timeid = NULL;
16172 
16173 	if (un->un_retry_bp != NULL) {
16174 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16175 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
16176 		    un, un->un_retry_bp);
16177 		sd_start_cmds(un, un->un_retry_bp);
16178 	}
16179 
16180 	mutex_exit(SD_MUTEX(un));
16181 
16182 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16183 	    "sd_start_retry_command: exit\n");
16184 }
16185 
16186 /*
16187  *    Function: sd_rmw_msg_print_handler
16188  *
16189  * Description: If RMW mode is enabled and warning message is triggered
16190  *              print I/O count during a fixed interval.
16191  *
16192  *   Arguments: arg - pointer to associated softstate for the device.
16193  *
16194  *     Context: timeout(9F) thread context. May not sleep.
16195  */
16196 static void
16197 sd_rmw_msg_print_handler(void *arg)
16198 {
16199 	struct sd_lun *un = arg;
16200 
16201 	ASSERT(un != NULL);
16202 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16203 
16204 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16205 	    "sd_rmw_msg_print_handler: entry\n");
16206 
16207 	mutex_enter(SD_MUTEX(un));
16208 
16209 	if (un->un_rmw_incre_count > 0) {
16210 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16211 		    "%"PRIu64" I/O requests are not aligned with %d disk "
16212 		    "sector size in %ld seconds. They are handled through "
16213 		    "Read Modify Write but the performance is very low!\n",
16214 		    un->un_rmw_incre_count, un->un_tgt_blocksize,
16215 		    drv_hztousec(SD_RMW_MSG_PRINT_TIMEOUT) / 1000000);
16216 		un->un_rmw_incre_count = 0;
16217 		un->un_rmw_msg_timeid = timeout(sd_rmw_msg_print_handler,
16218 		    un, SD_RMW_MSG_PRINT_TIMEOUT);
16219 	} else {
16220 		un->un_rmw_msg_timeid = NULL;
16221 	}
16222 
16223 	mutex_exit(SD_MUTEX(un));
16224 
16225 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16226 	    "sd_rmw_msg_print_handler: exit\n");
16227 }
16228 
16229 /*
16230  *    Function: sd_start_direct_priority_command
16231  *
16232  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
16233  *		received TRAN_BUSY when we called scsi_transport() to send it
16234  *		to the underlying HBA. This function is called from timeout(9F)
16235  *		context after the delay interval has expired.
16236  *
16237  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
16238  *
16239  *     Context: timeout(9F) thread context.  May not sleep.
16240  */
16241 
16242 static void
16243 sd_start_direct_priority_command(void *arg)
16244 {
16245 	struct buf	*priority_bp = arg;
16246 	struct sd_lun	*un;
16247 
16248 	ASSERT(priority_bp != NULL);
16249 	un = SD_GET_UN(priority_bp);
16250 	ASSERT(un != NULL);
16251 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16252 
16253 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16254 	    "sd_start_direct_priority_command: entry\n");
16255 
16256 	mutex_enter(SD_MUTEX(un));
16257 	un->un_direct_priority_timeid = NULL;
16258 	sd_start_cmds(un, priority_bp);
16259 	mutex_exit(SD_MUTEX(un));
16260 
16261 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16262 	    "sd_start_direct_priority_command: exit\n");
16263 }
16264 
16265 
16266 /*
16267  *    Function: sd_send_request_sense_command
16268  *
16269  * Description: Sends a REQUEST SENSE command to the target
16270  *
16271  *     Context: May be called from interrupt context.
16272  */
16273 
16274 static void
16275 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
16276 	struct scsi_pkt *pktp)
16277 {
16278 	ASSERT(bp != NULL);
16279 	ASSERT(un != NULL);
16280 	ASSERT(mutex_owned(SD_MUTEX(un)));
16281 
16282 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
16283 	    "entry: buf:0x%p\n", bp);
16284 
16285 	/*
16286 	 * If we are syncing or dumping, then fail the command to avoid a
16287 	 * recursive callback into scsi_transport(). Also fail the command
16288 	 * if we are suspended (legacy behavior).
16289 	 */
16290 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
16291 	    (un->un_state == SD_STATE_DUMPING)) {
16292 		sd_return_failed_command(un, bp, EIO);
16293 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16294 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
16295 		return;
16296 	}
16297 
16298 	/*
16299 	 * Retry the failed command and don't issue the request sense if:
16300 	 *    1) the sense buf is busy
16301 	 *    2) we have 1 or more outstanding commands on the target
16302 	 *    (the sense data will be cleared or invalidated any way)
16303 	 *
16304 	 * Note: There could be an issue with not checking a retry limit here,
16305 	 * the problem is determining which retry limit to check.
16306 	 */
16307 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
16308 		/* Don't retry if the command is flagged as non-retryable */
16309 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16310 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
16311 			    NULL, NULL, 0, un->un_busy_timeout,
16312 			    kstat_waitq_enter);
16313 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16314 			    "sd_send_request_sense_command: "
16315 			    "at full throttle, retrying exit\n");
16316 		} else {
16317 			sd_return_failed_command(un, bp, EIO);
16318 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16319 			    "sd_send_request_sense_command: "
16320 			    "at full throttle, non-retryable exit\n");
16321 		}
16322 		return;
16323 	}
16324 
16325 	sd_mark_rqs_busy(un, bp);
16326 	sd_start_cmds(un, un->un_rqs_bp);
16327 
16328 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16329 	    "sd_send_request_sense_command: exit\n");
16330 }
16331 
16332 
16333 /*
16334  *    Function: sd_mark_rqs_busy
16335  *
16336  * Description: Indicate that the request sense bp for this instance is
16337  *		in use.
16338  *
16339  *     Context: May be called under interrupt context
16340  */
16341 
16342 static void
16343 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
16344 {
16345 	struct sd_xbuf	*sense_xp;
16346 
16347 	ASSERT(un != NULL);
16348 	ASSERT(bp != NULL);
16349 	ASSERT(mutex_owned(SD_MUTEX(un)));
16350 	ASSERT(un->un_sense_isbusy == 0);
16351 
16352 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
16353 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
16354 
16355 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
16356 	ASSERT(sense_xp != NULL);
16357 
16358 	SD_INFO(SD_LOG_IO, un,
16359 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
16360 
16361 	ASSERT(sense_xp->xb_pktp != NULL);
16362 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
16363 	    == (FLAG_SENSING | FLAG_HEAD));
16364 
16365 	un->un_sense_isbusy = 1;
16366 	un->un_rqs_bp->b_resid = 0;
16367 	sense_xp->xb_pktp->pkt_resid  = 0;
16368 	sense_xp->xb_pktp->pkt_reason = 0;
16369 
16370 	/* So we can get back the bp at interrupt time! */
16371 	sense_xp->xb_sense_bp = bp;
16372 
16373 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
16374 
16375 	/*
16376 	 * Mark this buf as awaiting sense data. (This is already set in
16377 	 * the pkt_flags for the RQS packet.)
16378 	 */
16379 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
16380 
16381 	/* Request sense down same path */
16382 	if (scsi_pkt_allocated_correctly((SD_GET_XBUF(bp))->xb_pktp) &&
16383 	    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance)
16384 		sense_xp->xb_pktp->pkt_path_instance =
16385 		    ((SD_GET_XBUF(bp))->xb_pktp)->pkt_path_instance;
16386 
16387 	sense_xp->xb_retry_count	= 0;
16388 	sense_xp->xb_victim_retry_count = 0;
16389 	sense_xp->xb_ua_retry_count	= 0;
16390 	sense_xp->xb_nr_retry_count 	= 0;
16391 	sense_xp->xb_dma_resid  = 0;
16392 
16393 	/* Clean up the fields for auto-request sense */
16394 	sense_xp->xb_sense_status = 0;
16395 	sense_xp->xb_sense_state  = 0;
16396 	sense_xp->xb_sense_resid  = 0;
16397 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
16398 
16399 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
16400 }
16401 
16402 
16403 /*
16404  *    Function: sd_mark_rqs_idle
16405  *
16406  * Description: SD_MUTEX must be held continuously through this routine
16407  *		to prevent reuse of the rqs struct before the caller can
16408  *		complete it's processing.
16409  *
16410  * Return Code: Pointer to the RQS buf
16411  *
16412  *     Context: May be called under interrupt context
16413  */
16414 
16415 static struct buf *
16416 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
16417 {
16418 	struct buf *bp;
16419 	ASSERT(un != NULL);
16420 	ASSERT(sense_xp != NULL);
16421 	ASSERT(mutex_owned(SD_MUTEX(un)));
16422 	ASSERT(un->un_sense_isbusy != 0);
16423 
16424 	un->un_sense_isbusy = 0;
16425 	bp = sense_xp->xb_sense_bp;
16426 	sense_xp->xb_sense_bp = NULL;
16427 
16428 	/* This pkt is no longer interested in getting sense data */
16429 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
16430 
16431 	return (bp);
16432 }
16433 
16434 
16435 
16436 /*
16437  *    Function: sd_alloc_rqs
16438  *
16439  * Description: Set up the unit to receive auto request sense data
16440  *
16441  * Return Code: DDI_SUCCESS or DDI_FAILURE
16442  *
16443  *     Context: Called under attach(9E) context
16444  */
16445 
16446 static int
16447 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
16448 {
16449 	struct sd_xbuf *xp;
16450 
16451 	ASSERT(un != NULL);
16452 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16453 	ASSERT(un->un_rqs_bp == NULL);
16454 	ASSERT(un->un_rqs_pktp == NULL);
16455 
16456 	/*
16457 	 * First allocate the required buf and scsi_pkt structs, then set up
16458 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
16459 	 */
16460 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
16461 	    MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
16462 	if (un->un_rqs_bp == NULL) {
16463 		return (DDI_FAILURE);
16464 	}
16465 
16466 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
16467 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
16468 
16469 	if (un->un_rqs_pktp == NULL) {
16470 		sd_free_rqs(un);
16471 		return (DDI_FAILURE);
16472 	}
16473 
16474 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
16475 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
16476 	    SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
16477 
16478 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
16479 
16480 	/* Set up the other needed members in the ARQ scsi_pkt. */
16481 	un->un_rqs_pktp->pkt_comp   = sdintr;
16482 	un->un_rqs_pktp->pkt_time   = sd_io_time;
16483 	un->un_rqs_pktp->pkt_flags |=
16484 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
16485 
16486 	/*
16487 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
16488 	 * provide any intpkt, destroypkt routines as we take care of
16489 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
16490 	 */
16491 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
16492 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
16493 	xp->xb_pktp = un->un_rqs_pktp;
16494 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
16495 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
16496 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
16497 
16498 	/*
16499 	 * Save the pointer to the request sense private bp so it can
16500 	 * be retrieved in sdintr.
16501 	 */
16502 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
16503 	ASSERT(un->un_rqs_bp->b_private == xp);
16504 
16505 	/*
16506 	 * See if the HBA supports auto-request sense for the specified
16507 	 * target/lun. If it does, then try to enable it (if not already
16508 	 * enabled).
16509 	 *
16510 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
16511 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
16512 	 * return success.  However, in both of these cases ARQ is always
16513 	 * enabled and scsi_ifgetcap will always return true. The best approach
16514 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
16515 	 *
16516 	 * The 3rd case is the HBA (adp) always return enabled on
16517 	 * scsi_ifgetgetcap even when it's not enable, the best approach
16518 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
16519 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
16520 	 */
16521 
16522 	if (un->un_f_is_fibre == TRUE) {
16523 		un->un_f_arq_enabled = TRUE;
16524 	} else {
16525 #if defined(__i386) || defined(__amd64)
16526 		/*
16527 		 * Circumvent the Adaptec bug, remove this code when
16528 		 * the bug is fixed
16529 		 */
16530 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
16531 #endif
16532 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
16533 		case 0:
16534 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16535 			    "sd_alloc_rqs: HBA supports ARQ\n");
16536 			/*
16537 			 * ARQ is supported by this HBA but currently is not
16538 			 * enabled. Attempt to enable it and if successful then
16539 			 * mark this instance as ARQ enabled.
16540 			 */
16541 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
16542 			    == 1) {
16543 				/* Successfully enabled ARQ in the HBA */
16544 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16545 				    "sd_alloc_rqs: ARQ enabled\n");
16546 				un->un_f_arq_enabled = TRUE;
16547 			} else {
16548 				/* Could not enable ARQ in the HBA */
16549 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
16550 				    "sd_alloc_rqs: failed ARQ enable\n");
16551 				un->un_f_arq_enabled = FALSE;
16552 			}
16553 			break;
16554 		case 1:
16555 			/*
16556 			 * ARQ is supported by this HBA and is already enabled.
16557 			 * Just mark ARQ as enabled for this instance.
16558 			 */
16559 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16560 			    "sd_alloc_rqs: ARQ already enabled\n");
16561 			un->un_f_arq_enabled = TRUE;
16562 			break;
16563 		default:
16564 			/*
16565 			 * ARQ is not supported by this HBA; disable it for this
16566 			 * instance.
16567 			 */
16568 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
16569 			    "sd_alloc_rqs: HBA does not support ARQ\n");
16570 			un->un_f_arq_enabled = FALSE;
16571 			break;
16572 		}
16573 	}
16574 
16575 	return (DDI_SUCCESS);
16576 }
16577 
16578 
16579 /*
16580  *    Function: sd_free_rqs
16581  *
16582  * Description: Cleanup for the pre-instance RQS command.
16583  *
16584  *     Context: Kernel thread context
16585  */
16586 
16587 static void
16588 sd_free_rqs(struct sd_lun *un)
16589 {
16590 	ASSERT(un != NULL);
16591 
16592 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
16593 
16594 	/*
16595 	 * If consistent memory is bound to a scsi_pkt, the pkt
16596 	 * has to be destroyed *before* freeing the consistent memory.
16597 	 * Don't change the sequence of this operations.
16598 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
16599 	 * after it was freed in scsi_free_consistent_buf().
16600 	 */
16601 	if (un->un_rqs_pktp != NULL) {
16602 		scsi_destroy_pkt(un->un_rqs_pktp);
16603 		un->un_rqs_pktp = NULL;
16604 	}
16605 
16606 	if (un->un_rqs_bp != NULL) {
16607 		struct sd_xbuf *xp = SD_GET_XBUF(un->un_rqs_bp);
16608 		if (xp != NULL) {
16609 			kmem_free(xp, sizeof (struct sd_xbuf));
16610 		}
16611 		scsi_free_consistent_buf(un->un_rqs_bp);
16612 		un->un_rqs_bp = NULL;
16613 	}
16614 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
16615 }
16616 
16617 
16618 
16619 /*
16620  *    Function: sd_reduce_throttle
16621  *
16622  * Description: Reduces the maximum # of outstanding commands on a
16623  *		target to the current number of outstanding commands.
16624  *		Queues a tiemout(9F) callback to restore the limit
16625  *		after a specified interval has elapsed.
16626  *		Typically used when we get a TRAN_BUSY return code
16627  *		back from scsi_transport().
16628  *
16629  *   Arguments: un - ptr to the sd_lun softstate struct
16630  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
16631  *
16632  *     Context: May be called from interrupt context
16633  */
16634 
16635 static void
16636 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
16637 {
16638 	ASSERT(un != NULL);
16639 	ASSERT(mutex_owned(SD_MUTEX(un)));
16640 	ASSERT(un->un_ncmds_in_transport >= 0);
16641 
16642 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16643 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
16644 	    un, un->un_throttle, un->un_ncmds_in_transport);
16645 
16646 	if (un->un_throttle > 1) {
16647 		if (un->un_f_use_adaptive_throttle == TRUE) {
16648 			switch (throttle_type) {
16649 			case SD_THROTTLE_TRAN_BUSY:
16650 				if (un->un_busy_throttle == 0) {
16651 					un->un_busy_throttle = un->un_throttle;
16652 				}
16653 				break;
16654 			case SD_THROTTLE_QFULL:
16655 				un->un_busy_throttle = 0;
16656 				break;
16657 			default:
16658 				ASSERT(FALSE);
16659 			}
16660 
16661 			if (un->un_ncmds_in_transport > 0) {
16662 				un->un_throttle = un->un_ncmds_in_transport;
16663 			}
16664 
16665 		} else {
16666 			if (un->un_ncmds_in_transport == 0) {
16667 				un->un_throttle = 1;
16668 			} else {
16669 				un->un_throttle = un->un_ncmds_in_transport;
16670 			}
16671 		}
16672 	}
16673 
16674 	/* Reschedule the timeout if none is currently active */
16675 	if (un->un_reset_throttle_timeid == NULL) {
16676 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
16677 		    un, SD_THROTTLE_RESET_INTERVAL);
16678 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16679 		    "sd_reduce_throttle: timeout scheduled!\n");
16680 	}
16681 
16682 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
16683 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16684 }
16685 
16686 
16687 
16688 /*
16689  *    Function: sd_restore_throttle
16690  *
16691  * Description: Callback function for timeout(9F).  Resets the current
16692  *		value of un->un_throttle to its default.
16693  *
16694  *   Arguments: arg - pointer to associated softstate for the device.
16695  *
16696  *     Context: May be called from interrupt context
16697  */
16698 
16699 static void
16700 sd_restore_throttle(void *arg)
16701 {
16702 	struct sd_lun	*un = arg;
16703 
16704 	ASSERT(un != NULL);
16705 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16706 
16707 	mutex_enter(SD_MUTEX(un));
16708 
16709 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16710 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
16711 
16712 	un->un_reset_throttle_timeid = NULL;
16713 
16714 	if (un->un_f_use_adaptive_throttle == TRUE) {
16715 		/*
16716 		 * If un_busy_throttle is nonzero, then it contains the
16717 		 * value that un_throttle was when we got a TRAN_BUSY back
16718 		 * from scsi_transport(). We want to revert back to this
16719 		 * value.
16720 		 *
16721 		 * In the QFULL case, the throttle limit will incrementally
16722 		 * increase until it reaches max throttle.
16723 		 */
16724 		if (un->un_busy_throttle > 0) {
16725 			un->un_throttle = un->un_busy_throttle;
16726 			un->un_busy_throttle = 0;
16727 		} else {
16728 			/*
16729 			 * increase throttle by 10% open gate slowly, schedule
16730 			 * another restore if saved throttle has not been
16731 			 * reached
16732 			 */
16733 			short throttle;
16734 			if (sd_qfull_throttle_enable) {
16735 				throttle = un->un_throttle +
16736 				    max((un->un_throttle / 10), 1);
16737 				un->un_throttle =
16738 				    (throttle < un->un_saved_throttle) ?
16739 				    throttle : un->un_saved_throttle;
16740 				if (un->un_throttle < un->un_saved_throttle) {
16741 					un->un_reset_throttle_timeid =
16742 					    timeout(sd_restore_throttle,
16743 					    un,
16744 					    SD_QFULL_THROTTLE_RESET_INTERVAL);
16745 				}
16746 			}
16747 		}
16748 
16749 		/*
16750 		 * If un_throttle has fallen below the low-water mark, we
16751 		 * restore the maximum value here (and allow it to ratchet
16752 		 * down again if necessary).
16753 		 */
16754 		if (un->un_throttle < un->un_min_throttle) {
16755 			un->un_throttle = un->un_saved_throttle;
16756 		}
16757 	} else {
16758 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16759 		    "restoring limit from 0x%x to 0x%x\n",
16760 		    un->un_throttle, un->un_saved_throttle);
16761 		un->un_throttle = un->un_saved_throttle;
16762 	}
16763 
16764 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16765 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16766 
16767 	sd_start_cmds(un, NULL);
16768 
16769 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16770 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16771 	    un, un->un_throttle);
16772 
16773 	mutex_exit(SD_MUTEX(un));
16774 
16775 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16776 }
16777 
16778 /*
16779  *    Function: sdrunout
16780  *
16781  * Description: Callback routine for scsi_init_pkt when a resource allocation
16782  *		fails.
16783  *
16784  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16785  *		soft state instance.
16786  *
16787  * Return Code: The scsi_init_pkt routine allows for the callback function to
16788  *		return a 0 indicating the callback should be rescheduled or a 1
16789  *		indicating not to reschedule. This routine always returns 1
16790  *		because the driver always provides a callback function to
16791  *		scsi_init_pkt. This results in a callback always being scheduled
16792  *		(via the scsi_init_pkt callback implementation) if a resource
16793  *		failure occurs.
16794  *
16795  *     Context: This callback function may not block or call routines that block
16796  *
16797  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16798  *		request persisting at the head of the list which cannot be
16799  *		satisfied even after multiple retries. In the future the driver
16800  *		may implement some time of maximum runout count before failing
16801  *		an I/O.
16802  */
16803 
16804 static int
16805 sdrunout(caddr_t arg)
16806 {
16807 	struct sd_lun	*un = (struct sd_lun *)arg;
16808 
16809 	ASSERT(un != NULL);
16810 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16811 
16812 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16813 
16814 	mutex_enter(SD_MUTEX(un));
16815 	sd_start_cmds(un, NULL);
16816 	mutex_exit(SD_MUTEX(un));
16817 	/*
16818 	 * This callback routine always returns 1 (i.e. do not reschedule)
16819 	 * because we always specify sdrunout as the callback handler for
16820 	 * scsi_init_pkt inside the call to sd_start_cmds.
16821 	 */
16822 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16823 	return (1);
16824 }
16825 
16826 
16827 /*
16828  *    Function: sdintr
16829  *
16830  * Description: Completion callback routine for scsi_pkt(9S) structs
16831  *		sent to the HBA driver via scsi_transport(9F).
16832  *
16833  *     Context: Interrupt context
16834  */
16835 
16836 static void
16837 sdintr(struct scsi_pkt *pktp)
16838 {
16839 	struct buf	*bp;
16840 	struct sd_xbuf	*xp;
16841 	struct sd_lun	*un;
16842 	size_t		actual_len;
16843 	sd_ssc_t	*sscp;
16844 
16845 	ASSERT(pktp != NULL);
16846 	bp = (struct buf *)pktp->pkt_private;
16847 	ASSERT(bp != NULL);
16848 	xp = SD_GET_XBUF(bp);
16849 	ASSERT(xp != NULL);
16850 	ASSERT(xp->xb_pktp != NULL);
16851 	un = SD_GET_UN(bp);
16852 	ASSERT(un != NULL);
16853 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16854 
16855 #ifdef SD_FAULT_INJECTION
16856 
16857 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16858 	/* SD FaultInjection */
16859 	sd_faultinjection(pktp);
16860 
16861 #endif /* SD_FAULT_INJECTION */
16862 
16863 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16864 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16865 
16866 	mutex_enter(SD_MUTEX(un));
16867 
16868 	ASSERT(un->un_fm_private != NULL);
16869 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
16870 	ASSERT(sscp != NULL);
16871 
16872 	/* Reduce the count of the #commands currently in transport */
16873 	un->un_ncmds_in_transport--;
16874 	ASSERT(un->un_ncmds_in_transport >= 0);
16875 
16876 	/* Increment counter to indicate that the callback routine is active */
16877 	un->un_in_callback++;
16878 
16879 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16880 
16881 #ifdef	SDDEBUG
16882 	if (bp == un->un_retry_bp) {
16883 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16884 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16885 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16886 	}
16887 #endif
16888 
16889 	/*
16890 	 * If pkt_reason is CMD_DEV_GONE, fail the command, and update the media
16891 	 * state if needed.
16892 	 */
16893 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16894 		/* Prevent multiple console messages for the same failure. */
16895 		if (un->un_last_pkt_reason != CMD_DEV_GONE) {
16896 			un->un_last_pkt_reason = CMD_DEV_GONE;
16897 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16898 			    "Command failed to complete...Device is gone\n");
16899 		}
16900 		if (un->un_mediastate != DKIO_DEV_GONE) {
16901 			un->un_mediastate = DKIO_DEV_GONE;
16902 			cv_broadcast(&un->un_state_cv);
16903 		}
16904 		/*
16905 		 * If the command happens to be the REQUEST SENSE command,
16906 		 * free up the rqs buf and fail the original command.
16907 		 */
16908 		if (bp == un->un_rqs_bp) {
16909 			bp = sd_mark_rqs_idle(un, xp);
16910 		}
16911 		sd_return_failed_command(un, bp, EIO);
16912 		goto exit;
16913 	}
16914 
16915 	if (pktp->pkt_state & STATE_XARQ_DONE) {
16916 		SD_TRACE(SD_LOG_COMMON, un,
16917 		    "sdintr: extra sense data received. pkt=%p\n", pktp);
16918 	}
16919 
16920 	/*
16921 	 * First see if the pkt has auto-request sense data with it....
16922 	 * Look at the packet state first so we don't take a performance
16923 	 * hit looking at the arq enabled flag unless absolutely necessary.
16924 	 */
16925 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16926 	    (un->un_f_arq_enabled == TRUE)) {
16927 		/*
16928 		 * The HBA did an auto request sense for this command so check
16929 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16930 		 * driver command that should not be retried.
16931 		 */
16932 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16933 			/*
16934 			 * Save the relevant sense info into the xp for the
16935 			 * original cmd.
16936 			 */
16937 			struct scsi_arq_status *asp;
16938 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16939 			xp->xb_sense_status =
16940 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16941 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16942 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16943 			if (pktp->pkt_state & STATE_XARQ_DONE) {
16944 				actual_len = MAX_SENSE_LENGTH -
16945 				    xp->xb_sense_resid;
16946 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16947 				    MAX_SENSE_LENGTH);
16948 			} else {
16949 				if (xp->xb_sense_resid > SENSE_LENGTH) {
16950 					actual_len = MAX_SENSE_LENGTH -
16951 					    xp->xb_sense_resid;
16952 				} else {
16953 					actual_len = SENSE_LENGTH -
16954 					    xp->xb_sense_resid;
16955 				}
16956 				if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16957 					if ((((struct uscsi_cmd *)
16958 					    (xp->xb_pktinfo))->uscsi_rqlen) >
16959 					    actual_len) {
16960 						xp->xb_sense_resid =
16961 						    (((struct uscsi_cmd *)
16962 						    (xp->xb_pktinfo))->
16963 						    uscsi_rqlen) - actual_len;
16964 					} else {
16965 						xp->xb_sense_resid = 0;
16966 					}
16967 				}
16968 				bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16969 				    SENSE_LENGTH);
16970 			}
16971 
16972 			/* fail the command */
16973 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16974 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16975 			sd_return_failed_command(un, bp, EIO);
16976 			goto exit;
16977 		}
16978 
16979 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16980 		/*
16981 		 * We want to either retry or fail this command, so free
16982 		 * the DMA resources here.  If we retry the command then
16983 		 * the DMA resources will be reallocated in sd_start_cmds().
16984 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16985 		 * causes the *entire* transfer to start over again from the
16986 		 * beginning of the request, even for PARTIAL chunks that
16987 		 * have already transferred successfully.
16988 		 */
16989 		if ((un->un_f_is_fibre == TRUE) &&
16990 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16991 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16992 			scsi_dmafree(pktp);
16993 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16994 		}
16995 #endif
16996 
16997 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16998 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16999 
17000 		sd_handle_auto_request_sense(un, bp, xp, pktp);
17001 		goto exit;
17002 	}
17003 
17004 	/* Next see if this is the REQUEST SENSE pkt for the instance */
17005 	if (pktp->pkt_flags & FLAG_SENSING)  {
17006 		/* This pktp is from the unit's REQUEST_SENSE command */
17007 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17008 		    "sdintr: sd_handle_request_sense\n");
17009 		sd_handle_request_sense(un, bp, xp, pktp);
17010 		goto exit;
17011 	}
17012 
17013 	/*
17014 	 * Check to see if the command successfully completed as requested;
17015 	 * this is the most common case (and also the hot performance path).
17016 	 *
17017 	 * Requirements for successful completion are:
17018 	 * pkt_reason is CMD_CMPLT and packet status is status good.
17019 	 * In addition:
17020 	 * - A residual of zero indicates successful completion no matter what
17021 	 *   the command is.
17022 	 * - If the residual is not zero and the command is not a read or
17023 	 *   write, then it's still defined as successful completion. In other
17024 	 *   words, if the command is a read or write the residual must be
17025 	 *   zero for successful completion.
17026 	 * - If the residual is not zero and the command is a read or
17027 	 *   write, and it's a USCSICMD, then it's still defined as
17028 	 *   successful completion.
17029 	 */
17030 	if ((pktp->pkt_reason == CMD_CMPLT) &&
17031 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
17032 
17033 		/*
17034 		 * Since this command is returned with a good status, we
17035 		 * can reset the count for Sonoma failover.
17036 		 */
17037 		un->un_sonoma_failure_count = 0;
17038 
17039 		/*
17040 		 * Return all USCSI commands on good status
17041 		 */
17042 		if (pktp->pkt_resid == 0) {
17043 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17044 			    "sdintr: returning command for resid == 0\n");
17045 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
17046 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
17047 			SD_UPDATE_B_RESID(bp, pktp);
17048 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17049 			    "sdintr: returning command for resid != 0\n");
17050 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17051 			SD_UPDATE_B_RESID(bp, pktp);
17052 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17053 			    "sdintr: returning uscsi command\n");
17054 		} else {
17055 			goto not_successful;
17056 		}
17057 		sd_return_command(un, bp);
17058 
17059 		/*
17060 		 * Decrement counter to indicate that the callback routine
17061 		 * is done.
17062 		 */
17063 		un->un_in_callback--;
17064 		ASSERT(un->un_in_callback >= 0);
17065 		mutex_exit(SD_MUTEX(un));
17066 
17067 		return;
17068 	}
17069 
17070 not_successful:
17071 
17072 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
17073 	/*
17074 	 * The following is based upon knowledge of the underlying transport
17075 	 * and its use of DMA resources.  This code should be removed when
17076 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
17077 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
17078 	 * and sd_start_cmds().
17079 	 *
17080 	 * Free any DMA resources associated with this command if there
17081 	 * is a chance it could be retried or enqueued for later retry.
17082 	 * If we keep the DMA binding then mpxio cannot reissue the
17083 	 * command on another path whenever a path failure occurs.
17084 	 *
17085 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
17086 	 * causes the *entire* transfer to start over again from the
17087 	 * beginning of the request, even for PARTIAL chunks that
17088 	 * have already transferred successfully.
17089 	 *
17090 	 * This is only done for non-uscsi commands (and also skipped for the
17091 	 * driver's internal RQS command). Also just do this for Fibre Channel
17092 	 * devices as these are the only ones that support mpxio.
17093 	 */
17094 	if ((un->un_f_is_fibre == TRUE) &&
17095 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
17096 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
17097 		scsi_dmafree(pktp);
17098 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
17099 	}
17100 #endif
17101 
17102 	/*
17103 	 * The command did not successfully complete as requested so check
17104 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
17105 	 * driver command that should not be retried so just return. If
17106 	 * FLAG_DIAGNOSE is not set the error will be processed below.
17107 	 */
17108 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
17109 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17110 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
17111 		/*
17112 		 * Issue a request sense if a check condition caused the error
17113 		 * (we handle the auto request sense case above), otherwise
17114 		 * just fail the command.
17115 		 */
17116 		if ((pktp->pkt_reason == CMD_CMPLT) &&
17117 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
17118 			sd_send_request_sense_command(un, bp, pktp);
17119 		} else {
17120 			sd_return_failed_command(un, bp, EIO);
17121 		}
17122 		goto exit;
17123 	}
17124 
17125 	/*
17126 	 * The command did not successfully complete as requested so process
17127 	 * the error, retry, and/or attempt recovery.
17128 	 */
17129 	switch (pktp->pkt_reason) {
17130 	case CMD_CMPLT:
17131 		switch (SD_GET_PKT_STATUS(pktp)) {
17132 		case STATUS_GOOD:
17133 			/*
17134 			 * The command completed successfully with a non-zero
17135 			 * residual
17136 			 */
17137 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17138 			    "sdintr: STATUS_GOOD \n");
17139 			sd_pkt_status_good(un, bp, xp, pktp);
17140 			break;
17141 
17142 		case STATUS_CHECK:
17143 		case STATUS_TERMINATED:
17144 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17145 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
17146 			sd_pkt_status_check_condition(un, bp, xp, pktp);
17147 			break;
17148 
17149 		case STATUS_BUSY:
17150 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17151 			    "sdintr: STATUS_BUSY\n");
17152 			sd_pkt_status_busy(un, bp, xp, pktp);
17153 			break;
17154 
17155 		case STATUS_RESERVATION_CONFLICT:
17156 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17157 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
17158 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17159 			break;
17160 
17161 		case STATUS_QFULL:
17162 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17163 			    "sdintr: STATUS_QFULL\n");
17164 			sd_pkt_status_qfull(un, bp, xp, pktp);
17165 			break;
17166 
17167 		case STATUS_MET:
17168 		case STATUS_INTERMEDIATE:
17169 		case STATUS_SCSI2:
17170 		case STATUS_INTERMEDIATE_MET:
17171 		case STATUS_ACA_ACTIVE:
17172 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17173 			    "Unexpected SCSI status received: 0x%x\n",
17174 			    SD_GET_PKT_STATUS(pktp));
17175 			/*
17176 			 * Mark the ssc_flags when detected invalid status
17177 			 * code for non-USCSI command.
17178 			 */
17179 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17180 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17181 				    0, "stat-code");
17182 			}
17183 			sd_return_failed_command(un, bp, EIO);
17184 			break;
17185 
17186 		default:
17187 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17188 			    "Invalid SCSI status received: 0x%x\n",
17189 			    SD_GET_PKT_STATUS(pktp));
17190 			if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17191 				sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_STATUS,
17192 				    0, "stat-code");
17193 			}
17194 			sd_return_failed_command(un, bp, EIO);
17195 			break;
17196 
17197 		}
17198 		break;
17199 
17200 	case CMD_INCOMPLETE:
17201 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17202 		    "sdintr:  CMD_INCOMPLETE\n");
17203 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
17204 		break;
17205 	case CMD_TRAN_ERR:
17206 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17207 		    "sdintr: CMD_TRAN_ERR\n");
17208 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
17209 		break;
17210 	case CMD_RESET:
17211 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17212 		    "sdintr: CMD_RESET \n");
17213 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
17214 		break;
17215 	case CMD_ABORTED:
17216 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17217 		    "sdintr: CMD_ABORTED \n");
17218 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
17219 		break;
17220 	case CMD_TIMEOUT:
17221 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17222 		    "sdintr: CMD_TIMEOUT\n");
17223 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
17224 		break;
17225 	case CMD_UNX_BUS_FREE:
17226 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17227 		    "sdintr: CMD_UNX_BUS_FREE \n");
17228 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
17229 		break;
17230 	case CMD_TAG_REJECT:
17231 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17232 		    "sdintr: CMD_TAG_REJECT\n");
17233 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
17234 		break;
17235 	default:
17236 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
17237 		    "sdintr: default\n");
17238 		/*
17239 		 * Mark the ssc_flags for detecting invliad pkt_reason.
17240 		 */
17241 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17242 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_PKT_REASON,
17243 			    0, "pkt-reason");
17244 		}
17245 		sd_pkt_reason_default(un, bp, xp, pktp);
17246 		break;
17247 	}
17248 
17249 exit:
17250 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
17251 
17252 	/* Decrement counter to indicate that the callback routine is done. */
17253 	un->un_in_callback--;
17254 	ASSERT(un->un_in_callback >= 0);
17255 
17256 	/*
17257 	 * At this point, the pkt has been dispatched, ie, it is either
17258 	 * being re-tried or has been returned to its caller and should
17259 	 * not be referenced.
17260 	 */
17261 
17262 	mutex_exit(SD_MUTEX(un));
17263 }
17264 
17265 
17266 /*
17267  *    Function: sd_print_incomplete_msg
17268  *
17269  * Description: Prints the error message for a CMD_INCOMPLETE error.
17270  *
17271  *   Arguments: un - ptr to associated softstate for the device.
17272  *		bp - ptr to the buf(9S) for the command.
17273  *		arg - message string ptr
17274  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
17275  *			or SD_NO_RETRY_ISSUED.
17276  *
17277  *     Context: May be called under interrupt context
17278  */
17279 
17280 static void
17281 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17282 {
17283 	struct scsi_pkt	*pktp;
17284 	char	*msgp;
17285 	char	*cmdp = arg;
17286 
17287 	ASSERT(un != NULL);
17288 	ASSERT(mutex_owned(SD_MUTEX(un)));
17289 	ASSERT(bp != NULL);
17290 	ASSERT(arg != NULL);
17291 	pktp = SD_GET_PKTP(bp);
17292 	ASSERT(pktp != NULL);
17293 
17294 	switch (code) {
17295 	case SD_DELAYED_RETRY_ISSUED:
17296 	case SD_IMMEDIATE_RETRY_ISSUED:
17297 		msgp = "retrying";
17298 		break;
17299 	case SD_NO_RETRY_ISSUED:
17300 	default:
17301 		msgp = "giving up";
17302 		break;
17303 	}
17304 
17305 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17306 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17307 		    "incomplete %s- %s\n", cmdp, msgp);
17308 	}
17309 }
17310 
17311 
17312 
17313 /*
17314  *    Function: sd_pkt_status_good
17315  *
17316  * Description: Processing for a STATUS_GOOD code in pkt_status.
17317  *
17318  *     Context: May be called under interrupt context
17319  */
17320 
17321 static void
17322 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
17323 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17324 {
17325 	char	*cmdp;
17326 
17327 	ASSERT(un != NULL);
17328 	ASSERT(mutex_owned(SD_MUTEX(un)));
17329 	ASSERT(bp != NULL);
17330 	ASSERT(xp != NULL);
17331 	ASSERT(pktp != NULL);
17332 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
17333 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
17334 	ASSERT(pktp->pkt_resid != 0);
17335 
17336 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
17337 
17338 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17339 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
17340 	case SCMD_READ:
17341 		cmdp = "read";
17342 		break;
17343 	case SCMD_WRITE:
17344 		cmdp = "write";
17345 		break;
17346 	default:
17347 		SD_UPDATE_B_RESID(bp, pktp);
17348 		sd_return_command(un, bp);
17349 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17350 		return;
17351 	}
17352 
17353 	/*
17354 	 * See if we can retry the read/write, preferrably immediately.
17355 	 * If retries are exhaused, then sd_retry_command() will update
17356 	 * the b_resid count.
17357 	 */
17358 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
17359 	    cmdp, EIO, (clock_t)0, NULL);
17360 
17361 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
17362 }
17363 
17364 
17365 
17366 
17367 
17368 /*
17369  *    Function: sd_handle_request_sense
17370  *
17371  * Description: Processing for non-auto Request Sense command.
17372  *
17373  *   Arguments: un - ptr to associated softstate
17374  *		sense_bp - ptr to buf(9S) for the RQS command
17375  *		sense_xp - ptr to the sd_xbuf for the RQS command
17376  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
17377  *
17378  *     Context: May be called under interrupt context
17379  */
17380 
17381 static void
17382 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
17383 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
17384 {
17385 	struct buf	*cmd_bp;	/* buf for the original command */
17386 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
17387 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
17388 	size_t		actual_len;	/* actual sense data length */
17389 
17390 	ASSERT(un != NULL);
17391 	ASSERT(mutex_owned(SD_MUTEX(un)));
17392 	ASSERT(sense_bp != NULL);
17393 	ASSERT(sense_xp != NULL);
17394 	ASSERT(sense_pktp != NULL);
17395 
17396 	/*
17397 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
17398 	 * RQS command and not the original command.
17399 	 */
17400 	ASSERT(sense_pktp == un->un_rqs_pktp);
17401 	ASSERT(sense_bp   == un->un_rqs_bp);
17402 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
17403 	    (FLAG_SENSING | FLAG_HEAD));
17404 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
17405 	    FLAG_SENSING) == FLAG_SENSING);
17406 
17407 	/* These are the bp, xp, and pktp for the original command */
17408 	cmd_bp = sense_xp->xb_sense_bp;
17409 	cmd_xp = SD_GET_XBUF(cmd_bp);
17410 	cmd_pktp = SD_GET_PKTP(cmd_bp);
17411 
17412 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
17413 		/*
17414 		 * The REQUEST SENSE command failed.  Release the REQUEST
17415 		 * SENSE command for re-use, get back the bp for the original
17416 		 * command, and attempt to re-try the original command if
17417 		 * FLAG_DIAGNOSE is not set in the original packet.
17418 		 */
17419 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17420 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17421 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
17422 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
17423 			    NULL, NULL, EIO, (clock_t)0, NULL);
17424 			return;
17425 		}
17426 	}
17427 
17428 	/*
17429 	 * Save the relevant sense info into the xp for the original cmd.
17430 	 *
17431 	 * Note: if the request sense failed the state info will be zero
17432 	 * as set in sd_mark_rqs_busy()
17433 	 */
17434 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
17435 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
17436 	actual_len = MAX_SENSE_LENGTH - sense_pktp->pkt_resid;
17437 	if ((cmd_xp->xb_pkt_flags & SD_XB_USCSICMD) &&
17438 	    (((struct uscsi_cmd *)cmd_xp->xb_pktinfo)->uscsi_rqlen >
17439 	    SENSE_LENGTH)) {
17440 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17441 		    MAX_SENSE_LENGTH);
17442 		cmd_xp->xb_sense_resid = sense_pktp->pkt_resid;
17443 	} else {
17444 		bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data,
17445 		    SENSE_LENGTH);
17446 		if (actual_len < SENSE_LENGTH) {
17447 			cmd_xp->xb_sense_resid = SENSE_LENGTH - actual_len;
17448 		} else {
17449 			cmd_xp->xb_sense_resid = 0;
17450 		}
17451 	}
17452 
17453 	/*
17454 	 *  Free up the RQS command....
17455 	 *  NOTE:
17456 	 *	Must do this BEFORE calling sd_validate_sense_data!
17457 	 *	sd_validate_sense_data may return the original command in
17458 	 *	which case the pkt will be freed and the flags can no
17459 	 *	longer be touched.
17460 	 *	SD_MUTEX is held through this process until the command
17461 	 *	is dispatched based upon the sense data, so there are
17462 	 *	no race conditions.
17463 	 */
17464 	(void) sd_mark_rqs_idle(un, sense_xp);
17465 
17466 	/*
17467 	 * For a retryable command see if we have valid sense data, if so then
17468 	 * turn it over to sd_decode_sense() to figure out the right course of
17469 	 * action. Just fail a non-retryable command.
17470 	 */
17471 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
17472 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp, actual_len) ==
17473 		    SD_SENSE_DATA_IS_VALID) {
17474 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
17475 		}
17476 	} else {
17477 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
17478 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17479 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
17480 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
17481 		sd_return_failed_command(un, cmd_bp, EIO);
17482 	}
17483 }
17484 
17485 
17486 
17487 
17488 /*
17489  *    Function: sd_handle_auto_request_sense
17490  *
17491  * Description: Processing for auto-request sense information.
17492  *
17493  *   Arguments: un - ptr to associated softstate
17494  *		bp - ptr to buf(9S) for the command
17495  *		xp - ptr to the sd_xbuf for the command
17496  *		pktp - ptr to the scsi_pkt(9S) for the command
17497  *
17498  *     Context: May be called under interrupt context
17499  */
17500 
17501 static void
17502 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
17503 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17504 {
17505 	struct scsi_arq_status *asp;
17506 	size_t actual_len;
17507 
17508 	ASSERT(un != NULL);
17509 	ASSERT(mutex_owned(SD_MUTEX(un)));
17510 	ASSERT(bp != NULL);
17511 	ASSERT(xp != NULL);
17512 	ASSERT(pktp != NULL);
17513 	ASSERT(pktp != un->un_rqs_pktp);
17514 	ASSERT(bp   != un->un_rqs_bp);
17515 
17516 	/*
17517 	 * For auto-request sense, we get a scsi_arq_status back from
17518 	 * the HBA, with the sense data in the sts_sensedata member.
17519 	 * The pkt_scbp of the packet points to this scsi_arq_status.
17520 	 */
17521 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
17522 
17523 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
17524 		/*
17525 		 * The auto REQUEST SENSE failed; see if we can re-try
17526 		 * the original command.
17527 		 */
17528 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17529 		    "auto request sense failed (reason=%s)\n",
17530 		    scsi_rname(asp->sts_rqpkt_reason));
17531 
17532 		sd_reset_target(un, pktp);
17533 
17534 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17535 		    NULL, NULL, EIO, (clock_t)0, NULL);
17536 		return;
17537 	}
17538 
17539 	/* Save the relevant sense info into the xp for the original cmd. */
17540 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
17541 	xp->xb_sense_state  = asp->sts_rqpkt_state;
17542 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
17543 	if (xp->xb_sense_state & STATE_XARQ_DONE) {
17544 		actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17545 		bcopy(&asp->sts_sensedata, xp->xb_sense_data,
17546 		    MAX_SENSE_LENGTH);
17547 	} else {
17548 		if (xp->xb_sense_resid > SENSE_LENGTH) {
17549 			actual_len = MAX_SENSE_LENGTH - xp->xb_sense_resid;
17550 		} else {
17551 			actual_len = SENSE_LENGTH - xp->xb_sense_resid;
17552 		}
17553 		if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
17554 			if ((((struct uscsi_cmd *)
17555 			    (xp->xb_pktinfo))->uscsi_rqlen) > actual_len) {
17556 				xp->xb_sense_resid = (((struct uscsi_cmd *)
17557 				    (xp->xb_pktinfo))->uscsi_rqlen) -
17558 				    actual_len;
17559 			} else {
17560 				xp->xb_sense_resid = 0;
17561 			}
17562 		}
17563 		bcopy(&asp->sts_sensedata, xp->xb_sense_data, SENSE_LENGTH);
17564 	}
17565 
17566 	/*
17567 	 * See if we have valid sense data, if so then turn it over to
17568 	 * sd_decode_sense() to figure out the right course of action.
17569 	 */
17570 	if (sd_validate_sense_data(un, bp, xp, actual_len) ==
17571 	    SD_SENSE_DATA_IS_VALID) {
17572 		sd_decode_sense(un, bp, xp, pktp);
17573 	}
17574 }
17575 
17576 
17577 /*
17578  *    Function: sd_print_sense_failed_msg
17579  *
17580  * Description: Print log message when RQS has failed.
17581  *
17582  *   Arguments: un - ptr to associated softstate
17583  *		bp - ptr to buf(9S) for the command
17584  *		arg - generic message string ptr
17585  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17586  *			or SD_NO_RETRY_ISSUED
17587  *
17588  *     Context: May be called from interrupt context
17589  */
17590 
17591 static void
17592 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
17593 	int code)
17594 {
17595 	char	*msgp = arg;
17596 
17597 	ASSERT(un != NULL);
17598 	ASSERT(mutex_owned(SD_MUTEX(un)));
17599 	ASSERT(bp != NULL);
17600 
17601 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
17602 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
17603 	}
17604 }
17605 
17606 
17607 /*
17608  *    Function: sd_validate_sense_data
17609  *
17610  * Description: Check the given sense data for validity.
17611  *		If the sense data is not valid, the command will
17612  *		be either failed or retried!
17613  *
17614  * Return Code: SD_SENSE_DATA_IS_INVALID
17615  *		SD_SENSE_DATA_IS_VALID
17616  *
17617  *     Context: May be called from interrupt context
17618  */
17619 
17620 static int
17621 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17622 	size_t actual_len)
17623 {
17624 	struct scsi_extended_sense *esp;
17625 	struct	scsi_pkt *pktp;
17626 	char	*msgp = NULL;
17627 	sd_ssc_t *sscp;
17628 
17629 	ASSERT(un != NULL);
17630 	ASSERT(mutex_owned(SD_MUTEX(un)));
17631 	ASSERT(bp != NULL);
17632 	ASSERT(bp != un->un_rqs_bp);
17633 	ASSERT(xp != NULL);
17634 	ASSERT(un->un_fm_private != NULL);
17635 
17636 	pktp = SD_GET_PKTP(bp);
17637 	ASSERT(pktp != NULL);
17638 
17639 	sscp = &((struct sd_fm_internal *)(un->un_fm_private))->fm_ssc;
17640 	ASSERT(sscp != NULL);
17641 
17642 	/*
17643 	 * Check the status of the RQS command (auto or manual).
17644 	 */
17645 	switch (xp->xb_sense_status & STATUS_MASK) {
17646 	case STATUS_GOOD:
17647 		break;
17648 
17649 	case STATUS_RESERVATION_CONFLICT:
17650 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
17651 		return (SD_SENSE_DATA_IS_INVALID);
17652 
17653 	case STATUS_BUSY:
17654 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17655 		    "Busy Status on REQUEST SENSE\n");
17656 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
17657 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17658 		return (SD_SENSE_DATA_IS_INVALID);
17659 
17660 	case STATUS_QFULL:
17661 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17662 		    "QFULL Status on REQUEST SENSE\n");
17663 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
17664 		    NULL, EIO, un->un_busy_timeout / 500, kstat_waitq_enter);
17665 		return (SD_SENSE_DATA_IS_INVALID);
17666 
17667 	case STATUS_CHECK:
17668 	case STATUS_TERMINATED:
17669 		msgp = "Check Condition on REQUEST SENSE\n";
17670 		goto sense_failed;
17671 
17672 	default:
17673 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
17674 		goto sense_failed;
17675 	}
17676 
17677 	/*
17678 	 * See if we got the minimum required amount of sense data.
17679 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
17680 	 * or less.
17681 	 */
17682 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
17683 	    (actual_len == 0)) {
17684 		msgp = "Request Sense couldn't get sense data\n";
17685 		goto sense_failed;
17686 	}
17687 
17688 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
17689 		msgp = "Not enough sense information\n";
17690 		/* Mark the ssc_flags for detecting invalid sense data */
17691 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17692 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17693 			    "sense-data");
17694 		}
17695 		goto sense_failed;
17696 	}
17697 
17698 	/*
17699 	 * We require the extended sense data
17700 	 */
17701 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
17702 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
17703 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17704 			static char tmp[8];
17705 			static char buf[148];
17706 			char *p = (char *)(xp->xb_sense_data);
17707 			int i;
17708 
17709 			mutex_enter(&sd_sense_mutex);
17710 			(void) strcpy(buf, "undecodable sense information:");
17711 			for (i = 0; i < actual_len; i++) {
17712 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
17713 				(void) strcpy(&buf[strlen(buf)], tmp);
17714 			}
17715 			i = strlen(buf);
17716 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
17717 
17718 			if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
17719 				scsi_log(SD_DEVINFO(un), sd_label,
17720 				    CE_WARN, buf);
17721 			}
17722 			mutex_exit(&sd_sense_mutex);
17723 		}
17724 
17725 		/* Mark the ssc_flags for detecting invalid sense data */
17726 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17727 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17728 			    "sense-data");
17729 		}
17730 
17731 		/* Note: Legacy behavior, fail the command with no retry */
17732 		sd_return_failed_command(un, bp, EIO);
17733 		return (SD_SENSE_DATA_IS_INVALID);
17734 	}
17735 
17736 	/*
17737 	 * Check that es_code is valid (es_class concatenated with es_code
17738 	 * make up the "response code" field.  es_class will always be 7, so
17739 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
17740 	 * format.
17741 	 */
17742 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
17743 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
17744 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
17745 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
17746 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
17747 		/* Mark the ssc_flags for detecting invalid sense data */
17748 		if (!(xp->xb_pkt_flags & SD_XB_USCSICMD)) {
17749 			sd_ssc_set_info(sscp, SSC_FLAGS_INVALID_SENSE, 0,
17750 			    "sense-data");
17751 		}
17752 		goto sense_failed;
17753 	}
17754 
17755 	return (SD_SENSE_DATA_IS_VALID);
17756 
17757 sense_failed:
17758 	/*
17759 	 * If the request sense failed (for whatever reason), attempt
17760 	 * to retry the original command.
17761 	 */
17762 #if defined(__i386) || defined(__amd64)
17763 	/*
17764 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
17765 	 * sddef.h for Sparc platform, and x86 uses 1 binary
17766 	 * for both SCSI/FC.
17767 	 * The SD_RETRY_DELAY value need to be adjusted here
17768 	 * when SD_RETRY_DELAY change in sddef.h
17769 	 */
17770 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17771 	    sd_print_sense_failed_msg, msgp, EIO,
17772 	    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
17773 #else
17774 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
17775 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
17776 #endif
17777 
17778 	return (SD_SENSE_DATA_IS_INVALID);
17779 }
17780 
17781 /*
17782  *    Function: sd_decode_sense
17783  *
17784  * Description: Take recovery action(s) when SCSI Sense Data is received.
17785  *
17786  *     Context: Interrupt context.
17787  */
17788 
17789 static void
17790 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
17791 	struct scsi_pkt *pktp)
17792 {
17793 	uint8_t sense_key;
17794 
17795 	ASSERT(un != NULL);
17796 	ASSERT(mutex_owned(SD_MUTEX(un)));
17797 	ASSERT(bp != NULL);
17798 	ASSERT(bp != un->un_rqs_bp);
17799 	ASSERT(xp != NULL);
17800 	ASSERT(pktp != NULL);
17801 
17802 	sense_key = scsi_sense_key(xp->xb_sense_data);
17803 
17804 	switch (sense_key) {
17805 	case KEY_NO_SENSE:
17806 		sd_sense_key_no_sense(un, bp, xp, pktp);
17807 		break;
17808 	case KEY_RECOVERABLE_ERROR:
17809 		sd_sense_key_recoverable_error(un, xp->xb_sense_data,
17810 		    bp, xp, pktp);
17811 		break;
17812 	case KEY_NOT_READY:
17813 		sd_sense_key_not_ready(un, xp->xb_sense_data,
17814 		    bp, xp, pktp);
17815 		break;
17816 	case KEY_MEDIUM_ERROR:
17817 	case KEY_HARDWARE_ERROR:
17818 		sd_sense_key_medium_or_hardware_error(un,
17819 		    xp->xb_sense_data, bp, xp, pktp);
17820 		break;
17821 	case KEY_ILLEGAL_REQUEST:
17822 		sd_sense_key_illegal_request(un, bp, xp, pktp);
17823 		break;
17824 	case KEY_UNIT_ATTENTION:
17825 		sd_sense_key_unit_attention(un, xp->xb_sense_data,
17826 		    bp, xp, pktp);
17827 		break;
17828 	case KEY_WRITE_PROTECT:
17829 	case KEY_VOLUME_OVERFLOW:
17830 	case KEY_MISCOMPARE:
17831 		sd_sense_key_fail_command(un, bp, xp, pktp);
17832 		break;
17833 	case KEY_BLANK_CHECK:
17834 		sd_sense_key_blank_check(un, bp, xp, pktp);
17835 		break;
17836 	case KEY_ABORTED_COMMAND:
17837 		sd_sense_key_aborted_command(un, bp, xp, pktp);
17838 		break;
17839 	case KEY_VENDOR_UNIQUE:
17840 	case KEY_COPY_ABORTED:
17841 	case KEY_EQUAL:
17842 	case KEY_RESERVED:
17843 	default:
17844 		sd_sense_key_default(un, xp->xb_sense_data,
17845 		    bp, xp, pktp);
17846 		break;
17847 	}
17848 }
17849 
17850 
17851 /*
17852  *    Function: sd_dump_memory
17853  *
17854  * Description: Debug logging routine to print the contents of a user provided
17855  *		buffer. The output of the buffer is broken up into 256 byte
17856  *		segments due to a size constraint of the scsi_log.
17857  *		implementation.
17858  *
17859  *   Arguments: un - ptr to softstate
17860  *		comp - component mask
17861  *		title - "title" string to preceed data when printed
17862  *		data - ptr to data block to be printed
17863  *		len - size of data block to be printed
17864  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17865  *
17866  *     Context: May be called from interrupt context
17867  */
17868 
17869 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17870 
17871 static char *sd_dump_format_string[] = {
17872 		" 0x%02x",
17873 		" %c"
17874 };
17875 
17876 static void
17877 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17878     int len, int fmt)
17879 {
17880 	int	i, j;
17881 	int	avail_count;
17882 	int	start_offset;
17883 	int	end_offset;
17884 	size_t	entry_len;
17885 	char	*bufp;
17886 	char	*local_buf;
17887 	char	*format_string;
17888 
17889 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17890 
17891 	/*
17892 	 * In the debug version of the driver, this function is called from a
17893 	 * number of places which are NOPs in the release driver.
17894 	 * The debug driver therefore has additional methods of filtering
17895 	 * debug output.
17896 	 */
17897 #ifdef SDDEBUG
17898 	/*
17899 	 * In the debug version of the driver we can reduce the amount of debug
17900 	 * messages by setting sd_error_level to something other than
17901 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17902 	 * sd_component_mask.
17903 	 */
17904 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17905 	    (sd_error_level != SCSI_ERR_ALL)) {
17906 		return;
17907 	}
17908 	if (((sd_component_mask & comp) == 0) ||
17909 	    (sd_error_level != SCSI_ERR_ALL)) {
17910 		return;
17911 	}
17912 #else
17913 	if (sd_error_level != SCSI_ERR_ALL) {
17914 		return;
17915 	}
17916 #endif
17917 
17918 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17919 	bufp = local_buf;
17920 	/*
17921 	 * Available length is the length of local_buf[], minus the
17922 	 * length of the title string, minus one for the ":", minus
17923 	 * one for the newline, minus one for the NULL terminator.
17924 	 * This gives the #bytes available for holding the printed
17925 	 * values from the given data buffer.
17926 	 */
17927 	if (fmt == SD_LOG_HEX) {
17928 		format_string = sd_dump_format_string[0];
17929 	} else /* SD_LOG_CHAR */ {
17930 		format_string = sd_dump_format_string[1];
17931 	}
17932 	/*
17933 	 * Available count is the number of elements from the given
17934 	 * data buffer that we can fit into the available length.
17935 	 * This is based upon the size of the format string used.
17936 	 * Make one entry and find it's size.
17937 	 */
17938 	(void) sprintf(bufp, format_string, data[0]);
17939 	entry_len = strlen(bufp);
17940 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17941 
17942 	j = 0;
17943 	while (j < len) {
17944 		bufp = local_buf;
17945 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17946 		start_offset = j;
17947 
17948 		end_offset = start_offset + avail_count;
17949 
17950 		(void) sprintf(bufp, "%s:", title);
17951 		bufp += strlen(bufp);
17952 		for (i = start_offset; ((i < end_offset) && (j < len));
17953 		    i++, j++) {
17954 			(void) sprintf(bufp, format_string, data[i]);
17955 			bufp += entry_len;
17956 		}
17957 		(void) sprintf(bufp, "\n");
17958 
17959 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17960 	}
17961 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17962 }
17963 
17964 /*
17965  *    Function: sd_print_sense_msg
17966  *
17967  * Description: Log a message based upon the given sense data.
17968  *
17969  *   Arguments: un - ptr to associated softstate
17970  *		bp - ptr to buf(9S) for the command
17971  *		arg - ptr to associate sd_sense_info struct
17972  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17973  *			or SD_NO_RETRY_ISSUED
17974  *
17975  *     Context: May be called from interrupt context
17976  */
17977 
17978 static void
17979 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17980 {
17981 	struct sd_xbuf	*xp;
17982 	struct scsi_pkt	*pktp;
17983 	uint8_t *sensep;
17984 	daddr_t request_blkno;
17985 	diskaddr_t err_blkno;
17986 	int severity;
17987 	int pfa_flag;
17988 	extern struct scsi_key_strings scsi_cmds[];
17989 
17990 	ASSERT(un != NULL);
17991 	ASSERT(mutex_owned(SD_MUTEX(un)));
17992 	ASSERT(bp != NULL);
17993 	xp = SD_GET_XBUF(bp);
17994 	ASSERT(xp != NULL);
17995 	pktp = SD_GET_PKTP(bp);
17996 	ASSERT(pktp != NULL);
17997 	ASSERT(arg != NULL);
17998 
17999 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
18000 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
18001 
18002 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
18003 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
18004 		severity = SCSI_ERR_RETRYABLE;
18005 	}
18006 
18007 	/* Use absolute block number for the request block number */
18008 	request_blkno = xp->xb_blkno;
18009 
18010 	/*
18011 	 * Now try to get the error block number from the sense data
18012 	 */
18013 	sensep = xp->xb_sense_data;
18014 
18015 	if (scsi_sense_info_uint64(sensep, SENSE_LENGTH,
18016 	    (uint64_t *)&err_blkno)) {
18017 		/*
18018 		 * We retrieved the error block number from the information
18019 		 * portion of the sense data.
18020 		 *
18021 		 * For USCSI commands we are better off using the error
18022 		 * block no. as the requested block no. (This is the best
18023 		 * we can estimate.)
18024 		 */
18025 		if ((SD_IS_BUFIO(xp) == FALSE) &&
18026 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
18027 			request_blkno = err_blkno;
18028 		}
18029 	} else {
18030 		/*
18031 		 * Without the es_valid bit set (for fixed format) or an
18032 		 * information descriptor (for descriptor format) we cannot
18033 		 * be certain of the error blkno, so just use the
18034 		 * request_blkno.
18035 		 */
18036 		err_blkno = (diskaddr_t)request_blkno;
18037 	}
18038 
18039 	/*
18040 	 * The following will log the buffer contents for the release driver
18041 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
18042 	 * level is set to verbose.
18043 	 */
18044 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
18045 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
18046 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
18047 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
18048 
18049 	if (pfa_flag == FALSE) {
18050 		/* This is normally only set for USCSI */
18051 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
18052 			return;
18053 		}
18054 
18055 		if ((SD_IS_BUFIO(xp) == TRUE) &&
18056 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
18057 		    (severity < sd_error_level))) {
18058 			return;
18059 		}
18060 	}
18061 	/*
18062 	 * Check for Sonoma Failover and keep a count of how many failed I/O's
18063 	 */
18064 	if ((SD_IS_LSI(un)) &&
18065 	    (scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) &&
18066 	    (scsi_sense_asc(sensep) == 0x94) &&
18067 	    (scsi_sense_ascq(sensep) == 0x01)) {
18068 		un->un_sonoma_failure_count++;
18069 		if (un->un_sonoma_failure_count > 1) {
18070 			return;
18071 		}
18072 	}
18073 
18074 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP ||
18075 	    ((scsi_sense_key(sensep) == KEY_RECOVERABLE_ERROR) &&
18076 	    (pktp->pkt_resid == 0))) {
18077 		scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
18078 		    request_blkno, err_blkno, scsi_cmds,
18079 		    (struct scsi_extended_sense *)sensep,
18080 		    un->un_additional_codes, NULL);
18081 	}
18082 }
18083 
18084 /*
18085  *    Function: sd_sense_key_no_sense
18086  *
18087  * Description: Recovery action when sense data was not received.
18088  *
18089  *     Context: May be called from interrupt context
18090  */
18091 
18092 static void
18093 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
18094 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18095 {
18096 	struct sd_sense_info	si;
18097 
18098 	ASSERT(un != NULL);
18099 	ASSERT(mutex_owned(SD_MUTEX(un)));
18100 	ASSERT(bp != NULL);
18101 	ASSERT(xp != NULL);
18102 	ASSERT(pktp != NULL);
18103 
18104 	si.ssi_severity = SCSI_ERR_FATAL;
18105 	si.ssi_pfa_flag = FALSE;
18106 
18107 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
18108 
18109 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18110 	    &si, EIO, (clock_t)0, NULL);
18111 }
18112 
18113 
18114 /*
18115  *    Function: sd_sense_key_recoverable_error
18116  *
18117  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
18118  *
18119  *     Context: May be called from interrupt context
18120  */
18121 
18122 static void
18123 sd_sense_key_recoverable_error(struct sd_lun *un,
18124 	uint8_t *sense_datap,
18125 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18126 {
18127 	struct sd_sense_info	si;
18128 	uint8_t asc = scsi_sense_asc(sense_datap);
18129 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18130 
18131 	ASSERT(un != NULL);
18132 	ASSERT(mutex_owned(SD_MUTEX(un)));
18133 	ASSERT(bp != NULL);
18134 	ASSERT(xp != NULL);
18135 	ASSERT(pktp != NULL);
18136 
18137 	/*
18138 	 * 0x00, 0x1D: ATA PASSTHROUGH INFORMATION AVAILABLE
18139 	 */
18140 	if (asc == 0x00 && ascq == 0x1D) {
18141 		sd_return_command(un, bp);
18142 		return;
18143 	}
18144 
18145 	/*
18146 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
18147 	 */
18148 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
18149 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18150 		si.ssi_severity = SCSI_ERR_INFO;
18151 		si.ssi_pfa_flag = TRUE;
18152 	} else {
18153 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
18154 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
18155 		si.ssi_severity = SCSI_ERR_RECOVERED;
18156 		si.ssi_pfa_flag = FALSE;
18157 	}
18158 
18159 	if (pktp->pkt_resid == 0) {
18160 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18161 		sd_return_command(un, bp);
18162 		return;
18163 	}
18164 
18165 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18166 	    &si, EIO, (clock_t)0, NULL);
18167 }
18168 
18169 
18170 
18171 
18172 /*
18173  *    Function: sd_sense_key_not_ready
18174  *
18175  * Description: Recovery actions for a SCSI "Not Ready" sense key.
18176  *
18177  *     Context: May be called from interrupt context
18178  */
18179 
18180 static void
18181 sd_sense_key_not_ready(struct sd_lun *un,
18182 	uint8_t *sense_datap,
18183 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18184 {
18185 	struct sd_sense_info	si;
18186 	uint8_t asc = scsi_sense_asc(sense_datap);
18187 	uint8_t ascq = scsi_sense_ascq(sense_datap);
18188 
18189 	ASSERT(un != NULL);
18190 	ASSERT(mutex_owned(SD_MUTEX(un)));
18191 	ASSERT(bp != NULL);
18192 	ASSERT(xp != NULL);
18193 	ASSERT(pktp != NULL);
18194 
18195 	si.ssi_severity = SCSI_ERR_FATAL;
18196 	si.ssi_pfa_flag = FALSE;
18197 
18198 	/*
18199 	 * Update error stats after first NOT READY error. Disks may have
18200 	 * been powered down and may need to be restarted.  For CDROMs,
18201 	 * report NOT READY errors only if media is present.
18202 	 */
18203 	if ((ISCD(un) && (asc == 0x3A)) ||
18204 	    (xp->xb_nr_retry_count > 0)) {
18205 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18206 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
18207 	}
18208 
18209 	/*
18210 	 * Just fail if the "not ready" retry limit has been reached.
18211 	 */
18212 	if (xp->xb_nr_retry_count >= un->un_notready_retry_count) {
18213 		/* Special check for error message printing for removables. */
18214 		if (un->un_f_has_removable_media && (asc == 0x04) &&
18215 		    (ascq >= 0x04)) {
18216 			si.ssi_severity = SCSI_ERR_ALL;
18217 		}
18218 		goto fail_command;
18219 	}
18220 
18221 	/*
18222 	 * Check the ASC and ASCQ in the sense data as needed, to determine
18223 	 * what to do.
18224 	 */
18225 	switch (asc) {
18226 	case 0x04:	/* LOGICAL UNIT NOT READY */
18227 		/*
18228 		 * disk drives that don't spin up result in a very long delay
18229 		 * in format without warning messages. We will log a message
18230 		 * if the error level is set to verbose.
18231 		 */
18232 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18233 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18234 			    "logical unit not ready, resetting disk\n");
18235 		}
18236 
18237 		/*
18238 		 * There are different requirements for CDROMs and disks for
18239 		 * the number of retries.  If a CD-ROM is giving this, it is
18240 		 * probably reading TOC and is in the process of getting
18241 		 * ready, so we should keep on trying for a long time to make
18242 		 * sure that all types of media are taken in account (for
18243 		 * some media the drive takes a long time to read TOC).  For
18244 		 * disks we do not want to retry this too many times as this
18245 		 * can cause a long hang in format when the drive refuses to
18246 		 * spin up (a very common failure).
18247 		 */
18248 		switch (ascq) {
18249 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
18250 			/*
18251 			 * Disk drives frequently refuse to spin up which
18252 			 * results in a very long hang in format without
18253 			 * warning messages.
18254 			 *
18255 			 * Note: This code preserves the legacy behavior of
18256 			 * comparing xb_nr_retry_count against zero for fibre
18257 			 * channel targets instead of comparing against the
18258 			 * un_reset_retry_count value.  The reason for this
18259 			 * discrepancy has been so utterly lost beneath the
18260 			 * Sands of Time that even Indiana Jones could not
18261 			 * find it.
18262 			 */
18263 			if (un->un_f_is_fibre == TRUE) {
18264 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18265 				    (xp->xb_nr_retry_count > 0)) &&
18266 				    (un->un_startstop_timeid == NULL)) {
18267 					scsi_log(SD_DEVINFO(un), sd_label,
18268 					    CE_WARN, "logical unit not ready, "
18269 					    "resetting disk\n");
18270 					sd_reset_target(un, pktp);
18271 				}
18272 			} else {
18273 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
18274 				    (xp->xb_nr_retry_count >
18275 				    un->un_reset_retry_count)) &&
18276 				    (un->un_startstop_timeid == NULL)) {
18277 					scsi_log(SD_DEVINFO(un), sd_label,
18278 					    CE_WARN, "logical unit not ready, "
18279 					    "resetting disk\n");
18280 					sd_reset_target(un, pktp);
18281 				}
18282 			}
18283 			break;
18284 
18285 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
18286 			/*
18287 			 * If the target is in the process of becoming
18288 			 * ready, just proceed with the retry. This can
18289 			 * happen with CD-ROMs that take a long time to
18290 			 * read TOC after a power cycle or reset.
18291 			 */
18292 			goto do_retry;
18293 
18294 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
18295 			break;
18296 
18297 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
18298 			/*
18299 			 * Retries cannot help here so just fail right away.
18300 			 */
18301 			goto fail_command;
18302 
18303 		case 0x88:
18304 			/*
18305 			 * Vendor-unique code for T3/T4: it indicates a
18306 			 * path problem in a mutipathed config, but as far as
18307 			 * the target driver is concerned it equates to a fatal
18308 			 * error, so we should just fail the command right away
18309 			 * (without printing anything to the console). If this
18310 			 * is not a T3/T4, fall thru to the default recovery
18311 			 * action.
18312 			 * T3/T4 is FC only, don't need to check is_fibre
18313 			 */
18314 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
18315 				sd_return_failed_command(un, bp, EIO);
18316 				return;
18317 			}
18318 			/* FALLTHRU */
18319 
18320 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
18321 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
18322 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
18323 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
18324 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
18325 		default:    /* Possible future codes in SCSI spec? */
18326 			/*
18327 			 * For removable-media devices, do not retry if
18328 			 * ASCQ > 2 as these result mostly from USCSI commands
18329 			 * on MMC devices issued to check status of an
18330 			 * operation initiated in immediate mode.  Also for
18331 			 * ASCQ >= 4 do not print console messages as these
18332 			 * mainly represent a user-initiated operation
18333 			 * instead of a system failure.
18334 			 */
18335 			if (un->un_f_has_removable_media) {
18336 				si.ssi_severity = SCSI_ERR_ALL;
18337 				goto fail_command;
18338 			}
18339 			break;
18340 		}
18341 
18342 		/*
18343 		 * As part of our recovery attempt for the NOT READY
18344 		 * condition, we issue a START STOP UNIT command. However
18345 		 * we want to wait for a short delay before attempting this
18346 		 * as there may still be more commands coming back from the
18347 		 * target with the check condition. To do this we use
18348 		 * timeout(9F) to call sd_start_stop_unit_callback() after
18349 		 * the delay interval expires. (sd_start_stop_unit_callback()
18350 		 * dispatches sd_start_stop_unit_task(), which will issue
18351 		 * the actual START STOP UNIT command. The delay interval
18352 		 * is one-half of the delay that we will use to retry the
18353 		 * command that generated the NOT READY condition.
18354 		 *
18355 		 * Note that we could just dispatch sd_start_stop_unit_task()
18356 		 * from here and allow it to sleep for the delay interval,
18357 		 * but then we would be tying up the taskq thread
18358 		 * uncesessarily for the duration of the delay.
18359 		 *
18360 		 * Do not issue the START STOP UNIT if the current command
18361 		 * is already a START STOP UNIT.
18362 		 */
18363 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
18364 			break;
18365 		}
18366 
18367 		/*
18368 		 * Do not schedule the timeout if one is already pending.
18369 		 */
18370 		if (un->un_startstop_timeid != NULL) {
18371 			SD_INFO(SD_LOG_ERROR, un,
18372 			    "sd_sense_key_not_ready: restart already issued to"
18373 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
18374 			    ddi_get_instance(SD_DEVINFO(un)));
18375 			break;
18376 		}
18377 
18378 		/*
18379 		 * Schedule the START STOP UNIT command, then queue the command
18380 		 * for a retry.
18381 		 *
18382 		 * Note: A timeout is not scheduled for this retry because we
18383 		 * want the retry to be serial with the START_STOP_UNIT. The
18384 		 * retry will be started when the START_STOP_UNIT is completed
18385 		 * in sd_start_stop_unit_task.
18386 		 */
18387 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
18388 		    un, un->un_busy_timeout / 2);
18389 		xp->xb_nr_retry_count++;
18390 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
18391 		return;
18392 
18393 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
18394 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18395 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18396 			    "unit does not respond to selection\n");
18397 		}
18398 		break;
18399 
18400 	case 0x3A:	/* MEDIUM NOT PRESENT */
18401 		if (sd_error_level >= SCSI_ERR_FATAL) {
18402 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18403 			    "Caddy not inserted in drive\n");
18404 		}
18405 
18406 		sr_ejected(un);
18407 		un->un_mediastate = DKIO_EJECTED;
18408 		/* The state has changed, inform the media watch routines */
18409 		cv_broadcast(&un->un_state_cv);
18410 		/* Just fail if no media is present in the drive. */
18411 		goto fail_command;
18412 
18413 	default:
18414 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
18415 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
18416 			    "Unit not Ready. Additional sense code 0x%x\n",
18417 			    asc);
18418 		}
18419 		break;
18420 	}
18421 
18422 do_retry:
18423 
18424 	/*
18425 	 * Retry the command, as some targets may report NOT READY for
18426 	 * several seconds after being reset.
18427 	 */
18428 	xp->xb_nr_retry_count++;
18429 	si.ssi_severity = SCSI_ERR_RETRYABLE;
18430 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18431 	    &si, EIO, un->un_busy_timeout, NULL);
18432 
18433 	return;
18434 
18435 fail_command:
18436 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18437 	sd_return_failed_command(un, bp, EIO);
18438 }
18439 
18440 
18441 
18442 /*
18443  *    Function: sd_sense_key_medium_or_hardware_error
18444  *
18445  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
18446  *		sense key.
18447  *
18448  *     Context: May be called from interrupt context
18449  */
18450 
18451 static void
18452 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
18453 	uint8_t *sense_datap,
18454 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18455 {
18456 	struct sd_sense_info	si;
18457 	uint8_t sense_key = scsi_sense_key(sense_datap);
18458 	uint8_t asc = scsi_sense_asc(sense_datap);
18459 
18460 	ASSERT(un != NULL);
18461 	ASSERT(mutex_owned(SD_MUTEX(un)));
18462 	ASSERT(bp != NULL);
18463 	ASSERT(xp != NULL);
18464 	ASSERT(pktp != NULL);
18465 
18466 	si.ssi_severity = SCSI_ERR_FATAL;
18467 	si.ssi_pfa_flag = FALSE;
18468 
18469 	if (sense_key == KEY_MEDIUM_ERROR) {
18470 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
18471 	}
18472 
18473 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18474 
18475 	if ((un->un_reset_retry_count != 0) &&
18476 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
18477 		mutex_exit(SD_MUTEX(un));
18478 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
18479 		if (un->un_f_allow_bus_device_reset == TRUE) {
18480 
18481 			boolean_t try_resetting_target = B_TRUE;
18482 
18483 			/*
18484 			 * We need to be able to handle specific ASC when we are
18485 			 * handling a KEY_HARDWARE_ERROR. In particular
18486 			 * taking the default action of resetting the target may
18487 			 * not be the appropriate way to attempt recovery.
18488 			 * Resetting a target because of a single LUN failure
18489 			 * victimizes all LUNs on that target.
18490 			 *
18491 			 * This is true for the LSI arrays, if an LSI
18492 			 * array controller returns an ASC of 0x84 (LUN Dead) we
18493 			 * should trust it.
18494 			 */
18495 
18496 			if (sense_key == KEY_HARDWARE_ERROR) {
18497 				switch (asc) {
18498 				case 0x84:
18499 					if (SD_IS_LSI(un)) {
18500 						try_resetting_target = B_FALSE;
18501 					}
18502 					break;
18503 				default:
18504 					break;
18505 				}
18506 			}
18507 
18508 			if (try_resetting_target == B_TRUE) {
18509 				int reset_retval = 0;
18510 				if (un->un_f_lun_reset_enabled == TRUE) {
18511 					SD_TRACE(SD_LOG_IO_CORE, un,
18512 					    "sd_sense_key_medium_or_hardware_"
18513 					    "error: issuing RESET_LUN\n");
18514 					reset_retval =
18515 					    scsi_reset(SD_ADDRESS(un),
18516 					    RESET_LUN);
18517 				}
18518 				if (reset_retval == 0) {
18519 					SD_TRACE(SD_LOG_IO_CORE, un,
18520 					    "sd_sense_key_medium_or_hardware_"
18521 					    "error: issuing RESET_TARGET\n");
18522 					(void) scsi_reset(SD_ADDRESS(un),
18523 					    RESET_TARGET);
18524 				}
18525 			}
18526 		}
18527 		mutex_enter(SD_MUTEX(un));
18528 	}
18529 
18530 	/*
18531 	 * This really ought to be a fatal error, but we will retry anyway
18532 	 * as some drives report this as a spurious error.
18533 	 */
18534 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18535 	    &si, EIO, (clock_t)0, NULL);
18536 }
18537 
18538 
18539 
18540 /*
18541  *    Function: sd_sense_key_illegal_request
18542  *
18543  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
18544  *
18545  *     Context: May be called from interrupt context
18546  */
18547 
18548 static void
18549 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
18550 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18551 {
18552 	struct sd_sense_info	si;
18553 
18554 	ASSERT(un != NULL);
18555 	ASSERT(mutex_owned(SD_MUTEX(un)));
18556 	ASSERT(bp != NULL);
18557 	ASSERT(xp != NULL);
18558 	ASSERT(pktp != NULL);
18559 
18560 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
18561 
18562 	si.ssi_severity = SCSI_ERR_INFO;
18563 	si.ssi_pfa_flag = FALSE;
18564 
18565 	/* Pointless to retry if the target thinks it's an illegal request */
18566 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18567 	sd_return_failed_command(un, bp, EIO);
18568 }
18569 
18570 
18571 
18572 
18573 /*
18574  *    Function: sd_sense_key_unit_attention
18575  *
18576  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
18577  *
18578  *     Context: May be called from interrupt context
18579  */
18580 
18581 static void
18582 sd_sense_key_unit_attention(struct sd_lun *un,
18583 	uint8_t *sense_datap,
18584 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18585 {
18586 	/*
18587 	 * For UNIT ATTENTION we allow retries for one minute. Devices
18588 	 * like Sonoma can return UNIT ATTENTION close to a minute
18589 	 * under certain conditions.
18590 	 */
18591 	int	retry_check_flag = SD_RETRIES_UA;
18592 	boolean_t	kstat_updated = B_FALSE;
18593 	struct	sd_sense_info		si;
18594 	uint8_t asc = scsi_sense_asc(sense_datap);
18595 	uint8_t	ascq = scsi_sense_ascq(sense_datap);
18596 
18597 	ASSERT(un != NULL);
18598 	ASSERT(mutex_owned(SD_MUTEX(un)));
18599 	ASSERT(bp != NULL);
18600 	ASSERT(xp != NULL);
18601 	ASSERT(pktp != NULL);
18602 
18603 	si.ssi_severity = SCSI_ERR_INFO;
18604 	si.ssi_pfa_flag = FALSE;
18605 
18606 
18607 	switch (asc) {
18608 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
18609 		if (sd_report_pfa != 0) {
18610 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
18611 			si.ssi_pfa_flag = TRUE;
18612 			retry_check_flag = SD_RETRIES_STANDARD;
18613 			goto do_retry;
18614 		}
18615 
18616 		break;
18617 
18618 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
18619 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
18620 			un->un_resvd_status |=
18621 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
18622 		}
18623 #ifdef _LP64
18624 		if (un->un_blockcount + 1 > SD_GROUP1_MAX_ADDRESS) {
18625 			if (taskq_dispatch(sd_tq, sd_reenable_dsense_task,
18626 			    un, KM_NOSLEEP) == 0) {
18627 				/*
18628 				 * If we can't dispatch the task we'll just
18629 				 * live without descriptor sense.  We can
18630 				 * try again on the next "unit attention"
18631 				 */
18632 				SD_ERROR(SD_LOG_ERROR, un,
18633 				    "sd_sense_key_unit_attention: "
18634 				    "Could not dispatch "
18635 				    "sd_reenable_dsense_task\n");
18636 			}
18637 		}
18638 #endif /* _LP64 */
18639 		/* FALLTHRU */
18640 
18641 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
18642 		if (!un->un_f_has_removable_media) {
18643 			break;
18644 		}
18645 
18646 		/*
18647 		 * When we get a unit attention from a removable-media device,
18648 		 * it may be in a state that will take a long time to recover
18649 		 * (e.g., from a reset).  Since we are executing in interrupt
18650 		 * context here, we cannot wait around for the device to come
18651 		 * back. So hand this command off to sd_media_change_task()
18652 		 * for deferred processing under taskq thread context. (Note
18653 		 * that the command still may be failed if a problem is
18654 		 * encountered at a later time.)
18655 		 */
18656 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
18657 		    KM_NOSLEEP) == 0) {
18658 			/*
18659 			 * Cannot dispatch the request so fail the command.
18660 			 */
18661 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
18662 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18663 			si.ssi_severity = SCSI_ERR_FATAL;
18664 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18665 			sd_return_failed_command(un, bp, EIO);
18666 		}
18667 
18668 		/*
18669 		 * If failed to dispatch sd_media_change_task(), we already
18670 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
18671 		 * we should update kstat later if it encounters an error. So,
18672 		 * we update kstat_updated flag here.
18673 		 */
18674 		kstat_updated = B_TRUE;
18675 
18676 		/*
18677 		 * Either the command has been successfully dispatched to a
18678 		 * task Q for retrying, or the dispatch failed. In either case
18679 		 * do NOT retry again by calling sd_retry_command. This sets up
18680 		 * two retries of the same command and when one completes and
18681 		 * frees the resources the other will access freed memory,
18682 		 * a bad thing.
18683 		 */
18684 		return;
18685 
18686 	default:
18687 		break;
18688 	}
18689 
18690 	/*
18691 	 * ASC  ASCQ
18692 	 *  2A   09	Capacity data has changed
18693 	 *  2A   01	Mode parameters changed
18694 	 *  3F   0E	Reported luns data has changed
18695 	 * Arrays that support logical unit expansion should report
18696 	 * capacity changes(2Ah/09). Mode parameters changed and
18697 	 * reported luns data has changed are the approximation.
18698 	 */
18699 	if (((asc == 0x2a) && (ascq == 0x09)) ||
18700 	    ((asc == 0x2a) && (ascq == 0x01)) ||
18701 	    ((asc == 0x3f) && (ascq == 0x0e))) {
18702 		if (taskq_dispatch(sd_tq, sd_target_change_task, un,
18703 		    KM_NOSLEEP) == 0) {
18704 			SD_ERROR(SD_LOG_ERROR, un,
18705 			    "sd_sense_key_unit_attention: "
18706 			    "Could not dispatch sd_target_change_task\n");
18707 		}
18708 	}
18709 
18710 	/*
18711 	 * Update kstat if we haven't done that.
18712 	 */
18713 	if (!kstat_updated) {
18714 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18715 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18716 	}
18717 
18718 do_retry:
18719 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
18720 	    EIO, SD_UA_RETRY_DELAY, NULL);
18721 }
18722 
18723 
18724 
18725 /*
18726  *    Function: sd_sense_key_fail_command
18727  *
18728  * Description: Use to fail a command when we don't like the sense key that
18729  *		was returned.
18730  *
18731  *     Context: May be called from interrupt context
18732  */
18733 
18734 static void
18735 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
18736 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18737 {
18738 	struct sd_sense_info	si;
18739 
18740 	ASSERT(un != NULL);
18741 	ASSERT(mutex_owned(SD_MUTEX(un)));
18742 	ASSERT(bp != NULL);
18743 	ASSERT(xp != NULL);
18744 	ASSERT(pktp != NULL);
18745 
18746 	si.ssi_severity = SCSI_ERR_FATAL;
18747 	si.ssi_pfa_flag = FALSE;
18748 
18749 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18750 	sd_return_failed_command(un, bp, EIO);
18751 }
18752 
18753 
18754 
18755 /*
18756  *    Function: sd_sense_key_blank_check
18757  *
18758  * Description: Recovery actions for a SCSI "Blank Check" sense key.
18759  *		Has no monetary connotation.
18760  *
18761  *     Context: May be called from interrupt context
18762  */
18763 
18764 static void
18765 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
18766 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18767 {
18768 	struct sd_sense_info	si;
18769 
18770 	ASSERT(un != NULL);
18771 	ASSERT(mutex_owned(SD_MUTEX(un)));
18772 	ASSERT(bp != NULL);
18773 	ASSERT(xp != NULL);
18774 	ASSERT(pktp != NULL);
18775 
18776 	/*
18777 	 * Blank check is not fatal for removable devices, therefore
18778 	 * it does not require a console message.
18779 	 */
18780 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
18781 	    SCSI_ERR_FATAL;
18782 	si.ssi_pfa_flag = FALSE;
18783 
18784 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18785 	sd_return_failed_command(un, bp, EIO);
18786 }
18787 
18788 
18789 
18790 
18791 /*
18792  *    Function: sd_sense_key_aborted_command
18793  *
18794  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18795  *
18796  *     Context: May be called from interrupt context
18797  */
18798 
18799 static void
18800 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18801 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18802 {
18803 	struct sd_sense_info	si;
18804 
18805 	ASSERT(un != NULL);
18806 	ASSERT(mutex_owned(SD_MUTEX(un)));
18807 	ASSERT(bp != NULL);
18808 	ASSERT(xp != NULL);
18809 	ASSERT(pktp != NULL);
18810 
18811 	si.ssi_severity = SCSI_ERR_FATAL;
18812 	si.ssi_pfa_flag = FALSE;
18813 
18814 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18815 
18816 	/*
18817 	 * This really ought to be a fatal error, but we will retry anyway
18818 	 * as some drives report this as a spurious error.
18819 	 */
18820 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18821 	    &si, EIO, drv_usectohz(100000), NULL);
18822 }
18823 
18824 
18825 
18826 /*
18827  *    Function: sd_sense_key_default
18828  *
18829  * Description: Default recovery action for several SCSI sense keys (basically
18830  *		attempts a retry).
18831  *
18832  *     Context: May be called from interrupt context
18833  */
18834 
18835 static void
18836 sd_sense_key_default(struct sd_lun *un,
18837 	uint8_t *sense_datap,
18838 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18839 {
18840 	struct sd_sense_info	si;
18841 	uint8_t sense_key = scsi_sense_key(sense_datap);
18842 
18843 	ASSERT(un != NULL);
18844 	ASSERT(mutex_owned(SD_MUTEX(un)));
18845 	ASSERT(bp != NULL);
18846 	ASSERT(xp != NULL);
18847 	ASSERT(pktp != NULL);
18848 
18849 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18850 
18851 	/*
18852 	 * Undecoded sense key.	Attempt retries and hope that will fix
18853 	 * the problem.  Otherwise, we're dead.
18854 	 */
18855 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18856 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18857 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18858 	}
18859 
18860 	si.ssi_severity = SCSI_ERR_FATAL;
18861 	si.ssi_pfa_flag = FALSE;
18862 
18863 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18864 	    &si, EIO, (clock_t)0, NULL);
18865 }
18866 
18867 
18868 
18869 /*
18870  *    Function: sd_print_retry_msg
18871  *
18872  * Description: Print a message indicating the retry action being taken.
18873  *
18874  *   Arguments: un - ptr to associated softstate
18875  *		bp - ptr to buf(9S) for the command
18876  *		arg - not used.
18877  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18878  *			or SD_NO_RETRY_ISSUED
18879  *
18880  *     Context: May be called from interrupt context
18881  */
18882 /* ARGSUSED */
18883 static void
18884 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18885 {
18886 	struct sd_xbuf	*xp;
18887 	struct scsi_pkt *pktp;
18888 	char *reasonp;
18889 	char *msgp;
18890 
18891 	ASSERT(un != NULL);
18892 	ASSERT(mutex_owned(SD_MUTEX(un)));
18893 	ASSERT(bp != NULL);
18894 	pktp = SD_GET_PKTP(bp);
18895 	ASSERT(pktp != NULL);
18896 	xp = SD_GET_XBUF(bp);
18897 	ASSERT(xp != NULL);
18898 
18899 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18900 	mutex_enter(&un->un_pm_mutex);
18901 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18902 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18903 	    (pktp->pkt_flags & FLAG_SILENT)) {
18904 		mutex_exit(&un->un_pm_mutex);
18905 		goto update_pkt_reason;
18906 	}
18907 	mutex_exit(&un->un_pm_mutex);
18908 
18909 	/*
18910 	 * Suppress messages if they are all the same pkt_reason; with
18911 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18912 	 * If we are in panic, then suppress the retry messages.
18913 	 */
18914 	switch (flag) {
18915 	case SD_NO_RETRY_ISSUED:
18916 		msgp = "giving up";
18917 		break;
18918 	case SD_IMMEDIATE_RETRY_ISSUED:
18919 	case SD_DELAYED_RETRY_ISSUED:
18920 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18921 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18922 		    (sd_error_level != SCSI_ERR_ALL))) {
18923 			return;
18924 		}
18925 		msgp = "retrying command";
18926 		break;
18927 	default:
18928 		goto update_pkt_reason;
18929 	}
18930 
18931 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18932 	    scsi_rname(pktp->pkt_reason));
18933 
18934 	if (SD_FM_LOG(un) == SD_FM_LOG_NSUP) {
18935 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18936 		    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18937 	}
18938 
18939 update_pkt_reason:
18940 	/*
18941 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18942 	 * This is to prevent multiple console messages for the same failure
18943 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18944 	 * when the command is retried successfully because there still may be
18945 	 * more commands coming back with the same value of pktp->pkt_reason.
18946 	 */
18947 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18948 		un->un_last_pkt_reason = pktp->pkt_reason;
18949 	}
18950 }
18951 
18952 
18953 /*
18954  *    Function: sd_print_cmd_incomplete_msg
18955  *
18956  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18957  *
18958  *   Arguments: un - ptr to associated softstate
18959  *		bp - ptr to buf(9S) for the command
18960  *		arg - passed to sd_print_retry_msg()
18961  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18962  *			or SD_NO_RETRY_ISSUED
18963  *
18964  *     Context: May be called from interrupt context
18965  */
18966 
18967 static void
18968 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18969 	int code)
18970 {
18971 	dev_info_t	*dip;
18972 
18973 	ASSERT(un != NULL);
18974 	ASSERT(mutex_owned(SD_MUTEX(un)));
18975 	ASSERT(bp != NULL);
18976 
18977 	switch (code) {
18978 	case SD_NO_RETRY_ISSUED:
18979 		/* Command was failed. Someone turned off this target? */
18980 		if (un->un_state != SD_STATE_OFFLINE) {
18981 			/*
18982 			 * Suppress message if we are detaching and
18983 			 * device has been disconnected
18984 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18985 			 * private interface and not part of the DDI
18986 			 */
18987 			dip = un->un_sd->sd_dev;
18988 			if (!(DEVI_IS_DETACHING(dip) &&
18989 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18990 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18991 				"disk not responding to selection\n");
18992 			}
18993 			New_state(un, SD_STATE_OFFLINE);
18994 		}
18995 		break;
18996 
18997 	case SD_DELAYED_RETRY_ISSUED:
18998 	case SD_IMMEDIATE_RETRY_ISSUED:
18999 	default:
19000 		/* Command was successfully queued for retry */
19001 		sd_print_retry_msg(un, bp, arg, code);
19002 		break;
19003 	}
19004 }
19005 
19006 
19007 /*
19008  *    Function: sd_pkt_reason_cmd_incomplete
19009  *
19010  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
19011  *
19012  *     Context: May be called from interrupt context
19013  */
19014 
19015 static void
19016 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
19017 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19018 {
19019 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
19020 
19021 	ASSERT(un != NULL);
19022 	ASSERT(mutex_owned(SD_MUTEX(un)));
19023 	ASSERT(bp != NULL);
19024 	ASSERT(xp != NULL);
19025 	ASSERT(pktp != NULL);
19026 
19027 	/* Do not do a reset if selection did not complete */
19028 	/* Note: Should this not just check the bit? */
19029 	if (pktp->pkt_state != STATE_GOT_BUS) {
19030 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19031 		sd_reset_target(un, pktp);
19032 	}
19033 
19034 	/*
19035 	 * If the target was not successfully selected, then set
19036 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
19037 	 * with the target, and further retries and/or commands are
19038 	 * likely to take a long time.
19039 	 */
19040 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
19041 		flag |= SD_RETRIES_FAILFAST;
19042 	}
19043 
19044 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19045 
19046 	sd_retry_command(un, bp, flag,
19047 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19048 }
19049 
19050 
19051 
19052 /*
19053  *    Function: sd_pkt_reason_cmd_tran_err
19054  *
19055  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
19056  *
19057  *     Context: May be called from interrupt context
19058  */
19059 
19060 static void
19061 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
19062 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19063 {
19064 	ASSERT(un != NULL);
19065 	ASSERT(mutex_owned(SD_MUTEX(un)));
19066 	ASSERT(bp != NULL);
19067 	ASSERT(xp != NULL);
19068 	ASSERT(pktp != NULL);
19069 
19070 	/*
19071 	 * Do not reset if we got a parity error, or if
19072 	 * selection did not complete.
19073 	 */
19074 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19075 	/* Note: Should this not just check the bit for pkt_state? */
19076 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
19077 	    (pktp->pkt_state != STATE_GOT_BUS)) {
19078 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
19079 		sd_reset_target(un, pktp);
19080 	}
19081 
19082 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19083 
19084 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19085 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19086 }
19087 
19088 
19089 
19090 /*
19091  *    Function: sd_pkt_reason_cmd_reset
19092  *
19093  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
19094  *
19095  *     Context: May be called from interrupt context
19096  */
19097 
19098 static void
19099 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
19100 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19101 {
19102 	ASSERT(un != NULL);
19103 	ASSERT(mutex_owned(SD_MUTEX(un)));
19104 	ASSERT(bp != NULL);
19105 	ASSERT(xp != NULL);
19106 	ASSERT(pktp != NULL);
19107 
19108 	/* The target may still be running the command, so try to reset. */
19109 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19110 	sd_reset_target(un, pktp);
19111 
19112 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19113 
19114 	/*
19115 	 * If pkt_reason is CMD_RESET chances are that this pkt got
19116 	 * reset because another target on this bus caused it. The target
19117 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19118 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19119 	 */
19120 
19121 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19122 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19123 }
19124 
19125 
19126 
19127 
19128 /*
19129  *    Function: sd_pkt_reason_cmd_aborted
19130  *
19131  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
19132  *
19133  *     Context: May be called from interrupt context
19134  */
19135 
19136 static void
19137 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
19138 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19139 {
19140 	ASSERT(un != NULL);
19141 	ASSERT(mutex_owned(SD_MUTEX(un)));
19142 	ASSERT(bp != NULL);
19143 	ASSERT(xp != NULL);
19144 	ASSERT(pktp != NULL);
19145 
19146 	/* The target may still be running the command, so try to reset. */
19147 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19148 	sd_reset_target(un, pktp);
19149 
19150 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19151 
19152 	/*
19153 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
19154 	 * aborted because another target on this bus caused it. The target
19155 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
19156 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
19157 	 */
19158 
19159 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
19160 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19161 }
19162 
19163 
19164 
19165 /*
19166  *    Function: sd_pkt_reason_cmd_timeout
19167  *
19168  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
19169  *
19170  *     Context: May be called from interrupt context
19171  */
19172 
19173 static void
19174 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
19175 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19176 {
19177 	ASSERT(un != NULL);
19178 	ASSERT(mutex_owned(SD_MUTEX(un)));
19179 	ASSERT(bp != NULL);
19180 	ASSERT(xp != NULL);
19181 	ASSERT(pktp != NULL);
19182 
19183 
19184 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19185 	sd_reset_target(un, pktp);
19186 
19187 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19188 
19189 	/*
19190 	 * A command timeout indicates that we could not establish
19191 	 * communication with the target, so set SD_RETRIES_FAILFAST
19192 	 * as further retries/commands are likely to take a long time.
19193 	 */
19194 	sd_retry_command(un, bp,
19195 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
19196 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19197 }
19198 
19199 
19200 
19201 /*
19202  *    Function: sd_pkt_reason_cmd_unx_bus_free
19203  *
19204  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
19205  *
19206  *     Context: May be called from interrupt context
19207  */
19208 
19209 static void
19210 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
19211 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19212 {
19213 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
19214 
19215 	ASSERT(un != NULL);
19216 	ASSERT(mutex_owned(SD_MUTEX(un)));
19217 	ASSERT(bp != NULL);
19218 	ASSERT(xp != NULL);
19219 	ASSERT(pktp != NULL);
19220 
19221 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19222 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19223 
19224 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
19225 	    sd_print_retry_msg : NULL;
19226 
19227 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19228 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19229 }
19230 
19231 
19232 /*
19233  *    Function: sd_pkt_reason_cmd_tag_reject
19234  *
19235  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
19236  *
19237  *     Context: May be called from interrupt context
19238  */
19239 
19240 static void
19241 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
19242 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19243 {
19244 	ASSERT(un != NULL);
19245 	ASSERT(mutex_owned(SD_MUTEX(un)));
19246 	ASSERT(bp != NULL);
19247 	ASSERT(xp != NULL);
19248 	ASSERT(pktp != NULL);
19249 
19250 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
19251 	pktp->pkt_flags = 0;
19252 	un->un_tagflags = 0;
19253 	if (un->un_f_opt_queueing == TRUE) {
19254 		un->un_throttle = min(un->un_throttle, 3);
19255 	} else {
19256 		un->un_throttle = 1;
19257 	}
19258 	mutex_exit(SD_MUTEX(un));
19259 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
19260 	mutex_enter(SD_MUTEX(un));
19261 
19262 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19263 
19264 	/* Legacy behavior not to check retry counts here. */
19265 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
19266 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19267 }
19268 
19269 
19270 /*
19271  *    Function: sd_pkt_reason_default
19272  *
19273  * Description: Default recovery actions for SCSA pkt_reason values that
19274  *		do not have more explicit recovery actions.
19275  *
19276  *     Context: May be called from interrupt context
19277  */
19278 
19279 static void
19280 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
19281 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19282 {
19283 	ASSERT(un != NULL);
19284 	ASSERT(mutex_owned(SD_MUTEX(un)));
19285 	ASSERT(bp != NULL);
19286 	ASSERT(xp != NULL);
19287 	ASSERT(pktp != NULL);
19288 
19289 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
19290 	sd_reset_target(un, pktp);
19291 
19292 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
19293 
19294 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
19295 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
19296 }
19297 
19298 
19299 
19300 /*
19301  *    Function: sd_pkt_status_check_condition
19302  *
19303  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
19304  *
19305  *     Context: May be called from interrupt context
19306  */
19307 
19308 static void
19309 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
19310 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19311 {
19312 	ASSERT(un != NULL);
19313 	ASSERT(mutex_owned(SD_MUTEX(un)));
19314 	ASSERT(bp != NULL);
19315 	ASSERT(xp != NULL);
19316 	ASSERT(pktp != NULL);
19317 
19318 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
19319 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
19320 
19321 	/*
19322 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
19323 	 * command will be retried after the request sense). Otherwise, retry
19324 	 * the command. Note: we are issuing the request sense even though the
19325 	 * retry limit may have been reached for the failed command.
19326 	 */
19327 	if (un->un_f_arq_enabled == FALSE) {
19328 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19329 		    "no ARQ, sending request sense command\n");
19330 		sd_send_request_sense_command(un, bp, pktp);
19331 	} else {
19332 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
19333 		    "ARQ,retrying request sense command\n");
19334 #if defined(__i386) || defined(__amd64)
19335 		/*
19336 		 * The SD_RETRY_DELAY value need to be adjusted here
19337 		 * when SD_RETRY_DELAY change in sddef.h
19338 		 */
19339 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19340 		    un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
19341 		    NULL);
19342 #else
19343 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
19344 		    EIO, SD_RETRY_DELAY, NULL);
19345 #endif
19346 	}
19347 
19348 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
19349 }
19350 
19351 
19352 /*
19353  *    Function: sd_pkt_status_busy
19354  *
19355  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
19356  *
19357  *     Context: May be called from interrupt context
19358  */
19359 
19360 static void
19361 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
19362 	struct scsi_pkt *pktp)
19363 {
19364 	ASSERT(un != NULL);
19365 	ASSERT(mutex_owned(SD_MUTEX(un)));
19366 	ASSERT(bp != NULL);
19367 	ASSERT(xp != NULL);
19368 	ASSERT(pktp != NULL);
19369 
19370 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19371 	    "sd_pkt_status_busy: entry\n");
19372 
19373 	/* If retries are exhausted, just fail the command. */
19374 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
19375 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19376 		    "device busy too long\n");
19377 		sd_return_failed_command(un, bp, EIO);
19378 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19379 		    "sd_pkt_status_busy: exit\n");
19380 		return;
19381 	}
19382 	xp->xb_retry_count++;
19383 
19384 	/*
19385 	 * Try to reset the target. However, we do not want to perform
19386 	 * more than one reset if the device continues to fail. The reset
19387 	 * will be performed when the retry count reaches the reset
19388 	 * threshold.  This threshold should be set such that at least
19389 	 * one retry is issued before the reset is performed.
19390 	 */
19391 	if (xp->xb_retry_count ==
19392 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
19393 		int rval = 0;
19394 		mutex_exit(SD_MUTEX(un));
19395 		if (un->un_f_allow_bus_device_reset == TRUE) {
19396 			/*
19397 			 * First try to reset the LUN; if we cannot then
19398 			 * try to reset the target.
19399 			 */
19400 			if (un->un_f_lun_reset_enabled == TRUE) {
19401 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19402 				    "sd_pkt_status_busy: RESET_LUN\n");
19403 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19404 			}
19405 			if (rval == 0) {
19406 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19407 				    "sd_pkt_status_busy: RESET_TARGET\n");
19408 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19409 			}
19410 		}
19411 		if (rval == 0) {
19412 			/*
19413 			 * If the RESET_LUN and/or RESET_TARGET failed,
19414 			 * try RESET_ALL
19415 			 */
19416 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19417 			    "sd_pkt_status_busy: RESET_ALL\n");
19418 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
19419 		}
19420 		mutex_enter(SD_MUTEX(un));
19421 		if (rval == 0) {
19422 			/*
19423 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
19424 			 * At this point we give up & fail the command.
19425 			 */
19426 			sd_return_failed_command(un, bp, EIO);
19427 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19428 			    "sd_pkt_status_busy: exit (failed cmd)\n");
19429 			return;
19430 		}
19431 	}
19432 
19433 	/*
19434 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
19435 	 * we have already checked the retry counts above.
19436 	 */
19437 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
19438 	    EIO, un->un_busy_timeout, NULL);
19439 
19440 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19441 	    "sd_pkt_status_busy: exit\n");
19442 }
19443 
19444 
19445 /*
19446  *    Function: sd_pkt_status_reservation_conflict
19447  *
19448  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
19449  *		command status.
19450  *
19451  *     Context: May be called from interrupt context
19452  */
19453 
19454 static void
19455 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
19456 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19457 {
19458 	ASSERT(un != NULL);
19459 	ASSERT(mutex_owned(SD_MUTEX(un)));
19460 	ASSERT(bp != NULL);
19461 	ASSERT(xp != NULL);
19462 	ASSERT(pktp != NULL);
19463 
19464 	/*
19465 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
19466 	 * conflict could be due to various reasons like incorrect keys, not
19467 	 * registered or not reserved etc. So, we return EACCES to the caller.
19468 	 */
19469 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
19470 		int cmd = SD_GET_PKT_OPCODE(pktp);
19471 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
19472 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
19473 			sd_return_failed_command(un, bp, EACCES);
19474 			return;
19475 		}
19476 	}
19477 
19478 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
19479 
19480 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
19481 		if (sd_failfast_enable != 0) {
19482 			/* By definition, we must panic here.... */
19483 			sd_panic_for_res_conflict(un);
19484 			/*NOTREACHED*/
19485 		}
19486 		SD_ERROR(SD_LOG_IO, un,
19487 		    "sd_handle_resv_conflict: Disk Reserved\n");
19488 		sd_return_failed_command(un, bp, EACCES);
19489 		return;
19490 	}
19491 
19492 	/*
19493 	 * 1147670: retry only if sd_retry_on_reservation_conflict
19494 	 * property is set (default is 1). Retries will not succeed
19495 	 * on a disk reserved by another initiator. HA systems
19496 	 * may reset this via sd.conf to avoid these retries.
19497 	 *
19498 	 * Note: The legacy return code for this failure is EIO, however EACCES
19499 	 * seems more appropriate for a reservation conflict.
19500 	 */
19501 	if (sd_retry_on_reservation_conflict == 0) {
19502 		SD_ERROR(SD_LOG_IO, un,
19503 		    "sd_handle_resv_conflict: Device Reserved\n");
19504 		sd_return_failed_command(un, bp, EIO);
19505 		return;
19506 	}
19507 
19508 	/*
19509 	 * Retry the command if we can.
19510 	 *
19511 	 * Note: The legacy return code for this failure is EIO, however EACCES
19512 	 * seems more appropriate for a reservation conflict.
19513 	 */
19514 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
19515 	    (clock_t)2, NULL);
19516 }
19517 
19518 
19519 
19520 /*
19521  *    Function: sd_pkt_status_qfull
19522  *
19523  * Description: Handle a QUEUE FULL condition from the target.  This can
19524  *		occur if the HBA does not handle the queue full condition.
19525  *		(Basically this means third-party HBAs as Sun HBAs will
19526  *		handle the queue full condition.)  Note that if there are
19527  *		some commands already in the transport, then the queue full
19528  *		has occurred because the queue for this nexus is actually
19529  *		full. If there are no commands in the transport, then the
19530  *		queue full is resulting from some other initiator or lun
19531  *		consuming all the resources at the target.
19532  *
19533  *     Context: May be called from interrupt context
19534  */
19535 
19536 static void
19537 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
19538 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
19539 {
19540 	ASSERT(un != NULL);
19541 	ASSERT(mutex_owned(SD_MUTEX(un)));
19542 	ASSERT(bp != NULL);
19543 	ASSERT(xp != NULL);
19544 	ASSERT(pktp != NULL);
19545 
19546 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19547 	    "sd_pkt_status_qfull: entry\n");
19548 
19549 	/*
19550 	 * Just lower the QFULL throttle and retry the command.  Note that
19551 	 * we do not limit the number of retries here.
19552 	 */
19553 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
19554 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
19555 	    SD_RESTART_TIMEOUT, NULL);
19556 
19557 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19558 	    "sd_pkt_status_qfull: exit\n");
19559 }
19560 
19561 
19562 /*
19563  *    Function: sd_reset_target
19564  *
19565  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
19566  *		RESET_TARGET, or RESET_ALL.
19567  *
19568  *     Context: May be called under interrupt context.
19569  */
19570 
19571 static void
19572 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
19573 {
19574 	int rval = 0;
19575 
19576 	ASSERT(un != NULL);
19577 	ASSERT(mutex_owned(SD_MUTEX(un)));
19578 	ASSERT(pktp != NULL);
19579 
19580 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
19581 
19582 	/*
19583 	 * No need to reset if the transport layer has already done so.
19584 	 */
19585 	if ((pktp->pkt_statistics &
19586 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
19587 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19588 		    "sd_reset_target: no reset\n");
19589 		return;
19590 	}
19591 
19592 	mutex_exit(SD_MUTEX(un));
19593 
19594 	if (un->un_f_allow_bus_device_reset == TRUE) {
19595 		if (un->un_f_lun_reset_enabled == TRUE) {
19596 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19597 			    "sd_reset_target: RESET_LUN\n");
19598 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
19599 		}
19600 		if (rval == 0) {
19601 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19602 			    "sd_reset_target: RESET_TARGET\n");
19603 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
19604 		}
19605 	}
19606 
19607 	if (rval == 0) {
19608 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19609 		    "sd_reset_target: RESET_ALL\n");
19610 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
19611 	}
19612 
19613 	mutex_enter(SD_MUTEX(un));
19614 
19615 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
19616 }
19617 
19618 /*
19619  *    Function: sd_target_change_task
19620  *
19621  * Description: Handle dynamic target change
19622  *
19623  *     Context: Executes in a taskq() thread context
19624  */
19625 static void
19626 sd_target_change_task(void *arg)
19627 {
19628 	struct sd_lun		*un = arg;
19629 	uint64_t		capacity;
19630 	diskaddr_t		label_cap;
19631 	uint_t			lbasize;
19632 	sd_ssc_t		*ssc;
19633 
19634 	ASSERT(un != NULL);
19635 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19636 
19637 	if ((un->un_f_blockcount_is_valid == FALSE) ||
19638 	    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
19639 		return;
19640 	}
19641 
19642 	ssc = sd_ssc_init(un);
19643 
19644 	if (sd_send_scsi_READ_CAPACITY(ssc, &capacity,
19645 	    &lbasize, SD_PATH_DIRECT) != 0) {
19646 		SD_ERROR(SD_LOG_ERROR, un,
19647 		    "sd_target_change_task: fail to read capacity\n");
19648 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19649 		goto task_exit;
19650 	}
19651 
19652 	mutex_enter(SD_MUTEX(un));
19653 	if (capacity <= un->un_blockcount) {
19654 		mutex_exit(SD_MUTEX(un));
19655 		goto task_exit;
19656 	}
19657 
19658 	sd_update_block_info(un, lbasize, capacity);
19659 	mutex_exit(SD_MUTEX(un));
19660 
19661 	/*
19662 	 * If lun is EFI labeled and lun capacity is greater than the
19663 	 * capacity contained in the label, log a sys event.
19664 	 */
19665 	if (cmlb_efi_label_capacity(un->un_cmlbhandle, &label_cap,
19666 	    (void*)SD_PATH_DIRECT) == 0) {
19667 		mutex_enter(SD_MUTEX(un));
19668 		if (un->un_f_blockcount_is_valid &&
19669 		    un->un_blockcount > label_cap) {
19670 			mutex_exit(SD_MUTEX(un));
19671 			sd_log_lun_expansion_event(un, KM_SLEEP);
19672 		} else {
19673 			mutex_exit(SD_MUTEX(un));
19674 		}
19675 	}
19676 
19677 task_exit:
19678 	sd_ssc_fini(ssc);
19679 }
19680 
19681 
19682 /*
19683  *    Function: sd_log_dev_status_event
19684  *
19685  * Description: Log EC_dev_status sysevent
19686  *
19687  *     Context: Never called from interrupt context
19688  */
19689 static void
19690 sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag)
19691 {
19692 	int err;
19693 	char			*path;
19694 	nvlist_t		*attr_list;
19695 
19696 	/* Allocate and build sysevent attribute list */
19697 	err = nvlist_alloc(&attr_list, NV_UNIQUE_NAME_TYPE, km_flag);
19698 	if (err != 0) {
19699 		SD_ERROR(SD_LOG_ERROR, un,
19700 		    "sd_log_dev_status_event: fail to allocate space\n");
19701 		return;
19702 	}
19703 
19704 	path = kmem_alloc(MAXPATHLEN, km_flag);
19705 	if (path == NULL) {
19706 		nvlist_free(attr_list);
19707 		SD_ERROR(SD_LOG_ERROR, un,
19708 		    "sd_log_dev_status_event: fail to allocate space\n");
19709 		return;
19710 	}
19711 	/*
19712 	 * Add path attribute to identify the lun.
19713 	 * We are using minor node 'a' as the sysevent attribute.
19714 	 */
19715 	(void) snprintf(path, MAXPATHLEN, "/devices");
19716 	(void) ddi_pathname(SD_DEVINFO(un), path + strlen(path));
19717 	(void) snprintf(path + strlen(path), MAXPATHLEN - strlen(path),
19718 	    ":a");
19719 
19720 	err = nvlist_add_string(attr_list, DEV_PHYS_PATH, path);
19721 	if (err != 0) {
19722 		nvlist_free(attr_list);
19723 		kmem_free(path, MAXPATHLEN);
19724 		SD_ERROR(SD_LOG_ERROR, un,
19725 		    "sd_log_dev_status_event: fail to add attribute\n");
19726 		return;
19727 	}
19728 
19729 	/* Log dynamic lun expansion sysevent */
19730 	err = ddi_log_sysevent(SD_DEVINFO(un), SUNW_VENDOR, EC_DEV_STATUS,
19731 	    esc, attr_list, NULL, km_flag);
19732 	if (err != DDI_SUCCESS) {
19733 		SD_ERROR(SD_LOG_ERROR, un,
19734 		    "sd_log_dev_status_event: fail to log sysevent\n");
19735 	}
19736 
19737 	nvlist_free(attr_list);
19738 	kmem_free(path, MAXPATHLEN);
19739 }
19740 
19741 
19742 /*
19743  *    Function: sd_log_lun_expansion_event
19744  *
19745  * Description: Log lun expansion sys event
19746  *
19747  *     Context: Never called from interrupt context
19748  */
19749 static void
19750 sd_log_lun_expansion_event(struct sd_lun *un, int km_flag)
19751 {
19752 	sd_log_dev_status_event(un, ESC_DEV_DLE, km_flag);
19753 }
19754 
19755 
19756 /*
19757  *    Function: sd_log_eject_request_event
19758  *
19759  * Description: Log eject request sysevent
19760  *
19761  *     Context: Never called from interrupt context
19762  */
19763 static void
19764 sd_log_eject_request_event(struct sd_lun *un, int km_flag)
19765 {
19766 	sd_log_dev_status_event(un, ESC_DEV_EJECT_REQUEST, km_flag);
19767 }
19768 
19769 
19770 /*
19771  *    Function: sd_media_change_task
19772  *
19773  * Description: Recovery action for CDROM to become available.
19774  *
19775  *     Context: Executes in a taskq() thread context
19776  */
19777 
19778 static void
19779 sd_media_change_task(void *arg)
19780 {
19781 	struct	scsi_pkt	*pktp = arg;
19782 	struct	sd_lun		*un;
19783 	struct	buf		*bp;
19784 	struct	sd_xbuf		*xp;
19785 	int	err		= 0;
19786 	int	retry_count	= 0;
19787 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
19788 	struct	sd_sense_info	si;
19789 
19790 	ASSERT(pktp != NULL);
19791 	bp = (struct buf *)pktp->pkt_private;
19792 	ASSERT(bp != NULL);
19793 	xp = SD_GET_XBUF(bp);
19794 	ASSERT(xp != NULL);
19795 	un = SD_GET_UN(bp);
19796 	ASSERT(un != NULL);
19797 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19798 	ASSERT(un->un_f_monitor_media_state);
19799 
19800 	si.ssi_severity = SCSI_ERR_INFO;
19801 	si.ssi_pfa_flag = FALSE;
19802 
19803 	/*
19804 	 * When a reset is issued on a CDROM, it takes a long time to
19805 	 * recover. First few attempts to read capacity and other things
19806 	 * related to handling unit attention fail (with a ASC 0x4 and
19807 	 * ASCQ 0x1). In that case we want to do enough retries and we want
19808 	 * to limit the retries in other cases of genuine failures like
19809 	 * no media in drive.
19810 	 */
19811 	while (retry_count++ < retry_limit) {
19812 		if ((err = sd_handle_mchange(un)) == 0) {
19813 			break;
19814 		}
19815 		if (err == EAGAIN) {
19816 			retry_limit = SD_UNIT_ATTENTION_RETRY;
19817 		}
19818 		/* Sleep for 0.5 sec. & try again */
19819 		delay(drv_usectohz(500000));
19820 	}
19821 
19822 	/*
19823 	 * Dispatch (retry or fail) the original command here,
19824 	 * along with appropriate console messages....
19825 	 *
19826 	 * Must grab the mutex before calling sd_retry_command,
19827 	 * sd_print_sense_msg and sd_return_failed_command.
19828 	 */
19829 	mutex_enter(SD_MUTEX(un));
19830 	if (err != SD_CMD_SUCCESS) {
19831 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
19832 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
19833 		si.ssi_severity = SCSI_ERR_FATAL;
19834 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
19835 		sd_return_failed_command(un, bp, EIO);
19836 	} else {
19837 		sd_retry_command(un, bp, SD_RETRIES_UA, sd_print_sense_msg,
19838 		    &si, EIO, (clock_t)0, NULL);
19839 	}
19840 	mutex_exit(SD_MUTEX(un));
19841 }
19842 
19843 
19844 
19845 /*
19846  *    Function: sd_handle_mchange
19847  *
19848  * Description: Perform geometry validation & other recovery when CDROM
19849  *		has been removed from drive.
19850  *
19851  * Return Code: 0 for success
19852  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
19853  *		sd_send_scsi_READ_CAPACITY()
19854  *
19855  *     Context: Executes in a taskq() thread context
19856  */
19857 
19858 static int
19859 sd_handle_mchange(struct sd_lun *un)
19860 {
19861 	uint64_t	capacity;
19862 	uint32_t	lbasize;
19863 	int		rval;
19864 	sd_ssc_t	*ssc;
19865 
19866 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19867 	ASSERT(un->un_f_monitor_media_state);
19868 
19869 	ssc = sd_ssc_init(un);
19870 	rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
19871 	    SD_PATH_DIRECT_PRIORITY);
19872 
19873 	if (rval != 0)
19874 		goto failed;
19875 
19876 	mutex_enter(SD_MUTEX(un));
19877 	sd_update_block_info(un, lbasize, capacity);
19878 
19879 	if (un->un_errstats != NULL) {
19880 		struct	sd_errstats *stp =
19881 		    (struct sd_errstats *)un->un_errstats->ks_data;
19882 		stp->sd_capacity.value.ui64 = (uint64_t)
19883 		    ((uint64_t)un->un_blockcount *
19884 		    (uint64_t)un->un_tgt_blocksize);
19885 	}
19886 
19887 	/*
19888 	 * Check if the media in the device is writable or not
19889 	 */
19890 	if (ISCD(un)) {
19891 		sd_check_for_writable_cd(ssc, SD_PATH_DIRECT_PRIORITY);
19892 	}
19893 
19894 	/*
19895 	 * Note: Maybe let the strategy/partitioning chain worry about getting
19896 	 * valid geometry.
19897 	 */
19898 	mutex_exit(SD_MUTEX(un));
19899 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
19900 
19901 
19902 	if (cmlb_validate(un->un_cmlbhandle, 0,
19903 	    (void *)SD_PATH_DIRECT_PRIORITY) != 0) {
19904 		sd_ssc_fini(ssc);
19905 		return (EIO);
19906 	} else {
19907 		if (un->un_f_pkstats_enabled) {
19908 			sd_set_pstats(un);
19909 			SD_TRACE(SD_LOG_IO_PARTITION, un,
19910 			    "sd_handle_mchange: un:0x%p pstats created and "
19911 			    "set\n", un);
19912 		}
19913 	}
19914 
19915 	/*
19916 	 * Try to lock the door
19917 	 */
19918 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
19919 	    SD_PATH_DIRECT_PRIORITY);
19920 failed:
19921 	if (rval != 0)
19922 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
19923 	sd_ssc_fini(ssc);
19924 	return (rval);
19925 }
19926 
19927 
19928 /*
19929  *    Function: sd_send_scsi_DOORLOCK
19930  *
19931  * Description: Issue the scsi DOOR LOCK command
19932  *
19933  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
19934  *                      structure for this target.
19935  *		flag  - SD_REMOVAL_ALLOW
19936  *			SD_REMOVAL_PREVENT
19937  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19938  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19939  *			to use the USCSI "direct" chain and bypass the normal
19940  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19941  *			command is issued as part of an error recovery action.
19942  *
19943  * Return Code: 0   - Success
19944  *		errno return code from sd_ssc_send()
19945  *
19946  *     Context: Can sleep.
19947  */
19948 
19949 static int
19950 sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag)
19951 {
19952 	struct scsi_extended_sense	sense_buf;
19953 	union scsi_cdb		cdb;
19954 	struct uscsi_cmd	ucmd_buf;
19955 	int			status;
19956 	struct sd_lun		*un;
19957 
19958 	ASSERT(ssc != NULL);
19959 	un = ssc->ssc_un;
19960 	ASSERT(un != NULL);
19961 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19962 
19963 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19964 
19965 	/* already determined doorlock is not supported, fake success */
19966 	if (un->un_f_doorlock_supported == FALSE) {
19967 		return (0);
19968 	}
19969 
19970 	/*
19971 	 * If we are ejecting and see an SD_REMOVAL_PREVENT
19972 	 * ignore the command so we can complete the eject
19973 	 * operation.
19974 	 */
19975 	if (flag == SD_REMOVAL_PREVENT) {
19976 		mutex_enter(SD_MUTEX(un));
19977 		if (un->un_f_ejecting == TRUE) {
19978 			mutex_exit(SD_MUTEX(un));
19979 			return (EAGAIN);
19980 		}
19981 		mutex_exit(SD_MUTEX(un));
19982 	}
19983 
19984 	bzero(&cdb, sizeof (cdb));
19985 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19986 
19987 	cdb.scc_cmd = SCMD_DOORLOCK;
19988 	cdb.cdb_opaque[4] = (uchar_t)flag;
19989 
19990 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19991 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19992 	ucmd_buf.uscsi_bufaddr	= NULL;
19993 	ucmd_buf.uscsi_buflen	= 0;
19994 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19995 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19996 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19997 	ucmd_buf.uscsi_timeout	= 15;
19998 
19999 	SD_TRACE(SD_LOG_IO, un,
20000 	    "sd_send_scsi_DOORLOCK: returning sd_ssc_send\n");
20001 
20002 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20003 	    UIO_SYSSPACE, path_flag);
20004 
20005 	if (status == 0)
20006 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20007 
20008 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
20009 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20010 	    (scsi_sense_key((uint8_t *)&sense_buf) == KEY_ILLEGAL_REQUEST)) {
20011 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20012 
20013 		/* fake success and skip subsequent doorlock commands */
20014 		un->un_f_doorlock_supported = FALSE;
20015 		return (0);
20016 	}
20017 
20018 	return (status);
20019 }
20020 
20021 /*
20022  *    Function: sd_send_scsi_READ_CAPACITY
20023  *
20024  * Description: This routine uses the scsi READ CAPACITY command to determine
20025  *		the device capacity in number of blocks and the device native
20026  *		block size. If this function returns a failure, then the
20027  *		values in *capp and *lbap are undefined.  If the capacity
20028  *		returned is 0xffffffff then the lun is too large for a
20029  *		normal READ CAPACITY command and the results of a
20030  *		READ CAPACITY 16 will be used instead.
20031  *
20032  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20033  *		capp - ptr to unsigned 64-bit variable to receive the
20034  *			capacity value from the command.
20035  *		lbap - ptr to unsigned 32-bit varaible to receive the
20036  *			block size value from the command
20037  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20038  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20039  *			to use the USCSI "direct" chain and bypass the normal
20040  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20041  *			command is issued as part of an error recovery action.
20042  *
20043  * Return Code: 0   - Success
20044  *		EIO - IO error
20045  *		EACCES - Reservation conflict detected
20046  *		EAGAIN - Device is becoming ready
20047  *		errno return code from sd_ssc_send()
20048  *
20049  *     Context: Can sleep.  Blocks until command completes.
20050  */
20051 
20052 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
20053 
20054 static int
20055 sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp, uint32_t *lbap,
20056 	int path_flag)
20057 {
20058 	struct	scsi_extended_sense	sense_buf;
20059 	struct	uscsi_cmd	ucmd_buf;
20060 	union	scsi_cdb	cdb;
20061 	uint32_t		*capacity_buf;
20062 	uint64_t		capacity;
20063 	uint32_t		lbasize;
20064 	uint32_t		pbsize;
20065 	int			status;
20066 	struct sd_lun		*un;
20067 
20068 	ASSERT(ssc != NULL);
20069 
20070 	un = ssc->ssc_un;
20071 	ASSERT(un != NULL);
20072 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20073 	ASSERT(capp != NULL);
20074 	ASSERT(lbap != NULL);
20075 
20076 	SD_TRACE(SD_LOG_IO, un,
20077 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20078 
20079 	/*
20080 	 * First send a READ_CAPACITY command to the target.
20081 	 * (This command is mandatory under SCSI-2.)
20082 	 *
20083 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
20084 	 * Medium Indicator bit is cleared.  The address field must be
20085 	 * zero if the PMI bit is zero.
20086 	 */
20087 	bzero(&cdb, sizeof (cdb));
20088 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20089 
20090 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
20091 
20092 	cdb.scc_cmd = SCMD_READ_CAPACITY;
20093 
20094 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20095 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20096 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
20097 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
20098 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20099 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20100 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20101 	ucmd_buf.uscsi_timeout	= 60;
20102 
20103 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20104 	    UIO_SYSSPACE, path_flag);
20105 
20106 	switch (status) {
20107 	case 0:
20108 		/* Return failure if we did not get valid capacity data. */
20109 		if (ucmd_buf.uscsi_resid != 0) {
20110 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20111 			    "sd_send_scsi_READ_CAPACITY received invalid "
20112 			    "capacity data");
20113 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20114 			return (EIO);
20115 		}
20116 		/*
20117 		 * Read capacity and block size from the READ CAPACITY 10 data.
20118 		 * This data may be adjusted later due to device specific
20119 		 * issues.
20120 		 *
20121 		 * According to the SCSI spec, the READ CAPACITY 10
20122 		 * command returns the following:
20123 		 *
20124 		 *  bytes 0-3: Maximum logical block address available.
20125 		 *		(MSB in byte:0 & LSB in byte:3)
20126 		 *
20127 		 *  bytes 4-7: Block length in bytes
20128 		 *		(MSB in byte:4 & LSB in byte:7)
20129 		 *
20130 		 */
20131 		capacity = BE_32(capacity_buf[0]);
20132 		lbasize = BE_32(capacity_buf[1]);
20133 
20134 		/*
20135 		 * Done with capacity_buf
20136 		 */
20137 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20138 
20139 		/*
20140 		 * if the reported capacity is set to all 0xf's, then
20141 		 * this disk is too large and requires SBC-2 commands.
20142 		 * Reissue the request using READ CAPACITY 16.
20143 		 */
20144 		if (capacity == 0xffffffff) {
20145 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20146 			status = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity,
20147 			    &lbasize, &pbsize, path_flag);
20148 			if (status != 0) {
20149 				return (status);
20150 			} else {
20151 				goto rc16_done;
20152 			}
20153 		}
20154 		break;	/* Success! */
20155 	case EIO:
20156 		switch (ucmd_buf.uscsi_status) {
20157 		case STATUS_RESERVATION_CONFLICT:
20158 			status = EACCES;
20159 			break;
20160 		case STATUS_CHECK:
20161 			/*
20162 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20163 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20164 			 */
20165 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20166 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20167 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20168 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20169 				return (EAGAIN);
20170 			}
20171 			break;
20172 		default:
20173 			break;
20174 		}
20175 		/* FALLTHRU */
20176 	default:
20177 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
20178 		return (status);
20179 	}
20180 
20181 	/*
20182 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20183 	 * (2352 and 0 are common) so for these devices always force the value
20184 	 * to 2048 as required by the ATAPI specs.
20185 	 */
20186 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20187 		lbasize = 2048;
20188 	}
20189 
20190 	/*
20191 	 * Get the maximum LBA value from the READ CAPACITY data.
20192 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20193 	 * was cleared when issuing the command. This means that the LBA
20194 	 * returned from the device is the LBA of the last logical block
20195 	 * on the logical unit.  The actual logical block count will be
20196 	 * this value plus one.
20197 	 */
20198 	capacity += 1;
20199 
20200 	/*
20201 	 * Currently, for removable media, the capacity is saved in terms
20202 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20203 	 */
20204 	if (un->un_f_has_removable_media)
20205 		capacity *= (lbasize / un->un_sys_blocksize);
20206 
20207 rc16_done:
20208 
20209 	/*
20210 	 * Copy the values from the READ CAPACITY command into the space
20211 	 * provided by the caller.
20212 	 */
20213 	*capp = capacity;
20214 	*lbap = lbasize;
20215 
20216 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
20217 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
20218 
20219 	/*
20220 	 * Both the lbasize and capacity from the device must be nonzero,
20221 	 * otherwise we assume that the values are not valid and return
20222 	 * failure to the caller. (4203735)
20223 	 */
20224 	if ((capacity == 0) || (lbasize == 0)) {
20225 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20226 		    "sd_send_scsi_READ_CAPACITY received invalid value "
20227 		    "capacity %llu lbasize %d", capacity, lbasize);
20228 		return (EIO);
20229 	}
20230 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20231 	return (0);
20232 }
20233 
20234 /*
20235  *    Function: sd_send_scsi_READ_CAPACITY_16
20236  *
20237  * Description: This routine uses the scsi READ CAPACITY 16 command to
20238  *		determine the device capacity in number of blocks and the
20239  *		device native block size.  If this function returns a failure,
20240  *		then the values in *capp and *lbap are undefined.
20241  *		This routine should be called by sd_send_scsi_READ_CAPACITY
20242  *              which will apply any device specific adjustments to capacity
20243  *              and lbasize. One exception is it is also called by
20244  *              sd_get_media_info_ext. In that function, there is no need to
20245  *              adjust the capacity and lbasize.
20246  *
20247  *   Arguments: ssc   - ssc contains ptr to soft state struct for the target
20248  *		capp - ptr to unsigned 64-bit variable to receive the
20249  *			capacity value from the command.
20250  *		lbap - ptr to unsigned 32-bit varaible to receive the
20251  *			block size value from the command
20252  *              psp  - ptr to unsigned 32-bit variable to receive the
20253  *                      physical block size value from the command
20254  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20255  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20256  *			to use the USCSI "direct" chain and bypass the normal
20257  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
20258  *			this command is issued as part of an error recovery
20259  *			action.
20260  *
20261  * Return Code: 0   - Success
20262  *		EIO - IO error
20263  *		EACCES - Reservation conflict detected
20264  *		EAGAIN - Device is becoming ready
20265  *		errno return code from sd_ssc_send()
20266  *
20267  *     Context: Can sleep.  Blocks until command completes.
20268  */
20269 
20270 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
20271 
20272 static int
20273 sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
20274 	uint32_t *lbap, uint32_t *psp, int path_flag)
20275 {
20276 	struct	scsi_extended_sense	sense_buf;
20277 	struct	uscsi_cmd	ucmd_buf;
20278 	union	scsi_cdb	cdb;
20279 	uint64_t		*capacity16_buf;
20280 	uint64_t		capacity;
20281 	uint32_t		lbasize;
20282 	uint32_t		pbsize;
20283 	uint32_t		lbpb_exp;
20284 	int			status;
20285 	struct sd_lun		*un;
20286 
20287 	ASSERT(ssc != NULL);
20288 
20289 	un = ssc->ssc_un;
20290 	ASSERT(un != NULL);
20291 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20292 	ASSERT(capp != NULL);
20293 	ASSERT(lbap != NULL);
20294 
20295 	SD_TRACE(SD_LOG_IO, un,
20296 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
20297 
20298 	/*
20299 	 * First send a READ_CAPACITY_16 command to the target.
20300 	 *
20301 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
20302 	 * Medium Indicator bit is cleared.  The address field must be
20303 	 * zero if the PMI bit is zero.
20304 	 */
20305 	bzero(&cdb, sizeof (cdb));
20306 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20307 
20308 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
20309 
20310 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20311 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
20312 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
20313 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
20314 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20315 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
20316 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20317 	ucmd_buf.uscsi_timeout	= 60;
20318 
20319 	/*
20320 	 * Read Capacity (16) is a Service Action In command.  One
20321 	 * command byte (0x9E) is overloaded for multiple operations,
20322 	 * with the second CDB byte specifying the desired operation
20323 	 */
20324 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
20325 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
20326 
20327 	/*
20328 	 * Fill in allocation length field
20329 	 */
20330 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
20331 
20332 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20333 	    UIO_SYSSPACE, path_flag);
20334 
20335 	switch (status) {
20336 	case 0:
20337 		/* Return failure if we did not get valid capacity data. */
20338 		if (ucmd_buf.uscsi_resid > 20) {
20339 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20340 			    "sd_send_scsi_READ_CAPACITY_16 received invalid "
20341 			    "capacity data");
20342 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20343 			return (EIO);
20344 		}
20345 
20346 		/*
20347 		 * Read capacity and block size from the READ CAPACITY 16 data.
20348 		 * This data may be adjusted later due to device specific
20349 		 * issues.
20350 		 *
20351 		 * According to the SCSI spec, the READ CAPACITY 16
20352 		 * command returns the following:
20353 		 *
20354 		 *  bytes 0-7: Maximum logical block address available.
20355 		 *		(MSB in byte:0 & LSB in byte:7)
20356 		 *
20357 		 *  bytes 8-11: Block length in bytes
20358 		 *		(MSB in byte:8 & LSB in byte:11)
20359 		 *
20360 		 *  byte 13: LOGICAL BLOCKS PER PHYSICAL BLOCK EXPONENT
20361 		 */
20362 		capacity = BE_64(capacity16_buf[0]);
20363 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
20364 		lbpb_exp = (BE_64(capacity16_buf[1]) >> 16) & 0x0f;
20365 
20366 		pbsize = lbasize << lbpb_exp;
20367 
20368 		/*
20369 		 * Done with capacity16_buf
20370 		 */
20371 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20372 
20373 		/*
20374 		 * if the reported capacity is set to all 0xf's, then
20375 		 * this disk is too large.  This could only happen with
20376 		 * a device that supports LBAs larger than 64 bits which
20377 		 * are not defined by any current T10 standards.
20378 		 */
20379 		if (capacity == 0xffffffffffffffff) {
20380 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20381 			    "disk is too large");
20382 			return (EIO);
20383 		}
20384 		break;	/* Success! */
20385 	case EIO:
20386 		switch (ucmd_buf.uscsi_status) {
20387 		case STATUS_RESERVATION_CONFLICT:
20388 			status = EACCES;
20389 			break;
20390 		case STATUS_CHECK:
20391 			/*
20392 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
20393 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
20394 			 */
20395 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20396 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x04) &&
20397 			    (scsi_sense_ascq((uint8_t *)&sense_buf) == 0x01)) {
20398 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20399 				return (EAGAIN);
20400 			}
20401 			break;
20402 		default:
20403 			break;
20404 		}
20405 		/* FALLTHRU */
20406 	default:
20407 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
20408 		return (status);
20409 	}
20410 
20411 	/*
20412 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
20413 	 * (2352 and 0 are common) so for these devices always force the value
20414 	 * to 2048 as required by the ATAPI specs.
20415 	 */
20416 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
20417 		lbasize = 2048;
20418 	}
20419 
20420 	/*
20421 	 * Get the maximum LBA value from the READ CAPACITY 16 data.
20422 	 * Here we assume that the Partial Medium Indicator (PMI) bit
20423 	 * was cleared when issuing the command. This means that the LBA
20424 	 * returned from the device is the LBA of the last logical block
20425 	 * on the logical unit.  The actual logical block count will be
20426 	 * this value plus one.
20427 	 */
20428 	capacity += 1;
20429 
20430 	/*
20431 	 * Currently, for removable media, the capacity is saved in terms
20432 	 * of un->un_sys_blocksize, so scale the capacity value to reflect this.
20433 	 */
20434 	if (un->un_f_has_removable_media)
20435 		capacity *= (lbasize / un->un_sys_blocksize);
20436 
20437 	*capp = capacity;
20438 	*lbap = lbasize;
20439 	*psp = pbsize;
20440 
20441 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
20442 	    "capacity:0x%llx  lbasize:0x%x, pbsize: 0x%x\n",
20443 	    capacity, lbasize, pbsize);
20444 
20445 	if ((capacity == 0) || (lbasize == 0) || (pbsize == 0)) {
20446 		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
20447 		    "sd_send_scsi_READ_CAPACITY_16 received invalid value "
20448 		    "capacity %llu lbasize %d pbsize %d", capacity, lbasize);
20449 		return (EIO);
20450 	}
20451 
20452 	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20453 	return (0);
20454 }
20455 
20456 
20457 /*
20458  *    Function: sd_send_scsi_START_STOP_UNIT
20459  *
20460  * Description: Issue a scsi START STOP UNIT command to the target.
20461  *
20462  *   Arguments: ssc    - ssc contatins pointer to driver soft state (unit)
20463  *                       structure for this target.
20464  *      pc_flag - SD_POWER_CONDITION
20465  *                SD_START_STOP
20466  *		flag  - SD_TARGET_START
20467  *			SD_TARGET_STOP
20468  *			SD_TARGET_EJECT
20469  *			SD_TARGET_CLOSE
20470  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20471  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20472  *			to use the USCSI "direct" chain and bypass the normal
20473  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
20474  *			command is issued as part of an error recovery action.
20475  *
20476  * Return Code: 0   - Success
20477  *		EIO - IO error
20478  *		EACCES - Reservation conflict detected
20479  *		ENXIO  - Not Ready, medium not present
20480  *		errno return code from sd_ssc_send()
20481  *
20482  *     Context: Can sleep.
20483  */
20484 
20485 static int
20486 sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag, int flag,
20487     int path_flag)
20488 {
20489 	struct	scsi_extended_sense	sense_buf;
20490 	union scsi_cdb		cdb;
20491 	struct uscsi_cmd	ucmd_buf;
20492 	int			status;
20493 	struct sd_lun		*un;
20494 
20495 	ASSERT(ssc != NULL);
20496 	un = ssc->ssc_un;
20497 	ASSERT(un != NULL);
20498 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20499 
20500 	SD_TRACE(SD_LOG_IO, un,
20501 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
20502 
20503 	if (un->un_f_check_start_stop &&
20504 	    (pc_flag == SD_START_STOP) &&
20505 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
20506 	    (un->un_f_start_stop_supported != TRUE)) {
20507 		return (0);
20508 	}
20509 
20510 	/*
20511 	 * If we are performing an eject operation and
20512 	 * we receive any command other than SD_TARGET_EJECT
20513 	 * we should immediately return.
20514 	 */
20515 	if (flag != SD_TARGET_EJECT) {
20516 		mutex_enter(SD_MUTEX(un));
20517 		if (un->un_f_ejecting == TRUE) {
20518 			mutex_exit(SD_MUTEX(un));
20519 			return (EAGAIN);
20520 		}
20521 		mutex_exit(SD_MUTEX(un));
20522 	}
20523 
20524 	bzero(&cdb, sizeof (cdb));
20525 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20526 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20527 
20528 	cdb.scc_cmd = SCMD_START_STOP;
20529 	cdb.cdb_opaque[4] = (pc_flag == SD_POWER_CONDITION) ?
20530 	    (uchar_t)(flag << 4) : (uchar_t)flag;
20531 
20532 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20533 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20534 	ucmd_buf.uscsi_bufaddr	= NULL;
20535 	ucmd_buf.uscsi_buflen	= 0;
20536 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20537 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20538 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20539 	ucmd_buf.uscsi_timeout	= 200;
20540 
20541 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20542 	    UIO_SYSSPACE, path_flag);
20543 
20544 	switch (status) {
20545 	case 0:
20546 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20547 		break;	/* Success! */
20548 	case EIO:
20549 		switch (ucmd_buf.uscsi_status) {
20550 		case STATUS_RESERVATION_CONFLICT:
20551 			status = EACCES;
20552 			break;
20553 		case STATUS_CHECK:
20554 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
20555 				switch (scsi_sense_key(
20556 				    (uint8_t *)&sense_buf)) {
20557 				case KEY_ILLEGAL_REQUEST:
20558 					status = ENOTSUP;
20559 					break;
20560 				case KEY_NOT_READY:
20561 					if (scsi_sense_asc(
20562 					    (uint8_t *)&sense_buf)
20563 					    == 0x3A) {
20564 						status = ENXIO;
20565 					}
20566 					break;
20567 				default:
20568 					break;
20569 				}
20570 			}
20571 			break;
20572 		default:
20573 			break;
20574 		}
20575 		break;
20576 	default:
20577 		break;
20578 	}
20579 
20580 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
20581 
20582 	return (status);
20583 }
20584 
20585 
20586 /*
20587  *    Function: sd_start_stop_unit_callback
20588  *
20589  * Description: timeout(9F) callback to begin recovery process for a
20590  *		device that has spun down.
20591  *
20592  *   Arguments: arg - pointer to associated softstate struct.
20593  *
20594  *     Context: Executes in a timeout(9F) thread context
20595  */
20596 
20597 static void
20598 sd_start_stop_unit_callback(void *arg)
20599 {
20600 	struct sd_lun	*un = arg;
20601 	ASSERT(un != NULL);
20602 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20603 
20604 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
20605 
20606 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
20607 }
20608 
20609 
20610 /*
20611  *    Function: sd_start_stop_unit_task
20612  *
20613  * Description: Recovery procedure when a drive is spun down.
20614  *
20615  *   Arguments: arg - pointer to associated softstate struct.
20616  *
20617  *     Context: Executes in a taskq() thread context
20618  */
20619 
20620 static void
20621 sd_start_stop_unit_task(void *arg)
20622 {
20623 	struct sd_lun	*un = arg;
20624 	sd_ssc_t	*ssc;
20625 	int		power_level;
20626 	int		rval;
20627 
20628 	ASSERT(un != NULL);
20629 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20630 
20631 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
20632 
20633 	/*
20634 	 * Some unformatted drives report not ready error, no need to
20635 	 * restart if format has been initiated.
20636 	 */
20637 	mutex_enter(SD_MUTEX(un));
20638 	if (un->un_f_format_in_progress == TRUE) {
20639 		mutex_exit(SD_MUTEX(un));
20640 		return;
20641 	}
20642 	mutex_exit(SD_MUTEX(un));
20643 
20644 	ssc = sd_ssc_init(un);
20645 	/*
20646 	 * When a START STOP command is issued from here, it is part of a
20647 	 * failure recovery operation and must be issued before any other
20648 	 * commands, including any pending retries. Thus it must be sent
20649 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
20650 	 * succeeds or not, we will start I/O after the attempt.
20651 	 * If power condition is supported and the current power level
20652 	 * is capable of performing I/O, we should set the power condition
20653 	 * to that level. Otherwise, set the power condition to ACTIVE.
20654 	 */
20655 	if (un->un_f_power_condition_supported) {
20656 		mutex_enter(SD_MUTEX(un));
20657 		ASSERT(SD_PM_IS_LEVEL_VALID(un, un->un_power_level));
20658 		power_level = sd_pwr_pc.ran_perf[un->un_power_level]
20659 		    > 0 ? un->un_power_level : SD_SPINDLE_ACTIVE;
20660 		mutex_exit(SD_MUTEX(un));
20661 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_POWER_CONDITION,
20662 		    sd_pl2pc[power_level], SD_PATH_DIRECT_PRIORITY);
20663 	} else {
20664 		rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
20665 		    SD_TARGET_START, SD_PATH_DIRECT_PRIORITY);
20666 	}
20667 
20668 	if (rval != 0)
20669 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
20670 	sd_ssc_fini(ssc);
20671 	/*
20672 	 * The above call blocks until the START_STOP_UNIT command completes.
20673 	 * Now that it has completed, we must re-try the original IO that
20674 	 * received the NOT READY condition in the first place. There are
20675 	 * three possible conditions here:
20676 	 *
20677 	 *  (1) The original IO is on un_retry_bp.
20678 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
20679 	 *	is NULL.
20680 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
20681 	 *	points to some other, unrelated bp.
20682 	 *
20683 	 * For each case, we must call sd_start_cmds() with un_retry_bp
20684 	 * as the argument. If un_retry_bp is NULL, this will initiate
20685 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
20686 	 * then this will process the bp on un_retry_bp. That may or may not
20687 	 * be the original IO, but that does not matter: the important thing
20688 	 * is to keep the IO processing going at this point.
20689 	 *
20690 	 * Note: This is a very specific error recovery sequence associated
20691 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
20692 	 * serialize the I/O with completion of the spin-up.
20693 	 */
20694 	mutex_enter(SD_MUTEX(un));
20695 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
20696 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
20697 	    un, un->un_retry_bp);
20698 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
20699 	sd_start_cmds(un, un->un_retry_bp);
20700 	mutex_exit(SD_MUTEX(un));
20701 
20702 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
20703 }
20704 
20705 
20706 /*
20707  *    Function: sd_send_scsi_INQUIRY
20708  *
20709  * Description: Issue the scsi INQUIRY command.
20710  *
20711  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20712  *                      structure for this target.
20713  *		bufaddr
20714  *		buflen
20715  *		evpd
20716  *		page_code
20717  *		page_length
20718  *
20719  * Return Code: 0   - Success
20720  *		errno return code from sd_ssc_send()
20721  *
20722  *     Context: Can sleep. Does not return until command is completed.
20723  */
20724 
20725 static int
20726 sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr, size_t buflen,
20727 	uchar_t evpd, uchar_t page_code, size_t *residp)
20728 {
20729 	union scsi_cdb		cdb;
20730 	struct uscsi_cmd	ucmd_buf;
20731 	int			status;
20732 	struct sd_lun		*un;
20733 
20734 	ASSERT(ssc != NULL);
20735 	un = ssc->ssc_un;
20736 	ASSERT(un != NULL);
20737 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20738 	ASSERT(bufaddr != NULL);
20739 
20740 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
20741 
20742 	bzero(&cdb, sizeof (cdb));
20743 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20744 	bzero(bufaddr, buflen);
20745 
20746 	cdb.scc_cmd = SCMD_INQUIRY;
20747 	cdb.cdb_opaque[1] = evpd;
20748 	cdb.cdb_opaque[2] = page_code;
20749 	FORMG0COUNT(&cdb, buflen);
20750 
20751 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20752 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20753 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20754 	ucmd_buf.uscsi_buflen	= buflen;
20755 	ucmd_buf.uscsi_rqbuf	= NULL;
20756 	ucmd_buf.uscsi_rqlen	= 0;
20757 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
20758 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
20759 
20760 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20761 	    UIO_SYSSPACE, SD_PATH_DIRECT);
20762 
20763 	/*
20764 	 * Only handle status == 0, the upper-level caller
20765 	 * will put different assessment based on the context.
20766 	 */
20767 	if (status == 0)
20768 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20769 
20770 	if ((status == 0) && (residp != NULL)) {
20771 		*residp = ucmd_buf.uscsi_resid;
20772 	}
20773 
20774 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
20775 
20776 	return (status);
20777 }
20778 
20779 
20780 /*
20781  *    Function: sd_send_scsi_TEST_UNIT_READY
20782  *
20783  * Description: Issue the scsi TEST UNIT READY command.
20784  *		This routine can be told to set the flag USCSI_DIAGNOSE to
20785  *		prevent retrying failed commands. Use this when the intent
20786  *		is either to check for device readiness, to clear a Unit
20787  *		Attention, or to clear any outstanding sense data.
20788  *		However under specific conditions the expected behavior
20789  *		is for retries to bring a device ready, so use the flag
20790  *		with caution.
20791  *
20792  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20793  *                      structure for this target.
20794  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
20795  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
20796  *			0: dont check for media present, do retries on cmd.
20797  *
20798  * Return Code: 0   - Success
20799  *		EIO - IO error
20800  *		EACCES - Reservation conflict detected
20801  *		ENXIO  - Not Ready, medium not present
20802  *		errno return code from sd_ssc_send()
20803  *
20804  *     Context: Can sleep. Does not return until command is completed.
20805  */
20806 
20807 static int
20808 sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag)
20809 {
20810 	struct	scsi_extended_sense	sense_buf;
20811 	union scsi_cdb		cdb;
20812 	struct uscsi_cmd	ucmd_buf;
20813 	int			status;
20814 	struct sd_lun		*un;
20815 
20816 	ASSERT(ssc != NULL);
20817 	un = ssc->ssc_un;
20818 	ASSERT(un != NULL);
20819 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20820 
20821 	SD_TRACE(SD_LOG_IO, un,
20822 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
20823 
20824 	/*
20825 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
20826 	 * timeouts when they receive a TUR and the queue is not empty. Check
20827 	 * the configuration flag set during attach (indicating the drive has
20828 	 * this firmware bug) and un_ncmds_in_transport before issuing the
20829 	 * TUR. If there are
20830 	 * pending commands return success, this is a bit arbitrary but is ok
20831 	 * for non-removables (i.e. the eliteI disks) and non-clustering
20832 	 * configurations.
20833 	 */
20834 	if (un->un_f_cfg_tur_check == TRUE) {
20835 		mutex_enter(SD_MUTEX(un));
20836 		if (un->un_ncmds_in_transport != 0) {
20837 			mutex_exit(SD_MUTEX(un));
20838 			return (0);
20839 		}
20840 		mutex_exit(SD_MUTEX(un));
20841 	}
20842 
20843 	bzero(&cdb, sizeof (cdb));
20844 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20845 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20846 
20847 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
20848 
20849 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20850 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
20851 	ucmd_buf.uscsi_bufaddr	= NULL;
20852 	ucmd_buf.uscsi_buflen	= 0;
20853 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20854 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20855 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
20856 
20857 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
20858 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
20859 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
20860 	}
20861 	ucmd_buf.uscsi_timeout	= 60;
20862 
20863 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20864 	    UIO_SYSSPACE, ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT :
20865 	    SD_PATH_STANDARD));
20866 
20867 	switch (status) {
20868 	case 0:
20869 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20870 		break;	/* Success! */
20871 	case EIO:
20872 		switch (ucmd_buf.uscsi_status) {
20873 		case STATUS_RESERVATION_CONFLICT:
20874 			status = EACCES;
20875 			break;
20876 		case STATUS_CHECK:
20877 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
20878 				break;
20879 			}
20880 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20881 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20882 			    KEY_NOT_READY) &&
20883 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x3A)) {
20884 				status = ENXIO;
20885 			}
20886 			break;
20887 		default:
20888 			break;
20889 		}
20890 		break;
20891 	default:
20892 		break;
20893 	}
20894 
20895 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
20896 
20897 	return (status);
20898 }
20899 
20900 /*
20901  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
20902  *
20903  * Description: Issue the scsi PERSISTENT RESERVE IN command.
20904  *
20905  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
20906  *                      structure for this target.
20907  *
20908  * Return Code: 0   - Success
20909  *		EACCES
20910  *		ENOTSUP
20911  *		errno return code from sd_ssc_send()
20912  *
20913  *     Context: Can sleep. Does not return until command is completed.
20914  */
20915 
20916 static int
20917 sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc, uchar_t  usr_cmd,
20918 	uint16_t data_len, uchar_t *data_bufp)
20919 {
20920 	struct scsi_extended_sense	sense_buf;
20921 	union scsi_cdb		cdb;
20922 	struct uscsi_cmd	ucmd_buf;
20923 	int			status;
20924 	int			no_caller_buf = FALSE;
20925 	struct sd_lun		*un;
20926 
20927 	ASSERT(ssc != NULL);
20928 	un = ssc->ssc_un;
20929 	ASSERT(un != NULL);
20930 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20931 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
20932 
20933 	SD_TRACE(SD_LOG_IO, un,
20934 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
20935 
20936 	bzero(&cdb, sizeof (cdb));
20937 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20938 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20939 	if (data_bufp == NULL) {
20940 		/* Allocate a default buf if the caller did not give one */
20941 		ASSERT(data_len == 0);
20942 		data_len  = MHIOC_RESV_KEY_SIZE;
20943 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
20944 		no_caller_buf = TRUE;
20945 	}
20946 
20947 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
20948 	cdb.cdb_opaque[1] = usr_cmd;
20949 	FORMG1COUNT(&cdb, data_len);
20950 
20951 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20952 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20953 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
20954 	ucmd_buf.uscsi_buflen	= data_len;
20955 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20956 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20957 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20958 	ucmd_buf.uscsi_timeout	= 60;
20959 
20960 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
20961 	    UIO_SYSSPACE, SD_PATH_STANDARD);
20962 
20963 	switch (status) {
20964 	case 0:
20965 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
20966 
20967 		break;	/* Success! */
20968 	case EIO:
20969 		switch (ucmd_buf.uscsi_status) {
20970 		case STATUS_RESERVATION_CONFLICT:
20971 			status = EACCES;
20972 			break;
20973 		case STATUS_CHECK:
20974 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20975 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
20976 			    KEY_ILLEGAL_REQUEST)) {
20977 				status = ENOTSUP;
20978 			}
20979 			break;
20980 		default:
20981 			break;
20982 		}
20983 		break;
20984 	default:
20985 		break;
20986 	}
20987 
20988 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
20989 
20990 	if (no_caller_buf == TRUE) {
20991 		kmem_free(data_bufp, data_len);
20992 	}
20993 
20994 	return (status);
20995 }
20996 
20997 
20998 /*
20999  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
21000  *
21001  * Description: This routine is the driver entry point for handling CD-ROM
21002  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
21003  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
21004  *		device.
21005  *
21006  *   Arguments: ssc  -  ssc contains un - pointer to soft state struct
21007  *                      for the target.
21008  *		usr_cmd SCSI-3 reservation facility command (one of
21009  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
21010  *			SD_SCSI3_PREEMPTANDABORT, SD_SCSI3_CLEAR)
21011  *		usr_bufp - user provided pointer register, reserve descriptor or
21012  *			preempt and abort structure (mhioc_register_t,
21013  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
21014  *
21015  * Return Code: 0   - Success
21016  *		EACCES
21017  *		ENOTSUP
21018  *		errno return code from sd_ssc_send()
21019  *
21020  *     Context: Can sleep. Does not return until command is completed.
21021  */
21022 
21023 static int
21024 sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc, uchar_t usr_cmd,
21025 	uchar_t	*usr_bufp)
21026 {
21027 	struct scsi_extended_sense	sense_buf;
21028 	union scsi_cdb		cdb;
21029 	struct uscsi_cmd	ucmd_buf;
21030 	int			status;
21031 	uchar_t			data_len = sizeof (sd_prout_t);
21032 	sd_prout_t		*prp;
21033 	struct sd_lun		*un;
21034 
21035 	ASSERT(ssc != NULL);
21036 	un = ssc->ssc_un;
21037 	ASSERT(un != NULL);
21038 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21039 	ASSERT(data_len == 24);	/* required by scsi spec */
21040 
21041 	SD_TRACE(SD_LOG_IO, un,
21042 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
21043 
21044 	if (usr_bufp == NULL) {
21045 		return (EINVAL);
21046 	}
21047 
21048 	bzero(&cdb, sizeof (cdb));
21049 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21050 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21051 	prp = kmem_zalloc(data_len, KM_SLEEP);
21052 
21053 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
21054 	cdb.cdb_opaque[1] = usr_cmd;
21055 	FORMG1COUNT(&cdb, data_len);
21056 
21057 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21058 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
21059 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
21060 	ucmd_buf.uscsi_buflen	= data_len;
21061 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21062 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21063 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21064 	ucmd_buf.uscsi_timeout	= 60;
21065 
21066 	switch (usr_cmd) {
21067 	case SD_SCSI3_REGISTER: {
21068 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
21069 
21070 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21071 		bcopy(ptr->newkey.key, prp->service_key,
21072 		    MHIOC_RESV_KEY_SIZE);
21073 		prp->aptpl = ptr->aptpl;
21074 		break;
21075 	}
21076 	case SD_SCSI3_CLEAR: {
21077 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21078 
21079 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21080 		break;
21081 	}
21082 	case SD_SCSI3_RESERVE:
21083 	case SD_SCSI3_RELEASE: {
21084 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
21085 
21086 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21087 		prp->scope_address = BE_32(ptr->scope_specific_addr);
21088 		cdb.cdb_opaque[2] = ptr->type;
21089 		break;
21090 	}
21091 	case SD_SCSI3_PREEMPTANDABORT: {
21092 		mhioc_preemptandabort_t *ptr =
21093 		    (mhioc_preemptandabort_t *)usr_bufp;
21094 
21095 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
21096 		bcopy(ptr->victim_key.key, prp->service_key,
21097 		    MHIOC_RESV_KEY_SIZE);
21098 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
21099 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
21100 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
21101 		break;
21102 	}
21103 	case SD_SCSI3_REGISTERANDIGNOREKEY:
21104 	{
21105 		mhioc_registerandignorekey_t *ptr;
21106 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
21107 		bcopy(ptr->newkey.key,
21108 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
21109 		prp->aptpl = ptr->aptpl;
21110 		break;
21111 	}
21112 	default:
21113 		ASSERT(FALSE);
21114 		break;
21115 	}
21116 
21117 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21118 	    UIO_SYSSPACE, SD_PATH_STANDARD);
21119 
21120 	switch (status) {
21121 	case 0:
21122 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21123 		break;	/* Success! */
21124 	case EIO:
21125 		switch (ucmd_buf.uscsi_status) {
21126 		case STATUS_RESERVATION_CONFLICT:
21127 			status = EACCES;
21128 			break;
21129 		case STATUS_CHECK:
21130 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
21131 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
21132 			    KEY_ILLEGAL_REQUEST)) {
21133 				status = ENOTSUP;
21134 			}
21135 			break;
21136 		default:
21137 			break;
21138 		}
21139 		break;
21140 	default:
21141 		break;
21142 	}
21143 
21144 	kmem_free(prp, data_len);
21145 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
21146 	return (status);
21147 }
21148 
21149 
21150 /*
21151  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
21152  *
21153  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
21154  *
21155  *   Arguments: un - pointer to the target's soft state struct
21156  *              dkc - pointer to the callback structure
21157  *
21158  * Return Code: 0 - success
21159  *		errno-type error code
21160  *
21161  *     Context: kernel thread context only.
21162  *
21163  *  _______________________________________________________________
21164  * | dkc_flag &   | dkc_callback | DKIOCFLUSHWRITECACHE            |
21165  * |FLUSH_VOLATILE|              | operation                       |
21166  * |______________|______________|_________________________________|
21167  * | 0            | NULL         | Synchronous flush on both       |
21168  * |              |              | volatile and non-volatile cache |
21169  * |______________|______________|_________________________________|
21170  * | 1            | NULL         | Synchronous flush on volatile   |
21171  * |              |              | cache; disk drivers may suppress|
21172  * |              |              | flush if disk table indicates   |
21173  * |              |              | non-volatile cache              |
21174  * |______________|______________|_________________________________|
21175  * | 0            | !NULL        | Asynchronous flush on both      |
21176  * |              |              | volatile and non-volatile cache;|
21177  * |______________|______________|_________________________________|
21178  * | 1            | !NULL        | Asynchronous flush on volatile  |
21179  * |              |              | cache; disk drivers may suppress|
21180  * |              |              | flush if disk table indicates   |
21181  * |              |              | non-volatile cache              |
21182  * |______________|______________|_________________________________|
21183  *
21184  */
21185 
21186 static int
21187 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
21188 {
21189 	struct sd_uscsi_info	*uip;
21190 	struct uscsi_cmd	*uscmd;
21191 	union scsi_cdb		*cdb;
21192 	struct buf		*bp;
21193 	int			rval = 0;
21194 	int			is_async;
21195 
21196 	SD_TRACE(SD_LOG_IO, un,
21197 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
21198 
21199 	ASSERT(un != NULL);
21200 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21201 
21202 	if (dkc == NULL || dkc->dkc_callback == NULL) {
21203 		is_async = FALSE;
21204 	} else {
21205 		is_async = TRUE;
21206 	}
21207 
21208 	mutex_enter(SD_MUTEX(un));
21209 	/* check whether cache flush should be suppressed */
21210 	if (un->un_f_suppress_cache_flush == TRUE) {
21211 		mutex_exit(SD_MUTEX(un));
21212 		/*
21213 		 * suppress the cache flush if the device is told to do
21214 		 * so by sd.conf or disk table
21215 		 */
21216 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: \
21217 		    skip the cache flush since suppress_cache_flush is %d!\n",
21218 		    un->un_f_suppress_cache_flush);
21219 
21220 		if (is_async == TRUE) {
21221 			/* invoke callback for asynchronous flush */
21222 			(*dkc->dkc_callback)(dkc->dkc_cookie, 0);
21223 		}
21224 		return (rval);
21225 	}
21226 	mutex_exit(SD_MUTEX(un));
21227 
21228 	/*
21229 	 * check dkc_flag & FLUSH_VOLATILE so SYNC_NV bit can be
21230 	 * set properly
21231 	 */
21232 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
21233 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
21234 
21235 	mutex_enter(SD_MUTEX(un));
21236 	if (dkc != NULL && un->un_f_sync_nv_supported &&
21237 	    (dkc->dkc_flag & FLUSH_VOLATILE)) {
21238 		/*
21239 		 * if the device supports SYNC_NV bit, turn on
21240 		 * the SYNC_NV bit to only flush volatile cache
21241 		 */
21242 		cdb->cdb_un.tag |= SD_SYNC_NV_BIT;
21243 	}
21244 	mutex_exit(SD_MUTEX(un));
21245 
21246 	/*
21247 	 * First get some memory for the uscsi_cmd struct and cdb
21248 	 * and initialize for SYNCHRONIZE_CACHE cmd.
21249 	 */
21250 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21251 	uscmd->uscsi_cdblen = CDB_GROUP1;
21252 	uscmd->uscsi_cdb = (caddr_t)cdb;
21253 	uscmd->uscsi_bufaddr = NULL;
21254 	uscmd->uscsi_buflen = 0;
21255 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21256 	uscmd->uscsi_rqlen = SENSE_LENGTH;
21257 	uscmd->uscsi_rqresid = SENSE_LENGTH;
21258 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
21259 	uscmd->uscsi_timeout = sd_io_time;
21260 
21261 	/*
21262 	 * Allocate an sd_uscsi_info struct and fill it with the info
21263 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
21264 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
21265 	 * since we allocate the buf here in this function, we do not
21266 	 * need to preserve the prior contents of b_private.
21267 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
21268 	 */
21269 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
21270 	uip->ui_flags = SD_PATH_DIRECT;
21271 	uip->ui_cmdp  = uscmd;
21272 
21273 	bp = getrbuf(KM_SLEEP);
21274 	bp->b_private = uip;
21275 
21276 	/*
21277 	 * Setup buffer to carry uscsi request.
21278 	 */
21279 	bp->b_flags  = B_BUSY;
21280 	bp->b_bcount = 0;
21281 	bp->b_blkno  = 0;
21282 
21283 	if (is_async == TRUE) {
21284 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
21285 		uip->ui_dkc = *dkc;
21286 	}
21287 
21288 	bp->b_edev = SD_GET_DEV(un);
21289 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
21290 
21291 	/*
21292 	 * Unset un_f_sync_cache_required flag
21293 	 */
21294 	mutex_enter(SD_MUTEX(un));
21295 	un->un_f_sync_cache_required = FALSE;
21296 	mutex_exit(SD_MUTEX(un));
21297 
21298 	(void) sd_uscsi_strategy(bp);
21299 
21300 	/*
21301 	 * If synchronous request, wait for completion
21302 	 * If async just return and let b_iodone callback
21303 	 * cleanup.
21304 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
21305 	 * but it was also incremented in sd_uscsi_strategy(), so
21306 	 * we should be ok.
21307 	 */
21308 	if (is_async == FALSE) {
21309 		(void) biowait(bp);
21310 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
21311 	}
21312 
21313 	return (rval);
21314 }
21315 
21316 
21317 static int
21318 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
21319 {
21320 	struct sd_uscsi_info *uip;
21321 	struct uscsi_cmd *uscmd;
21322 	uint8_t *sense_buf;
21323 	struct sd_lun *un;
21324 	int status;
21325 	union scsi_cdb *cdb;
21326 
21327 	uip = (struct sd_uscsi_info *)(bp->b_private);
21328 	ASSERT(uip != NULL);
21329 
21330 	uscmd = uip->ui_cmdp;
21331 	ASSERT(uscmd != NULL);
21332 
21333 	sense_buf = (uint8_t *)uscmd->uscsi_rqbuf;
21334 	ASSERT(sense_buf != NULL);
21335 
21336 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
21337 	ASSERT(un != NULL);
21338 
21339 	cdb = (union scsi_cdb *)uscmd->uscsi_cdb;
21340 
21341 	status = geterror(bp);
21342 	switch (status) {
21343 	case 0:
21344 		break;	/* Success! */
21345 	case EIO:
21346 		switch (uscmd->uscsi_status) {
21347 		case STATUS_RESERVATION_CONFLICT:
21348 			/* Ignore reservation conflict */
21349 			status = 0;
21350 			goto done;
21351 
21352 		case STATUS_CHECK:
21353 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
21354 			    (scsi_sense_key(sense_buf) ==
21355 			    KEY_ILLEGAL_REQUEST)) {
21356 				/* Ignore Illegal Request error */
21357 				if (cdb->cdb_un.tag&SD_SYNC_NV_BIT) {
21358 					mutex_enter(SD_MUTEX(un));
21359 					un->un_f_sync_nv_supported = FALSE;
21360 					mutex_exit(SD_MUTEX(un));
21361 					status = 0;
21362 					SD_TRACE(SD_LOG_IO, un,
21363 					    "un_f_sync_nv_supported \
21364 					    is set to false.\n");
21365 					goto done;
21366 				}
21367 
21368 				mutex_enter(SD_MUTEX(un));
21369 				un->un_f_sync_cache_supported = FALSE;
21370 				mutex_exit(SD_MUTEX(un));
21371 				SD_TRACE(SD_LOG_IO, un,
21372 				    "sd_send_scsi_SYNCHRONIZE_CACHE_biodone: \
21373 				    un_f_sync_cache_supported set to false \
21374 				    with asc = %x, ascq = %x\n",
21375 				    scsi_sense_asc(sense_buf),
21376 				    scsi_sense_ascq(sense_buf));
21377 				status = ENOTSUP;
21378 				goto done;
21379 			}
21380 			break;
21381 		default:
21382 			break;
21383 		}
21384 		/* FALLTHRU */
21385 	default:
21386 		/*
21387 		 * Turn on the un_f_sync_cache_required flag
21388 		 * since the SYNC CACHE command failed
21389 		 */
21390 		mutex_enter(SD_MUTEX(un));
21391 		un->un_f_sync_cache_required = TRUE;
21392 		mutex_exit(SD_MUTEX(un));
21393 
21394 		/*
21395 		 * Don't log an error message if this device
21396 		 * has removable media.
21397 		 */
21398 		if (!un->un_f_has_removable_media) {
21399 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
21400 			    "SYNCHRONIZE CACHE command failed (%d)\n", status);
21401 		}
21402 		break;
21403 	}
21404 
21405 done:
21406 	if (uip->ui_dkc.dkc_callback != NULL) {
21407 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
21408 	}
21409 
21410 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
21411 	freerbuf(bp);
21412 	kmem_free(uip, sizeof (struct sd_uscsi_info));
21413 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
21414 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
21415 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
21416 
21417 	return (status);
21418 }
21419 
21420 
21421 /*
21422  *    Function: sd_send_scsi_GET_CONFIGURATION
21423  *
21424  * Description: Issues the get configuration command to the device.
21425  *		Called from sd_check_for_writable_cd & sd_get_media_info
21426  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
21427  *   Arguments: ssc
21428  *		ucmdbuf
21429  *		rqbuf
21430  *		rqbuflen
21431  *		bufaddr
21432  *		buflen
21433  *		path_flag
21434  *
21435  * Return Code: 0   - Success
21436  *		errno return code from sd_ssc_send()
21437  *
21438  *     Context: Can sleep. Does not return until command is completed.
21439  *
21440  */
21441 
21442 static int
21443 sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc, struct uscsi_cmd *ucmdbuf,
21444 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen,
21445 	int path_flag)
21446 {
21447 	char	cdb[CDB_GROUP1];
21448 	int	status;
21449 	struct sd_lun	*un;
21450 
21451 	ASSERT(ssc != NULL);
21452 	un = ssc->ssc_un;
21453 	ASSERT(un != NULL);
21454 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21455 	ASSERT(bufaddr != NULL);
21456 	ASSERT(ucmdbuf != NULL);
21457 	ASSERT(rqbuf != NULL);
21458 
21459 	SD_TRACE(SD_LOG_IO, un,
21460 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
21461 
21462 	bzero(cdb, sizeof (cdb));
21463 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21464 	bzero(rqbuf, rqbuflen);
21465 	bzero(bufaddr, buflen);
21466 
21467 	/*
21468 	 * Set up cdb field for the get configuration command.
21469 	 */
21470 	cdb[0] = SCMD_GET_CONFIGURATION;
21471 	cdb[1] = 0x02;  /* Requested Type */
21472 	cdb[8] = SD_PROFILE_HEADER_LEN;
21473 	ucmdbuf->uscsi_cdb = cdb;
21474 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21475 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21476 	ucmdbuf->uscsi_buflen = buflen;
21477 	ucmdbuf->uscsi_timeout = sd_io_time;
21478 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21479 	ucmdbuf->uscsi_rqlen = rqbuflen;
21480 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21481 
21482 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21483 	    UIO_SYSSPACE, path_flag);
21484 
21485 	switch (status) {
21486 	case 0:
21487 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21488 		break;  /* Success! */
21489 	case EIO:
21490 		switch (ucmdbuf->uscsi_status) {
21491 		case STATUS_RESERVATION_CONFLICT:
21492 			status = EACCES;
21493 			break;
21494 		default:
21495 			break;
21496 		}
21497 		break;
21498 	default:
21499 		break;
21500 	}
21501 
21502 	if (status == 0) {
21503 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21504 		    "sd_send_scsi_GET_CONFIGURATION: data",
21505 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21506 	}
21507 
21508 	SD_TRACE(SD_LOG_IO, un,
21509 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
21510 
21511 	return (status);
21512 }
21513 
21514 /*
21515  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
21516  *
21517  * Description: Issues the get configuration command to the device to
21518  *              retrieve a specific feature. Called from
21519  *		sd_check_for_writable_cd & sd_set_mmc_caps.
21520  *   Arguments: ssc
21521  *              ucmdbuf
21522  *              rqbuf
21523  *              rqbuflen
21524  *              bufaddr
21525  *              buflen
21526  *		feature
21527  *
21528  * Return Code: 0   - Success
21529  *              errno return code from sd_ssc_send()
21530  *
21531  *     Context: Can sleep. Does not return until command is completed.
21532  *
21533  */
21534 static int
21535 sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
21536 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
21537 	uchar_t *bufaddr, uint_t buflen, char feature, int path_flag)
21538 {
21539 	char    cdb[CDB_GROUP1];
21540 	int	status;
21541 	struct sd_lun	*un;
21542 
21543 	ASSERT(ssc != NULL);
21544 	un = ssc->ssc_un;
21545 	ASSERT(un != NULL);
21546 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21547 	ASSERT(bufaddr != NULL);
21548 	ASSERT(ucmdbuf != NULL);
21549 	ASSERT(rqbuf != NULL);
21550 
21551 	SD_TRACE(SD_LOG_IO, un,
21552 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
21553 
21554 	bzero(cdb, sizeof (cdb));
21555 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
21556 	bzero(rqbuf, rqbuflen);
21557 	bzero(bufaddr, buflen);
21558 
21559 	/*
21560 	 * Set up cdb field for the get configuration command.
21561 	 */
21562 	cdb[0] = SCMD_GET_CONFIGURATION;
21563 	cdb[1] = 0x02;  /* Requested Type */
21564 	cdb[3] = feature;
21565 	cdb[8] = buflen;
21566 	ucmdbuf->uscsi_cdb = cdb;
21567 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
21568 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
21569 	ucmdbuf->uscsi_buflen = buflen;
21570 	ucmdbuf->uscsi_timeout = sd_io_time;
21571 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
21572 	ucmdbuf->uscsi_rqlen = rqbuflen;
21573 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
21574 
21575 	status = sd_ssc_send(ssc, ucmdbuf, FKIOCTL,
21576 	    UIO_SYSSPACE, path_flag);
21577 
21578 	switch (status) {
21579 	case 0:
21580 
21581 		break;  /* Success! */
21582 	case EIO:
21583 		switch (ucmdbuf->uscsi_status) {
21584 		case STATUS_RESERVATION_CONFLICT:
21585 			status = EACCES;
21586 			break;
21587 		default:
21588 			break;
21589 		}
21590 		break;
21591 	default:
21592 		break;
21593 	}
21594 
21595 	if (status == 0) {
21596 		SD_DUMP_MEMORY(un, SD_LOG_IO,
21597 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
21598 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
21599 	}
21600 
21601 	SD_TRACE(SD_LOG_IO, un,
21602 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
21603 
21604 	return (status);
21605 }
21606 
21607 
21608 /*
21609  *    Function: sd_send_scsi_MODE_SENSE
21610  *
21611  * Description: Utility function for issuing a scsi MODE SENSE command.
21612  *		Note: This routine uses a consistent implementation for Group0,
21613  *		Group1, and Group2 commands across all platforms. ATAPI devices
21614  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21615  *
21616  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21617  *                      structure for this target.
21618  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21619  *			  CDB_GROUP[1|2] (10 byte).
21620  *		bufaddr - buffer for page data retrieved from the target.
21621  *		buflen - size of page to be retrieved.
21622  *		page_code - page code of data to be retrieved from the target.
21623  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21624  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21625  *			to use the USCSI "direct" chain and bypass the normal
21626  *			command waitq.
21627  *
21628  * Return Code: 0   - Success
21629  *		errno return code from sd_ssc_send()
21630  *
21631  *     Context: Can sleep. Does not return until command is completed.
21632  */
21633 
21634 static int
21635 sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21636 	size_t buflen,  uchar_t page_code, int path_flag)
21637 {
21638 	struct	scsi_extended_sense	sense_buf;
21639 	union scsi_cdb		cdb;
21640 	struct uscsi_cmd	ucmd_buf;
21641 	int			status;
21642 	int			headlen;
21643 	struct sd_lun		*un;
21644 
21645 	ASSERT(ssc != NULL);
21646 	un = ssc->ssc_un;
21647 	ASSERT(un != NULL);
21648 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21649 	ASSERT(bufaddr != NULL);
21650 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21651 	    (cdbsize == CDB_GROUP2));
21652 
21653 	SD_TRACE(SD_LOG_IO, un,
21654 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
21655 
21656 	bzero(&cdb, sizeof (cdb));
21657 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21658 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21659 	bzero(bufaddr, buflen);
21660 
21661 	if (cdbsize == CDB_GROUP0) {
21662 		cdb.scc_cmd = SCMD_MODE_SENSE;
21663 		cdb.cdb_opaque[2] = page_code;
21664 		FORMG0COUNT(&cdb, buflen);
21665 		headlen = MODE_HEADER_LENGTH;
21666 	} else {
21667 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
21668 		cdb.cdb_opaque[2] = page_code;
21669 		FORMG1COUNT(&cdb, buflen);
21670 		headlen = MODE_HEADER_LENGTH_GRP2;
21671 	}
21672 
21673 	ASSERT(headlen <= buflen);
21674 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21675 
21676 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21677 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21678 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21679 	ucmd_buf.uscsi_buflen	= buflen;
21680 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21681 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21682 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
21683 	ucmd_buf.uscsi_timeout	= 60;
21684 
21685 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21686 	    UIO_SYSSPACE, path_flag);
21687 
21688 	switch (status) {
21689 	case 0:
21690 		/*
21691 		 * sr_check_wp() uses 0x3f page code and check the header of
21692 		 * mode page to determine if target device is write-protected.
21693 		 * But some USB devices return 0 bytes for 0x3f page code. For
21694 		 * this case, make sure that mode page header is returned at
21695 		 * least.
21696 		 */
21697 		if (buflen - ucmd_buf.uscsi_resid <  headlen) {
21698 			status = EIO;
21699 			sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, -1,
21700 			    "mode page header is not returned");
21701 		}
21702 		break;	/* Success! */
21703 	case EIO:
21704 		switch (ucmd_buf.uscsi_status) {
21705 		case STATUS_RESERVATION_CONFLICT:
21706 			status = EACCES;
21707 			break;
21708 		default:
21709 			break;
21710 		}
21711 		break;
21712 	default:
21713 		break;
21714 	}
21715 
21716 	if (status == 0) {
21717 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
21718 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21719 	}
21720 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
21721 
21722 	return (status);
21723 }
21724 
21725 
21726 /*
21727  *    Function: sd_send_scsi_MODE_SELECT
21728  *
21729  * Description: Utility function for issuing a scsi MODE SELECT command.
21730  *		Note: This routine uses a consistent implementation for Group0,
21731  *		Group1, and Group2 commands across all platforms. ATAPI devices
21732  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
21733  *
21734  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21735  *                      structure for this target.
21736  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
21737  *			  CDB_GROUP[1|2] (10 byte).
21738  *		bufaddr - buffer for page data retrieved from the target.
21739  *		buflen - size of page to be retrieved.
21740  *		save_page - boolean to determin if SP bit should be set.
21741  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21742  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21743  *			to use the USCSI "direct" chain and bypass the normal
21744  *			command waitq.
21745  *
21746  * Return Code: 0   - Success
21747  *		errno return code from sd_ssc_send()
21748  *
21749  *     Context: Can sleep. Does not return until command is completed.
21750  */
21751 
21752 static int
21753 sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize, uchar_t *bufaddr,
21754 	size_t buflen,  uchar_t save_page, int path_flag)
21755 {
21756 	struct	scsi_extended_sense	sense_buf;
21757 	union scsi_cdb		cdb;
21758 	struct uscsi_cmd	ucmd_buf;
21759 	int			status;
21760 	struct sd_lun		*un;
21761 
21762 	ASSERT(ssc != NULL);
21763 	un = ssc->ssc_un;
21764 	ASSERT(un != NULL);
21765 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21766 	ASSERT(bufaddr != NULL);
21767 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
21768 	    (cdbsize == CDB_GROUP2));
21769 
21770 	SD_TRACE(SD_LOG_IO, un,
21771 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
21772 
21773 	bzero(&cdb, sizeof (cdb));
21774 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21775 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21776 
21777 	/* Set the PF bit for many third party drives */
21778 	cdb.cdb_opaque[1] = 0x10;
21779 
21780 	/* Set the savepage(SP) bit if given */
21781 	if (save_page == SD_SAVE_PAGE) {
21782 		cdb.cdb_opaque[1] |= 0x01;
21783 	}
21784 
21785 	if (cdbsize == CDB_GROUP0) {
21786 		cdb.scc_cmd = SCMD_MODE_SELECT;
21787 		FORMG0COUNT(&cdb, buflen);
21788 	} else {
21789 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
21790 		FORMG1COUNT(&cdb, buflen);
21791 	}
21792 
21793 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21794 
21795 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21796 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21797 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
21798 	ucmd_buf.uscsi_buflen	= buflen;
21799 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21800 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21801 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
21802 	ucmd_buf.uscsi_timeout	= 60;
21803 
21804 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21805 	    UIO_SYSSPACE, path_flag);
21806 
21807 	switch (status) {
21808 	case 0:
21809 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21810 		break;	/* Success! */
21811 	case EIO:
21812 		switch (ucmd_buf.uscsi_status) {
21813 		case STATUS_RESERVATION_CONFLICT:
21814 			status = EACCES;
21815 			break;
21816 		default:
21817 			break;
21818 		}
21819 		break;
21820 	default:
21821 		break;
21822 	}
21823 
21824 	if (status == 0) {
21825 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
21826 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21827 	}
21828 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
21829 
21830 	return (status);
21831 }
21832 
21833 
21834 /*
21835  *    Function: sd_send_scsi_RDWR
21836  *
21837  * Description: Issue a scsi READ or WRITE command with the given parameters.
21838  *
21839  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21840  *                      structure for this target.
21841  *		cmd:	 SCMD_READ or SCMD_WRITE
21842  *		bufaddr: Address of caller's buffer to receive the RDWR data
21843  *		buflen:  Length of caller's buffer receive the RDWR data.
21844  *		start_block: Block number for the start of the RDWR operation.
21845  *			 (Assumes target-native block size.)
21846  *		residp:  Pointer to variable to receive the redisual of the
21847  *			 RDWR operation (may be NULL of no residual requested).
21848  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
21849  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
21850  *			to use the USCSI "direct" chain and bypass the normal
21851  *			command waitq.
21852  *
21853  * Return Code: 0   - Success
21854  *		errno return code from sd_ssc_send()
21855  *
21856  *     Context: Can sleep. Does not return until command is completed.
21857  */
21858 
21859 static int
21860 sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
21861 	size_t buflen, daddr_t start_block, int path_flag)
21862 {
21863 	struct	scsi_extended_sense	sense_buf;
21864 	union scsi_cdb		cdb;
21865 	struct uscsi_cmd	ucmd_buf;
21866 	uint32_t		block_count;
21867 	int			status;
21868 	int			cdbsize;
21869 	uchar_t			flag;
21870 	struct sd_lun		*un;
21871 
21872 	ASSERT(ssc != NULL);
21873 	un = ssc->ssc_un;
21874 	ASSERT(un != NULL);
21875 	ASSERT(!mutex_owned(SD_MUTEX(un)));
21876 	ASSERT(bufaddr != NULL);
21877 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
21878 
21879 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
21880 
21881 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
21882 		return (EINVAL);
21883 	}
21884 
21885 	mutex_enter(SD_MUTEX(un));
21886 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
21887 	mutex_exit(SD_MUTEX(un));
21888 
21889 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
21890 
21891 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
21892 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
21893 	    bufaddr, buflen, start_block, block_count);
21894 
21895 	bzero(&cdb, sizeof (cdb));
21896 	bzero(&ucmd_buf, sizeof (ucmd_buf));
21897 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
21898 
21899 	/* Compute CDB size to use */
21900 	if (start_block > 0xffffffff)
21901 		cdbsize = CDB_GROUP4;
21902 	else if ((start_block & 0xFFE00000) ||
21903 	    (un->un_f_cfg_is_atapi == TRUE))
21904 		cdbsize = CDB_GROUP1;
21905 	else
21906 		cdbsize = CDB_GROUP0;
21907 
21908 	switch (cdbsize) {
21909 	case CDB_GROUP0:	/* 6-byte CDBs */
21910 		cdb.scc_cmd = cmd;
21911 		FORMG0ADDR(&cdb, start_block);
21912 		FORMG0COUNT(&cdb, block_count);
21913 		break;
21914 	case CDB_GROUP1:	/* 10-byte CDBs */
21915 		cdb.scc_cmd = cmd | SCMD_GROUP1;
21916 		FORMG1ADDR(&cdb, start_block);
21917 		FORMG1COUNT(&cdb, block_count);
21918 		break;
21919 	case CDB_GROUP4:	/* 16-byte CDBs */
21920 		cdb.scc_cmd = cmd | SCMD_GROUP4;
21921 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
21922 		FORMG4COUNT(&cdb, block_count);
21923 		break;
21924 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
21925 	default:
21926 		/* All others reserved */
21927 		return (EINVAL);
21928 	}
21929 
21930 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
21931 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
21932 
21933 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
21934 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
21935 	ucmd_buf.uscsi_bufaddr	= bufaddr;
21936 	ucmd_buf.uscsi_buflen	= buflen;
21937 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
21938 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
21939 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
21940 	ucmd_buf.uscsi_timeout	= 60;
21941 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
21942 	    UIO_SYSSPACE, path_flag);
21943 
21944 	switch (status) {
21945 	case 0:
21946 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
21947 		break;	/* Success! */
21948 	case EIO:
21949 		switch (ucmd_buf.uscsi_status) {
21950 		case STATUS_RESERVATION_CONFLICT:
21951 			status = EACCES;
21952 			break;
21953 		default:
21954 			break;
21955 		}
21956 		break;
21957 	default:
21958 		break;
21959 	}
21960 
21961 	if (status == 0) {
21962 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
21963 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
21964 	}
21965 
21966 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
21967 
21968 	return (status);
21969 }
21970 
21971 
21972 /*
21973  *    Function: sd_send_scsi_LOG_SENSE
21974  *
21975  * Description: Issue a scsi LOG_SENSE command with the given parameters.
21976  *
21977  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
21978  *                      structure for this target.
21979  *
21980  * Return Code: 0   - Success
21981  *		errno return code from sd_ssc_send()
21982  *
21983  *     Context: Can sleep. Does not return until command is completed.
21984  */
21985 
21986 static int
21987 sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr, uint16_t buflen,
21988 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
21989 	int path_flag)
21990 
21991 {
21992 	struct scsi_extended_sense	sense_buf;
21993 	union scsi_cdb		cdb;
21994 	struct uscsi_cmd	ucmd_buf;
21995 	int			status;
21996 	struct sd_lun		*un;
21997 
21998 	ASSERT(ssc != NULL);
21999 	un = ssc->ssc_un;
22000 	ASSERT(un != NULL);
22001 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22002 
22003 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
22004 
22005 	bzero(&cdb, sizeof (cdb));
22006 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22007 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
22008 
22009 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
22010 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
22011 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
22012 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
22013 	FORMG1COUNT(&cdb, buflen);
22014 
22015 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22016 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22017 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22018 	ucmd_buf.uscsi_buflen	= buflen;
22019 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
22020 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
22021 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
22022 	ucmd_buf.uscsi_timeout	= 60;
22023 
22024 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22025 	    UIO_SYSSPACE, path_flag);
22026 
22027 	switch (status) {
22028 	case 0:
22029 		break;
22030 	case EIO:
22031 		switch (ucmd_buf.uscsi_status) {
22032 		case STATUS_RESERVATION_CONFLICT:
22033 			status = EACCES;
22034 			break;
22035 		case STATUS_CHECK:
22036 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
22037 			    (scsi_sense_key((uint8_t *)&sense_buf) ==
22038 				KEY_ILLEGAL_REQUEST) &&
22039 			    (scsi_sense_asc((uint8_t *)&sense_buf) == 0x24)) {
22040 				/*
22041 				 * ASC 0x24: INVALID FIELD IN CDB
22042 				 */
22043 				switch (page_code) {
22044 				case START_STOP_CYCLE_PAGE:
22045 					/*
22046 					 * The start stop cycle counter is
22047 					 * implemented as page 0x31 in earlier
22048 					 * generation disks. In new generation
22049 					 * disks the start stop cycle counter is
22050 					 * implemented as page 0xE. To properly
22051 					 * handle this case if an attempt for
22052 					 * log page 0xE is made and fails we
22053 					 * will try again using page 0x31.
22054 					 *
22055 					 * Network storage BU committed to
22056 					 * maintain the page 0x31 for this
22057 					 * purpose and will not have any other
22058 					 * page implemented with page code 0x31
22059 					 * until all disks transition to the
22060 					 * standard page.
22061 					 */
22062 					mutex_enter(SD_MUTEX(un));
22063 					un->un_start_stop_cycle_page =
22064 					    START_STOP_CYCLE_VU_PAGE;
22065 					cdb.cdb_opaque[2] =
22066 					    (char)(page_control << 6) |
22067 					    un->un_start_stop_cycle_page;
22068 					mutex_exit(SD_MUTEX(un));
22069 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22070 					status = sd_ssc_send(
22071 					    ssc, &ucmd_buf, FKIOCTL,
22072 					    UIO_SYSSPACE, path_flag);
22073 
22074 					break;
22075 				case TEMPERATURE_PAGE:
22076 					status = ENOTTY;
22077 					break;
22078 				default:
22079 					break;
22080 				}
22081 			}
22082 			break;
22083 		default:
22084 			break;
22085 		}
22086 		break;
22087 	default:
22088 		break;
22089 	}
22090 
22091 	if (status == 0) {
22092 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22093 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
22094 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
22095 	}
22096 
22097 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
22098 
22099 	return (status);
22100 }
22101 
22102 
22103 /*
22104  *    Function: sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
22105  *
22106  * Description: Issue the scsi GET EVENT STATUS NOTIFICATION command.
22107  *
22108  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
22109  *                      structure for this target.
22110  *		bufaddr
22111  *		buflen
22112  *		class_req
22113  *
22114  * Return Code: 0   - Success
22115  *		errno return code from sd_ssc_send()
22116  *
22117  *     Context: Can sleep. Does not return until command is completed.
22118  */
22119 
22120 static int
22121 sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc, uchar_t *bufaddr,
22122 	size_t buflen, uchar_t class_req)
22123 {
22124 	union scsi_cdb		cdb;
22125 	struct uscsi_cmd	ucmd_buf;
22126 	int			status;
22127 	struct sd_lun		*un;
22128 
22129 	ASSERT(ssc != NULL);
22130 	un = ssc->ssc_un;
22131 	ASSERT(un != NULL);
22132 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22133 	ASSERT(bufaddr != NULL);
22134 
22135 	SD_TRACE(SD_LOG_IO, un,
22136 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: entry: un:0x%p\n", un);
22137 
22138 	bzero(&cdb, sizeof (cdb));
22139 	bzero(&ucmd_buf, sizeof (ucmd_buf));
22140 	bzero(bufaddr, buflen);
22141 
22142 	cdb.scc_cmd = SCMD_GET_EVENT_STATUS_NOTIFICATION;
22143 	cdb.cdb_opaque[1] = 1; /* polled */
22144 	cdb.cdb_opaque[4] = class_req;
22145 	FORMG1COUNT(&cdb, buflen);
22146 
22147 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
22148 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
22149 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
22150 	ucmd_buf.uscsi_buflen	= buflen;
22151 	ucmd_buf.uscsi_rqbuf	= NULL;
22152 	ucmd_buf.uscsi_rqlen	= 0;
22153 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
22154 	ucmd_buf.uscsi_timeout	= 60;
22155 
22156 	status = sd_ssc_send(ssc, &ucmd_buf, FKIOCTL,
22157 	    UIO_SYSSPACE, SD_PATH_DIRECT);
22158 
22159 	/*
22160 	 * Only handle status == 0, the upper-level caller
22161 	 * will put different assessment based on the context.
22162 	 */
22163 	if (status == 0) {
22164 		sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22165 
22166 		if (ucmd_buf.uscsi_resid != 0) {
22167 			status = EIO;
22168 		}
22169 	}
22170 
22171 	SD_TRACE(SD_LOG_IO, un,
22172 	    "sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION: exit\n");
22173 
22174 	return (status);
22175 }
22176 
22177 
22178 static boolean_t
22179 sd_gesn_media_data_valid(uchar_t *data)
22180 {
22181 	uint16_t			len;
22182 
22183 	len = (data[1] << 8) | data[0];
22184 	return ((len >= 6) &&
22185 	    ((data[2] & SD_GESN_HEADER_NEA) == 0) &&
22186 	    ((data[2] & SD_GESN_HEADER_CLASS) == SD_GESN_MEDIA_CLASS) &&
22187 	    ((data[3] & (1 << SD_GESN_MEDIA_CLASS)) != 0));
22188 }
22189 
22190 
22191 /*
22192  *    Function: sdioctl
22193  *
22194  * Description: Driver's ioctl(9e) entry point function.
22195  *
22196  *   Arguments: dev     - device number
22197  *		cmd     - ioctl operation to be performed
22198  *		arg     - user argument, contains data to be set or reference
22199  *			  parameter for get
22200  *		flag    - bit flag, indicating open settings, 32/64 bit type
22201  *		cred_p  - user credential pointer
22202  *		rval_p  - calling process return value (OPT)
22203  *
22204  * Return Code: EINVAL
22205  *		ENOTTY
22206  *		ENXIO
22207  *		EIO
22208  *		EFAULT
22209  *		ENOTSUP
22210  *		EPERM
22211  *
22212  *     Context: Called from the device switch at normal priority.
22213  */
22214 
22215 static int
22216 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
22217 {
22218 	struct sd_lun	*un = NULL;
22219 	int		err = 0;
22220 	int		i = 0;
22221 	cred_t		*cr;
22222 	int		tmprval = EINVAL;
22223 	boolean_t	is_valid;
22224 	sd_ssc_t	*ssc;
22225 
22226 	/*
22227 	 * All device accesses go thru sdstrategy where we check on suspend
22228 	 * status
22229 	 */
22230 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22231 		return (ENXIO);
22232 	}
22233 
22234 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22235 
22236 	/* Initialize sd_ssc_t for internal uscsi commands */
22237 	ssc = sd_ssc_init(un);
22238 
22239 	is_valid = SD_IS_VALID_LABEL(un);
22240 
22241 	/*
22242 	 * Moved this wait from sd_uscsi_strategy to here for
22243 	 * reasons of deadlock prevention. Internal driver commands,
22244 	 * specifically those to change a devices power level, result
22245 	 * in a call to sd_uscsi_strategy.
22246 	 */
22247 	mutex_enter(SD_MUTEX(un));
22248 	while ((un->un_state == SD_STATE_SUSPENDED) ||
22249 	    (un->un_state == SD_STATE_PM_CHANGING)) {
22250 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
22251 	}
22252 	/*
22253 	 * Twiddling the counter here protects commands from now
22254 	 * through to the top of sd_uscsi_strategy. Without the
22255 	 * counter inc. a power down, for example, could get in
22256 	 * after the above check for state is made and before
22257 	 * execution gets to the top of sd_uscsi_strategy.
22258 	 * That would cause problems.
22259 	 */
22260 	un->un_ncmds_in_driver++;
22261 
22262 	if (!is_valid &&
22263 	    (flag & (FNDELAY | FNONBLOCK))) {
22264 		switch (cmd) {
22265 		case DKIOCGGEOM:	/* SD_PATH_DIRECT */
22266 		case DKIOCGVTOC:
22267 		case DKIOCGEXTVTOC:
22268 		case DKIOCGAPART:
22269 		case DKIOCPARTINFO:
22270 		case DKIOCEXTPARTINFO:
22271 		case DKIOCSGEOM:
22272 		case DKIOCSAPART:
22273 		case DKIOCGETEFI:
22274 		case DKIOCPARTITION:
22275 		case DKIOCSVTOC:
22276 		case DKIOCSEXTVTOC:
22277 		case DKIOCSETEFI:
22278 		case DKIOCGMBOOT:
22279 		case DKIOCSMBOOT:
22280 		case DKIOCG_PHYGEOM:
22281 		case DKIOCG_VIRTGEOM:
22282 #if defined(__i386) || defined(__amd64)
22283 		case DKIOCSETEXTPART:
22284 #endif
22285 			/* let cmlb handle it */
22286 			goto skip_ready_valid;
22287 
22288 		case CDROMPAUSE:
22289 		case CDROMRESUME:
22290 		case CDROMPLAYMSF:
22291 		case CDROMPLAYTRKIND:
22292 		case CDROMREADTOCHDR:
22293 		case CDROMREADTOCENTRY:
22294 		case CDROMSTOP:
22295 		case CDROMSTART:
22296 		case CDROMVOLCTRL:
22297 		case CDROMSUBCHNL:
22298 		case CDROMREADMODE2:
22299 		case CDROMREADMODE1:
22300 		case CDROMREADOFFSET:
22301 		case CDROMSBLKMODE:
22302 		case CDROMGBLKMODE:
22303 		case CDROMGDRVSPEED:
22304 		case CDROMSDRVSPEED:
22305 		case CDROMCDDA:
22306 		case CDROMCDXA:
22307 		case CDROMSUBCODE:
22308 			if (!ISCD(un)) {
22309 				un->un_ncmds_in_driver--;
22310 				ASSERT(un->un_ncmds_in_driver >= 0);
22311 				mutex_exit(SD_MUTEX(un));
22312 				err = ENOTTY;
22313 				goto done_without_assess;
22314 			}
22315 			break;
22316 		case FDEJECT:
22317 		case DKIOCEJECT:
22318 		case CDROMEJECT:
22319 			if (!un->un_f_eject_media_supported) {
22320 				un->un_ncmds_in_driver--;
22321 				ASSERT(un->un_ncmds_in_driver >= 0);
22322 				mutex_exit(SD_MUTEX(un));
22323 				err = ENOTTY;
22324 				goto done_without_assess;
22325 			}
22326 			break;
22327 		case DKIOCFLUSHWRITECACHE:
22328 			mutex_exit(SD_MUTEX(un));
22329 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22330 			if (err != 0) {
22331 				mutex_enter(SD_MUTEX(un));
22332 				un->un_ncmds_in_driver--;
22333 				ASSERT(un->un_ncmds_in_driver >= 0);
22334 				mutex_exit(SD_MUTEX(un));
22335 				err = EIO;
22336 				goto done_quick_assess;
22337 			}
22338 			mutex_enter(SD_MUTEX(un));
22339 			/* FALLTHROUGH */
22340 		case DKIOCREMOVABLE:
22341 		case DKIOCHOTPLUGGABLE:
22342 		case DKIOCINFO:
22343 		case DKIOCGMEDIAINFO:
22344 		case DKIOCGMEDIAINFOEXT:
22345 		case DKIOCSOLIDSTATE:
22346 		case MHIOCENFAILFAST:
22347 		case MHIOCSTATUS:
22348 		case MHIOCTKOWN:
22349 		case MHIOCRELEASE:
22350 		case MHIOCGRP_INKEYS:
22351 		case MHIOCGRP_INRESV:
22352 		case MHIOCGRP_REGISTER:
22353 		case MHIOCGRP_CLEAR:
22354 		case MHIOCGRP_RESERVE:
22355 		case MHIOCGRP_PREEMPTANDABORT:
22356 		case MHIOCGRP_REGISTERANDIGNOREKEY:
22357 		case CDROMCLOSETRAY:
22358 		case USCSICMD:
22359 			goto skip_ready_valid;
22360 		default:
22361 			break;
22362 		}
22363 
22364 		mutex_exit(SD_MUTEX(un));
22365 		err = sd_ready_and_valid(ssc, SDPART(dev));
22366 		mutex_enter(SD_MUTEX(un));
22367 
22368 		if (err != SD_READY_VALID) {
22369 			switch (cmd) {
22370 			case DKIOCSTATE:
22371 			case CDROMGDRVSPEED:
22372 			case CDROMSDRVSPEED:
22373 			case FDEJECT:	/* for eject command */
22374 			case DKIOCEJECT:
22375 			case CDROMEJECT:
22376 			case DKIOCREMOVABLE:
22377 			case DKIOCHOTPLUGGABLE:
22378 				break;
22379 			default:
22380 				if (un->un_f_has_removable_media) {
22381 					err = ENXIO;
22382 				} else {
22383 				/* Do not map SD_RESERVED_BY_OTHERS to EIO */
22384 					if (err == SD_RESERVED_BY_OTHERS) {
22385 						err = EACCES;
22386 					} else {
22387 						err = EIO;
22388 					}
22389 				}
22390 				un->un_ncmds_in_driver--;
22391 				ASSERT(un->un_ncmds_in_driver >= 0);
22392 				mutex_exit(SD_MUTEX(un));
22393 
22394 				goto done_without_assess;
22395 			}
22396 		}
22397 	}
22398 
22399 skip_ready_valid:
22400 	mutex_exit(SD_MUTEX(un));
22401 
22402 	switch (cmd) {
22403 	case DKIOCINFO:
22404 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
22405 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
22406 		break;
22407 
22408 	case DKIOCGMEDIAINFO:
22409 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
22410 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
22411 		break;
22412 
22413 	case DKIOCGMEDIAINFOEXT:
22414 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFOEXT\n");
22415 		err = sd_get_media_info_ext(dev, (caddr_t)arg, flag);
22416 		break;
22417 
22418 	case DKIOCGGEOM:
22419 	case DKIOCGVTOC:
22420 	case DKIOCGEXTVTOC:
22421 	case DKIOCGAPART:
22422 	case DKIOCPARTINFO:
22423 	case DKIOCEXTPARTINFO:
22424 	case DKIOCSGEOM:
22425 	case DKIOCSAPART:
22426 	case DKIOCGETEFI:
22427 	case DKIOCPARTITION:
22428 	case DKIOCSVTOC:
22429 	case DKIOCSEXTVTOC:
22430 	case DKIOCSETEFI:
22431 	case DKIOCGMBOOT:
22432 	case DKIOCSMBOOT:
22433 	case DKIOCG_PHYGEOM:
22434 	case DKIOCG_VIRTGEOM:
22435 #if defined(__i386) || defined(__amd64)
22436 	case DKIOCSETEXTPART:
22437 #endif
22438 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOC %d\n", cmd);
22439 
22440 		/* TUR should spin up */
22441 
22442 		if (un->un_f_has_removable_media)
22443 			err = sd_send_scsi_TEST_UNIT_READY(ssc,
22444 			    SD_CHECK_FOR_MEDIA);
22445 
22446 		else
22447 			err = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
22448 
22449 		if (err != 0)
22450 			goto done_with_assess;
22451 
22452 		err = cmlb_ioctl(un->un_cmlbhandle, dev,
22453 		    cmd, arg, flag, cred_p, rval_p, (void *)SD_PATH_DIRECT);
22454 
22455 		if ((err == 0) &&
22456 		    ((cmd == DKIOCSETEFI) ||
22457 		    (un->un_f_pkstats_enabled) &&
22458 		    (cmd == DKIOCSAPART || cmd == DKIOCSVTOC ||
22459 		    cmd == DKIOCSEXTVTOC))) {
22460 
22461 			tmprval = cmlb_validate(un->un_cmlbhandle, CMLB_SILENT,
22462 			    (void *)SD_PATH_DIRECT);
22463 			if ((tmprval == 0) && un->un_f_pkstats_enabled) {
22464 				sd_set_pstats(un);
22465 				SD_TRACE(SD_LOG_IO_PARTITION, un,
22466 				    "sd_ioctl: un:0x%p pstats created and "
22467 				    "set\n", un);
22468 			}
22469 		}
22470 
22471 		if ((cmd == DKIOCSVTOC || cmd == DKIOCSEXTVTOC) ||
22472 		    ((cmd == DKIOCSETEFI) && (tmprval == 0))) {
22473 
22474 			mutex_enter(SD_MUTEX(un));
22475 			if (un->un_f_devid_supported &&
22476 			    (un->un_f_opt_fab_devid == TRUE)) {
22477 				if (un->un_devid == NULL) {
22478 					sd_register_devid(ssc, SD_DEVINFO(un),
22479 					    SD_TARGET_IS_UNRESERVED);
22480 				} else {
22481 					/*
22482 					 * The device id for this disk
22483 					 * has been fabricated. The
22484 					 * device id must be preserved
22485 					 * by writing it back out to
22486 					 * disk.
22487 					 */
22488 					if (sd_write_deviceid(ssc) != 0) {
22489 						ddi_devid_free(un->un_devid);
22490 						un->un_devid = NULL;
22491 					}
22492 				}
22493 			}
22494 			mutex_exit(SD_MUTEX(un));
22495 		}
22496 
22497 		break;
22498 
22499 	case DKIOCLOCK:
22500 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
22501 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
22502 		    SD_PATH_STANDARD);
22503 		goto done_with_assess;
22504 
22505 	case DKIOCUNLOCK:
22506 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
22507 		err = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
22508 		    SD_PATH_STANDARD);
22509 		goto done_with_assess;
22510 
22511 	case DKIOCSTATE: {
22512 		enum dkio_state		state;
22513 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
22514 
22515 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
22516 			err = EFAULT;
22517 		} else {
22518 			err = sd_check_media(dev, state);
22519 			if (err == 0) {
22520 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
22521 				    sizeof (int), flag) != 0)
22522 					err = EFAULT;
22523 			}
22524 		}
22525 		break;
22526 	}
22527 
22528 	case DKIOCREMOVABLE:
22529 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
22530 		i = un->un_f_has_removable_media ? 1 : 0;
22531 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22532 			err = EFAULT;
22533 		} else {
22534 			err = 0;
22535 		}
22536 		break;
22537 
22538 	case DKIOCSOLIDSTATE:
22539 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSOLIDSTATE\n");
22540 		i = un->un_f_is_solid_state ? 1 : 0;
22541 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22542 			err = EFAULT;
22543 		} else {
22544 			err = 0;
22545 		}
22546 		break;
22547 
22548 	case DKIOCHOTPLUGGABLE:
22549 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
22550 		i = un->un_f_is_hotpluggable ? 1 : 0;
22551 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22552 			err = EFAULT;
22553 		} else {
22554 			err = 0;
22555 		}
22556 		break;
22557 
22558 	case DKIOCREADONLY:
22559 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREADONLY\n");
22560 		i = 0;
22561 		if ((ISCD(un) && !un->un_f_mmc_writable_media) ||
22562 		    (sr_check_wp(dev) != 0)) {
22563 			i = 1;
22564 		}
22565 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
22566 			err = EFAULT;
22567 		} else {
22568 			err = 0;
22569 		}
22570 		break;
22571 
22572 	case DKIOCGTEMPERATURE:
22573 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
22574 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
22575 		break;
22576 
22577 	case MHIOCENFAILFAST:
22578 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
22579 		if ((err = drv_priv(cred_p)) == 0) {
22580 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
22581 		}
22582 		break;
22583 
22584 	case MHIOCTKOWN:
22585 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
22586 		if ((err = drv_priv(cred_p)) == 0) {
22587 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
22588 		}
22589 		break;
22590 
22591 	case MHIOCRELEASE:
22592 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
22593 		if ((err = drv_priv(cred_p)) == 0) {
22594 			err = sd_mhdioc_release(dev);
22595 		}
22596 		break;
22597 
22598 	case MHIOCSTATUS:
22599 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
22600 		if ((err = drv_priv(cred_p)) == 0) {
22601 			switch (sd_send_scsi_TEST_UNIT_READY(ssc, 0)) {
22602 			case 0:
22603 				err = 0;
22604 				break;
22605 			case EACCES:
22606 				*rval_p = 1;
22607 				err = 0;
22608 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
22609 				break;
22610 			default:
22611 				err = EIO;
22612 				goto done_with_assess;
22613 			}
22614 		}
22615 		break;
22616 
22617 	case MHIOCQRESERVE:
22618 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
22619 		if ((err = drv_priv(cred_p)) == 0) {
22620 			err = sd_reserve_release(dev, SD_RESERVE);
22621 		}
22622 		break;
22623 
22624 	case MHIOCREREGISTERDEVID:
22625 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
22626 		if (drv_priv(cred_p) == EPERM) {
22627 			err = EPERM;
22628 		} else if (!un->un_f_devid_supported) {
22629 			err = ENOTTY;
22630 		} else {
22631 			err = sd_mhdioc_register_devid(dev);
22632 		}
22633 		break;
22634 
22635 	case MHIOCGRP_INKEYS:
22636 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
22637 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22638 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22639 				err = ENOTSUP;
22640 			} else {
22641 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
22642 				    flag);
22643 			}
22644 		}
22645 		break;
22646 
22647 	case MHIOCGRP_INRESV:
22648 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
22649 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
22650 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22651 				err = ENOTSUP;
22652 			} else {
22653 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
22654 			}
22655 		}
22656 		break;
22657 
22658 	case MHIOCGRP_REGISTER:
22659 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
22660 		if ((err = drv_priv(cred_p)) != EPERM) {
22661 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22662 				err = ENOTSUP;
22663 			} else if (arg != NULL) {
22664 				mhioc_register_t reg;
22665 				if (ddi_copyin((void *)arg, &reg,
22666 				    sizeof (mhioc_register_t), flag) != 0) {
22667 					err = EFAULT;
22668 				} else {
22669 					err =
22670 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22671 					    ssc, SD_SCSI3_REGISTER,
22672 					    (uchar_t *)&reg);
22673 					if (err != 0)
22674 						goto done_with_assess;
22675 				}
22676 			}
22677 		}
22678 		break;
22679 
22680 	case MHIOCGRP_CLEAR:
22681 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_CLEAR\n");
22682 		if ((err = drv_priv(cred_p)) != EPERM) {
22683 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22684 				err = ENOTSUP;
22685 			} else if (arg != NULL) {
22686 				mhioc_register_t reg;
22687 				if (ddi_copyin((void *)arg, &reg,
22688 				    sizeof (mhioc_register_t), flag) != 0) {
22689 					err = EFAULT;
22690 				} else {
22691 					err =
22692 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22693 					    ssc, SD_SCSI3_CLEAR,
22694 					    (uchar_t *)&reg);
22695 					if (err != 0)
22696 						goto done_with_assess;
22697 				}
22698 			}
22699 		}
22700 		break;
22701 
22702 	case MHIOCGRP_RESERVE:
22703 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
22704 		if ((err = drv_priv(cred_p)) != EPERM) {
22705 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22706 				err = ENOTSUP;
22707 			} else if (arg != NULL) {
22708 				mhioc_resv_desc_t resv_desc;
22709 				if (ddi_copyin((void *)arg, &resv_desc,
22710 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
22711 					err = EFAULT;
22712 				} else {
22713 					err =
22714 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22715 					    ssc, SD_SCSI3_RESERVE,
22716 					    (uchar_t *)&resv_desc);
22717 					if (err != 0)
22718 						goto done_with_assess;
22719 				}
22720 			}
22721 		}
22722 		break;
22723 
22724 	case MHIOCGRP_PREEMPTANDABORT:
22725 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
22726 		if ((err = drv_priv(cred_p)) != EPERM) {
22727 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22728 				err = ENOTSUP;
22729 			} else if (arg != NULL) {
22730 				mhioc_preemptandabort_t preempt_abort;
22731 				if (ddi_copyin((void *)arg, &preempt_abort,
22732 				    sizeof (mhioc_preemptandabort_t),
22733 				    flag) != 0) {
22734 					err = EFAULT;
22735 				} else {
22736 					err =
22737 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22738 					    ssc, SD_SCSI3_PREEMPTANDABORT,
22739 					    (uchar_t *)&preempt_abort);
22740 					if (err != 0)
22741 						goto done_with_assess;
22742 				}
22743 			}
22744 		}
22745 		break;
22746 
22747 	case MHIOCGRP_REGISTERANDIGNOREKEY:
22748 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTERANDIGNOREKEY\n");
22749 		if ((err = drv_priv(cred_p)) != EPERM) {
22750 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
22751 				err = ENOTSUP;
22752 			} else if (arg != NULL) {
22753 				mhioc_registerandignorekey_t r_and_i;
22754 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
22755 				    sizeof (mhioc_registerandignorekey_t),
22756 				    flag) != 0) {
22757 					err = EFAULT;
22758 				} else {
22759 					err =
22760 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
22761 					    ssc, SD_SCSI3_REGISTERANDIGNOREKEY,
22762 					    (uchar_t *)&r_and_i);
22763 					if (err != 0)
22764 						goto done_with_assess;
22765 				}
22766 			}
22767 		}
22768 		break;
22769 
22770 	case USCSICMD:
22771 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
22772 		cr = ddi_get_cred();
22773 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
22774 			err = EPERM;
22775 		} else {
22776 			enum uio_seg	uioseg;
22777 
22778 			uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE :
22779 			    UIO_USERSPACE;
22780 			if (un->un_f_format_in_progress == TRUE) {
22781 				err = EAGAIN;
22782 				break;
22783 			}
22784 
22785 			err = sd_ssc_send(ssc,
22786 			    (struct uscsi_cmd *)arg,
22787 			    flag, uioseg, SD_PATH_STANDARD);
22788 			if (err != 0)
22789 				goto done_with_assess;
22790 			else
22791 				sd_ssc_assessment(ssc, SD_FMT_STANDARD);
22792 		}
22793 		break;
22794 
22795 	case CDROMPAUSE:
22796 	case CDROMRESUME:
22797 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
22798 		if (!ISCD(un)) {
22799 			err = ENOTTY;
22800 		} else {
22801 			err = sr_pause_resume(dev, cmd);
22802 		}
22803 		break;
22804 
22805 	case CDROMPLAYMSF:
22806 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
22807 		if (!ISCD(un)) {
22808 			err = ENOTTY;
22809 		} else {
22810 			err = sr_play_msf(dev, (caddr_t)arg, flag);
22811 		}
22812 		break;
22813 
22814 	case CDROMPLAYTRKIND:
22815 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
22816 #if defined(__i386) || defined(__amd64)
22817 		/*
22818 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
22819 		 */
22820 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22821 #else
22822 		if (!ISCD(un)) {
22823 #endif
22824 			err = ENOTTY;
22825 		} else {
22826 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
22827 		}
22828 		break;
22829 
22830 	case CDROMREADTOCHDR:
22831 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
22832 		if (!ISCD(un)) {
22833 			err = ENOTTY;
22834 		} else {
22835 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
22836 		}
22837 		break;
22838 
22839 	case CDROMREADTOCENTRY:
22840 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
22841 		if (!ISCD(un)) {
22842 			err = ENOTTY;
22843 		} else {
22844 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
22845 		}
22846 		break;
22847 
22848 	case CDROMSTOP:
22849 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
22850 		if (!ISCD(un)) {
22851 			err = ENOTTY;
22852 		} else {
22853 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22854 			    SD_TARGET_STOP, SD_PATH_STANDARD);
22855 			goto done_with_assess;
22856 		}
22857 		break;
22858 
22859 	case CDROMSTART:
22860 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
22861 		if (!ISCD(un)) {
22862 			err = ENOTTY;
22863 		} else {
22864 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22865 			    SD_TARGET_START, SD_PATH_STANDARD);
22866 			goto done_with_assess;
22867 		}
22868 		break;
22869 
22870 	case CDROMCLOSETRAY:
22871 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
22872 		if (!ISCD(un)) {
22873 			err = ENOTTY;
22874 		} else {
22875 			err = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
22876 			    SD_TARGET_CLOSE, SD_PATH_STANDARD);
22877 			goto done_with_assess;
22878 		}
22879 		break;
22880 
22881 	case FDEJECT:	/* for eject command */
22882 	case DKIOCEJECT:
22883 	case CDROMEJECT:
22884 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
22885 		if (!un->un_f_eject_media_supported) {
22886 			err = ENOTTY;
22887 		} else {
22888 			err = sr_eject(dev);
22889 		}
22890 		break;
22891 
22892 	case CDROMVOLCTRL:
22893 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
22894 		if (!ISCD(un)) {
22895 			err = ENOTTY;
22896 		} else {
22897 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
22898 		}
22899 		break;
22900 
22901 	case CDROMSUBCHNL:
22902 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
22903 		if (!ISCD(un)) {
22904 			err = ENOTTY;
22905 		} else {
22906 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
22907 		}
22908 		break;
22909 
22910 	case CDROMREADMODE2:
22911 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
22912 		if (!ISCD(un)) {
22913 			err = ENOTTY;
22914 		} else if (un->un_f_cfg_is_atapi == TRUE) {
22915 			/*
22916 			 * If the drive supports READ CD, use that instead of
22917 			 * switching the LBA size via a MODE SELECT
22918 			 * Block Descriptor
22919 			 */
22920 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
22921 		} else {
22922 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
22923 		}
22924 		break;
22925 
22926 	case CDROMREADMODE1:
22927 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
22928 		if (!ISCD(un)) {
22929 			err = ENOTTY;
22930 		} else {
22931 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
22932 		}
22933 		break;
22934 
22935 	case CDROMREADOFFSET:
22936 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
22937 		if (!ISCD(un)) {
22938 			err = ENOTTY;
22939 		} else {
22940 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
22941 			    flag);
22942 		}
22943 		break;
22944 
22945 	case CDROMSBLKMODE:
22946 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
22947 		/*
22948 		 * There is no means of changing block size in case of atapi
22949 		 * drives, thus return ENOTTY if drive type is atapi
22950 		 */
22951 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
22952 			err = ENOTTY;
22953 		} else if (un->un_f_mmc_cap == TRUE) {
22954 
22955 			/*
22956 			 * MMC Devices do not support changing the
22957 			 * logical block size
22958 			 *
22959 			 * Note: EINVAL is being returned instead of ENOTTY to
22960 			 * maintain consistancy with the original mmc
22961 			 * driver update.
22962 			 */
22963 			err = EINVAL;
22964 		} else {
22965 			mutex_enter(SD_MUTEX(un));
22966 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
22967 			    (un->un_ncmds_in_transport > 0)) {
22968 				mutex_exit(SD_MUTEX(un));
22969 				err = EINVAL;
22970 			} else {
22971 				mutex_exit(SD_MUTEX(un));
22972 				err = sr_change_blkmode(dev, cmd, arg, flag);
22973 			}
22974 		}
22975 		break;
22976 
22977 	case CDROMGBLKMODE:
22978 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
22979 		if (!ISCD(un)) {
22980 			err = ENOTTY;
22981 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
22982 		    (un->un_f_blockcount_is_valid != FALSE)) {
22983 			/*
22984 			 * Drive is an ATAPI drive so return target block
22985 			 * size for ATAPI drives since we cannot change the
22986 			 * blocksize on ATAPI drives. Used primarily to detect
22987 			 * if an ATAPI cdrom is present.
22988 			 */
22989 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
22990 			    sizeof (int), flag) != 0) {
22991 				err = EFAULT;
22992 			} else {
22993 				err = 0;
22994 			}
22995 
22996 		} else {
22997 			/*
22998 			 * Drive supports changing block sizes via a Mode
22999 			 * Select.
23000 			 */
23001 			err = sr_change_blkmode(dev, cmd, arg, flag);
23002 		}
23003 		break;
23004 
23005 	case CDROMGDRVSPEED:
23006 	case CDROMSDRVSPEED:
23007 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
23008 		if (!ISCD(un)) {
23009 			err = ENOTTY;
23010 		} else if (un->un_f_mmc_cap == TRUE) {
23011 			/*
23012 			 * Note: In the future the driver implementation
23013 			 * for getting and
23014 			 * setting cd speed should entail:
23015 			 * 1) If non-mmc try the Toshiba mode page
23016 			 *    (sr_change_speed)
23017 			 * 2) If mmc but no support for Real Time Streaming try
23018 			 *    the SET CD SPEED (0xBB) command
23019 			 *   (sr_atapi_change_speed)
23020 			 * 3) If mmc and support for Real Time Streaming
23021 			 *    try the GET PERFORMANCE and SET STREAMING
23022 			 *    commands (not yet implemented, 4380808)
23023 			 */
23024 			/*
23025 			 * As per recent MMC spec, CD-ROM speed is variable
23026 			 * and changes with LBA. Since there is no such
23027 			 * things as drive speed now, fail this ioctl.
23028 			 *
23029 			 * Note: EINVAL is returned for consistancy of original
23030 			 * implementation which included support for getting
23031 			 * the drive speed of mmc devices but not setting
23032 			 * the drive speed. Thus EINVAL would be returned
23033 			 * if a set request was made for an mmc device.
23034 			 * We no longer support get or set speed for
23035 			 * mmc but need to remain consistent with regard
23036 			 * to the error code returned.
23037 			 */
23038 			err = EINVAL;
23039 		} else if (un->un_f_cfg_is_atapi == TRUE) {
23040 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
23041 		} else {
23042 			err = sr_change_speed(dev, cmd, arg, flag);
23043 		}
23044 		break;
23045 
23046 	case CDROMCDDA:
23047 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
23048 		if (!ISCD(un)) {
23049 			err = ENOTTY;
23050 		} else {
23051 			err = sr_read_cdda(dev, (void *)arg, flag);
23052 		}
23053 		break;
23054 
23055 	case CDROMCDXA:
23056 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
23057 		if (!ISCD(un)) {
23058 			err = ENOTTY;
23059 		} else {
23060 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
23061 		}
23062 		break;
23063 
23064 	case CDROMSUBCODE:
23065 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
23066 		if (!ISCD(un)) {
23067 			err = ENOTTY;
23068 		} else {
23069 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
23070 		}
23071 		break;
23072 
23073 
23074 #ifdef SDDEBUG
23075 /* RESET/ABORTS testing ioctls */
23076 	case DKIOCRESET: {
23077 		int	reset_level;
23078 
23079 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
23080 			err = EFAULT;
23081 		} else {
23082 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
23083 			    "reset_level = 0x%lx\n", reset_level);
23084 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
23085 				err = 0;
23086 			} else {
23087 				err = EIO;
23088 			}
23089 		}
23090 		break;
23091 	}
23092 
23093 	case DKIOCABORT:
23094 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
23095 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
23096 			err = 0;
23097 		} else {
23098 			err = EIO;
23099 		}
23100 		break;
23101 #endif
23102 
23103 #ifdef SD_FAULT_INJECTION
23104 /* SDIOC FaultInjection testing ioctls */
23105 	case SDIOCSTART:
23106 	case SDIOCSTOP:
23107 	case SDIOCINSERTPKT:
23108 	case SDIOCINSERTXB:
23109 	case SDIOCINSERTUN:
23110 	case SDIOCINSERTARQ:
23111 	case SDIOCPUSH:
23112 	case SDIOCRETRIEVE:
23113 	case SDIOCRUN:
23114 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
23115 		    "SDIOC detected cmd:0x%X:\n", cmd);
23116 		/* call error generator */
23117 		sd_faultinjection_ioctl(cmd, arg, un);
23118 		err = 0;
23119 		break;
23120 
23121 #endif /* SD_FAULT_INJECTION */
23122 
23123 	case DKIOCFLUSHWRITECACHE:
23124 		{
23125 			struct dk_callback *dkc = (struct dk_callback *)arg;
23126 
23127 			mutex_enter(SD_MUTEX(un));
23128 			if (!un->un_f_sync_cache_supported ||
23129 			    !un->un_f_write_cache_enabled) {
23130 				err = un->un_f_sync_cache_supported ?
23131 				    0 : ENOTSUP;
23132 				mutex_exit(SD_MUTEX(un));
23133 				if ((flag & FKIOCTL) && dkc != NULL &&
23134 				    dkc->dkc_callback != NULL) {
23135 					(*dkc->dkc_callback)(dkc->dkc_cookie,
23136 					    err);
23137 					/*
23138 					 * Did callback and reported error.
23139 					 * Since we did a callback, ioctl
23140 					 * should return 0.
23141 					 */
23142 					err = 0;
23143 				}
23144 				break;
23145 			}
23146 			mutex_exit(SD_MUTEX(un));
23147 
23148 			if ((flag & FKIOCTL) && dkc != NULL &&
23149 			    dkc->dkc_callback != NULL) {
23150 				/* async SYNC CACHE request */
23151 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
23152 			} else {
23153 				/* synchronous SYNC CACHE request */
23154 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
23155 			}
23156 		}
23157 		break;
23158 
23159 	case DKIOCGETWCE: {
23160 
23161 		int wce;
23162 
23163 		if ((err = sd_get_write_cache_enabled(ssc, &wce)) != 0) {
23164 			break;
23165 		}
23166 
23167 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
23168 			err = EFAULT;
23169 		}
23170 		break;
23171 	}
23172 
23173 	case DKIOCSETWCE: {
23174 
23175 		int wce, sync_supported;
23176 		int cur_wce = 0;
23177 
23178 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
23179 			err = EFAULT;
23180 			break;
23181 		}
23182 
23183 		/*
23184 		 * Synchronize multiple threads trying to enable
23185 		 * or disable the cache via the un_f_wcc_cv
23186 		 * condition variable.
23187 		 */
23188 		mutex_enter(SD_MUTEX(un));
23189 
23190 		/*
23191 		 * Don't allow the cache to be enabled if the
23192 		 * config file has it disabled.
23193 		 */
23194 		if (un->un_f_opt_disable_cache && wce) {
23195 			mutex_exit(SD_MUTEX(un));
23196 			err = EINVAL;
23197 			break;
23198 		}
23199 
23200 		/*
23201 		 * Wait for write cache change in progress
23202 		 * bit to be clear before proceeding.
23203 		 */
23204 		while (un->un_f_wcc_inprog)
23205 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
23206 
23207 		un->un_f_wcc_inprog = 1;
23208 
23209 		mutex_exit(SD_MUTEX(un));
23210 
23211 		/*
23212 		 * Get the current write cache state
23213 		 */
23214 		if ((err = sd_get_write_cache_enabled(ssc, &cur_wce)) != 0) {
23215 			mutex_enter(SD_MUTEX(un));
23216 			un->un_f_wcc_inprog = 0;
23217 			cv_broadcast(&un->un_wcc_cv);
23218 			mutex_exit(SD_MUTEX(un));
23219 			break;
23220 		}
23221 
23222 		mutex_enter(SD_MUTEX(un));
23223 		un->un_f_write_cache_enabled = (cur_wce != 0);
23224 
23225 		if (un->un_f_write_cache_enabled && wce == 0) {
23226 			/*
23227 			 * Disable the write cache.  Don't clear
23228 			 * un_f_write_cache_enabled until after
23229 			 * the mode select and flush are complete.
23230 			 */
23231 			sync_supported = un->un_f_sync_cache_supported;
23232 
23233 			/*
23234 			 * If cache flush is suppressed, we assume that the
23235 			 * controller firmware will take care of managing the
23236 			 * write cache for us: no need to explicitly
23237 			 * disable it.
23238 			 */
23239 			if (!un->un_f_suppress_cache_flush) {
23240 				mutex_exit(SD_MUTEX(un));
23241 				if ((err = sd_cache_control(ssc,
23242 				    SD_CACHE_NOCHANGE,
23243 				    SD_CACHE_DISABLE)) == 0 &&
23244 				    sync_supported) {
23245 					err = sd_send_scsi_SYNCHRONIZE_CACHE(un,
23246 					    NULL);
23247 				}
23248 			} else {
23249 				mutex_exit(SD_MUTEX(un));
23250 			}
23251 
23252 			mutex_enter(SD_MUTEX(un));
23253 			if (err == 0) {
23254 				un->un_f_write_cache_enabled = 0;
23255 			}
23256 
23257 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
23258 			/*
23259 			 * Set un_f_write_cache_enabled first, so there is
23260 			 * no window where the cache is enabled, but the
23261 			 * bit says it isn't.
23262 			 */
23263 			un->un_f_write_cache_enabled = 1;
23264 
23265 			/*
23266 			 * If cache flush is suppressed, we assume that the
23267 			 * controller firmware will take care of managing the
23268 			 * write cache for us: no need to explicitly
23269 			 * enable it.
23270 			 */
23271 			if (!un->un_f_suppress_cache_flush) {
23272 				mutex_exit(SD_MUTEX(un));
23273 				err = sd_cache_control(ssc, SD_CACHE_NOCHANGE,
23274 				    SD_CACHE_ENABLE);
23275 			} else {
23276 				mutex_exit(SD_MUTEX(un));
23277 			}
23278 
23279 			mutex_enter(SD_MUTEX(un));
23280 
23281 			if (err) {
23282 				un->un_f_write_cache_enabled = 0;
23283 			}
23284 		}
23285 
23286 		un->un_f_wcc_inprog = 0;
23287 		cv_broadcast(&un->un_wcc_cv);
23288 		mutex_exit(SD_MUTEX(un));
23289 		break;
23290 	}
23291 
23292 	default:
23293 		err = ENOTTY;
23294 		break;
23295 	}
23296 	mutex_enter(SD_MUTEX(un));
23297 	un->un_ncmds_in_driver--;
23298 	ASSERT(un->un_ncmds_in_driver >= 0);
23299 	mutex_exit(SD_MUTEX(un));
23300 
23301 
23302 done_without_assess:
23303 	sd_ssc_fini(ssc);
23304 
23305 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23306 	return (err);
23307 
23308 done_with_assess:
23309 	mutex_enter(SD_MUTEX(un));
23310 	un->un_ncmds_in_driver--;
23311 	ASSERT(un->un_ncmds_in_driver >= 0);
23312 	mutex_exit(SD_MUTEX(un));
23313 
23314 done_quick_assess:
23315 	if (err != 0)
23316 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23317 	/* Uninitialize sd_ssc_t pointer */
23318 	sd_ssc_fini(ssc);
23319 
23320 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
23321 	return (err);
23322 }
23323 
23324 
23325 /*
23326  *    Function: sd_dkio_ctrl_info
23327  *
23328  * Description: This routine is the driver entry point for handling controller
23329  *		information ioctl requests (DKIOCINFO).
23330  *
23331  *   Arguments: dev  - the device number
23332  *		arg  - pointer to user provided dk_cinfo structure
23333  *		       specifying the controller type and attributes.
23334  *		flag - this argument is a pass through to ddi_copyxxx()
23335  *		       directly from the mode argument of ioctl().
23336  *
23337  * Return Code: 0
23338  *		EFAULT
23339  *		ENXIO
23340  */
23341 
23342 static int
23343 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
23344 {
23345 	struct sd_lun	*un = NULL;
23346 	struct dk_cinfo	*info;
23347 	dev_info_t	*pdip;
23348 	int		lun, tgt;
23349 
23350 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23351 		return (ENXIO);
23352 	}
23353 
23354 	info = (struct dk_cinfo *)
23355 	    kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
23356 
23357 	switch (un->un_ctype) {
23358 	case CTYPE_CDROM:
23359 		info->dki_ctype = DKC_CDROM;
23360 		break;
23361 	default:
23362 		info->dki_ctype = DKC_SCSI_CCS;
23363 		break;
23364 	}
23365 	pdip = ddi_get_parent(SD_DEVINFO(un));
23366 	info->dki_cnum = ddi_get_instance(pdip);
23367 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
23368 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
23369 	} else {
23370 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
23371 		    DK_DEVLEN - 1);
23372 	}
23373 
23374 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23375 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
23376 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
23377 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
23378 
23379 	/* Unit Information */
23380 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
23381 	info->dki_slave = ((tgt << 3) | lun);
23382 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
23383 	    DK_DEVLEN - 1);
23384 	info->dki_flags = DKI_FMTVOL;
23385 	info->dki_partition = SDPART(dev);
23386 
23387 	/* Max Transfer size of this device in blocks */
23388 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
23389 	info->dki_addr = 0;
23390 	info->dki_space = 0;
23391 	info->dki_prio = 0;
23392 	info->dki_vec = 0;
23393 
23394 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
23395 		kmem_free(info, sizeof (struct dk_cinfo));
23396 		return (EFAULT);
23397 	} else {
23398 		kmem_free(info, sizeof (struct dk_cinfo));
23399 		return (0);
23400 	}
23401 }
23402 
23403 /*
23404  *    Function: sd_get_media_info_com
23405  *
23406  * Description: This routine returns the information required to populate
23407  *		the fields for the dk_minfo/dk_minfo_ext structures.
23408  *
23409  *   Arguments: dev		- the device number
23410  *		dki_media_type	- media_type
23411  *		dki_lbsize	- logical block size
23412  *		dki_capacity	- capacity in blocks
23413  *		dki_pbsize	- physical block size (if requested)
23414  *
23415  * Return Code: 0
23416  *		EACCESS
23417  *		EFAULT
23418  *		ENXIO
23419  *		EIO
23420  */
23421 static int
23422 sd_get_media_info_com(dev_t dev, uint_t *dki_media_type, uint_t *dki_lbsize,
23423 	diskaddr_t *dki_capacity, uint_t *dki_pbsize)
23424 {
23425 	struct sd_lun		*un = NULL;
23426 	struct uscsi_cmd	com;
23427 	struct scsi_inquiry	*sinq;
23428 	u_longlong_t		media_capacity;
23429 	uint64_t		capacity;
23430 	uint_t			lbasize;
23431 	uint_t			pbsize;
23432 	uchar_t			*out_data;
23433 	uchar_t			*rqbuf;
23434 	int			rval = 0;
23435 	int			rtn;
23436 	sd_ssc_t		*ssc;
23437 
23438 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
23439 	    (un->un_state == SD_STATE_OFFLINE)) {
23440 		return (ENXIO);
23441 	}
23442 
23443 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info_com: entry\n");
23444 
23445 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
23446 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
23447 	ssc = sd_ssc_init(un);
23448 
23449 	/* Issue a TUR to determine if the drive is ready with media present */
23450 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, SD_CHECK_FOR_MEDIA);
23451 	if (rval == ENXIO) {
23452 		goto done;
23453 	} else if (rval != 0) {
23454 		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23455 	}
23456 
23457 	/* Now get configuration data */
23458 	if (ISCD(un)) {
23459 		*dki_media_type = DK_CDROM;
23460 
23461 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
23462 		if (un->un_f_mmc_cap == TRUE) {
23463 			rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf,
23464 			    SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN,
23465 			    SD_PATH_STANDARD);
23466 
23467 			if (rtn) {
23468 				/*
23469 				 * We ignore all failures for CD and need to
23470 				 * put the assessment before processing code
23471 				 * to avoid missing assessment for FMA.
23472 				 */
23473 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23474 				/*
23475 				 * Failed for other than an illegal request
23476 				 * or command not supported
23477 				 */
23478 				if ((com.uscsi_status == STATUS_CHECK) &&
23479 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
23480 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
23481 					    (rqbuf[12] != 0x20)) {
23482 						rval = EIO;
23483 						goto no_assessment;
23484 					}
23485 				}
23486 			} else {
23487 				/*
23488 				 * The GET CONFIGURATION command succeeded
23489 				 * so set the media type according to the
23490 				 * returned data
23491 				 */
23492 				*dki_media_type = out_data[6];
23493 				*dki_media_type <<= 8;
23494 				*dki_media_type |= out_data[7];
23495 			}
23496 		}
23497 	} else {
23498 		/*
23499 		 * The profile list is not available, so we attempt to identify
23500 		 * the media type based on the inquiry data
23501 		 */
23502 		sinq = un->un_sd->sd_inq;
23503 		if ((sinq->inq_dtype == DTYPE_DIRECT) ||
23504 		    (sinq->inq_dtype == DTYPE_OPTICAL)) {
23505 			/* This is a direct access device  or optical disk */
23506 			*dki_media_type = DK_FIXED_DISK;
23507 
23508 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
23509 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
23510 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
23511 					*dki_media_type = DK_ZIP;
23512 				} else if (
23513 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
23514 					*dki_media_type = DK_JAZ;
23515 				}
23516 			}
23517 		} else {
23518 			/*
23519 			 * Not a CD, direct access or optical disk so return
23520 			 * unknown media
23521 			 */
23522 			*dki_media_type = DK_UNKNOWN;
23523 		}
23524 	}
23525 
23526 	/*
23527 	 * Now read the capacity so we can provide the lbasize,
23528 	 * pbsize and capacity.
23529 	 */
23530 	if (dki_pbsize && un->un_f_descr_format_supported) {
23531 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
23532 		    &pbsize, SD_PATH_DIRECT);
23533 
23534 		/*
23535 		 * Override the physical blocksize if the instance already
23536 		 * has a larger value.
23537 		 */
23538 		pbsize = MAX(pbsize, un->un_phy_blocksize);
23539 	}
23540 
23541 	if (dki_pbsize == NULL || rval != 0 ||
23542 	    !un->un_f_descr_format_supported) {
23543 		rval = sd_send_scsi_READ_CAPACITY(ssc, &capacity, &lbasize,
23544 		    SD_PATH_DIRECT);
23545 
23546 		switch (rval) {
23547 		case 0:
23548 			if (un->un_f_enable_rmw &&
23549 			    un->un_phy_blocksize != 0) {
23550 				pbsize = un->un_phy_blocksize;
23551 			} else {
23552 				pbsize = lbasize;
23553 			}
23554 			media_capacity = capacity;
23555 
23556 			/*
23557 			 * sd_send_scsi_READ_CAPACITY() reports capacity in
23558 			 * un->un_sys_blocksize chunks. So we need to convert
23559 			 * it into cap.lbsize chunks.
23560 			 */
23561 			if (un->un_f_has_removable_media) {
23562 				media_capacity *= un->un_sys_blocksize;
23563 				media_capacity /= lbasize;
23564 			}
23565 			break;
23566 		case EACCES:
23567 			rval = EACCES;
23568 			goto done;
23569 		default:
23570 			rval = EIO;
23571 			goto done;
23572 		}
23573 	} else {
23574 		if (un->un_f_enable_rmw &&
23575 		    !ISP2(pbsize % DEV_BSIZE)) {
23576 			pbsize = SSD_SECSIZE;
23577 		} else if (!ISP2(lbasize % DEV_BSIZE) ||
23578 		    !ISP2(pbsize % DEV_BSIZE)) {
23579 			pbsize = lbasize = DEV_BSIZE;
23580 		}
23581 		media_capacity = capacity;
23582 	}
23583 
23584 	/*
23585 	 * If lun is expanded dynamically, update the un structure.
23586 	 */
23587 	mutex_enter(SD_MUTEX(un));
23588 	if ((un->un_f_blockcount_is_valid == TRUE) &&
23589 	    (un->un_f_tgt_blocksize_is_valid == TRUE) &&
23590 	    (capacity > un->un_blockcount)) {
23591 		un->un_f_expnevent = B_FALSE;
23592 		sd_update_block_info(un, lbasize, capacity);
23593 	}
23594 	mutex_exit(SD_MUTEX(un));
23595 
23596 	*dki_lbsize = lbasize;
23597 	*dki_capacity = media_capacity;
23598 	if (dki_pbsize)
23599 		*dki_pbsize = pbsize;
23600 
23601 done:
23602 	if (rval != 0) {
23603 		if (rval == EIO)
23604 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23605 		else
23606 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23607 	}
23608 no_assessment:
23609 	sd_ssc_fini(ssc);
23610 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
23611 	kmem_free(rqbuf, SENSE_LENGTH);
23612 	return (rval);
23613 }
23614 
23615 /*
23616  *    Function: sd_get_media_info
23617  *
23618  * Description: This routine is the driver entry point for handling ioctl
23619  *		requests for the media type or command set profile used by the
23620  *		drive to operate on the media (DKIOCGMEDIAINFO).
23621  *
23622  *   Arguments: dev	- the device number
23623  *		arg	- pointer to user provided dk_minfo structure
23624  *			  specifying the media type, logical block size and
23625  *			  drive capacity.
23626  *		flag	- this argument is a pass through to ddi_copyxxx()
23627  *			  directly from the mode argument of ioctl().
23628  *
23629  * Return Code: returns the value from sd_get_media_info_com
23630  */
23631 static int
23632 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
23633 {
23634 	struct dk_minfo		mi;
23635 	int			rval;
23636 
23637 	rval = sd_get_media_info_com(dev, &mi.dki_media_type,
23638 	    &mi.dki_lbsize, &mi.dki_capacity, NULL);
23639 
23640 	if (rval)
23641 		return (rval);
23642 	if (ddi_copyout(&mi, arg, sizeof (struct dk_minfo), flag))
23643 		rval = EFAULT;
23644 	return (rval);
23645 }
23646 
23647 /*
23648  *    Function: sd_get_media_info_ext
23649  *
23650  * Description: This routine is the driver entry point for handling ioctl
23651  *		requests for the media type or command set profile used by the
23652  *		drive to operate on the media (DKIOCGMEDIAINFOEXT). The
23653  *		difference this ioctl and DKIOCGMEDIAINFO is the return value
23654  *		of this ioctl contains both logical block size and physical
23655  *		block size.
23656  *
23657  *
23658  *   Arguments: dev	- the device number
23659  *		arg	- pointer to user provided dk_minfo_ext structure
23660  *			  specifying the media type, logical block size,
23661  *			  physical block size and disk capacity.
23662  *		flag	- this argument is a pass through to ddi_copyxxx()
23663  *			  directly from the mode argument of ioctl().
23664  *
23665  * Return Code: returns the value from sd_get_media_info_com
23666  */
23667 static int
23668 sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag)
23669 {
23670 	struct dk_minfo_ext	mie;
23671 	int			rval = 0;
23672 
23673 	rval = sd_get_media_info_com(dev, &mie.dki_media_type,
23674 	    &mie.dki_lbsize, &mie.dki_capacity, &mie.dki_pbsize);
23675 
23676 	if (rval)
23677 		return (rval);
23678 	if (ddi_copyout(&mie, arg, sizeof (struct dk_minfo_ext), flag))
23679 		rval = EFAULT;
23680 	return (rval);
23681 
23682 }
23683 
23684 /*
23685  *    Function: sd_watch_request_submit
23686  *
23687  * Description: Call scsi_watch_request_submit or scsi_mmc_watch_request_submit
23688  *		depending on which is supported by device.
23689  */
23690 static opaque_t
23691 sd_watch_request_submit(struct sd_lun *un)
23692 {
23693 	dev_t			dev;
23694 
23695 	/* All submissions are unified to use same device number */
23696 	dev = sd_make_device(SD_DEVINFO(un));
23697 
23698 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
23699 		return (scsi_mmc_watch_request_submit(SD_SCSI_DEVP(un),
23700 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23701 		    (caddr_t)dev));
23702 	} else {
23703 		return (scsi_watch_request_submit(SD_SCSI_DEVP(un),
23704 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23705 		    (caddr_t)dev));
23706 	}
23707 }
23708 
23709 
23710 /*
23711  *    Function: sd_check_media
23712  *
23713  * Description: This utility routine implements the functionality for the
23714  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23715  *		driver state changes from that specified by the user
23716  *		(inserted or ejected). For example, if the user specifies
23717  *		DKIO_EJECTED and the current media state is inserted this
23718  *		routine will immediately return DKIO_INSERTED. However, if the
23719  *		current media state is not inserted the user thread will be
23720  *		blocked until the drive state changes. If DKIO_NONE is specified
23721  *		the user thread will block until a drive state change occurs.
23722  *
23723  *   Arguments: dev  - the device number
23724  *		state  - user pointer to a dkio_state, updated with the current
23725  *			drive state at return.
23726  *
23727  * Return Code: ENXIO
23728  *		EIO
23729  *		EAGAIN
23730  *		EINTR
23731  */
23732 
23733 static int
23734 sd_check_media(dev_t dev, enum dkio_state state)
23735 {
23736 	struct sd_lun		*un = NULL;
23737 	enum dkio_state		prev_state;
23738 	opaque_t		token = NULL;
23739 	int			rval = 0;
23740 	sd_ssc_t		*ssc;
23741 
23742 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23743 		return (ENXIO);
23744 	}
23745 
23746 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23747 
23748 	ssc = sd_ssc_init(un);
23749 
23750 	mutex_enter(SD_MUTEX(un));
23751 
23752 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23753 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
23754 
23755 	prev_state = un->un_mediastate;
23756 
23757 	/* is there anything to do? */
23758 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23759 		/*
23760 		 * submit the request to the scsi_watch service;
23761 		 * scsi_media_watch_cb() does the real work
23762 		 */
23763 		mutex_exit(SD_MUTEX(un));
23764 
23765 		/*
23766 		 * This change handles the case where a scsi watch request is
23767 		 * added to a device that is powered down. To accomplish this
23768 		 * we power up the device before adding the scsi watch request,
23769 		 * since the scsi watch sends a TUR directly to the device
23770 		 * which the device cannot handle if it is powered down.
23771 		 */
23772 		if (sd_pm_entry(un) != DDI_SUCCESS) {
23773 			mutex_enter(SD_MUTEX(un));
23774 			goto done;
23775 		}
23776 
23777 		token = sd_watch_request_submit(un);
23778 
23779 		sd_pm_exit(un);
23780 
23781 		mutex_enter(SD_MUTEX(un));
23782 		if (token == NULL) {
23783 			rval = EAGAIN;
23784 			goto done;
23785 		}
23786 
23787 		/*
23788 		 * This is a special case IOCTL that doesn't return
23789 		 * until the media state changes. Routine sdpower
23790 		 * knows about and handles this so don't count it
23791 		 * as an active cmd in the driver, which would
23792 		 * keep the device busy to the pm framework.
23793 		 * If the count isn't decremented the device can't
23794 		 * be powered down.
23795 		 */
23796 		un->un_ncmds_in_driver--;
23797 		ASSERT(un->un_ncmds_in_driver >= 0);
23798 
23799 		/*
23800 		 * if a prior request had been made, this will be the same
23801 		 * token, as scsi_watch was designed that way.
23802 		 */
23803 		un->un_swr_token = token;
23804 		un->un_specified_mediastate = state;
23805 
23806 		/*
23807 		 * now wait for media change
23808 		 * we will not be signalled unless mediastate == state but it is
23809 		 * still better to test for this condition, since there is a
23810 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23811 		 */
23812 		SD_TRACE(SD_LOG_COMMON, un,
23813 		    "sd_check_media: waiting for media state change\n");
23814 		while (un->un_mediastate == state) {
23815 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23816 				SD_TRACE(SD_LOG_COMMON, un,
23817 				    "sd_check_media: waiting for media state "
23818 				    "was interrupted\n");
23819 				un->un_ncmds_in_driver++;
23820 				rval = EINTR;
23821 				goto done;
23822 			}
23823 			SD_TRACE(SD_LOG_COMMON, un,
23824 			    "sd_check_media: received signal, state=%x\n",
23825 			    un->un_mediastate);
23826 		}
23827 		/*
23828 		 * Inc the counter to indicate the device once again
23829 		 * has an active outstanding cmd.
23830 		 */
23831 		un->un_ncmds_in_driver++;
23832 	}
23833 
23834 	/* invalidate geometry */
23835 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23836 		sr_ejected(un);
23837 	}
23838 
23839 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23840 		uint64_t	capacity;
23841 		uint_t		lbasize;
23842 
23843 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23844 		mutex_exit(SD_MUTEX(un));
23845 		/*
23846 		 * Since the following routines use SD_PATH_DIRECT, we must
23847 		 * call PM directly before the upcoming disk accesses. This
23848 		 * may cause the disk to be power/spin up.
23849 		 */
23850 
23851 		if (sd_pm_entry(un) == DDI_SUCCESS) {
23852 			rval = sd_send_scsi_READ_CAPACITY(ssc,
23853 			    &capacity, &lbasize, SD_PATH_DIRECT);
23854 			if (rval != 0) {
23855 				sd_pm_exit(un);
23856 				if (rval == EIO)
23857 					sd_ssc_assessment(ssc,
23858 					    SD_FMT_STATUS_CHECK);
23859 				else
23860 					sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23861 				mutex_enter(SD_MUTEX(un));
23862 				goto done;
23863 			}
23864 		} else {
23865 			rval = EIO;
23866 			mutex_enter(SD_MUTEX(un));
23867 			goto done;
23868 		}
23869 		mutex_enter(SD_MUTEX(un));
23870 
23871 		sd_update_block_info(un, lbasize, capacity);
23872 
23873 		/*
23874 		 *  Check if the media in the device is writable or not
23875 		 */
23876 		if (ISCD(un)) {
23877 			sd_check_for_writable_cd(ssc, SD_PATH_DIRECT);
23878 		}
23879 
23880 		mutex_exit(SD_MUTEX(un));
23881 		cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT);
23882 		if ((cmlb_validate(un->un_cmlbhandle, 0,
23883 		    (void *)SD_PATH_DIRECT) == 0) && un->un_f_pkstats_enabled) {
23884 			sd_set_pstats(un);
23885 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23886 			    "sd_check_media: un:0x%p pstats created and "
23887 			    "set\n", un);
23888 		}
23889 
23890 		rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_PREVENT,
23891 		    SD_PATH_DIRECT);
23892 
23893 		sd_pm_exit(un);
23894 
23895 		if (rval != 0) {
23896 			if (rval == EIO)
23897 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
23898 			else
23899 				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
23900 		}
23901 
23902 		mutex_enter(SD_MUTEX(un));
23903 	}
23904 done:
23905 	sd_ssc_fini(ssc);
23906 	un->un_f_watcht_stopped = FALSE;
23907 	if (token != NULL && un->un_swr_token != NULL) {
23908 		/*
23909 		 * Use of this local token and the mutex ensures that we avoid
23910 		 * some race conditions associated with terminating the
23911 		 * scsi watch.
23912 		 */
23913 		token = un->un_swr_token;
23914 		mutex_exit(SD_MUTEX(un));
23915 		(void) scsi_watch_request_terminate(token,
23916 		    SCSI_WATCH_TERMINATE_WAIT);
23917 		if (scsi_watch_get_ref_count(token) == 0) {
23918 			mutex_enter(SD_MUTEX(un));
23919 			un->un_swr_token = (opaque_t)NULL;
23920 		} else {
23921 			mutex_enter(SD_MUTEX(un));
23922 		}
23923 	}
23924 
23925 	/*
23926 	 * Update the capacity kstat value, if no media previously
23927 	 * (capacity kstat is 0) and a media has been inserted
23928 	 * (un_f_blockcount_is_valid == TRUE)
23929 	 */
23930 	if (un->un_errstats) {
23931 		struct sd_errstats	*stp = NULL;
23932 
23933 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
23934 		if ((stp->sd_capacity.value.ui64 == 0) &&
23935 		    (un->un_f_blockcount_is_valid == TRUE)) {
23936 			stp->sd_capacity.value.ui64 =
23937 			    (uint64_t)((uint64_t)un->un_blockcount *
23938 			    un->un_sys_blocksize);
23939 		}
23940 	}
23941 	mutex_exit(SD_MUTEX(un));
23942 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23943 	return (rval);
23944 }
23945 
23946 
23947 /*
23948  *    Function: sd_delayed_cv_broadcast
23949  *
23950  * Description: Delayed cv_broadcast to allow for target to recover from media
23951  *		insertion.
23952  *
23953  *   Arguments: arg - driver soft state (unit) structure
23954  */
23955 
23956 static void
23957 sd_delayed_cv_broadcast(void *arg)
23958 {
23959 	struct sd_lun *un = arg;
23960 
23961 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23962 
23963 	mutex_enter(SD_MUTEX(un));
23964 	un->un_dcvb_timeid = NULL;
23965 	cv_broadcast(&un->un_state_cv);
23966 	mutex_exit(SD_MUTEX(un));
23967 }
23968 
23969 
23970 /*
23971  *    Function: sd_media_watch_cb
23972  *
23973  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23974  *		routine processes the TUR sense data and updates the driver
23975  *		state if a transition has occurred. The user thread
23976  *		(sd_check_media) is then signalled.
23977  *
23978  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23979  *			among multiple watches that share this callback function
23980  *		resultp - scsi watch facility result packet containing scsi
23981  *			  packet, status byte and sense data
23982  *
23983  * Return Code: 0 for success, -1 for failure
23984  */
23985 
23986 static int
23987 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23988 {
23989 	struct sd_lun			*un;
23990 	struct scsi_status		*statusp = resultp->statusp;
23991 	uint8_t				*sensep = (uint8_t *)resultp->sensep;
23992 	enum dkio_state			state = DKIO_NONE;
23993 	dev_t				dev = (dev_t)arg;
23994 	uchar_t				actual_sense_length;
23995 	uint8_t				skey, asc, ascq;
23996 
23997 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23998 		return (-1);
23999 	}
24000 	actual_sense_length = resultp->actual_sense_length;
24001 
24002 	mutex_enter(SD_MUTEX(un));
24003 	SD_TRACE(SD_LOG_COMMON, un,
24004 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24005 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24006 
24007 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24008 		un->un_mediastate = DKIO_DEV_GONE;
24009 		cv_broadcast(&un->un_state_cv);
24010 		mutex_exit(SD_MUTEX(un));
24011 
24012 		return (0);
24013 	}
24014 
24015 	if (un->un_f_mmc_cap && un->un_f_mmc_gesn_polling) {
24016 		if (sd_gesn_media_data_valid(resultp->mmc_data)) {
24017 			if ((resultp->mmc_data[5] &
24018 			    SD_GESN_MEDIA_EVENT_STATUS_PRESENT) != 0) {
24019 				state = DKIO_INSERTED;
24020 			} else {
24021 				state = DKIO_EJECTED;
24022 			}
24023 			if ((resultp->mmc_data[4] & SD_GESN_MEDIA_EVENT_CODE) ==
24024 			    SD_GESN_MEDIA_EVENT_EJECTREQUEST) {
24025 				sd_log_eject_request_event(un, KM_NOSLEEP);
24026 			}
24027 		}
24028 	} else if (sensep != NULL) {
24029 		/*
24030 		 * If there was a check condition then sensep points to valid
24031 		 * sense data. If status was not a check condition but a
24032 		 * reservation or busy status then the new state is DKIO_NONE.
24033 		 */
24034 		skey = scsi_sense_key(sensep);
24035 		asc = scsi_sense_asc(sensep);
24036 		ascq = scsi_sense_ascq(sensep);
24037 
24038 		SD_INFO(SD_LOG_COMMON, un,
24039 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24040 		    skey, asc, ascq);
24041 		/* This routine only uses up to 13 bytes of sense data. */
24042 		if (actual_sense_length >= 13) {
24043 			if (skey == KEY_UNIT_ATTENTION) {
24044 				if (asc == 0x28) {
24045 					state = DKIO_INSERTED;
24046 				}
24047 			} else if (skey == KEY_NOT_READY) {
24048 				/*
24049 				 * Sense data of 02/06/00 means that the
24050 				 * drive could not read the media (No
24051 				 * reference position found). In this case
24052 				 * to prevent a hang on the DKIOCSTATE IOCTL
24053 				 * we set the media state to DKIO_INSERTED.
24054 				 */
24055 				if (asc == 0x06 && ascq == 0x00)
24056 					state = DKIO_INSERTED;
24057 
24058 				/*
24059 				 * if 02/04/02  means that the host
24060 				 * should send start command. Explicitly
24061 				 * leave the media state as is
24062 				 * (inserted) as the media is inserted
24063 				 * and host has stopped device for PM
24064 				 * reasons. Upon next true read/write
24065 				 * to this media will bring the
24066 				 * device to the right state good for
24067 				 * media access.
24068 				 */
24069 				if (asc == 0x3a) {
24070 					state = DKIO_EJECTED;
24071 				} else {
24072 					/*
24073 					 * If the drive is busy with an
24074 					 * operation or long write, keep the
24075 					 * media in an inserted state.
24076 					 */
24077 
24078 					if ((asc == 0x04) &&
24079 					    ((ascq == 0x02) ||
24080 					    (ascq == 0x07) ||
24081 					    (ascq == 0x08))) {
24082 						state = DKIO_INSERTED;
24083 					}
24084 				}
24085 			} else if (skey == KEY_NO_SENSE) {
24086 				if ((asc == 0x00) && (ascq == 0x00)) {
24087 					/*
24088 					 * Sense Data 00/00/00 does not provide
24089 					 * any information about the state of
24090 					 * the media. Ignore it.
24091 					 */
24092 					mutex_exit(SD_MUTEX(un));
24093 					return (0);
24094 				}
24095 			}
24096 		}
24097 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24098 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24099 		state = DKIO_INSERTED;
24100 	}
24101 
24102 	SD_TRACE(SD_LOG_COMMON, un,
24103 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24104 	    state, un->un_specified_mediastate);
24105 
24106 	/*
24107 	 * now signal the waiting thread if this is *not* the specified state;
24108 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24109 	 * to recover
24110 	 */
24111 	if (state != un->un_specified_mediastate) {
24112 		un->un_mediastate = state;
24113 		if (state == DKIO_INSERTED) {
24114 			/*
24115 			 * delay the signal to give the drive a chance
24116 			 * to do what it apparently needs to do
24117 			 */
24118 			SD_TRACE(SD_LOG_COMMON, un,
24119 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24120 			if (un->un_dcvb_timeid == NULL) {
24121 				un->un_dcvb_timeid =
24122 				    timeout(sd_delayed_cv_broadcast, un,
24123 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24124 			}
24125 		} else {
24126 			SD_TRACE(SD_LOG_COMMON, un,
24127 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24128 			cv_broadcast(&un->un_state_cv);
24129 		}
24130 	}
24131 	mutex_exit(SD_MUTEX(un));
24132 	return (0);
24133 }
24134 
24135 
24136 /*
24137  *    Function: sd_dkio_get_temp
24138  *
24139  * Description: This routine is the driver entry point for handling ioctl
24140  *		requests to get the disk temperature.
24141  *
24142  *   Arguments: dev  - the device number
24143  *		arg  - pointer to user provided dk_temperature structure.
24144  *		flag - this argument is a pass through to ddi_copyxxx()
24145  *		       directly from the mode argument of ioctl().
24146  *
24147  * Return Code: 0
24148  *		EFAULT
24149  *		ENXIO
24150  *		EAGAIN
24151  */
24152 
24153 static int
24154 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24155 {
24156 	struct sd_lun		*un = NULL;
24157 	struct dk_temperature	*dktemp = NULL;
24158 	uchar_t			*temperature_page;
24159 	int			rval = 0;
24160 	int			path_flag = SD_PATH_STANDARD;
24161 	sd_ssc_t		*ssc;
24162 
24163 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24164 		return (ENXIO);
24165 	}
24166 
24167 	ssc = sd_ssc_init(un);
24168 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24169 
24170 	/* copyin the disk temp argument to get the user flags */
24171 	if (ddi_copyin((void *)arg, dktemp,
24172 	    sizeof (struct dk_temperature), flag) != 0) {
24173 		rval = EFAULT;
24174 		goto done;
24175 	}
24176 
24177 	/* Initialize the temperature to invalid. */
24178 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24179 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24180 
24181 	/*
24182 	 * Note: Investigate removing the "bypass pm" semantic.
24183 	 * Can we just bypass PM always?
24184 	 */
24185 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24186 		path_flag = SD_PATH_DIRECT;
24187 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24188 		mutex_enter(&un->un_pm_mutex);
24189 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24190 			/*
24191 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24192 			 * in low power mode, we can not wake it up, Need to
24193 			 * return EAGAIN.
24194 			 */
24195 			mutex_exit(&un->un_pm_mutex);
24196 			rval = EAGAIN;
24197 			goto done;
24198 		} else {
24199 			/*
24200 			 * Indicate to PM the device is busy. This is required
24201 			 * to avoid a race - i.e. the ioctl is issuing a
24202 			 * command and the pm framework brings down the device
24203 			 * to low power mode (possible power cut-off on some
24204 			 * platforms).
24205 			 */
24206 			mutex_exit(&un->un_pm_mutex);
24207 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24208 				rval = EAGAIN;
24209 				goto done;
24210 			}
24211 		}
24212 	}
24213 
24214 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24215 
24216 	rval = sd_send_scsi_LOG_SENSE(ssc, temperature_page,
24217 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag);
24218 	if (rval != 0)
24219 		goto done2;
24220 
24221 	/*
24222 	 * For the current temperature verify that the parameter length is 0x02
24223 	 * and the parameter code is 0x00
24224 	 */
24225 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24226 	    (temperature_page[5] == 0x00)) {
24227 		if (temperature_page[9] == 0xFF) {
24228 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24229 		} else {
24230 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24231 		}
24232 	}
24233 
24234 	/*
24235 	 * For the reference temperature verify that the parameter
24236 	 * length is 0x02 and the parameter code is 0x01
24237 	 */
24238 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24239 	    (temperature_page[11] == 0x01)) {
24240 		if (temperature_page[15] == 0xFF) {
24241 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24242 		} else {
24243 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24244 		}
24245 	}
24246 
24247 	/* Do the copyout regardless of the temperature commands status. */
24248 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24249 	    flag) != 0) {
24250 		rval = EFAULT;
24251 		goto done1;
24252 	}
24253 
24254 done2:
24255 	if (rval != 0) {
24256 		if (rval == EIO)
24257 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24258 		else
24259 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24260 	}
24261 done1:
24262 	if (path_flag == SD_PATH_DIRECT) {
24263 		sd_pm_exit(un);
24264 	}
24265 
24266 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24267 done:
24268 	sd_ssc_fini(ssc);
24269 	if (dktemp != NULL) {
24270 		kmem_free(dktemp, sizeof (struct dk_temperature));
24271 	}
24272 
24273 	return (rval);
24274 }
24275 
24276 
24277 /*
24278  *    Function: sd_log_page_supported
24279  *
24280  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24281  *		supported log pages.
24282  *
24283  *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
24284  *                      structure for this target.
24285  *		log_page -
24286  *
24287  * Return Code: -1 - on error (log sense is optional and may not be supported).
24288  *		0  - log page not found.
24289  *  		1  - log page found.
24290  */
24291 
24292 static int
24293 sd_log_page_supported(sd_ssc_t *ssc, int log_page)
24294 {
24295 	uchar_t *log_page_data;
24296 	int	i;
24297 	int	match = 0;
24298 	int	log_size;
24299 	int	status = 0;
24300 	struct sd_lun	*un;
24301 
24302 	ASSERT(ssc != NULL);
24303 	un = ssc->ssc_un;
24304 	ASSERT(un != NULL);
24305 
24306 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24307 
24308 	status = sd_send_scsi_LOG_SENSE(ssc, log_page_data, 0xFF, 0, 0x01, 0,
24309 	    SD_PATH_DIRECT);
24310 
24311 	if (status != 0) {
24312 		if (status == EIO) {
24313 			/*
24314 			 * Some disks do not support log sense, we
24315 			 * should ignore this kind of error(sense key is
24316 			 * 0x5 - illegal request).
24317 			 */
24318 			uint8_t *sensep;
24319 			int senlen;
24320 
24321 			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
24322 			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
24323 			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
24324 
24325 			if (senlen > 0 &&
24326 			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
24327 				sd_ssc_assessment(ssc,
24328 				    SD_FMT_IGNORE_COMPROMISE);
24329 			} else {
24330 				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24331 			}
24332 		} else {
24333 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24334 		}
24335 
24336 		SD_ERROR(SD_LOG_COMMON, un,
24337 		    "sd_log_page_supported: failed log page retrieval\n");
24338 		kmem_free(log_page_data, 0xFF);
24339 		return (-1);
24340 	}
24341 
24342 	log_size = log_page_data[3];
24343 
24344 	/*
24345 	 * The list of supported log pages start from the fourth byte. Check
24346 	 * until we run out of log pages or a match is found.
24347 	 */
24348 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24349 		if (log_page_data[i] == log_page) {
24350 			match++;
24351 		}
24352 	}
24353 	kmem_free(log_page_data, 0xFF);
24354 	return (match);
24355 }
24356 
24357 
24358 /*
24359  *    Function: sd_mhdioc_failfast
24360  *
24361  * Description: This routine is the driver entry point for handling ioctl
24362  *		requests to enable/disable the multihost failfast option.
24363  *		(MHIOCENFAILFAST)
24364  *
24365  *   Arguments: dev	- the device number
24366  *		arg	- user specified probing interval.
24367  *		flag	- this argument is a pass through to ddi_copyxxx()
24368  *			  directly from the mode argument of ioctl().
24369  *
24370  * Return Code: 0
24371  *		EFAULT
24372  *		ENXIO
24373  */
24374 
24375 static int
24376 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24377 {
24378 	struct sd_lun	*un = NULL;
24379 	int		mh_time;
24380 	int		rval = 0;
24381 
24382 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24383 		return (ENXIO);
24384 	}
24385 
24386 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24387 		return (EFAULT);
24388 
24389 	if (mh_time) {
24390 		mutex_enter(SD_MUTEX(un));
24391 		un->un_resvd_status |= SD_FAILFAST;
24392 		mutex_exit(SD_MUTEX(un));
24393 		/*
24394 		 * If mh_time is INT_MAX, then this ioctl is being used for
24395 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24396 		 */
24397 		if (mh_time != INT_MAX) {
24398 			rval = sd_check_mhd(dev, mh_time);
24399 		}
24400 	} else {
24401 		(void) sd_check_mhd(dev, 0);
24402 		mutex_enter(SD_MUTEX(un));
24403 		un->un_resvd_status &= ~SD_FAILFAST;
24404 		mutex_exit(SD_MUTEX(un));
24405 	}
24406 	return (rval);
24407 }
24408 
24409 
24410 /*
24411  *    Function: sd_mhdioc_takeown
24412  *
24413  * Description: This routine is the driver entry point for handling ioctl
24414  *		requests to forcefully acquire exclusive access rights to the
24415  *		multihost disk (MHIOCTKOWN).
24416  *
24417  *   Arguments: dev	- the device number
24418  *		arg	- user provided structure specifying the delay
24419  *			  parameters in milliseconds
24420  *		flag	- this argument is a pass through to ddi_copyxxx()
24421  *			  directly from the mode argument of ioctl().
24422  *
24423  * Return Code: 0
24424  *		EFAULT
24425  *		ENXIO
24426  */
24427 
24428 static int
24429 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24430 {
24431 	struct sd_lun		*un = NULL;
24432 	struct mhioctkown	*tkown = NULL;
24433 	int			rval = 0;
24434 
24435 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24436 		return (ENXIO);
24437 	}
24438 
24439 	if (arg != NULL) {
24440 		tkown = (struct mhioctkown *)
24441 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24442 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24443 		if (rval != 0) {
24444 			rval = EFAULT;
24445 			goto error;
24446 		}
24447 	}
24448 
24449 	rval = sd_take_ownership(dev, tkown);
24450 	mutex_enter(SD_MUTEX(un));
24451 	if (rval == 0) {
24452 		un->un_resvd_status |= SD_RESERVE;
24453 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24454 			sd_reinstate_resv_delay =
24455 			    tkown->reinstate_resv_delay * 1000;
24456 		} else {
24457 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24458 		}
24459 		/*
24460 		 * Give the scsi_watch routine interval set by
24461 		 * the MHIOCENFAILFAST ioctl precedence here.
24462 		 */
24463 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24464 			mutex_exit(SD_MUTEX(un));
24465 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24466 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24467 			    "sd_mhdioc_takeown : %d\n",
24468 			    sd_reinstate_resv_delay);
24469 		} else {
24470 			mutex_exit(SD_MUTEX(un));
24471 		}
24472 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24473 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24474 	} else {
24475 		un->un_resvd_status &= ~SD_RESERVE;
24476 		mutex_exit(SD_MUTEX(un));
24477 	}
24478 
24479 error:
24480 	if (tkown != NULL) {
24481 		kmem_free(tkown, sizeof (struct mhioctkown));
24482 	}
24483 	return (rval);
24484 }
24485 
24486 
24487 /*
24488  *    Function: sd_mhdioc_release
24489  *
24490  * Description: This routine is the driver entry point for handling ioctl
24491  *		requests to release exclusive access rights to the multihost
24492  *		disk (MHIOCRELEASE).
24493  *
24494  *   Arguments: dev	- the device number
24495  *
24496  * Return Code: 0
24497  *		ENXIO
24498  */
24499 
24500 static int
24501 sd_mhdioc_release(dev_t dev)
24502 {
24503 	struct sd_lun		*un = NULL;
24504 	timeout_id_t		resvd_timeid_save;
24505 	int			resvd_status_save;
24506 	int			rval = 0;
24507 
24508 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24509 		return (ENXIO);
24510 	}
24511 
24512 	mutex_enter(SD_MUTEX(un));
24513 	resvd_status_save = un->un_resvd_status;
24514 	un->un_resvd_status &=
24515 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24516 	if (un->un_resvd_timeid) {
24517 		resvd_timeid_save = un->un_resvd_timeid;
24518 		un->un_resvd_timeid = NULL;
24519 		mutex_exit(SD_MUTEX(un));
24520 		(void) untimeout(resvd_timeid_save);
24521 	} else {
24522 		mutex_exit(SD_MUTEX(un));
24523 	}
24524 
24525 	/*
24526 	 * destroy any pending timeout thread that may be attempting to
24527 	 * reinstate reservation on this device.
24528 	 */
24529 	sd_rmv_resv_reclaim_req(dev);
24530 
24531 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24532 		mutex_enter(SD_MUTEX(un));
24533 		if ((un->un_mhd_token) &&
24534 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24535 			mutex_exit(SD_MUTEX(un));
24536 			(void) sd_check_mhd(dev, 0);
24537 		} else {
24538 			mutex_exit(SD_MUTEX(un));
24539 		}
24540 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24541 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24542 	} else {
24543 		/*
24544 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24545 		 */
24546 		mutex_enter(SD_MUTEX(un));
24547 		un->un_resvd_status = resvd_status_save;
24548 		mutex_exit(SD_MUTEX(un));
24549 	}
24550 	return (rval);
24551 }
24552 
24553 
24554 /*
24555  *    Function: sd_mhdioc_register_devid
24556  *
24557  * Description: This routine is the driver entry point for handling ioctl
24558  *		requests to register the device id (MHIOCREREGISTERDEVID).
24559  *
24560  *		Note: The implementation for this ioctl has been updated to
24561  *		be consistent with the original PSARC case (1999/357)
24562  *		(4375899, 4241671, 4220005)
24563  *
24564  *   Arguments: dev	- the device number
24565  *
24566  * Return Code: 0
24567  *		ENXIO
24568  */
24569 
24570 static int
24571 sd_mhdioc_register_devid(dev_t dev)
24572 {
24573 	struct sd_lun	*un = NULL;
24574 	int		rval = 0;
24575 	sd_ssc_t	*ssc;
24576 
24577 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24578 		return (ENXIO);
24579 	}
24580 
24581 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24582 
24583 	mutex_enter(SD_MUTEX(un));
24584 
24585 	/* If a devid already exists, de-register it */
24586 	if (un->un_devid != NULL) {
24587 		ddi_devid_unregister(SD_DEVINFO(un));
24588 		/*
24589 		 * After unregister devid, needs to free devid memory
24590 		 */
24591 		ddi_devid_free(un->un_devid);
24592 		un->un_devid = NULL;
24593 	}
24594 
24595 	/* Check for reservation conflict */
24596 	mutex_exit(SD_MUTEX(un));
24597 	ssc = sd_ssc_init(un);
24598 	rval = sd_send_scsi_TEST_UNIT_READY(ssc, 0);
24599 	mutex_enter(SD_MUTEX(un));
24600 
24601 	switch (rval) {
24602 	case 0:
24603 		sd_register_devid(ssc, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24604 		break;
24605 	case EACCES:
24606 		break;
24607 	default:
24608 		rval = EIO;
24609 	}
24610 
24611 	mutex_exit(SD_MUTEX(un));
24612 	if (rval != 0) {
24613 		if (rval == EIO)
24614 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
24615 		else
24616 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
24617 	}
24618 	sd_ssc_fini(ssc);
24619 	return (rval);
24620 }
24621 
24622 
24623 /*
24624  *    Function: sd_mhdioc_inkeys
24625  *
24626  * Description: This routine is the driver entry point for handling ioctl
24627  *		requests to issue the SCSI-3 Persistent In Read Keys command
24628  *		to the device (MHIOCGRP_INKEYS).
24629  *
24630  *   Arguments: dev	- the device number
24631  *		arg	- user provided in_keys structure
24632  *		flag	- this argument is a pass through to ddi_copyxxx()
24633  *			  directly from the mode argument of ioctl().
24634  *
24635  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24636  *		ENXIO
24637  *		EFAULT
24638  */
24639 
24640 static int
24641 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24642 {
24643 	struct sd_lun		*un;
24644 	mhioc_inkeys_t		inkeys;
24645 	int			rval = 0;
24646 
24647 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24648 		return (ENXIO);
24649 	}
24650 
24651 #ifdef _MULTI_DATAMODEL
24652 	switch (ddi_model_convert_from(flag & FMODELS)) {
24653 	case DDI_MODEL_ILP32: {
24654 		struct mhioc_inkeys32	inkeys32;
24655 
24656 		if (ddi_copyin(arg, &inkeys32,
24657 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24658 			return (EFAULT);
24659 		}
24660 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24661 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24662 		    &inkeys, flag)) != 0) {
24663 			return (rval);
24664 		}
24665 		inkeys32.generation = inkeys.generation;
24666 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24667 		    flag) != 0) {
24668 			return (EFAULT);
24669 		}
24670 		break;
24671 	}
24672 	case DDI_MODEL_NONE:
24673 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24674 		    flag) != 0) {
24675 			return (EFAULT);
24676 		}
24677 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24678 		    &inkeys, flag)) != 0) {
24679 			return (rval);
24680 		}
24681 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24682 		    flag) != 0) {
24683 			return (EFAULT);
24684 		}
24685 		break;
24686 	}
24687 
24688 #else /* ! _MULTI_DATAMODEL */
24689 
24690 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24691 		return (EFAULT);
24692 	}
24693 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24694 	if (rval != 0) {
24695 		return (rval);
24696 	}
24697 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24698 		return (EFAULT);
24699 	}
24700 
24701 #endif /* _MULTI_DATAMODEL */
24702 
24703 	return (rval);
24704 }
24705 
24706 
24707 /*
24708  *    Function: sd_mhdioc_inresv
24709  *
24710  * Description: This routine is the driver entry point for handling ioctl
24711  *		requests to issue the SCSI-3 Persistent In Read Reservations
24712  *		command to the device (MHIOCGRP_INKEYS).
24713  *
24714  *   Arguments: dev	- the device number
24715  *		arg	- user provided in_resv structure
24716  *		flag	- this argument is a pass through to ddi_copyxxx()
24717  *			  directly from the mode argument of ioctl().
24718  *
24719  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24720  *		ENXIO
24721  *		EFAULT
24722  */
24723 
24724 static int
24725 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24726 {
24727 	struct sd_lun		*un;
24728 	mhioc_inresvs_t		inresvs;
24729 	int			rval = 0;
24730 
24731 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24732 		return (ENXIO);
24733 	}
24734 
24735 #ifdef _MULTI_DATAMODEL
24736 
24737 	switch (ddi_model_convert_from(flag & FMODELS)) {
24738 	case DDI_MODEL_ILP32: {
24739 		struct mhioc_inresvs32	inresvs32;
24740 
24741 		if (ddi_copyin(arg, &inresvs32,
24742 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24743 			return (EFAULT);
24744 		}
24745 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24746 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24747 		    &inresvs, flag)) != 0) {
24748 			return (rval);
24749 		}
24750 		inresvs32.generation = inresvs.generation;
24751 		if (ddi_copyout(&inresvs32, arg,
24752 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24753 			return (EFAULT);
24754 		}
24755 		break;
24756 	}
24757 	case DDI_MODEL_NONE:
24758 		if (ddi_copyin(arg, &inresvs,
24759 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24760 			return (EFAULT);
24761 		}
24762 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24763 		    &inresvs, flag)) != 0) {
24764 			return (rval);
24765 		}
24766 		if (ddi_copyout(&inresvs, arg,
24767 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24768 			return (EFAULT);
24769 		}
24770 		break;
24771 	}
24772 
24773 #else /* ! _MULTI_DATAMODEL */
24774 
24775 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24776 		return (EFAULT);
24777 	}
24778 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24779 	if (rval != 0) {
24780 		return (rval);
24781 	}
24782 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24783 		return (EFAULT);
24784 	}
24785 
24786 #endif /* ! _MULTI_DATAMODEL */
24787 
24788 	return (rval);
24789 }
24790 
24791 
24792 /*
24793  * The following routines support the clustering functionality described below
24794  * and implement lost reservation reclaim functionality.
24795  *
24796  * Clustering
24797  * ----------
24798  * The clustering code uses two different, independent forms of SCSI
24799  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24800  * Persistent Group Reservations. For any particular disk, it will use either
24801  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24802  *
24803  * SCSI-2
24804  * The cluster software takes ownership of a multi-hosted disk by issuing the
24805  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24806  * MHIOCRELEASE ioctl.  Closely related is the MHIOCENFAILFAST ioctl -- a
24807  * cluster, just after taking ownership of the disk with the MHIOCTKOWN ioctl
24808  * then issues the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the
24809  * driver. The meaning of failfast is that if the driver (on this host) ever
24810  * encounters the scsi error return code RESERVATION_CONFLICT from the device,
24811  * it should immediately panic the host. The motivation for this ioctl is that
24812  * if this host does encounter reservation conflict, the underlying cause is
24813  * that some other host of the cluster has decided that this host is no longer
24814  * in the cluster and has seized control of the disks for itself. Since this
24815  * host is no longer in the cluster, it ought to panic itself. The
24816  * MHIOCENFAILFAST ioctl does two things:
24817  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24818  *      error to panic the host
24819  *      (b) it sets up a periodic timer to test whether this host still has
24820  *      "access" (in that no other host has reserved the device):  if the
24821  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24822  *      purpose of that periodic timer is to handle scenarios where the host is
24823  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24824  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24825  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24826  * the device itself.
24827  *
24828  * SCSI-3 PGR
24829  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24830  * facility is supported through the shared multihost disk ioctls
24831  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24832  * MHIOCGRP_PREEMPTANDABORT, MHIOCGRP_CLEAR)
24833  *
24834  * Reservation Reclaim:
24835  * --------------------
24836  * To support the lost reservation reclaim operations this driver creates a
24837  * single thread to handle reinstating reservations on all devices that have
24838  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24839  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24840  * and the reservation reclaim thread loops through the requests to regain the
24841  * lost reservations.
24842  */
24843 
24844 /*
24845  *    Function: sd_check_mhd()
24846  *
24847  * Description: This function sets up and submits a scsi watch request or
24848  *		terminates an existing watch request. This routine is used in
24849  *		support of reservation reclaim.
24850  *
24851  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24852  *			 among multiple watches that share the callback function
24853  *		interval - the number of microseconds specifying the watch
24854  *			   interval for issuing TEST UNIT READY commands. If
24855  *			   set to 0 the watch should be terminated. If the
24856  *			   interval is set to 0 and if the device is required
24857  *			   to hold reservation while disabling failfast, the
24858  *			   watch is restarted with an interval of
24859  *			   reinstate_resv_delay.
24860  *
24861  * Return Code: 0	   - Successful submit/terminate of scsi watch request
24862  *		ENXIO      - Indicates an invalid device was specified
24863  *		EAGAIN     - Unable to submit the scsi watch request
24864  */
24865 
24866 static int
24867 sd_check_mhd(dev_t dev, int interval)
24868 {
24869 	struct sd_lun	*un;
24870 	opaque_t	token;
24871 
24872 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24873 		return (ENXIO);
24874 	}
24875 
24876 	/* is this a watch termination request? */
24877 	if (interval == 0) {
24878 		mutex_enter(SD_MUTEX(un));
24879 		/* if there is an existing watch task then terminate it */
24880 		if (un->un_mhd_token) {
24881 			token = un->un_mhd_token;
24882 			un->un_mhd_token = NULL;
24883 			mutex_exit(SD_MUTEX(un));
24884 			(void) scsi_watch_request_terminate(token,
24885 			    SCSI_WATCH_TERMINATE_ALL_WAIT);
24886 			mutex_enter(SD_MUTEX(un));
24887 		} else {
24888 			mutex_exit(SD_MUTEX(un));
24889 			/*
24890 			 * Note: If we return here we don't check for the
24891 			 * failfast case. This is the original legacy
24892 			 * implementation but perhaps we should be checking
24893 			 * the failfast case.
24894 			 */
24895 			return (0);
24896 		}
24897 		/*
24898 		 * If the device is required to hold reservation while
24899 		 * disabling failfast, we need to restart the scsi_watch
24900 		 * routine with an interval of reinstate_resv_delay.
24901 		 */
24902 		if (un->un_resvd_status & SD_RESERVE) {
24903 			interval = sd_reinstate_resv_delay/1000;
24904 		} else {
24905 			/* no failfast so bail */
24906 			mutex_exit(SD_MUTEX(un));
24907 			return (0);
24908 		}
24909 		mutex_exit(SD_MUTEX(un));
24910 	}
24911 
24912 	/*
24913 	 * adjust minimum time interval to 1 second,
24914 	 * and convert from msecs to usecs
24915 	 */
24916 	if (interval > 0 && interval < 1000) {
24917 		interval = 1000;
24918 	}
24919 	interval *= 1000;
24920 
24921 	/*
24922 	 * submit the request to the scsi_watch service
24923 	 */
24924 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24925 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24926 	if (token == NULL) {
24927 		return (EAGAIN);
24928 	}
24929 
24930 	/*
24931 	 * save token for termination later on
24932 	 */
24933 	mutex_enter(SD_MUTEX(un));
24934 	un->un_mhd_token = token;
24935 	mutex_exit(SD_MUTEX(un));
24936 	return (0);
24937 }
24938 
24939 
24940 /*
24941  *    Function: sd_mhd_watch_cb()
24942  *
24943  * Description: This function is the call back function used by the scsi watch
24944  *		facility. The scsi watch facility sends the "Test Unit Ready"
24945  *		and processes the status. If applicable (i.e. a "Unit Attention"
24946  *		status and automatic "Request Sense" not used) the scsi watch
24947  *		facility will send a "Request Sense" and retrieve the sense data
24948  *		to be passed to this callback function. In either case the
24949  *		automatic "Request Sense" or the facility submitting one, this
24950  *		callback is passed the status and sense data.
24951  *
24952  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24953  *			among multiple watches that share this callback function
24954  *		resultp - scsi watch facility result packet containing scsi
24955  *			  packet, status byte and sense data
24956  *
24957  * Return Code: 0 - continue the watch task
24958  *		non-zero - terminate the watch task
24959  */
24960 
24961 static int
24962 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24963 {
24964 	struct sd_lun			*un;
24965 	struct scsi_status		*statusp;
24966 	uint8_t				*sensep;
24967 	struct scsi_pkt			*pkt;
24968 	uchar_t				actual_sense_length;
24969 	dev_t  				dev = (dev_t)arg;
24970 
24971 	ASSERT(resultp != NULL);
24972 	statusp			= resultp->statusp;
24973 	sensep			= (uint8_t *)resultp->sensep;
24974 	pkt			= resultp->pkt;
24975 	actual_sense_length	= resultp->actual_sense_length;
24976 
24977 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24978 		return (ENXIO);
24979 	}
24980 
24981 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
24982 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24983 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24984 
24985 	/* Begin processing of the status and/or sense data */
24986 	if (pkt->pkt_reason != CMD_CMPLT) {
24987 		/* Handle the incomplete packet */
24988 		sd_mhd_watch_incomplete(un, pkt);
24989 		return (0);
24990 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24991 		if (*((unsigned char *)statusp)
24992 		    == STATUS_RESERVATION_CONFLICT) {
24993 			/*
24994 			 * Handle a reservation conflict by panicking if
24995 			 * configured for failfast or by logging the conflict
24996 			 * and updating the reservation status
24997 			 */
24998 			mutex_enter(SD_MUTEX(un));
24999 			if ((un->un_resvd_status & SD_FAILFAST) &&
25000 			    (sd_failfast_enable)) {
25001 				sd_panic_for_res_conflict(un);
25002 				/*NOTREACHED*/
25003 			}
25004 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25005 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25006 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25007 			mutex_exit(SD_MUTEX(un));
25008 		}
25009 	}
25010 
25011 	if (sensep != NULL) {
25012 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25013 			mutex_enter(SD_MUTEX(un));
25014 			if ((scsi_sense_asc(sensep) ==
25015 			    SD_SCSI_RESET_SENSE_CODE) &&
25016 			    (un->un_resvd_status & SD_RESERVE)) {
25017 				/*
25018 				 * The additional sense code indicates a power
25019 				 * on or bus device reset has occurred; update
25020 				 * the reservation status.
25021 				 */
25022 				un->un_resvd_status |=
25023 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25024 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25025 				    "sd_mhd_watch_cb: Lost Reservation\n");
25026 			}
25027 		} else {
25028 			return (0);
25029 		}
25030 	} else {
25031 		mutex_enter(SD_MUTEX(un));
25032 	}
25033 
25034 	if ((un->un_resvd_status & SD_RESERVE) &&
25035 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25036 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25037 			/*
25038 			 * A reset occurred in between the last probe and this
25039 			 * one so if a timeout is pending cancel it.
25040 			 */
25041 			if (un->un_resvd_timeid) {
25042 				timeout_id_t temp_id = un->un_resvd_timeid;
25043 				un->un_resvd_timeid = NULL;
25044 				mutex_exit(SD_MUTEX(un));
25045 				(void) untimeout(temp_id);
25046 				mutex_enter(SD_MUTEX(un));
25047 			}
25048 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25049 		}
25050 		if (un->un_resvd_timeid == 0) {
25051 			/* Schedule a timeout to handle the lost reservation */
25052 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25053 			    (void *)dev,
25054 			    drv_usectohz(sd_reinstate_resv_delay));
25055 		}
25056 	}
25057 	mutex_exit(SD_MUTEX(un));
25058 	return (0);
25059 }
25060 
25061 
25062 /*
25063  *    Function: sd_mhd_watch_incomplete()
25064  *
25065  * Description: This function is used to find out why a scsi pkt sent by the
25066  *		scsi watch facility was not completed. Under some scenarios this
25067  *		routine will return. Otherwise it will send a bus reset to see
25068  *		if the drive is still online.
25069  *
25070  *   Arguments: un  - driver soft state (unit) structure
25071  *		pkt - incomplete scsi pkt
25072  */
25073 
25074 static void
25075 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25076 {
25077 	int	be_chatty;
25078 	int	perr;
25079 
25080 	ASSERT(pkt != NULL);
25081 	ASSERT(un != NULL);
25082 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25083 	perr		= (pkt->pkt_statistics & STAT_PERR);
25084 
25085 	mutex_enter(SD_MUTEX(un));
25086 	if (un->un_state == SD_STATE_DUMPING) {
25087 		mutex_exit(SD_MUTEX(un));
25088 		return;
25089 	}
25090 
25091 	switch (pkt->pkt_reason) {
25092 	case CMD_UNX_BUS_FREE:
25093 		/*
25094 		 * If we had a parity error that caused the target to drop BSY*,
25095 		 * don't be chatty about it.
25096 		 */
25097 		if (perr && be_chatty) {
25098 			be_chatty = 0;
25099 		}
25100 		break;
25101 	case CMD_TAG_REJECT:
25102 		/*
25103 		 * The SCSI-2 spec states that a tag reject will be sent by the
25104 		 * target if tagged queuing is not supported. A tag reject may
25105 		 * also be sent during certain initialization periods or to
25106 		 * control internal resources. For the latter case the target
25107 		 * may also return Queue Full.
25108 		 *
25109 		 * If this driver receives a tag reject from a target that is
25110 		 * going through an init period or controlling internal
25111 		 * resources tagged queuing will be disabled. This is a less
25112 		 * than optimal behavior but the driver is unable to determine
25113 		 * the target state and assumes tagged queueing is not supported
25114 		 */
25115 		pkt->pkt_flags = 0;
25116 		un->un_tagflags = 0;
25117 
25118 		if (un->un_f_opt_queueing == TRUE) {
25119 			un->un_throttle = min(un->un_throttle, 3);
25120 		} else {
25121 			un->un_throttle = 1;
25122 		}
25123 		mutex_exit(SD_MUTEX(un));
25124 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25125 		mutex_enter(SD_MUTEX(un));
25126 		break;
25127 	case CMD_INCOMPLETE:
25128 		/*
25129 		 * The transport stopped with an abnormal state, fallthrough and
25130 		 * reset the target and/or bus unless selection did not complete
25131 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25132 		 * go through a target/bus reset
25133 		 */
25134 		if (pkt->pkt_state == STATE_GOT_BUS) {
25135 			break;
25136 		}
25137 		/*FALLTHROUGH*/
25138 
25139 	case CMD_TIMEOUT:
25140 	default:
25141 		/*
25142 		 * The lun may still be running the command, so a lun reset
25143 		 * should be attempted. If the lun reset fails or cannot be
25144 		 * issued, than try a target reset. Lastly try a bus reset.
25145 		 */
25146 		if ((pkt->pkt_statistics &
25147 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25148 			int reset_retval = 0;
25149 			mutex_exit(SD_MUTEX(un));
25150 			if (un->un_f_allow_bus_device_reset == TRUE) {
25151 				if (un->un_f_lun_reset_enabled == TRUE) {
25152 					reset_retval =
25153 					    scsi_reset(SD_ADDRESS(un),
25154 					    RESET_LUN);
25155 				}
25156 				if (reset_retval == 0) {
25157 					reset_retval =
25158 					    scsi_reset(SD_ADDRESS(un),
25159 					    RESET_TARGET);
25160 				}
25161 			}
25162 			if (reset_retval == 0) {
25163 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25164 			}
25165 			mutex_enter(SD_MUTEX(un));
25166 		}
25167 		break;
25168 	}
25169 
25170 	/* A device/bus reset has occurred; update the reservation status. */
25171 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25172 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25173 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25174 			un->un_resvd_status |=
25175 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25176 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25177 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25178 		}
25179 	}
25180 
25181 	/*
25182 	 * The disk has been turned off; Update the device state.
25183 	 *
25184 	 * Note: Should we be offlining the disk here?
25185 	 */
25186 	if (pkt->pkt_state == STATE_GOT_BUS) {
25187 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25188 		    "Disk not responding to selection\n");
25189 		if (un->un_state != SD_STATE_OFFLINE) {
25190 			New_state(un, SD_STATE_OFFLINE);
25191 		}
25192 	} else if (be_chatty) {
25193 		/*
25194 		 * suppress messages if they are all the same pkt reason;
25195 		 * with TQ, many (up to 256) are returned with the same
25196 		 * pkt_reason
25197 		 */
25198 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25199 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25200 			    "sd_mhd_watch_incomplete: "
25201 			    "SCSI transport failed: reason '%s'\n",
25202 			    scsi_rname(pkt->pkt_reason));
25203 		}
25204 	}
25205 	un->un_last_pkt_reason = pkt->pkt_reason;
25206 	mutex_exit(SD_MUTEX(un));
25207 }
25208 
25209 
25210 /*
25211  *    Function: sd_sname()
25212  *
25213  * Description: This is a simple little routine to return a string containing
25214  *		a printable description of command status byte for use in
25215  *		logging.
25216  *
25217  *   Arguments: status - pointer to a status byte
25218  *
25219  * Return Code: char * - string containing status description.
25220  */
25221 
25222 static char *
25223 sd_sname(uchar_t status)
25224 {
25225 	switch (status & STATUS_MASK) {
25226 	case STATUS_GOOD:
25227 		return ("good status");
25228 	case STATUS_CHECK:
25229 		return ("check condition");
25230 	case STATUS_MET:
25231 		return ("condition met");
25232 	case STATUS_BUSY:
25233 		return ("busy");
25234 	case STATUS_INTERMEDIATE:
25235 		return ("intermediate");
25236 	case STATUS_INTERMEDIATE_MET:
25237 		return ("intermediate - condition met");
25238 	case STATUS_RESERVATION_CONFLICT:
25239 		return ("reservation_conflict");
25240 	case STATUS_TERMINATED:
25241 		return ("command terminated");
25242 	case STATUS_QFULL:
25243 		return ("queue full");
25244 	default:
25245 		return ("<unknown status>");
25246 	}
25247 }
25248 
25249 
25250 /*
25251  *    Function: sd_mhd_resvd_recover()
25252  *
25253  * Description: This function adds a reservation entry to the
25254  *		sd_resv_reclaim_request list and signals the reservation
25255  *		reclaim thread that there is work pending. If the reservation
25256  *		reclaim thread has not been previously created this function
25257  *		will kick it off.
25258  *
25259  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25260  *			among multiple watches that share this callback function
25261  *
25262  *     Context: This routine is called by timeout() and is run in interrupt
25263  *		context. It must not sleep or call other functions which may
25264  *		sleep.
25265  */
25266 
25267 static void
25268 sd_mhd_resvd_recover(void *arg)
25269 {
25270 	dev_t			dev = (dev_t)arg;
25271 	struct sd_lun		*un;
25272 	struct sd_thr_request	*sd_treq = NULL;
25273 	struct sd_thr_request	*sd_cur = NULL;
25274 	struct sd_thr_request	*sd_prev = NULL;
25275 	int			already_there = 0;
25276 
25277 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25278 		return;
25279 	}
25280 
25281 	mutex_enter(SD_MUTEX(un));
25282 	un->un_resvd_timeid = NULL;
25283 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25284 		/*
25285 		 * There was a reset so don't issue the reserve, allow the
25286 		 * sd_mhd_watch_cb callback function to notice this and
25287 		 * reschedule the timeout for reservation.
25288 		 */
25289 		mutex_exit(SD_MUTEX(un));
25290 		return;
25291 	}
25292 	mutex_exit(SD_MUTEX(un));
25293 
25294 	/*
25295 	 * Add this device to the sd_resv_reclaim_request list and the
25296 	 * sd_resv_reclaim_thread should take care of the rest.
25297 	 *
25298 	 * Note: We can't sleep in this context so if the memory allocation
25299 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25300 	 * reschedule the timeout for reservation.  (4378460)
25301 	 */
25302 	sd_treq = (struct sd_thr_request *)
25303 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25304 	if (sd_treq == NULL) {
25305 		return;
25306 	}
25307 
25308 	sd_treq->sd_thr_req_next = NULL;
25309 	sd_treq->dev = dev;
25310 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25311 	if (sd_tr.srq_thr_req_head == NULL) {
25312 		sd_tr.srq_thr_req_head = sd_treq;
25313 	} else {
25314 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25315 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25316 			if (sd_cur->dev == dev) {
25317 				/*
25318 				 * already in Queue so don't log
25319 				 * another request for the device
25320 				 */
25321 				already_there = 1;
25322 				break;
25323 			}
25324 			sd_prev = sd_cur;
25325 		}
25326 		if (!already_there) {
25327 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25328 			    "logging request for %lx\n", dev);
25329 			sd_prev->sd_thr_req_next = sd_treq;
25330 		} else {
25331 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25332 		}
25333 	}
25334 
25335 	/*
25336 	 * Create a kernel thread to do the reservation reclaim and free up this
25337 	 * thread. We cannot block this thread while we go away to do the
25338 	 * reservation reclaim
25339 	 */
25340 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25341 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25342 		    sd_resv_reclaim_thread, NULL,
25343 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25344 
25345 	/* Tell the reservation reclaim thread that it has work to do */
25346 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25347 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25348 }
25349 
25350 /*
25351  *    Function: sd_resv_reclaim_thread()
25352  *
25353  * Description: This function implements the reservation reclaim operations
25354  *
25355  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25356  *		      among multiple watches that share this callback function
25357  */
25358 
25359 static void
25360 sd_resv_reclaim_thread()
25361 {
25362 	struct sd_lun		*un;
25363 	struct sd_thr_request	*sd_mhreq;
25364 
25365 	/* Wait for work */
25366 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25367 	if (sd_tr.srq_thr_req_head == NULL) {
25368 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25369 		    &sd_tr.srq_resv_reclaim_mutex);
25370 	}
25371 
25372 	/* Loop while we have work */
25373 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25374 		un = ddi_get_soft_state(sd_state,
25375 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25376 		if (un == NULL) {
25377 			/*
25378 			 * softstate structure is NULL so just
25379 			 * dequeue the request and continue
25380 			 */
25381 			sd_tr.srq_thr_req_head =
25382 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25383 			kmem_free(sd_tr.srq_thr_cur_req,
25384 			    sizeof (struct sd_thr_request));
25385 			continue;
25386 		}
25387 
25388 		/* dequeue the request */
25389 		sd_mhreq = sd_tr.srq_thr_cur_req;
25390 		sd_tr.srq_thr_req_head =
25391 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25392 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25393 
25394 		/*
25395 		 * Reclaim reservation only if SD_RESERVE is still set. There
25396 		 * may have been a call to MHIOCRELEASE before we got here.
25397 		 */
25398 		mutex_enter(SD_MUTEX(un));
25399 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25400 			/*
25401 			 * Note: The SD_LOST_RESERVE flag is cleared before
25402 			 * reclaiming the reservation. If this is done after the
25403 			 * call to sd_reserve_release a reservation loss in the
25404 			 * window between pkt completion of reserve cmd and
25405 			 * mutex_enter below may not be recognized
25406 			 */
25407 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25408 			mutex_exit(SD_MUTEX(un));
25409 
25410 			if (sd_reserve_release(sd_mhreq->dev,
25411 			    SD_RESERVE) == 0) {
25412 				mutex_enter(SD_MUTEX(un));
25413 				un->un_resvd_status |= SD_RESERVE;
25414 				mutex_exit(SD_MUTEX(un));
25415 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25416 				    "sd_resv_reclaim_thread: "
25417 				    "Reservation Recovered\n");
25418 			} else {
25419 				mutex_enter(SD_MUTEX(un));
25420 				un->un_resvd_status |= SD_LOST_RESERVE;
25421 				mutex_exit(SD_MUTEX(un));
25422 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25423 				    "sd_resv_reclaim_thread: Failed "
25424 				    "Reservation Recovery\n");
25425 			}
25426 		} else {
25427 			mutex_exit(SD_MUTEX(un));
25428 		}
25429 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25430 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25431 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25432 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25433 		/*
25434 		 * wakeup the destroy thread if anyone is waiting on
25435 		 * us to complete.
25436 		 */
25437 		cv_signal(&sd_tr.srq_inprocess_cv);
25438 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25439 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25440 	}
25441 
25442 	/*
25443 	 * cleanup the sd_tr structure now that this thread will not exist
25444 	 */
25445 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25446 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25447 	sd_tr.srq_resv_reclaim_thread = NULL;
25448 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25449 	thread_exit();
25450 }
25451 
25452 
25453 /*
25454  *    Function: sd_rmv_resv_reclaim_req()
25455  *
25456  * Description: This function removes any pending reservation reclaim requests
25457  *		for the specified device.
25458  *
25459  *   Arguments: dev - the device 'dev_t'
25460  */
25461 
25462 static void
25463 sd_rmv_resv_reclaim_req(dev_t dev)
25464 {
25465 	struct sd_thr_request *sd_mhreq;
25466 	struct sd_thr_request *sd_prev;
25467 
25468 	/* Remove a reservation reclaim request from the list */
25469 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25470 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25471 		/*
25472 		 * We are attempting to reinstate reservation for
25473 		 * this device. We wait for sd_reserve_release()
25474 		 * to return before we return.
25475 		 */
25476 		cv_wait(&sd_tr.srq_inprocess_cv,
25477 		    &sd_tr.srq_resv_reclaim_mutex);
25478 	} else {
25479 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25480 		if (sd_mhreq && sd_mhreq->dev == dev) {
25481 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25482 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25483 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25484 			return;
25485 		}
25486 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25487 			if (sd_mhreq && sd_mhreq->dev == dev) {
25488 				break;
25489 			}
25490 			sd_prev = sd_mhreq;
25491 		}
25492 		if (sd_mhreq != NULL) {
25493 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25494 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25495 		}
25496 	}
25497 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25498 }
25499 
25500 
25501 /*
25502  *    Function: sd_mhd_reset_notify_cb()
25503  *
25504  * Description: This is a call back function for scsi_reset_notify. This
25505  *		function updates the softstate reserved status and logs the
25506  *		reset. The driver scsi watch facility callback function
25507  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25508  *		will reclaim the reservation.
25509  *
25510  *   Arguments: arg  - driver soft state (unit) structure
25511  */
25512 
25513 static void
25514 sd_mhd_reset_notify_cb(caddr_t arg)
25515 {
25516 	struct sd_lun *un = (struct sd_lun *)arg;
25517 
25518 	mutex_enter(SD_MUTEX(un));
25519 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25520 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25521 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25522 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25523 	}
25524 	mutex_exit(SD_MUTEX(un));
25525 }
25526 
25527 
25528 /*
25529  *    Function: sd_take_ownership()
25530  *
25531  * Description: This routine implements an algorithm to achieve a stable
25532  *		reservation on disks which don't implement priority reserve,
25533  *		and makes sure that other host lose re-reservation attempts.
25534  *		This algorithm contains of a loop that keeps issuing the RESERVE
25535  *		for some period of time (min_ownership_delay, default 6 seconds)
25536  *		During that loop, it looks to see if there has been a bus device
25537  *		reset or bus reset (both of which cause an existing reservation
25538  *		to be lost). If the reservation is lost issue RESERVE until a
25539  *		period of min_ownership_delay with no resets has gone by, or
25540  *		until max_ownership_delay has expired. This loop ensures that
25541  *		the host really did manage to reserve the device, in spite of
25542  *		resets. The looping for min_ownership_delay (default six
25543  *		seconds) is important to early generation clustering products,
25544  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25545  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25546  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25547  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25548  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25549  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25550  *		no longer "owns" the disk and will have panicked itself.  Thus,
25551  *		the host issuing the MHIOCTKOWN is assured (with timing
25552  *		dependencies) that by the time it actually starts to use the
25553  *		disk for real work, the old owner is no longer accessing it.
25554  *
25555  *		min_ownership_delay is the minimum amount of time for which the
25556  *		disk must be reserved continuously devoid of resets before the
25557  *		MHIOCTKOWN ioctl will return success.
25558  *
25559  *		max_ownership_delay indicates the amount of time by which the
25560  *		take ownership should succeed or timeout with an error.
25561  *
25562  *   Arguments: dev - the device 'dev_t'
25563  *		*p  - struct containing timing info.
25564  *
25565  * Return Code: 0 for success or error code
25566  */
25567 
25568 static int
25569 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25570 {
25571 	struct sd_lun	*un;
25572 	int		rval;
25573 	int		err;
25574 	int		reservation_count   = 0;
25575 	int		min_ownership_delay =  6000000; /* in usec */
25576 	int		max_ownership_delay = 30000000; /* in usec */
25577 	clock_t		start_time;	/* starting time of this algorithm */
25578 	clock_t		end_time;	/* time limit for giving up */
25579 	clock_t		ownership_time;	/* time limit for stable ownership */
25580 	clock_t		current_time;
25581 	clock_t		previous_current_time;
25582 
25583 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25584 		return (ENXIO);
25585 	}
25586 
25587 	/*
25588 	 * Attempt a device reservation. A priority reservation is requested.
25589 	 */
25590 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25591 	    != SD_SUCCESS) {
25592 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25593 		    "sd_take_ownership: return(1)=%d\n", rval);
25594 		return (rval);
25595 	}
25596 
25597 	/* Update the softstate reserved status to indicate the reservation */
25598 	mutex_enter(SD_MUTEX(un));
25599 	un->un_resvd_status |= SD_RESERVE;
25600 	un->un_resvd_status &=
25601 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25602 	mutex_exit(SD_MUTEX(un));
25603 
25604 	if (p != NULL) {
25605 		if (p->min_ownership_delay != 0) {
25606 			min_ownership_delay = p->min_ownership_delay * 1000;
25607 		}
25608 		if (p->max_ownership_delay != 0) {
25609 			max_ownership_delay = p->max_ownership_delay * 1000;
25610 		}
25611 	}
25612 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25613 	    "sd_take_ownership: min, max delays: %d, %d\n",
25614 	    min_ownership_delay, max_ownership_delay);
25615 
25616 	start_time = ddi_get_lbolt();
25617 	current_time	= start_time;
25618 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25619 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25620 
25621 	while (current_time - end_time < 0) {
25622 		delay(drv_usectohz(500000));
25623 
25624 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25625 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25626 				mutex_enter(SD_MUTEX(un));
25627 				rval = (un->un_resvd_status &
25628 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25629 				mutex_exit(SD_MUTEX(un));
25630 				break;
25631 			}
25632 		}
25633 		previous_current_time = current_time;
25634 		current_time = ddi_get_lbolt();
25635 		mutex_enter(SD_MUTEX(un));
25636 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25637 			ownership_time = ddi_get_lbolt() +
25638 			    drv_usectohz(min_ownership_delay);
25639 			reservation_count = 0;
25640 		} else {
25641 			reservation_count++;
25642 		}
25643 		un->un_resvd_status |= SD_RESERVE;
25644 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25645 		mutex_exit(SD_MUTEX(un));
25646 
25647 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25648 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25649 		    "reservation=%s\n", (current_time - previous_current_time),
25650 		    reservation_count ? "ok" : "reclaimed");
25651 
25652 		if (current_time - ownership_time >= 0 &&
25653 		    reservation_count >= 4) {
25654 			rval = 0; /* Achieved a stable ownership */
25655 			break;
25656 		}
25657 		if (current_time - end_time >= 0) {
25658 			rval = EACCES; /* No ownership in max possible time */
25659 			break;
25660 		}
25661 	}
25662 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25663 	    "sd_take_ownership: return(2)=%d\n", rval);
25664 	return (rval);
25665 }
25666 
25667 
25668 /*
25669  *    Function: sd_reserve_release()
25670  *
25671  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25672  *		PRIORITY RESERVE commands based on a user specified command type
25673  *
25674  *   Arguments: dev - the device 'dev_t'
25675  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25676  *		      SD_RESERVE, SD_RELEASE
25677  *
25678  * Return Code: 0 or Error Code
25679  */
25680 
25681 static int
25682 sd_reserve_release(dev_t dev, int cmd)
25683 {
25684 	struct uscsi_cmd	*com = NULL;
25685 	struct sd_lun		*un = NULL;
25686 	char			cdb[CDB_GROUP0];
25687 	int			rval;
25688 
25689 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25690 	    (cmd == SD_PRIORITY_RESERVE));
25691 
25692 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25693 		return (ENXIO);
25694 	}
25695 
25696 	/* instantiate and initialize the command and cdb */
25697 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25698 	bzero(cdb, CDB_GROUP0);
25699 	com->uscsi_flags   = USCSI_SILENT;
25700 	com->uscsi_timeout = un->un_reserve_release_time;
25701 	com->uscsi_cdblen  = CDB_GROUP0;
25702 	com->uscsi_cdb	   = cdb;
25703 	if (cmd == SD_RELEASE) {
25704 		cdb[0] = SCMD_RELEASE;
25705 	} else {
25706 		cdb[0] = SCMD_RESERVE;
25707 	}
25708 
25709 	/* Send the command. */
25710 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25711 	    SD_PATH_STANDARD);
25712 
25713 	/*
25714 	 * "break" a reservation that is held by another host, by issuing a
25715 	 * reset if priority reserve is desired, and we could not get the
25716 	 * device.
25717 	 */
25718 	if ((cmd == SD_PRIORITY_RESERVE) &&
25719 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25720 		/*
25721 		 * First try to reset the LUN. If we cannot, then try a target
25722 		 * reset, followed by a bus reset if the target reset fails.
25723 		 */
25724 		int reset_retval = 0;
25725 		if (un->un_f_lun_reset_enabled == TRUE) {
25726 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25727 		}
25728 		if (reset_retval == 0) {
25729 			/* The LUN reset either failed or was not issued */
25730 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25731 		}
25732 		if ((reset_retval == 0) &&
25733 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25734 			rval = EIO;
25735 			kmem_free(com, sizeof (*com));
25736 			return (rval);
25737 		}
25738 
25739 		bzero(com, sizeof (struct uscsi_cmd));
25740 		com->uscsi_flags   = USCSI_SILENT;
25741 		com->uscsi_cdb	   = cdb;
25742 		com->uscsi_cdblen  = CDB_GROUP0;
25743 		com->uscsi_timeout = 5;
25744 
25745 		/*
25746 		 * Reissue the last reserve command, this time without request
25747 		 * sense.  Assume that it is just a regular reserve command.
25748 		 */
25749 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
25750 		    SD_PATH_STANDARD);
25751 	}
25752 
25753 	/* Return an error if still getting a reservation conflict. */
25754 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25755 		rval = EACCES;
25756 	}
25757 
25758 	kmem_free(com, sizeof (*com));
25759 	return (rval);
25760 }
25761 
25762 
25763 #define	SD_NDUMP_RETRIES	12
25764 /*
25765  *	System Crash Dump routine
25766  */
25767 
25768 static int
25769 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25770 {
25771 	int		instance;
25772 	int		partition;
25773 	int		i;
25774 	int		err;
25775 	struct sd_lun	*un;
25776 	struct scsi_pkt *wr_pktp;
25777 	struct buf	*wr_bp;
25778 	struct buf	wr_buf;
25779 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25780 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25781 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25782 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25783 	size_t		io_start_offset;
25784 	int		doing_rmw = FALSE;
25785 	int		rval;
25786 	ssize_t		dma_resid;
25787 	daddr_t		oblkno;
25788 	diskaddr_t	nblks = 0;
25789 	diskaddr_t	start_block;
25790 
25791 	instance = SDUNIT(dev);
25792 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25793 	    !SD_IS_VALID_LABEL(un) || ISCD(un)) {
25794 		return (ENXIO);
25795 	}
25796 
25797 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25798 
25799 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25800 
25801 	partition = SDPART(dev);
25802 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25803 
25804 	if (!(NOT_DEVBSIZE(un))) {
25805 		int secmask = 0;
25806 		int blknomask = 0;
25807 
25808 		blknomask = (un->un_tgt_blocksize / DEV_BSIZE) - 1;
25809 		secmask = un->un_tgt_blocksize - 1;
25810 
25811 		if (blkno & blknomask) {
25812 			SD_TRACE(SD_LOG_DUMP, un,
25813 			    "sddump: dump start block not modulo %d\n",
25814 			    un->un_tgt_blocksize);
25815 			return (EINVAL);
25816 		}
25817 
25818 		if ((nblk * DEV_BSIZE) & secmask) {
25819 			SD_TRACE(SD_LOG_DUMP, un,
25820 			    "sddump: dump length not modulo %d\n",
25821 			    un->un_tgt_blocksize);
25822 			return (EINVAL);
25823 		}
25824 
25825 	}
25826 
25827 	/* Validate blocks to dump at against partition size. */
25828 
25829 	(void) cmlb_partinfo(un->un_cmlbhandle, partition,
25830 	    &nblks, &start_block, NULL, NULL, (void *)SD_PATH_DIRECT);
25831 
25832 	if (NOT_DEVBSIZE(un)) {
25833 		if ((blkno + nblk) > nblks) {
25834 			SD_TRACE(SD_LOG_DUMP, un,
25835 			    "sddump: dump range larger than partition: "
25836 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25837 			    blkno, nblk, nblks);
25838 			return (EINVAL);
25839 		}
25840 	} else {
25841 		if (((blkno / (un->un_tgt_blocksize / DEV_BSIZE)) +
25842 		    (nblk / (un->un_tgt_blocksize / DEV_BSIZE))) > nblks) {
25843 			SD_TRACE(SD_LOG_DUMP, un,
25844 			    "sddump: dump range larger than partition: "
25845 			    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25846 			    blkno, nblk, nblks);
25847 			return (EINVAL);
25848 		}
25849 	}
25850 
25851 	mutex_enter(&un->un_pm_mutex);
25852 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25853 		struct scsi_pkt *start_pktp;
25854 
25855 		mutex_exit(&un->un_pm_mutex);
25856 
25857 		/*
25858 		 * use pm framework to power on HBA 1st
25859 		 */
25860 		(void) pm_raise_power(SD_DEVINFO(un), 0,
25861 		    SD_PM_STATE_ACTIVE(un));
25862 
25863 		/*
25864 		 * Dump no long uses sdpower to power on a device, it's
25865 		 * in-line here so it can be done in polled mode.
25866 		 */
25867 
25868 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25869 
25870 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25871 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25872 
25873 		if (start_pktp == NULL) {
25874 			/* We were not given a SCSI packet, fail. */
25875 			return (EIO);
25876 		}
25877 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25878 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25879 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25880 		start_pktp->pkt_flags = FLAG_NOINTR;
25881 
25882 		mutex_enter(SD_MUTEX(un));
25883 		SD_FILL_SCSI1_LUN(un, start_pktp);
25884 		mutex_exit(SD_MUTEX(un));
25885 		/*
25886 		 * Scsi_poll returns 0 (success) if the command completes and
25887 		 * the status block is STATUS_GOOD.
25888 		 */
25889 		if (sd_scsi_poll(un, start_pktp) != 0) {
25890 			scsi_destroy_pkt(start_pktp);
25891 			return (EIO);
25892 		}
25893 		scsi_destroy_pkt(start_pktp);
25894 		(void) sd_pm_state_change(un, SD_PM_STATE_ACTIVE(un),
25895 		    SD_PM_STATE_CHANGE);
25896 	} else {
25897 		mutex_exit(&un->un_pm_mutex);
25898 	}
25899 
25900 	mutex_enter(SD_MUTEX(un));
25901 	un->un_throttle = 0;
25902 
25903 	/*
25904 	 * The first time through, reset the specific target device.
25905 	 * However, when cpr calls sddump we know that sd is in a
25906 	 * a good state so no bus reset is required.
25907 	 * Clear sense data via Request Sense cmd.
25908 	 * In sddump we don't care about allow_bus_device_reset anymore
25909 	 */
25910 
25911 	if ((un->un_state != SD_STATE_SUSPENDED) &&
25912 	    (un->un_state != SD_STATE_DUMPING)) {
25913 
25914 		New_state(un, SD_STATE_DUMPING);
25915 
25916 		if (un->un_f_is_fibre == FALSE) {
25917 			mutex_exit(SD_MUTEX(un));
25918 			/*
25919 			 * Attempt a bus reset for parallel scsi.
25920 			 *
25921 			 * Note: A bus reset is required because on some host
25922 			 * systems (i.e. E420R) a bus device reset is
25923 			 * insufficient to reset the state of the target.
25924 			 *
25925 			 * Note: Don't issue the reset for fibre-channel,
25926 			 * because this tends to hang the bus (loop) for
25927 			 * too long while everyone is logging out and in
25928 			 * and the deadman timer for dumping will fire
25929 			 * before the dump is complete.
25930 			 */
25931 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25932 				mutex_enter(SD_MUTEX(un));
25933 				Restore_state(un);
25934 				mutex_exit(SD_MUTEX(un));
25935 				return (EIO);
25936 			}
25937 
25938 			/* Delay to give the device some recovery time. */
25939 			drv_usecwait(10000);
25940 
25941 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
25942 				SD_INFO(SD_LOG_DUMP, un,
25943 				    "sddump: sd_send_polled_RQS failed\n");
25944 			}
25945 			mutex_enter(SD_MUTEX(un));
25946 		}
25947 	}
25948 
25949 	/*
25950 	 * Convert the partition-relative block number to a
25951 	 * disk physical block number.
25952 	 */
25953 	if (NOT_DEVBSIZE(un)) {
25954 		blkno += start_block;
25955 	} else {
25956 		blkno = blkno / (un->un_tgt_blocksize / DEV_BSIZE);
25957 		blkno += start_block;
25958 	}
25959 
25960 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25961 
25962 
25963 	/*
25964 	 * Check if the device has a non-512 block size.
25965 	 */
25966 	wr_bp = NULL;
25967 	if (NOT_DEVBSIZE(un)) {
25968 		tgt_byte_offset = blkno * un->un_sys_blocksize;
25969 		tgt_byte_count = nblk * un->un_sys_blocksize;
25970 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25971 		    (tgt_byte_count % un->un_tgt_blocksize)) {
25972 			doing_rmw = TRUE;
25973 			/*
25974 			 * Calculate the block number and number of block
25975 			 * in terms of the media block size.
25976 			 */
25977 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25978 			tgt_nblk =
25979 			    ((tgt_byte_offset + tgt_byte_count +
25980 			    (un->un_tgt_blocksize - 1)) /
25981 			    un->un_tgt_blocksize) - tgt_blkno;
25982 
25983 			/*
25984 			 * Invoke the routine which is going to do read part
25985 			 * of read-modify-write.
25986 			 * Note that this routine returns a pointer to
25987 			 * a valid bp in wr_bp.
25988 			 */
25989 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25990 			    &wr_bp);
25991 			if (err) {
25992 				mutex_exit(SD_MUTEX(un));
25993 				return (err);
25994 			}
25995 			/*
25996 			 * Offset is being calculated as -
25997 			 * (original block # * system block size) -
25998 			 * (new block # * target block size)
25999 			 */
26000 			io_start_offset =
26001 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26002 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26003 
26004 			ASSERT((io_start_offset >= 0) &&
26005 			    (io_start_offset < un->un_tgt_blocksize));
26006 			/*
26007 			 * Do the modify portion of read modify write.
26008 			 */
26009 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26010 			    (size_t)nblk * un->un_sys_blocksize);
26011 		} else {
26012 			doing_rmw = FALSE;
26013 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26014 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26015 		}
26016 
26017 		/* Convert blkno and nblk to target blocks */
26018 		blkno = tgt_blkno;
26019 		nblk = tgt_nblk;
26020 	} else {
26021 		wr_bp = &wr_buf;
26022 		bzero(wr_bp, sizeof (struct buf));
26023 		wr_bp->b_flags		= B_BUSY;
26024 		wr_bp->b_un.b_addr	= addr;
26025 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26026 		wr_bp->b_resid		= 0;
26027 	}
26028 
26029 	mutex_exit(SD_MUTEX(un));
26030 
26031 	/*
26032 	 * Obtain a SCSI packet for the write command.
26033 	 * It should be safe to call the allocator here without
26034 	 * worrying about being locked for DVMA mapping because
26035 	 * the address we're passed is already a DVMA mapping
26036 	 *
26037 	 * We are also not going to worry about semaphore ownership
26038 	 * in the dump buffer. Dumping is single threaded at present.
26039 	 */
26040 
26041 	wr_pktp = NULL;
26042 
26043 	dma_resid = wr_bp->b_bcount;
26044 	oblkno = blkno;
26045 
26046 	if (!(NOT_DEVBSIZE(un))) {
26047 		nblk = nblk / (un->un_tgt_blocksize / DEV_BSIZE);
26048 	}
26049 
26050 	while (dma_resid != 0) {
26051 
26052 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26053 		wr_bp->b_flags &= ~B_ERROR;
26054 
26055 		if (un->un_partial_dma_supported == 1) {
26056 			blkno = oblkno +
26057 			    ((wr_bp->b_bcount - dma_resid) /
26058 			    un->un_tgt_blocksize);
26059 			nblk = dma_resid / un->un_tgt_blocksize;
26060 
26061 			if (wr_pktp) {
26062 				/*
26063 				 * Partial DMA transfers after initial transfer
26064 				 */
26065 				rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26066 				    blkno, nblk);
26067 			} else {
26068 				/* Initial transfer */
26069 				rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26070 				    un->un_pkt_flags, NULL_FUNC, NULL,
26071 				    blkno, nblk);
26072 			}
26073 		} else {
26074 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26075 			    0, NULL_FUNC, NULL, blkno, nblk);
26076 		}
26077 
26078 		if (rval == 0) {
26079 			/* We were given a SCSI packet, continue. */
26080 			break;
26081 		}
26082 
26083 		if (i == 0) {
26084 			if (wr_bp->b_flags & B_ERROR) {
26085 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26086 				    "no resources for dumping; "
26087 				    "error code: 0x%x, retrying",
26088 				    geterror(wr_bp));
26089 			} else {
26090 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26091 				    "no resources for dumping; retrying");
26092 			}
26093 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26094 			if (wr_bp->b_flags & B_ERROR) {
26095 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26096 				    "no resources for dumping; error code: "
26097 				    "0x%x, retrying\n", geterror(wr_bp));
26098 			}
26099 		} else {
26100 			if (wr_bp->b_flags & B_ERROR) {
26101 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26102 				    "no resources for dumping; "
26103 				    "error code: 0x%x, retries failed, "
26104 				    "giving up.\n", geterror(wr_bp));
26105 			} else {
26106 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26107 				    "no resources for dumping; "
26108 				    "retries failed, giving up.\n");
26109 			}
26110 			mutex_enter(SD_MUTEX(un));
26111 			Restore_state(un);
26112 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26113 				mutex_exit(SD_MUTEX(un));
26114 				scsi_free_consistent_buf(wr_bp);
26115 			} else {
26116 				mutex_exit(SD_MUTEX(un));
26117 			}
26118 			return (EIO);
26119 		}
26120 		drv_usecwait(10000);
26121 	}
26122 
26123 	if (un->un_partial_dma_supported == 1) {
26124 		/*
26125 		 * save the resid from PARTIAL_DMA
26126 		 */
26127 		dma_resid = wr_pktp->pkt_resid;
26128 		if (dma_resid != 0)
26129 			nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26130 		wr_pktp->pkt_resid = 0;
26131 	} else {
26132 		dma_resid = 0;
26133 	}
26134 
26135 	/* SunBug 1222170 */
26136 	wr_pktp->pkt_flags = FLAG_NOINTR;
26137 
26138 	err = EIO;
26139 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26140 
26141 		/*
26142 		 * Scsi_poll returns 0 (success) if the command completes and
26143 		 * the status block is STATUS_GOOD.  We should only check
26144 		 * errors if this condition is not true.  Even then we should
26145 		 * send our own request sense packet only if we have a check
26146 		 * condition and auto request sense has not been performed by
26147 		 * the hba.
26148 		 */
26149 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26150 
26151 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26152 		    (wr_pktp->pkt_resid == 0)) {
26153 			err = SD_SUCCESS;
26154 			break;
26155 		}
26156 
26157 		/*
26158 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26159 		 */
26160 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26161 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26162 			    "Error while dumping state...Device is gone\n");
26163 			break;
26164 		}
26165 
26166 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26167 			SD_INFO(SD_LOG_DUMP, un,
26168 			    "sddump: write failed with CHECK, try # %d\n", i);
26169 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26170 				(void) sd_send_polled_RQS(un);
26171 			}
26172 
26173 			continue;
26174 		}
26175 
26176 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26177 			int reset_retval = 0;
26178 
26179 			SD_INFO(SD_LOG_DUMP, un,
26180 			    "sddump: write failed with BUSY, try # %d\n", i);
26181 
26182 			if (un->un_f_lun_reset_enabled == TRUE) {
26183 				reset_retval = scsi_reset(SD_ADDRESS(un),
26184 				    RESET_LUN);
26185 			}
26186 			if (reset_retval == 0) {
26187 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26188 			}
26189 			(void) sd_send_polled_RQS(un);
26190 
26191 		} else {
26192 			SD_INFO(SD_LOG_DUMP, un,
26193 			    "sddump: write failed with 0x%x, try # %d\n",
26194 			    SD_GET_PKT_STATUS(wr_pktp), i);
26195 			mutex_enter(SD_MUTEX(un));
26196 			sd_reset_target(un, wr_pktp);
26197 			mutex_exit(SD_MUTEX(un));
26198 		}
26199 
26200 		/*
26201 		 * If we are not getting anywhere with lun/target resets,
26202 		 * let's reset the bus.
26203 		 */
26204 		if (i == SD_NDUMP_RETRIES/2) {
26205 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26206 			(void) sd_send_polled_RQS(un);
26207 		}
26208 	}
26209 	}
26210 
26211 	scsi_destroy_pkt(wr_pktp);
26212 	mutex_enter(SD_MUTEX(un));
26213 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26214 		mutex_exit(SD_MUTEX(un));
26215 		scsi_free_consistent_buf(wr_bp);
26216 	} else {
26217 		mutex_exit(SD_MUTEX(un));
26218 	}
26219 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26220 	return (err);
26221 }
26222 
26223 /*
26224  *    Function: sd_scsi_poll()
26225  *
26226  * Description: This is a wrapper for the scsi_poll call.
26227  *
26228  *   Arguments: sd_lun - The unit structure
26229  *              scsi_pkt - The scsi packet being sent to the device.
26230  *
26231  * Return Code: 0 - Command completed successfully with good status
26232  *             -1 - Command failed.  This could indicate a check condition
26233  *                  or other status value requiring recovery action.
26234  *
26235  * NOTE: This code is only called off sddump().
26236  */
26237 
26238 static int
26239 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26240 {
26241 	int status;
26242 
26243 	ASSERT(un != NULL);
26244 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26245 	ASSERT(pktp != NULL);
26246 
26247 	status = SD_SUCCESS;
26248 
26249 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26250 		pktp->pkt_flags |= un->un_tagflags;
26251 		pktp->pkt_flags &= ~FLAG_NODISCON;
26252 	}
26253 
26254 	status = sd_ddi_scsi_poll(pktp);
26255 	/*
26256 	 * Scsi_poll returns 0 (success) if the command completes and the
26257 	 * status block is STATUS_GOOD.  We should only check errors if this
26258 	 * condition is not true.  Even then we should send our own request
26259 	 * sense packet only if we have a check condition and auto
26260 	 * request sense has not been performed by the hba.
26261 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26262 	 */
26263 	if ((status != SD_SUCCESS) &&
26264 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26265 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26266 	    (pktp->pkt_reason != CMD_DEV_GONE))
26267 		(void) sd_send_polled_RQS(un);
26268 
26269 	return (status);
26270 }
26271 
26272 /*
26273  *    Function: sd_send_polled_RQS()
26274  *
26275  * Description: This sends the request sense command to a device.
26276  *
26277  *   Arguments: sd_lun - The unit structure
26278  *
26279  * Return Code: 0 - Command completed successfully with good status
26280  *             -1 - Command failed.
26281  *
26282  */
26283 
26284 static int
26285 sd_send_polled_RQS(struct sd_lun *un)
26286 {
26287 	int	ret_val;
26288 	struct	scsi_pkt	*rqs_pktp;
26289 	struct	buf		*rqs_bp;
26290 
26291 	ASSERT(un != NULL);
26292 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26293 
26294 	ret_val = SD_SUCCESS;
26295 
26296 	rqs_pktp = un->un_rqs_pktp;
26297 	rqs_bp	 = un->un_rqs_bp;
26298 
26299 	mutex_enter(SD_MUTEX(un));
26300 
26301 	if (un->un_sense_isbusy) {
26302 		ret_val = SD_FAILURE;
26303 		mutex_exit(SD_MUTEX(un));
26304 		return (ret_val);
26305 	}
26306 
26307 	/*
26308 	 * If the request sense buffer (and packet) is not in use,
26309 	 * let's set the un_sense_isbusy and send our packet
26310 	 */
26311 	un->un_sense_isbusy 	= 1;
26312 	rqs_pktp->pkt_resid  	= 0;
26313 	rqs_pktp->pkt_reason 	= 0;
26314 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26315 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26316 
26317 	mutex_exit(SD_MUTEX(un));
26318 
26319 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26320 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26321 
26322 	/*
26323 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26324 	 * axle - it has a call into us!
26325 	 */
26326 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26327 		SD_INFO(SD_LOG_COMMON, un,
26328 		    "sd_send_polled_RQS: RQS failed\n");
26329 	}
26330 
26331 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26332 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26333 
26334 	mutex_enter(SD_MUTEX(un));
26335 	un->un_sense_isbusy = 0;
26336 	mutex_exit(SD_MUTEX(un));
26337 
26338 	return (ret_val);
26339 }
26340 
26341 /*
26342  * Defines needed for localized version of the scsi_poll routine.
26343  */
26344 #define	CSEC		10000			/* usecs */
26345 #define	SEC_TO_CSEC	(1000000/CSEC)
26346 
26347 /*
26348  *    Function: sd_ddi_scsi_poll()
26349  *
26350  * Description: Localized version of the scsi_poll routine.  The purpose is to
26351  *		send a scsi_pkt to a device as a polled command.  This version
26352  *		is to ensure more robust handling of transport errors.
26353  *		Specifically this routine cures not ready, coming ready
26354  *		transition for power up and reset of sonoma's.  This can take
26355  *		up to 45 seconds for power-on and 20 seconds for reset of a
26356  * 		sonoma lun.
26357  *
26358  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26359  *
26360  * Return Code: 0 - Command completed successfully with good status
26361  *             -1 - Command failed.
26362  *
26363  * NOTE: This code is almost identical to scsi_poll, however before 6668774 can
26364  * be fixed (removing this code), we need to determine how to handle the
26365  * KEY_UNIT_ATTENTION condition below in conditions not as limited as sddump().
26366  *
26367  * NOTE: This code is only called off sddump().
26368  */
26369 static int
26370 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26371 {
26372 	int			rval = -1;
26373 	int			savef;
26374 	long			savet;
26375 	void			(*savec)();
26376 	int			timeout;
26377 	int			busy_count;
26378 	int			poll_delay;
26379 	int			rc;
26380 	uint8_t			*sensep;
26381 	struct scsi_arq_status	*arqstat;
26382 	extern int		do_polled_io;
26383 
26384 	ASSERT(pkt->pkt_scbp);
26385 
26386 	/*
26387 	 * save old flags..
26388 	 */
26389 	savef = pkt->pkt_flags;
26390 	savec = pkt->pkt_comp;
26391 	savet = pkt->pkt_time;
26392 
26393 	pkt->pkt_flags |= FLAG_NOINTR;
26394 
26395 	/*
26396 	 * XXX there is nothing in the SCSA spec that states that we should not
26397 	 * do a callback for polled cmds; however, removing this will break sd
26398 	 * and probably other target drivers
26399 	 */
26400 	pkt->pkt_comp = NULL;
26401 
26402 	/*
26403 	 * we don't like a polled command without timeout.
26404 	 * 60 seconds seems long enough.
26405 	 */
26406 	if (pkt->pkt_time == 0)
26407 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26408 
26409 	/*
26410 	 * Send polled cmd.
26411 	 *
26412 	 * We do some error recovery for various errors.  Tran_busy,
26413 	 * queue full, and non-dispatched commands are retried every 10 msec.
26414 	 * as they are typically transient failures.  Busy status and Not
26415 	 * Ready are retried every second as this status takes a while to
26416 	 * change.
26417 	 */
26418 	timeout = pkt->pkt_time * SEC_TO_CSEC;
26419 
26420 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26421 		/*
26422 		 * Initialize pkt status variables.
26423 		 */
26424 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26425 
26426 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26427 			if (rc != TRAN_BUSY) {
26428 				/* Transport failed - give up. */
26429 				break;
26430 			} else {
26431 				/* Transport busy - try again. */
26432 				poll_delay = 1 * CSEC;		/* 10 msec. */
26433 			}
26434 		} else {
26435 			/*
26436 			 * Transport accepted - check pkt status.
26437 			 */
26438 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26439 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26440 			    (rc == STATUS_CHECK) &&
26441 			    (pkt->pkt_state & STATE_ARQ_DONE)) {
26442 				arqstat =
26443 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26444 				sensep = (uint8_t *)&arqstat->sts_sensedata;
26445 			} else {
26446 				sensep = NULL;
26447 			}
26448 
26449 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26450 			    (rc == STATUS_GOOD)) {
26451 				/* No error - we're done */
26452 				rval = 0;
26453 				break;
26454 
26455 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26456 				/* Lost connection - give up */
26457 				break;
26458 
26459 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26460 			    (pkt->pkt_state == 0)) {
26461 				/* Pkt not dispatched - try again. */
26462 				poll_delay = 1 * CSEC;		/* 10 msec. */
26463 
26464 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26465 			    (rc == STATUS_QFULL)) {
26466 				/* Queue full - try again. */
26467 				poll_delay = 1 * CSEC;		/* 10 msec. */
26468 
26469 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26470 			    (rc == STATUS_BUSY)) {
26471 				/* Busy - try again. */
26472 				poll_delay = 100 * CSEC;	/* 1 sec. */
26473 				busy_count += (SEC_TO_CSEC - 1);
26474 
26475 			} else if ((sensep != NULL) &&
26476 			    (scsi_sense_key(sensep) == KEY_UNIT_ATTENTION)) {
26477 				/*
26478 				 * Unit Attention - try again.
26479 				 * Pretend it took 1 sec.
26480 				 * NOTE: 'continue' avoids poll_delay
26481 				 */
26482 				busy_count += (SEC_TO_CSEC - 1);
26483 				continue;
26484 
26485 			} else if ((sensep != NULL) &&
26486 			    (scsi_sense_key(sensep) == KEY_NOT_READY) &&
26487 			    (scsi_sense_asc(sensep) == 0x04) &&
26488 			    (scsi_sense_ascq(sensep) == 0x01)) {
26489 				/*
26490 				 * Not ready -> ready - try again.
26491 				 * 04h/01h: LUN IS IN PROCESS OF BECOMING READY
26492 				 * ...same as STATUS_BUSY
26493 				 */
26494 				poll_delay = 100 * CSEC;	/* 1 sec. */
26495 				busy_count += (SEC_TO_CSEC - 1);
26496 
26497 			} else {
26498 				/* BAD status - give up. */
26499 				break;
26500 			}
26501 		}
26502 
26503 		if (((curthread->t_flag & T_INTR_THREAD) == 0) &&
26504 		    !do_polled_io) {
26505 			delay(drv_usectohz(poll_delay));
26506 		} else {
26507 			/* we busy wait during cpr_dump or interrupt threads */
26508 			drv_usecwait(poll_delay);
26509 		}
26510 	}
26511 
26512 	pkt->pkt_flags = savef;
26513 	pkt->pkt_comp = savec;
26514 	pkt->pkt_time = savet;
26515 
26516 	/* return on error */
26517 	if (rval)
26518 		return (rval);
26519 
26520 	/*
26521 	 * This is not a performance critical code path.
26522 	 *
26523 	 * As an accommodation for scsi_poll callers, to avoid ddi_dma_sync()
26524 	 * issues associated with looking at DMA memory prior to
26525 	 * scsi_pkt_destroy(), we scsi_sync_pkt() prior to return.
26526 	 */
26527 	scsi_sync_pkt(pkt);
26528 	return (0);
26529 }
26530 
26531 
26532 
26533 /*
26534  *    Function: sd_persistent_reservation_in_read_keys
26535  *
26536  * Description: This routine is the driver entry point for handling CD-ROM
26537  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26538  *		by sending the SCSI-3 PRIN commands to the device.
26539  *		Processes the read keys command response by copying the
26540  *		reservation key information into the user provided buffer.
26541  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26542  *
26543  *   Arguments: un   -  Pointer to soft state struct for the target.
26544  *		usrp -	user provided pointer to multihost Persistent In Read
26545  *			Keys structure (mhioc_inkeys_t)
26546  *		flag -	this argument is a pass through to ddi_copyxxx()
26547  *			directly from the mode argument of ioctl().
26548  *
26549  * Return Code: 0   - Success
26550  *		EACCES
26551  *		ENOTSUP
26552  *		errno return code from sd_send_scsi_cmd()
26553  *
26554  *     Context: Can sleep. Does not return until command is completed.
26555  */
26556 
26557 static int
26558 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26559     mhioc_inkeys_t *usrp, int flag)
26560 {
26561 #ifdef _MULTI_DATAMODEL
26562 	struct mhioc_key_list32	li32;
26563 #endif
26564 	sd_prin_readkeys_t	*in;
26565 	mhioc_inkeys_t		*ptr;
26566 	mhioc_key_list_t	li;
26567 	uchar_t			*data_bufp;
26568 	int 			data_len;
26569 	int			rval = 0;
26570 	size_t			copysz;
26571 	sd_ssc_t		*ssc;
26572 
26573 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26574 		return (EINVAL);
26575 	}
26576 	bzero(&li, sizeof (mhioc_key_list_t));
26577 
26578 	ssc = sd_ssc_init(un);
26579 
26580 	/*
26581 	 * Get the listsize from user
26582 	 */
26583 #ifdef _MULTI_DATAMODEL
26584 
26585 	switch (ddi_model_convert_from(flag & FMODELS)) {
26586 	case DDI_MODEL_ILP32:
26587 		copysz = sizeof (struct mhioc_key_list32);
26588 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26589 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26590 			    "sd_persistent_reservation_in_read_keys: "
26591 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26592 			rval = EFAULT;
26593 			goto done;
26594 		}
26595 		li.listsize = li32.listsize;
26596 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26597 		break;
26598 
26599 	case DDI_MODEL_NONE:
26600 		copysz = sizeof (mhioc_key_list_t);
26601 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26602 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26603 			    "sd_persistent_reservation_in_read_keys: "
26604 			    "failed ddi_copyin: mhioc_key_list_t\n");
26605 			rval = EFAULT;
26606 			goto done;
26607 		}
26608 		break;
26609 	}
26610 
26611 #else /* ! _MULTI_DATAMODEL */
26612 	copysz = sizeof (mhioc_key_list_t);
26613 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26614 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26615 		    "sd_persistent_reservation_in_read_keys: "
26616 		    "failed ddi_copyin: mhioc_key_list_t\n");
26617 		rval = EFAULT;
26618 		goto done;
26619 	}
26620 #endif
26621 
26622 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26623 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26624 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26625 
26626 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_KEYS,
26627 	    data_len, data_bufp);
26628 	if (rval != 0) {
26629 		if (rval == EIO)
26630 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26631 		else
26632 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26633 		goto done;
26634 	}
26635 	in = (sd_prin_readkeys_t *)data_bufp;
26636 	ptr->generation = BE_32(in->generation);
26637 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26638 
26639 	/*
26640 	 * Return the min(listsize, listlen) keys
26641 	 */
26642 #ifdef _MULTI_DATAMODEL
26643 
26644 	switch (ddi_model_convert_from(flag & FMODELS)) {
26645 	case DDI_MODEL_ILP32:
26646 		li32.listlen = li.listlen;
26647 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26648 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26649 			    "sd_persistent_reservation_in_read_keys: "
26650 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26651 			rval = EFAULT;
26652 			goto done;
26653 		}
26654 		break;
26655 
26656 	case DDI_MODEL_NONE:
26657 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26658 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26659 			    "sd_persistent_reservation_in_read_keys: "
26660 			    "failed ddi_copyout: mhioc_key_list_t\n");
26661 			rval = EFAULT;
26662 			goto done;
26663 		}
26664 		break;
26665 	}
26666 
26667 #else /* ! _MULTI_DATAMODEL */
26668 
26669 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26670 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26671 		    "sd_persistent_reservation_in_read_keys: "
26672 		    "failed ddi_copyout: mhioc_key_list_t\n");
26673 		rval = EFAULT;
26674 		goto done;
26675 	}
26676 
26677 #endif /* _MULTI_DATAMODEL */
26678 
26679 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26680 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26681 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26682 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26683 		    "sd_persistent_reservation_in_read_keys: "
26684 		    "failed ddi_copyout: keylist\n");
26685 		rval = EFAULT;
26686 	}
26687 done:
26688 	sd_ssc_fini(ssc);
26689 	kmem_free(data_bufp, data_len);
26690 	return (rval);
26691 }
26692 
26693 
26694 /*
26695  *    Function: sd_persistent_reservation_in_read_resv
26696  *
26697  * Description: This routine is the driver entry point for handling CD-ROM
26698  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26699  *		by sending the SCSI-3 PRIN commands to the device.
26700  *		Process the read persistent reservations command response by
26701  *		copying the reservation information into the user provided
26702  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26703  *
26704  *   Arguments: un   -  Pointer to soft state struct for the target.
26705  *		usrp -	user provided pointer to multihost Persistent In Read
26706  *			Keys structure (mhioc_inkeys_t)
26707  *		flag -	this argument is a pass through to ddi_copyxxx()
26708  *			directly from the mode argument of ioctl().
26709  *
26710  * Return Code: 0   - Success
26711  *		EACCES
26712  *		ENOTSUP
26713  *		errno return code from sd_send_scsi_cmd()
26714  *
26715  *     Context: Can sleep. Does not return until command is completed.
26716  */
26717 
26718 static int
26719 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26720     mhioc_inresvs_t *usrp, int flag)
26721 {
26722 #ifdef _MULTI_DATAMODEL
26723 	struct mhioc_resv_desc_list32 resvlist32;
26724 #endif
26725 	sd_prin_readresv_t	*in;
26726 	mhioc_inresvs_t		*ptr;
26727 	sd_readresv_desc_t	*readresv_ptr;
26728 	mhioc_resv_desc_list_t	resvlist;
26729 	mhioc_resv_desc_t 	resvdesc;
26730 	uchar_t			*data_bufp = NULL;
26731 	int 			data_len;
26732 	int			rval = 0;
26733 	int			i;
26734 	size_t			copysz;
26735 	mhioc_resv_desc_t	*bufp;
26736 	sd_ssc_t		*ssc;
26737 
26738 	if ((ptr = usrp) == NULL) {
26739 		return (EINVAL);
26740 	}
26741 
26742 	ssc = sd_ssc_init(un);
26743 
26744 	/*
26745 	 * Get the listsize from user
26746 	 */
26747 #ifdef _MULTI_DATAMODEL
26748 	switch (ddi_model_convert_from(flag & FMODELS)) {
26749 	case DDI_MODEL_ILP32:
26750 		copysz = sizeof (struct mhioc_resv_desc_list32);
26751 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26752 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26753 			    "sd_persistent_reservation_in_read_resv: "
26754 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26755 			rval = EFAULT;
26756 			goto done;
26757 		}
26758 		resvlist.listsize = resvlist32.listsize;
26759 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26760 		break;
26761 
26762 	case DDI_MODEL_NONE:
26763 		copysz = sizeof (mhioc_resv_desc_list_t);
26764 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26765 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26766 			    "sd_persistent_reservation_in_read_resv: "
26767 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26768 			rval = EFAULT;
26769 			goto done;
26770 		}
26771 		break;
26772 	}
26773 #else /* ! _MULTI_DATAMODEL */
26774 	copysz = sizeof (mhioc_resv_desc_list_t);
26775 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26776 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26777 		    "sd_persistent_reservation_in_read_resv: "
26778 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26779 		rval = EFAULT;
26780 		goto done;
26781 	}
26782 #endif /* ! _MULTI_DATAMODEL */
26783 
26784 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26785 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26786 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26787 
26788 	rval = sd_send_scsi_PERSISTENT_RESERVE_IN(ssc, SD_READ_RESV,
26789 	    data_len, data_bufp);
26790 	if (rval != 0) {
26791 		if (rval == EIO)
26792 			sd_ssc_assessment(ssc, SD_FMT_IGNORE_COMPROMISE);
26793 		else
26794 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
26795 		goto done;
26796 	}
26797 	in = (sd_prin_readresv_t *)data_bufp;
26798 	ptr->generation = BE_32(in->generation);
26799 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26800 
26801 	/*
26802 	 * Return the min(listsize, listlen( keys
26803 	 */
26804 #ifdef _MULTI_DATAMODEL
26805 
26806 	switch (ddi_model_convert_from(flag & FMODELS)) {
26807 	case DDI_MODEL_ILP32:
26808 		resvlist32.listlen = resvlist.listlen;
26809 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26810 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26811 			    "sd_persistent_reservation_in_read_resv: "
26812 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26813 			rval = EFAULT;
26814 			goto done;
26815 		}
26816 		break;
26817 
26818 	case DDI_MODEL_NONE:
26819 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26820 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26821 			    "sd_persistent_reservation_in_read_resv: "
26822 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26823 			rval = EFAULT;
26824 			goto done;
26825 		}
26826 		break;
26827 	}
26828 
26829 #else /* ! _MULTI_DATAMODEL */
26830 
26831 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26832 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26833 		    "sd_persistent_reservation_in_read_resv: "
26834 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26835 		rval = EFAULT;
26836 		goto done;
26837 	}
26838 
26839 #endif /* ! _MULTI_DATAMODEL */
26840 
26841 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26842 	bufp = resvlist.list;
26843 	copysz = sizeof (mhioc_resv_desc_t);
26844 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26845 	    i++, readresv_ptr++, bufp++) {
26846 
26847 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26848 		    MHIOC_RESV_KEY_SIZE);
26849 		resvdesc.type  = readresv_ptr->type;
26850 		resvdesc.scope = readresv_ptr->scope;
26851 		resvdesc.scope_specific_addr =
26852 		    BE_32(readresv_ptr->scope_specific_addr);
26853 
26854 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26855 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26856 			    "sd_persistent_reservation_in_read_resv: "
26857 			    "failed ddi_copyout: resvlist\n");
26858 			rval = EFAULT;
26859 			goto done;
26860 		}
26861 	}
26862 done:
26863 	sd_ssc_fini(ssc);
26864 	/* only if data_bufp is allocated, we need to free it */
26865 	if (data_bufp) {
26866 		kmem_free(data_bufp, data_len);
26867 	}
26868 	return (rval);
26869 }
26870 
26871 
26872 /*
26873  *    Function: sr_change_blkmode()
26874  *
26875  * Description: This routine is the driver entry point for handling CD-ROM
26876  *		block mode ioctl requests. Support for returning and changing
26877  *		the current block size in use by the device is implemented. The
26878  *		LBA size is changed via a MODE SELECT Block Descriptor.
26879  *
26880  *		This routine issues a mode sense with an allocation length of
26881  *		12 bytes for the mode page header and a single block descriptor.
26882  *
26883  *   Arguments: dev - the device 'dev_t'
26884  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26885  *		      CDROMSBLKMODE (set)
26886  *		data - current block size or requested block size
26887  *		flag - this argument is a pass through to ddi_copyxxx() directly
26888  *		       from the mode argument of ioctl().
26889  *
26890  * Return Code: the code returned by sd_send_scsi_cmd()
26891  *		EINVAL if invalid arguments are provided
26892  *		EFAULT if ddi_copyxxx() fails
26893  *		ENXIO if fail ddi_get_soft_state
26894  *		EIO if invalid mode sense block descriptor length
26895  *
26896  */
26897 
26898 static int
26899 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26900 {
26901 	struct sd_lun			*un = NULL;
26902 	struct mode_header		*sense_mhp, *select_mhp;
26903 	struct block_descriptor		*sense_desc, *select_desc;
26904 	int				current_bsize;
26905 	int				rval = EINVAL;
26906 	uchar_t				*sense = NULL;
26907 	uchar_t				*select = NULL;
26908 	sd_ssc_t			*ssc;
26909 
26910 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26911 
26912 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26913 		return (ENXIO);
26914 	}
26915 
26916 	/*
26917 	 * The block length is changed via the Mode Select block descriptor, the
26918 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26919 	 * required as part of this routine. Therefore the mode sense allocation
26920 	 * length is specified to be the length of a mode page header and a
26921 	 * block descriptor.
26922 	 */
26923 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26924 
26925 	ssc = sd_ssc_init(un);
26926 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
26927 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
26928 	sd_ssc_fini(ssc);
26929 	if (rval != 0) {
26930 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26931 		    "sr_change_blkmode: Mode Sense Failed\n");
26932 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26933 		return (rval);
26934 	}
26935 
26936 	/* Check the block descriptor len to handle only 1 block descriptor */
26937 	sense_mhp = (struct mode_header *)sense;
26938 	if ((sense_mhp->bdesc_length == 0) ||
26939 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26940 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26941 		    "sr_change_blkmode: Mode Sense returned invalid block"
26942 		    " descriptor length\n");
26943 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26944 		return (EIO);
26945 	}
26946 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26947 	current_bsize = ((sense_desc->blksize_hi << 16) |
26948 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26949 
26950 	/* Process command */
26951 	switch (cmd) {
26952 	case CDROMGBLKMODE:
26953 		/* Return the block size obtained during the mode sense */
26954 		if (ddi_copyout(&current_bsize, (void *)data,
26955 		    sizeof (int), flag) != 0)
26956 			rval = EFAULT;
26957 		break;
26958 	case CDROMSBLKMODE:
26959 		/* Validate the requested block size */
26960 		switch (data) {
26961 		case CDROM_BLK_512:
26962 		case CDROM_BLK_1024:
26963 		case CDROM_BLK_2048:
26964 		case CDROM_BLK_2056:
26965 		case CDROM_BLK_2336:
26966 		case CDROM_BLK_2340:
26967 		case CDROM_BLK_2352:
26968 		case CDROM_BLK_2368:
26969 		case CDROM_BLK_2448:
26970 		case CDROM_BLK_2646:
26971 		case CDROM_BLK_2647:
26972 			break;
26973 		default:
26974 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26975 			    "sr_change_blkmode: "
26976 			    "Block Size '%ld' Not Supported\n", data);
26977 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26978 			return (EINVAL);
26979 		}
26980 
26981 		/*
26982 		 * The current block size matches the requested block size so
26983 		 * there is no need to send the mode select to change the size
26984 		 */
26985 		if (current_bsize == data) {
26986 			break;
26987 		}
26988 
26989 		/* Build the select data for the requested block size */
26990 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26991 		select_mhp = (struct mode_header *)select;
26992 		select_desc =
26993 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26994 		/*
26995 		 * The LBA size is changed via the block descriptor, so the
26996 		 * descriptor is built according to the user data
26997 		 */
26998 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26999 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
27000 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
27001 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27002 
27003 		/* Send the mode select for the requested block size */
27004 		ssc = sd_ssc_init(un);
27005 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27006 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27007 		    SD_PATH_STANDARD);
27008 		sd_ssc_fini(ssc);
27009 		if (rval != 0) {
27010 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27011 			    "sr_change_blkmode: Mode Select Failed\n");
27012 			/*
27013 			 * The mode select failed for the requested block size,
27014 			 * so reset the data for the original block size and
27015 			 * send it to the target. The error is indicated by the
27016 			 * return value for the failed mode select.
27017 			 */
27018 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27019 			select_desc->blksize_mid = sense_desc->blksize_mid;
27020 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27021 			ssc = sd_ssc_init(un);
27022 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0,
27023 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27024 			    SD_PATH_STANDARD);
27025 			sd_ssc_fini(ssc);
27026 		} else {
27027 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27028 			mutex_enter(SD_MUTEX(un));
27029 			sd_update_block_info(un, (uint32_t)data, 0);
27030 			mutex_exit(SD_MUTEX(un));
27031 		}
27032 		break;
27033 	default:
27034 		/* should not reach here, but check anyway */
27035 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27036 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27037 		rval = EINVAL;
27038 		break;
27039 	}
27040 
27041 	if (select) {
27042 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27043 	}
27044 	if (sense) {
27045 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27046 	}
27047 	return (rval);
27048 }
27049 
27050 
27051 /*
27052  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27053  * implement driver support for getting and setting the CD speed. The command
27054  * set used will be based on the device type. If the device has not been
27055  * identified as MMC the Toshiba vendor specific mode page will be used. If
27056  * the device is MMC but does not support the Real Time Streaming feature
27057  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27058  * be used to read the speed.
27059  */
27060 
27061 /*
27062  *    Function: sr_change_speed()
27063  *
27064  * Description: This routine is the driver entry point for handling CD-ROM
27065  *		drive speed ioctl requests for devices supporting the Toshiba
27066  *		vendor specific drive speed mode page. Support for returning
27067  *		and changing the current drive speed in use by the device is
27068  *		implemented.
27069  *
27070  *   Arguments: dev - the device 'dev_t'
27071  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27072  *		      CDROMSDRVSPEED (set)
27073  *		data - current drive speed or requested drive speed
27074  *		flag - this argument is a pass through to ddi_copyxxx() directly
27075  *		       from the mode argument of ioctl().
27076  *
27077  * Return Code: the code returned by sd_send_scsi_cmd()
27078  *		EINVAL if invalid arguments are provided
27079  *		EFAULT if ddi_copyxxx() fails
27080  *		ENXIO if fail ddi_get_soft_state
27081  *		EIO if invalid mode sense block descriptor length
27082  */
27083 
27084 static int
27085 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27086 {
27087 	struct sd_lun			*un = NULL;
27088 	struct mode_header		*sense_mhp, *select_mhp;
27089 	struct mode_speed		*sense_page, *select_page;
27090 	int				current_speed;
27091 	int				rval = EINVAL;
27092 	int				bd_len;
27093 	uchar_t				*sense = NULL;
27094 	uchar_t				*select = NULL;
27095 	sd_ssc_t			*ssc;
27096 
27097 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27098 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27099 		return (ENXIO);
27100 	}
27101 
27102 	/*
27103 	 * Note: The drive speed is being modified here according to a Toshiba
27104 	 * vendor specific mode page (0x31).
27105 	 */
27106 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27107 
27108 	ssc = sd_ssc_init(un);
27109 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
27110 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27111 	    SD_PATH_STANDARD);
27112 	sd_ssc_fini(ssc);
27113 	if (rval != 0) {
27114 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27115 		    "sr_change_speed: Mode Sense Failed\n");
27116 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27117 		return (rval);
27118 	}
27119 	sense_mhp  = (struct mode_header *)sense;
27120 
27121 	/* Check the block descriptor len to handle only 1 block descriptor */
27122 	bd_len = sense_mhp->bdesc_length;
27123 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27124 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27125 		    "sr_change_speed: Mode Sense returned invalid block "
27126 		    "descriptor length\n");
27127 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27128 		return (EIO);
27129 	}
27130 
27131 	sense_page = (struct mode_speed *)
27132 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27133 	current_speed = sense_page->speed;
27134 
27135 	/* Process command */
27136 	switch (cmd) {
27137 	case CDROMGDRVSPEED:
27138 		/* Return the drive speed obtained during the mode sense */
27139 		if (current_speed == 0x2) {
27140 			current_speed = CDROM_TWELVE_SPEED;
27141 		}
27142 		if (ddi_copyout(&current_speed, (void *)data,
27143 		    sizeof (int), flag) != 0) {
27144 			rval = EFAULT;
27145 		}
27146 		break;
27147 	case CDROMSDRVSPEED:
27148 		/* Validate the requested drive speed */
27149 		switch ((uchar_t)data) {
27150 		case CDROM_TWELVE_SPEED:
27151 			data = 0x2;
27152 			/*FALLTHROUGH*/
27153 		case CDROM_NORMAL_SPEED:
27154 		case CDROM_DOUBLE_SPEED:
27155 		case CDROM_QUAD_SPEED:
27156 		case CDROM_MAXIMUM_SPEED:
27157 			break;
27158 		default:
27159 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27160 			    "sr_change_speed: "
27161 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27162 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27163 			return (EINVAL);
27164 		}
27165 
27166 		/*
27167 		 * The current drive speed matches the requested drive speed so
27168 		 * there is no need to send the mode select to change the speed
27169 		 */
27170 		if (current_speed == data) {
27171 			break;
27172 		}
27173 
27174 		/* Build the select data for the requested drive speed */
27175 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27176 		select_mhp = (struct mode_header *)select;
27177 		select_mhp->bdesc_length = 0;
27178 		select_page =
27179 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27180 		select_page =
27181 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27182 		select_page->mode_page.code = CDROM_MODE_SPEED;
27183 		select_page->mode_page.length = 2;
27184 		select_page->speed = (uchar_t)data;
27185 
27186 		/* Send the mode select for the requested block size */
27187 		ssc = sd_ssc_init(un);
27188 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27189 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27190 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27191 		sd_ssc_fini(ssc);
27192 		if (rval != 0) {
27193 			/*
27194 			 * The mode select failed for the requested drive speed,
27195 			 * so reset the data for the original drive speed and
27196 			 * send it to the target. The error is indicated by the
27197 			 * return value for the failed mode select.
27198 			 */
27199 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27200 			    "sr_drive_speed: Mode Select Failed\n");
27201 			select_page->speed = sense_page->speed;
27202 			ssc = sd_ssc_init(un);
27203 			(void) sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
27204 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27205 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27206 			sd_ssc_fini(ssc);
27207 		}
27208 		break;
27209 	default:
27210 		/* should not reach here, but check anyway */
27211 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27212 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27213 		rval = EINVAL;
27214 		break;
27215 	}
27216 
27217 	if (select) {
27218 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27219 	}
27220 	if (sense) {
27221 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27222 	}
27223 
27224 	return (rval);
27225 }
27226 
27227 
27228 /*
27229  *    Function: sr_atapi_change_speed()
27230  *
27231  * Description: This routine is the driver entry point for handling CD-ROM
27232  *		drive speed ioctl requests for MMC devices that do not support
27233  *		the Real Time Streaming feature (0x107).
27234  *
27235  *		Note: This routine will use the SET SPEED command which may not
27236  *		be supported by all devices.
27237  *
27238  *   Arguments: dev- the device 'dev_t'
27239  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27240  *		     CDROMSDRVSPEED (set)
27241  *		data- current drive speed or requested drive speed
27242  *		flag- this argument is a pass through to ddi_copyxxx() directly
27243  *		      from the mode argument of ioctl().
27244  *
27245  * Return Code: the code returned by sd_send_scsi_cmd()
27246  *		EINVAL if invalid arguments are provided
27247  *		EFAULT if ddi_copyxxx() fails
27248  *		ENXIO if fail ddi_get_soft_state
27249  *		EIO if invalid mode sense block descriptor length
27250  */
27251 
27252 static int
27253 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27254 {
27255 	struct sd_lun			*un;
27256 	struct uscsi_cmd		*com = NULL;
27257 	struct mode_header_grp2		*sense_mhp;
27258 	uchar_t				*sense_page;
27259 	uchar_t				*sense = NULL;
27260 	char				cdb[CDB_GROUP5];
27261 	int				bd_len;
27262 	int				current_speed = 0;
27263 	int				max_speed = 0;
27264 	int				rval;
27265 	sd_ssc_t			*ssc;
27266 
27267 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27268 
27269 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27270 		return (ENXIO);
27271 	}
27272 
27273 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27274 
27275 	ssc = sd_ssc_init(un);
27276 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
27277 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27278 	    SD_PATH_STANDARD);
27279 	sd_ssc_fini(ssc);
27280 	if (rval != 0) {
27281 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27282 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27283 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27284 		return (rval);
27285 	}
27286 
27287 	/* Check the block descriptor len to handle only 1 block descriptor */
27288 	sense_mhp = (struct mode_header_grp2 *)sense;
27289 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27290 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27291 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27292 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27293 		    "block descriptor length\n");
27294 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27295 		return (EIO);
27296 	}
27297 
27298 	/* Calculate the current and maximum drive speeds */
27299 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27300 	current_speed = (sense_page[14] << 8) | sense_page[15];
27301 	max_speed = (sense_page[8] << 8) | sense_page[9];
27302 
27303 	/* Process the command */
27304 	switch (cmd) {
27305 	case CDROMGDRVSPEED:
27306 		current_speed /= SD_SPEED_1X;
27307 		if (ddi_copyout(&current_speed, (void *)data,
27308 		    sizeof (int), flag) != 0)
27309 			rval = EFAULT;
27310 		break;
27311 	case CDROMSDRVSPEED:
27312 		/* Convert the speed code to KB/sec */
27313 		switch ((uchar_t)data) {
27314 		case CDROM_NORMAL_SPEED:
27315 			current_speed = SD_SPEED_1X;
27316 			break;
27317 		case CDROM_DOUBLE_SPEED:
27318 			current_speed = 2 * SD_SPEED_1X;
27319 			break;
27320 		case CDROM_QUAD_SPEED:
27321 			current_speed = 4 * SD_SPEED_1X;
27322 			break;
27323 		case CDROM_TWELVE_SPEED:
27324 			current_speed = 12 * SD_SPEED_1X;
27325 			break;
27326 		case CDROM_MAXIMUM_SPEED:
27327 			current_speed = 0xffff;
27328 			break;
27329 		default:
27330 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27331 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27332 			    (uchar_t)data);
27333 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27334 			return (EINVAL);
27335 		}
27336 
27337 		/* Check the request against the drive's max speed. */
27338 		if (current_speed != 0xffff) {
27339 			if (current_speed > max_speed) {
27340 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27341 				return (EINVAL);
27342 			}
27343 		}
27344 
27345 		/*
27346 		 * Build and send the SET SPEED command
27347 		 *
27348 		 * Note: The SET SPEED (0xBB) command used in this routine is
27349 		 * obsolete per the SCSI MMC spec but still supported in the
27350 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27351 		 * therefore the command is still implemented in this routine.
27352 		 */
27353 		bzero(cdb, sizeof (cdb));
27354 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27355 		cdb[2] = (uchar_t)(current_speed >> 8);
27356 		cdb[3] = (uchar_t)current_speed;
27357 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27358 		com->uscsi_cdb	   = (caddr_t)cdb;
27359 		com->uscsi_cdblen  = CDB_GROUP5;
27360 		com->uscsi_bufaddr = NULL;
27361 		com->uscsi_buflen  = 0;
27362 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27363 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, 0, SD_PATH_STANDARD);
27364 		break;
27365 	default:
27366 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27367 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27368 		rval = EINVAL;
27369 	}
27370 
27371 	if (sense) {
27372 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27373 	}
27374 	if (com) {
27375 		kmem_free(com, sizeof (*com));
27376 	}
27377 	return (rval);
27378 }
27379 
27380 
27381 /*
27382  *    Function: sr_pause_resume()
27383  *
27384  * Description: This routine is the driver entry point for handling CD-ROM
27385  *		pause/resume ioctl requests. This only affects the audio play
27386  *		operation.
27387  *
27388  *   Arguments: dev - the device 'dev_t'
27389  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27390  *		      for setting the resume bit of the cdb.
27391  *
27392  * Return Code: the code returned by sd_send_scsi_cmd()
27393  *		EINVAL if invalid mode specified
27394  *
27395  */
27396 
27397 static int
27398 sr_pause_resume(dev_t dev, int cmd)
27399 {
27400 	struct sd_lun		*un;
27401 	struct uscsi_cmd	*com;
27402 	char			cdb[CDB_GROUP1];
27403 	int			rval;
27404 
27405 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27406 		return (ENXIO);
27407 	}
27408 
27409 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27410 	bzero(cdb, CDB_GROUP1);
27411 	cdb[0] = SCMD_PAUSE_RESUME;
27412 	switch (cmd) {
27413 	case CDROMRESUME:
27414 		cdb[8] = 1;
27415 		break;
27416 	case CDROMPAUSE:
27417 		cdb[8] = 0;
27418 		break;
27419 	default:
27420 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27421 		    " Command '%x' Not Supported\n", cmd);
27422 		rval = EINVAL;
27423 		goto done;
27424 	}
27425 
27426 	com->uscsi_cdb    = cdb;
27427 	com->uscsi_cdblen = CDB_GROUP1;
27428 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27429 
27430 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27431 	    SD_PATH_STANDARD);
27432 
27433 done:
27434 	kmem_free(com, sizeof (*com));
27435 	return (rval);
27436 }
27437 
27438 
27439 /*
27440  *    Function: sr_play_msf()
27441  *
27442  * Description: This routine is the driver entry point for handling CD-ROM
27443  *		ioctl requests to output the audio signals at the specified
27444  *		starting address and continue the audio play until the specified
27445  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27446  *		Frame (MSF) format.
27447  *
27448  *   Arguments: dev	- the device 'dev_t'
27449  *		data	- pointer to user provided audio msf structure,
27450  *		          specifying start/end addresses.
27451  *		flag	- this argument is a pass through to ddi_copyxxx()
27452  *		          directly from the mode argument of ioctl().
27453  *
27454  * Return Code: the code returned by sd_send_scsi_cmd()
27455  *		EFAULT if ddi_copyxxx() fails
27456  *		ENXIO if fail ddi_get_soft_state
27457  *		EINVAL if data pointer is NULL
27458  */
27459 
27460 static int
27461 sr_play_msf(dev_t dev, caddr_t data, int flag)
27462 {
27463 	struct sd_lun		*un;
27464 	struct uscsi_cmd	*com;
27465 	struct cdrom_msf	msf_struct;
27466 	struct cdrom_msf	*msf = &msf_struct;
27467 	char			cdb[CDB_GROUP1];
27468 	int			rval;
27469 
27470 	if (data == NULL) {
27471 		return (EINVAL);
27472 	}
27473 
27474 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27475 		return (ENXIO);
27476 	}
27477 
27478 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27479 		return (EFAULT);
27480 	}
27481 
27482 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27483 	bzero(cdb, CDB_GROUP1);
27484 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27485 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27486 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27487 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27488 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27489 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27490 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27491 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27492 	} else {
27493 		cdb[3] = msf->cdmsf_min0;
27494 		cdb[4] = msf->cdmsf_sec0;
27495 		cdb[5] = msf->cdmsf_frame0;
27496 		cdb[6] = msf->cdmsf_min1;
27497 		cdb[7] = msf->cdmsf_sec1;
27498 		cdb[8] = msf->cdmsf_frame1;
27499 	}
27500 	com->uscsi_cdb    = cdb;
27501 	com->uscsi_cdblen = CDB_GROUP1;
27502 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27503 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27504 	    SD_PATH_STANDARD);
27505 	kmem_free(com, sizeof (*com));
27506 	return (rval);
27507 }
27508 
27509 
27510 /*
27511  *    Function: sr_play_trkind()
27512  *
27513  * Description: This routine is the driver entry point for handling CD-ROM
27514  *		ioctl requests to output the audio signals at the specified
27515  *		starting address and continue the audio play until the specified
27516  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27517  *		format.
27518  *
27519  *   Arguments: dev	- the device 'dev_t'
27520  *		data	- pointer to user provided audio track/index structure,
27521  *		          specifying start/end addresses.
27522  *		flag	- this argument is a pass through to ddi_copyxxx()
27523  *		          directly from the mode argument of ioctl().
27524  *
27525  * Return Code: the code returned by sd_send_scsi_cmd()
27526  *		EFAULT if ddi_copyxxx() fails
27527  *		ENXIO if fail ddi_get_soft_state
27528  *		EINVAL if data pointer is NULL
27529  */
27530 
27531 static int
27532 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27533 {
27534 	struct cdrom_ti		ti_struct;
27535 	struct cdrom_ti		*ti = &ti_struct;
27536 	struct uscsi_cmd	*com = NULL;
27537 	char			cdb[CDB_GROUP1];
27538 	int			rval;
27539 
27540 	if (data == NULL) {
27541 		return (EINVAL);
27542 	}
27543 
27544 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27545 		return (EFAULT);
27546 	}
27547 
27548 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27549 	bzero(cdb, CDB_GROUP1);
27550 	cdb[0] = SCMD_PLAYAUDIO_TI;
27551 	cdb[4] = ti->cdti_trk0;
27552 	cdb[5] = ti->cdti_ind0;
27553 	cdb[7] = ti->cdti_trk1;
27554 	cdb[8] = ti->cdti_ind1;
27555 	com->uscsi_cdb    = cdb;
27556 	com->uscsi_cdblen = CDB_GROUP1;
27557 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27558 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27559 	    SD_PATH_STANDARD);
27560 	kmem_free(com, sizeof (*com));
27561 	return (rval);
27562 }
27563 
27564 
27565 /*
27566  *    Function: sr_read_all_subcodes()
27567  *
27568  * Description: This routine is the driver entry point for handling CD-ROM
27569  *		ioctl requests to return raw subcode data while the target is
27570  *		playing audio (CDROMSUBCODE).
27571  *
27572  *   Arguments: dev	- the device 'dev_t'
27573  *		data	- pointer to user provided cdrom subcode structure,
27574  *		          specifying the transfer length and address.
27575  *		flag	- this argument is a pass through to ddi_copyxxx()
27576  *		          directly from the mode argument of ioctl().
27577  *
27578  * Return Code: the code returned by sd_send_scsi_cmd()
27579  *		EFAULT if ddi_copyxxx() fails
27580  *		ENXIO if fail ddi_get_soft_state
27581  *		EINVAL if data pointer is NULL
27582  */
27583 
27584 static int
27585 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27586 {
27587 	struct sd_lun		*un = NULL;
27588 	struct uscsi_cmd	*com = NULL;
27589 	struct cdrom_subcode	*subcode = NULL;
27590 	int			rval;
27591 	size_t			buflen;
27592 	char			cdb[CDB_GROUP5];
27593 
27594 #ifdef _MULTI_DATAMODEL
27595 	/* To support ILP32 applications in an LP64 world */
27596 	struct cdrom_subcode32		cdrom_subcode32;
27597 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27598 #endif
27599 	if (data == NULL) {
27600 		return (EINVAL);
27601 	}
27602 
27603 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27604 		return (ENXIO);
27605 	}
27606 
27607 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27608 
27609 #ifdef _MULTI_DATAMODEL
27610 	switch (ddi_model_convert_from(flag & FMODELS)) {
27611 	case DDI_MODEL_ILP32:
27612 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27613 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27614 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27615 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27616 			return (EFAULT);
27617 		}
27618 		/* Convert the ILP32 uscsi data from the application to LP64 */
27619 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27620 		break;
27621 	case DDI_MODEL_NONE:
27622 		if (ddi_copyin(data, subcode,
27623 		    sizeof (struct cdrom_subcode), flag)) {
27624 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27625 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27626 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27627 			return (EFAULT);
27628 		}
27629 		break;
27630 	}
27631 #else /* ! _MULTI_DATAMODEL */
27632 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27633 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27634 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27635 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27636 		return (EFAULT);
27637 	}
27638 #endif /* _MULTI_DATAMODEL */
27639 
27640 	/*
27641 	 * Since MMC-2 expects max 3 bytes for length, check if the
27642 	 * length input is greater than 3 bytes
27643 	 */
27644 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27645 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27646 		    "sr_read_all_subcodes: "
27647 		    "cdrom transfer length too large: %d (limit %d)\n",
27648 		    subcode->cdsc_length, 0xFFFFFF);
27649 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27650 		return (EINVAL);
27651 	}
27652 
27653 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27654 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27655 	bzero(cdb, CDB_GROUP5);
27656 
27657 	if (un->un_f_mmc_cap == TRUE) {
27658 		cdb[0] = (char)SCMD_READ_CD;
27659 		cdb[2] = (char)0xff;
27660 		cdb[3] = (char)0xff;
27661 		cdb[4] = (char)0xff;
27662 		cdb[5] = (char)0xff;
27663 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27664 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27665 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27666 		cdb[10] = 1;
27667 	} else {
27668 		/*
27669 		 * Note: A vendor specific command (0xDF) is being used here to
27670 		 * request a read of all subcodes.
27671 		 */
27672 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27673 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27674 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27675 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27676 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27677 	}
27678 	com->uscsi_cdb	   = cdb;
27679 	com->uscsi_cdblen  = CDB_GROUP5;
27680 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27681 	com->uscsi_buflen  = buflen;
27682 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27683 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
27684 	    SD_PATH_STANDARD);
27685 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27686 	kmem_free(com, sizeof (*com));
27687 	return (rval);
27688 }
27689 
27690 
27691 /*
27692  *    Function: sr_read_subchannel()
27693  *
27694  * Description: This routine is the driver entry point for handling CD-ROM
27695  *		ioctl requests to return the Q sub-channel data of the CD
27696  *		current position block. (CDROMSUBCHNL) The data includes the
27697  *		track number, index number, absolute CD-ROM address (LBA or MSF
27698  *		format per the user) , track relative CD-ROM address (LBA or MSF
27699  *		format per the user), control data and audio status.
27700  *
27701  *   Arguments: dev	- the device 'dev_t'
27702  *		data	- pointer to user provided cdrom sub-channel structure
27703  *		flag	- this argument is a pass through to ddi_copyxxx()
27704  *		          directly from the mode argument of ioctl().
27705  *
27706  * Return Code: the code returned by sd_send_scsi_cmd()
27707  *		EFAULT if ddi_copyxxx() fails
27708  *		ENXIO if fail ddi_get_soft_state
27709  *		EINVAL if data pointer is NULL
27710  */
27711 
27712 static int
27713 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27714 {
27715 	struct sd_lun		*un;
27716 	struct uscsi_cmd	*com;
27717 	struct cdrom_subchnl	subchanel;
27718 	struct cdrom_subchnl	*subchnl = &subchanel;
27719 	char			cdb[CDB_GROUP1];
27720 	caddr_t			buffer;
27721 	int			rval;
27722 
27723 	if (data == NULL) {
27724 		return (EINVAL);
27725 	}
27726 
27727 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27728 	    (un->un_state == SD_STATE_OFFLINE)) {
27729 		return (ENXIO);
27730 	}
27731 
27732 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27733 		return (EFAULT);
27734 	}
27735 
27736 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27737 	bzero(cdb, CDB_GROUP1);
27738 	cdb[0] = SCMD_READ_SUBCHANNEL;
27739 	/* Set the MSF bit based on the user requested address format */
27740 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27741 	/*
27742 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27743 	 * returned
27744 	 */
27745 	cdb[2] = 0x40;
27746 	/*
27747 	 * Set byte 3 to specify the return data format. A value of 0x01
27748 	 * indicates that the CD-ROM current position should be returned.
27749 	 */
27750 	cdb[3] = 0x01;
27751 	cdb[8] = 0x10;
27752 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27753 	com->uscsi_cdb	   = cdb;
27754 	com->uscsi_cdblen  = CDB_GROUP1;
27755 	com->uscsi_bufaddr = buffer;
27756 	com->uscsi_buflen  = 16;
27757 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27758 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27759 	    SD_PATH_STANDARD);
27760 	if (rval != 0) {
27761 		kmem_free(buffer, 16);
27762 		kmem_free(com, sizeof (*com));
27763 		return (rval);
27764 	}
27765 
27766 	/* Process the returned Q sub-channel data */
27767 	subchnl->cdsc_audiostatus = buffer[1];
27768 	subchnl->cdsc_adr	= (buffer[5] & 0xF0) >> 4;
27769 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27770 	subchnl->cdsc_trk	= buffer[6];
27771 	subchnl->cdsc_ind	= buffer[7];
27772 	if (subchnl->cdsc_format & CDROM_LBA) {
27773 		subchnl->cdsc_absaddr.lba =
27774 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27775 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27776 		subchnl->cdsc_reladdr.lba =
27777 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27778 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27779 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27780 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27781 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27782 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27783 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27784 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27785 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27786 	} else {
27787 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27788 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27789 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27790 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27791 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27792 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27793 	}
27794 	kmem_free(buffer, 16);
27795 	kmem_free(com, sizeof (*com));
27796 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27797 	    != 0) {
27798 		return (EFAULT);
27799 	}
27800 	return (rval);
27801 }
27802 
27803 
27804 /*
27805  *    Function: sr_read_tocentry()
27806  *
27807  * Description: This routine is the driver entry point for handling CD-ROM
27808  *		ioctl requests to read from the Table of Contents (TOC)
27809  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27810  *		fields, the starting address (LBA or MSF format per the user)
27811  *		and the data mode if the user specified track is a data track.
27812  *
27813  *		Note: The READ HEADER (0x44) command used in this routine is
27814  *		obsolete per the SCSI MMC spec but still supported in the
27815  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27816  *		therefore the command is still implemented in this routine.
27817  *
27818  *   Arguments: dev	- the device 'dev_t'
27819  *		data	- pointer to user provided toc entry structure,
27820  *			  specifying the track # and the address format
27821  *			  (LBA or MSF).
27822  *		flag	- this argument is a pass through to ddi_copyxxx()
27823  *		          directly from the mode argument of ioctl().
27824  *
27825  * Return Code: the code returned by sd_send_scsi_cmd()
27826  *		EFAULT if ddi_copyxxx() fails
27827  *		ENXIO if fail ddi_get_soft_state
27828  *		EINVAL if data pointer is NULL
27829  */
27830 
27831 static int
27832 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27833 {
27834 	struct sd_lun		*un = NULL;
27835 	struct uscsi_cmd	*com;
27836 	struct cdrom_tocentry	toc_entry;
27837 	struct cdrom_tocentry	*entry = &toc_entry;
27838 	caddr_t			buffer;
27839 	int			rval;
27840 	char			cdb[CDB_GROUP1];
27841 
27842 	if (data == NULL) {
27843 		return (EINVAL);
27844 	}
27845 
27846 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27847 	    (un->un_state == SD_STATE_OFFLINE)) {
27848 		return (ENXIO);
27849 	}
27850 
27851 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27852 		return (EFAULT);
27853 	}
27854 
27855 	/* Validate the requested track and address format */
27856 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27857 		return (EINVAL);
27858 	}
27859 
27860 	if (entry->cdte_track == 0) {
27861 		return (EINVAL);
27862 	}
27863 
27864 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27865 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27866 	bzero(cdb, CDB_GROUP1);
27867 
27868 	cdb[0] = SCMD_READ_TOC;
27869 	/* Set the MSF bit based on the user requested address format  */
27870 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27871 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27872 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27873 	} else {
27874 		cdb[6] = entry->cdte_track;
27875 	}
27876 
27877 	/*
27878 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27879 	 * (4 byte TOC response header + 8 byte track descriptor)
27880 	 */
27881 	cdb[8] = 12;
27882 	com->uscsi_cdb	   = cdb;
27883 	com->uscsi_cdblen  = CDB_GROUP1;
27884 	com->uscsi_bufaddr = buffer;
27885 	com->uscsi_buflen  = 0x0C;
27886 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27887 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27888 	    SD_PATH_STANDARD);
27889 	if (rval != 0) {
27890 		kmem_free(buffer, 12);
27891 		kmem_free(com, sizeof (*com));
27892 		return (rval);
27893 	}
27894 
27895 	/* Process the toc entry */
27896 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27897 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27898 	if (entry->cdte_format & CDROM_LBA) {
27899 		entry->cdte_addr.lba =
27900 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27901 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27902 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27903 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27904 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27905 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27906 		/*
27907 		 * Send a READ TOC command using the LBA address format to get
27908 		 * the LBA for the track requested so it can be used in the
27909 		 * READ HEADER request
27910 		 *
27911 		 * Note: The MSF bit of the READ HEADER command specifies the
27912 		 * output format. The block address specified in that command
27913 		 * must be in LBA format.
27914 		 */
27915 		cdb[1] = 0;
27916 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27917 		    SD_PATH_STANDARD);
27918 		if (rval != 0) {
27919 			kmem_free(buffer, 12);
27920 			kmem_free(com, sizeof (*com));
27921 			return (rval);
27922 		}
27923 	} else {
27924 		entry->cdte_addr.msf.minute	= buffer[9];
27925 		entry->cdte_addr.msf.second	= buffer[10];
27926 		entry->cdte_addr.msf.frame	= buffer[11];
27927 		/*
27928 		 * Send a READ TOC command using the LBA address format to get
27929 		 * the LBA for the track requested so it can be used in the
27930 		 * READ HEADER request
27931 		 *
27932 		 * Note: The MSF bit of the READ HEADER command specifies the
27933 		 * output format. The block address specified in that command
27934 		 * must be in LBA format.
27935 		 */
27936 		cdb[1] = 0;
27937 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27938 		    SD_PATH_STANDARD);
27939 		if (rval != 0) {
27940 			kmem_free(buffer, 12);
27941 			kmem_free(com, sizeof (*com));
27942 			return (rval);
27943 		}
27944 	}
27945 
27946 	/*
27947 	 * Build and send the READ HEADER command to determine the data mode of
27948 	 * the user specified track.
27949 	 */
27950 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27951 	    (entry->cdte_track != CDROM_LEADOUT)) {
27952 		bzero(cdb, CDB_GROUP1);
27953 		cdb[0] = SCMD_READ_HEADER;
27954 		cdb[2] = buffer[8];
27955 		cdb[3] = buffer[9];
27956 		cdb[4] = buffer[10];
27957 		cdb[5] = buffer[11];
27958 		cdb[8] = 0x08;
27959 		com->uscsi_buflen = 0x08;
27960 		rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
27961 		    SD_PATH_STANDARD);
27962 		if (rval == 0) {
27963 			entry->cdte_datamode = buffer[0];
27964 		} else {
27965 			/*
27966 			 * READ HEADER command failed, since this is
27967 			 * obsoleted in one spec, its better to return
27968 			 * -1 for an invlid track so that we can still
27969 			 * receive the rest of the TOC data.
27970 			 */
27971 			entry->cdte_datamode = (uchar_t)-1;
27972 		}
27973 	} else {
27974 		entry->cdte_datamode = (uchar_t)-1;
27975 	}
27976 
27977 	kmem_free(buffer, 12);
27978 	kmem_free(com, sizeof (*com));
27979 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27980 		return (EFAULT);
27981 
27982 	return (rval);
27983 }
27984 
27985 
27986 /*
27987  *    Function: sr_read_tochdr()
27988  *
27989  * Description: This routine is the driver entry point for handling CD-ROM
27990  * 		ioctl requests to read the Table of Contents (TOC) header
27991  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
27992  *		and ending track numbers
27993  *
27994  *   Arguments: dev	- the device 'dev_t'
27995  *		data	- pointer to user provided toc header structure,
27996  *			  specifying the starting and ending track numbers.
27997  *		flag	- this argument is a pass through to ddi_copyxxx()
27998  *			  directly from the mode argument of ioctl().
27999  *
28000  * Return Code: the code returned by sd_send_scsi_cmd()
28001  *		EFAULT if ddi_copyxxx() fails
28002  *		ENXIO if fail ddi_get_soft_state
28003  *		EINVAL if data pointer is NULL
28004  */
28005 
28006 static int
28007 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28008 {
28009 	struct sd_lun		*un;
28010 	struct uscsi_cmd	*com;
28011 	struct cdrom_tochdr	toc_header;
28012 	struct cdrom_tochdr	*hdr = &toc_header;
28013 	char			cdb[CDB_GROUP1];
28014 	int			rval;
28015 	caddr_t			buffer;
28016 
28017 	if (data == NULL) {
28018 		return (EINVAL);
28019 	}
28020 
28021 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28022 	    (un->un_state == SD_STATE_OFFLINE)) {
28023 		return (ENXIO);
28024 	}
28025 
28026 	buffer = kmem_zalloc(4, KM_SLEEP);
28027 	bzero(cdb, CDB_GROUP1);
28028 	cdb[0] = SCMD_READ_TOC;
28029 	/*
28030 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28031 	 * that the TOC header should be returned
28032 	 */
28033 	cdb[6] = 0x00;
28034 	/*
28035 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28036 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28037 	 */
28038 	cdb[8] = 0x04;
28039 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28040 	com->uscsi_cdb	   = cdb;
28041 	com->uscsi_cdblen  = CDB_GROUP1;
28042 	com->uscsi_bufaddr = buffer;
28043 	com->uscsi_buflen  = 0x04;
28044 	com->uscsi_timeout = 300;
28045 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28046 
28047 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
28048 	    SD_PATH_STANDARD);
28049 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28050 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28051 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28052 	} else {
28053 		hdr->cdth_trk0 = buffer[2];
28054 		hdr->cdth_trk1 = buffer[3];
28055 	}
28056 	kmem_free(buffer, 4);
28057 	kmem_free(com, sizeof (*com));
28058 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28059 		return (EFAULT);
28060 	}
28061 	return (rval);
28062 }
28063 
28064 
28065 /*
28066  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28067  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28068  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28069  * digital audio and extended architecture digital audio. These modes are
28070  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28071  * MMC specs.
28072  *
28073  * In addition to support for the various data formats these routines also
28074  * include support for devices that implement only the direct access READ
28075  * commands (0x08, 0x28), devices that implement the READ_CD commands
28076  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28077  * READ CDXA commands (0xD8, 0xDB)
28078  */
28079 
28080 /*
28081  *    Function: sr_read_mode1()
28082  *
28083  * Description: This routine is the driver entry point for handling CD-ROM
28084  *		ioctl read mode1 requests (CDROMREADMODE1).
28085  *
28086  *   Arguments: dev	- the device 'dev_t'
28087  *		data	- pointer to user provided cd read structure specifying
28088  *			  the lba buffer address and length.
28089  *		flag	- this argument is a pass through to ddi_copyxxx()
28090  *			  directly from the mode argument of ioctl().
28091  *
28092  * Return Code: the code returned by sd_send_scsi_cmd()
28093  *		EFAULT if ddi_copyxxx() fails
28094  *		ENXIO if fail ddi_get_soft_state
28095  *		EINVAL if data pointer is NULL
28096  */
28097 
28098 static int
28099 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28100 {
28101 	struct sd_lun		*un;
28102 	struct cdrom_read	mode1_struct;
28103 	struct cdrom_read	*mode1 = &mode1_struct;
28104 	int			rval;
28105 	sd_ssc_t		*ssc;
28106 
28107 #ifdef _MULTI_DATAMODEL
28108 	/* To support ILP32 applications in an LP64 world */
28109 	struct cdrom_read32	cdrom_read32;
28110 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28111 #endif /* _MULTI_DATAMODEL */
28112 
28113 	if (data == NULL) {
28114 		return (EINVAL);
28115 	}
28116 
28117 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28118 	    (un->un_state == SD_STATE_OFFLINE)) {
28119 		return (ENXIO);
28120 	}
28121 
28122 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28123 	    "sd_read_mode1: entry: un:0x%p\n", un);
28124 
28125 #ifdef _MULTI_DATAMODEL
28126 	switch (ddi_model_convert_from(flag & FMODELS)) {
28127 	case DDI_MODEL_ILP32:
28128 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28129 			return (EFAULT);
28130 		}
28131 		/* Convert the ILP32 uscsi data from the application to LP64 */
28132 		cdrom_read32tocdrom_read(cdrd32, mode1);
28133 		break;
28134 	case DDI_MODEL_NONE:
28135 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28136 			return (EFAULT);
28137 		}
28138 	}
28139 #else /* ! _MULTI_DATAMODEL */
28140 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28141 		return (EFAULT);
28142 	}
28143 #endif /* _MULTI_DATAMODEL */
28144 
28145 	ssc = sd_ssc_init(un);
28146 	rval = sd_send_scsi_READ(ssc, mode1->cdread_bufaddr,
28147 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28148 	sd_ssc_fini(ssc);
28149 
28150 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28151 	    "sd_read_mode1: exit: un:0x%p\n", un);
28152 
28153 	return (rval);
28154 }
28155 
28156 
28157 /*
28158  *    Function: sr_read_cd_mode2()
28159  *
28160  * Description: This routine is the driver entry point for handling CD-ROM
28161  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28162  *		support the READ CD (0xBE) command or the 1st generation
28163  *		READ CD (0xD4) command.
28164  *
28165  *   Arguments: dev	- the device 'dev_t'
28166  *		data	- pointer to user provided cd read structure specifying
28167  *			  the lba buffer address and length.
28168  *		flag	- this argument is a pass through to ddi_copyxxx()
28169  *			  directly from the mode argument of ioctl().
28170  *
28171  * Return Code: the code returned by sd_send_scsi_cmd()
28172  *		EFAULT if ddi_copyxxx() fails
28173  *		ENXIO if fail ddi_get_soft_state
28174  *		EINVAL if data pointer is NULL
28175  */
28176 
28177 static int
28178 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28179 {
28180 	struct sd_lun		*un;
28181 	struct uscsi_cmd	*com;
28182 	struct cdrom_read	mode2_struct;
28183 	struct cdrom_read	*mode2 = &mode2_struct;
28184 	uchar_t			cdb[CDB_GROUP5];
28185 	int			nblocks;
28186 	int			rval;
28187 #ifdef _MULTI_DATAMODEL
28188 	/*  To support ILP32 applications in an LP64 world */
28189 	struct cdrom_read32	cdrom_read32;
28190 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28191 #endif /* _MULTI_DATAMODEL */
28192 
28193 	if (data == NULL) {
28194 		return (EINVAL);
28195 	}
28196 
28197 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28198 	    (un->un_state == SD_STATE_OFFLINE)) {
28199 		return (ENXIO);
28200 	}
28201 
28202 #ifdef _MULTI_DATAMODEL
28203 	switch (ddi_model_convert_from(flag & FMODELS)) {
28204 	case DDI_MODEL_ILP32:
28205 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28206 			return (EFAULT);
28207 		}
28208 		/* Convert the ILP32 uscsi data from the application to LP64 */
28209 		cdrom_read32tocdrom_read(cdrd32, mode2);
28210 		break;
28211 	case DDI_MODEL_NONE:
28212 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28213 			return (EFAULT);
28214 		}
28215 		break;
28216 	}
28217 
28218 #else /* ! _MULTI_DATAMODEL */
28219 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28220 		return (EFAULT);
28221 	}
28222 #endif /* _MULTI_DATAMODEL */
28223 
28224 	bzero(cdb, sizeof (cdb));
28225 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28226 		/* Read command supported by 1st generation atapi drives */
28227 		cdb[0] = SCMD_READ_CDD4;
28228 	} else {
28229 		/* Universal CD Access Command */
28230 		cdb[0] = SCMD_READ_CD;
28231 	}
28232 
28233 	/*
28234 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28235 	 */
28236 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28237 
28238 	/* set the start address */
28239 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28240 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28241 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28242 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28243 
28244 	/* set the transfer length */
28245 	nblocks = mode2->cdread_buflen / 2336;
28246 	cdb[6] = (uchar_t)(nblocks >> 16);
28247 	cdb[7] = (uchar_t)(nblocks >> 8);
28248 	cdb[8] = (uchar_t)nblocks;
28249 
28250 	/* set the filter bits */
28251 	cdb[9] = CDROM_READ_CD_USERDATA;
28252 
28253 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28254 	com->uscsi_cdb = (caddr_t)cdb;
28255 	com->uscsi_cdblen = sizeof (cdb);
28256 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28257 	com->uscsi_buflen = mode2->cdread_buflen;
28258 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28259 
28260 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28261 	    SD_PATH_STANDARD);
28262 	kmem_free(com, sizeof (*com));
28263 	return (rval);
28264 }
28265 
28266 
28267 /*
28268  *    Function: sr_read_mode2()
28269  *
28270  * Description: This routine is the driver entry point for handling CD-ROM
28271  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28272  *		do not support the READ CD (0xBE) command.
28273  *
28274  *   Arguments: dev	- the device 'dev_t'
28275  *		data	- pointer to user provided cd read structure specifying
28276  *			  the lba buffer address and length.
28277  *		flag	- this argument is a pass through to ddi_copyxxx()
28278  *			  directly from the mode argument of ioctl().
28279  *
28280  * Return Code: the code returned by sd_send_scsi_cmd()
28281  *		EFAULT if ddi_copyxxx() fails
28282  *		ENXIO if fail ddi_get_soft_state
28283  *		EINVAL if data pointer is NULL
28284  *		EIO if fail to reset block size
28285  *		EAGAIN if commands are in progress in the driver
28286  */
28287 
28288 static int
28289 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28290 {
28291 	struct sd_lun		*un;
28292 	struct cdrom_read	mode2_struct;
28293 	struct cdrom_read	*mode2 = &mode2_struct;
28294 	int			rval;
28295 	uint32_t		restore_blksize;
28296 	struct uscsi_cmd	*com;
28297 	uchar_t			cdb[CDB_GROUP0];
28298 	int			nblocks;
28299 
28300 #ifdef _MULTI_DATAMODEL
28301 	/* To support ILP32 applications in an LP64 world */
28302 	struct cdrom_read32	cdrom_read32;
28303 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28304 #endif /* _MULTI_DATAMODEL */
28305 
28306 	if (data == NULL) {
28307 		return (EINVAL);
28308 	}
28309 
28310 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28311 	    (un->un_state == SD_STATE_OFFLINE)) {
28312 		return (ENXIO);
28313 	}
28314 
28315 	/*
28316 	 * Because this routine will update the device and driver block size
28317 	 * being used we want to make sure there are no commands in progress.
28318 	 * If commands are in progress the user will have to try again.
28319 	 *
28320 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28321 	 * in sdioctl to protect commands from sdioctl through to the top of
28322 	 * sd_uscsi_strategy. See sdioctl for details.
28323 	 */
28324 	mutex_enter(SD_MUTEX(un));
28325 	if (un->un_ncmds_in_driver != 1) {
28326 		mutex_exit(SD_MUTEX(un));
28327 		return (EAGAIN);
28328 	}
28329 	mutex_exit(SD_MUTEX(un));
28330 
28331 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28332 	    "sd_read_mode2: entry: un:0x%p\n", un);
28333 
28334 #ifdef _MULTI_DATAMODEL
28335 	switch (ddi_model_convert_from(flag & FMODELS)) {
28336 	case DDI_MODEL_ILP32:
28337 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28338 			return (EFAULT);
28339 		}
28340 		/* Convert the ILP32 uscsi data from the application to LP64 */
28341 		cdrom_read32tocdrom_read(cdrd32, mode2);
28342 		break;
28343 	case DDI_MODEL_NONE:
28344 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28345 			return (EFAULT);
28346 		}
28347 		break;
28348 	}
28349 #else /* ! _MULTI_DATAMODEL */
28350 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28351 		return (EFAULT);
28352 	}
28353 #endif /* _MULTI_DATAMODEL */
28354 
28355 	/* Store the current target block size for restoration later */
28356 	restore_blksize = un->un_tgt_blocksize;
28357 
28358 	/* Change the device and soft state target block size to 2336 */
28359 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28360 		rval = EIO;
28361 		goto done;
28362 	}
28363 
28364 
28365 	bzero(cdb, sizeof (cdb));
28366 
28367 	/* set READ operation */
28368 	cdb[0] = SCMD_READ;
28369 
28370 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28371 	mode2->cdread_lba >>= 2;
28372 
28373 	/* set the start address */
28374 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28375 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28376 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28377 
28378 	/* set the transfer length */
28379 	nblocks = mode2->cdread_buflen / 2336;
28380 	cdb[4] = (uchar_t)nblocks & 0xFF;
28381 
28382 	/* build command */
28383 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28384 	com->uscsi_cdb = (caddr_t)cdb;
28385 	com->uscsi_cdblen = sizeof (cdb);
28386 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28387 	com->uscsi_buflen = mode2->cdread_buflen;
28388 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28389 
28390 	/*
28391 	 * Issue SCSI command with user space address for read buffer.
28392 	 *
28393 	 * This sends the command through main channel in the driver.
28394 	 *
28395 	 * Since this is accessed via an IOCTL call, we go through the
28396 	 * standard path, so that if the device was powered down, then
28397 	 * it would be 'awakened' to handle the command.
28398 	 */
28399 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28400 	    SD_PATH_STANDARD);
28401 
28402 	kmem_free(com, sizeof (*com));
28403 
28404 	/* Restore the device and soft state target block size */
28405 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28406 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28407 		    "can't do switch back to mode 1\n");
28408 		/*
28409 		 * If sd_send_scsi_READ succeeded we still need to report
28410 		 * an error because we failed to reset the block size
28411 		 */
28412 		if (rval == 0) {
28413 			rval = EIO;
28414 		}
28415 	}
28416 
28417 done:
28418 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28419 	    "sd_read_mode2: exit: un:0x%p\n", un);
28420 
28421 	return (rval);
28422 }
28423 
28424 
28425 /*
28426  *    Function: sr_sector_mode()
28427  *
28428  * Description: This utility function is used by sr_read_mode2 to set the target
28429  *		block size based on the user specified size. This is a legacy
28430  *		implementation based upon a vendor specific mode page
28431  *
28432  *   Arguments: dev	- the device 'dev_t'
28433  *		data	- flag indicating if block size is being set to 2336 or
28434  *			  512.
28435  *
28436  * Return Code: the code returned by sd_send_scsi_cmd()
28437  *		EFAULT if ddi_copyxxx() fails
28438  *		ENXIO if fail ddi_get_soft_state
28439  *		EINVAL if data pointer is NULL
28440  */
28441 
28442 static int
28443 sr_sector_mode(dev_t dev, uint32_t blksize)
28444 {
28445 	struct sd_lun	*un;
28446 	uchar_t		*sense;
28447 	uchar_t		*select;
28448 	int		rval;
28449 	sd_ssc_t	*ssc;
28450 
28451 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28452 	    (un->un_state == SD_STATE_OFFLINE)) {
28453 		return (ENXIO);
28454 	}
28455 
28456 	sense = kmem_zalloc(20, KM_SLEEP);
28457 
28458 	/* Note: This is a vendor specific mode page (0x81) */
28459 	ssc = sd_ssc_init(un);
28460 	rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, 20, 0x81,
28461 	    SD_PATH_STANDARD);
28462 	sd_ssc_fini(ssc);
28463 	if (rval != 0) {
28464 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28465 		    "sr_sector_mode: Mode Sense failed\n");
28466 		kmem_free(sense, 20);
28467 		return (rval);
28468 	}
28469 	select = kmem_zalloc(20, KM_SLEEP);
28470 	select[3] = 0x08;
28471 	select[10] = ((blksize >> 8) & 0xff);
28472 	select[11] = (blksize & 0xff);
28473 	select[12] = 0x01;
28474 	select[13] = 0x06;
28475 	select[14] = sense[14];
28476 	select[15] = sense[15];
28477 	if (blksize == SD_MODE2_BLKSIZE) {
28478 		select[14] |= 0x01;
28479 	}
28480 
28481 	ssc = sd_ssc_init(un);
28482 	rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select, 20,
28483 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28484 	sd_ssc_fini(ssc);
28485 	if (rval != 0) {
28486 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28487 		    "sr_sector_mode: Mode Select failed\n");
28488 	} else {
28489 		/*
28490 		 * Only update the softstate block size if we successfully
28491 		 * changed the device block mode.
28492 		 */
28493 		mutex_enter(SD_MUTEX(un));
28494 		sd_update_block_info(un, blksize, 0);
28495 		mutex_exit(SD_MUTEX(un));
28496 	}
28497 	kmem_free(sense, 20);
28498 	kmem_free(select, 20);
28499 	return (rval);
28500 }
28501 
28502 
28503 /*
28504  *    Function: sr_read_cdda()
28505  *
28506  * Description: This routine is the driver entry point for handling CD-ROM
28507  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28508  *		the target supports CDDA these requests are handled via a vendor
28509  *		specific command (0xD8) If the target does not support CDDA
28510  *		these requests are handled via the READ CD command (0xBE).
28511  *
28512  *   Arguments: dev	- the device 'dev_t'
28513  *		data	- pointer to user provided CD-DA structure specifying
28514  *			  the track starting address, transfer length, and
28515  *			  subcode options.
28516  *		flag	- this argument is a pass through to ddi_copyxxx()
28517  *			  directly from the mode argument of ioctl().
28518  *
28519  * Return Code: the code returned by sd_send_scsi_cmd()
28520  *		EFAULT if ddi_copyxxx() fails
28521  *		ENXIO if fail ddi_get_soft_state
28522  *		EINVAL if invalid arguments are provided
28523  *		ENOTTY
28524  */
28525 
28526 static int
28527 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28528 {
28529 	struct sd_lun			*un;
28530 	struct uscsi_cmd		*com;
28531 	struct cdrom_cdda		*cdda;
28532 	int				rval;
28533 	size_t				buflen;
28534 	char				cdb[CDB_GROUP5];
28535 
28536 #ifdef _MULTI_DATAMODEL
28537 	/* To support ILP32 applications in an LP64 world */
28538 	struct cdrom_cdda32	cdrom_cdda32;
28539 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28540 #endif /* _MULTI_DATAMODEL */
28541 
28542 	if (data == NULL) {
28543 		return (EINVAL);
28544 	}
28545 
28546 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28547 		return (ENXIO);
28548 	}
28549 
28550 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28551 
28552 #ifdef _MULTI_DATAMODEL
28553 	switch (ddi_model_convert_from(flag & FMODELS)) {
28554 	case DDI_MODEL_ILP32:
28555 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28556 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28557 			    "sr_read_cdda: ddi_copyin Failed\n");
28558 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28559 			return (EFAULT);
28560 		}
28561 		/* Convert the ILP32 uscsi data from the application to LP64 */
28562 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28563 		break;
28564 	case DDI_MODEL_NONE:
28565 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28566 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28567 			    "sr_read_cdda: ddi_copyin Failed\n");
28568 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28569 			return (EFAULT);
28570 		}
28571 		break;
28572 	}
28573 #else /* ! _MULTI_DATAMODEL */
28574 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28575 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28576 		    "sr_read_cdda: ddi_copyin Failed\n");
28577 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28578 		return (EFAULT);
28579 	}
28580 #endif /* _MULTI_DATAMODEL */
28581 
28582 	/*
28583 	 * Since MMC-2 expects max 3 bytes for length, check if the
28584 	 * length input is greater than 3 bytes
28585 	 */
28586 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28587 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28588 		    "cdrom transfer length too large: %d (limit %d)\n",
28589 		    cdda->cdda_length, 0xFFFFFF);
28590 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28591 		return (EINVAL);
28592 	}
28593 
28594 	switch (cdda->cdda_subcode) {
28595 	case CDROM_DA_NO_SUBCODE:
28596 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28597 		break;
28598 	case CDROM_DA_SUBQ:
28599 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28600 		break;
28601 	case CDROM_DA_ALL_SUBCODE:
28602 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28603 		break;
28604 	case CDROM_DA_SUBCODE_ONLY:
28605 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28606 		break;
28607 	default:
28608 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28609 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28610 		    cdda->cdda_subcode);
28611 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28612 		return (EINVAL);
28613 	}
28614 
28615 	/* Build and send the command */
28616 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28617 	bzero(cdb, CDB_GROUP5);
28618 
28619 	if (un->un_f_cfg_cdda == TRUE) {
28620 		cdb[0] = (char)SCMD_READ_CD;
28621 		cdb[1] = 0x04;
28622 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28623 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28624 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28625 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28626 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28627 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28628 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28629 		cdb[9] = 0x10;
28630 		switch (cdda->cdda_subcode) {
28631 		case CDROM_DA_NO_SUBCODE :
28632 			cdb[10] = 0x0;
28633 			break;
28634 		case CDROM_DA_SUBQ :
28635 			cdb[10] = 0x2;
28636 			break;
28637 		case CDROM_DA_ALL_SUBCODE :
28638 			cdb[10] = 0x1;
28639 			break;
28640 		case CDROM_DA_SUBCODE_ONLY :
28641 			/* FALLTHROUGH */
28642 		default :
28643 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28644 			kmem_free(com, sizeof (*com));
28645 			return (ENOTTY);
28646 		}
28647 	} else {
28648 		cdb[0] = (char)SCMD_READ_CDDA;
28649 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28650 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28651 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28652 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28653 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28654 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28655 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28656 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28657 		cdb[10] = cdda->cdda_subcode;
28658 	}
28659 
28660 	com->uscsi_cdb = cdb;
28661 	com->uscsi_cdblen = CDB_GROUP5;
28662 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28663 	com->uscsi_buflen = buflen;
28664 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28665 
28666 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28667 	    SD_PATH_STANDARD);
28668 
28669 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28670 	kmem_free(com, sizeof (*com));
28671 	return (rval);
28672 }
28673 
28674 
28675 /*
28676  *    Function: sr_read_cdxa()
28677  *
28678  * Description: This routine is the driver entry point for handling CD-ROM
28679  *		ioctl requests to return CD-XA (Extended Architecture) data.
28680  *		(CDROMCDXA).
28681  *
28682  *   Arguments: dev	- the device 'dev_t'
28683  *		data	- pointer to user provided CD-XA structure specifying
28684  *			  the data starting address, transfer length, and format
28685  *		flag	- this argument is a pass through to ddi_copyxxx()
28686  *			  directly from the mode argument of ioctl().
28687  *
28688  * Return Code: the code returned by sd_send_scsi_cmd()
28689  *		EFAULT if ddi_copyxxx() fails
28690  *		ENXIO if fail ddi_get_soft_state
28691  *		EINVAL if data pointer is NULL
28692  */
28693 
28694 static int
28695 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28696 {
28697 	struct sd_lun		*un;
28698 	struct uscsi_cmd	*com;
28699 	struct cdrom_cdxa	*cdxa;
28700 	int			rval;
28701 	size_t			buflen;
28702 	char			cdb[CDB_GROUP5];
28703 	uchar_t			read_flags;
28704 
28705 #ifdef _MULTI_DATAMODEL
28706 	/* To support ILP32 applications in an LP64 world */
28707 	struct cdrom_cdxa32		cdrom_cdxa32;
28708 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28709 #endif /* _MULTI_DATAMODEL */
28710 
28711 	if (data == NULL) {
28712 		return (EINVAL);
28713 	}
28714 
28715 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28716 		return (ENXIO);
28717 	}
28718 
28719 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28720 
28721 #ifdef _MULTI_DATAMODEL
28722 	switch (ddi_model_convert_from(flag & FMODELS)) {
28723 	case DDI_MODEL_ILP32:
28724 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28725 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28726 			return (EFAULT);
28727 		}
28728 		/*
28729 		 * Convert the ILP32 uscsi data from the
28730 		 * application to LP64 for internal use.
28731 		 */
28732 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28733 		break;
28734 	case DDI_MODEL_NONE:
28735 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28736 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28737 			return (EFAULT);
28738 		}
28739 		break;
28740 	}
28741 #else /* ! _MULTI_DATAMODEL */
28742 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28743 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28744 		return (EFAULT);
28745 	}
28746 #endif /* _MULTI_DATAMODEL */
28747 
28748 	/*
28749 	 * Since MMC-2 expects max 3 bytes for length, check if the
28750 	 * length input is greater than 3 bytes
28751 	 */
28752 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28753 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28754 		    "cdrom transfer length too large: %d (limit %d)\n",
28755 		    cdxa->cdxa_length, 0xFFFFFF);
28756 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28757 		return (EINVAL);
28758 	}
28759 
28760 	switch (cdxa->cdxa_format) {
28761 	case CDROM_XA_DATA:
28762 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28763 		read_flags = 0x10;
28764 		break;
28765 	case CDROM_XA_SECTOR_DATA:
28766 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28767 		read_flags = 0xf8;
28768 		break;
28769 	case CDROM_XA_DATA_W_ERROR:
28770 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28771 		read_flags = 0xfc;
28772 		break;
28773 	default:
28774 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28775 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28776 		    cdxa->cdxa_format);
28777 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28778 		return (EINVAL);
28779 	}
28780 
28781 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28782 	bzero(cdb, CDB_GROUP5);
28783 	if (un->un_f_mmc_cap == TRUE) {
28784 		cdb[0] = (char)SCMD_READ_CD;
28785 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28786 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28787 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28788 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28789 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28790 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28791 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28792 		cdb[9] = (char)read_flags;
28793 	} else {
28794 		/*
28795 		 * Note: A vendor specific command (0xDB) is being used her to
28796 		 * request a read of all subcodes.
28797 		 */
28798 		cdb[0] = (char)SCMD_READ_CDXA;
28799 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28800 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28801 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28802 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28803 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28804 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28805 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28806 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28807 		cdb[10] = cdxa->cdxa_format;
28808 	}
28809 	com->uscsi_cdb	   = cdb;
28810 	com->uscsi_cdblen  = CDB_GROUP5;
28811 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28812 	com->uscsi_buflen  = buflen;
28813 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28814 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_USERSPACE,
28815 	    SD_PATH_STANDARD);
28816 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28817 	kmem_free(com, sizeof (*com));
28818 	return (rval);
28819 }
28820 
28821 
28822 /*
28823  *    Function: sr_eject()
28824  *
28825  * Description: This routine is the driver entry point for handling CD-ROM
28826  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28827  *
28828  *   Arguments: dev	- the device 'dev_t'
28829  *
28830  * Return Code: the code returned by sd_send_scsi_cmd()
28831  */
28832 
28833 static int
28834 sr_eject(dev_t dev)
28835 {
28836 	struct sd_lun	*un;
28837 	int		rval;
28838 	sd_ssc_t	*ssc;
28839 
28840 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28841 	    (un->un_state == SD_STATE_OFFLINE)) {
28842 		return (ENXIO);
28843 	}
28844 
28845 	/*
28846 	 * To prevent race conditions with the eject
28847 	 * command, keep track of an eject command as
28848 	 * it progresses. If we are already handling
28849 	 * an eject command in the driver for the given
28850 	 * unit and another request to eject is received
28851 	 * immediately return EAGAIN so we don't lose
28852 	 * the command if the current eject command fails.
28853 	 */
28854 	mutex_enter(SD_MUTEX(un));
28855 	if (un->un_f_ejecting == TRUE) {
28856 		mutex_exit(SD_MUTEX(un));
28857 		return (EAGAIN);
28858 	}
28859 	un->un_f_ejecting = TRUE;
28860 	mutex_exit(SD_MUTEX(un));
28861 
28862 	ssc = sd_ssc_init(un);
28863 	rval = sd_send_scsi_DOORLOCK(ssc, SD_REMOVAL_ALLOW,
28864 	    SD_PATH_STANDARD);
28865 	sd_ssc_fini(ssc);
28866 
28867 	if (rval != 0) {
28868 		mutex_enter(SD_MUTEX(un));
28869 		un->un_f_ejecting = FALSE;
28870 		mutex_exit(SD_MUTEX(un));
28871 		return (rval);
28872 	}
28873 
28874 	ssc = sd_ssc_init(un);
28875 	rval = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
28876 	    SD_TARGET_EJECT, SD_PATH_STANDARD);
28877 	sd_ssc_fini(ssc);
28878 
28879 	if (rval == 0) {
28880 		mutex_enter(SD_MUTEX(un));
28881 		sr_ejected(un);
28882 		un->un_mediastate = DKIO_EJECTED;
28883 		un->un_f_ejecting = FALSE;
28884 		cv_broadcast(&un->un_state_cv);
28885 		mutex_exit(SD_MUTEX(un));
28886 	} else {
28887 		mutex_enter(SD_MUTEX(un));
28888 		un->un_f_ejecting = FALSE;
28889 		mutex_exit(SD_MUTEX(un));
28890 	}
28891 	return (rval);
28892 }
28893 
28894 
28895 /*
28896  *    Function: sr_ejected()
28897  *
28898  * Description: This routine updates the soft state structure to invalidate the
28899  *		geometry information after the media has been ejected or a
28900  *		media eject has been detected.
28901  *
28902  *   Arguments: un - driver soft state (unit) structure
28903  */
28904 
28905 static void
28906 sr_ejected(struct sd_lun *un)
28907 {
28908 	struct sd_errstats *stp;
28909 
28910 	ASSERT(un != NULL);
28911 	ASSERT(mutex_owned(SD_MUTEX(un)));
28912 
28913 	un->un_f_blockcount_is_valid	= FALSE;
28914 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28915 	mutex_exit(SD_MUTEX(un));
28916 	cmlb_invalidate(un->un_cmlbhandle, (void *)SD_PATH_DIRECT_PRIORITY);
28917 	mutex_enter(SD_MUTEX(un));
28918 
28919 	if (un->un_errstats != NULL) {
28920 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28921 		stp->sd_capacity.value.ui64 = 0;
28922 	}
28923 }
28924 
28925 
28926 /*
28927  *    Function: sr_check_wp()
28928  *
28929  * Description: This routine checks the write protection of a removable
28930  *      media disk and hotpluggable devices via the write protect bit of
28931  *      the Mode Page Header device specific field. Some devices choke
28932  *      on unsupported mode page. In order to workaround this issue,
28933  *      this routine has been implemented to use 0x3f mode page(request
28934  *      for all pages) for all device types.
28935  *
28936  *   Arguments: dev             - the device 'dev_t'
28937  *
28938  * Return Code: int indicating if the device is write protected (1) or not (0)
28939  *
28940  *     Context: Kernel thread.
28941  *
28942  */
28943 
28944 static int
28945 sr_check_wp(dev_t dev)
28946 {
28947 	struct sd_lun	*un;
28948 	uchar_t		device_specific;
28949 	uchar_t		*sense;
28950 	int		hdrlen;
28951 	int		rval = FALSE;
28952 	int		status;
28953 	sd_ssc_t	*ssc;
28954 
28955 	/*
28956 	 * Note: The return codes for this routine should be reworked to
28957 	 * properly handle the case of a NULL softstate.
28958 	 */
28959 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28960 		return (FALSE);
28961 	}
28962 
28963 	if (un->un_f_cfg_is_atapi == TRUE) {
28964 		/*
28965 		 * The mode page contents are not required; set the allocation
28966 		 * length for the mode page header only
28967 		 */
28968 		hdrlen = MODE_HEADER_LENGTH_GRP2;
28969 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28970 		ssc = sd_ssc_init(un);
28971 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense, hdrlen,
28972 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28973 		sd_ssc_fini(ssc);
28974 		if (status != 0)
28975 			goto err_exit;
28976 		device_specific =
28977 		    ((struct mode_header_grp2 *)sense)->device_specific;
28978 	} else {
28979 		hdrlen = MODE_HEADER_LENGTH;
28980 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28981 		ssc = sd_ssc_init(un);
28982 		status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense, hdrlen,
28983 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD);
28984 		sd_ssc_fini(ssc);
28985 		if (status != 0)
28986 			goto err_exit;
28987 		device_specific =
28988 		    ((struct mode_header *)sense)->device_specific;
28989 	}
28990 
28991 
28992 	/*
28993 	 * Write protect mode sense failed; not all disks
28994 	 * understand this query. Return FALSE assuming that
28995 	 * these devices are not writable.
28996 	 */
28997 	if (device_specific & WRITE_PROTECT) {
28998 		rval = TRUE;
28999 	}
29000 
29001 err_exit:
29002 	kmem_free(sense, hdrlen);
29003 	return (rval);
29004 }
29005 
29006 /*
29007  *    Function: sr_volume_ctrl()
29008  *
29009  * Description: This routine is the driver entry point for handling CD-ROM
29010  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29011  *
29012  *   Arguments: dev	- the device 'dev_t'
29013  *		data	- pointer to user audio volume control structure
29014  *		flag	- this argument is a pass through to ddi_copyxxx()
29015  *			  directly from the mode argument of ioctl().
29016  *
29017  * Return Code: the code returned by sd_send_scsi_cmd()
29018  *		EFAULT if ddi_copyxxx() fails
29019  *		ENXIO if fail ddi_get_soft_state
29020  *		EINVAL if data pointer is NULL
29021  *
29022  */
29023 
29024 static int
29025 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29026 {
29027 	struct sd_lun		*un;
29028 	struct cdrom_volctrl    volume;
29029 	struct cdrom_volctrl    *vol = &volume;
29030 	uchar_t			*sense_page;
29031 	uchar_t			*select_page;
29032 	uchar_t			*sense;
29033 	uchar_t			*select;
29034 	int			sense_buflen;
29035 	int			select_buflen;
29036 	int			rval;
29037 	sd_ssc_t		*ssc;
29038 
29039 	if (data == NULL) {
29040 		return (EINVAL);
29041 	}
29042 
29043 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29044 	    (un->un_state == SD_STATE_OFFLINE)) {
29045 		return (ENXIO);
29046 	}
29047 
29048 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29049 		return (EFAULT);
29050 	}
29051 
29052 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29053 		struct mode_header_grp2		*sense_mhp;
29054 		struct mode_header_grp2		*select_mhp;
29055 		int				bd_len;
29056 
29057 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29058 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29059 		    MODEPAGE_AUDIO_CTRL_LEN;
29060 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29061 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29062 		ssc = sd_ssc_init(un);
29063 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, sense,
29064 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29065 		    SD_PATH_STANDARD);
29066 		sd_ssc_fini(ssc);
29067 
29068 		if (rval != 0) {
29069 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29070 			    "sr_volume_ctrl: Mode Sense Failed\n");
29071 			kmem_free(sense, sense_buflen);
29072 			kmem_free(select, select_buflen);
29073 			return (rval);
29074 		}
29075 		sense_mhp = (struct mode_header_grp2 *)sense;
29076 		select_mhp = (struct mode_header_grp2 *)select;
29077 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29078 		    sense_mhp->bdesc_length_lo;
29079 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29080 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29081 			    "sr_volume_ctrl: Mode Sense returned invalid "
29082 			    "block descriptor length\n");
29083 			kmem_free(sense, sense_buflen);
29084 			kmem_free(select, select_buflen);
29085 			return (EIO);
29086 		}
29087 		sense_page = (uchar_t *)
29088 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29089 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29090 		select_mhp->length_msb = 0;
29091 		select_mhp->length_lsb = 0;
29092 		select_mhp->bdesc_length_hi = 0;
29093 		select_mhp->bdesc_length_lo = 0;
29094 	} else {
29095 		struct mode_header		*sense_mhp, *select_mhp;
29096 
29097 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29098 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29099 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29100 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29101 		ssc = sd_ssc_init(un);
29102 		rval = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, sense,
29103 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29104 		    SD_PATH_STANDARD);
29105 		sd_ssc_fini(ssc);
29106 
29107 		if (rval != 0) {
29108 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29109 			    "sr_volume_ctrl: Mode Sense Failed\n");
29110 			kmem_free(sense, sense_buflen);
29111 			kmem_free(select, select_buflen);
29112 			return (rval);
29113 		}
29114 		sense_mhp  = (struct mode_header *)sense;
29115 		select_mhp = (struct mode_header *)select;
29116 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29117 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29118 			    "sr_volume_ctrl: Mode Sense returned invalid "
29119 			    "block descriptor length\n");
29120 			kmem_free(sense, sense_buflen);
29121 			kmem_free(select, select_buflen);
29122 			return (EIO);
29123 		}
29124 		sense_page = (uchar_t *)
29125 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29126 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29127 		select_mhp->length = 0;
29128 		select_mhp->bdesc_length = 0;
29129 	}
29130 	/*
29131 	 * Note: An audio control data structure could be created and overlayed
29132 	 * on the following in place of the array indexing method implemented.
29133 	 */
29134 
29135 	/* Build the select data for the user volume data */
29136 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29137 	select_page[1] = 0xE;
29138 	/* Set the immediate bit */
29139 	select_page[2] = 0x04;
29140 	/* Zero out reserved fields */
29141 	select_page[3] = 0x00;
29142 	select_page[4] = 0x00;
29143 	/* Return sense data for fields not to be modified */
29144 	select_page[5] = sense_page[5];
29145 	select_page[6] = sense_page[6];
29146 	select_page[7] = sense_page[7];
29147 	/* Set the user specified volume levels for channel 0 and 1 */
29148 	select_page[8] = 0x01;
29149 	select_page[9] = vol->channel0;
29150 	select_page[10] = 0x02;
29151 	select_page[11] = vol->channel1;
29152 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29153 	select_page[12] = sense_page[12];
29154 	select_page[13] = sense_page[13];
29155 	select_page[14] = sense_page[14];
29156 	select_page[15] = sense_page[15];
29157 
29158 	ssc = sd_ssc_init(un);
29159 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29160 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP1, select,
29161 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29162 	} else {
29163 		rval = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, select,
29164 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29165 	}
29166 	sd_ssc_fini(ssc);
29167 
29168 	kmem_free(sense, sense_buflen);
29169 	kmem_free(select, select_buflen);
29170 	return (rval);
29171 }
29172 
29173 
29174 /*
29175  *    Function: sr_read_sony_session_offset()
29176  *
29177  * Description: This routine is the driver entry point for handling CD-ROM
29178  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29179  *		The address of the first track in the last session of a
29180  *		multi-session CD-ROM is returned
29181  *
29182  *		Note: This routine uses a vendor specific key value in the
29183  *		command control field without implementing any vendor check here
29184  *		or in the ioctl routine.
29185  *
29186  *   Arguments: dev	- the device 'dev_t'
29187  *		data	- pointer to an int to hold the requested address
29188  *		flag	- this argument is a pass through to ddi_copyxxx()
29189  *			  directly from the mode argument of ioctl().
29190  *
29191  * Return Code: the code returned by sd_send_scsi_cmd()
29192  *		EFAULT if ddi_copyxxx() fails
29193  *		ENXIO if fail ddi_get_soft_state
29194  *		EINVAL if data pointer is NULL
29195  */
29196 
29197 static int
29198 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29199 {
29200 	struct sd_lun		*un;
29201 	struct uscsi_cmd	*com;
29202 	caddr_t			buffer;
29203 	char			cdb[CDB_GROUP1];
29204 	int			session_offset = 0;
29205 	int			rval;
29206 
29207 	if (data == NULL) {
29208 		return (EINVAL);
29209 	}
29210 
29211 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29212 	    (un->un_state == SD_STATE_OFFLINE)) {
29213 		return (ENXIO);
29214 	}
29215 
29216 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29217 	bzero(cdb, CDB_GROUP1);
29218 	cdb[0] = SCMD_READ_TOC;
29219 	/*
29220 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29221 	 * (4 byte TOC response header + 8 byte response data)
29222 	 */
29223 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29224 	/* Byte 9 is the control byte. A vendor specific value is used */
29225 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29226 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29227 	com->uscsi_cdb = cdb;
29228 	com->uscsi_cdblen = CDB_GROUP1;
29229 	com->uscsi_bufaddr = buffer;
29230 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29231 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29232 
29233 	rval = sd_send_scsi_cmd(dev, com, FKIOCTL, UIO_SYSSPACE,
29234 	    SD_PATH_STANDARD);
29235 	if (rval != 0) {
29236 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29237 		kmem_free(com, sizeof (*com));
29238 		return (rval);
29239 	}
29240 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29241 		session_offset =
29242 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29243 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29244 		/*
29245 		 * Offset returned offset in current lbasize block's. Convert to
29246 		 * 2k block's to return to the user
29247 		 */
29248 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29249 			session_offset >>= 2;
29250 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29251 			session_offset >>= 1;
29252 		}
29253 	}
29254 
29255 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29256 		rval = EFAULT;
29257 	}
29258 
29259 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29260 	kmem_free(com, sizeof (*com));
29261 	return (rval);
29262 }
29263 
29264 
29265 /*
29266  *    Function: sd_wm_cache_constructor()
29267  *
29268  * Description: Cache Constructor for the wmap cache for the read/modify/write
29269  * 		devices.
29270  *
29271  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29272  *		un	- sd_lun structure for the device.
29273  *		flag	- the km flags passed to constructor
29274  *
29275  * Return Code: 0 on success.
29276  *		-1 on failure.
29277  */
29278 
29279 /*ARGSUSED*/
29280 static int
29281 sd_wm_cache_constructor(void *wm, void *un, int flags)
29282 {
29283 	bzero(wm, sizeof (struct sd_w_map));
29284 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29285 	return (0);
29286 }
29287 
29288 
29289 /*
29290  *    Function: sd_wm_cache_destructor()
29291  *
29292  * Description: Cache destructor for the wmap cache for the read/modify/write
29293  * 		devices.
29294  *
29295  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29296  *		un	- sd_lun structure for the device.
29297  */
29298 /*ARGSUSED*/
29299 static void
29300 sd_wm_cache_destructor(void *wm, void *un)
29301 {
29302 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29303 }
29304 
29305 
29306 /*
29307  *    Function: sd_range_lock()
29308  *
29309  * Description: Lock the range of blocks specified as parameter to ensure
29310  *		that read, modify write is atomic and no other i/o writes
29311  *		to the same location. The range is specified in terms
29312  *		of start and end blocks. Block numbers are the actual
29313  *		media block numbers and not system.
29314  *
29315  *   Arguments: un	- sd_lun structure for the device.
29316  *		startb - The starting block number
29317  *		endb - The end block number
29318  *		typ - type of i/o - simple/read_modify_write
29319  *
29320  * Return Code: wm  - pointer to the wmap structure.
29321  *
29322  *     Context: This routine can sleep.
29323  */
29324 
29325 static struct sd_w_map *
29326 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29327 {
29328 	struct sd_w_map *wmp = NULL;
29329 	struct sd_w_map *sl_wmp = NULL;
29330 	struct sd_w_map *tmp_wmp;
29331 	wm_state state = SD_WM_CHK_LIST;
29332 
29333 
29334 	ASSERT(un != NULL);
29335 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29336 
29337 	mutex_enter(SD_MUTEX(un));
29338 
29339 	while (state != SD_WM_DONE) {
29340 
29341 		switch (state) {
29342 		case SD_WM_CHK_LIST:
29343 			/*
29344 			 * This is the starting state. Check the wmap list
29345 			 * to see if the range is currently available.
29346 			 */
29347 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29348 				/*
29349 				 * If this is a simple write and no rmw
29350 				 * i/o is pending then try to lock the
29351 				 * range as the range should be available.
29352 				 */
29353 				state = SD_WM_LOCK_RANGE;
29354 			} else {
29355 				tmp_wmp = sd_get_range(un, startb, endb);
29356 				if (tmp_wmp != NULL) {
29357 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29358 						/*
29359 						 * Should not keep onlist wmps
29360 						 * while waiting this macro
29361 						 * will also do wmp = NULL;
29362 						 */
29363 						FREE_ONLIST_WMAP(un, wmp);
29364 					}
29365 					/*
29366 					 * sl_wmp is the wmap on which wait
29367 					 * is done, since the tmp_wmp points
29368 					 * to the inuse wmap, set sl_wmp to
29369 					 * tmp_wmp and change the state to sleep
29370 					 */
29371 					sl_wmp = tmp_wmp;
29372 					state = SD_WM_WAIT_MAP;
29373 				} else {
29374 					state = SD_WM_LOCK_RANGE;
29375 				}
29376 
29377 			}
29378 			break;
29379 
29380 		case SD_WM_LOCK_RANGE:
29381 			ASSERT(un->un_wm_cache);
29382 			/*
29383 			 * The range need to be locked, try to get a wmap.
29384 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29385 			 * if possible as we will have to release the sd mutex
29386 			 * if we have to sleep.
29387 			 */
29388 			if (wmp == NULL)
29389 				wmp = kmem_cache_alloc(un->un_wm_cache,
29390 				    KM_NOSLEEP);
29391 			if (wmp == NULL) {
29392 				mutex_exit(SD_MUTEX(un));
29393 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29394 				    (sd_lun::un_wm_cache))
29395 				wmp = kmem_cache_alloc(un->un_wm_cache,
29396 				    KM_SLEEP);
29397 				mutex_enter(SD_MUTEX(un));
29398 				/*
29399 				 * we released the mutex so recheck and go to
29400 				 * check list state.
29401 				 */
29402 				state = SD_WM_CHK_LIST;
29403 			} else {
29404 				/*
29405 				 * We exit out of state machine since we
29406 				 * have the wmap. Do the housekeeping first.
29407 				 * place the wmap on the wmap list if it is not
29408 				 * on it already and then set the state to done.
29409 				 */
29410 				wmp->wm_start = startb;
29411 				wmp->wm_end = endb;
29412 				wmp->wm_flags = typ | SD_WM_BUSY;
29413 				if (typ & SD_WTYPE_RMW) {
29414 					un->un_rmw_count++;
29415 				}
29416 				/*
29417 				 * If not already on the list then link
29418 				 */
29419 				if (!ONLIST(un, wmp)) {
29420 					wmp->wm_next = un->un_wm;
29421 					wmp->wm_prev = NULL;
29422 					if (wmp->wm_next)
29423 						wmp->wm_next->wm_prev = wmp;
29424 					un->un_wm = wmp;
29425 				}
29426 				state = SD_WM_DONE;
29427 			}
29428 			break;
29429 
29430 		case SD_WM_WAIT_MAP:
29431 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29432 			/*
29433 			 * Wait is done on sl_wmp, which is set in the
29434 			 * check_list state.
29435 			 */
29436 			sl_wmp->wm_wanted_count++;
29437 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29438 			sl_wmp->wm_wanted_count--;
29439 			/*
29440 			 * We can reuse the memory from the completed sl_wmp
29441 			 * lock range for our new lock, but only if noone is
29442 			 * waiting for it.
29443 			 */
29444 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29445 			if (sl_wmp->wm_wanted_count == 0) {
29446 				if (wmp != NULL)
29447 					CHK_N_FREEWMP(un, wmp);
29448 				wmp = sl_wmp;
29449 			}
29450 			sl_wmp = NULL;
29451 			/*
29452 			 * After waking up, need to recheck for availability of
29453 			 * range.
29454 			 */
29455 			state = SD_WM_CHK_LIST;
29456 			break;
29457 
29458 		default:
29459 			panic("sd_range_lock: "
29460 			    "Unknown state %d in sd_range_lock", state);
29461 			/*NOTREACHED*/
29462 		} /* switch(state) */
29463 
29464 	} /* while(state != SD_WM_DONE) */
29465 
29466 	mutex_exit(SD_MUTEX(un));
29467 
29468 	ASSERT(wmp != NULL);
29469 
29470 	return (wmp);
29471 }
29472 
29473 
29474 /*
29475  *    Function: sd_get_range()
29476  *
29477  * Description: Find if there any overlapping I/O to this one
29478  *		Returns the write-map of 1st such I/O, NULL otherwise.
29479  *
29480  *   Arguments: un	- sd_lun structure for the device.
29481  *		startb - The starting block number
29482  *		endb - The end block number
29483  *
29484  * Return Code: wm  - pointer to the wmap structure.
29485  */
29486 
29487 static struct sd_w_map *
29488 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29489 {
29490 	struct sd_w_map *wmp;
29491 
29492 	ASSERT(un != NULL);
29493 
29494 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29495 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29496 			continue;
29497 		}
29498 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29499 			break;
29500 		}
29501 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29502 			break;
29503 		}
29504 	}
29505 
29506 	return (wmp);
29507 }
29508 
29509 
29510 /*
29511  *    Function: sd_free_inlist_wmap()
29512  *
29513  * Description: Unlink and free a write map struct.
29514  *
29515  *   Arguments: un      - sd_lun structure for the device.
29516  *		wmp	- sd_w_map which needs to be unlinked.
29517  */
29518 
29519 static void
29520 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29521 {
29522 	ASSERT(un != NULL);
29523 
29524 	if (un->un_wm == wmp) {
29525 		un->un_wm = wmp->wm_next;
29526 	} else {
29527 		wmp->wm_prev->wm_next = wmp->wm_next;
29528 	}
29529 
29530 	if (wmp->wm_next) {
29531 		wmp->wm_next->wm_prev = wmp->wm_prev;
29532 	}
29533 
29534 	wmp->wm_next = wmp->wm_prev = NULL;
29535 
29536 	kmem_cache_free(un->un_wm_cache, wmp);
29537 }
29538 
29539 
29540 /*
29541  *    Function: sd_range_unlock()
29542  *
29543  * Description: Unlock the range locked by wm.
29544  *		Free write map if nobody else is waiting on it.
29545  *
29546  *   Arguments: un      - sd_lun structure for the device.
29547  *              wmp     - sd_w_map which needs to be unlinked.
29548  */
29549 
29550 static void
29551 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29552 {
29553 	ASSERT(un != NULL);
29554 	ASSERT(wm != NULL);
29555 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29556 
29557 	mutex_enter(SD_MUTEX(un));
29558 
29559 	if (wm->wm_flags & SD_WTYPE_RMW) {
29560 		un->un_rmw_count--;
29561 	}
29562 
29563 	if (wm->wm_wanted_count) {
29564 		wm->wm_flags = 0;
29565 		/*
29566 		 * Broadcast that the wmap is available now.
29567 		 */
29568 		cv_broadcast(&wm->wm_avail);
29569 	} else {
29570 		/*
29571 		 * If no one is waiting on the map, it should be free'ed.
29572 		 */
29573 		sd_free_inlist_wmap(un, wm);
29574 	}
29575 
29576 	mutex_exit(SD_MUTEX(un));
29577 }
29578 
29579 
29580 /*
29581  *    Function: sd_read_modify_write_task
29582  *
29583  * Description: Called from a taskq thread to initiate the write phase of
29584  *		a read-modify-write request.  This is used for targets where
29585  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29586  *
29587  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29588  *
29589  *     Context: Called under taskq thread context.
29590  */
29591 
29592 static void
29593 sd_read_modify_write_task(void *arg)
29594 {
29595 	struct sd_mapblocksize_info	*bsp;
29596 	struct buf	*bp;
29597 	struct sd_xbuf	*xp;
29598 	struct sd_lun	*un;
29599 
29600 	bp = arg;	/* The bp is given in arg */
29601 	ASSERT(bp != NULL);
29602 
29603 	/* Get the pointer to the layer-private data struct */
29604 	xp = SD_GET_XBUF(bp);
29605 	ASSERT(xp != NULL);
29606 	bsp = xp->xb_private;
29607 	ASSERT(bsp != NULL);
29608 
29609 	un = SD_GET_UN(bp);
29610 	ASSERT(un != NULL);
29611 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29612 
29613 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29614 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29615 
29616 	/*
29617 	 * This is the write phase of a read-modify-write request, called
29618 	 * under the context of a taskq thread in response to the completion
29619 	 * of the read portion of the rmw request completing under interrupt
29620 	 * context. The write request must be sent from here down the iostart
29621 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29622 	 * we use the layer index saved in the layer-private data area.
29623 	 */
29624 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29625 
29626 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29627 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29628 }
29629 
29630 
29631 /*
29632  *    Function: sddump_do_read_of_rmw()
29633  *
29634  * Description: This routine will be called from sddump, If sddump is called
29635  *		with an I/O which not aligned on device blocksize boundary
29636  *		then the write has to be converted to read-modify-write.
29637  *		Do the read part here in order to keep sddump simple.
29638  *		Note - That the sd_mutex is held across the call to this
29639  *		routine.
29640  *
29641  *   Arguments: un	- sd_lun
29642  *		blkno	- block number in terms of media block size.
29643  *		nblk	- number of blocks.
29644  *		bpp	- pointer to pointer to the buf structure. On return
29645  *			from this function, *bpp points to the valid buffer
29646  *			to which the write has to be done.
29647  *
29648  * Return Code: 0 for success or errno-type return code
29649  */
29650 
29651 static int
29652 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29653 	struct buf **bpp)
29654 {
29655 	int err;
29656 	int i;
29657 	int rval;
29658 	struct buf *bp;
29659 	struct scsi_pkt *pkt = NULL;
29660 	uint32_t target_blocksize;
29661 
29662 	ASSERT(un != NULL);
29663 	ASSERT(mutex_owned(SD_MUTEX(un)));
29664 
29665 	target_blocksize = un->un_tgt_blocksize;
29666 
29667 	mutex_exit(SD_MUTEX(un));
29668 
29669 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29670 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29671 	if (bp == NULL) {
29672 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29673 		    "no resources for dumping; giving up");
29674 		err = ENOMEM;
29675 		goto done;
29676 	}
29677 
29678 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29679 	    blkno, nblk);
29680 	if (rval != 0) {
29681 		scsi_free_consistent_buf(bp);
29682 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29683 		    "no resources for dumping; giving up");
29684 		err = ENOMEM;
29685 		goto done;
29686 	}
29687 
29688 	pkt->pkt_flags |= FLAG_NOINTR;
29689 
29690 	err = EIO;
29691 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29692 
29693 		/*
29694 		 * Scsi_poll returns 0 (success) if the command completes and
29695 		 * the status block is STATUS_GOOD.  We should only check
29696 		 * errors if this condition is not true.  Even then we should
29697 		 * send our own request sense packet only if we have a check
29698 		 * condition and auto request sense has not been performed by
29699 		 * the hba.
29700 		 */
29701 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29702 
29703 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29704 			err = 0;
29705 			break;
29706 		}
29707 
29708 		/*
29709 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29710 		 * no need to read RQS data.
29711 		 */
29712 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29713 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29714 			    "Error while dumping state with rmw..."
29715 			    "Device is gone\n");
29716 			break;
29717 		}
29718 
29719 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29720 			SD_INFO(SD_LOG_DUMP, un,
29721 			    "sddump: read failed with CHECK, try # %d\n", i);
29722 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29723 				(void) sd_send_polled_RQS(un);
29724 			}
29725 
29726 			continue;
29727 		}
29728 
29729 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29730 			int reset_retval = 0;
29731 
29732 			SD_INFO(SD_LOG_DUMP, un,
29733 			    "sddump: read failed with BUSY, try # %d\n", i);
29734 
29735 			if (un->un_f_lun_reset_enabled == TRUE) {
29736 				reset_retval = scsi_reset(SD_ADDRESS(un),
29737 				    RESET_LUN);
29738 			}
29739 			if (reset_retval == 0) {
29740 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29741 			}
29742 			(void) sd_send_polled_RQS(un);
29743 
29744 		} else {
29745 			SD_INFO(SD_LOG_DUMP, un,
29746 			    "sddump: read failed with 0x%x, try # %d\n",
29747 			    SD_GET_PKT_STATUS(pkt), i);
29748 			mutex_enter(SD_MUTEX(un));
29749 			sd_reset_target(un, pkt);
29750 			mutex_exit(SD_MUTEX(un));
29751 		}
29752 
29753 		/*
29754 		 * If we are not getting anywhere with lun/target resets,
29755 		 * let's reset the bus.
29756 		 */
29757 		if (i > SD_NDUMP_RETRIES/2) {
29758 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29759 			(void) sd_send_polled_RQS(un);
29760 		}
29761 
29762 	}
29763 	scsi_destroy_pkt(pkt);
29764 
29765 	if (err != 0) {
29766 		scsi_free_consistent_buf(bp);
29767 		*bpp = NULL;
29768 	} else {
29769 		*bpp = bp;
29770 	}
29771 
29772 done:
29773 	mutex_enter(SD_MUTEX(un));
29774 	return (err);
29775 }
29776 
29777 
29778 /*
29779  *    Function: sd_failfast_flushq
29780  *
29781  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29782  *		in b_flags and move them onto the failfast queue, then kick
29783  *		off a thread to return all bp's on the failfast queue to
29784  *		their owners with an error set.
29785  *
29786  *   Arguments: un - pointer to the soft state struct for the instance.
29787  *
29788  *     Context: may execute in interrupt context.
29789  */
29790 
29791 static void
29792 sd_failfast_flushq(struct sd_lun *un)
29793 {
29794 	struct buf *bp;
29795 	struct buf *next_waitq_bp;
29796 	struct buf *prev_waitq_bp = NULL;
29797 
29798 	ASSERT(un != NULL);
29799 	ASSERT(mutex_owned(SD_MUTEX(un)));
29800 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29801 	ASSERT(un->un_failfast_bp == NULL);
29802 
29803 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29804 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29805 
29806 	/*
29807 	 * Check if we should flush all bufs when entering failfast state, or
29808 	 * just those with B_FAILFAST set.
29809 	 */
29810 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29811 		/*
29812 		 * Move *all* bp's on the wait queue to the failfast flush
29813 		 * queue, including those that do NOT have B_FAILFAST set.
29814 		 */
29815 		if (un->un_failfast_headp == NULL) {
29816 			ASSERT(un->un_failfast_tailp == NULL);
29817 			un->un_failfast_headp = un->un_waitq_headp;
29818 		} else {
29819 			ASSERT(un->un_failfast_tailp != NULL);
29820 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29821 		}
29822 
29823 		un->un_failfast_tailp = un->un_waitq_tailp;
29824 
29825 		/* update kstat for each bp moved out of the waitq */
29826 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29827 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29828 		}
29829 
29830 		/* empty the waitq */
29831 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29832 
29833 	} else {
29834 		/*
29835 		 * Go thru the wait queue, pick off all entries with
29836 		 * B_FAILFAST set, and move these onto the failfast queue.
29837 		 */
29838 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29839 			/*
29840 			 * Save the pointer to the next bp on the wait queue,
29841 			 * so we get to it on the next iteration of this loop.
29842 			 */
29843 			next_waitq_bp = bp->av_forw;
29844 
29845 			/*
29846 			 * If this bp from the wait queue does NOT have
29847 			 * B_FAILFAST set, just move on to the next element
29848 			 * in the wait queue. Note, this is the only place
29849 			 * where it is correct to set prev_waitq_bp.
29850 			 */
29851 			if ((bp->b_flags & B_FAILFAST) == 0) {
29852 				prev_waitq_bp = bp;
29853 				continue;
29854 			}
29855 
29856 			/*
29857 			 * Remove the bp from the wait queue.
29858 			 */
29859 			if (bp == un->un_waitq_headp) {
29860 				/* The bp is the first element of the waitq. */
29861 				un->un_waitq_headp = next_waitq_bp;
29862 				if (un->un_waitq_headp == NULL) {
29863 					/* The wait queue is now empty */
29864 					un->un_waitq_tailp = NULL;
29865 				}
29866 			} else {
29867 				/*
29868 				 * The bp is either somewhere in the middle
29869 				 * or at the end of the wait queue.
29870 				 */
29871 				ASSERT(un->un_waitq_headp != NULL);
29872 				ASSERT(prev_waitq_bp != NULL);
29873 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29874 				    == 0);
29875 				if (bp == un->un_waitq_tailp) {
29876 					/* bp is the last entry on the waitq. */
29877 					ASSERT(next_waitq_bp == NULL);
29878 					un->un_waitq_tailp = prev_waitq_bp;
29879 				}
29880 				prev_waitq_bp->av_forw = next_waitq_bp;
29881 			}
29882 			bp->av_forw = NULL;
29883 
29884 			/*
29885 			 * update kstat since the bp is moved out of
29886 			 * the waitq
29887 			 */
29888 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29889 
29890 			/*
29891 			 * Now put the bp onto the failfast queue.
29892 			 */
29893 			if (un->un_failfast_headp == NULL) {
29894 				/* failfast queue is currently empty */
29895 				ASSERT(un->un_failfast_tailp == NULL);
29896 				un->un_failfast_headp =
29897 				    un->un_failfast_tailp = bp;
29898 			} else {
29899 				/* Add the bp to the end of the failfast q */
29900 				ASSERT(un->un_failfast_tailp != NULL);
29901 				ASSERT(un->un_failfast_tailp->b_flags &
29902 				    B_FAILFAST);
29903 				un->un_failfast_tailp->av_forw = bp;
29904 				un->un_failfast_tailp = bp;
29905 			}
29906 		}
29907 	}
29908 
29909 	/*
29910 	 * Now return all bp's on the failfast queue to their owners.
29911 	 */
29912 	while ((bp = un->un_failfast_headp) != NULL) {
29913 
29914 		un->un_failfast_headp = bp->av_forw;
29915 		if (un->un_failfast_headp == NULL) {
29916 			un->un_failfast_tailp = NULL;
29917 		}
29918 
29919 		/*
29920 		 * We want to return the bp with a failure error code, but
29921 		 * we do not want a call to sd_start_cmds() to occur here,
29922 		 * so use sd_return_failed_command_no_restart() instead of
29923 		 * sd_return_failed_command().
29924 		 */
29925 		sd_return_failed_command_no_restart(un, bp, EIO);
29926 	}
29927 
29928 	/* Flush the xbuf queues if required. */
29929 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29930 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29931 	}
29932 
29933 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29934 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29935 }
29936 
29937 
29938 /*
29939  *    Function: sd_failfast_flushq_callback
29940  *
29941  * Description: Return TRUE if the given bp meets the criteria for failfast
29942  *		flushing. Used with ddi_xbuf_flushq(9F).
29943  *
29944  *   Arguments: bp - ptr to buf struct to be examined.
29945  *
29946  *     Context: Any
29947  */
29948 
29949 static int
29950 sd_failfast_flushq_callback(struct buf *bp)
29951 {
29952 	/*
29953 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29954 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29955 	 */
29956 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29957 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29958 }
29959 
29960 
29961 
29962 /*
29963  * Function: sd_setup_next_xfer
29964  *
29965  * Description: Prepare next I/O operation using DMA_PARTIAL
29966  *
29967  */
29968 
29969 static int
29970 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29971     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29972 {
29973 	ssize_t	num_blks_not_xfered;
29974 	daddr_t	strt_blk_num;
29975 	ssize_t	bytes_not_xfered;
29976 	int	rval;
29977 
29978 	ASSERT(pkt->pkt_resid == 0);
29979 
29980 	/*
29981 	 * Calculate next block number and amount to be transferred.
29982 	 *
29983 	 * How much data NOT transfered to the HBA yet.
29984 	 */
29985 	bytes_not_xfered = xp->xb_dma_resid;
29986 
29987 	/*
29988 	 * figure how many blocks NOT transfered to the HBA yet.
29989 	 */
29990 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29991 
29992 	/*
29993 	 * set starting block number to the end of what WAS transfered.
29994 	 */
29995 	strt_blk_num = xp->xb_blkno +
29996 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29997 
29998 	/*
29999 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
30000 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
30001 	 * the disk mutex here.
30002 	 */
30003 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30004 	    strt_blk_num, num_blks_not_xfered);
30005 
30006 	if (rval == 0) {
30007 
30008 		/*
30009 		 * Success.
30010 		 *
30011 		 * Adjust things if there are still more blocks to be
30012 		 * transfered.
30013 		 */
30014 		xp->xb_dma_resid = pkt->pkt_resid;
30015 		pkt->pkt_resid = 0;
30016 
30017 		return (1);
30018 	}
30019 
30020 	/*
30021 	 * There's really only one possible return value from
30022 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30023 	 * returns NULL.
30024 	 */
30025 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30026 
30027 	bp->b_resid = bp->b_bcount;
30028 	bp->b_flags |= B_ERROR;
30029 
30030 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30031 	    "Error setting up next portion of DMA transfer\n");
30032 
30033 	return (0);
30034 }
30035 
30036 /*
30037  *    Function: sd_panic_for_res_conflict
30038  *
30039  * Description: Call panic with a string formatted with "Reservation Conflict"
30040  *		and a human readable identifier indicating the SD instance
30041  *		that experienced the reservation conflict.
30042  *
30043  *   Arguments: un - pointer to the soft state struct for the instance.
30044  *
30045  *     Context: may execute in interrupt context.
30046  */
30047 
30048 #define	SD_RESV_CONFLICT_FMT_LEN 40
30049 void
30050 sd_panic_for_res_conflict(struct sd_lun *un)
30051 {
30052 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30053 	char path_str[MAXPATHLEN];
30054 
30055 	(void) snprintf(panic_str, sizeof (panic_str),
30056 	    "Reservation Conflict\nDisk: %s",
30057 	    ddi_pathname(SD_DEVINFO(un), path_str));
30058 
30059 	panic(panic_str);
30060 }
30061 
30062 /*
30063  * Note: The following sd_faultinjection_ioctl( ) routines implement
30064  * driver support for handling fault injection for error analysis
30065  * causing faults in multiple layers of the driver.
30066  *
30067  */
30068 
30069 #ifdef SD_FAULT_INJECTION
30070 static uint_t   sd_fault_injection_on = 0;
30071 
30072 /*
30073  *    Function: sd_faultinjection_ioctl()
30074  *
30075  * Description: This routine is the driver entry point for handling
30076  *              faultinjection ioctls to inject errors into the
30077  *              layer model
30078  *
30079  *   Arguments: cmd	- the ioctl cmd received
30080  *		arg	- the arguments from user and returns
30081  */
30082 
30083 static void
30084 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30085 
30086 	uint_t i = 0;
30087 	uint_t rval;
30088 
30089 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30090 
30091 	mutex_enter(SD_MUTEX(un));
30092 
30093 	switch (cmd) {
30094 	case SDIOCRUN:
30095 		/* Allow pushed faults to be injected */
30096 		SD_INFO(SD_LOG_SDTEST, un,
30097 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30098 
30099 		sd_fault_injection_on = 1;
30100 
30101 		SD_INFO(SD_LOG_IOERR, un,
30102 		    "sd_faultinjection_ioctl: run finished\n");
30103 		break;
30104 
30105 	case SDIOCSTART:
30106 		/* Start Injection Session */
30107 		SD_INFO(SD_LOG_SDTEST, un,
30108 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30109 
30110 		sd_fault_injection_on = 0;
30111 		un->sd_injection_mask = 0xFFFFFFFF;
30112 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30113 			un->sd_fi_fifo_pkt[i] = NULL;
30114 			un->sd_fi_fifo_xb[i] = NULL;
30115 			un->sd_fi_fifo_un[i] = NULL;
30116 			un->sd_fi_fifo_arq[i] = NULL;
30117 		}
30118 		un->sd_fi_fifo_start = 0;
30119 		un->sd_fi_fifo_end = 0;
30120 
30121 		mutex_enter(&(un->un_fi_mutex));
30122 		un->sd_fi_log[0] = '\0';
30123 		un->sd_fi_buf_len = 0;
30124 		mutex_exit(&(un->un_fi_mutex));
30125 
30126 		SD_INFO(SD_LOG_IOERR, un,
30127 		    "sd_faultinjection_ioctl: start finished\n");
30128 		break;
30129 
30130 	case SDIOCSTOP:
30131 		/* Stop Injection Session */
30132 		SD_INFO(SD_LOG_SDTEST, un,
30133 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30134 		sd_fault_injection_on = 0;
30135 		un->sd_injection_mask = 0x0;
30136 
30137 		/* Empty stray or unuseds structs from fifo */
30138 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30139 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30140 				kmem_free(un->sd_fi_fifo_pkt[i],
30141 				    sizeof (struct sd_fi_pkt));
30142 			}
30143 			if (un->sd_fi_fifo_xb[i] != NULL) {
30144 				kmem_free(un->sd_fi_fifo_xb[i],
30145 				    sizeof (struct sd_fi_xb));
30146 			}
30147 			if (un->sd_fi_fifo_un[i] != NULL) {
30148 				kmem_free(un->sd_fi_fifo_un[i],
30149 				    sizeof (struct sd_fi_un));
30150 			}
30151 			if (un->sd_fi_fifo_arq[i] != NULL) {
30152 				kmem_free(un->sd_fi_fifo_arq[i],
30153 				    sizeof (struct sd_fi_arq));
30154 			}
30155 			un->sd_fi_fifo_pkt[i] = NULL;
30156 			un->sd_fi_fifo_un[i] = NULL;
30157 			un->sd_fi_fifo_xb[i] = NULL;
30158 			un->sd_fi_fifo_arq[i] = NULL;
30159 		}
30160 		un->sd_fi_fifo_start = 0;
30161 		un->sd_fi_fifo_end = 0;
30162 
30163 		SD_INFO(SD_LOG_IOERR, un,
30164 		    "sd_faultinjection_ioctl: stop finished\n");
30165 		break;
30166 
30167 	case SDIOCINSERTPKT:
30168 		/* Store a packet struct to be pushed onto fifo */
30169 		SD_INFO(SD_LOG_SDTEST, un,
30170 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30171 
30172 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30173 
30174 		sd_fault_injection_on = 0;
30175 
30176 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30177 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30178 			kmem_free(un->sd_fi_fifo_pkt[i],
30179 			    sizeof (struct sd_fi_pkt));
30180 		}
30181 		if (arg != NULL) {
30182 			un->sd_fi_fifo_pkt[i] =
30183 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30184 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30185 				/* Alloc failed don't store anything */
30186 				break;
30187 			}
30188 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30189 			    sizeof (struct sd_fi_pkt), 0);
30190 			if (rval == -1) {
30191 				kmem_free(un->sd_fi_fifo_pkt[i],
30192 				    sizeof (struct sd_fi_pkt));
30193 				un->sd_fi_fifo_pkt[i] = NULL;
30194 			}
30195 		} else {
30196 			SD_INFO(SD_LOG_IOERR, un,
30197 			    "sd_faultinjection_ioctl: pkt null\n");
30198 		}
30199 		break;
30200 
30201 	case SDIOCINSERTXB:
30202 		/* Store a xb struct to be pushed onto fifo */
30203 		SD_INFO(SD_LOG_SDTEST, un,
30204 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30205 
30206 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30207 
30208 		sd_fault_injection_on = 0;
30209 
30210 		if (un->sd_fi_fifo_xb[i] != NULL) {
30211 			kmem_free(un->sd_fi_fifo_xb[i],
30212 			    sizeof (struct sd_fi_xb));
30213 			un->sd_fi_fifo_xb[i] = NULL;
30214 		}
30215 		if (arg != NULL) {
30216 			un->sd_fi_fifo_xb[i] =
30217 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30218 			if (un->sd_fi_fifo_xb[i] == NULL) {
30219 				/* Alloc failed don't store anything */
30220 				break;
30221 			}
30222 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30223 			    sizeof (struct sd_fi_xb), 0);
30224 
30225 			if (rval == -1) {
30226 				kmem_free(un->sd_fi_fifo_xb[i],
30227 				    sizeof (struct sd_fi_xb));
30228 				un->sd_fi_fifo_xb[i] = NULL;
30229 			}
30230 		} else {
30231 			SD_INFO(SD_LOG_IOERR, un,
30232 			    "sd_faultinjection_ioctl: xb null\n");
30233 		}
30234 		break;
30235 
30236 	case SDIOCINSERTUN:
30237 		/* Store a un struct to be pushed onto fifo */
30238 		SD_INFO(SD_LOG_SDTEST, un,
30239 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30240 
30241 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30242 
30243 		sd_fault_injection_on = 0;
30244 
30245 		if (un->sd_fi_fifo_un[i] != NULL) {
30246 			kmem_free(un->sd_fi_fifo_un[i],
30247 			    sizeof (struct sd_fi_un));
30248 			un->sd_fi_fifo_un[i] = NULL;
30249 		}
30250 		if (arg != NULL) {
30251 			un->sd_fi_fifo_un[i] =
30252 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30253 			if (un->sd_fi_fifo_un[i] == NULL) {
30254 				/* Alloc failed don't store anything */
30255 				break;
30256 			}
30257 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30258 			    sizeof (struct sd_fi_un), 0);
30259 			if (rval == -1) {
30260 				kmem_free(un->sd_fi_fifo_un[i],
30261 				    sizeof (struct sd_fi_un));
30262 				un->sd_fi_fifo_un[i] = NULL;
30263 			}
30264 
30265 		} else {
30266 			SD_INFO(SD_LOG_IOERR, un,
30267 			    "sd_faultinjection_ioctl: un null\n");
30268 		}
30269 
30270 		break;
30271 
30272 	case SDIOCINSERTARQ:
30273 		/* Store a arq struct to be pushed onto fifo */
30274 		SD_INFO(SD_LOG_SDTEST, un,
30275 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30276 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30277 
30278 		sd_fault_injection_on = 0;
30279 
30280 		if (un->sd_fi_fifo_arq[i] != NULL) {
30281 			kmem_free(un->sd_fi_fifo_arq[i],
30282 			    sizeof (struct sd_fi_arq));
30283 			un->sd_fi_fifo_arq[i] = NULL;
30284 		}
30285 		if (arg != NULL) {
30286 			un->sd_fi_fifo_arq[i] =
30287 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30288 			if (un->sd_fi_fifo_arq[i] == NULL) {
30289 				/* Alloc failed don't store anything */
30290 				break;
30291 			}
30292 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30293 			    sizeof (struct sd_fi_arq), 0);
30294 			if (rval == -1) {
30295 				kmem_free(un->sd_fi_fifo_arq[i],
30296 				    sizeof (struct sd_fi_arq));
30297 				un->sd_fi_fifo_arq[i] = NULL;
30298 			}
30299 
30300 		} else {
30301 			SD_INFO(SD_LOG_IOERR, un,
30302 			    "sd_faultinjection_ioctl: arq null\n");
30303 		}
30304 
30305 		break;
30306 
30307 	case SDIOCPUSH:
30308 		/* Push stored xb, pkt, un, and arq onto fifo */
30309 		sd_fault_injection_on = 0;
30310 
30311 		if (arg != NULL) {
30312 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30313 			if (rval != -1 &&
30314 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30315 				un->sd_fi_fifo_end += i;
30316 			}
30317 		} else {
30318 			SD_INFO(SD_LOG_IOERR, un,
30319 			    "sd_faultinjection_ioctl: push arg null\n");
30320 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30321 				un->sd_fi_fifo_end++;
30322 			}
30323 		}
30324 		SD_INFO(SD_LOG_IOERR, un,
30325 		    "sd_faultinjection_ioctl: push to end=%d\n",
30326 		    un->sd_fi_fifo_end);
30327 		break;
30328 
30329 	case SDIOCRETRIEVE:
30330 		/* Return buffer of log from Injection session */
30331 		SD_INFO(SD_LOG_SDTEST, un,
30332 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30333 
30334 		sd_fault_injection_on = 0;
30335 
30336 		mutex_enter(&(un->un_fi_mutex));
30337 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30338 		    un->sd_fi_buf_len+1, 0);
30339 		mutex_exit(&(un->un_fi_mutex));
30340 
30341 		if (rval == -1) {
30342 			/*
30343 			 * arg is possibly invalid setting
30344 			 * it to NULL for return
30345 			 */
30346 			arg = NULL;
30347 		}
30348 		break;
30349 	}
30350 
30351 	mutex_exit(SD_MUTEX(un));
30352 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30353 			    " exit\n");
30354 }
30355 
30356 
30357 /*
30358  *    Function: sd_injection_log()
30359  *
30360  * Description: This routine adds buff to the already existing injection log
30361  *              for retrieval via faultinjection_ioctl for use in fault
30362  *              detection and recovery
30363  *
30364  *   Arguments: buf - the string to add to the log
30365  */
30366 
30367 static void
30368 sd_injection_log(char *buf, struct sd_lun *un)
30369 {
30370 	uint_t len;
30371 
30372 	ASSERT(un != NULL);
30373 	ASSERT(buf != NULL);
30374 
30375 	mutex_enter(&(un->un_fi_mutex));
30376 
30377 	len = min(strlen(buf), 255);
30378 	/* Add logged value to Injection log to be returned later */
30379 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30380 		uint_t	offset = strlen((char *)un->sd_fi_log);
30381 		char *destp = (char *)un->sd_fi_log + offset;
30382 		int i;
30383 		for (i = 0; i < len; i++) {
30384 			*destp++ = *buf++;
30385 		}
30386 		un->sd_fi_buf_len += len;
30387 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30388 	}
30389 
30390 	mutex_exit(&(un->un_fi_mutex));
30391 }
30392 
30393 
30394 /*
30395  *    Function: sd_faultinjection()
30396  *
30397  * Description: This routine takes the pkt and changes its
30398  *		content based on error injection scenerio.
30399  *
30400  *   Arguments: pktp	- packet to be changed
30401  */
30402 
30403 static void
30404 sd_faultinjection(struct scsi_pkt *pktp)
30405 {
30406 	uint_t i;
30407 	struct sd_fi_pkt *fi_pkt;
30408 	struct sd_fi_xb *fi_xb;
30409 	struct sd_fi_un *fi_un;
30410 	struct sd_fi_arq *fi_arq;
30411 	struct buf *bp;
30412 	struct sd_xbuf *xb;
30413 	struct sd_lun *un;
30414 
30415 	ASSERT(pktp != NULL);
30416 
30417 	/* pull bp xb and un from pktp */
30418 	bp = (struct buf *)pktp->pkt_private;
30419 	xb = SD_GET_XBUF(bp);
30420 	un = SD_GET_UN(bp);
30421 
30422 	ASSERT(un != NULL);
30423 
30424 	mutex_enter(SD_MUTEX(un));
30425 
30426 	SD_TRACE(SD_LOG_SDTEST, un,
30427 	    "sd_faultinjection: entry Injection from sdintr\n");
30428 
30429 	/* if injection is off return */
30430 	if (sd_fault_injection_on == 0 ||
30431 	    un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30432 		mutex_exit(SD_MUTEX(un));
30433 		return;
30434 	}
30435 
30436 	SD_INFO(SD_LOG_SDTEST, un,
30437 	    "sd_faultinjection: is working for copying\n");
30438 
30439 	/* take next set off fifo */
30440 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30441 
30442 	fi_pkt = un->sd_fi_fifo_pkt[i];
30443 	fi_xb = un->sd_fi_fifo_xb[i];
30444 	fi_un = un->sd_fi_fifo_un[i];
30445 	fi_arq = un->sd_fi_fifo_arq[i];
30446 
30447 
30448 	/* set variables accordingly */
30449 	/* set pkt if it was on fifo */
30450 	if (fi_pkt != NULL) {
30451 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30452 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30453 		if (fi_pkt->pkt_cdbp != 0xff)
30454 			SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30455 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30456 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30457 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30458 
30459 	}
30460 	/* set xb if it was on fifo */
30461 	if (fi_xb != NULL) {
30462 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30463 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30464 		if (fi_xb->xb_retry_count != 0)
30465 			SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30466 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30467 		    "xb_victim_retry_count");
30468 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30469 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30470 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30471 
30472 		/* copy in block data from sense */
30473 		/*
30474 		 * if (fi_xb->xb_sense_data[0] != -1) {
30475 		 *	bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30476 		 *	SENSE_LENGTH);
30477 		 * }
30478 		 */
30479 		bcopy(fi_xb->xb_sense_data, xb->xb_sense_data, SENSE_LENGTH);
30480 
30481 		/* copy in extended sense codes */
30482 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30483 		    xb, es_code, "es_code");
30484 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30485 		    xb, es_key, "es_key");
30486 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30487 		    xb, es_add_code, "es_add_code");
30488 		SD_CONDSET(((struct scsi_extended_sense *)xb->xb_sense_data),
30489 		    xb, es_qual_code, "es_qual_code");
30490 		struct scsi_extended_sense *esp;
30491 		esp = (struct scsi_extended_sense *)xb->xb_sense_data;
30492 		esp->es_class = CLASS_EXTENDED_SENSE;
30493 	}
30494 
30495 	/* set un if it was on fifo */
30496 	if (fi_un != NULL) {
30497 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30498 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30499 		SD_CONDSET(un, un, un_reset_retry_count,
30500 		    "un_reset_retry_count");
30501 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30502 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30503 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30504 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30505 		    "un_f_allow_bus_device_reset");
30506 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30507 
30508 	}
30509 
30510 	/* copy in auto request sense if it was on fifo */
30511 	if (fi_arq != NULL) {
30512 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30513 	}
30514 
30515 	/* free structs */
30516 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30517 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30518 	}
30519 	if (un->sd_fi_fifo_xb[i] != NULL) {
30520 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30521 	}
30522 	if (un->sd_fi_fifo_un[i] != NULL) {
30523 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30524 	}
30525 	if (un->sd_fi_fifo_arq[i] != NULL) {
30526 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30527 	}
30528 
30529 	/*
30530 	 * kmem_free does not gurantee to set to NULL
30531 	 * since we uses these to determine if we set
30532 	 * values or not lets confirm they are always
30533 	 * NULL after free
30534 	 */
30535 	un->sd_fi_fifo_pkt[i] = NULL;
30536 	un->sd_fi_fifo_un[i] = NULL;
30537 	un->sd_fi_fifo_xb[i] = NULL;
30538 	un->sd_fi_fifo_arq[i] = NULL;
30539 
30540 	un->sd_fi_fifo_start++;
30541 
30542 	mutex_exit(SD_MUTEX(un));
30543 
30544 	SD_INFO(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30545 }
30546 
30547 #endif /* SD_FAULT_INJECTION */
30548 
30549 /*
30550  * This routine is invoked in sd_unit_attach(). Before calling it, the
30551  * properties in conf file should be processed already, and "hotpluggable"
30552  * property was processed also.
30553  *
30554  * The sd driver distinguishes 3 different type of devices: removable media,
30555  * non-removable media, and hotpluggable. Below the differences are defined:
30556  *
30557  * 1. Device ID
30558  *
30559  *     The device ID of a device is used to identify this device. Refer to
30560  *     ddi_devid_register(9F).
30561  *
30562  *     For a non-removable media disk device which can provide 0x80 or 0x83
30563  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30564  *     device ID is created to identify this device. For other non-removable
30565  *     media devices, a default device ID is created only if this device has
30566  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30567  *
30568  *     -------------------------------------------------------
30569  *     removable media   hotpluggable  | Can Have Device ID
30570  *     -------------------------------------------------------
30571  *         false             false     |     Yes
30572  *         false             true      |     Yes
30573  *         true                x       |     No
30574  *     ------------------------------------------------------
30575  *
30576  *
30577  * 2. SCSI group 4 commands
30578  *
30579  *     In SCSI specs, only some commands in group 4 command set can use
30580  *     8-byte addresses that can be used to access >2TB storage spaces.
30581  *     Other commands have no such capability. Without supporting group4,
30582  *     it is impossible to make full use of storage spaces of a disk with
30583  *     capacity larger than 2TB.
30584  *
30585  *     -----------------------------------------------
30586  *     removable media   hotpluggable   LP64  |  Group
30587  *     -----------------------------------------------
30588  *           false          false       false |   1
30589  *           false          false       true  |   4
30590  *           false          true        false |   1
30591  *           false          true        true  |   4
30592  *           true             x           x   |   5
30593  *     -----------------------------------------------
30594  *
30595  *
30596  * 3. Check for VTOC Label
30597  *
30598  *     If a direct-access disk has no EFI label, sd will check if it has a
30599  *     valid VTOC label. Now, sd also does that check for removable media
30600  *     and hotpluggable devices.
30601  *
30602  *     --------------------------------------------------------------
30603  *     Direct-Access   removable media    hotpluggable |  Check Label
30604  *     -------------------------------------------------------------
30605  *         false          false           false        |   No
30606  *         false          false           true         |   No
30607  *         false          true            false        |   Yes
30608  *         false          true            true         |   Yes
30609  *         true            x                x          |   Yes
30610  *     --------------------------------------------------------------
30611  *
30612  *
30613  * 4. Building default VTOC label
30614  *
30615  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30616  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30617  *     create default VTOC for them. Currently sd creates default VTOC label
30618  *     for all devices on x86 platform (VTOC_16), but only for removable
30619  *     media devices on SPARC (VTOC_8).
30620  *
30621  *     -----------------------------------------------------------
30622  *       removable media hotpluggable platform   |   Default Label
30623  *     -----------------------------------------------------------
30624  *             false          false    sparc     |     No
30625  *             false          true      x86      |     Yes
30626  *             false          true     sparc     |     Yes
30627  *             true             x        x       |     Yes
30628  *     ----------------------------------------------------------
30629  *
30630  *
30631  * 5. Supported blocksizes of target devices
30632  *
30633  *     Sd supports non-512-byte blocksize for removable media devices only.
30634  *     For other devices, only 512-byte blocksize is supported. This may be
30635  *     changed in near future because some RAID devices require non-512-byte
30636  *     blocksize
30637  *
30638  *     -----------------------------------------------------------
30639  *     removable media    hotpluggable    | non-512-byte blocksize
30640  *     -----------------------------------------------------------
30641  *           false          false         |   No
30642  *           false          true          |   No
30643  *           true             x           |   Yes
30644  *     -----------------------------------------------------------
30645  *
30646  *
30647  * 6. Automatic mount & unmount
30648  *
30649  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30650  *     if a device is removable media device. It return 1 for removable media
30651  *     devices, and 0 for others.
30652  *
30653  *     The automatic mounting subsystem should distinguish between the types
30654  *     of devices and apply automounting policies to each.
30655  *
30656  *
30657  * 7. fdisk partition management
30658  *
30659  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30660  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30661  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30662  *     fdisk partitions on both x86 and SPARC platform.
30663  *
30664  *     -----------------------------------------------------------
30665  *       platform   removable media  USB/1394  |  fdisk supported
30666  *     -----------------------------------------------------------
30667  *        x86         X               X        |       true
30668  *     ------------------------------------------------------------
30669  *        sparc       X               X        |       false
30670  *     ------------------------------------------------------------
30671  *
30672  *
30673  * 8. MBOOT/MBR
30674  *
30675  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30676  *     read/write mboot for removable media devices on sparc platform.
30677  *
30678  *     -----------------------------------------------------------
30679  *       platform   removable media  USB/1394  |  mboot supported
30680  *     -----------------------------------------------------------
30681  *        x86         X               X        |       true
30682  *     ------------------------------------------------------------
30683  *        sparc      false           false     |       false
30684  *        sparc      false           true      |       true
30685  *        sparc      true            false     |       true
30686  *        sparc      true            true      |       true
30687  *     ------------------------------------------------------------
30688  *
30689  *
30690  * 9.  error handling during opening device
30691  *
30692  *     If failed to open a disk device, an errno is returned. For some kinds
30693  *     of errors, different errno is returned depending on if this device is
30694  *     a removable media device. This brings USB/1394 hard disks in line with
30695  *     expected hard disk behavior. It is not expected that this breaks any
30696  *     application.
30697  *
30698  *     ------------------------------------------------------
30699  *       removable media    hotpluggable   |  errno
30700  *     ------------------------------------------------------
30701  *             false          false        |   EIO
30702  *             false          true         |   EIO
30703  *             true             x          |   ENXIO
30704  *     ------------------------------------------------------
30705  *
30706  *
30707  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30708  *
30709  *     These IOCTLs are applicable only to removable media devices.
30710  *
30711  *     -----------------------------------------------------------
30712  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30713  *     -----------------------------------------------------------
30714  *             false          false        |     No
30715  *             false          true         |     No
30716  *             true            x           |     Yes
30717  *     -----------------------------------------------------------
30718  *
30719  *
30720  * 12. Kstats for partitions
30721  *
30722  *     sd creates partition kstat for non-removable media devices. USB and
30723  *     Firewire hard disks now have partition kstats
30724  *
30725  *      ------------------------------------------------------
30726  *       removable media    hotpluggable   |   kstat
30727  *      ------------------------------------------------------
30728  *             false          false        |    Yes
30729  *             false          true         |    Yes
30730  *             true             x          |    No
30731  *       ------------------------------------------------------
30732  *
30733  *
30734  * 13. Removable media & hotpluggable properties
30735  *
30736  *     Sd driver creates a "removable-media" property for removable media
30737  *     devices. Parent nexus drivers create a "hotpluggable" property if
30738  *     it supports hotplugging.
30739  *
30740  *     ---------------------------------------------------------------------
30741  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30742  *     ---------------------------------------------------------------------
30743  *       false            false       |    No                   No
30744  *       false            true        |    No                   Yes
30745  *       true             false       |    Yes                  No
30746  *       true             true        |    Yes                  Yes
30747  *     ---------------------------------------------------------------------
30748  *
30749  *
30750  * 14. Power Management
30751  *
30752  *     sd only power manages removable media devices or devices that support
30753  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30754  *
30755  *     A parent nexus that supports hotplugging can also set "pm-capable"
30756  *     if the disk can be power managed.
30757  *
30758  *     ------------------------------------------------------------
30759  *       removable media hotpluggable pm-capable  |   power manage
30760  *     ------------------------------------------------------------
30761  *             false          false     false     |     No
30762  *             false          false     true      |     Yes
30763  *             false          true      false     |     No
30764  *             false          true      true      |     Yes
30765  *             true             x        x        |     Yes
30766  *     ------------------------------------------------------------
30767  *
30768  *      USB and firewire hard disks can now be power managed independently
30769  *      of the framebuffer
30770  *
30771  *
30772  * 15. Support for USB disks with capacity larger than 1TB
30773  *
30774  *     Currently, sd doesn't permit a fixed disk device with capacity
30775  *     larger than 1TB to be used in a 32-bit operating system environment.
30776  *     However, sd doesn't do that for removable media devices. Instead, it
30777  *     assumes that removable media devices cannot have a capacity larger
30778  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30779  *     supported, which can cause some unexpected results.
30780  *
30781  *     ---------------------------------------------------------------------
30782  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30783  *     ---------------------------------------------------------------------
30784  *             false          false  |   true         |     no
30785  *             false          true   |   true         |     no
30786  *             true           false  |   true         |     Yes
30787  *             true           true   |   true         |     Yes
30788  *     ---------------------------------------------------------------------
30789  *
30790  *
30791  * 16. Check write-protection at open time
30792  *
30793  *     When a removable media device is being opened for writing without NDELAY
30794  *     flag, sd will check if this device is writable. If attempting to open
30795  *     without NDELAY flag a write-protected device, this operation will abort.
30796  *
30797  *     ------------------------------------------------------------
30798  *       removable media    USB/1394   |   WP Check
30799  *     ------------------------------------------------------------
30800  *             false          false    |     No
30801  *             false          true     |     No
30802  *             true           false    |     Yes
30803  *             true           true     |     Yes
30804  *     ------------------------------------------------------------
30805  *
30806  *
30807  * 17. syslog when corrupted VTOC is encountered
30808  *
30809  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30810  *      for fixed SCSI disks.
30811  *     ------------------------------------------------------------
30812  *       removable media    USB/1394   |   print syslog
30813  *     ------------------------------------------------------------
30814  *             false          false    |     Yes
30815  *             false          true     |     No
30816  *             true           false    |     No
30817  *             true           true     |     No
30818  *     ------------------------------------------------------------
30819  */
30820 static void
30821 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30822 {
30823 	int	pm_cap;
30824 
30825 	ASSERT(un->un_sd);
30826 	ASSERT(un->un_sd->sd_inq);
30827 
30828 	/*
30829 	 * Enable SYNC CACHE support for all devices.
30830 	 */
30831 	un->un_f_sync_cache_supported = TRUE;
30832 
30833 	/*
30834 	 * Set the sync cache required flag to false.
30835 	 * This would ensure that there is no SYNC CACHE
30836 	 * sent when there are no writes
30837 	 */
30838 	un->un_f_sync_cache_required = FALSE;
30839 
30840 	if (un->un_sd->sd_inq->inq_rmb) {
30841 		/*
30842 		 * The media of this device is removable. And for this kind
30843 		 * of devices, it is possible to change medium after opening
30844 		 * devices. Thus we should support this operation.
30845 		 */
30846 		un->un_f_has_removable_media = TRUE;
30847 
30848 		/*
30849 		 * support non-512-byte blocksize of removable media devices
30850 		 */
30851 		un->un_f_non_devbsize_supported = TRUE;
30852 
30853 		/*
30854 		 * Assume that all removable media devices support DOOR_LOCK
30855 		 */
30856 		un->un_f_doorlock_supported = TRUE;
30857 
30858 		/*
30859 		 * For a removable media device, it is possible to be opened
30860 		 * with NDELAY flag when there is no media in drive, in this
30861 		 * case we don't care if device is writable. But if without
30862 		 * NDELAY flag, we need to check if media is write-protected.
30863 		 */
30864 		un->un_f_chk_wp_open = TRUE;
30865 
30866 		/*
30867 		 * need to start a SCSI watch thread to monitor media state,
30868 		 * when media is being inserted or ejected, notify syseventd.
30869 		 */
30870 		un->un_f_monitor_media_state = TRUE;
30871 
30872 		/*
30873 		 * Some devices don't support START_STOP_UNIT command.
30874 		 * Therefore, we'd better check if a device supports it
30875 		 * before sending it.
30876 		 */
30877 		un->un_f_check_start_stop = TRUE;
30878 
30879 		/*
30880 		 * support eject media ioctl:
30881 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
30882 		 */
30883 		un->un_f_eject_media_supported = TRUE;
30884 
30885 		/*
30886 		 * Because many removable-media devices don't support
30887 		 * LOG_SENSE, we couldn't use this command to check if
30888 		 * a removable media device support power-management.
30889 		 * We assume that they support power-management via
30890 		 * START_STOP_UNIT command and can be spun up and down
30891 		 * without limitations.
30892 		 */
30893 		un->un_f_pm_supported = TRUE;
30894 
30895 		/*
30896 		 * Need to create a zero length (Boolean) property
30897 		 * removable-media for the removable media devices.
30898 		 * Note that the return value of the property is not being
30899 		 * checked, since if unable to create the property
30900 		 * then do not want the attach to fail altogether. Consistent
30901 		 * with other property creation in attach.
30902 		 */
30903 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30904 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30905 
30906 	} else {
30907 		/*
30908 		 * create device ID for device
30909 		 */
30910 		un->un_f_devid_supported = TRUE;
30911 
30912 		/*
30913 		 * Spin up non-removable-media devices once it is attached
30914 		 */
30915 		un->un_f_attach_spinup = TRUE;
30916 
30917 		/*
30918 		 * According to SCSI specification, Sense data has two kinds of
30919 		 * format: fixed format, and descriptor format. At present, we
30920 		 * don't support descriptor format sense data for removable
30921 		 * media.
30922 		 */
30923 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30924 			un->un_f_descr_format_supported = TRUE;
30925 		}
30926 
30927 		/*
30928 		 * kstats are created only for non-removable media devices.
30929 		 *
30930 		 * Set this in sd.conf to 0 in order to disable kstats.  The
30931 		 * default is 1, so they are enabled by default.
30932 		 */
30933 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
30934 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
30935 		    "enable-partition-kstats", 1));
30936 
30937 		/*
30938 		 * Check if HBA has set the "pm-capable" property.
30939 		 * If "pm-capable" exists and is non-zero then we can
30940 		 * power manage the device without checking the start/stop
30941 		 * cycle count log sense page.
30942 		 *
30943 		 * If "pm-capable" exists and is set to be false (0),
30944 		 * then we should not power manage the device.
30945 		 *
30946 		 * If "pm-capable" doesn't exist then pm_cap will
30947 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
30948 		 * sd will check the start/stop cycle count log sense page
30949 		 * and power manage the device if the cycle count limit has
30950 		 * not been exceeded.
30951 		 */
30952 		pm_cap = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
30953 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
30954 		if (SD_PM_CAPABLE_IS_UNDEFINED(pm_cap)) {
30955 			un->un_f_log_sense_supported = TRUE;
30956 			if (!un->un_f_power_condition_disabled &&
30957 			    SD_INQUIRY(un)->inq_ansi == 6) {
30958 				un->un_f_power_condition_supported = TRUE;
30959 			}
30960 		} else {
30961 			/*
30962 			 * pm-capable property exists.
30963 			 *
30964 			 * Convert "TRUE" values for pm_cap to
30965 			 * SD_PM_CAPABLE_IS_TRUE to make it easier to check
30966 			 * later. "TRUE" values are any values defined in
30967 			 * inquiry.h.
30968 			 */
30969 			if (SD_PM_CAPABLE_IS_FALSE(pm_cap)) {
30970 				un->un_f_log_sense_supported = FALSE;
30971 			} else {
30972 				/* SD_PM_CAPABLE_IS_TRUE case */
30973 				un->un_f_pm_supported = TRUE;
30974 				if (!un->un_f_power_condition_disabled &&
30975 				    SD_PM_CAPABLE_IS_SPC_4(pm_cap)) {
30976 					un->un_f_power_condition_supported =
30977 					    TRUE;
30978 				}
30979 				if (SD_PM_CAP_LOG_SUPPORTED(pm_cap)) {
30980 					un->un_f_log_sense_supported = TRUE;
30981 					un->un_f_pm_log_sense_smart =
30982 					    SD_PM_CAP_SMART_LOG(pm_cap);
30983 				}
30984 			}
30985 
30986 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
30987 			    "sd_unit_attach: un:0x%p pm-capable "
30988 			    "property set to %d.\n", un, un->un_f_pm_supported);
30989 		}
30990 	}
30991 
30992 	if (un->un_f_is_hotpluggable) {
30993 
30994 		/*
30995 		 * Have to watch hotpluggable devices as well, since
30996 		 * that's the only way for userland applications to
30997 		 * detect hot removal while device is busy/mounted.
30998 		 */
30999 		un->un_f_monitor_media_state = TRUE;
31000 
31001 		un->un_f_check_start_stop = TRUE;
31002 
31003 	}
31004 }
31005 
31006 /*
31007  * sd_tg_rdwr:
31008  * Provides rdwr access for cmlb via sd_tgops. The start_block is
31009  * in sys block size, req_length in bytes.
31010  *
31011  */
31012 static int
31013 sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
31014     diskaddr_t start_block, size_t reqlength, void *tg_cookie)
31015 {
31016 	struct sd_lun *un;
31017 	int path_flag = (int)(uintptr_t)tg_cookie;
31018 	char *dkl = NULL;
31019 	diskaddr_t real_addr = start_block;
31020 	diskaddr_t first_byte, end_block;
31021 
31022 	size_t	buffer_size = reqlength;
31023 	int rval = 0;
31024 	diskaddr_t	cap;
31025 	uint32_t	lbasize;
31026 	sd_ssc_t	*ssc;
31027 
31028 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31029 	if (un == NULL)
31030 		return (ENXIO);
31031 
31032 	if (cmd != TG_READ && cmd != TG_WRITE)
31033 		return (EINVAL);
31034 
31035 	ssc = sd_ssc_init(un);
31036 	mutex_enter(SD_MUTEX(un));
31037 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
31038 		mutex_exit(SD_MUTEX(un));
31039 		rval = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31040 		    &lbasize, path_flag);
31041 		if (rval != 0)
31042 			goto done1;
31043 		mutex_enter(SD_MUTEX(un));
31044 		sd_update_block_info(un, lbasize, cap);
31045 		if ((un->un_f_tgt_blocksize_is_valid == FALSE)) {
31046 			mutex_exit(SD_MUTEX(un));
31047 			rval = EIO;
31048 			goto done;
31049 		}
31050 	}
31051 
31052 	if (NOT_DEVBSIZE(un)) {
31053 		/*
31054 		 * sys_blocksize != tgt_blocksize, need to re-adjust
31055 		 * blkno and save the index to beginning of dk_label
31056 		 */
31057 		first_byte  = SD_SYSBLOCKS2BYTES(start_block);
31058 		real_addr = first_byte / un->un_tgt_blocksize;
31059 
31060 		end_block = (first_byte + reqlength +
31061 		    un->un_tgt_blocksize - 1) / un->un_tgt_blocksize;
31062 
31063 		/* round up buffer size to multiple of target block size */
31064 		buffer_size = (end_block - real_addr) * un->un_tgt_blocksize;
31065 
31066 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_tg_rdwr",
31067 		    "label_addr: 0x%x allocation size: 0x%x\n",
31068 		    real_addr, buffer_size);
31069 
31070 		if (((first_byte % un->un_tgt_blocksize) != 0) ||
31071 		    (reqlength % un->un_tgt_blocksize) != 0)
31072 			/* the request is not aligned */
31073 			dkl = kmem_zalloc(buffer_size, KM_SLEEP);
31074 	}
31075 
31076 	/*
31077 	 * The MMC standard allows READ CAPACITY to be
31078 	 * inaccurate by a bounded amount (in the interest of
31079 	 * response latency).  As a result, failed READs are
31080 	 * commonplace (due to the reading of metadata and not
31081 	 * data). Depending on the per-Vendor/drive Sense data,
31082 	 * the failed READ can cause many (unnecessary) retries.
31083 	 */
31084 
31085 	if (ISCD(un) && (cmd == TG_READ) &&
31086 	    (un->un_f_blockcount_is_valid == TRUE) &&
31087 	    ((start_block == (un->un_blockcount - 1))||
31088 	    (start_block == (un->un_blockcount - 2)))) {
31089 			path_flag = SD_PATH_DIRECT_PRIORITY;
31090 	}
31091 
31092 	mutex_exit(SD_MUTEX(un));
31093 	if (cmd == TG_READ) {
31094 		rval = sd_send_scsi_READ(ssc, (dkl != NULL)? dkl: bufaddr,
31095 		    buffer_size, real_addr, path_flag);
31096 		if (dkl != NULL)
31097 			bcopy(dkl + SD_TGTBYTEOFFSET(un, start_block,
31098 			    real_addr), bufaddr, reqlength);
31099 	} else {
31100 		if (dkl) {
31101 			rval = sd_send_scsi_READ(ssc, dkl, buffer_size,
31102 			    real_addr, path_flag);
31103 			if (rval) {
31104 				goto done1;
31105 			}
31106 			bcopy(bufaddr, dkl + SD_TGTBYTEOFFSET(un, start_block,
31107 			    real_addr), reqlength);
31108 		}
31109 		rval = sd_send_scsi_WRITE(ssc, (dkl != NULL)? dkl: bufaddr,
31110 		    buffer_size, real_addr, path_flag);
31111 	}
31112 
31113 done1:
31114 	if (dkl != NULL)
31115 		kmem_free(dkl, buffer_size);
31116 
31117 	if (rval != 0) {
31118 		if (rval == EIO)
31119 			sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
31120 		else
31121 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31122 	}
31123 done:
31124 	sd_ssc_fini(ssc);
31125 	return (rval);
31126 }
31127 
31128 
31129 static int
31130 sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie)
31131 {
31132 
31133 	struct sd_lun *un;
31134 	diskaddr_t	cap;
31135 	uint32_t	lbasize;
31136 	int		path_flag = (int)(uintptr_t)tg_cookie;
31137 	int		ret = 0;
31138 
31139 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
31140 	if (un == NULL)
31141 		return (ENXIO);
31142 
31143 	switch (cmd) {
31144 	case TG_GETPHYGEOM:
31145 	case TG_GETVIRTGEOM:
31146 	case TG_GETCAPACITY:
31147 	case TG_GETBLOCKSIZE:
31148 		mutex_enter(SD_MUTEX(un));
31149 
31150 		if ((un->un_f_blockcount_is_valid == TRUE) &&
31151 		    (un->un_f_tgt_blocksize_is_valid == TRUE)) {
31152 			cap = un->un_blockcount;
31153 			lbasize = un->un_tgt_blocksize;
31154 			mutex_exit(SD_MUTEX(un));
31155 		} else {
31156 			sd_ssc_t	*ssc;
31157 			mutex_exit(SD_MUTEX(un));
31158 			ssc = sd_ssc_init(un);
31159 			ret = sd_send_scsi_READ_CAPACITY(ssc, (uint64_t *)&cap,
31160 			    &lbasize, path_flag);
31161 			if (ret != 0) {
31162 				if (ret == EIO)
31163 					sd_ssc_assessment(ssc,
31164 					    SD_FMT_STATUS_CHECK);
31165 				else
31166 					sd_ssc_assessment(ssc,
31167 					    SD_FMT_IGNORE);
31168 				sd_ssc_fini(ssc);
31169 				return (ret);
31170 			}
31171 			sd_ssc_fini(ssc);
31172 			mutex_enter(SD_MUTEX(un));
31173 			sd_update_block_info(un, lbasize, cap);
31174 			if ((un->un_f_blockcount_is_valid == FALSE) ||
31175 			    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
31176 				mutex_exit(SD_MUTEX(un));
31177 				return (EIO);
31178 			}
31179 			mutex_exit(SD_MUTEX(un));
31180 		}
31181 
31182 		if (cmd == TG_GETCAPACITY) {
31183 			*(diskaddr_t *)arg = cap;
31184 			return (0);
31185 		}
31186 
31187 		if (cmd == TG_GETBLOCKSIZE) {
31188 			*(uint32_t *)arg = lbasize;
31189 			return (0);
31190 		}
31191 
31192 		if (cmd == TG_GETPHYGEOM)
31193 			ret = sd_get_physical_geometry(un, (cmlb_geom_t *)arg,
31194 			    cap, lbasize, path_flag);
31195 		else
31196 			/* TG_GETVIRTGEOM */
31197 			ret = sd_get_virtual_geometry(un,
31198 			    (cmlb_geom_t *)arg, cap, lbasize);
31199 
31200 		return (ret);
31201 
31202 	case TG_GETATTR:
31203 		mutex_enter(SD_MUTEX(un));
31204 		((tg_attribute_t *)arg)->media_is_writable =
31205 		    un->un_f_mmc_writable_media;
31206 		((tg_attribute_t *)arg)->media_is_solid_state =
31207 		    un->un_f_is_solid_state;
31208 		mutex_exit(SD_MUTEX(un));
31209 		return (0);
31210 	default:
31211 		return (ENOTTY);
31212 
31213 	}
31214 }
31215 
31216 /*
31217  *    Function: sd_ssc_ereport_post
31218  *
31219  * Description: Will be called when SD driver need to post an ereport.
31220  *
31221  *    Context: Kernel thread or interrupt context.
31222  */
31223 
31224 #define	DEVID_IF_KNOWN(d) "devid", DATA_TYPE_STRING, (d) ? (d) : "unknown"
31225 
31226 static void
31227 sd_ssc_ereport_post(sd_ssc_t *ssc, enum sd_driver_assessment drv_assess)
31228 {
31229 	int uscsi_path_instance = 0;
31230 	uchar_t	uscsi_pkt_reason;
31231 	uint32_t uscsi_pkt_state;
31232 	uint32_t uscsi_pkt_statistics;
31233 	uint64_t uscsi_ena;
31234 	uchar_t op_code;
31235 	uint8_t *sensep;
31236 	union scsi_cdb *cdbp;
31237 	uint_t cdblen = 0;
31238 	uint_t senlen = 0;
31239 	struct sd_lun *un;
31240 	dev_info_t *dip;
31241 	char *devid;
31242 	int ssc_invalid_flags = SSC_FLAGS_INVALID_PKT_REASON |
31243 	    SSC_FLAGS_INVALID_STATUS |
31244 	    SSC_FLAGS_INVALID_SENSE |
31245 	    SSC_FLAGS_INVALID_DATA;
31246 	char assessment[16];
31247 
31248 	ASSERT(ssc != NULL);
31249 	ASSERT(ssc->ssc_uscsi_cmd != NULL);
31250 	ASSERT(ssc->ssc_uscsi_info != NULL);
31251 
31252 	un = ssc->ssc_un;
31253 	ASSERT(un != NULL);
31254 
31255 	dip = un->un_sd->sd_dev;
31256 
31257 	/*
31258 	 * Get the devid:
31259 	 *	devid will only be passed to non-transport error reports.
31260 	 */
31261 	devid = DEVI(dip)->devi_devid_str;
31262 
31263 	/*
31264 	 * If we are syncing or dumping, the command will not be executed
31265 	 * so we bypass this situation.
31266 	 */
31267 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
31268 	    (un->un_state == SD_STATE_DUMPING))
31269 		return;
31270 
31271 	uscsi_pkt_reason = ssc->ssc_uscsi_info->ui_pkt_reason;
31272 	uscsi_path_instance = ssc->ssc_uscsi_cmd->uscsi_path_instance;
31273 	uscsi_pkt_state = ssc->ssc_uscsi_info->ui_pkt_state;
31274 	uscsi_pkt_statistics = ssc->ssc_uscsi_info->ui_pkt_statistics;
31275 	uscsi_ena = ssc->ssc_uscsi_info->ui_ena;
31276 
31277 	sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
31278 	cdbp = (union scsi_cdb *)ssc->ssc_uscsi_cmd->uscsi_cdb;
31279 
31280 	/* In rare cases, EG:DOORLOCK, the cdb could be NULL */
31281 	if (cdbp == NULL) {
31282 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
31283 		    "sd_ssc_ereport_post meet empty cdb\n");
31284 		return;
31285 	}
31286 
31287 	op_code = cdbp->scc_cmd;
31288 
31289 	cdblen = (int)ssc->ssc_uscsi_cmd->uscsi_cdblen;
31290 	senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
31291 	    ssc->ssc_uscsi_cmd->uscsi_rqresid);
31292 
31293 	if (senlen > 0)
31294 		ASSERT(sensep != NULL);
31295 
31296 	/*
31297 	 * Initialize drv_assess to corresponding values.
31298 	 * SD_FM_DRV_FATAL will be mapped to "fail" or "fatal" depending
31299 	 * on the sense-key returned back.
31300 	 */
31301 	switch (drv_assess) {
31302 		case SD_FM_DRV_RECOVERY:
31303 			(void) sprintf(assessment, "%s", "recovered");
31304 			break;
31305 		case SD_FM_DRV_RETRY:
31306 			(void) sprintf(assessment, "%s", "retry");
31307 			break;
31308 		case SD_FM_DRV_NOTICE:
31309 			(void) sprintf(assessment, "%s", "info");
31310 			break;
31311 		case SD_FM_DRV_FATAL:
31312 		default:
31313 			(void) sprintf(assessment, "%s", "unknown");
31314 	}
31315 	/*
31316 	 * If drv_assess == SD_FM_DRV_RECOVERY, this should be a recovered
31317 	 * command, we will post ereport.io.scsi.cmd.disk.recovered.
31318 	 * driver-assessment will always be "recovered" here.
31319 	 */
31320 	if (drv_assess == SD_FM_DRV_RECOVERY) {
31321 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31322 		    "cmd.disk.recovered", uscsi_ena, devid, NULL,
31323 		    DDI_NOSLEEP, NULL,
31324 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31325 		    DEVID_IF_KNOWN(devid),
31326 		    "driver-assessment", DATA_TYPE_STRING, assessment,
31327 		    "op-code", DATA_TYPE_UINT8, op_code,
31328 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31329 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31330 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31331 		    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31332 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31333 		    NULL);
31334 		return;
31335 	}
31336 
31337 	/*
31338 	 * If there is un-expected/un-decodable data, we should post
31339 	 * ereport.io.scsi.cmd.disk.dev.uderr.
31340 	 * driver-assessment will be set based on parameter drv_assess.
31341 	 * SSC_FLAGS_INVALID_SENSE - invalid sense data sent back.
31342 	 * SSC_FLAGS_INVALID_PKT_REASON - invalid pkt-reason encountered.
31343 	 * SSC_FLAGS_INVALID_STATUS - invalid stat-code encountered.
31344 	 * SSC_FLAGS_INVALID_DATA - invalid data sent back.
31345 	 */
31346 	if (ssc->ssc_flags & ssc_invalid_flags) {
31347 		if (ssc->ssc_flags & SSC_FLAGS_INVALID_SENSE) {
31348 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31349 			    NULL, "cmd.disk.dev.uderr", uscsi_ena, devid,
31350 			    NULL, DDI_NOSLEEP, NULL,
31351 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31352 			    DEVID_IF_KNOWN(devid),
31353 			    "driver-assessment", DATA_TYPE_STRING,
31354 			    drv_assess == SD_FM_DRV_FATAL ?
31355 			    "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_UINT32, uscsi_pkt_state,
31361 			    "pkt-stats", DATA_TYPE_UINT32,
31362 			    uscsi_pkt_statistics,
31363 			    "stat-code", DATA_TYPE_UINT8,
31364 			    ssc->ssc_uscsi_cmd->uscsi_status,
31365 			    "un-decode-info", DATA_TYPE_STRING,
31366 			    ssc->ssc_info,
31367 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31368 			    senlen, sensep,
31369 			    NULL);
31370 		} else {
31371 			/*
31372 			 * For other type of invalid data, the
31373 			 * un-decode-value field would be empty because the
31374 			 * un-decodable content could be seen from upper
31375 			 * level payload or inside un-decode-info.
31376 			 */
31377 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31378 			    NULL,
31379 			    "cmd.disk.dev.uderr", uscsi_ena, devid,
31380 			    NULL, DDI_NOSLEEP, NULL,
31381 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31382 			    DEVID_IF_KNOWN(devid),
31383 			    "driver-assessment", DATA_TYPE_STRING,
31384 			    drv_assess == SD_FM_DRV_FATAL ?
31385 			    "fail" : assessment,
31386 			    "op-code", DATA_TYPE_UINT8, op_code,
31387 			    "cdb", DATA_TYPE_UINT8_ARRAY,
31388 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31389 			    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31390 			    "pkt-state", DATA_TYPE_UINT32, uscsi_pkt_state,
31391 			    "pkt-stats", DATA_TYPE_UINT32,
31392 			    uscsi_pkt_statistics,
31393 			    "stat-code", DATA_TYPE_UINT8,
31394 			    ssc->ssc_uscsi_cmd->uscsi_status,
31395 			    "un-decode-info", DATA_TYPE_STRING,
31396 			    ssc->ssc_info,
31397 			    "un-decode-value", DATA_TYPE_UINT8_ARRAY,
31398 			    0, NULL,
31399 			    NULL);
31400 		}
31401 		ssc->ssc_flags &= ~ssc_invalid_flags;
31402 		return;
31403 	}
31404 
31405 	if (uscsi_pkt_reason != CMD_CMPLT ||
31406 	    (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)) {
31407 		/*
31408 		 * pkt-reason != CMD_CMPLT or SSC_FLAGS_TRAN_ABORT was
31409 		 * set inside sd_start_cmds due to errors(bad packet or
31410 		 * fatal transport error), we should take it as a
31411 		 * transport error, so we post ereport.io.scsi.cmd.disk.tran.
31412 		 * driver-assessment will be set based on drv_assess.
31413 		 * We will set devid to NULL because it is a transport
31414 		 * error.
31415 		 */
31416 		if (ssc->ssc_flags & SSC_FLAGS_TRAN_ABORT)
31417 			ssc->ssc_flags &= ~SSC_FLAGS_TRAN_ABORT;
31418 
31419 		scsi_fm_ereport_post(un->un_sd, uscsi_path_instance, NULL,
31420 		    "cmd.disk.tran", uscsi_ena, NULL, NULL, DDI_NOSLEEP, NULL,
31421 		    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31422 		    DEVID_IF_KNOWN(devid),
31423 		    "driver-assessment", DATA_TYPE_STRING,
31424 		    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31425 		    "op-code", DATA_TYPE_UINT8, op_code,
31426 		    "cdb", DATA_TYPE_UINT8_ARRAY,
31427 		    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31428 		    "pkt-reason", DATA_TYPE_UINT8, uscsi_pkt_reason,
31429 		    "pkt-state", DATA_TYPE_UINT8, uscsi_pkt_state,
31430 		    "pkt-stats", DATA_TYPE_UINT32, uscsi_pkt_statistics,
31431 		    NULL);
31432 	} else {
31433 		/*
31434 		 * If we got here, we have a completed command, and we need
31435 		 * to further investigate the sense data to see what kind
31436 		 * of ereport we should post.
31437 		 * No ereport is needed if sense-key is KEY_RECOVERABLE_ERROR
31438 		 * and asc/ascq is "ATA PASS-THROUGH INFORMATION AVAILABLE".
31439 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.merr if sense-key is
31440 		 * KEY_MEDIUM_ERROR.
31441 		 * Post ereport.io.scsi.cmd.disk.dev.rqs.derr otherwise.
31442 		 * driver-assessment will be set based on the parameter
31443 		 * drv_assess.
31444 		 */
31445 		if (senlen > 0) {
31446 			/*
31447 			 * Here we have sense data available.
31448 			 */
31449 			uint8_t sense_key = scsi_sense_key(sensep);
31450 			uint8_t sense_asc = scsi_sense_asc(sensep);
31451 			uint8_t sense_ascq = scsi_sense_ascq(sensep);
31452 
31453 			if (sense_key == KEY_RECOVERABLE_ERROR &&
31454 			    sense_asc == 0x00 && sense_ascq == 0x1d)
31455 				return;
31456 
31457 			if (sense_key == KEY_MEDIUM_ERROR) {
31458 				/*
31459 				 * driver-assessment should be "fatal" if
31460 				 * drv_assess is SD_FM_DRV_FATAL.
31461 				 */
31462 				scsi_fm_ereport_post(un->un_sd,
31463 				    uscsi_path_instance, NULL,
31464 				    "cmd.disk.dev.rqs.merr",
31465 				    uscsi_ena, devid, NULL, DDI_NOSLEEP, NULL,
31466 				    FM_VERSION, DATA_TYPE_UINT8,
31467 				    FM_EREPORT_VERS0,
31468 				    DEVID_IF_KNOWN(devid),
31469 				    "driver-assessment",
31470 				    DATA_TYPE_STRING,
31471 				    drv_assess == SD_FM_DRV_FATAL ?
31472 				    "fatal" : assessment,
31473 				    "op-code",
31474 				    DATA_TYPE_UINT8, op_code,
31475 				    "cdb",
31476 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31477 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31478 				    "pkt-reason",
31479 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31480 				    "pkt-state",
31481 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31482 				    "pkt-stats",
31483 				    DATA_TYPE_UINT32,
31484 				    uscsi_pkt_statistics,
31485 				    "stat-code",
31486 				    DATA_TYPE_UINT8,
31487 				    ssc->ssc_uscsi_cmd->uscsi_status,
31488 				    "key",
31489 				    DATA_TYPE_UINT8,
31490 				    scsi_sense_key(sensep),
31491 				    "asc",
31492 				    DATA_TYPE_UINT8,
31493 				    scsi_sense_asc(sensep),
31494 				    "ascq",
31495 				    DATA_TYPE_UINT8,
31496 				    scsi_sense_ascq(sensep),
31497 				    "sense-data",
31498 				    DATA_TYPE_UINT8_ARRAY,
31499 				    senlen, sensep,
31500 				    "lba",
31501 				    DATA_TYPE_UINT64,
31502 				    ssc->ssc_uscsi_info->ui_lba,
31503 				    NULL);
31504 			} else {
31505 				/*
31506 				 * if sense-key == 0x4(hardware
31507 				 * error), driver-assessment should
31508 				 * be "fatal" if drv_assess is
31509 				 * SD_FM_DRV_FATAL.
31510 				 */
31511 				scsi_fm_ereport_post(un->un_sd,
31512 				    uscsi_path_instance, NULL,
31513 				    "cmd.disk.dev.rqs.derr",
31514 				    uscsi_ena, devid,
31515 				    NULL, DDI_NOSLEEP, NULL,
31516 				    FM_VERSION,
31517 				    DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31518 				    DEVID_IF_KNOWN(devid),
31519 				    "driver-assessment",
31520 				    DATA_TYPE_STRING,
31521 				    drv_assess == SD_FM_DRV_FATAL ?
31522 				    (sense_key == 0x4 ?
31523 				    "fatal" : "fail") : assessment,
31524 				    "op-code",
31525 				    DATA_TYPE_UINT8, op_code,
31526 				    "cdb",
31527 				    DATA_TYPE_UINT8_ARRAY, cdblen,
31528 				    ssc->ssc_uscsi_cmd->uscsi_cdb,
31529 				    "pkt-reason",
31530 				    DATA_TYPE_UINT8, uscsi_pkt_reason,
31531 				    "pkt-state",
31532 				    DATA_TYPE_UINT8, uscsi_pkt_state,
31533 				    "pkt-stats",
31534 				    DATA_TYPE_UINT32,
31535 				    uscsi_pkt_statistics,
31536 				    "stat-code",
31537 				    DATA_TYPE_UINT8,
31538 				    ssc->ssc_uscsi_cmd->uscsi_status,
31539 				    "key",
31540 				    DATA_TYPE_UINT8,
31541 				    scsi_sense_key(sensep),
31542 				    "asc",
31543 				    DATA_TYPE_UINT8,
31544 				    scsi_sense_asc(sensep),
31545 				    "ascq",
31546 				    DATA_TYPE_UINT8,
31547 				    scsi_sense_ascq(sensep),
31548 				    "sense-data",
31549 				    DATA_TYPE_UINT8_ARRAY,
31550 				    senlen, sensep,
31551 				    NULL);
31552 			}
31553 		} else {
31554 			/*
31555 			 * For stat_code == STATUS_GOOD, this is not a
31556 			 * hardware error.
31557 			 */
31558 			if (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD)
31559 				return;
31560 
31561 			/*
31562 			 * Post ereport.io.scsi.cmd.disk.dev.serr if we got the
31563 			 * stat-code but with sense data unavailable.
31564 			 * driver-assessment will be set based on parameter
31565 			 * drv_assess.
31566 			 */
31567 			scsi_fm_ereport_post(un->un_sd, uscsi_path_instance,
31568 			    NULL,
31569 			    "cmd.disk.dev.serr", uscsi_ena,
31570 			    devid, NULL, DDI_NOSLEEP, NULL,
31571 			    FM_VERSION, DATA_TYPE_UINT8, FM_EREPORT_VERS0,
31572 			    DEVID_IF_KNOWN(devid),
31573 			    "driver-assessment", DATA_TYPE_STRING,
31574 			    drv_assess == SD_FM_DRV_FATAL ? "fail" : assessment,
31575 			    "op-code", DATA_TYPE_UINT8, op_code,
31576 			    "cdb",
31577 			    DATA_TYPE_UINT8_ARRAY,
31578 			    cdblen, ssc->ssc_uscsi_cmd->uscsi_cdb,
31579 			    "pkt-reason",
31580 			    DATA_TYPE_UINT8, uscsi_pkt_reason,
31581 			    "pkt-state",
31582 			    DATA_TYPE_UINT8, uscsi_pkt_state,
31583 			    "pkt-stats",
31584 			    DATA_TYPE_UINT32, uscsi_pkt_statistics,
31585 			    "stat-code",
31586 			    DATA_TYPE_UINT8,
31587 			    ssc->ssc_uscsi_cmd->uscsi_status,
31588 			    NULL);
31589 		}
31590 	}
31591 }
31592 
31593 /*
31594  *     Function: sd_ssc_extract_info
31595  *
31596  * Description: Extract information available to help generate ereport.
31597  *
31598  *     Context: Kernel thread or interrupt context.
31599  */
31600 static void
31601 sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un, struct scsi_pkt *pktp,
31602     struct buf *bp, struct sd_xbuf *xp)
31603 {
31604 	size_t senlen = 0;
31605 	union scsi_cdb *cdbp;
31606 	int path_instance;
31607 	/*
31608 	 * Need scsi_cdb_size array to determine the cdb length.
31609 	 */
31610 	extern uchar_t	scsi_cdb_size[];
31611 
31612 	ASSERT(un != NULL);
31613 	ASSERT(pktp != NULL);
31614 	ASSERT(bp != NULL);
31615 	ASSERT(xp != NULL);
31616 	ASSERT(ssc != NULL);
31617 	ASSERT(mutex_owned(SD_MUTEX(un)));
31618 
31619 	/*
31620 	 * Transfer the cdb buffer pointer here.
31621 	 */
31622 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
31623 
31624 	ssc->ssc_uscsi_cmd->uscsi_cdblen = scsi_cdb_size[GETGROUP(cdbp)];
31625 	ssc->ssc_uscsi_cmd->uscsi_cdb = (caddr_t)cdbp;
31626 
31627 	/*
31628 	 * Transfer the sense data buffer pointer if sense data is available,
31629 	 * calculate the sense data length first.
31630 	 */
31631 	if ((xp->xb_sense_state & STATE_XARQ_DONE) ||
31632 	    (xp->xb_sense_state & STATE_ARQ_DONE)) {
31633 		/*
31634 		 * For arq case, we will enter here.
31635 		 */
31636 		if (xp->xb_sense_state & STATE_XARQ_DONE) {
31637 			senlen = MAX_SENSE_LENGTH - xp->xb_sense_resid;
31638 		} else {
31639 			senlen = SENSE_LENGTH;
31640 		}
31641 	} else {
31642 		/*
31643 		 * For non-arq case, we will enter this branch.
31644 		 */
31645 		if (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK &&
31646 		    (xp->xb_sense_state & STATE_XFERRED_DATA)) {
31647 			senlen = SENSE_LENGTH - xp->xb_sense_resid;
31648 		}
31649 
31650 	}
31651 
31652 	ssc->ssc_uscsi_cmd->uscsi_rqlen = (senlen & 0xff);
31653 	ssc->ssc_uscsi_cmd->uscsi_rqresid = 0;
31654 	ssc->ssc_uscsi_cmd->uscsi_rqbuf = (caddr_t)xp->xb_sense_data;
31655 
31656 	ssc->ssc_uscsi_cmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
31657 
31658 	/*
31659 	 * Only transfer path_instance when scsi_pkt was properly allocated.
31660 	 */
31661 	path_instance = pktp->pkt_path_instance;
31662 	if (scsi_pkt_allocated_correctly(pktp) && path_instance)
31663 		ssc->ssc_uscsi_cmd->uscsi_path_instance = path_instance;
31664 	else
31665 		ssc->ssc_uscsi_cmd->uscsi_path_instance = 0;
31666 
31667 	/*
31668 	 * Copy in the other fields we may need when posting ereport.
31669 	 */
31670 	ssc->ssc_uscsi_info->ui_pkt_reason = pktp->pkt_reason;
31671 	ssc->ssc_uscsi_info->ui_pkt_state = pktp->pkt_state;
31672 	ssc->ssc_uscsi_info->ui_pkt_statistics = pktp->pkt_statistics;
31673 	ssc->ssc_uscsi_info->ui_lba = (uint64_t)SD_GET_BLKNO(bp);
31674 
31675 	/*
31676 	 * For partially read/write command, we will not create ena
31677 	 * in case of a successful command be reconized as recovered.
31678 	 */
31679 	if ((pktp->pkt_reason == CMD_CMPLT) &&
31680 	    (ssc->ssc_uscsi_cmd->uscsi_status == STATUS_GOOD) &&
31681 	    (senlen == 0)) {
31682 		return;
31683 	}
31684 
31685 	/*
31686 	 * To associate ereports of a single command execution flow, we
31687 	 * need a shared ena for a specific command.
31688 	 */
31689 	if (xp->xb_ena == 0)
31690 		xp->xb_ena = fm_ena_generate(0, FM_ENA_FMT1);
31691 	ssc->ssc_uscsi_info->ui_ena = xp->xb_ena;
31692 }
31693 
31694 
31695 /*
31696  *     Function: sd_check_solid_state
31697  *
31698  * Description: Query the optional INQUIRY VPD page 0xb1. If the device
31699  *              supports VPD page 0xb1, sd examines the MEDIUM ROTATION
31700  *              RATE. If the MEDIUM ROTATION RATE is 1, sd assumes the
31701  *              device is a solid state drive.
31702  *
31703  *     Context: Kernel thread or interrupt context.
31704  */
31705 
31706 static void
31707 sd_check_solid_state(sd_ssc_t *ssc)
31708 {
31709 	int		rval		= 0;
31710 	uchar_t		*inqb1		= NULL;
31711 	size_t		inqb1_len	= MAX_INQUIRY_SIZE;
31712 	size_t		inqb1_resid	= 0;
31713 	struct sd_lun	*un;
31714 
31715 	ASSERT(ssc != NULL);
31716 	un = ssc->ssc_un;
31717 	ASSERT(un != NULL);
31718 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31719 
31720 	mutex_enter(SD_MUTEX(un));
31721 	un->un_f_is_solid_state = FALSE;
31722 
31723 	if (ISCD(un)) {
31724 		mutex_exit(SD_MUTEX(un));
31725 		return;
31726 	}
31727 
31728 	if (sd_check_vpd_page_support(ssc) == 0 &&
31729 	    un->un_vpd_page_mask & SD_VPD_DEV_CHARACTER_PG) {
31730 		mutex_exit(SD_MUTEX(un));
31731 		/* collect page b1 data */
31732 		inqb1 = kmem_zalloc(inqb1_len, KM_SLEEP);
31733 
31734 		rval = sd_send_scsi_INQUIRY(ssc, inqb1, inqb1_len,
31735 		    0x01, 0xB1, &inqb1_resid);
31736 
31737 		if (rval == 0 && (inqb1_len - inqb1_resid > 5)) {
31738 			SD_TRACE(SD_LOG_COMMON, un,
31739 			    "sd_check_solid_state: \
31740 			    successfully get VPD page: %x \
31741 			    PAGE LENGTH: %x BYTE 4: %x \
31742 			    BYTE 5: %x", inqb1[1], inqb1[3], inqb1[4],
31743 			    inqb1[5]);
31744 
31745 			mutex_enter(SD_MUTEX(un));
31746 			/*
31747 			 * Check the MEDIUM ROTATION RATE. If it is set
31748 			 * to 1, the device is a solid state drive.
31749 			 */
31750 			if (inqb1[4] == 0 && inqb1[5] == 1) {
31751 				un->un_f_is_solid_state = TRUE;
31752 				/* solid state drives don't need disksort */
31753 				un->un_f_disksort_disabled = TRUE;
31754 			}
31755 			mutex_exit(SD_MUTEX(un));
31756 		} else if (rval != 0) {
31757 			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
31758 		}
31759 
31760 		kmem_free(inqb1, inqb1_len);
31761 	} else {
31762 		mutex_exit(SD_MUTEX(un));
31763 	}
31764 }
31765 
31766 /*
31767  *	Function: sd_check_emulation_mode
31768  *
31769  *   Description: Check whether the SSD is at emulation mode
31770  *		  by issuing READ_CAPACITY_16 to see whether
31771  *		  we can get physical block size of the drive.
31772  *
31773  *	 Context: Kernel thread or interrupt context.
31774  */
31775 
31776 static void
31777 sd_check_emulation_mode(sd_ssc_t *ssc)
31778 {
31779 	int		rval = 0;
31780 	uint64_t	capacity;
31781 	uint_t		lbasize;
31782 	uint_t		pbsize;
31783 	int		i;
31784 	int		devid_len;
31785 	struct sd_lun	*un;
31786 
31787 	ASSERT(ssc != NULL);
31788 	un = ssc->ssc_un;
31789 	ASSERT(un != NULL);
31790 	ASSERT(!mutex_owned(SD_MUTEX(un)));
31791 
31792 	mutex_enter(SD_MUTEX(un));
31793 	if (ISCD(un)) {
31794 		mutex_exit(SD_MUTEX(un));
31795 		return;
31796 	}
31797 
31798 	if (un->un_f_descr_format_supported) {
31799 		mutex_exit(SD_MUTEX(un));
31800 		rval = sd_send_scsi_READ_CAPACITY_16(ssc, &capacity, &lbasize,
31801 		    &pbsize, SD_PATH_DIRECT);
31802 		mutex_enter(SD_MUTEX(un));
31803 
31804 		if (rval != 0) {
31805 			un->un_phy_blocksize = DEV_BSIZE;
31806 		} else {
31807 			if (!ISP2(pbsize % DEV_BSIZE) || pbsize == 0) {
31808 				un->un_phy_blocksize = DEV_BSIZE;
31809 			} else if (pbsize > un->un_phy_blocksize) {
31810 				/*
31811 				 * Don't reset the physical blocksize
31812 				 * unless we've detected a larger value.
31813 				 */
31814 				un->un_phy_blocksize = pbsize;
31815 			}
31816 		}
31817 	}
31818 
31819 	for (i = 0; i < sd_flash_dev_table_size; i++) {
31820 		devid_len = (int)strlen(sd_flash_dev_table[i]);
31821 		if (sd_sdconf_id_match(un, sd_flash_dev_table[i], devid_len)
31822 		    == SD_SUCCESS) {
31823 			un->un_phy_blocksize = SSD_SECSIZE;
31824 			if (un->un_f_is_solid_state &&
31825 			    un->un_phy_blocksize != un->un_tgt_blocksize)
31826 				un->un_f_enable_rmw = TRUE;
31827 		}
31828 	}
31829 
31830 	mutex_exit(SD_MUTEX(un));
31831 }
31832